From 20bdcf9ea82c390ee70180c3edb51888e7d237bc Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 5 Jan 2022 18:21:33 +0100 Subject: [PATCH] Introduce a constant for the length of an abbreviated hash string Signed-off-by: Sebastian Schuberth Change-Id: I196d58a813f7caa1965af4cf8e2f977ed4cdc350 --- .../src/org/eclipse/jgit/pgm/Blame.java | 4 +++- .../src/org/eclipse/jgit/pgm/Merge.java | 8 ++++++-- .../src/org/eclipse/jgit/pgm/Reflog.java | 5 ++++- .../tst/org/eclipse/jgit/api/RevertCommandTest.java | 5 ++++- .../src/org/eclipse/jgit/api/CherryPickCommand.java | 4 +++- .../src/org/eclipse/jgit/api/RevertCommand.java | 7 +++++-- .../org/eclipse/jgit/api/StashCreateCommand.java | 13 ++++++++++--- .../src/org/eclipse/jgit/diff/DiffFormatter.java | 3 ++- .../src/org/eclipse/jgit/lib/Constants.java | 9 +++++++++ .../src/org/eclipse/jgit/lib/ObjectReader.java | 4 +++- .../eclipse/jgit/transport/TrackingRefUpdate.java | 10 ++++++++-- 11 files changed, 57 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java index 2b49cf73d..1a3a2f6f4 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java @@ -15,6 +15,7 @@ import static java.lang.Integer.valueOf; import static java.lang.Long.valueOf; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH; import java.io.IOException; @@ -116,7 +117,8 @@ protected void run() { boolean autoAbbrev = abbrev == 0; if (abbrev == 0) { - abbrev = db.getConfig().getInt("core", "abbrev", 7); //$NON-NLS-1$ //$NON-NLS-2$ + abbrev = db.getConfig().getInt("core", "abbrev", //$NON-NLS-1$ //$NON-NLS-2$ + OBJECT_ID_ABBREV_STRING_LENGTH); } if (!showBlankBoundary) { root = db.getConfig().getBoolean("blame", "blankboundary", false); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java index ca4877fb3..27a3d90fa 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java @@ -10,6 +10,8 @@ package org.eclipse.jgit.pgm; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.IOException; import java.text.MessageFormat; import java.util.Map; @@ -144,8 +146,10 @@ protected void run() { case FAST_FORWARD: ObjectId oldHeadId = oldHead.getObjectId(); if (oldHeadId != null) { - String oldId = oldHeadId.abbreviate(7).name(); - String newId = result.getNewHead().abbreviate(7).name(); + String oldId = oldHeadId + .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name(); + String newId = result.getNewHead() + .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name(); outw.println(MessageFormat.format(CLIText.get().updating, oldId, newId)); } diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java index 030119ea3..c63532df6 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java @@ -9,6 +9,8 @@ */ package org.eclipse.jgit.pgm; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.IOException; import java.util.Collection; @@ -45,7 +47,8 @@ protected void run() { private String toString(ReflogEntry entry, int i) { final StringBuilder s = new StringBuilder(); - s.append(entry.getNewId().abbreviate(7).name()); + s.append(entry.getNewId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name()); s.append(" "); //$NON-NLS-1$ s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref)); s.append("@{" + i + "}:"); //$NON-NLS-1$ //$NON-NLS-2$ diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java index d833f12d2..1c7b8d13a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java @@ -9,6 +9,7 @@ */ package org.eclipse.jgit.api; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -166,7 +167,9 @@ public void testRevertMultipleWithFail() throws IOException, checkFile(new File(db.getWorkTree(), "a"), "first\n" + "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n" - + ">>>>>>> " + secondCommit.getId().abbreviate(7).name() + + ">>>>>>> " + + secondCommit.getId() + .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + " add second\n"); Iterator history = git.log().call().iterator(); RevCommit revertCommit = history.next(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java index 8ee1e965e..f88179ac1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java @@ -9,6 +9,8 @@ */ package org.eclipse.jgit.api; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.IOException; import java.text.MessageFormat; import java.util.LinkedList; @@ -124,7 +126,7 @@ public CherryPickResult call() throws GitAPIException, NoMessageException, final RevCommit srcParent = getParentCommit(srcCommit, revWalk); String ourName = calculateOurName(headRef); - String cherryPickName = srcCommit.getId().abbreviate(7).name() + String cherryPickName = srcCommit.getId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ Merger merger = strategy.newMerger(repo); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java index 02f581cbb..db88ad8dc 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java @@ -9,6 +9,8 @@ */ package org.eclipse.jgit.api; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.IOException; import java.text.MessageFormat; import java.util.LinkedList; @@ -128,8 +130,9 @@ public RevCommit call() throws NoMessageException, UnmergedPathsException, revWalk.parseHeaders(srcParent); String ourName = calculateOurName(headRef); - String revertName = srcCommit.getId().abbreviate(7).name() - + " " + srcCommit.getShortMessage(); //$NON-NLS-1$ + String revertName = srcCommit.getId() + .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + " " //$NON-NLS-1$ + + srcCommit.getShortMessage(); ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java index 35fd8992b..f7a1f4eff 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java @@ -9,6 +9,8 @@ */ package org.eclipse.jgit.api; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -302,7 +304,8 @@ public void apply(DirCacheEntry ent) { builder.setParentId(headCommit); builder.setTreeId(cache.writeTree(inserter)); builder.setMessage(MessageFormat.format(indexMessage, branch, - headCommit.abbreviate(7).name(), + headCommit.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name(), headCommit.getShortMessage())); ObjectId indexCommit = inserter.insert(builder); @@ -319,7 +322,10 @@ public void apply(DirCacheEntry ent) { builder.setParentIds(new ObjectId[0]); builder.setTreeId(untrackedDirCache.writeTree(inserter)); builder.setMessage(MessageFormat.format(MSG_UNTRACKED, - branch, headCommit.abbreviate(7).name(), + branch, + headCommit + .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name(), headCommit.getShortMessage())); untrackedCommit = inserter.insert(builder); } @@ -339,7 +345,8 @@ public void apply(DirCacheEntry ent) { builder.addParentId(untrackedCommit); builder.setMessage(MessageFormat.format( workingDirectoryMessage, branch, - headCommit.abbreviate(7).name(), + headCommit.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name(), headCommit.getShortMessage())); builder.setTreeId(cache.writeTree(inserter)); commitId = inserter.insert(builder); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java index 49da95c9a..1a5f74f98 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java @@ -18,6 +18,7 @@ import static org.eclipse.jgit.diff.DiffEntry.ChangeType.RENAME; import static org.eclipse.jgit.diff.DiffEntry.Side.NEW; import static org.eclipse.jgit.diff.DiffEntry.Side.OLD; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; import static org.eclipse.jgit.lib.Constants.encode; import static org.eclipse.jgit.lib.Constants.encodeASCII; import static org.eclipse.jgit.lib.FileMode.GITLINK; @@ -90,7 +91,7 @@ public class DiffFormatter implements AutoCloseable { private int context = 3; - private int abbreviationLength = 7; + private int abbreviationLength = OBJECT_ID_ABBREV_STRING_LENGTH; private DiffAlgorithm diffAlgorithm; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java index 92367ebd0..cf2e69dbb 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java @@ -48,6 +48,15 @@ public final class Constants { */ public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2; + /** + * The historic length of an abbreviated Git object hash string. Git 2.11 + * changed this static number to a dynamically calculated one that scales + * as the repository grows. + * + * @since 6.1 + */ + public static final int OBJECT_ID_ABBREV_STRING_LENGTH = 7; + /** Special name for the "HEAD" symbolic-ref. */ public static final String HEAD = "HEAD"; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java index a2c7381ce..26c3ff671 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java @@ -10,6 +10,8 @@ package org.eclipse.jgit.lib; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import java.io.IOException; import java.util.ArrayList; import java.util.Collection; @@ -76,7 +78,7 @@ public abstract class ObjectReader implements AutoCloseable { */ public AbbreviatedObjectId abbreviate(AnyObjectId objectId) throws IOException { - return abbreviate(objectId, 7); + return abbreviate(objectId, OBJECT_ID_ABBREV_STRING_LENGTH); } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TrackingRefUpdate.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TrackingRefUpdate.java index 696ca7cf4..51bc07cb9 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/TrackingRefUpdate.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/TrackingRefUpdate.java @@ -12,6 +12,8 @@ package org.eclipse.jgit.transport; +import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH; + import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.RefUpdate; @@ -184,9 +186,13 @@ public String toString() { if (forceUpdate) sb.append(" (forced)"); sb.append(" "); - sb.append(oldObjectId == null ? "" : oldObjectId.abbreviate(7).name()); + sb.append(oldObjectId == null ? "" + : oldObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name()); sb.append(".."); - sb.append(newObjectId == null ? "" : newObjectId.abbreviate(7).name()); + sb.append(newObjectId == null ? "" + : newObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH) + .name()); sb.append("]"); return sb.toString(); }