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 discovery of git directories within submodules #582

Merged
merged 1 commit into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/NerdBank.GitVersioning/GitContext.cs
Expand Up @@ -214,8 +214,16 @@ protected static bool IsVersionFileChangedInWorkingTree(VersionOptions? committe
return committedVersion is object;
}

private protected static bool TryFindGitPaths(string path, [NotNullWhen(true)] out string? gitDirectory, [NotNullWhen(true)] out string? workingTreeDirectory, [NotNullWhen(true)] out string? workingTreeRelativePath)
internal static bool TryFindGitPaths(string? path, [NotNullWhen(true)] out string? gitDirectory, [NotNullWhen(true)] out string? workingTreeDirectory, [NotNullWhen(true)] out string? workingTreeRelativePath)
{
if (path is null || path.Length == 0)
{
gitDirectory = null;
workingTreeDirectory = null;
workingTreeRelativePath = null;
return false;
}

path = Path.GetFullPath(path);
var gitDirs = FindGitDir(path);
if (gitDirs is null)
Expand Down
37 changes: 4 additions & 33 deletions src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs
Expand Up @@ -40,50 +40,21 @@ public class GitRepository : IDisposable
/// </returns>
public static GitRepository? Create(string? workingDirectory)
{
// Search for the top-level directory of the current git repository. This is the directory
// which contains a directory of file named .git.
// Loop until Path.GetDirectoryName returns null; in this case, we've reached the root of
// the file system (and we're not in a git repository).
while (!string.IsNullOrEmpty(workingDirectory)
&& !File.Exists(Path.Combine(workingDirectory, GitDirectoryName))
&& !Directory.Exists(Path.Combine(workingDirectory, GitDirectoryName)))
{
workingDirectory = Path.GetDirectoryName(workingDirectory);
}

if (string.IsNullOrEmpty(workingDirectory))
{
return null;
}

var gitDirectory = Path.Combine(workingDirectory, GitDirectoryName);

if (File.Exists(gitDirectory))
{
// This is a worktree, and the path to the git directory is stored in the .git file
var worktreeConfig = File.ReadAllText(gitDirectory);

var gitDirStart = worktreeConfig.IndexOf("gitdir: ");
var gitDirEnd = worktreeConfig.IndexOf("\n", gitDirStart);

gitDirectory = worktreeConfig.Substring(gitDirStart + 8, gitDirEnd - gitDirStart - 8);
}

if (!Directory.Exists(gitDirectory))
if (!GitContext.TryFindGitPaths(workingDirectory, out string? gitDirectory, out string? workingTreeDirectory, out string? workingTreeRelativePath))
{
return null;
}

var commonDirectory = gitDirectory;
var commonDirFile = Path.Combine(gitDirectory, "commondir");
string commonDirectory = gitDirectory;
string commonDirFile = Path.Combine(gitDirectory, "commondir");

if (File.Exists(commonDirFile))
{
var commonDirectoryRelativePath = File.ReadAllText(commonDirFile).Trim('\n');
commonDirectory = Path.Combine(gitDirectory, commonDirectoryRelativePath);
}

var objectDirectory = Path.Combine(commonDirectory, "objects");
string objectDirectory = Path.Combine(commonDirectory, "objects");

return new GitRepository(workingDirectory!, gitDirectory, commonDirectory, objectDirectory);
}
Expand Down