Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correcting AppVeyor Support for Pull Requests #262

Merged
merged 3 commits into from
Oct 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions GitVersionCore/BuildServers/AppVeyor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
};
}
}
}
31 changes: 23 additions & 8 deletions GitVersionCore/BuildServers/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class GitHelper
{
const string MergeMessageRegexPattern = "refs/heads/pull(-requests)?/(?<issuenumber>[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))
{
Expand All @@ -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();
}
}
}
Expand Down