@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
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;
}
}
+ @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);