Skip to content

Commit d67adc7

Browse files
authored
Merge pull request #77 from Dschaehn/WorktreeError
Fix detection of git worktree directory
2 parents 27eba75 + 004de1f commit d67adc7

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

TGit/Helpers/FileHelper.cs

+6-25
Original file line numberDiff line numberDiff line change
@@ -76,38 +76,19 @@ public static async Task<bool> HasSolutionDir()
7676
/// Start at the solution dir and traverse up to find a .git folder or file
7777
/// </summary>
7878
/// <param name="path">Path to start traversing from.</param>
79-
/// <returns>Path to the .git folder or file.</returns>
79+
/// <returns>Path to the folder of the git repo or worktree.</returns>
8080
private static async Task<string> FindGitdir(string path)
8181
{
8282
try
8383
{
84-
var di = new DirectoryInfo(path);
85-
if (di.GetDirectories().Any(d => d.Name.Equals(".git")))
84+
var gitPath = Path.Combine(path, ".git");
85+
// A git repo has a .git directory, while a worktree only has a file
86+
if (Directory.Exists(gitPath) || File.Exists(gitPath))
8687
{
87-
return di.FullName;
88-
}
89-
90-
var gitFilePath = Path.Combine(path, ".git");
91-
if (File.Exists(gitFilePath))
92-
{
93-
var text = File.ReadAllText(gitFilePath);
94-
var match = Regex.Match(text, "gitdir:(.*)");
95-
if (match.Success)
96-
{
97-
var gitDirPath = match.Groups[1].Value.Trim();
98-
99-
if (Directory.Exists(gitDirPath))
100-
{
101-
return gitDirPath;
102-
}
103-
104-
if (File.Exists(gitDirPath))
105-
{
106-
return File.ReadAllText(gitDirPath);
107-
}
108-
}
88+
return path;
10989
}
11090

91+
var di = new DirectoryInfo(path);
11192
if (di.Parent != null)
11293
{
11394
return await FindGitdir(di.Parent.FullName);

0 commit comments

Comments
 (0)