From 5ae5681c991d9d73def765087b16a3b0acc46df4 Mon Sep 17 00:00:00 2001 From: Phil Oyston Date: Wed, 26 Aug 2015 09:53:34 +0100 Subject: [PATCH 1/2] re #611 - Ensures branch when using dynamic repositories with buildserver When using a dynamic repository with a buildserver the current branch will for most buildserver types return null or empty, this causes no branch to be set, dynamic repositories require a branch. The fix ensures that if we are using dynamic repositories with a buildserver that if the buildserver does not return a branch we use the targetBranch specified via /b --- src/GitVersionCore/ExecuteCore.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/GitVersionCore/ExecuteCore.cs b/src/GitVersionCore/ExecuteCore.cs index 06689d964e..e4aa26d7f9 100644 --- a/src/GitVersionCore/ExecuteCore.cs +++ b/src/GitVersionCore/ExecuteCore.cs @@ -13,6 +13,8 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string var applicableBuildServers = BuildServerList.GetApplicableBuildServers(); var buildServer = applicableBuildServers.FirstOrDefault(); var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch(); + + currentBranch = string.IsNullOrWhiteSpace(currentBranch) ? targetBranch : currentBranch; if (!string.IsNullOrEmpty(currentBranch)) { Logger.WriteInfo("Branch from build environment: " + currentBranch); From 628cb9934dc877a3d81582bb0caced6a72b9ea7d Mon Sep 17 00:00:00 2001 From: Phil Oyston Date: Wed, 26 Aug 2015 12:14:40 +0100 Subject: [PATCH 2/2] re #611 - current branch resolution simplification Updates the BuildServerBase to provide a default implementation of GetCurrentBranch() that returns null, derived build servers can override if needed, this simplifies the resolution of the current branch name within ExecuteCore --- .../BuildServers/BuildServerBaseTests.cs | 5 ----- src/GitVersionCore/BuildServers/AppVeyor.cs | 5 ----- .../BuildServers/BuildServerBase.cs | 6 +++++- src/GitVersionCore/BuildServers/ContinuaCi.cs | 2 -- src/GitVersionCore/BuildServers/Jenkins.cs | 2 -- src/GitVersionCore/BuildServers/MyGet.cs | 2 -- src/GitVersionCore/BuildServers/TeamCity.cs | 2 -- src/GitVersionCore/ExecuteCore.cs | 19 ++++++++++++------- 8 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs b/src/GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs index e888f9aec8..580cd52d84 100644 --- a/src/GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs @@ -44,10 +44,5 @@ public override string[] GenerateSetParameterMessage(string name, string value) { return new string[0]; } - - public override string GetCurrentBranch() - { - throw new NotImplementedException(); - } } } \ No newline at end of file diff --git a/src/GitVersionCore/BuildServers/AppVeyor.cs b/src/GitVersionCore/BuildServers/AppVeyor.cs index 91fb9833ab..8d21861cbe 100644 --- a/src/GitVersionCore/BuildServers/AppVeyor.cs +++ b/src/GitVersionCore/BuildServers/AppVeyor.cs @@ -59,10 +59,5 @@ public override string[] GenerateSetParameterMessage(string name, string value) string.Format("Adding Environment Variable. name='GitVersion_{0}' value='{1}']", name, value) }; } - - public override string GetCurrentBranch() - { - return null; - } } } \ No newline at end of file diff --git a/src/GitVersionCore/BuildServers/BuildServerBase.cs b/src/GitVersionCore/BuildServers/BuildServerBase.cs index 2bc98a1483..f41c354fdd 100644 --- a/src/GitVersionCore/BuildServers/BuildServerBase.cs +++ b/src/GitVersionCore/BuildServers/BuildServerBase.cs @@ -7,7 +7,11 @@ public abstract class BuildServerBase : IBuildServer public abstract bool CanApplyToCurrentContext(); public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber); public abstract string[] GenerateSetParameterMessage(string name, string value); - public abstract string GetCurrentBranch(); + + public virtual string GetCurrentBranch() + { + return null; + } public virtual void WriteIntegration(Action writer, VersionVariables variables) { diff --git a/src/GitVersionCore/BuildServers/ContinuaCi.cs b/src/GitVersionCore/BuildServers/ContinuaCi.cs index dfefee8fef..1478fbd65e 100644 --- a/src/GitVersionCore/BuildServers/ContinuaCi.cs +++ b/src/GitVersionCore/BuildServers/ContinuaCi.cs @@ -29,8 +29,6 @@ public override string[] GenerateSetParameterMessage(string name, string value) }; } - public override string GetCurrentBranch() { return string.Empty; } - public override string GenerateSetVersionMessage(string versionToUseForBuildNumber) { return string.Format("@@continua[setBuildVersion value='{0}']", versionToUseForBuildNumber); diff --git a/src/GitVersionCore/BuildServers/Jenkins.cs b/src/GitVersionCore/BuildServers/Jenkins.cs index f1970312a8..d9608741f4 100644 --- a/src/GitVersionCore/BuildServers/Jenkins.cs +++ b/src/GitVersionCore/BuildServers/Jenkins.cs @@ -26,8 +26,6 @@ public override string GenerateSetVersionMessage(string versionToUseForBuildNumb return versionToUseForBuildNumber; } - public override string GetCurrentBranch() { return string.Empty; } - public override string[] GenerateSetParameterMessage(string name, string value) { return new[] diff --git a/src/GitVersionCore/BuildServers/MyGet.cs b/src/GitVersionCore/BuildServers/MyGet.cs index 2f4a1b7fcb..0b74e308e7 100644 --- a/src/GitVersionCore/BuildServers/MyGet.cs +++ b/src/GitVersionCore/BuildServers/MyGet.cs @@ -13,8 +13,6 @@ public override bool CanApplyToCurrentContext() && buildRunner.Equals("MyGet", StringComparison.InvariantCultureIgnoreCase); } - public override string GetCurrentBranch() { return string.Empty; } - public override string[] GenerateSetParameterMessage(string name, string value) { var messages = new List diff --git a/src/GitVersionCore/BuildServers/TeamCity.cs b/src/GitVersionCore/BuildServers/TeamCity.cs index ffb207bace..2140a886f8 100644 --- a/src/GitVersionCore/BuildServers/TeamCity.cs +++ b/src/GitVersionCore/BuildServers/TeamCity.cs @@ -9,8 +9,6 @@ public override bool CanApplyToCurrentContext() return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION")); } - public override string GetCurrentBranch() { return string.Empty; } - public override string[] GenerateSetParameterMessage(string name, string value) { return new[] diff --git a/src/GitVersionCore/ExecuteCore.cs b/src/GitVersionCore/ExecuteCore.cs index e4aa26d7f9..9c40d3437d 100644 --- a/src/GitVersionCore/ExecuteCore.cs +++ b/src/GitVersionCore/ExecuteCore.cs @@ -12,14 +12,9 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory); var applicableBuildServers = BuildServerList.GetApplicableBuildServers(); var buildServer = applicableBuildServers.FirstOrDefault(); - var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch(); - currentBranch = string.IsNullOrWhiteSpace(currentBranch) ? targetBranch : currentBranch; - if (!string.IsNullOrEmpty(currentBranch)) - { - Logger.WriteInfo("Branch from build environment: " + currentBranch); - } - gitPreparer.Initialise(buildServer != null, currentBranch ?? targetBranch); + gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch)); + var dotGitDirectory = gitPreparer.GetDotGitDirectory(); var projectRoot = gitPreparer.GetProjectRootDirectory(); Logger.WriteInfo(string.Format("Project root is: " + projectRoot)); @@ -42,5 +37,15 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string return variables; } + + private static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch) + { + if (buildServer == null) return targetBranch; + + var currentBranch = buildServer.GetCurrentBranch() ?? targetBranch; + Logger.WriteInfo("Branch from build environment: " + currentBranch); + + return currentBranch; + } } } \ No newline at end of file