Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2025 DiffPlug
* Copyright 2020-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,11 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -30,6 +34,9 @@
import com.diffplug.spotless.extra.GitRatchet;

final class GitRatchetMaven extends GitRatchet<File> {

Map<Key, Set<String>> cache = new HashMap<>();

private GitRatchetMaven() {}

@Override
Expand All @@ -55,16 +62,32 @@ static GitRatchetMaven instance() {
return instance;
}

Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom) throws IOException {
List<String> getDirtyFiles(File baseDir, String ratchetFrom) throws IOException {
Repository repository = repositoryFor(baseDir);
ObjectId sha = rootTreeShaOf(baseDir, ratchetFrom);
Set<String> dirtyPaths = null;
Key key = new Key(repository.getIdentifier(), ratchetFrom);
synchronized (this) {
if (cache.containsKey(key)) {
dirtyPaths = cache.get(key);
} else {
dirtyPaths = getDirtyFilesInternal(repository, baseDir, ratchetFrom);
cache.put(key, dirtyPaths);
}
}

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

return dirtyPaths.stream()
.map(path -> baseDirPath.relativize(Path.of(workTreePath, path)).toString())
.collect(Collectors.toList());
}

Set<String> getDirtyFilesInternal(Repository repository, File baseDir, String ratchetFrom) throws IOException {
ObjectId sha = rootTreeShaOf(baseDir, ratchetFrom);
IndexDiff indexDiff = new IndexDiff(repository, sha, new FileTreeIterator(repository));
indexDiff.diff();

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

Set<String> dirtyPaths = new HashSet<>(indexDiff.getChanged());
dirtyPaths.addAll(indexDiff.getAdded());
dirtyPaths.addAll(indexDiff.getConflicting());
Expand All @@ -91,8 +114,29 @@ Iterable<String> getDirtyFiles(File baseDir, String ratchetFrom) throws IOExcept
// A file can be modified in the index but removed in the tree
dirtyPaths.removeAll(indexDiff.getMissing());

return dirtyPaths.stream()
.map(path -> baseDirPath.relativize(Path.of(workTreePath, path)).toString())
.collect(Collectors.toList());
return dirtyPaths;
}

private static final class Key {
private final String identifier;
private final String ratchetFrom;

private Key(String identifier, String ratchetFrom) {
this.identifier = identifier;
this.ratchetFrom = ratchetFrom;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Key key = (Key) o;
return Objects.equals(identifier, key.identifier) && Objects.equals(ratchetFrom, key.ratchetFrom);
}

@Override
public int hashCode() {
return Objects.hash(identifier, ratchetFrom);
}
}
}