[EGIT PATCH 15/31] Add simple abbreviate() method to ObjectId

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

 



It's common usage to search for short, but unique abbreviation of
object's SHA-1, so let's have a method for that.

This is a really workaround implementation, but it has been already used
in other places. Let's have other usages pointing here, so we can easily
change only this implementation later.

Signed-off-by: Marek Zawirski <marek.zawirski@xxxxxxxxx>
---
 .../src/org/spearce/jgit/pgm/Fetch.java            |   10 +++++-----
 .../src/org/spearce/jgit/pgm/Push.java             |    7 +++----
 .../src/org/spearce/jgit/pgm/TextBuiltin.java      |    4 ----
 .../src/org/spearce/jgit/lib/AnyObjectId.java      |   15 +++++++++++++++
 4 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Fetch.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Fetch.java
index 4eff32b..99ed101 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Fetch.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Fetch.java
@@ -93,7 +93,7 @@ class Fetch extends TextBuiltin {
 		}
 	}
 
-	private static String longTypeOf(final TrackingRefUpdate u) {
+	private String longTypeOf(final TrackingRefUpdate u) {
 		final RefUpdate.Result r = u.getResult();
 		if (r == RefUpdate.Result.LOCK_FAILURE)
 			return "[lock fail]";
@@ -110,14 +110,14 @@ class Fetch extends TextBuiltin {
 		}
 
 		if (r == RefUpdate.Result.FORCED) {
-			final String aOld = abbreviateObject(u.getOldObjectId());
-			final String aNew = abbreviateObject(u.getNewObjectId());
+			final String aOld = u.getOldObjectId().abbreviate(db);
+			final String aNew = u.getNewObjectId().abbreviate(db);
 			return aOld + "..." + aNew;
 		}
 
 		if (r == RefUpdate.Result.FAST_FORWARD) {
-			final String aOld = abbreviateObject(u.getOldObjectId());
-			final String aNew = abbreviateObject(u.getNewObjectId());
+			final String aOld = u.getOldObjectId().abbreviate(db);
+			final String aNew = u.getNewObjectId().abbreviate(db);
 			return aOld + ".." + aNew;
 		}
 
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
index f5b24c6..b61071c 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Push.java
@@ -173,10 +173,9 @@ class Push extends TextBuiltin {
 				} else {
 					boolean fastForward = rru.isFastForward();
 					final char flag = fastForward ? ' ' : '+';
-					final String summary = abbreviateObject(oldRef
-							.getObjectId())
+					final String summary = oldRef.getObjectId().abbreviate(db)
 							+ (fastForward ? ".." : "...")
-							+ abbreviateObject(rru.getNewObjectId());
+							+ rru.getNewObjectId().abbreviate(db);
 					final String message = fastForward ? null : "forced update";
 					printUpdateLine(flag, summary, srcRef, remoteName, message);
 				}
@@ -199,7 +198,7 @@ class Push extends TextBuiltin {
 
 		case REJECTED_REMOTE_CHANGED:
 			final String message = "remote ref object changed - is not expected one "
-					+ abbreviateObject(rru.getExpectedOldObjectId());
+					+ rru.getExpectedOldObjectId().abbreviate(db);
 			printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
 			break;
 
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
index 5c066cb..23ab92b 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/TextBuiltin.java
@@ -179,10 +179,6 @@ public abstract class TextBuiltin {
 		return new Die(why);
 	}
 
-	protected static String abbreviateObject(final ObjectId id) {
-		return id.toString().substring(0, 7);
-	}
-
 	protected String abbreviateRef(String dst, boolean abbreviateRemote) {
 		if (dst.startsWith(REFS_HEADS))
 			dst = dst.substring(REFS_HEADS.length());
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/AnyObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/AnyObjectId.java
index 7357e57..2c5518a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/AnyObjectId.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/AnyObjectId.java
@@ -411,6 +411,21 @@ public abstract class AnyObjectId implements Comparable {
 	}
 
 	/**
+	 * Return unique abbreviation (prefix) of this object SHA-1.
+	 * <p>
+	 * Current implementation is not guaranteeing uniqueness, it just returns
+	 * fixed-length prefix of SHA-1 string.
+	 * 
+	 * @param repo
+	 *            repository for checking uniqueness within.
+	 * @return SHA-1 abbreviation.
+	 */
+	public String abbreviate(final Repository repo) {
+		// TODO implement checking for uniqueness
+		return toString().substring(0, 7);
+	}
+
+	/**
 	 * Obtain an immutable copy of this current object name value.
 	 * <p>
 	 * Only returns <code>this</code> if this instance is an unsubclassed
-- 
1.5.6.3

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