Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Add support for detecting and writing source link files for submodules #245

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions dotnet-sourcelink-git/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ public static void Create(CommandLineApplication command)
}
else
{
var index = repo.Index[sf.GitPath];
if (index == null)
{
var index = repo.Index[sf.GitPath] ?? FindInSubmodule(repo, sf);
if (index == null) {
filesNotInGit.Add(sf);
}
else if (index.Path != sf.GitPath)
Expand Down Expand Up @@ -244,16 +243,27 @@ public static void Create(CommandLineApplication command)
}
}

var documents = new Dictionary<string, string> {
{string.Format("{0}{1}{2}", repoPath, Path.DirectorySeparatorChar, '*'), url},
};

using (var repo = new Repository(repoPath)) {
foreach (var submodule in repo.Submodules) {
var path =
$"{repoPath}{Path.DirectorySeparatorChar}{submodule.Path.Replace('/', Path.DirectorySeparatorChar)}{Path.DirectorySeparatorChar}*";
var rawUrl = submodule.Url.Replace("ssh://git@", "https://")
.Replace("github.com", "raw.githubusercontent.com");
var fullUrl = $"{rawUrl}/{submodule.IndexCommitId}/*";
documents.Add(path, fullUrl);
}
}

var json = new SourceLinkJson
{
documents = new Dictionary<string, string>
{
{ string.Format("{0}{1}{2}", repoPath, Path.DirectorySeparatorChar, '*'), url },
}
documents = documents
};

using (var sw = new StreamWriter(File.OpenWrite(file)))
{
using (var sw = new StreamWriter(File.Open(file, FileMode.Create, FileAccess.Write, FileShare.Read))) {
var js = new JsonSerializer();
js.Serialize(sw, json);
}
Expand All @@ -280,6 +290,22 @@ public static void Create(CommandLineApplication command)
});
}

public static IndexEntry FindInSubmodule(Repository repo, SourceFile file)
{
foreach (var submodule in repo.Submodules) {
var normalizedSubmodulePath = submodule.Path.Replace('/', Path.DirectorySeparatorChar);
var submoduleFilePath = file.GitPath.Replace(normalizedSubmodulePath + Path.DirectorySeparatorChar, "");
using (var r = new Repository(Path.Combine(repo.Info.WorkingDirectory, normalizedSubmodulePath))) {
if (r.Index[submoduleFilePath] != null) {
file.GitPath = GetGitPath(r.Info.WorkingDirectory.TrimEnd(Path.DirectorySeparatorChar), file.FilePath);
return r.Index[submoduleFilePath];
}
}
}

return null;
}

public static bool TryFixLineEndings(SHA1 sha1, SourceFile sf)
{
var fileBytes = new byte[] { };
Expand Down
2 changes: 1 addition & 1 deletion dotnet-sourcelink/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public static IEnumerable<Document> GetDocumentsWithUrls(DebugReaderProvider drp
public static string GetUrl(string file, SourceLinkJson json)
{
if (json == null) return null;
foreach (var key in json.documents.Keys)
foreach (var key in json.documents.Keys.OrderByDescending(x => x.Split(Path.DirectorySeparatorChar).Length).ThenBy(x => x))
{
if (key.Contains("*"))
{
Expand Down