From 4252c0f5688467dd0134adc32f9730ced322e6ec Mon Sep 17 00:00:00 2001 From: Georg Hinkel Date: Mon, 18 Dec 2023 09:56:28 +0100 Subject: [PATCH] Support new submodule format using .git file (#9540) --- src/Docfx.Common/Git/GitUtility.cs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Docfx.Common/Git/GitUtility.cs b/src/Docfx.Common/Git/GitUtility.cs index 32986a401cf..ca3ed56ddb3 100644 --- a/src/Docfx.Common/Git/GitUtility.cs +++ b/src/Docfx.Common/Git/GitUtility.cs @@ -87,6 +87,8 @@ static string GitUrlToHttps(string url) } } + private const string GitDir = "gitdir: "; + private static Repo? GetRepoInfo(string? directory) { if (string.IsNullOrEmpty(directory)) @@ -94,17 +96,18 @@ static string GitUrlToHttps(string url) 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; @@ -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;