Skip to content

Commit 6972365

Browse files
committed
Reduce ratchet memory usage for multi-module projects
Ratchet creates a cache of Git repository per project but each Git repository is a new FileRepository instance even if two projects share the same repository on disk. Since FileRepository is thread-safe, change GitRatchet to cache FileRepository instances per git repository
1 parent 6dfa398 commit 6972365

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
138138
private final static int WORKDIR = 2;
139139

140140
Map<Project, Repository> gitRoots = new HashMap<>();
141+
Map<File, Repository> gitRepositories = new HashMap<>();
141142
Table<Repository, String, ObjectId> rootTreeShaCache = HashBasedTable.create();
142143
Map<Project, ObjectId> subtreeShaCache = new HashMap<>();
143144

@@ -174,7 +175,7 @@ protected Repository repositoryFor(Project project) throws IOException {
174175

175176
protected abstract @Nullable Project getParent(Project project);
176177

177-
private static @Nullable Repository traverseParentsUntil(File startWith, @Nullable File file) throws IOException {
178+
private @Nullable Repository traverseParentsUntil(File startWith, @Nullable File file) throws IOException {
178179
while (startWith != null && !Objects.equals(startWith, file)) {
179180
if (isGitRoot(startWith)) {
180181
return createRepo(startWith);
@@ -190,8 +191,14 @@ private static boolean isGitRoot(File dir) {
190191
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
191192
}
192193

193-
static Repository createRepo(File dir) throws IOException {
194-
return FileRepositoryBuilder.create(GitWorkarounds.getDotGitDir(dir));
194+
Repository createRepo(File dir) throws IOException {
195+
File dotGitDir = GitWorkarounds.getDotGitDir(dir);
196+
Repository repo = gitRepositories.get(dotGitDir);
197+
if (repo == null) {
198+
repo = FileRepositoryBuilder.create(dotGitDir);
199+
gitRepositories.put(dotGitDir, repo);
200+
}
201+
return repo;
195202
}
196203

197204
/**

0 commit comments

Comments
 (0)