Add support for Git submodules to GitInfo (#89741)

This commit is contained in:
Mark Vieira 2022-09-06 09:21:25 -07:00 committed by GitHub
parent 284dce6a2a
commit 12b841e048
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -75,18 +75,25 @@ public class GitInfo {
head = dotGit.resolve("HEAD"); head = dotGit.resolve("HEAD");
gitDir = dotGit; gitDir = dotGit;
} else { } else {
// this is a git worktree, follow the pointer to the repository // this is a git worktree or submodule, follow the pointer to the repository
final Path workTree = Paths.get(readFirstLine(dotGit).substring("gitdir:".length()).trim()); final Path reference = Paths.get(readFirstLine(dotGit).substring("gitdir:".length()).trim());
if (Files.exists(workTree) == false) { if (reference.getParent().endsWith("modules")) {
return new GitInfo("unknown", "unknown"); // this is a git submodule so follow the reference to the git repo
} gitDir = rootDir.toPath().resolve(reference);
head = workTree.resolve("HEAD"); head = gitDir.resolve("HEAD");
final Path commonDir = Paths.get(readFirstLine(workTree.resolve("commondir")));
if (commonDir.isAbsolute()) {
gitDir = commonDir;
} else { } else {
// this is the common case // this is a worktree so resolve the root repo directory
gitDir = workTree.resolve(commonDir); if (Files.exists(reference) == false) {
return new GitInfo("unknown", "unknown");
}
head = reference.resolve("HEAD");
final Path commonDir = Paths.get(readFirstLine(reference.resolve("commondir")));
if (commonDir.isAbsolute()) {
gitDir = commonDir;
} else {
// this is the common case
gitDir = reference.resolve(commonDir);
}
} }
} }
final String ref = readFirstLine(head); final String ref = readFirstLine(head);