Skip to content

Commit

Permalink
Replace DiffCommand with IndexDiff
Browse files Browse the repository at this point in the history
DiffCommand suffered from not taking smudge/clean filters into account
  • Loading branch information
Erik committed Sep 27, 2020
1 parent d9cf50a commit 3cbeca0
Showing 1 changed file with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.IndexDiff;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.FileTreeIterator;

import com.diffplug.spotless.extra.GitRatchet;

Expand Down Expand Up @@ -58,26 +56,23 @@ static GitRatchetMaven instance() {
return instance;
}

Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom)
throws IOException, GitAPIException {
Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom) throws IOException {
Repository repository = repositoryFor(baseDir);
ObjectId sha = rootTreeShaOf(baseDir, ratchetFrom);

ObjectReader oldReader = repository.newObjectReader();
CanonicalTreeParser oldTree = new CanonicalTreeParser();
oldTree.reset(oldReader, sha);

Git git = new Git(repository);
List<DiffEntry> diffs = git.diff()
.setShowNameAndStatusOnly(true)
.setOldTree(oldTree)
.call();
IndexDiff indexDiff = new IndexDiff(repository, sha, new FileTreeIterator(repository));
indexDiff.diff();

String workTreePath = repository.getWorkTree().getPath();
Path baseDirPath = Paths.get(baseDir.getPath());

return diffs.stream()
.map(DiffEntry::getNewPath)
return Stream.of(
indexDiff.getAdded(),
indexDiff.getChanged(),
indexDiff.getConflicting(),
indexDiff.getModified(),
indexDiff.getUntracked())
.flatMap(Collection::parallelStream)
.map(path -> Paths.get(workTreePath, path))
.map(path -> baseDirPath.relativize(path).toString())
.collect(Collectors.toList());
Expand Down

0 comments on commit 3cbeca0

Please sign in to comment.