final Repository db;
try {
db = repoManager.openRepository(projectName);
} catch (RepositoryNotFoundException noGitRepository) {
return new ListBranchesResult(branches, false, true);
}
try {
final Map<String, Ref> all = db.getAllRefs();
if (!all.containsKey(Constants.HEAD)) {
// The branch pointed to by HEAD doesn't exist yet, so getAllRefs
// filtered it out. If we ask for it individually we can find the
// underlying target and put it into the map anyway.
//
try {
Ref head = db.getRef(Constants.HEAD);
if (head != null) {
all.put(Constants.HEAD, head);
}
} catch (IOException e) {
// Ignore the failure reading HEAD.
}
}
for (final Ref ref : all.values()) {
if (ref.isSymbolic()) {
targets.add(ref.getTarget().getName());
}
}
for (final Ref ref : all.values()) {
if (ref.isSymbolic()) {
// A symbolic reference to another branch, instead of
// showing the resolved value, show the name it references.
//
String target = ref.getTarget().getName();
RefControl targetRefControl = pctl.controlForRef(target);
if (!targetRefControl.isVisible()) {
continue;
}
if (target.startsWith(Constants.R_HEADS)) {
target = target.substring(Constants.R_HEADS.length());
}
Branch b = createBranch(ref.getName());
b.setRevision(new RevId(target));
if (Constants.HEAD.equals(ref.getName())) {
b.setCanDelete(false);
headBranch = b;
} else {
b.setCanDelete(targetRefControl.canDelete());
branches.add(b);
}
continue;
}
final RefControl refControl = pctl.controlForRef(ref.getName());
if (refControl.isVisible()) {
if (ref.getName().startsWith(Constants.R_HEADS)) {
branches.add(createBranch(ref, refControl, targets));
} else if (GitRepositoryManager.REF_CONFIG.equals(ref.getName())) {
configBranch = createBranch(ref, refControl, targets);
}
}
}
} finally {
db.close();
}
Collections.sort(branches, new Comparator<Branch>() {
@Override
public int compare(final Branch a, final Branch b) {
return a.getName().compareTo(b.getName());
}
});
if (configBranch != null) {
branches.add(0, configBranch);
}
if (headBranch != null) {
branches.add(0, headBranch);
}
return new ListBranchesResult(branches, pctl.canAddRefs(), false);
}