in org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java [250:386]
public String mapCommitToRef(Repository repository, String commitId,
boolean refresh) {
synchronized (commitMappingCache) {
if (!ObjectId.isId(commitId)) {
return null;
}
try {
List<ReflogEntry> lastEntry = safeReadReflog(repository,
Constants.HEAD);
for (ReflogEntry entry : lastEntry) {
if (entry.getNewId().name().equals(commitId)) {
CheckoutEntry checkoutEntry = entry.parseCheckout();
if (checkoutEntry != null) {
Ref ref = repository
.findRef(checkoutEntry.getToBranch());
if (ref != null) {
ObjectId objectId = ref.getObjectId();
if (objectId != null && objectId.getName()
.equals(commitId)) {
return checkoutEntry.getToBranch();
}
ref = repository.getRefDatabase().peel(ref);
}
if (ref != null) {
ObjectId id = ref.getPeeledObjectId();
if (id != null
&& id.getName().equals(commitId)) {
return checkoutEntry.getToBranch();
}
}
}
}
}
} catch (IOException e) {
// ignore here
}
Map<String, String> cacheEntry = commitMappingCache.get(repository
.getDirectory().toString());
if (!refresh && cacheEntry != null
&& cacheEntry.containsKey(commitId)) {
// this may be null in fact
return cacheEntry.get(commitId);
}
if (cacheEntry == null) {
cacheEntry = new HashMap<>();
commitMappingCache.put(repository.getDirectory().getPath(),
cacheEntry);
} else {
cacheEntry.clear();
}
Map<String, Date> tagMap = new HashMap<>();
try (RevWalk rw = new RevWalk(repository)) {
List<Ref> tags = repository.getRefDatabase().getRefsByPrefix(
Constants.R_TAGS);
for (Ref tagRef : tags) {
ObjectId id = tagRef.getLeaf().getObjectId();
if (id == null) {
continue;
}
RevObject any = rw.parseAny(id);
if (any instanceof RevTag) {
RevTag tag = (RevTag) any;
if (tag.getObject().name().equals(commitId)) {
Date timestamp;
if (tag.getTaggerIdent() != null) {
timestamp = tag.getTaggerIdent().getWhen();
} else {
try {
RevCommit commit = rw.parseCommit(tag.getObject());
timestamp = commit.getCommitterIdent().getWhen();
} catch (IncorrectObjectTypeException e) {
// not referencing a commit
timestamp = null;
}
}
tagMap.put(tagRef.getName(), timestamp);
}
} else if (any instanceof RevCommit) {
RevCommit commit = ((RevCommit)any);
if (commit.name().equals(commitId))
tagMap.put(tagRef.getName(), commit.getCommitterIdent().getWhen());
} // else ignore here
}
} catch (IOException e) {
// ignore here
}
String cacheValue = null;
if (!tagMap.isEmpty()) {
// we try to obtain the "latest" tag
Date compareDate = new Date(0);
for (Map.Entry<String, Date> tagEntry : tagMap.entrySet()) {
if (tagEntry.getValue() != null
&& tagEntry.getValue().after(compareDate)) {
compareDate = tagEntry.getValue();
cacheValue = tagEntry.getKey();
}
}
// if we don't have time stamps, we sort
if (cacheValue == null) {
for (String tagName : tagMap.keySet()) {
if (cacheValue == null
|| cacheValue.compareTo(tagName) < 0) {
cacheValue = tagName;
}
}
}
}
if (cacheValue == null) {
// we didnt't find a tag, so let's look for local branches
try {
cacheValue = lastRefNameForCommitId(repository,
Constants.R_HEADS, commitId);
} catch (IOException e) {
// ignore here
}
}
if (cacheValue == null) {
// last try: remote branches
try {
cacheValue = lastRefNameForCommitId(repository,
Constants.R_REMOTES, commitId);
} catch (IOException e) {
// ignore here
}
}
cacheEntry.put(commitId, cacheValue);
return cacheValue;
}
}