diff --git a/GitVersionCore/BuildServers/AppVeyor.cs b/GitVersionCore/BuildServers/AppVeyor.cs index d5e28114de..67a513933e 100644 --- a/GitVersionCore/BuildServers/AppVeyor.cs +++ b/GitVersionCore/BuildServers/AppVeyor.cs @@ -22,12 +22,10 @@ public override void PerformPreProcessingSteps(string gitDirectory) { if (string.IsNullOrEmpty(gitDirectory)) { - 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"); + throw new WarningException("Failed to find .git directory on agent."); } - var repoBranch = Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH"); - - GitHelper.NormalizeGitDirectory(gitDirectory, authentication, repoBranch); + GitHelper.NormalizeGitDirectory(gitDirectory, authentication); } public override string GenerateSetVersionMessage(string versionToUseForBuildNumber) @@ -63,8 +61,12 @@ public override string GenerateSetVersionMessage(string versionToUseForBuildNumb public override string[] GenerateSetParameterMessage(string name, string value) { - // Currently not supported by AppVeyor API - return new string[0]; + Environment.SetEnvironmentVariable("GitVersion." + name, value); + + return new[] + { + string.Format("Adding Environment Variable. name='GitVersion.{0}' value='{1}']", name, value), + }; } } } \ No newline at end of file diff --git a/GitVersionCore/BuildServers/GitHelper.cs b/GitVersionCore/BuildServers/GitHelper.cs index 6174e2e3f3..2e58d25a86 100644 --- a/GitVersionCore/BuildServers/GitHelper.cs +++ b/GitVersionCore/BuildServers/GitHelper.cs @@ -9,7 +9,7 @@ public static class GitHelper { const string MergeMessageRegexPattern = "refs/heads/pull(-requests)?/(?[0-9]*)/merge"; - public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, string branch = null) + public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication) { using (var repo = new Repository(gitDirectory)) { @@ -25,22 +25,37 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name); + var headSha = repo.Refs.Head.TargetIdentifier; + if (!repo.Info.IsHeadDetached) { - Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", repo.Refs.Head.TargetIdentifier)); + Logger.WriteInfo(string.Format("HEAD points at branch '{0}'.", headSha)); return; } + + Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", headSha)); + + // 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. + // If they do, go ahead and checkout that branch + // If no, go ahead and check out a new branch, using the known commit SHA as the pointer + var localBranchesWhereCommitShaIsHead = repo.Branches.Where(b => !b.IsRemote && b.Tip.Sha == headSha).ToList(); + + if (localBranchesWhereCommitShaIsHead.Count > 1) + { + var names = string.Join(", ", localBranchesWhereCommitShaIsHead.Select(r => r.CanonicalName)); + 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); + throw new WarningException(message); + } - Logger.WriteInfo(string.Format("HEAD is detached and points at commit '{0}'.", repo.Refs.Head.TargetIdentifier)); - - if (branch != null) + if (localBranchesWhereCommitShaIsHead.Count == 0) { - Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", branch)); - repo.Checkout("refs/heads/" + branch); + Logger.WriteInfo(string.Format("No local branch pointing at the commit '{0}'. Fake branch needs to be created.", headSha)); + CreateFakeBranchPointingAtThePullRequestTip(repo, authentication); } else { - CreateFakeBranchPointingAtThePullRequestTip(repo, authentication); + Logger.WriteInfo(string.Format("Checking out local branch 'refs/heads/{0}'.", localBranchesWhereCommitShaIsHead[0].Name)); + repo.Branches[localBranchesWhereCommitShaIsHead[0].Name].Checkout(); } } }