[JGIT PATCH] Fix update of the refs cache when updating via a symbolic ref

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The branch pointed to by HEAD was lost in the refs cache, i.e. the
HEAD Ref was inserted into the cache with the the peeled ref as
key instead of HEAD. Then when asking for the peeled ref name HEAD
was returned.

This only happened when the ref name was a loose ref and not in
packed-refs at all, hence we test with an unborn branch, though an
existing loose (only) ref would show the same problem.

See http://bugs.eclipse.org/285991

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---
 .../tst/org/spearce/jgit/lib/RefUpdateTest.java    |   61 ++++++++++++++++++++
 .../src/org/spearce/jgit/lib/RefDatabase.java      |    2 +-
 2 files changed, 62 insertions(+), 1 deletions(-)


I'm not posting the patch to bugzilla since this is bug in JGit and not EGit.

-- robin

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java
index 8df8c2a..655e54e 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java
@@ -298,6 +298,67 @@ public void testUpdateRefNoChange() throws IOException {
 	}
 
 	/**
+	 * Test case originating from 
+	 * <a href="http://bugs.eclipse.org/285991";>bug 285991</a>
+	 *
+	 * Make sure the in memory cache is updated properly after
+	 * update of symref. This one did not fail because the
+	 * ref was packed due to implementation issues.
+	 *
+	 * @throws Exception
+	 */
+	public void testRefsCacheAfterUpdate() throws Exception {
+		// Do not use the defalt repo for this case.
+		Map<String, Ref> allRefs = db.getAllRefs();
+		ObjectId oldValue = db.resolve("HEAD");
+		ObjectId newValue = db.resolve("HEAD^");
+		// first make HEAD refer to loose ref
+		RefUpdate updateRef = db.updateRef(Constants.HEAD);
+		updateRef.setForceUpdate(true);
+		updateRef.setNewObjectId(newValue);
+		Result update = updateRef.update();
+		assertEquals(Result.FORCED, update);
+		
+		// now update that ref
+		updateRef = db.updateRef(Constants.HEAD);
+		updateRef.setForceUpdate(true);
+		updateRef.setNewObjectId(oldValue);
+		update = updateRef.update();
+		assertEquals(Result.FAST_FORWARD, update);
+		allRefs = db.getAllRefs();
+		assertEquals("refs/heads/master", allRefs.get("refs/heads/master").getName());
+		assertEquals("refs/heads/master", allRefs.get("refs/heads/master").getOrigName());
+		assertEquals("refs/heads/master", allRefs.get("HEAD").getName());
+		assertEquals("HEAD", allRefs.get("HEAD").getOrigName());
+	}
+	
+	/**
+	 * Test case originating from 
+	 * <a href="http://bugs.eclipse.org/285991";>bug 285991</a>
+	 *
+	 * Make sure the in memory cache is updated properly after
+	 * update of symref.
+	 *
+	 * @throws Exception
+	 */
+	public void testRefsCacheAfterUpdateLoosOnly() throws Exception {
+		// Do not use the defalt repo for this case.
+		Map<String, Ref> allRefs = db.getAllRefs();
+		ObjectId oldValue = db.resolve("HEAD");
+		db.writeSymref(Constants.HEAD, "refs/heads/newref");
+		RefUpdate updateRef = db.updateRef(Constants.HEAD);
+		updateRef.setForceUpdate(true);
+		updateRef.setNewObjectId(oldValue);
+		Result update = updateRef.update();
+		assertEquals(Result.NEW, update);
+		allRefs = db.getAllRefs();
+		assertEquals("refs/heads/newref", allRefs.get("HEAD").getName());
+		assertEquals("HEAD", allRefs.get("HEAD").getOrigName());
+		assertEquals("refs/heads/newref", allRefs.get("refs/heads/newref").getName());
+		assertEquals("refs/heads/newref", allRefs.get("refs/heads/newref").getOrigName());
+	}
+
+	/**
 	 * Try modify a ref, but get wrong expected old value
 	 *
 	 * @throws IOException
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
index f7751c4..ba4b654 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
@@ -140,7 +140,7 @@ RefUpdate newUpdate(final String name) throws IOException {
 
 	void stored(final String origName, final String name, final ObjectId id, final long time) {
 		synchronized (this) {
-			looseRefs.put(name, new Ref(Ref.Storage.LOOSE, origName, name, id));
+			looseRefs.put(name, new Ref(Ref.Storage.LOOSE, name, name, id));
 			looseRefsMTime.put(name, time);
 			setModified();
 		}
-- 
1.6.4.115.gc0eb0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]