From ee6334bccf6d81b1bf513c44897e2a63b6300400 Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Tue, 6 Sep 2022 00:07:17 +0200 Subject: [PATCH] Revert "Option to pass start RevCommit to be blamed on to the BlameGenerator." This reverts commit 5747bba48b22a11beba8ebe0caf13a53d4ca96f2. This is done as a quick fix for the failure of egit tests caused by the introduction of FilteredRevCommit. Bug: 580690 Change-Id: Ia0178bc2de4fc825a81207bbd7979bf3a386c955 --- .../jgit/api/blame/BlameGeneratorTest.java | 329 ++---------------- .../eclipse/jgit/blame/BlameGenerator.java | 47 +-- 2 files changed, 39 insertions(+), 337 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java index b175ead8ec..f47f447375 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java @@ -13,330 +13,56 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.util.Iterator; - import org.eclipse.jgit.api.Git; import org.eclipse.jgit.blame.BlameGenerator; import org.eclipse.jgit.blame.BlameResult; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.merge.MergeStrategy; -import org.eclipse.jgit.revwalk.FilteredRevCommit; import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevWalk; import org.junit.Test; /** Unit tests of {@link BlameGenerator}. */ public class BlameGeneratorTest extends RepositoryTestCase { - - public static final String OTHER_FILE = "other_file.txt"; - - public static final String INTERESTING_FILE = "interesting_file.txt"; - @Test - public void testSingleBlame() throws Exception { - - /** - *
-		 * (ts) 	OTHER_FILE			INTERESTING_FILE
-		 * 1 		a
-		 * 2	 	a, b
-		 * 3							1, 2				c1 <--
-		 * 4	 	a, b, c										 |
-		 * 5							1, 2, 3				c2---
-		 * 
- */ - try (Git git = new Git(db); - RevWalk revWalk = new RevWalk(git.getRepository())) { - writeTrashFile(OTHER_FILE, join("a")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("create file").call(); - - writeTrashFile(OTHER_FILE, join("a", "b")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("amend file").call(); - - writeTrashFile(INTERESTING_FILE, join("1", "2")); - git.add().addFilepattern(INTERESTING_FILE).call(); + public void testBoundLineDelete() throws Exception { + try (Git git = new Git(db)) { + String[] content1 = new String[] { "first", "second" }; + writeTrashFile("file.txt", join(content1)); + git.add().addFilepattern("file.txt").call(); RevCommit c1 = git.commit().setMessage("create file").call(); - writeTrashFile(OTHER_FILE, join("a", "b", "c")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("amend file").call(); - - writeTrashFile(INTERESTING_FILE, join("1", "2", "3")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c2 = git.commit().setMessage("amend file").call(); - - RevCommit filteredC1 = new FilteredRevCommit(c1); - RevCommit filteredC2 = new FilteredRevCommit(c2, filteredC1); - - revWalk.parseHeaders(filteredC2); + String[] content2 = new String[] { "third", "first", "second" }; + writeTrashFile("file.txt", join(content2)); + git.add().addFilepattern("file.txt").call(); + RevCommit c2 = git.commit().setMessage("create file").call(); - try (BlameGenerator generator = new BlameGenerator(db, - INTERESTING_FILE)) { - generator.push(filteredC2); + try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) { + generator.push(null, db.resolve(Constants.HEAD)); assertEquals(3, generator.getResultContents().size()); assertTrue(generator.next()); assertEquals(c2, generator.getSourceCommit()); assertEquals(1, generator.getRegionLength()); - assertEquals(2, generator.getResultStart()); - assertEquals(3, generator.getResultEnd()); - assertEquals(2, generator.getSourceStart()); - assertEquals(3, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); - - assertTrue(generator.next()); - assertEquals(c1, generator.getSourceCommit()); - assertEquals(2, generator.getRegionLength()); assertEquals(0, generator.getResultStart()); - assertEquals(2, generator.getResultEnd()); + assertEquals(1, generator.getResultEnd()); assertEquals(0, generator.getSourceStart()); - assertEquals(2, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); - - assertFalse(generator.next()); - } - } - } - - @Test - public void testMergeSingleBlame() throws Exception { - try (Git git = new Git(db); - RevWalk revWalk = new RevWalk(git.getRepository())) { - - /** - * - * - *
-			 *  refs/heads/master
-			 *      A
-			 *     / \       		 refs/heads/side
-			 *    /   ---------------->  side
-			 *   /                        |
-			 *  merge <-------------------
-			 * 
- */ - - writeTrashFile(INTERESTING_FILE, join("1", "2")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - - createBranch(c1, "refs/heads/side"); - checkoutBranch("refs/heads/side"); - writeTrashFile(INTERESTING_FILE, join("1", "2", "3", "4")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit sideCommit = git.commit() - .setMessage("amend file in another branch").call(); - - checkoutBranch("refs/heads/master"); - git.merge().setMessage("merge").include(sideCommit) - .setStrategy(MergeStrategy.RESOLVE).call(); - - Iterator it = git.log().call().iterator(); - RevCommit mergeCommit = it.next(); - - RevCommit filteredC1 = new FilteredRevCommit(c1); - RevCommit filteredSide = new FilteredRevCommit(sideCommit, - filteredC1); - RevCommit filteredMerge = new FilteredRevCommit(mergeCommit, - filteredSide, filteredC1); - - revWalk.parseHeaders(filteredMerge); - - try (BlameGenerator generator = new BlameGenerator(db, - INTERESTING_FILE)) { - generator.push(filteredMerge); - assertEquals(4, generator.getResultContents().size()); - - assertTrue(generator.next()); - assertEquals(mergeCommit, generator.getSourceCommit()); - assertEquals(2, generator.getRegionLength()); - assertEquals(2, generator.getResultStart()); - assertEquals(4, generator.getResultEnd()); - assertEquals(2, generator.getSourceStart()); - assertEquals(4, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); + assertEquals(1, generator.getSourceEnd()); + assertEquals("file.txt", generator.getSourcePath()); assertTrue(generator.next()); - assertEquals(filteredC1, generator.getSourceCommit()); + assertEquals(c1, generator.getSourceCommit()); assertEquals(2, generator.getRegionLength()); - assertEquals(0, generator.getResultStart()); - assertEquals(2, generator.getResultEnd()); - assertEquals(0, generator.getSourceStart()); - assertEquals(2, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); - - assertFalse(generator.next()); - } - } - } - - @Test - public void testMergeBlame() throws Exception { - try (Git git = new Git(db); - RevWalk revWalk = new RevWalk(git.getRepository())) { - - /** - * - * - *
-			 *  refs/heads/master
-			 *      A
-			 *     / \       		 refs/heads/side
-			 *    B   ---------------->  side
-			 *   /                        |
-			 *  merge <-------------------
-			 * 
- */ - writeTrashFile(INTERESTING_FILE, join("1", "2")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - - createBranch(c1, "refs/heads/side"); - checkoutBranch("refs/heads/side"); - writeTrashFile(INTERESTING_FILE, join("1", "2", "3")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit sideCommit = git.commit().setMessage("amend file").call(); - - checkoutBranch("refs/heads/master"); - writeTrashFile(INTERESTING_FILE, join("1", "2", "4")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c2 = git.commit().setMessage("delete and amend file") - .call(); - - git.merge().setMessage("merge").include(sideCommit) - .setStrategy(MergeStrategy.RESOLVE).call(); - writeTrashFile(INTERESTING_FILE, join("1", "2", "3", "4")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit mergeCommit = git.commit().setMessage("merge commit") - .call(); - - RevCommit filteredC1 = new FilteredRevCommit(c1); - RevCommit filteredSide = new FilteredRevCommit(sideCommit, - filteredC1); - RevCommit filteredC2 = new FilteredRevCommit(c2, filteredC1); - - RevCommit filteredMerge = new FilteredRevCommit(mergeCommit, - filteredSide, filteredC2); - - revWalk.parseHeaders(filteredMerge); - - try (BlameGenerator generator = new BlameGenerator(db, - INTERESTING_FILE)) { - generator.push(filteredMerge); - assertEquals(4, generator.getResultContents().size()); - - assertTrue(generator.next()); - assertEquals(filteredC2, generator.getSourceCommit()); - assertEquals(1, generator.getRegionLength()); - assertEquals(3, generator.getResultStart()); - assertEquals(4, generator.getResultEnd()); - assertEquals(2, generator.getSourceStart()); - assertEquals(3, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); - - assertTrue(generator.next()); - assertEquals(filteredSide, generator.getSourceCommit()); - assertEquals(1, generator.getRegionLength()); - assertEquals(2, generator.getResultStart()); + assertEquals(1, generator.getResultStart()); assertEquals(3, generator.getResultEnd()); - assertEquals(2, generator.getSourceStart()); - assertEquals(3, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); - - assertTrue(generator.next()); - assertEquals(filteredC1, generator.getSourceCommit()); - assertEquals(2, generator.getRegionLength()); - assertEquals(0, generator.getResultStart()); - assertEquals(2, generator.getResultEnd()); assertEquals(0, generator.getSourceStart()); assertEquals(2, generator.getSourceEnd()); - assertEquals(INTERESTING_FILE, generator.getSourcePath()); + assertEquals("file.txt", generator.getSourcePath()); assertFalse(generator.next()); } } } - @Test - public void testSingleBlame_compareWithWalk() throws Exception { - /** - *
-		 * (ts) 	OTHER_FILE			INTERESTING_FILE
-		 * 1 		a
-		 * 2	 	a, b
-		 * 3							1, 2				c1 <--
-		 * 4	 	a, b, c										 |
-		 * 6							3, 1, 2				c2---
-		 * 
- */ - try (Git git = new Git(db); - RevWalk revWalk = new RevWalk(git.getRepository())) { - writeTrashFile(OTHER_FILE, join("a")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("create file").call(); - - writeTrashFile(OTHER_FILE, join("a", "b")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("amend file").call(); - - writeTrashFile(INTERESTING_FILE, join("1", "2")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c1 = git.commit().setMessage("create file").call(); - - writeTrashFile(OTHER_FILE, join("a", "b", "c")); - git.add().addFilepattern(OTHER_FILE).call(); - git.commit().setMessage("amend file").call(); - - writeTrashFile(INTERESTING_FILE, join("3", "1", "2")); - git.add().addFilepattern(INTERESTING_FILE).call(); - RevCommit c2 = git.commit().setMessage("prepend").call(); - - RevCommit filteredC1 = new FilteredRevCommit(c1); - RevCommit filteredC2 = new FilteredRevCommit(c2, filteredC1); - - revWalk.parseHeaders(filteredC2); - - try (BlameGenerator g1 = new BlameGenerator(db, INTERESTING_FILE); - BlameGenerator g2 = new BlameGenerator(db, - INTERESTING_FILE)) { - g1.push(null, c2); - g2.push(null, filteredC2); - - assertEquals(g1.getResultContents().size(), - g2.getResultContents().size()); // 3 - - assertTrue(g1.next()); - assertTrue(g2.next()); - - assertEquals(g1.getSourceCommit(), g2.getSourceCommit()); // c2 - assertEquals(INTERESTING_FILE, g1.getSourcePath()); - assertEquals(g1.getRegionLength(), g2.getRegionLength()); // 1 - assertEquals(g1.getResultStart(), g2.getResultStart()); // 0 - assertEquals(g1.getResultEnd(), g2.getResultEnd()); // 1 - assertEquals(g1.getSourceStart(), g2.getSourceStart()); // 0 - assertEquals(g1.getSourceEnd(), g2.getSourceEnd()); // 1 - assertEquals(g1.getSourcePath(), g2.getSourcePath()); // INTERESTING_FILE - - assertTrue(g1.next()); - assertTrue(g2.next()); - - assertEquals(g1.getSourceCommit(), g2.getSourceCommit()); // c1 - assertEquals(g1.getRegionLength(), g2.getRegionLength()); // 2 - assertEquals(g1.getResultStart(), g2.getResultStart()); // 1 - assertEquals(g1.getResultEnd(), g2.getResultEnd()); // 3 - assertEquals(g1.getSourceStart(), g2.getSourceStart()); // 0 - assertEquals(g1.getSourceEnd(), g2.getSourceEnd()); // 2 - assertEquals(g1.getSourcePath(), g2.getSourcePath()); // INTERESTING_FILE - - assertFalse(g1.next()); - assertFalse(g2.next()); - } - } - } - @Test public void testRenamedBoundLineDelete() throws Exception { try (Git git = new Git(db)) { @@ -361,8 +87,7 @@ public class BlameGeneratorTest extends RepositoryTestCase { git.add().addFilepattern(FILENAME_2).call(); RevCommit c2 = git.commit().setMessage("change file2").call(); - try (BlameGenerator generator = new BlameGenerator(db, - FILENAME_2)) { + try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) { generator.push(null, db.resolve(Constants.HEAD)); assertEquals(3, generator.getResultContents().size()); @@ -388,8 +113,7 @@ public class BlameGeneratorTest extends RepositoryTestCase { } // and test again with other BlameGenerator API: - try (BlameGenerator generator = new BlameGenerator(db, - FILENAME_2)) { + try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) { generator.push(null, db.resolve(Constants.HEAD)); BlameResult result = generator.computeBlameResult(); @@ -412,22 +136,21 @@ public class BlameGeneratorTest extends RepositoryTestCase { try (Git git = new Git(db)) { String[] content1 = new String[] { "first", "second", "third" }; - writeTrashFile(INTERESTING_FILE, join(content1)); - git.add().addFilepattern(INTERESTING_FILE).call(); + writeTrashFile("file.txt", join(content1)); + git.add().addFilepattern("file.txt").call(); git.commit().setMessage("create file").call(); String[] content2 = new String[] { "" }; - writeTrashFile(INTERESTING_FILE, join(content2)); - git.add().addFilepattern(INTERESTING_FILE).call(); + writeTrashFile("file.txt", join(content2)); + git.add().addFilepattern("file.txt").call(); git.commit().setMessage("create file").call(); - writeTrashFile(INTERESTING_FILE, join(content1)); - git.add().addFilepattern(INTERESTING_FILE).call(); + writeTrashFile("file.txt", join(content1)); + git.add().addFilepattern("file.txt").call(); RevCommit c3 = git.commit().setMessage("create file").call(); - try (BlameGenerator generator = new BlameGenerator(db, - INTERESTING_FILE)) { + try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) { generator.push(null, db.resolve(Constants.HEAD)); assertEquals(3, generator.getResultContents().size()); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java index 93ddfc6607..77967df2e5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java @@ -129,7 +129,6 @@ public class BlameGenerator implements AutoCloseable { /** Blame is currently assigned to this source. */ private Candidate outCandidate; - private Region outRegion; /** @@ -396,35 +395,6 @@ public class BlameGenerator implements AutoCloseable { return copy; } - /** - * Push a candidate object onto the generator's traversal stack. - *

- * Candidates should be pushed in history order from oldest-to-newest. - * Applications should push the starting commit first, then the index - * revision (if the index is interesting), and finally the working tree copy - * (if the working tree is interesting). - * - * @param blameCommit - * ordered commits to use instead of RevWalk. - * @return {@code this} - * @throws java.io.IOException - * the repository cannot be read. - * @since 6.3 - */ - public BlameGenerator push(RevCommit blameCommit) throws IOException { - if (!find(blameCommit, resultPath)) { - return this; - } - - Candidate c = new Candidate(getRepository(), blameCommit, resultPath); - c.sourceBlob = idBuf.toObjectId(); - c.loadText(reader); - c.regionList = new Region(0, 0, c.sourceText.size()); - remaining = c.sourceText.size(); - push(c); - return this; - } - /** * Push a candidate object onto the generator's traversal stack. *

@@ -458,7 +428,16 @@ public class BlameGenerator implements AutoCloseable { } RevCommit commit = revPool.parseCommit(id); - return push(commit); + if (!find(commit, resultPath)) + return this; + + Candidate c = new Candidate(getRepository(), commit, resultPath); + c.sourceBlob = idBuf.toObjectId(); + c.loadText(reader); + c.regionList = new Region(0, 0, c.sourceText.size()); + remaining = c.sourceText.size(); + push(c); + return this; } /** @@ -626,7 +605,7 @@ public class BlameGenerator implements AutoCloseable { // Do not generate a tip of a reverse. The region // survives and should not appear to be deleted. - } else /* if (pCnt == 0) */ { + } else /* if (pCnt == 0) */{ // Root commit, with at least one surviving region. // Assign the remaining blame here. return result(n); @@ -867,8 +846,8 @@ public class BlameGenerator implements AutoCloseable { editList = new EditList(0); } else { p.loadText(reader); - editList = diffAlgorithm.diff(textComparator, p.sourceText, - n.sourceText); + editList = diffAlgorithm.diff(textComparator, + p.sourceText, n.sourceText); } if (editList.isEmpty()) { -- 2.39.5