From c0f89747b69a7919796f1ba185bb67cbbf1c8045 Mon Sep 17 00:00:00 2001 From: Minh Thai Date: Mon, 17 Sep 2018 14:55:45 -0700 Subject: [PATCH] Using getRefsByPrefix with multiple prefixes Change-Id: I9901e733797bd661f2485cc42914ad01699617d3 Signed-off-by: Minh Thai --- .../eclipse/jgit/api/ListBranchCommand.java | 22 +++++++++---------- .../eclipse/jgit/transport/UploadPack.java | 7 +++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java index 28a27a90e..29a51a0f0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/ListBranchCommand.java @@ -44,6 +44,10 @@ */ package org.eclipse.jgit.api; +import static org.eclipse.jgit.lib.Constants.HEAD; +import static org.eclipse.jgit.lib.Constants.R_HEADS; +import static org.eclipse.jgit.lib.Constants.R_REMOTES; + import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; @@ -56,7 +60,6 @@ import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.internal.JGitText; -import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; @@ -113,17 +116,18 @@ public class ListBranchCommand extends GitCommand> { Collection refs = new ArrayList<>(); // Also return HEAD if it's detached - Ref head = repo.exactRef(Constants.HEAD); - if (head != null && head.getLeaf().getName().equals(Constants.HEAD)) + Ref head = repo.exactRef(HEAD); + if (head != null && head.getLeaf().getName().equals(HEAD)) { refs.add(head); + } if (listMode == null) { - refs.addAll(getRefs(Constants.R_HEADS)); + refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS)); } else if (listMode == ListMode.REMOTE) { - refs.addAll(getRefs(Constants.R_REMOTES)); + refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_REMOTES)); } else { - refs.addAll(getRefs(Constants.R_HEADS)); - refs.addAll(getRefs(Constants.R_REMOTES)); + refs.addAll(repo.getRefDatabase().getRefsByPrefix(R_HEADS, + R_REMOTES)); } resultRefs = new ArrayList<>(filterRefs(refs)); } catch (IOException e) { @@ -185,8 +189,4 @@ public class ListBranchCommand extends GitCommand> { this.containsCommitish = containsCommitish; return this; } - - private Collection getRefs(String prefix) throws IOException { - return repo.getRefDatabase().getRefsByPrefix(prefix); - } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 3e54e2a5d..428b6edfe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -922,10 +922,9 @@ public class UploadPack { refsToSend = getAdvertisedOrDefaultRefs(); } else { refsToSend = new HashMap<>(); - for (String refPrefix : req.getRefPrefixes()) { - for (Ref ref : db.getRefDatabase().getRefsByPrefix(refPrefix)) { - refsToSend.put(ref.getName(), ref); - } + String[] prefixes = req.getRefPrefixes().toArray(new String[0]); + for (Ref ref : db.getRefDatabase().getRefsByPrefix(prefixes)) { + refsToSend.put(ref.getName(), ref); } } if (req.getSymrefs()) {