Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
- Add the ability to specify a wildcard version (`*`) for external formatter executables. ([#2757](https://github.com/diffplug/spotless/issues/2757))
### Changes
- Dramatic (~100x) performance improvement when using git `ratchetFrom`. ([#2805](https://github.com/diffplug/spotless/pull/2805))
### Fixed
- [fix] `NPE` due to workingTreeIterator being null for git ignored files. #911 ([#2771](https://github.com/diffplug/spotless/issues/2771))
- Prevent race conditions when multiple npm-based formatters launch the server process simultaneously while sharing the same `node_modules` directory. ([#2786](https://github.com/diffplug/spotless/pull/2786))
Expand Down
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,30 @@ 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);
}
}
}
Loading