diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index 24d51a5ea..5d66a4fbd 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java @@ -647,16 +647,32 @@ public void pack(List refs) throws IOException { RefList cur = readPackedRefs(); // Iterate over all refs to be packed + boolean dirty = false; for (String refName : refs) { - Ref ref = readRef(refName, cur); - if (ref.isSymbolic()) + Ref oldRef = readRef(refName, cur); + if (oldRef.isSymbolic()) { continue; // can't pack symbolic refs + } // Add/Update it to packed-refs + Ref newRef = peeledPackedRef(oldRef); + if (newRef == oldRef) { + // No-op; peeledPackedRef returns the input ref only if it's already + // packed, and readRef returns a packed ref only if there is no loose + // ref. + continue; + } + + dirty = true; int idx = cur.find(refName); - if (idx >= 0) - cur = cur.set(idx, peeledPackedRef(ref)); - else - cur = cur.add(idx, peeledPackedRef(ref)); + if (idx >= 0) { + cur = cur.set(idx, newRef); + } else { + cur = cur.add(idx, newRef); + } + } + if (!dirty) { + // All requested refs were already packed accurately + return; } // The new content for packed-refs is collected. Persist it. diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchRefUpdate.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchRefUpdate.java index 3f6995de8..3043d4fcd 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchRefUpdate.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BatchRefUpdate.java @@ -462,7 +462,7 @@ public void execute(RevWalk walk, ProgressMonitor monitor, break SWITCH; } ru.setCheckConflicting(false); - addRefToPrefixes(takenPrefixes, cmd.getRefName()); + takenPrefixes.addAll(getPrefixes(cmd.getRefName())); takenNames.add(cmd.getRefName()); cmd.setResult(ru.update(walk)); } @@ -523,29 +523,26 @@ public void execute(RevWalk walk, ProgressMonitor monitor) execute(walk, monitor, null); } - private static Collection getTakenPrefixes( - final Collection names) { + private static Collection getTakenPrefixes(Collection names) { Collection ref = new HashSet<>(); - for (String name : names) - ref.addAll(getPrefixes(name)); - return ref; - } - - private static void addRefToPrefixes(Collection prefixes, - String name) { - for (String prefix : getPrefixes(name)) { - prefixes.add(prefix); + for (String name : names) { + addPrefixesTo(name, ref); } + return ref; } static Collection getPrefixes(String s) { Collection ret = new HashSet<>(); + addPrefixesTo(s, ret); + return ret; + } + + static void addPrefixesTo(String s, Collection out) { int p1 = s.indexOf('/'); while (p1 > 0) { - ret.add(s.substring(0, p1)); + out.add(s.substring(0, p1)); p1 = s.indexOf('/', p1 + 1); } - return ret; } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RefList.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RefList.java index 159781795..ce4b7c750 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RefList.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RefList.java @@ -338,10 +338,11 @@ public Builder() { * Create an empty list with at least the specified capacity. * * @param capacity - * the new capacity. + * the new capacity; if zero or negative, behavior is the same as + * {@link #Builder()}. */ public Builder(int capacity) { - list = new Ref[capacity]; + list = new Ref[Math.max(capacity, 16)]; } /** @return number of items in this builder's internal collection. */