diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java index aa67268ac2..c8ed2e6e51 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java @@ -50,11 +50,13 @@ final class Differ implements AutoCloseable { private final RevWalk walk; private final TreeWalk treeWalk; private final RevCommit commit; + private final PathFilter pathFilter; - private Differ(RevCommit commit, RevWalk walk, TreeWalk treeWalk) { + private Differ(RevCommit commit, RevWalk walk, TreeWalk treeWalk, PathFilter pathFilter) { this.commit = commit; this.walk = walk; this.treeWalk = treeWalk; + this.pathFilter = pathFilter; } static Diff diff(Repository repository, DiffCommandRequest request) throws IOException { @@ -83,11 +85,11 @@ final class Differ implements AutoCloseable { treeWalk.reset(); treeWalk.setRecursive(true); + PathFilter pathFilter = null; if (Util.isNotEmpty(request.getPath())) { - treeWalk.setFilter(PathFilter.create(request.getPath())); + pathFilter = PathFilter.create(request.getPath()); } - if (!Strings.isNullOrEmpty(request.getAncestorChangeset())) { ObjectId otherRevision = repository.resolve(request.getAncestorChangeset()); ObjectId ancestorId = GitUtil.computeCommonAncestor(repository, revision, otherRevision); @@ -107,13 +109,16 @@ final class Differ implements AutoCloseable { treeWalk.addTree(commit.getTree()); - return new Differ(commit, walk, treeWalk); + return new Differ(commit, walk, treeWalk, pathFilter); } private Diff diff(Repository repository) throws IOException { DiffFormatter diffFormatter = new DiffFormatter(null); diffFormatter.setRepository(repository); diffFormatter.setDetectRenames(true); + if (pathFilter != null) { + diffFormatter.setPathFilter(pathFilter); + } List entries = diffFormatter.scan( treeWalk.getTree(0, AbstractTreeIterator.class), treeWalk.getTree(1, AbstractTreeIterator.class)); diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitDiffCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitDiffCommand.java index 29c9f81b14..0e851456a7 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitDiffCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitDiffCommand.java @@ -57,7 +57,7 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand { formatter.setRepository(repository); for (DiffEntry e : diff.getEntries()) { - if (!e.getOldId().equals(e.getNewId())) { + if (!e.getOldId().equals(e.getNewId()) || !e.getNewPath().equals(e.getOldPath())) { formatter.format(e); } } diff --git a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitDiffCommandTest.java b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitDiffCommandTest.java index 042c1f3186..7a9e9f660e 100644 --- a/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitDiffCommandTest.java +++ b/scm-plugins/scm-git-plugin/src/test/java/sonia/scm/repository/spi/GitDiffCommandTest.java @@ -24,11 +24,13 @@ package sonia.scm.repository.spi; +import org.assertj.core.api.Assertions; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.IOException; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; public class GitDiffCommandTest extends AbstractGitCommandTestBase { @@ -140,4 +142,19 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase { gitDiffCommand.getDiffResult(diffCommandRequest).accept(output); assertEquals(DIFF_FILE_PARTIAL_MERGE, output.toString()); } + + @Test + public void diffBetweenTwoBranchesWithMovedFiles() throws IOException { + GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext()); + DiffCommandRequest diffCommandRequest = new DiffCommandRequest(); + diffCommandRequest.setRevision("rename"); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + gitDiffCommand.getDiffResult(diffCommandRequest).accept(output); + assertThat(output.toString()) + .contains("similarity index 100%") + .contains("rename from a.txt") + .contains("rename to a-copy.txt") + .contains("rename from b.txt") + .contains("rename to b-copy.txt"); + } } diff --git a/scm-plugins/scm-git-plugin/src/test/resources/sonia/scm/repository/spi/scm-git-spi-test.zip b/scm-plugins/scm-git-plugin/src/test/resources/sonia/scm/repository/spi/scm-git-spi-test.zip index da166155a7..80483d04d1 100644 Binary files a/scm-plugins/scm-git-plugin/src/test/resources/sonia/scm/repository/spi/scm-git-spi-test.zip and b/scm-plugins/scm-git-plugin/src/test/resources/sonia/scm/repository/spi/scm-git-spi-test.zip differ