diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java index 5ad913c56..d55856ace 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RepositoryResolveTest.java @@ -48,6 +48,7 @@ package org.eclipse.jgit.lib; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import java.io.IOException; @@ -181,6 +182,24 @@ public class RepositoryResolveTest extends SampleDataRepositoryTestCase { assertEquals(db.resolve("b~2"), db.resolve("B-6-g7f82283~2")); } + @Test + public void testParseNonGitDescribe() throws IOException { + ObjectId id = id("49322bb17d3acc9146f98c97d078513228bbf3c0"); + RefUpdate ru = db.updateRef("refs/heads/foo-g032c"); + ru.setNewObjectId(id); + assertSame(RefUpdate.Result.NEW, ru.update()); + + assertEquals(id, db.resolve("refs/heads/foo-g032c")); + assertEquals(id, db.resolve("foo-g032c")); + + ru = db.updateRef("refs/heads/foo-g032c-dev"); + ru.setNewObjectId(id); + assertSame(RefUpdate.Result.NEW, ru.update()); + + assertEquals(id, db.resolve("refs/heads/foo-g032c-dev")); + assertEquals(id, db.resolve("foo-g032c-dev")); + } + @Test public void testParseLookupPath() throws IOException { ObjectId b2_txt = id("10da5895682013006950e7da534b705252b03be6"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java index c4d2d73bc..6916a86f4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -577,24 +577,6 @@ public abstract class Repository { revstr); i = m - 1; break; - case '-': - if (i + 4 < rev.length && rev[i + 1] == 'g' - && isHex(rev[i + 2]) && isHex(rev[i + 3])) { - // Possibly output from git describe? - // Resolve longest valid abbreviation. - int cnt = 2; - while (i + 2 + cnt < rev.length && isHex(rev[i + 2 + cnt])) - cnt++; - String s = new String(rev, i + 2, cnt); - if (AbbreviatedObjectId.isId(s)) { - ObjectId id = resolveAbbreviation(s); - if (id != null) { - ref = rw.parseAny(id); - i += 1 + s.length(); - } - } - } - break; case ':': { RevTree tree; if (ref == null) { @@ -637,6 +619,14 @@ public abstract class Repository { || ('A' <= c && c <= 'F'); } + private static boolean isAllHex(String str, int ptr) { + while (ptr < str.length()) { + if (!isHex(str.charAt(ptr++))) + return false; + } + return true; + } + private RevObject parseSimple(RevWalk rw, String revstr) throws IOException { ObjectId id = resolveSimple(revstr); return id != null ? rw.parseAny(id) : null; @@ -653,6 +643,17 @@ public abstract class Repository { if (AbbreviatedObjectId.isId(revstr)) return resolveAbbreviation(revstr); + int dashg = revstr.indexOf("-g"); + if (4 < revstr.length() && 0 <= dashg + && isHex(revstr.charAt(dashg + 2)) + && isHex(revstr.charAt(dashg + 3)) + && isAllHex(revstr, dashg + 4)) { + // Possibly output from git describe? + String s = revstr.substring(dashg + 2); + if (AbbreviatedObjectId.isId(s)) + return resolveAbbreviation(s); + } + return null; }