aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarsten Pfeiffer2011-10-27 18:15:27 (EDT)
committerChris Aniszczyk2011-11-10 14:06:03 (EST)
commit92752f6b50ba8c2a1d1946abac0064ea2e318591 (patch)
treec692cf57cb283618e1a8c27f2e8b705ec0325fba
parentda901c49688ef72858cd2b6904e0f8e478fa0ec3 (diff)
downloadjgit-92752f6b50ba8c2a1d1946abac0064ea2e318591.zip
jgit-92752f6b50ba8c2a1d1946abac0064ea2e318591.tar.gz
jgit-92752f6b50ba8c2a1d1946abac0064ea2e318591.tar.bz2
[blame] Fix blame following renames in non-toplevel directoriesrefs/changes/68/4368/4
Mark the treeWalk as recursive; otherwise following renames only works for toplevel files. Bug: 302549 Change-Id: I70867928eadf332b0942f8bf6877a3acb3828c87 Signed-off-by: Carsten Pfeiffer <carsten.pfeiffer@gebit.de> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com> Signed-off-by: Chris Aniszczyk <zx@twitter.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java37
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java75
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java1
3 files changed, 102 insertions, 11 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java
index cfae7ed..7e1c22c 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java
@@ -115,39 +115,54 @@ public class BlameCommandTest extends RepositoryTestCase {
@Test
public void testRename() throws Exception {
+ testRename("file1.txt", "file2.txt");
+ }
+
+ @Test
+ public void testRenameInSubDir() throws Exception {
+ testRename("subdir/file1.txt", "subdir/file2.txt");
+ }
+
+ @Test
+ public void testMoveToOtherDir() throws Exception {
+ testRename("subdir/file1.txt", "otherdir/file1.txt");
+ }
+
+ private void testRename(final String sourcePath, final String destPath)
+ throws Exception {
Git git = new Git(db);
String[] content1 = new String[] { "a", "b", "c" };
- writeTrashFile("file.txt", join(content1));
- git.add().addFilepattern("file.txt").call();
+ writeTrashFile(sourcePath, join(content1));
+ git.add().addFilepattern(sourcePath).call();
RevCommit commit1 = git.commit().setMessage("create file").call();
- writeTrashFile("file1.txt", join(content1));
- git.add().addFilepattern("file1.txt").call();
- git.rm().addFilepattern("file.txt").call();
+ writeTrashFile(destPath, join(content1));
+ git.add().addFilepattern(destPath).call();
+ git.rm().addFilepattern(sourcePath).call();
git.commit().setMessage("moving file").call();
String[] content2 = new String[] { "a", "b", "c2" };
- writeTrashFile("file1.txt", join(content2));
- git.add().addFilepattern("file1.txt").call();
+ writeTrashFile(destPath, join(content2));
+ git.add().addFilepattern(destPath).call();
RevCommit commit3 = git.commit().setMessage("editing file").call();
BlameCommand command = new BlameCommand(db);
command.setFollowFileRenames(true);
- command.setFilePath("file1.txt");
+ command.setFilePath(destPath);
BlameResult lines = command.call();
assertEquals(commit1, lines.getSourceCommit(0));
assertEquals(0, lines.getSourceLine(0));
- assertEquals("file.txt", lines.getSourcePath(0));
+ assertEquals(sourcePath, lines.getSourcePath(0));
assertEquals(commit1, lines.getSourceCommit(1));
assertEquals(1, lines.getSourceLine(1));
- assertEquals("file.txt", lines.getSourcePath(1));
+ assertEquals(sourcePath, lines.getSourcePath(1));
assertEquals(commit3, lines.getSourceCommit(2));
assertEquals(2, lines.getSourceLine(2));
- assertEquals("file1.txt", lines.getSourcePath(2));
+ assertEquals(destPath, lines.getSourcePath(2));
}
@Test
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 657cf38..d021282 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
@@ -48,6 +48,7 @@ import static org.junit.Assert.assertTrue;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.blame.BlameGenerator;
+import org.eclipse.jgit.blame.BlameResult;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.RepositoryTestCase;
import org.eclipse.jgit.revwalk.RevCommit;
@@ -99,6 +100,80 @@ public class BlameGeneratorTest extends RepositoryTestCase {
}
@Test
+ public void testRenamedBoundLineDelete() throws Exception {
+ Git git = new Git(db);
+ final String FILENAME_1 = "subdir/file1.txt";
+ final String FILENAME_2 = "subdir/file2.txt";
+
+ String[] content1 = new String[] { "first", "second" };
+ writeTrashFile(FILENAME_1, join(content1));
+ git.add().addFilepattern(FILENAME_1).call();
+ RevCommit c1 = git.commit().setMessage("create file1").call();
+
+ // rename it
+ writeTrashFile(FILENAME_2, join(content1));
+ git.add().addFilepattern(FILENAME_2).call();
+ deleteTrashFile(FILENAME_1);
+ git.rm().addFilepattern(FILENAME_1).call();
+ git.commit().setMessage("rename file1.txt to file2.txt").call();
+
+ // and change the new file
+ String[] content2 = new String[] { "third", "first", "second" };
+ writeTrashFile(FILENAME_2, join(content2));
+ git.add().addFilepattern(FILENAME_2).call();
+ RevCommit c2 = git.commit().setMessage("change file2").call();
+
+ BlameGenerator generator = new BlameGenerator(db, FILENAME_2);
+ try {
+ generator.push(null, db.resolve(Constants.HEAD));
+ assertEquals(3, generator.getResultContents().size());
+
+ assertTrue(generator.next());
+ assertEquals(c2, generator.getSourceCommit());
+ assertEquals(1, generator.getRegionLength());
+ assertEquals(0, generator.getResultStart());
+ assertEquals(1, generator.getResultEnd());
+ assertEquals(0, generator.getSourceStart());
+ assertEquals(1, generator.getSourceEnd());
+ assertEquals(FILENAME_2, generator.getSourcePath());
+
+ assertTrue(generator.next());
+ assertEquals(c1, generator.getSourceCommit());
+ assertEquals(2, generator.getRegionLength());
+ assertEquals(1, generator.getResultStart());
+ assertEquals(3, generator.getResultEnd());
+ assertEquals(0, generator.getSourceStart());
+ assertEquals(2, generator.getSourceEnd());
+ assertEquals(FILENAME_1, generator.getSourcePath());
+
+ assertFalse(generator.next());
+ } finally {
+ generator.release();
+ }
+
+ // and test again with other BlameGenerator API:
+ generator = new BlameGenerator(db, FILENAME_2);
+ try {
+ generator.push(null, db.resolve(Constants.HEAD));
+ BlameResult result = generator.computeBlameResult();
+
+ assertEquals(3, result.getResultContents().size());
+
+ assertEquals(c2, result.getSourceCommit(0));
+ assertEquals(FILENAME_2, result.getSourcePath(0));
+
+ assertEquals(c1, result.getSourceCommit(1));
+ assertEquals(FILENAME_1, result.getSourcePath(1));
+
+ assertEquals(c1, result.getSourceCommit(2));
+ assertEquals(FILENAME_1, result.getSourcePath(2));
+
+ } finally {
+ generator.release();
+ }
+ }
+
+ @Test
public void testLinesAllDeletedShortenedWalk() throws Exception {
Git git = new Git(db);
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 5157364..028817f 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java
@@ -178,6 +178,7 @@ public class BlameGenerator {
SEEN = revPool.newFlag("SEEN");
reader = revPool.getObjectReader();
treeWalk = new TreeWalk(reader);
+ treeWalk.setRecursive(true);
}
/** @return repository being scanned for revision history. */