aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Schuberth <sebastian.schuberth@bosch.io>2022-01-05 18:07:48 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2022-02-19 22:04:28 +0100
commita7386ffe3aada6d1e0f73536f985dbe21cebadc2 (patch)
tree46f0e2b12a3e0177fc8eb8946a4a47a363343470
parent69ef598bd933dda36ff852700e5ff7853e7ae04e (diff)
downloadjgit-a7386ffe3aada6d1e0f73536f985dbe21cebadc2.tar.gz
jgit-a7386ffe3aada6d1e0f73536f985dbe21cebadc2.zip
DescribeCommand: Support configuring the hash abbreviation
Bug: 537883 Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io> Change-Id: Ic52dcebc564bbb0d934cc3a6205704b7aeaee30e
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java28
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java31
2 files changed, 51 insertions, 8 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
index b460e3f52e..2051169996 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java
@@ -10,9 +10,11 @@
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 {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
index 1e524fadab..e572773e43 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.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.eclipse.jgit.lib.Constants.R_REFS;
import static org.eclipse.jgit.lib.Constants.R_TAGS;
@@ -89,6 +90,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.
*
* @param repo
@@ -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,