[PATCH] Implement simple tags

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

 



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

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---
 .../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

-
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]

  Powered by Linux