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

[JGIT PATCH] Read symbolic refs too when reading all refs

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
May 11, 2009, 22:49 UTC
Message-ID
<1242082140-740-1-git-send-email-robin.rosenberg@dewire.com>

We failed to read symrefs when scanning for all refs. Single refs were ok before too.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../tst/org/spearce/jgit/lib/RefTest.java          |   71 ++++++++++++++++++++
 .../src/org/spearce/jgit/lib/RefDatabase.java      |   13 ++--
 2 files changed, 77 insertions(+), 7 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefTest.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefTest.java
new file mode 100644
index 0000000..db79a8a
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RefTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2009, Robin Rosenberg
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.spearce.jgit.lib;
+
+import java.util.Map;
+
+/**
+ * Misc tests for refs. A lot of things are tested elsewhere so not having a
+ * test for a ref related method, does not mean it is untested.
+ */
+public class RefTest extends RepositoryTestCase {
+
+	public void testReadAllIncludingSymrefs() throws Exception {
+		ObjectId masterId = db.resolve("refs/heads/master");
+		RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
+		updateRef.setNewObjectId(masterId);
+		updateRef.setForceUpdate(true);
+		updateRef.update();
+		db
+				.writeSymref("refs/remotes/origin/HEAD",
+						"refs/remotes/origin/master");
+
+		ObjectId r = db.resolve("refs/remotes/origin/HEAD");
+		assertEquals(masterId, r);
+
+		Map<String, Ref> allRefs = db.getAllRefs();
+		Ref refHEAD = allRefs.get("refs/remotes/origin/HEAD");
+		assertEquals(masterId, refHEAD.getObjectId());
+		assertTrue(refHEAD.isPeeled());
+		assertNull(refHEAD.getPeeledObjectId());
+
+		Ref refmaster = allRefs.get("refs/remotes/origin/master");
+		assertEquals(masterId, refmaster.getObjectId());
+		assertFalse(refmaster.isPeeled());
+		assertNull(refmaster.getPeeledObjectId());
+	}
+}
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 87f26bf..e364b15 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/RefDatabase.java
@@ -265,23 +265,22 @@ private synchronized void readOneLooseRef(final Map<String, Ref> avail,
 					final byte[] str = new byte[Constants.OBJECT_ID_LENGTH * 2];
 					NB.readFully(in, str, 0, str.length);
 					id = ObjectId.fromString(str, 0);
+					ref = new Ref(Ref.Storage.LOOSE, origName, refName, id, null, false); // unpeeled
 				} catch (EOFException tooShortToBeRef) {
 					// Its below the minimum length needed. It could
 					// be a symbolic reference.
 					//
-					return;
+					ref = readRefBasic(refName, refName, 0); 
 				} catch (IllegalArgumentException notRef) {
 					// It is not a well-formed ObjectId. It may be
 					// a symbolic reference ("ref: ").
 					//
-					return;
+					ref = readRefBasic(refName, refName, 0); 
 				}
 
-				ref = new Ref(Ref.Storage.LOOSE, origName, refName, id, null, false); // unpeeled
-
-				looseRefs.put(ref.getName(), ref);
-				looseRefsMTime.put(ref.getName(), ent.lastModified());
-				avail.put(ref.getName(), ref);
+				looseRefs.put(ref.getOrigName(), ref);
+				looseRefsMTime.put(ref.getOrigName(), ent.lastModified());
+				avail.put(ref.getOrigName(), ref);
 			} finally {
 				in.close();
 			}
-- 
1.6.3.dirty
Next: Shawn O. Pearce
Message 1 of 2 in “Read symbolic refs too when reading all refs”
  1. Read symbolic refs too when reading all refsRobin Rosenberg, May 11, 2009
  2. Shawn O. PearceMay 12, 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.