From e18444de30f1f019b5f7f0a463da42a884cb6a9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BCdiger=20Herrmann?= Date: Thu, 12 Nov 2015 15:54:13 +0100 Subject: [PATCH] Fix MissingObjectException in RenameDetector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When attempting to determine the size of a blob that does not exist, the RenameDetector throws a MissingObjectException. The fix is to return a size of zero if the size is requested for a blob id that doesn't exist. Bug: 481577 Change-Id: I4e86276039c630617610cc51d0eefa56d7d3952f Signed-off-by: RĂ¼diger Herrmann --- .../eclipse/jgit/diff/RenameDetectorTest.java | 31 +++++++++++++++++++ .../org/eclipse/jgit/diff/ContentSource.java | 6 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java index e83ef772f..4315be9e4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java @@ -48,6 +48,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.Arrays; import java.util.List; import org.eclipse.jgit.diff.DiffEntry.ChangeType; @@ -226,6 +227,19 @@ public class RenameDetectorTest extends RepositoryTestCase { assertCopy(d, b, 100, entries.get(2)); } + @Test + public void testExactRename_UnstagedFile() throws Exception { + ObjectId aId = blob("foo"); + DiffEntry a = DiffEntry.delete(PATH_A, aId); + DiffEntry b = DiffEntry.add(PATH_B, aId); + + rd.addAll(Arrays.asList(a, b)); + List entries = rd.compute(); + + assertEquals(1, entries.size()); + assertRename(a, b, 100, entries.get(0)); + } + @Test public void testInexactRename_OnePair() throws Exception { ObjectId aId = blob("foo\nbar\nbaz\nblarg\n"); @@ -429,6 +443,23 @@ public class RenameDetectorTest extends RepositoryTestCase { assertSame(b, entries.get(1)); } + @Test + public void testNoRenames_UntrackedFile() throws Exception { + ObjectId aId = blob("foo"); + ObjectId bId = ObjectId + .fromString("3049eb6eee7e1318f4e78e799bf33f1e54af9cbf"); + + DiffEntry a = DiffEntry.delete(PATH_A, aId); + DiffEntry b = DiffEntry.add(PATH_B, bId); + + rd.addAll(Arrays.asList(a, b)); + List entries = rd.compute(); + + assertEquals(2, entries.size()); + assertSame(a, entries.get(0)); + assertSame(b, entries.get(1)); + } + @Test public void testBreakModify_BreakAll() throws Exception { ObjectId aId = blob("foo"); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/ContentSource.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/ContentSource.java index 0dc4b0578..444ab1cb8 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/ContentSource.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/ContentSource.java @@ -132,7 +132,11 @@ public abstract class ContentSource { @Override public long size(String path, ObjectId id) throws IOException { - return reader.getObjectSize(id, Constants.OBJ_BLOB); + try { + return reader.getObjectSize(id, Constants.OBJ_BLOB); + } catch (MissingObjectException ignore) { + return 0; + } } @Override