]> source.dussan.org Git - jgit.git/commitdiff
Introduce a constant for the length of an abbreviated hash string 37/189337/5
authorSebastian Schuberth <sebastian.schuberth@bosch.io>
Wed, 5 Jan 2022 17:21:33 +0000 (18:21 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 2 Feb 2022 23:21:25 +0000 (00:21 +0100)
Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
Change-Id: I196d58a813f7caa1965af4cf8e2f977ed4cdc350

org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Blame.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Merge.java
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/RevertCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CherryPickCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/RevertCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/api/StashCreateCommand.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffFormatter.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java
org.eclipse.jgit/src/org/eclipse/jgit/transport/TrackingRefUpdate.java

index 2b49cf73d411a87a281dc39c726872303f8096f4..1a3a2f6f4b07968950d0a49939df1bebd19685d6 100644 (file)
@@ -15,6 +15,7 @@ package org.eclipse.jgit.pgm;
 
 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 @@ class Blame extends TextBuiltin {
 
                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$
index ca4877fb34782252379c43b24bea78ec672d5168..27a3d90fad8ff1ff862bb44e22997d474dfa9580 100644 (file)
@@ -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 @@ class Merge extends TextBuiltin {
                        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));
                                }
index 030119ea3471f174425935479c728641be1286a5..c63532df60da1f6bfca44c3669180313eb5452c2 100644 (file)
@@ -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 @@ class Reflog extends TextBuiltin {
 
        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$
index d833f12d2607210641555fe812813d587aa697fb..1c7b8d13a85e17c845139877117b8c57dab32fd6 100644 (file)
@@ -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 class RevertCommandTest extends RepositoryTestCase {
 
                        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<RevCommit> history = git.log().call().iterator();
                        RevCommit revertCommit = history.next();
index 8ee1e965e9dd190209098ceb54e135c778b33511..f88179ac1a8d891c674e6fc36e39ca1b53a55e89 100644 (file)
@@ -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 class CherryPickCommand extends GitCommand<CherryPickResult> {
                                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);
index 02f581cbb6232d116f64b941377477ed58a9d679..db88ad8dc998d4f410e7bb714fa88fb365324e3d 100644 (file)
@@ -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 class RevertCommand extends GitCommand<RevCommit> {
                                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));
index 35fd8992b6c8690df34ad841bd7886ffe0b5dd23..f7a1f4eff82ec7013a23a6a9bca73a51d6135bd2 100644 (file)
@@ -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 class StashCreateCommand extends GitCommand<RevCommit> {
                                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 class StashCreateCommand extends GitCommand<RevCommit> {
                                        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 class StashCreateCommand extends GitCommand<RevCommit> {
                                        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);
index 49da95c9abd177b02839837ee3300e5d60a65fc5..1a5f74f98a4a13db870fd886a71ebb228e0e46b9 100644 (file)
@@ -18,6 +18,7 @@ import static org.eclipse.jgit.diff.DiffEntry.ChangeType.MODIFY;
 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;
 
index 92367ebd0c11d8c84ea4a8f262cfa35c2c817833..cf2e69dbb52d6ba2888042d0bd3ea62eea92e2dc 100644 (file)
@@ -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";
 
index a2c7381ce794dff9e1c624e509847d2a4ddda939..26c3ff671873b1a08cba49305b925477fb704f44 100644 (file)
@@ -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);
        }
 
        /**
index 696ca7cf46f9fb251c262899421038b46a7430f4..51bc07cb948b036f8aa95a8668051924243f2a8a 100644 (file)
@@ -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 class TrackingRefUpdate {
                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();
        }