Skip to content

Commit 6771692

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

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 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,36 @@ 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(dir.toPath()), StandardCharsets.UTF_8)
201+
.split(":")[1].trim();
202+
return getDotGitDir(dir, relativePath);
203+
} catch (IOException e) {
204+
return null;
205+
}
206+
} else {
207+
return null;
208+
}
209+
}
210+
186211
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);
212+
File dotGit = getDotGitDir(dir, Constants.DOT_GIT);
213+
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
189214
}
190215

191216
static Repository createRepo(File dir) throws IOException {
192-
return FileRepositoryBuilder.create(new File(dir, Constants.DOT_GIT));
217+
return FileRepositoryBuilder.create(getDotGitDir(dir, Constants.DOT_GIT));
193218
}
194219

195220
/**

0 commit comments

Comments
 (0)