Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detection of git worktree directory #77

Merged
merged 1 commit into from
Sep 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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