Skip to content

Commit

Permalink
Fix reading remote from config (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat authored Jul 16, 2019
1 parent b138bff commit 855afb5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
17 changes: 13 additions & 4 deletions src/Common/GetSourceLinkUrlGitTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void ExecuteImpl()
return;
}

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

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

private IEnumerable<UrlMapping> GetUrlMappings(Uri gitUri)
{
bool isValidContentUri(Uri uri)
=> uri.Query == "" && uri.UserInfo == "";
static bool isValidContentUri(Uri uri)
=> uri.GetHost() != "" && uri.Query == "" && uri.UserInfo == "";

if (Hosts != null)
{
Expand Down Expand Up @@ -186,7 +186,16 @@ bool isValidContentUri(Uri uri)
{
if (Uri.TryCreate(RepositoryUrl, UriKind.Absolute, out var uri))
{
yield return new UrlMapping(uri.GetHost(), hostItem: null, uri.GetExplicitPort(), GetDefaultContentUriFromRepositoryUri(uri), hasDefaultContentUri: true);
// If the URL is a local path the host will be empty.
var host = uri.GetHost();
if (host != "")
{
yield return new UrlMapping(host, hostItem: null, uri.GetExplicitPort(), GetDefaultContentUriFromRepositoryUri(uri), hasDefaultContentUri: true);
}
else
{
Log.LogError(CommonResources.ValuePassedToTaskParameterNotValidHostUri, nameof(RepositoryUrl), RepositoryUrl);
}
}
else
{
Expand Down
10 changes: 6 additions & 4 deletions src/Microsoft.Build.Tasks.Git/GitOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ internal static class GitOperations
{
private const string SourceControlName = "git";
private const string RemoteSectionName = "remote";
private const string RemoteOriginName = "origin";
private const string UrlSectionName = "url";
private const string UrlVariableName = "url";

public static string GetRepositoryUrl(GitRepository repository, string remoteName, Action<string, object[]> logWarning = null)
{
string unknownRemoteName = null;
string remoteUrl = null;
if (!string.IsNullOrEmpty(remoteName))
{
remoteUrl = repository.Config.GetVariableValue(RemoteSectionName, remoteName, "url");
remoteUrl = repository.Config.GetVariableValue(RemoteSectionName, remoteName, UrlVariableName);
if (remoteUrl == null)
{
unknownRemoteName = remoteName;
Expand Down Expand Up @@ -52,15 +54,15 @@ public static string GetRepositoryUrl(GitRepository repository, string remoteNam

private static bool TryGetRemote(GitConfig config, out string remoteName, out string remoteUrl)
{
remoteName = "origin";
remoteUrl = config.GetVariableValue(RemoteSectionName, remoteName, "url");
remoteName = RemoteOriginName;
remoteUrl = config.GetVariableValue(RemoteSectionName, remoteName, UrlVariableName);
if (remoteUrl != null)
{
return true;
}

var remoteVariable = config.Variables.
Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName)).
Where(kvp => kvp.Key.SectionNameEquals(RemoteSectionName) && kvp.Key.VariableNameEquals(UrlVariableName)).
OrderBy(kvp => kvp.Key.SubsectionName, GitVariableName.SubsectionNameComparer).
FirstOrDefault();

Expand Down
3 changes: 1 addition & 2 deletions src/Microsoft.Build.Tasks.Git/RepositoryTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ private GitRepository GetOrCreateRepositoryInstance()

var initialPath = GetInitialPath();

GitRepositoryLocation location;
if (!GitRepository.TryFindRepository(initialPath, out location))
if (!GitRepository.TryFindRepository(initialPath, out var location))
{
Log.LogWarning(Resources.UnableToLocateRepository, initialPath);
return null;
Expand Down
23 changes: 23 additions & 0 deletions src/SourceLink.Common.UnitTests/GetSourceLinkUrlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class GetSourceLinkUrlTests
[InlineData("contoso.com/a?x=2")]
[InlineData("contoso.com/x")]
[InlineData("[email protected]")]
[InlineData("file:///D:/contoso")]
[InlineData("http://contoso.com")]
[InlineData("http://contoso.com/a")]
[InlineData("http://[email protected]")]
Expand Down Expand Up @@ -60,6 +61,28 @@ public void ImplicitHost_Errors(string repositoryUrl)
Assert.False(result);
}

[Theory]
[InlineData("file:///D:/a/b")]
public void ImplicitHost_Local(string repositoryUrl)
{
var engine = new MockEngine();

var task = new MockGetSourceLinkUrlGitTask()
{
BuildEngine = engine,
SourceRoot = new MockItem("x", KVP("RepositoryUrl", "http://abc.com"), KVP("SourceControl", "git")),
RepositoryUrl = repositoryUrl,
IsSingleProvider = true,
};

bool result = task.Execute();

AssertEx.AssertEqualToleratingWhitespaceDifferences(
"ERROR : " + string.Format(CommonResources.ValuePassedToTaskParameterNotValidHostUri, "RepositoryUrl", repositoryUrl), engine.Log);

Assert.False(result);
}

[Theory]
[InlineData("contoso.com")]
[InlineData("contoso.com/a")]
Expand Down

0 comments on commit 855afb5

Please sign in to comment.