Skip to content

Commit e9f895f

Browse files
author
conderls
committed
feat: 'ratchetFrom' in git submodule supported (diffplug#746)
1 parent 11c2cb9 commit e9f895f

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1111

1212
## [Unreleased]
1313
### Changed
14+
* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746))
1415
* Update default ktfmt from 0.16 to 0.18 ([#748](https://github.com/diffplug/spotless/issues/748))
1516
* fix typo in javadoc comment for SQL\_FORMATTER\_INDENT\_TYPE ([#753](https://github.com/diffplug/spotless/pull/753))
1617

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
/**

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+
### Added
7+
* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746))
68

79
## [5.8.2] - 2020-11-16
810
### Fixed

plugin-maven/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 `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Added support for git-submodule with `ratchetFrom` ([#746](https://github.com/diffplug/spotless/issues/746))
68
### Fixed
79
* Fix broken test for spotlessFiles parameter on windows ([#737](https://github.com/diffplug/spotless/pull/737))
810

0 commit comments

Comments
 (0)