Skip to content

Commit 4002aff

Browse files
authored
fix ratchtFrom when used with git submodule (#754)
2 parents 4237e2e + 21cdb68 commit 4002aff

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

CHANGES.md

+1
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

+29-3
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

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1010
### Changed
1111
* Updated default eclipse-jdt from 4.17.0 to 4.18.0.
1212
* Updated default eclipse-wtp from 4.17.0 to 4.18.0.
13+
### Fixed
14+
* `ratchetFrom` now works with git-submodule ([#746](https://github.com/diffplug/spotless/issues/746))
1315

1416
## [5.8.2] - 2020-11-16
1517
### Fixed

plugin-maven/CHANGES.md

+1
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
* Updated default eclipse-jdt from 4.17.0 to 4.18.0.
1212
* Updated default eclipse-wtp from 4.17.0 to 4.18.0.
1313
### Fixed
14+
* `ratchetFrom` now works with git-submodule ([#746](https://github.com/diffplug/spotless/issues/746))
1415
* Fix broken test for spotlessFiles parameter on windows ([#737](https://github.com/diffplug/spotless/pull/737))
1516

1617
## [2.6.1] - 2020-11-16

0 commit comments

Comments
 (0)