Skip to content

Commit

Permalink
Merge pull request #77 from Dschaehn/WorktreeError
Browse files Browse the repository at this point in the history
Fix detection of git worktree directory
  • Loading branch information
sboulema authored Sep 17, 2022
2 parents 27eba75 + 004de1f commit d67adc7
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions TGit/Helpers/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,38 +76,19 @@ public static async Task<bool> HasSolutionDir()
/// Start at the solution dir and traverse up to find a .git folder or file
/// </summary>
/// <param name="path">Path to start traversing from.</param>
/// <returns>Path to the .git folder or file.</returns>
/// <returns>Path to the folder of the git repo or worktree.</returns>
private static async Task<string> FindGitdir(string path)
{
try
{
var di = new DirectoryInfo(path);
if (di.GetDirectories().Any(d => d.Name.Equals(".git")))
var gitPath = Path.Combine(path, ".git");
// A git repo has a .git directory, while a worktree only has a file
if (Directory.Exists(gitPath) || File.Exists(gitPath))
{
return di.FullName;
}

var gitFilePath = Path.Combine(path, ".git");
if (File.Exists(gitFilePath))
{
var text = File.ReadAllText(gitFilePath);
var match = Regex.Match(text, "gitdir:(.*)");
if (match.Success)
{
var gitDirPath = match.Groups[1].Value.Trim();

if (Directory.Exists(gitDirPath))
{
return gitDirPath;
}

if (File.Exists(gitDirPath))
{
return File.ReadAllText(gitDirPath);
}
}
return path;
}

var di = new DirectoryInfo(path);
if (di.Parent != null)
{
return await FindGitdir(di.Parent.FullName);
Expand Down

0 comments on commit d67adc7

Please sign in to comment.