From a048fc28c6995b496c0036fa2b69a48260fa8072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Matou=C5=A1ek?= Date: Wed, 26 Jun 2019 18:31:51 -0700 Subject: [PATCH] Implement url..InsteadOf (#299) --- .../GitOperationsTests.cs | 49 +++++++++++++++++-- .../GitOperations.cs | 37 +++++++++++++- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs b/src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs index fe57377d..1ab8f7e7 100644 --- a/src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs +++ b/src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs @@ -69,6 +69,10 @@ private static GitConfig CreateConfig(params (string Name, string Value)[] varia => new GitConfig(ImmutableDictionary.CreateRange( variables.Select(v => new KeyValuePair>(CreateVariableName(v.Name), ImmutableArray.Create(v.Value))))); + private static GitConfig CreateConfig(params (string Name, string[] Values)[] variables) + => new GitConfig(ImmutableDictionary.CreateRange( + variables.Select(v => new KeyValuePair>(CreateVariableName(v.Name), ImmutableArray.CreateRange(v.Values))))); + [Fact] public void GetRepositoryUrl_NoRemotes() { @@ -173,6 +177,20 @@ public void GetRepositoryUrl_BadUrl() }, warnings.Select(TestUtilities.InspectDiagnostic)); } + [Fact] + public void GetRepositoryUrl_InsteadOf() + { + var repo = CreateRepository(config: new GitConfig(ImmutableDictionary.CreateRange(new[] + { + new KeyValuePair>(new GitVariableName("remote", "origin", "url"), ImmutableArray.Create("http://?")), + new KeyValuePair>(new GitVariableName("url", "git@github.com:org/repo", "insteadOf"), ImmutableArray.Create("http://?")) + }))); + + var warnings = new List<(string, object[])>(); + Assert.Equal("ssh://git@github.com/org/repo", GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args)))); + Assert.Empty(warnings); + } + [Theory] [InlineData("https://github.com/org/repo")] [InlineData("http://github.com/org/repo")] @@ -255,6 +273,30 @@ public void GetRepositoryUrl_ScpSyntax(string url, string expectedUrl) Assert.Equal(expectedUrl, GitOperations.NormalizeUrl(url, s_root)); } + [Theory] + [InlineData("http://test.com/test-repo", "http", "ssh", "ssh://test.com/test-repo")] + [InlineData("http://test.com/test-repo", "", "pre-", "pre-http://test.com/test-repo")] + [InlineData("http://test.com/test-repo", "http", "", "://test.com/test-repo")] + [InlineData("http://test.com/test-repo", "Http://", "xxx", "http://test.com/test-repo")] + public void ApplyInsteadOfUrlMapping_Single(string url, string prefix, string replacement, string mappedUrl) + { + var config = CreateConfig(($"url.{replacement}.insteadOf", prefix)); + var actualMappedUrl = GitOperations.ApplyInsteadOfUrlMapping(config, url); + Assert.Equal(mappedUrl, actualMappedUrl); + } + + [Fact] + public void ApplyInsteadOfUrlMapping_Multiple() + { + var config = CreateConfig( + ("url.A.insteadOf", new[] { "http://github", "http:" }), + ("url.B.insteadOf", new[] { "http://" }), + ("url.C.insteadOf", new[] { "http:/" })); + + var actualMappedUrl = GitOperations.ApplyInsteadOfUrlMapping(config, "http://github.com"); + Assert.Equal("A.com", actualMappedUrl); + } + [Fact] public void GetSourceRoots_RepoWithoutCommits() { @@ -272,17 +314,18 @@ public void GetSourceRoots_RepoWithoutCommitsWithSubmodules() { var repo = CreateRepository( commitSha: null, + config: CreateConfig(("url.ssh://.insteadOf", "http://")), submodules: ImmutableArray.Create( CreateSubmodule("1", "sub/1", "http://1.com", "1111111111111111111111111111111111111111"), - CreateSubmodule("1", "sub/2", "http://2.com", "2222222222222222222222222222222222222222"))); + CreateSubmodule("1", "sub/2", "http://2.com", "2222222222222222222222222222222222222222"))); ; var warnings = new List<(string, object[])>(); var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args))); AssertEx.Equal(new[] { - $@"'{_workingDir}{s}sub{s}1{s}' SourceControl='git' RevisionId='1111111111111111111111111111111111111111' NestedRoot='sub/1/' ContainingRoot='{_workingDir}{s}' ScmRepositoryUrl='http://1.com/'", - $@"'{_workingDir}{s}sub{s}2{s}' SourceControl='git' RevisionId='2222222222222222222222222222222222222222' NestedRoot='sub/2/' ContainingRoot='{_workingDir}{s}' ScmRepositoryUrl='http://2.com/'", + $@"'{_workingDir}{s}sub{s}1{s}' SourceControl='git' RevisionId='1111111111111111111111111111111111111111' NestedRoot='sub/1/' ContainingRoot='{_workingDir}{s}' ScmRepositoryUrl='ssh://1.com/'", + $@"'{_workingDir}{s}sub{s}2{s}' SourceControl='git' RevisionId='2222222222222222222222222222222222222222' NestedRoot='sub/2/' ContainingRoot='{_workingDir}{s}' ScmRepositoryUrl='ssh://2.com/'", }, items.Select(TestUtilities.InspectSourceRoot)); AssertEx.Equal(new[] { Resources.RepositoryHasNoCommit }, warnings.Select(TestUtilities.InspectDiagnostic)); diff --git a/src/Microsoft.Build.Tasks.Git/GitOperations.cs b/src/Microsoft.Build.Tasks.Git/GitOperations.cs index 78cca4cf..600d75bb 100644 --- a/src/Microsoft.Build.Tasks.Git/GitOperations.cs +++ b/src/Microsoft.Build.Tasks.Git/GitOperations.cs @@ -15,6 +15,7 @@ internal static class GitOperations { private const string SourceControlName = "git"; private const string RemoteSectionName = "remote"; + private const string UrlSectionName = "url"; public static string GetRepositoryUrl(GitRepository repository, Action logWarning = null, string remoteName = null) { @@ -40,7 +41,7 @@ public static string GetRepositoryUrl(GitRepository repository, Action longestPrefixLength && url.StartsWith(prefix, StringComparison.Ordinal)) + { + longestPrefixLength = prefix.Length; + replacement = variable.Key.SubsectionName; + } + } + } + } + + return (longestPrefixLength >= 0) ? replacement + url.Substring(longestPrefixLength) : url; + } + + internal static string NormalizeUrl(GitConfig config, string url, string root) + => NormalizeUrl(ApplyInsteadOfUrlMapping(config, url), root); + internal static string NormalizeUrl(string url, string root) { // Since git supports scp-like syntax for SSH URLs we convert it here, @@ -181,7 +214,7 @@ public static ITaskItem[] GetSourceRoots(GitRepository repository, Action