]> source.dussan.org Git - jgit.git/commitdiff
DescribeCommand: Support configuring the hash abbreviation 38/189338/7
authorSebastian Schuberth <sebastian.schuberth@bosch.io>
Wed, 5 Jan 2022 17:07:48 +0000 (18:07 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Sat, 19 Feb 2022 21:04:28 +0000 (22:04 +0100)
Bug: 537883
Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
Change-Id: Ic52dcebc564bbb0d934cc3a6205704b7aeaee30e

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java

index b460e3f52e4e60d6c81f080e103a6d0004f1e029..205116999608a50775daec9ea8cd6ca89320eb79 100644 (file)
 package org.eclipse.jgit.api;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.io.BufferedWriter;
@@ -108,6 +110,21 @@ public class DescribeCommandTest extends RepositoryTestCase {
                        assertEquals("3747db3", describe(c2, false, true));
                        assertEquals("44579eb", describe(c3, false, true));
                        assertEquals("3e563c5", describe(c4, false, true));
+
+                       assertEquals("3747db3267", describe(c2, false, true, 10));
+                       assertEquals("44579ebe7f", describe(c3, false, true, 10));
+                       assertEquals("3e563c5592", describe(c4, false, true, 10));
+
+                       assertEquals("3e", describe(c4, false, true, 2));
+                       assertEquals("3e563c55927905f21e3bc7c00a3d83a31bf4ed3a",
+                                       describe(c4, false, true, 40));
+
+                       assertThrows(StringIndexOutOfBoundsException.class,
+                                       () -> describe(c4, false, true, -10));
+                       assertThrows(StringIndexOutOfBoundsException.class,
+                                       () -> describe(c4, false, true, 1));
+                       assertThrows(StringIndexOutOfBoundsException.class,
+                                       () -> describe(c4, false, true, 41));
                }
 
                // test default target
@@ -474,10 +491,15 @@ public class DescribeCommandTest extends RepositoryTestCase {
                }
        }
 
+       private String describe(ObjectId c1, boolean longDesc, boolean always,
+                       int abbrev) throws GitAPIException, IOException {
+               return git.describe().setTarget(c1).setTags(describeUseAllTags)
+                               .setLong(longDesc).setAlways(always).setAbbrev(abbrev).call();
+       }
+
        private String describe(ObjectId c1, boolean longDesc, boolean always)
                        throws GitAPIException, IOException {
-               return git.describe().setTarget(c1).setTags(describeUseAllTags)
-                               .setLong(longDesc).setAlways(always).call();
+               return describe(c1, longDesc, always, OBJECT_ID_ABBREV_STRING_LENGTH);
        }
 
        private String describe(ObjectId c1) throws GitAPIException, IOException {
index 1e524fadab988a3e72ed81a3ea8e37bad456a1fc..e572773e430e5a61c1e3869fbd5a37773c2e868c 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.eclipse.jgit.lib.Constants.R_REFS;
 import static org.eclipse.jgit.lib.Constants.R_TAGS;
 
@@ -88,6 +89,11 @@ public class DescribeCommand extends GitCommand<String> {
         */
        private boolean always;
 
+       /**
+        * The prefix length to use when abbreviating a commit hash.
+        */
+       private int abbrev = OBJECT_ID_ABBREV_STRING_LENGTH;
+
        /**
         * Constructor for DescribeCommand.
         *
@@ -205,12 +211,25 @@ public class DescribeCommand extends GitCommand<String> {
                return this;
        }
 
+       /**
+        * Sets the prefix length to use when abbreviating an object SHA-1.
+        *
+        * @param abbrev
+        *            minimum length of the abbreviated string. Must be in the range
+        *            [2, {@value Constants#OBJECT_ID_STRING_LENGTH}].
+        * @return {@code this}
+        * @since 6.1
+        */
+       public DescribeCommand setAbbrev(int abbrev) {
+               this.abbrev = abbrev;
+               return this;
+       }
+
        private String longDescription(Ref tag, int depth, ObjectId tip)
                        throws IOException {
-               return String.format(
-                               "%s-%d-g%s", formatRefName(tag.getName()), //$NON-NLS-1$
-                               Integer.valueOf(depth), w.getObjectReader().abbreviate(tip)
-                                               .name());
+               return String.format("%s-%d-g%s", formatRefName(tag.getName()), //$NON-NLS-1$
+                               Integer.valueOf(depth),
+                               w.getObjectReader().abbreviate(tip, abbrev).name());
        }
 
        /**
@@ -413,7 +432,9 @@ public class DescribeCommand extends GitCommand<String> {
 
                        // if all the nodes are dominated by all the tags, the walk stops
                        if (candidates.isEmpty()) {
-                               return always ? w.getObjectReader().abbreviate(target).name() : null;
+                               return always
+                                               ? w.getObjectReader().abbreviate(target, abbrev).name()
+                                               : null;
                        }
 
                        Candidate best = Collections.min(candidates,