public static void CreateFakeBranchPointingAtThePullRequestTip(this IGitRepository repo, ILog log, AuthenticationInfo authentication) { var remote = repo.Network.Remotes.Single(); log.Info("Fetching remote refs to see if there is a pull request ref"); var remoteTips = (string.IsNullOrEmpty(authentication.Username) ? repo.GetRemoteTipsForAnonymousUser(remote) : repo.GetRemoteTipsUsingUsernamePasswordCredentials(remote, authentication.Username, authentication.Password)) .ToList(); log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName))); var headTipSha = repo.Head.Tip.Sha; var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList(); if (refs.Count == 0) { var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'."; throw new WarningException(message); } if (refs.Count > 1) { var names = string.Join(", ", refs.Select(r => r.CanonicalName)); var message = $"Found more than one remote tip from remote '{remote.Url}' pointing at the commit '{headTipSha}'. Unable to determine which one to use ({names})."; throw new WarningException(message); } var reference = refs[0]; var canonicalName = reference.CanonicalName; log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'."); if (canonicalName.StartsWith("refs/tags")) { log.Info($"Checking out tag '{canonicalName}'"); repo.Commands.Checkout(reference.Target.Sha); return; } if (!canonicalName.StartsWith("refs/pull/") && !canonicalName.StartsWith("refs/pull-requests/")) { var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request."; throw new WarningException(message); } var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); log.Info($"Creating fake local branch '{fakeBranchName}'."); repo.Refs.Add(fakeBranchName, new ObjectId(headTipSha)); log.Info($"Checking local branch '{fakeBranchName}' out."); repo.Commands.Checkout(fakeBranchName); }