Skip to content

Commit

Permalink
Support new submodule format using .git file (#9540)
Browse files Browse the repository at this point in the history
  • Loading branch information
georghinkel authored Dec 18, 2023
1 parent 9120453 commit 4252c0f
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Docfx.Common/Git/GitUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,27 @@ static string GitUrlToHttps(string url)
}
}

private const string GitDir = "gitdir: ";

private static Repo? GetRepoInfo(string? directory)
{
if (string.IsNullOrEmpty(directory))
return null;

return s_cache.GetOrAdd(directory, _ =>
{
if (IsGitRoot(directory))
var gitRoot = GetRepoDatabase(directory);
if (gitRoot != null)
{
return GetRepoInfoCore(directory);
return GetRepoInfoCore(directory, gitRoot);
}

return GetRepoInfo(Path.GetDirectoryName(directory));
});

static Repo? GetRepoInfoCore(string directory)
static Repo? GetRepoInfoCore(string directory, string gitRoot)
{
var remoteUrls = ParseRemoteUrls(directory).ToArray();
var remoteUrls = ParseRemoteUrls(directory, gitRoot).ToArray();
var url = remoteUrls.FirstOrDefault(r => r.key == "origin").value ?? remoteUrls.FirstOrDefault().value;
if (string.IsNullOrEmpty(url))
return null;
Expand All @@ -129,15 +132,26 @@ static string GitUrlToHttps(string url)
}
}

static bool IsGitRoot(string directory)
static string? GetRepoDatabase(string directory)
{
var gitPath = Path.Combine(directory, ".git");
return Directory.Exists(gitPath);
if (Directory.Exists(gitPath))
return gitPath;

if (File.Exists(gitPath))
{
var firstLine = File.ReadLines(gitPath).FirstOrDefault();
if (firstLine != null && firstLine.StartsWith(GitDir))
return Path.Combine(directory, firstLine.Substring(GitDir.Length));
}

return null;

}

static IEnumerable<(string key, string value)> ParseRemoteUrls(string directory)
static IEnumerable<(string key, string value)> ParseRemoteUrls(string directory, string gitRoot)
{
var configPath = Path.Combine(directory, ".git", "config");
var configPath = Path.Combine(gitRoot, "config");
if (!File.Exists(configPath))
yield break;

Expand Down

0 comments on commit 4252c0f

Please sign in to comment.