Skip to content

Commit 4252c0f

Browse files
authored
Support new submodule format using .git file (#9540)
1 parent 9120453 commit 4252c0f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/Docfx.Common/Git/GitUtility.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,27 @@ static string GitUrlToHttps(string url)
8787
}
8888
}
8989

90+
private const string GitDir = "gitdir: ";
91+
9092
private static Repo? GetRepoInfo(string? directory)
9193
{
9294
if (string.IsNullOrEmpty(directory))
9395
return null;
9496

9597
return s_cache.GetOrAdd(directory, _ =>
9698
{
97-
if (IsGitRoot(directory))
99+
var gitRoot = GetRepoDatabase(directory);
100+
if (gitRoot != null)
98101
{
99-
return GetRepoInfoCore(directory);
102+
return GetRepoInfoCore(directory, gitRoot);
100103
}
101104

102105
return GetRepoInfo(Path.GetDirectoryName(directory));
103106
});
104107

105-
static Repo? GetRepoInfoCore(string directory)
108+
static Repo? GetRepoInfoCore(string directory, string gitRoot)
106109
{
107-
var remoteUrls = ParseRemoteUrls(directory).ToArray();
110+
var remoteUrls = ParseRemoteUrls(directory, gitRoot).ToArray();
108111
var url = remoteUrls.FirstOrDefault(r => r.key == "origin").value ?? remoteUrls.FirstOrDefault().value;
109112
if (string.IsNullOrEmpty(url))
110113
return null;
@@ -129,15 +132,26 @@ static string GitUrlToHttps(string url)
129132
}
130133
}
131134

132-
static bool IsGitRoot(string directory)
135+
static string? GetRepoDatabase(string directory)
133136
{
134137
var gitPath = Path.Combine(directory, ".git");
135-
return Directory.Exists(gitPath);
138+
if (Directory.Exists(gitPath))
139+
return gitPath;
140+
141+
if (File.Exists(gitPath))
142+
{
143+
var firstLine = File.ReadLines(gitPath).FirstOrDefault();
144+
if (firstLine != null && firstLine.StartsWith(GitDir))
145+
return Path.Combine(directory, firstLine.Substring(GitDir.Length));
146+
}
147+
148+
return null;
149+
136150
}
137151

138-
static IEnumerable<(string key, string value)> ParseRemoteUrls(string directory)
152+
static IEnumerable<(string key, string value)> ParseRemoteUrls(string directory, string gitRoot)
139153
{
140-
var configPath = Path.Combine(directory, ".git", "config");
154+
var configPath = Path.Combine(gitRoot, "config");
141155
if (!File.Exists(configPath))
142156
yield break;
143157

0 commit comments

Comments
 (0)