Skip to content

Commit 683590c

Browse files
author
Gary Ewan Park
committed
(#260) Correcing AppVeyor Support for Pull Requests
- As per discussion with @nulltoken, implemented a fix for AppVeyor where if the commit sha is not present of any detected local branches, check out a new branch using the sha as the pointer
1 parent 5706e41 commit 683590c

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ public override void PerformPreProcessingSteps(string gitDirectory)
2222
{
2323
if (string.IsNullOrEmpty(gitDirectory))
2424
{
25-
throw new WarningException("Failed to find .git directory on agent. Please make sure agent checkout mode is enabled for you VCS roots - http://confluence.jetbrains.com/display/TCD8/VCS+Checkout+Mode");
25+
throw new WarningException("Failed to find .git directory on agent.");
2626
}
2727

28-
var repoBranch = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH");
29-
30-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, repoBranch);
28+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
3129
}
3230

3331
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class GitHelper
99
{
1010
const string MergeMessageRegexPattern = "refs/heads/pull(-requests)?/(?<issuenumber>[0-9]*)/merge";
1111

12-
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, string branch = null)
12+
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication)
1313
{
1414
using (var repo = new Repository(gitDirectory))
1515
{
@@ -25,22 +25,37 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut
2525

2626
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
2727

28+
var headSha = repo.Refs.Head.TargetIdentifier;
29+
2830
if (!repo.Info.IsHeadDetached)
2931
{
30-
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", repo.Refs.Head.TargetIdentifier));
32+
Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha));
3133
return;
3234
}
35+
36+
Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", headSha));
37+
38+
// In order to decide whether a fake branch is required or not, first check to see if any local branches have the same commit SHA of the head SHA.
39+
// If they do, go ahead and checkout that branch
40+
// If no, go ahead and check out a new branch, using the known commit SHA as the pointer
41+
var localBranchesWhereCommitShaIsHead = repo.Branches.Where(b => b.Tip.Sha == headSha).ToList();
42+
43+
if (localBranchesWhereCommitShaIsHead.Count > 1)
44+
{
45+
var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName));
46+
var message = string.Format("Found more than one local branch pointing at the commit '{0}'. Unable to determine which one to use ({1}).", headSha, names);
47+
throw new WarningException(message);
48+
}
3349

34-
Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", repo.Refs.Head.TargetIdentifier));
35-
36-
if (branch != null)
50+
if (localBranchesWhereCommitShaIsHead.Count == 0)
3751
{
38-
Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", branch));
39-
repo.Checkout("refs/heads/" + branch);
52+
Logger.WriteInfo(string.Format("No local branch pointing at the commit '{0}'. Fake branch needs to be created.", headSha));
53+
CreateFakeBranchPointingAtThePullRequestTip(repo, authentication);
4054
}
4155
else
4256
{
43-
CreateFakeBranchPointingAtThePullRequestTip(repo, authentication);
57+
Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", localBranchesWhereCommitShaIsHead[0].Name));
58+
repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout();
4459
}
4560
}
4661
}

0 commit comments

Comments
 (0)