summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2022-02-20 12:00:49 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2022-03-02 09:56:03 +0100
commit85d8b31cb25e779f2511d96e0dd1a0606512e8b4 (patch)
tree5df9d5a31283da853d54b683a01380dc92b36f2f
parenta2d5650b8fc6775cc8684550d205502c70ab8771 (diff)
downloadjgit-85d8b31cb25e779f2511d96e0dd1a0606512e8b4.tar.gz
jgit-85d8b31cb25e779f2511d96e0dd1a0606512e8b4.zip
Cap describe abbrev option
- minimum is 4 [1] - maximum is length of a full ObjectId [1] https://git-scm.com/docs/git-config#Documentation/git-config.txt-coreabbrev Change-Id: I145bde1a218f71b87b8d8260761dd0853770bb76
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/DescribeCommandTest.java18
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java12
2 files changed, 19 insertions, 11 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 48980ad6e4..ab87fa9662 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
@@ -14,7 +14,6 @@ import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import static org.junit.Assert.assertEquals;
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;
@@ -104,6 +103,10 @@ public class DescribeCommandTest extends RepositoryTestCase {
assertEquals("bob-t2-1-g3e563c5", describe(c4, "a*", "b*", "c*"));
assertEquals("bob-t2", describe(c4, false, true, 0));
+ assertEquals("bob-t2-1-g3e56", describe(c4, false, true, 1));
+ assertEquals("bob-t2-1-g3e56", describe(c4, false, true, -10));
+ assertEquals("bob-t2-1-g3e563c55927905f21e3bc7c00a3d83a31bf4ed3a",
+ describe(c4, false, true, 50));
} else {
assertEquals(null, describe(c2));
assertEquals(null, describe(c3));
@@ -117,16 +120,13 @@ public class DescribeCommandTest extends RepositoryTestCase {
assertEquals("44579ebe7f", describe(c3, false, true, 10));
assertEquals("3e563c5592", describe(c4, false, true, 10));
- assertEquals("3e", describe(c4, false, true, 2));
+ assertEquals("3e56", describe(c4, false, true, -10));
+ assertEquals("3e56", describe(c4, false, true, 0));
+ assertEquals("3e56", 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));
+ assertEquals("3e563c55927905f21e3bc7c00a3d83a31bf4ed3a",
+ describe(c4, false, true, 42));
}
// test default target
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 955d147691..2480e2ebd7 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/DescribeCommand.java
@@ -232,7 +232,13 @@ public class DescribeCommand extends GitCommand<String> {
}
return String.format("%s-%d-g%s", formatRefName(tag.getName()), //$NON-NLS-1$
Integer.valueOf(depth),
- w.getObjectReader().abbreviate(tip, abbrev).name());
+ w.getObjectReader().abbreviate(tip, getCappedAbbrev()).name());
+ }
+
+ private int getCappedAbbrev() {
+ int len = Math.max(abbrev, 4);
+ len = Math.min(len, Constants.OBJECT_ID_STRING_LENGTH);
+ return len;
}
/**
@@ -436,7 +442,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, abbrev).name()
+ ? w.getObjectReader()
+ .abbreviate(target, getCappedAbbrev())
+ .name()
: null;
}