]> source.dussan.org Git - jgit.git/commitdiff
RefDirectory.pack: Only rely on packed refs from disk 21/200221/9
authorKaushik Lingarkar <quic_kaushikl@quicinc.com>
Mon, 27 Feb 2023 23:17:55 +0000 (15:17 -0800)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 30 Mar 2023 20:32:50 +0000 (22:32 +0200)
Since packed-refs is read from disk anyway, don't rely on the
in-memory copy as that is racy and if outdated, could result in
commit of pack-refs throwing an exception. This change also avoids
a possible unnecessary double read of packed-refs from disk.

Change-Id: I684a64991f53f8bdad58bbd248aae6522d11267d
Signed-off-by: Kaushik Lingarkar <quic_kaushikl@quicinc.com>
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java

index d381ddd41462eff20577c2d70531a6c81e2b7d7f..32eb067d36da89baa3877ce4dac9ae9313c9fd09 100644 (file)
@@ -746,13 +746,13 @@ public class RefDirectory extends RefDatabase {
                try {
                        LockFile lck = lockPackedRefsOrThrow();
                        try {
-                               final PackedRefList packed = getPackedRefs();
-                               RefList<Ref> cur = readPackedRefs();
+                               PackedRefList oldPacked = refreshPackedRefs();
+                               RefList<Ref> newPacked = oldPacked;
 
                                // Iterate over all refs to be packed
                                boolean dirty = false;
                                for (String refName : refs) {
-                                       Ref oldRef = readRef(refName, cur);
+                                       Ref oldRef = readRef(refName, newPacked);
                                        if (oldRef == null) {
                                                continue; // A non-existent ref is already correctly packed.
                                        }
@@ -769,11 +769,11 @@ public class RefDirectory extends RefDatabase {
                                        }
 
                                        dirty = true;
-                                       int idx = cur.find(refName);
+                                       int idx = newPacked.find(refName);
                                        if (idx >= 0) {
-                                               cur = cur.set(idx, newRef);
+                                               newPacked = newPacked.set(idx, newRef);
                                        } else {
-                                               cur = cur.add(idx, newRef);
+                                               newPacked = newPacked.add(idx, newRef);
                                        }
                                }
                                if (!dirty) {
@@ -782,7 +782,7 @@ public class RefDirectory extends RefDatabase {
                                }
 
                                // The new content for packed-refs is collected. Persist it.
-                               commitPackedRefs(lck, cur, packed,false);
+                               commitPackedRefs(lck, newPacked, oldPacked,false);
 
                                // Now delete the loose refs which are now packed
                                for (String refName : refs) {
@@ -809,7 +809,7 @@ public class RefDirectory extends RefDatabase {
                                                if (currentLooseRef == null || currentLooseRef.isSymbolic()) {
                                                        continue;
                                                }
-                                               Ref packedRef = cur.get(refName);
+                                               Ref packedRef = newPacked.get(refName);
                                                ObjectId clr_oid = currentLooseRef.getObjectId();
                                                if (clr_oid != null
                                                                && clr_oid.equals(packedRef.getObjectId())) {