From 70ae16d708e0a207035872a16943f8b9151f33cd Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Tue, 18 Sep 2012 19:49:35 +0200 Subject: [PATCH] Fix resolving expression with ~ and ^ than extends beyond history resolve("foo~X") where X is greater than the distance from foo to the root should return null, but 2a2362fb introduced a bug causing it to either return resolve("foo") or NPE. Add a test for the correct behavior. Also add an analogous test for foo^X where X is greater than the number of parents (which was not broken by that commit). Change-Id: Ic580081ece57c8c2df29b652897b425ecb34e11f --- .../jgit/lib/RepositoryResolveTest.java | 23 +++++++++++++++++++ .../src/org/eclipse/jgit/lib/Repository.java | 5 +++- 2 files changed, 27 insertions(+), 1 deletion(-) 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 deb830ff5b..defd93b107 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 @@ -99,6 +99,15 @@ public class RepositoryResolveTest extends SampleDataRepositoryTestCase { assertEquals("d0114ab8ac326bab30e3a657a0397578c5a1af88",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^03").name()); } + @Test + public void testObjectId_objectid_invalid_explicit_parent() throws IOException { + assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4^1").name()); + assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4^2")); + assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^0").name()); + assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^1")); + assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1^2")); + } + @Test public void testRef_refname() throws IOException { assertEquals("49322bb17d3acc9146f98c97d078513228bbf3c0",db.resolve("master^0").name()); @@ -120,6 +129,20 @@ public class RepositoryResolveTest extends SampleDataRepositoryTestCase { assertEquals("1203b03dc816ccbb67773f28b3c19318654b0bc8",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0~~~0").name()); } + @Test + public void testDistance_past_root() throws IOException { + assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~1").name()); + assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~~")); + assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~2")); + assertNull(db.resolve("6462e7d8024396b14d7651e2ec11e2bbf07a05c4~99")); + assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~~")); + assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~2")); + assertNull(db.resolve("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1~99")); + assertEquals("42e4e7c5e507e113ebbb7801b16b52cf867b7ce1",db.resolve("master~6").name()); + assertNull(db.resolve("master~7")); + assertNull(db.resolve("master~6~")); + } + @Test public void testTree() throws IOException { assertEquals("6020a3b8d5d636e549ccbd0c53e2764684bb3125",db.resolve("49322bb17d3acc9146f98c97d078513228bbf3c0^{tree}").name()); 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 82394dd757..16f8cdfef7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java @@ -532,6 +532,7 @@ public abstract class Repository { if (name == null) name = new String(revChars, done, i); rev = parseSimple(rw, name); + name = null; if (rev == null) return null; } @@ -588,6 +589,7 @@ public abstract class Repository { // detached name = Constants.HEAD; Ref ref = getRef(name); + name = null; if (ref == null) return null; if (ref.isSymbolic()) @@ -635,6 +637,7 @@ public abstract class Repository { if (name.equals("")) name = Constants.HEAD; Ref ref = getRef(name); + name = null; if (ref == null) return null; // @{n} means current branch, not HEAD@{1} unless @@ -642,7 +645,6 @@ public abstract class Repository { if (ref.isSymbolic()) ref = ref.getLeaf(); rev = resolveReflog(rw, ref, time); - name = null; } i = m; } else @@ -656,6 +658,7 @@ public abstract class Repository { if (name.equals("")) name = Constants.HEAD; rev = parseSimple(rw, name); + name = null; } if (rev == null) return null; -- 2.39.5