Skip to content

Commit

Permalink
Attempted workaround for git worktree issue
Browse files Browse the repository at this point in the history
If the directory found as the git dir contains a file called `gitdir`,
attempts to resolve it to find the actual git directory.

For issue diffplug#75 or my issue diffplug#964
  • Loading branch information
hakanai committed Oct 11, 2021
1 parent d21a7bc commit 78efa46
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,37 @@ protected Repository repositoryFor(Project project) throws IOException {
File dotGitPath = new File(dir, dotGit);

if (dotGitPath.isDirectory()) {
// Handle case where directory is a worktree
File mainRepoRefFile = new File(dotGitPath, "gitdir");
if (mainRepoRefFile.isFile()) {
return tryReadGitMeta(mainRepoRefFile)
.map(path -> getDotGitDir(dir, path))
.orElse(null);
}

return dotGitPath;
} else if (dotGitPath.isFile()) {
try {
String relativePath = new String(Files.readAllBytes(dotGitPath.toPath()), StandardCharsets.UTF_8)
.split(":")[1].trim();
return getDotGitDir(dir, relativePath);
} catch (IOException e) {
System.err.println("failed to parse git meta: " + e.getMessage());
return null;
}
return tryReadGitMeta(dotGitPath)
.map(str -> {
String relativePath = str.split(":")[1].trim();
return getDotGitDir(dir, relativePath);
})
.orElse(null);
} else {
return null;
}
}

private static Optional<String> tryReadGitMeta(File file) {
try {
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim();
return Optional.of(content);
} catch (IOException e) {
System.err.println("failed to parse git meta: " + e.getMessage());
return Optional.empty();
}
}

private static boolean isGitRoot(File dir) {
File dotGit = getDotGitDir(dir, Constants.DOT_GIT);
return dotGit != null && RepositoryCache.FileKey.isGitRepository(dotGit, FS.DETECTED);
Expand Down

0 comments on commit 78efa46

Please sign in to comment.