Skip to content

Commit 2201f0d

Browse files
committed
Merge pull request #468 from eggapauli/use-authentication
Use authentication in `GitPreparer.GetRemoteReference`
2 parents 97dade8 + 7b343f3 commit 2201f0d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GitVersion
22
{
3+
using System;
34
using LibGit2Sharp;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -164,6 +165,20 @@ static void CreateFakeBranchPointingAtThePullRequestTip(Repository repo, Authent
164165
repo.Checkout(fakeBranchName);
165166
}
166167

168+
internal static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, string repoUrl, string username, string password)
169+
{
170+
// This is a work-around as long as https://github.com/libgit2/libgit2sharp/issues/1099 is not fixed
171+
var remote = repo.Network.Remotes.Add(Guid.NewGuid().ToString(), repoUrl);
172+
try
173+
{
174+
return GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, username, password);
175+
}
176+
finally
177+
{
178+
repo.Network.Remotes.Remove(remote.Name);
179+
}
180+
}
181+
167182
static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCredentials(Repository repo, Remote remote, string username, string password)
168183
{
169184
return repo.Network.ListReferences(remote, (url, fromUrl, types) => new UsernamePasswordCredentials

GitVersionCore/GitPreparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
172172

173173
if (newHead == null)
174174
{
175-
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl);
175+
var remoteReference = GetRemoteReference(repository, targetBranch, repositoryUrl, authentication);
176176
if (remoteReference != null)
177177
{
178178
repository.Network.Fetch(repositoryUrl, new[]
@@ -202,11 +202,11 @@ private static Reference GetLocalReference(Repository repository, string branchN
202202
return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName));
203203
}
204204

205-
private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl)
205+
private static DirectReference GetRemoteReference(Repository repository, string branchName, string repositoryUrl, Authentication authentication)
206206
{
207207
var targetBranchName = branchName.GetCanonicalBranchName();
208-
var remoteReferences = repository.Network.ListReferences(repositoryUrl);
209208

209+
var remoteReferences = GitHelper.GetRemoteTipsUsingUsernamePasswordCredentials(repository, repositoryUrl, authentication.Username, authentication.Password);
210210
return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName));
211211
}
212212
}

0 commit comments

Comments
 (0)