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
This commit is contained in:
Matthias Sohn 2022-02-20 12:00:49 +01:00
parent a2d5650b8f
commit 85d8b31cb2
2 changed files with 19 additions and 11 deletions

View File

@ -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

View File

@ -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;
}