Skip to content

Commit 8126d95

Browse files
authored
Merge pull request #582 from dotnet/fix578
Fix discovery of git directories within submodules
2 parents 536c1fd + b321efd commit 8126d95

File tree

2 files changed

+13
-34
lines changed

2 files changed

+13
-34
lines changed

src/NerdBank.GitVersioning/GitContext.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,16 @@ protected static bool IsVersionFileChangedInWorkingTree(VersionOptions? committe
214214
return committedVersion is object;
215215
}
216216

217-
private protected static bool TryFindGitPaths(string path, [NotNullWhen(true)] out string? gitDirectory, [NotNullWhen(true)] out string? workingTreeDirectory, [NotNullWhen(true)] out string? workingTreeRelativePath)
217+
internal static bool TryFindGitPaths(string? path, [NotNullWhen(true)] out string? gitDirectory, [NotNullWhen(true)] out string? workingTreeDirectory, [NotNullWhen(true)] out string? workingTreeRelativePath)
218218
{
219+
if (path is null || path.Length == 0)
220+
{
221+
gitDirectory = null;
222+
workingTreeDirectory = null;
223+
workingTreeRelativePath = null;
224+
return false;
225+
}
226+
219227
path = Path.GetFullPath(path);
220228
var gitDirs = FindGitDir(path);
221229
if (gitDirs is null)

src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,50 +40,21 @@ public class GitRepository : IDisposable
4040
/// </returns>
4141
public static GitRepository? Create(string? workingDirectory)
4242
{
43-
// Search for the top-level directory of the current git repository. This is the directory
44-
// which contains a directory of file named .git.
45-
// Loop until Path.GetDirectoryName returns null; in this case, we've reached the root of
46-
// the file system (and we're not in a git repository).
47-
while (!string.IsNullOrEmpty(workingDirectory)
48-
&& !File.Exists(Path.Combine(workingDirectory, GitDirectoryName))
49-
&& !Directory.Exists(Path.Combine(workingDirectory, GitDirectoryName)))
50-
{
51-
workingDirectory = Path.GetDirectoryName(workingDirectory);
52-
}
53-
54-
if (string.IsNullOrEmpty(workingDirectory))
55-
{
56-
return null;
57-
}
58-
59-
var gitDirectory = Path.Combine(workingDirectory, GitDirectoryName);
60-
61-
if (File.Exists(gitDirectory))
62-
{
63-
// This is a worktree, and the path to the git directory is stored in the .git file
64-
var worktreeConfig = File.ReadAllText(gitDirectory);
65-
66-
var gitDirStart = worktreeConfig.IndexOf("gitdir: ");
67-
var gitDirEnd = worktreeConfig.IndexOf("\n", gitDirStart);
68-
69-
gitDirectory = worktreeConfig.Substring(gitDirStart + 8, gitDirEnd - gitDirStart - 8);
70-
}
71-
72-
if (!Directory.Exists(gitDirectory))
43+
if (!GitContext.TryFindGitPaths(workingDirectory, out string? gitDirectory, out string? workingTreeDirectory, out string? workingTreeRelativePath))
7344
{
7445
return null;
7546
}
7647

77-
var commonDirectory = gitDirectory;
78-
var commonDirFile = Path.Combine(gitDirectory, "commondir");
48+
string commonDirectory = gitDirectory;
49+
string commonDirFile = Path.Combine(gitDirectory, "commondir");
7950

8051
if (File.Exists(commonDirFile))
8152
{
8253
var commonDirectoryRelativePath = File.ReadAllText(commonDirFile).Trim('\n');
8354
commonDirectory = Path.Combine(gitDirectory, commonDirectoryRelativePath);
8455
}
8556

86-
var objectDirectory = Path.Combine(commonDirectory, "objects");
57+
string objectDirectory = Path.Combine(commonDirectory, "objects");
8758

8859
return new GitRepository(workingDirectory!, gitDirectory, commonDirectory, objectDirectory);
8960
}

0 commit comments

Comments
 (0)