Skip to content

Commit 567db30

Browse files
author
Erik
committed
Replace DiffCommand with IndexDiff
DiffCommand suffered from not taking smudge/clean filters into account
1 parent d9cf50a commit 567db30

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.eclipse.aether.RepositorySystem;
4646
import org.eclipse.aether.RepositorySystemSession;
4747
import org.eclipse.aether.repository.RemoteRepository;
48-
import org.eclipse.jgit.api.errors.GitAPIException;
4948

5049
import com.diffplug.spotless.Formatter;
5150
import com.diffplug.spotless.LineEnding;
@@ -190,8 +189,6 @@ private List<File> collectFilesFromGit(FormatterFactory formatterFactory, String
190189
.instance().getDirtyFiles(baseDir, ratchetFrom);
191190
} catch (IOException e) {
192191
throw new MojoExecutionException("Unable to scan file tree rooted at " + baseDir, e);
193-
} catch (GitAPIException e) {
194-
throw new MojoExecutionException("Error getting diff against 'ratchetFrom' setting '" + ratchetFrom + "'", e);
195192
}
196193

197194
List<File> result = new ArrayList<>();

plugin-maven/src/main/java/com/diffplug/spotless/maven/GitRatchetMaven.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919
import java.io.IOException;
2020
import java.nio.file.Path;
2121
import java.nio.file.Paths;
22-
import java.util.List;
22+
import java.util.Collection;
2323
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2425

25-
import org.eclipse.jgit.api.Git;
26-
import org.eclipse.jgit.api.errors.GitAPIException;
27-
import org.eclipse.jgit.diff.DiffEntry;
26+
import org.eclipse.jgit.lib.IndexDiff;
2827
import org.eclipse.jgit.lib.ObjectId;
29-
import org.eclipse.jgit.lib.ObjectReader;
3028
import org.eclipse.jgit.lib.Repository;
31-
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
29+
import org.eclipse.jgit.treewalk.FileTreeIterator;
3230

3331
import com.diffplug.spotless.extra.GitRatchet;
3432

@@ -58,26 +56,23 @@ static GitRatchetMaven instance() {
5856
return instance;
5957
}
6058

61-
Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom)
62-
throws IOException, GitAPIException {
59+
Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom) throws IOException {
6360
Repository repository = repositoryFor(baseDir);
6461
ObjectId sha = rootTreeShaOf(baseDir, ratchetFrom);
6562

66-
ObjectReader oldReader = repository.newObjectReader();
67-
CanonicalTreeParser oldTree = new CanonicalTreeParser();
68-
oldTree.reset(oldReader, sha);
69-
70-
Git git = new Git(repository);
71-
List<DiffEntry> diffs = git.diff()
72-
.setShowNameAndStatusOnly(true)
73-
.setOldTree(oldTree)
74-
.call();
63+
IndexDiff indexDiff = new IndexDiff(repository, sha, new FileTreeIterator(repository));
64+
indexDiff.diff();
7565

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

79-
return diffs.stream()
80-
.map(DiffEntry::getNewPath)
69+
return Stream.of(
70+
indexDiff.getAdded(),
71+
indexDiff.getChanged(),
72+
indexDiff.getConflicting(),
73+
indexDiff.getModified(),
74+
indexDiff.getUntracked())
75+
.flatMap(Collection::parallelStream)
8176
.map(path -> Paths.get(workTreePath, path))
8277
.map(path -> baseDirPath.relativize(path).toString())
8378
.collect(Collectors.toList());

0 commit comments

Comments
 (0)