From 85d8b31cb25e779f2511d96e0dd1a0606512e8b4 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Sun, 20 Feb 2022 12:00:49 +0100 Subject: [PATCH] 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 --- .../eclipse/jgit/api/DescribeCommandTest.java | 18 +++++++++--------- .../org/eclipse/jgit/api/DescribeCommand.java | 12 ++++++++++-- 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 48980ad6e..ab87fa966 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.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 void testDescribe() throws Exception { 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 void testDescribe() throws Exception { 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 955d14769..2480e2ebd 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 @@ private String longDescription(Ref tag, int depth, ObjectId tip) } 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 @@ String describe(ObjectId tip) throws IOException { // 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; }