From 683590cfef3faff69f97f3b91a4bfbc1a9dff573 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Fri, 26 Sep 2014 16:59:25 +0100 Subject: [PATCH 1/3] (#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 --- GitVersionCore/BuildServers/AppVeyor.cs | 6 ++--- GitVersionCore/BuildServers/GitHelper.cs | 31 ++++++++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/GitVersionCore/BuildServers/AppVeyor.cs b/GitVersionCore/BuildServers/AppVeyor.cs index d5e28114de..c9ea750984 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) diff --git a/GitVersionCore/BuildServers/GitHelper.cs b/GitVersionCore/BuildServers/GitHelper.cs index 6174e2e3f3..8d0c097a45 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.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(); } } } From d1fafa176bee5195511e9e615e71926f51ccdf7e Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Fri, 26 Sep 2014 17:38:16 +0100 Subject: [PATCH 2/3] (#260) Adding output for GenerateSetParameterMessage - As per suggestion here: https://github.com/Particular/GitVersion/issues/260#issuecomment-56985673 --- GitVersionCore/BuildServers/AppVeyor.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/GitVersionCore/BuildServers/AppVeyor.cs b/GitVersionCore/BuildServers/AppVeyor.cs index c9ea750984..67a513933e 100644 --- a/GitVersionCore/BuildServers/AppVeyor.cs +++ b/GitVersionCore/BuildServers/AppVeyor.cs @@ -61,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 From 8b9aef9a84cd20c74d01d9c7806cafb34ed9bb47 Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Sat, 27 Sep 2014 11:12:58 +0100 Subject: [PATCH 3/3] (#260) Favour local branches over remote ones - Updated with suggestion from @nulltoken - https://github.com/Particular/GitVersion/pull/262#discussion_r18103422 --- GitVersionCore/BuildServers/GitHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitVersionCore/BuildServers/GitHelper.cs b/GitVersionCore/BuildServers/GitHelper.cs index 8d0c097a45..2e58d25a86 100644 --- a/GitVersionCore/BuildServers/GitHelper.cs +++ b/GitVersionCore/BuildServers/GitHelper.cs @@ -38,8 +38,8 @@ public static void NormalizeGitDirectory(string gitDirectory, Authentication aut // 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.Tip.Sha == headSha).ToList(); - + 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));