Skip to content

Commit 80f82d3

Browse files
authored
Reduce ratchet memory usage for multi-module projects (#1426)
2 parents f312ece + e46b9a1 commit 80f82d3

File tree

5 files changed

+18
-42
lines changed

5 files changed

+18
-42
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1315

1416
## [2.31.0] - 2022-11-24
1517
### Added
1618
* `importOrder` now support groups of imports without blank lines ([#1401](https://github.com/diffplug/spotless/pull/1401))
1719
### Fixed
1820
* Don't treat `@Value` as a type annotation [#1367](https://github.com/diffplug/spotless/pull/1367)
1921
* Support `ktlint_disabled_rules` in `ktlint` 0.47.x [#1378](https://github.com/diffplug/spotless/pull/1378)
22+
* Share git repositories across projects when using ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
2023
### Changes
2124
* Bump default `ktfmt` version to latest `0.40` -> `0.41` ([#1340](https://github.com/diffplug/spotless/pull/1340))
2225
* Bump default `scalafmt` version to latest `3.5.9` -> `3.6.1` ([#1373](https://github.com/diffplug/spotless/pull/1373))

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ spotless {
1818
trimTrailingWhitespace()
1919
endWithNewline()
2020
}
21-
}
21+
}

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

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2022 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.util.HashMap;
2121
import java.util.Map;
22-
import java.util.Objects;
2322
import java.util.Optional;
2423

2524
import javax.annotation.Nullable;
@@ -137,7 +136,7 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
137136
private final static int INDEX = 1;
138137
private final static int WORKDIR = 2;
139138

140-
Map<Project, Repository> gitRoots = new HashMap<>();
139+
Map<File, Repository> gitRoots = new HashMap<>();
141140
Table<Repository, String, ObjectId> rootTreeShaCache = HashBasedTable.create();
142141
Map<Project, ObjectId> subtreeShaCache = new HashMap<>();
143142

@@ -147,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
147146
* We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it.
148147
*/
149148
protected Repository repositoryFor(Project project) throws IOException {
150-
Repository repo = gitRoots.get(project);
149+
File projectGitDir = GitWorkarounds.getDotGitDir(getDir(project));
150+
if (projectGitDir == null || !RepositoryCache.FileKey.isGitRepository(projectGitDir, FS.DETECTED)) {
151+
throw new IllegalArgumentException("Cannot find git repository in any parent directory");
152+
}
153+
Repository repo = gitRoots.get(projectGitDir);
151154
if (repo == null) {
152-
if (isGitRoot(getDir(project))) {
153-
repo = createRepo(getDir(project));
154-
} else {
155-
Project parentProj = getParent(project);
156-
if (parentProj == null) {
157-
repo = traverseParentsUntil(getDir(project).getParentFile(), null);
158-
if (repo == null) {
159-
throw new IllegalArgumentException("Cannot find git repository in any parent directory");
160-
}
161-
} else {
162-
repo = traverseParentsUntil(getDir(project).getParentFile(), getDir(parentProj));
163-
if (repo == null) {
164-
repo = repositoryFor(parentProj);
165-
}
166-
}
167-
}
168-
gitRoots.put(project, repo);
155+
repo = FileRepositoryBuilder.create(projectGitDir);
156+
gitRoots.put(projectGitDir, repo);
169157
}
170158
return repo;
171159
}
@@ -174,26 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException {
174162

175163
protected abstract @Nullable Project getParent(Project project);
176164

177-
private static @Nullable Repository traverseParentsUntil(File startWith, @Nullable File file) throws IOException {
178-
while (startWith != null && !Objects.equals(startWith, file)) {
179-
if (isGitRoot(startWith)) {
180-
return createRepo(startWith);
181-
} else {
182-
startWith = startWith.getParentFile();
183-
}
184-
}
185-
return null;
186-
}
187-
188-
private static boolean isGitRoot(File dir) {
189-
File dotGit = GitWorkarounds.getDotGitDir(dir);
190-
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
191-
}
192-
193-
static Repository createRepo(File dir) throws IOException {
194-
return FileRepositoryBuilder.create(GitWorkarounds.getDotGitDir(dir));
195-
}
196-
197165
/**
198166
* Fast way to return treeSha of the given ref against the git repository which stores the given project.
199167
* Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
68

79
## [6.12.0] - 2022-11-24
810
### Added

plugin-maven/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
68

79
## [2.28.0] - 2022-11-24
810
### Added
911
* `importOrder` now support groups of imports without blank lines ([#1401](https://github.com/diffplug/spotless/pull/1401))
1012
### Fixed
1113
* Don't treat `@Value` as a type annotation [#1367](https://github.com/diffplug/spotless/pull/1367)
1214
* Support `ktlint_disabled_rules` in `ktlint` 0.47.x [#1378](https://github.com/diffplug/spotless/pull/1378)
15+
* Share git repositories across projects when using ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1316
### Changes
1417
* Bump default `ktfmt` version to latest `0.40` -> `0.41` ([#1340](https://github.com/diffplug/spotless/pull/1340))
1518
* Bump default `scalafmt` version to latest `3.5.9` -> `3.6.1` ([#1373](https://github.com/diffplug/spotless/pull/1373))

0 commit comments

Comments
 (0)