From 4f7627be24cc9115323f6dba4663f376179c59e2 Mon Sep 17 00:00:00 2001 From: Kaushik Lingarkar Date: Mon, 27 Feb 2023 15:17:55 -0800 Subject: [PATCH] RefDirectory.pack: Only rely on packed refs from disk 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 --- .../jgit/internal/storage/file/RefDirectory.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 d381ddd414..32eb067d36 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 @@ -746,13 +746,13 @@ public class RefDirectory extends RefDatabase { try { LockFile lck = lockPackedRefsOrThrow(); try { - final PackedRefList packed = getPackedRefs(); - RefList cur = readPackedRefs(); + PackedRefList oldPacked = refreshPackedRefs(); + RefList 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())) { -- 2.39.5