From f6e56309ca2c0f857a671aadf26e0a8a3a1b95e6 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Tue, 17 Mar 2015 14:08:25 -0700 Subject: [PATCH] ListBranches: Handle HEAD and refs/meta/config in the Comparator This simplifies the loop body somewhat as we can just add these refs wherever. It also avoids extra copying inherent in adding to the front of the ArrayList. Factor out a Comparator class as well. Change-Id: I21b0ca0b4840349cc833f9f6166c1743d80aedcc --- .../gerrit/server/project/ListBranches.java | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/project/ListBranches.java b/gerrit-server/src/main/java/com/google/gerrit/server/project/ListBranches.java index 4af0f0af89..689e25fb85 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/project/ListBranches.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/project/ListBranches.java @@ -16,6 +16,7 @@ package com.google.gerrit.server.project; import com.google.common.base.Predicate; import com.google.common.base.Strings; +import com.google.common.collect.ComparisonChain; import com.google.common.collect.FluentIterable; import com.google.common.collect.Sets; import com.google.gerrit.extensions.common.ActionInfo; @@ -83,9 +84,6 @@ public class ListBranches implements RestReadView { public List apply(ProjectResource rsrc) throws ResourceNotFoundException, IOException, BadRequestException { List branchList; - BranchInfo headBranch = null; - BranchInfo configBranch = null; - try (Repository db = repoManager.openRepository(rsrc.getNameKey())) { Collection heads = db.getRefDatabase().getRefs(Constants.R_HEADS).values(); @@ -117,41 +115,23 @@ public class ListBranches implements RestReadView { } BranchInfo b = new BranchInfo(ref.getName(), target, false); + branchList.add(b); - if (Constants.HEAD.equals(ref.getName())) { - headBranch = b; - } else { + if (!Constants.HEAD.equals(ref.getName())) { b.setCanDelete(targetRefControl.canDelete()); - branchList.add(b); } continue; } RefControl refControl = rsrc.getControl().controlForRef(ref.getName()); if (refControl.isVisible()) { - if (RefNames.REFS_CONFIG.equals(ref.getName())) { - configBranch = createBranchInfo(ref, refControl, targets); - } else { - branchList.add(createBranchInfo(ref, refControl, targets)); - } + branchList.add(createBranchInfo(ref, refControl, targets)); } } } catch (RepositoryNotFoundException noGitRepository) { throw new ResourceNotFoundException(); } - Collections.sort(branchList, new Comparator() { - @Override - public int compare(BranchInfo a, BranchInfo b) { - return a.ref.compareTo(b.ref); - } - }); - if (configBranch != null) { - branchList.add(0, configBranch); - } - if (headBranch != null) { - branchList.add(0, headBranch); - } - + Collections.sort(branchList, new BranchComparator()); FluentIterable branches = filterBranches(branchList); if (start > 0) { branches = branches.skip(start); @@ -162,6 +142,25 @@ public class ListBranches implements RestReadView { return branches.toList(); } + private static class BranchComparator implements Comparator { + @Override + public int compare(BranchInfo a, BranchInfo b) { + return ComparisonChain.start() + .compareTrueFirst(isHead(a), isHead(b)) + .compareTrueFirst(isConfig(a), isConfig(b)) + .compare(a.ref, b.ref) + .result(); + } + + private static boolean isHead(BranchInfo i) { + return Constants.HEAD.equals(i.ref); + } + + private static boolean isConfig(BranchInfo i) { + return RefNames.REFS_CONFIG.equals(i.ref); + } + } + private static void addRef(Repository db, List refs, String name) throws IOException { Ref ref = db.getRef(name);