Skip to content

Commit

Permalink
Merge pull request #582 from dotnet/fix578
Browse files Browse the repository at this point in the history
Fix discovery of git directories within submodules
  • Loading branch information
AArnott committed Apr 7, 2021
2 parents 536c1fd + b321efd commit 8126d95
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 34 deletions.
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

0 comments on commit 8126d95

Please sign in to comment.