git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Aug 15, 2009, 18:43 UTC
Message-ID
<1250361819-20031-1-git-send-email-robin.rosenberg@dewire.com>

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@dewire.com>
---
 .../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
Message 1 of 1 in “Fix update of the refs cache when updating via a symbolic ref”
  1. Fix update of the refs cache when updating via a symbolic refRobin Rosenberg, Aug 15, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.