public void FetchWithoutConflict(string remoteUrl, string branchName)
{
var tracer = _tracerFactory.GetTracer();
try
{
using (var repo = new LibGit2Sharp.Repository(RepositoryPath))
{
var trackedBranchName = string.Format("{0}/{1}", _remoteAlias, branchName);
var refSpec = string.Format("+refs/heads/{0}:refs/remotes/{1}", branchName, trackedBranchName);
// Remove it if it already exists (does not throw if it doesn't)
repo.Network.Remotes.Remove(_remoteAlias);
// Configure the remote
var remote = repo.Network.Remotes.Add(_remoteAlias, remoteUrl, refSpec);
using (tracer.Step("LibGit2SharpRepository Fetch"))
{
// This will only retrieve the "master"
repo.Network.Fetch(remote);
}
using (tracer.Step("LibGit2SharpRepository Update"))
{
// Are we fetching a tag?
if (branchName.Trim().StartsWith("refs/tags/", StringComparison.OrdinalIgnoreCase))
{
var trackedTag = repo.Tags[branchName];
if (trackedTag == null)
{
throw new BranchNotFoundException(branchName, null);
}
// Update the raw ref to point to the tag
UpdateRawRef(branchName, branchName);
// Now checkout out the tag, which points to the right place
Update(branchName);
}
else
{
// Optionally set up the branch tracking configuration
var trackedBranch = repo.Branches[trackedBranchName];
if (trackedBranch == null)
{
throw new BranchNotFoundException(branchName, null);
}
var branch = repo.Branches[branchName] ?? repo.CreateBranch(branchName, trackedBranch.Tip);
repo.Branches.Update(branch,
b => b.TrackedBranch = trackedBranch.CanonicalName);
// Update the raw ref to point the head of branchName to the latest fetched branch
UpdateRawRef(string.Format("refs/heads/{0}", branchName), trackedBranchName);
// Now checkout out our branch, which points to the right place
Update(branchName);
}
}
}
}
catch (LibGit2SharpException exception)
{
// LibGit2Sharp doesn't support SSH yet. Use GitExeRepository
// LibGit2Sharp only supports smart Http protocol
if (exception.Message.Equals("Unsupported URL protocol") ||
exception.Message.Equals("Received unexpected content-type"))
{
tracer.TraceWarning("LibGit2SharpRepository fallback to git.exe due to {0}", exception.Message);
_legacyGitExeRepository.FetchWithoutConflict(remoteUrl, branchName);
}
else
{
throw;
}
}
}