Fix path handling

This commit is contained in:
René Pfeuffer
2020-05-11 14:21:25 +02:00
parent ae51a583fd
commit dbc58784e4
4 changed files with 27 additions and 5 deletions

View File

@@ -50,11 +50,13 @@ final class Differ implements AutoCloseable {
private final RevWalk walk; private final RevWalk walk;
private final TreeWalk treeWalk; private final TreeWalk treeWalk;
private final RevCommit commit; 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.commit = commit;
this.walk = walk; this.walk = walk;
this.treeWalk = treeWalk; this.treeWalk = treeWalk;
this.pathFilter = pathFilter;
} }
static Diff diff(Repository repository, DiffCommandRequest request) throws IOException { static Diff diff(Repository repository, DiffCommandRequest request) throws IOException {
@@ -83,11 +85,11 @@ final class Differ implements AutoCloseable {
treeWalk.reset(); treeWalk.reset();
treeWalk.setRecursive(true); treeWalk.setRecursive(true);
PathFilter pathFilter = null;
if (Util.isNotEmpty(request.getPath())) { if (Util.isNotEmpty(request.getPath())) {
treeWalk.setFilter(PathFilter.create(request.getPath())); pathFilter = PathFilter.create(request.getPath());
} }
if (!Strings.isNullOrEmpty(request.getAncestorChangeset())) { if (!Strings.isNullOrEmpty(request.getAncestorChangeset())) {
ObjectId otherRevision = repository.resolve(request.getAncestorChangeset()); ObjectId otherRevision = repository.resolve(request.getAncestorChangeset());
ObjectId ancestorId = GitUtil.computeCommonAncestor(repository, revision, otherRevision); ObjectId ancestorId = GitUtil.computeCommonAncestor(repository, revision, otherRevision);
@@ -107,13 +109,16 @@ final class Differ implements AutoCloseable {
treeWalk.addTree(commit.getTree()); treeWalk.addTree(commit.getTree());
return new Differ(commit, walk, treeWalk); return new Differ(commit, walk, treeWalk, pathFilter);
} }
private Diff diff(Repository repository) throws IOException { private Diff diff(Repository repository) throws IOException {
DiffFormatter diffFormatter = new DiffFormatter(null); DiffFormatter diffFormatter = new DiffFormatter(null);
diffFormatter.setRepository(repository); diffFormatter.setRepository(repository);
diffFormatter.setDetectRenames(true); diffFormatter.setDetectRenames(true);
if (pathFilter != null) {
diffFormatter.setPathFilter(pathFilter);
}
List<DiffEntry> entries = diffFormatter.scan( List<DiffEntry> entries = diffFormatter.scan(
treeWalk.getTree(0, AbstractTreeIterator.class), treeWalk.getTree(0, AbstractTreeIterator.class),
treeWalk.getTree(1, AbstractTreeIterator.class)); treeWalk.getTree(1, AbstractTreeIterator.class));

View File

@@ -57,7 +57,7 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
formatter.setRepository(repository); formatter.setRepository(repository);
for (DiffEntry e : diff.getEntries()) { 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); formatter.format(e);
} }
} }

View File

@@ -24,11 +24,13 @@
package sonia.scm.repository.spi; package sonia.scm.repository.spi;
import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class GitDiffCommandTest extends AbstractGitCommandTestBase { public class GitDiffCommandTest extends AbstractGitCommandTestBase {
@@ -140,4 +142,19 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
gitDiffCommand.getDiffResult(diffCommandRequest).accept(output); gitDiffCommand.getDiffResult(diffCommandRequest).accept(output);
assertEquals(DIFF_FILE_PARTIAL_MERGE, output.toString()); 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");
}
} }