{"thread":{"id":"20609","subject":"[JGIT PATCH] Fix update of the refs cache when updating via a symbolic ref","startedAt":"2009-08-15T18:43:39Z","lastAt":"2009-08-15T18:43:39Z","messageCount":1,"participants":["Robin Rosenberg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"120725","messageId":"1250361819-20031-1-git-send-email-robin.rosenberg@dewire.com","threadId":"20609","inReplyTo":null,"subject":"[JGIT PATCH] Fix update of the refs cache when updating via a symbolic ref","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2009-08-15T18:43:39Z","receivedAt":"2009-08-15T18:43:39Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"The branch pointed to by HEAD was lost in the refs cache, i.e. the\nHEAD Ref was inserted into the cache with the the peeled ref as\nkey instead of HEAD. Then when asking for the peeled ref name HEAD\nwas returned.\n\nThis only happened when the ref name was a loose ref and not in\npacked-refs at all, hence we test with an unborn branch, though an\nexisting loose (only) ref would show the same problem.\n\nSee http://bugs.eclipse.org/285991\n\nSigned-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n .../tst/org/spearce/jgit/lib/RefUpdateTest.java    |   61 ++++++++++++++++++++\n .../src/org/spearce/jgit/lib/RefDatabase.java      |    2 +-\n 2 files changed, 62 insertions(+), 1 deletions(-)\n\n\nI'm not posting the patch to bugzilla since this is bug in JGit and not EGit.\n\n-- robin\n\ndiff --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\nindex 8df8c2a..655e54e 100644\n--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java\n+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefUpdateTest.java\n@@ -298,6 +298,67 @@ public void testUpdateRefNoChange() throws IOException {\n \t}\n \n \t/**\n+\t * Test case originating from \n+\t * <a href=\"http://bugs.eclipse.org/285991\">bug 285991</a>\n+\t *\n+\t * Make sure the in memory cache is updated properly after\n+\t * update of symref. This one did not fail because the\n+\t * ref was packed due to implementation issues.\n+\t *\n+\t * @throws Exception\n+\t */\n+\tpublic void testRefsCacheAfterUpdate() throws Exception {\n+\t\t// Do not use the defalt repo for this case.\n+\t\tMap<String, Ref> allRefs = db.getAllRefs();\n+\t\tObjectId oldValue = db.resolve(\"HEAD\");\n+\t\tObjectId newValue = db.resolve(\"HEAD^\");\n+\t\t// first make HEAD refer to loose ref\n+\t\tRefUpdate updateRef = db.updateRef(Constants.HEAD);\n+\t\tupdateRef.setForceUpdate(true);\n+\t\tupdateRef.setNewObjectId(newValue);\n+\t\tResult update = updateRef.update();\n+\t\tassertEquals(Result.FORCED, update);\n+\t\t\n+\t\t// now update that ref\n+\t\tupdateRef = db.updateRef(Constants.HEAD);\n+\t\tupdateRef.setForceUpdate(true);\n+\t\tupdateRef.setNewObjectId(oldValue);\n+\t\tupdate = updateRef.update();\n+\t\tassertEquals(Result.FAST_FORWARD, update);\n+\t\tallRefs = db.getAllRefs();\n+\t\tassertEquals(\"refs/heads/master\", allRefs.get(\"refs/heads/master\").getName());\n+\t\tassertEquals(\"refs/heads/master\", allRefs.get(\"refs/heads/master\").getOrigName());\n+\t\tassertEquals(\"refs/heads/master\", allRefs.get(\"HEAD\").getName());\n+\t\tassertEquals(\"HEAD\", allRefs.get(\"HEAD\").getOrigName());\n+\t}\n+\t\n+\t/**\n+\t * Test case originating from \n+\t * <a href=\"http://bugs.eclipse.org/285991\">bug 285991</a>\n+\t *\n+\t * Make sure the in memory cache is updated properly after\n+\t * update of symref.\n+\t *\n+\t * @throws Exception\n+\t */\n+\tpublic void testRefsCacheAfterUpdateLoosOnly() throws Exception {\n+\t\t// Do not use the defalt repo for this case.\n+\t\tMap<String, Ref> allRefs = db.getAllRefs();\n+\t\tObjectId oldValue = db.resolve(\"HEAD\");\n+\t\tdb.writeSymref(Constants.HEAD, \"refs/heads/newref\");\n+\t\tRefUpdate updateRef = db.updateRef(Constants.HEAD);\n+\t\tupdateRef.setForceUpdate(true);\n+\t\tupdateRef.setNewObjectId(oldValue);\n+\t\tResult update = updateRef.update();\n+\t\tassertEquals(Result.NEW, update);\n+\t\tallRefs = db.getAllRefs();\n+\t\tassertEquals(\"refs/heads/newref\", allRefs.get(\"HEAD\").getName());\n+\t\tassertEquals(\"HEAD\", allRefs.get(\"HEAD\").getOrigName());\n+\t\tassertEquals(\"refs/heads/newref\", allRefs.get(\"refs/heads/newref\").getName());\n+\t\tassertEquals(\"refs/heads/newref\", allRefs.get(\"refs/heads/newref\").getOrigName());\n+\t}\n+\n+\t/**\n \t * Try modify a ref, but get wrong expected old value\n \t *\n \t * @throws IOException\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java\nindex f7751c4..ba4b654 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java\n@@ -140,7 +140,7 @@ RefUpdate newUpdate(final String name) throws IOException {\n \n \tvoid stored(final String origName, final String name, final ObjectId id, final long time) {\n \t\tsynchronized (this) {\n-\t\t\tlooseRefs.put(name, new Ref(Ref.Storage.LOOSE, origName, name, id));\n+\t\t\tlooseRefs.put(name, new Ref(Ref.Storage.LOOSE, name, name, id));\n \t\t\tlooseRefsMTime.put(name, time);\n \t\t\tsetModified();\n \t\t}\n-- \n1.6.4.115.gc0eb0\n"}]}