From 2f0d69cedeb83a09d836c612985d9533027dd9b2 Mon Sep 17 00:00:00 2001 From: Tim Van Holder Date: Sat, 22 Feb 2020 14:10:40 +0100 Subject: [PATCH] GH-576: Fix relative submodule URL handling. They should be resolved against the main repository's URL, not the working directory. --- src/Microsoft.Build.Tasks.Git/GitOperations.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.Build.Tasks.Git/GitOperations.cs b/src/Microsoft.Build.Tasks.Git/GitOperations.cs index e21948a4..a86fb418 100644 --- a/src/Microsoft.Build.Tasks.Git/GitOperations.cs +++ b/src/Microsoft.Build.Tasks.Git/GitOperations.cs @@ -158,7 +158,7 @@ internal static string ApplyInsteadOfUrlMapping(GitConfig config, string url) internal static Uri? NormalizeUrl(GitRepository repository, string url) { // Git (v2.23.0) treats local relative URLs as relative to the working directory. - // This doesn't work when a relative URL is used in a config file locatede in a main .git directory + // This doesn't work when a relative URL is used in a config file located in a main .git directory // but is resolved from a worktree that has a different working directory. // Currently we implement the same behavior as git. @@ -244,11 +244,13 @@ public static ITaskItem[] GetSourceRoots(GitRepository repository, string? remot var result = new List(); var repoRoot = repository.WorkingDirectory.EndWithSeparator(); + string? repositoryUrl = null; + var revisionId = repository.GetHeadCommitSha(); if (revisionId != null) { // Don't report a warning since it has already been reported by GetRepositoryUrl task. - string? repositoryUrl = GetRepositoryUrl(repository, remoteName, logWarning: null); + repositoryUrl = GetRepositoryUrl(repository, remoteName, logWarning: null); // Item metadata are stored msbuild-escaped. GetMetadata unescapes, SetMetadata stores the value as specified. // Escape msbuild special characters so that URL escapes in the URL are preserved when the URL is read by GetMetadata. @@ -276,7 +278,9 @@ public static ITaskItem[] GetSourceRoots(GitRepository repository, string? remot } // https://git-scm.com/docs/git-submodule - var submoduleUri = NormalizeUrl(repository, submodule.Url); + // GH-576: The plain NormalizeUrl normalizes against the working directory, but that is not what + // Git does for the submodule URLs. Those are relative to the root's remote URL. + var submoduleUri = NormalizeUrl(ApplyInsteadOfUrlMapping(repository.Config, submodule.Url), repositoryUrl ?? repository.WorkingDirectory); if (submoduleUri == null) { logWarning(Resources.SourceCodeWontBeAvailableViaSourceLink,