Skip to content

Commit b5c0ae5

Browse files
committed
Merge pull request #262 from gep13/AppVeyorFixes
Correcting AppVeyor Support for Pull Requests
2 parents 7472ceb + 8b9aef9 commit b5c0ae5

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 8 additions & 6 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)
@@ -63,8 +61,12 @@ public override string GenerateSetVersionMessage(string versionToUseForBuildNumb
6361

6462
public override string[] GenerateSetParameterMessage(string name, string value)
6563
{
66-
// Currently not supported by AppVeyor API
67-
return new string[0];
64+
Environment.SetEnvironmentVariable("GitVersion." + name, value);
65+
66+
return new[]
67+
{
68+
string.Format("Adding Environment Variable. name='GitVersion.{0}' value='{1}']", name, value),
69+
};
6870
}
6971
}
7072
}

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.IsRemote && 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)