diff --git a/GitVersionCore/BuildServers/GitHelper.cs b/GitVersionCore/BuildServers/GitHelper.cs index 5071804a63..35397f7de6 100644 --- a/GitVersionCore/BuildServers/GitHelper.cs +++ b/GitVersionCore/BuildServers/GitHelper.cs @@ -1,5 +1,6 @@ namespace GitVersion { + using System; using LibGit2Sharp; using System.Collections.Generic; using System.Linq; @@ -164,6 +165,20 @@ static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Authent repo.Checkout(fakeBranchName); } + internal static IEnumerable GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, string repoUrl, string username, string password) + { + // This is a work-around as long as https://github.com/libgit2/libgit2sharp/issues/1099 is not fixed + var remote = repo.Network.Remotes.Add(Guid.NewGuid().ToString(), repoUrl); + try + { + return GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, username, password); + } + finally + { + repo.Network.Remotes.Remove(remote.Name); + } + } + static IEnumerable GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, Remote remote, string username, string password) { return repo.Network.ListReferences(remote, (url, fromUrl, types) => new UsernamePasswordCredentials diff --git a/GitVersionCore/GitPreparer.cs b/GitVersionCore/GitPreparer.cs index f3bdd399da..96ffb2309d 100644 --- a/GitVersionCore/GitPreparer.cs +++ b/GitVersionCore/GitPreparer.cs @@ -172,7 +172,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti if (newHead == null) { - var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl); + var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl, authentication); if (remoteReference != null) { repository.Network.Fetch(repositoryUrl, new[] @@ -202,11 +202,11 @@ private static Reference GetLocalReference(Repository repository, string branchN return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName)); } - private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl) + private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl, Authentication authentication) { var targetBranchName = branchName.GetCanonicalBranchName(); - var remoteReferences = repository.Network.ListReferences(repositoryUrl); + var remoteReferences = GitHelper.GetRemoteTipsUsingUsernamePasswordCredentials(repository, repositoryUrl, authentication.Username, authentication.Password); return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName)); } }