diff --git a/dotnet-sourcelink-git/Program.cs b/dotnet-sourcelink-git/Program.cs index b2bb86d..f8a1cc3 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,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); } @@ -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[] { }; 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<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("*")) {