]> source.dussan.org Git - jgit.git/commitdiff
[blame] Fix blame following renames in non-toplevel directories 68/4368/4
authorCarsten Pfeiffer <carsten.pfeiffer@gebit.de>
Thu, 27 Oct 2011 22:15:27 +0000 (00:15 +0200)
committerChris Aniszczyk <zx@twitter.com>
Thu, 10 Nov 2011 19:06:03 +0000 (11:06 -0800)
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>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BlameCommandTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/blame/BlameGeneratorTest.java
org.eclipse.jgit/src/org/eclipse/jgit/blame/BlameGenerator.java

index cfae7ed6a5e62e110e9816369210e581f7add453..7e1c22cb7da71bd18252607397699a7fa389a3e8 100644 (file)
@@ -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
index 657cf387080641551a7c1952d8f53cdb2fcf64a8..d021282b4b9b5876d9283fa3f3e97be43ae37950 100644 (file)
@@ -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;
@@ -98,6 +99,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);
index 5157364e15a2d267b1991a06aa0cae802365ee8c..028817fc253647349616d71fa2cff731589a02cd 100644 (file)
@@ -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. */