From 7c384c2449dca1e423971e6dec8b1f5a4e810eb9 Mon Sep 17 00:00:00 2001 From: Jim Borden Date: Sat, 29 Jul 2017 12:43:53 +0900 Subject: [PATCH 1/2] Add support for detecting and writing source link files for submodules --- dotnet-sourcelink-git/Program.cs | 41 ++++++++++++++++++++++++++------ dotnet-sourcelink/Program.cs | 2 +- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/dotnet-sourcelink-git/Program.cs b/dotnet-sourcelink-git/Program.cs index b2bb86d..a147c43 100644 --- a/dotnet-sourcelink-git/Program.cs +++ b/dotnet-sourcelink-git/Program.cs @@ -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) @@ -244,12 +243,24 @@ public static void Create(CommandLineApplication command) } } + var documents = new Dictionary { + {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.Format("{0}{1}{2}", repoPath, Path.DirectorySeparatorChar, '*'), url }, - } + documents = documents }; using (var sw = new StreamWriter(File.OpenWrite(file))) @@ -280,6 +291,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[] { }; diff --git a/dotnet-sourcelink/Program.cs b/dotnet-sourcelink/Program.cs index 65e172f..64c74a2 100644 --- a/dotnet-sourcelink/Program.cs +++ b/dotnet-sourcelink/Program.cs @@ -407,7 +407,7 @@ public static IEnumerable 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("*")) { From 95a8bf74108e36ac37f1ff503c0670e23dcc1244 Mon Sep 17 00:00:00 2001 From: Jim Borden Date: Sat, 29 Jul 2017 19:12:35 +0900 Subject: [PATCH 2/2] Prevent multiple writes to the file from apendding and causing chaos --- dotnet-sourcelink-git/Program.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/dotnet-sourcelink-git/Program.cs b/dotnet-sourcelink-git/Program.cs index a147c43..f8a1cc3 100644 --- a/dotnet-sourcelink-git/Program.cs +++ b/dotnet-sourcelink-git/Program.cs @@ -263,8 +263,7 @@ public static void Create(CommandLineApplication command) 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); }