Skip to content

Commit 855afb5

Browse files
authored
Fix reading remote from config (#345)
1 parent b138bff commit 855afb5

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

src/Common/GetSourceLinkUrlGitTask.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private void ExecuteImpl()
103103
return;
104104
}
105105

106-
bool IsHexDigit(char c)
106+
static bool IsHexDigit(char c)
107107
=> c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';
108108

109109
string revisionId = SourceRoot.GetMetadata(Names.SourceRoot.RevisionId);
@@ -149,8 +149,8 @@ public UrlMapping(string host, ITaskItem hostItem, int port, Uri contentUri, boo
149149

150150
private IEnumerable<UrlMapping> GetUrlMappings(Uri gitUri)
151151
{
152-
bool isValidContentUri(Uri uri)
153-
=> uri.Query == "" && uri.UserInfo == "";
152+
static bool isValidContentUri(Uri uri)
153+
=> uri.GetHost() != "" && uri.Query == "" && uri.UserInfo == "";
154154

155155
if (Hosts != null)
156156
{
@@ -186,7 +186,16 @@ bool isValidContentUri(Uri uri)
186186
{
187187
if (Uri.TryCreate(RepositoryUrl, UriKind.Absolute, out var uri))
188188
{
189-
yield return new UrlMapping(uri.GetHost(), hostItem: null, uri.GetExplicitPort(), GetDefaultContentUriFromRepositoryUri(uri), hasDefaultContentUri: true);
189+
// If the URL is a local path the host will be empty.
190+
var host = uri.GetHost();
191+
if (host != "")
192+
{
193+
yield return new UrlMapping(host, hostItem: null, uri.GetExplicitPort(), GetDefaultContentUriFromRepositoryUri(uri), hasDefaultContentUri: true);
194+
}
195+
else
196+
{
197+
Log.LogError(CommonResources.ValuePassedToTaskParameterNotValidHostUri, nameof(RepositoryUrl), RepositoryUrl);
198+
}
190199
}
191200
else
192201
{

src/Microsoft.Build.Tasks.Git/GitOperations.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ internal static class GitOperations
1515
{
1616
private const string SourceControlName = "git";
1717
private const string RemoteSectionName = "remote";
18+
private const string RemoteOriginName = "origin";
1819
private const string UrlSectionName = "url";
20+
private const string UrlVariableName = "url";
1921

2022
public static string GetRepositoryUrl(GitRepository repository, string remoteName, Action<string, object[]> logWarning = null)
2123
{
2224
string unknownRemoteName = null;
2325
string remoteUrl = null;
2426
if (!string.IsNullOrEmpty(remoteName))
2527
{
26-
remoteUrl = repository.Config.GetVariableValue(RemoteSectionName, remoteName, "url");
28+
remoteUrl = repository.Config.GetVariableValue(RemoteSectionName, remoteName, UrlVariableName);
2729
if (remoteUrl == null)
2830
{
2931
unknownRemoteName = remoteName;
@@ -52,15 +54,15 @@ public static string GetRepositoryUrl(GitRepository repository, string remoteNam
5254

5355
private static bool TryGetRemote(GitConfig config, out string remoteName, out string remoteUrl)
5456
{
55-
remoteName = "origin";
56-
remoteUrl = config.GetVariableValue(RemoteSectionName, remoteName, "url");
57+
remoteName = RemoteOriginName;
58+
remoteUrl = config.GetVariableValue(RemoteSectionName, remoteName, UrlVariableName);
5759
if (remoteUrl != null)
5860
{
5961
return true;
6062
}
6163

6264
var remoteVariable = config.Variables.
63-
Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName)).
65+
Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName) && kvp.Key.VariableNameEquals(UrlVariableName)).
6466
OrderBy(kvp => kvp.Key.SubsectionName, GitVariableName.SubsectionNameComparer).
6567
FirstOrDefault();
6668

src/Microsoft.Build.Tasks.Git/RepositoryTask.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ private GitRepository GetOrCreateRepositoryInstance()
9494

9595
var initialPath = GetInitialPath();
9696

97-
GitRepositoryLocation location;
98-
if (!GitRepository.TryFindRepository(initialPath, out location))
97+
if (!GitRepository.TryFindRepository(initialPath, out var location))
9998
{
10099
Log.LogWarning(Resources.UnableToLocateRepository, initialPath);
101100
return null;

src/SourceLink.Common.UnitTests/GetSourceLinkUrlTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class GetSourceLinkUrlTests
1414
[InlineData("contoso.com/a?x=2")]
1515
[InlineData("contoso.com/x")]
1616
[InlineData("[email protected]")]
17+
[InlineData("file:///D:/contoso")]
1718
[InlineData("http://contoso.com")]
1819
[InlineData("http://contoso.com/a")]
1920
[InlineData("http://[email protected]")]
@@ -60,6 +61,28 @@ public void ImplicitHost_Errors(string repositoryUrl)
6061
Assert.False(result);
6162
}
6263

64+
[Theory]
65+
[InlineData("file:///D:/a/b")]
66+
public void ImplicitHost_Local(string repositoryUrl)
67+
{
68+
var engine = new MockEngine();
69+
70+
var task = new MockGetSourceLinkUrlGitTask()
71+
{
72+
BuildEngine = engine,
73+
SourceRoot = new MockItem("x", KVP("RepositoryUrl", "http://abc.com"), KVP("SourceControl", "git")),
74+
RepositoryUrl = repositoryUrl,
75+
IsSingleProvider = true,
76+
};
77+
78+
bool result = task.Execute();
79+
80+
AssertEx.AssertEqualToleratingWhitespaceDifferences(
81+
"ERROR : " + string.Format(CommonResources.ValuePassedToTaskParameterNotValidHostUri, "RepositoryUrl", repositoryUrl), engine.Log);
82+
83+
Assert.False(result);
84+
}
85+
6386
[Theory]
6487
[InlineData("contoso.com")]
6588
[InlineData("contoso.com/a")]

0 commit comments

Comments
 (0)