Skip to content

Commit 8891bfb

Browse files
author
conderls
committed
feat: 'ratchetFrom' in git submodule supported (diffplug#746)
1 parent 75fdd3e commit 8891bfb

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

CHANGES.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ 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-
### Changed
14-
* Update default ktfmt from 0.16 to 0.18 ([#748](https://github.com/diffplug/spotless/issues/748))
15-
16-
## [2.10.2] - 2020-11-16
17-
### Fixed
18-
* Fixed a bug which occurred if the root directory of the project was also the filesystem root ([#732](https://github.com/diffplug/spotless/pull/732))
19-
20-
## [2.10.1] - 2020-11-13
21-
### Fixed
22-
* Bump JGit from `5.8.0` to `5.9.0` to improve performance ([#726](https://github.com/diffplug/spotless/issues/726))
23-
24-
## [2.10.0] - 2020-11-02
25-
### Added
26-
* Added support to npm-based steps for picking up `.npmrc` files ([#727](https://github.com/diffplug/spotless/pull/727))
2713

2814
## [2.9.0] - 2020-10-20
2915
### Added

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.io.File;
1919
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
21+
import java.nio.file.Files;
2022
import java.util.HashMap;
2123
import java.util.Map;
2224
import java.util.Objects;
@@ -183,13 +185,37 @@ protected Repository repositoryFor(Project project) throws IOException {
183185
return null;
184186
}
185187

188+
/**
189+
* When populating a new submodule directory with "git submodule init", the $GIT_DIR meta-information directory
190+
* for submodules is created inside $GIT_DIR/modules// directory of the super-project
191+
* and referenced via the git-file mechanism.
192+
*/
193+
private static @Nullable File getDotGitDir(File dir, String dotGit) {
194+
File dotGitPath = new File(dir, dotGit);
195+
196+
if (dotGitPath.isDirectory()) {
197+
return dotGitPath;
198+
} else if (dotGitPath.isFile()) {
199+
try {
200+
String relativePath = new String(Files.readAllBytes(dotGitPath.toPath()), StandardCharsets.UTF_8)
201+
.split(":")[1].trim();
202+
return getDotGitDir(dir, relativePath);
203+
} catch (IOException e) {
204+
System.err.println("failed to parse git meta: " + e.getMessage());
205+
return null;
206+
}
207+
} else {
208+
return null;
209+
}
210+
}
211+
186212
private static boolean isGitRoot(File dir) {
187-
File dotGit = new File(dir, Constants.DOT_GIT);
188-
return dotGit.isDirectory() && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
213+
File dotGit = getDotGitDir(dir, Constants.DOT_GIT);
214+
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
189215
}
190216

191217
static Repository createRepo(File dir) throws IOException {
192-
return FileRepositoryBuilder.create(new File(dir, Constants.DOT_GIT));
218+
return FileRepositoryBuilder.create(getDotGitDir(dir, Constants.DOT_GIT));
193219
}
194220

195221
/**

0 commit comments

Comments
 (0)