|
17 | 17 |
|
18 | 18 | import java.io.File;
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
20 | 22 | import java.util.HashMap;
|
21 | 23 | import java.util.Map;
|
22 | 24 | import java.util.Objects;
|
@@ -183,13 +185,37 @@ protected Repository repositoryFor(Project project) throws IOException {
|
183 | 185 | return null;
|
184 | 186 | }
|
185 | 187 |
|
| 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 | + |
186 | 212 | 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); |
189 | 215 | }
|
190 | 216 |
|
191 | 217 | 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)); |
193 | 219 | }
|
194 | 220 |
|
195 | 221 | /**
|
|
0 commit comments