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

[PATCH] Implement simple tags

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
May 13, 2007, 23:39 UTC
Message-ID
<11790995571082-git-send-email-robin.rosenberg@dewire.com>
In-Reply-To
<11790995572233-git-send-email-robin.rosenberg@dewire.com>

This is just a reference in <gitdir>/refs/tags with the SHA-1 of the tagged object.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../src/org/spearce/jgit/lib/Repository.java       |   14 +++++++-------
 org.spearce.jgit/src/org/spearce/jgit/lib/Tag.java |   20 ++++++++++++++++----
 .../tst/org/spearce/jgit/lib/T0003_Basic.java      |   13 +++++++++++++
 3 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 482f41d..76191be 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -262,17 +262,17 @@ public class Repository {
 
 	public Tag mapTag(String revstr) throws IOException {
 		final ObjectId id = resolve(revstr);
-		return id != null ? mapTag(id) : null;
+		return id != null ? mapTag(revstr, id) : null;
 	}
 
-	public Tag mapTag(final ObjectId id) throws IOException {
+	public Tag mapTag(final String refName, final ObjectId id) throws IOException {
 		final ObjectLoader or = openObject(id);
 		if (or == null)
 			return null;
 		final byte[] raw = or.getBytes();
 		if (Constants.TYPE_TAG.equals(or.getType()))
-			return new Tag(this, id, raw);
-		throw new IncorrectObjectTypeException(id, Constants.TYPE_TAG);
+			return new Tag(this, id, refName, raw);
+		return new Tag(this, id, refName, null);
 	}
 
 	public RefLock lockRef(final String ref) throws IOException {
@@ -469,7 +469,7 @@ public class Repository {
 		return listFilesRecursively(new File(refsDir, "heads"), null);
 	}
 
-	public Collection getTags() {
+	public Collection<String> getTags() {
 		return listFilesRecursively(new File(refsDir, "tags"), null);
 	}
 
@@ -535,10 +535,10 @@ public class Repository {
 		return ret;
 	}
 
-	private Collection listFilesRecursively(File root, File start) {
+	private Collection<String> listFilesRecursively(File root, File start) {
 		if (start == null)
 			start = root;
-		Collection ret = new ArrayList();
+		Collection<String> ret = new ArrayList();
 		File[] files = start.listFiles();
 		for (int i = 0; i < files.length; ++i) {
 			if (files[i].isDirectory())
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Tag.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Tag.java
index 877c440..d5c6b54 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Tag.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Tag.java
@@ -44,10 +44,16 @@ public class Tag {
 		objdb = db;
 	}
 
-	public Tag(final Repository db, final ObjectId id, final byte[] raw) {
+	public Tag(final Repository db, final ObjectId id, String refName, final byte[] raw) {
 		objdb = db;
-		tagId = id;
-		objId = ObjectId.fromString(raw, 7);
+		if (raw != null) {
+			tagId = id;
+			objId = ObjectId.fromString(raw, 7);
+		} else
+			objId = id;
+		if (refName.startsWith("refs/tags/"))
+			refName = refName.substring(10);
+		tag = refName;
 		this.raw = raw;
 	}
 
@@ -119,7 +125,13 @@ public class Tag {
 	public void tag() throws IOException {
 		if (getTagId() != null)
 			throw new IllegalStateException("exists " + getTagId());
-		setTagId(new ObjectWriter(objdb).writeTag(this));
+		if (tagger!=null || message!=null || type!=null) {
+			ObjectId tagid = new ObjectWriter(objdb).writeTag(this);
+			setTagId(tagid);
+			objdb.writeRef("refs/heads/"+getTag(),tagid);
+		} else {
+			objdb.writeRef("refs/heads/"+getTag(),objId);
+		}
 	}
 
 	public String toString() {
diff --git a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java
index 0302a45..115e391 100644
--- a/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java
+++ b/org.spearce.jgit/tst/org/spearce/jgit/lib/T0003_Basic.java
@@ -338,6 +338,19 @@ public class T0003_Basic extends RepositoryTestCase {
 		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().toString());
 	}
 
+	public void test020b_createBlobPlainTag() throws IOException {
+		test020_createBlobTag();
+		Tag t = new Tag(db);
+		t.setTag("test020b");
+		t.setObjId(new ObjectId("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
+		t.tag();
+		
+		Tag mapTag = db.mapTag("test020b");
+		assertEquals("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", mapTag.getObjId().toString());
+		
+		// We do not repeat the plain tag test for other object types
+	}
+
 	public void test021_createTreeTag() throws IOException {
 		final ObjectId emptyId = new ObjectWriter(db).writeBlob(new byte[0]);
 		final Tree almostEmptyTree = new Tree(db);
-- 
1.5.1.1
Previous: Robin RosenbergNext: Robin Rosenberg
Message 2 of 13 in “This week in EGIT - more tags”
  1. Robin RosenbergMay 13, 2007
  2. Implement simple tagsRobin Rosenberg, May 13, 2007
  3. Write refs when creating tagsRobin Rosenberg, May 13, 2007
  4. Implement packed refsRobin Rosenberg, May 13, 2007
  5. Show tags in history viewRobin Rosenberg, May 13, 2007
  6. Shawn O. PearceMay 14, 2007
  7. Robin RosenbergMay 14, 2007
  8. The first patch in the seriesRobin Rosenberg, May 13, 2007
  9. Require JDK1.5Robin Rosenberg, May 13, 2007
  10. Grzegorz KulewskiMay 14, 2007
  11. Noel GrandinMay 14, 2007
  12. Robin RosenbergMay 14, 2007
  13. Set required execution enviromentRobin Rosenberg, May 14, 2007

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.