Skip to content

Commit 628cb99

Browse files
committed
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
1 parent 5ae5681 commit 628cb99

File tree

8 files changed

+17
-26
lines changed

8 files changed

+17
-26
lines changed

src/GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,5 @@ public override string[] GenerateSetParameterMessage(string name, string value)
4444
{
4545
return new string[0];
4646
}
47-
48-
public override string GetCurrentBranch()
49-
{
50-
throw new NotImplementedException();
51-
}
5247
}
5348
}

src/GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,5 @@ public override string[] GenerateSetParameterMessage(string name, string value)
5959
string.Format("Adding Environment Variable. name='GitVersion_{0}' value='{1}']", name, value)
6060
};
6161
}
62-
63-
public override string GetCurrentBranch()
64-
{
65-
return null;
66-
}
6762
}
6863
}

src/GitVersionCore/BuildServers/BuildServerBase.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ public abstract class BuildServerBase : IBuildServer
77
public abstract bool CanApplyToCurrentContext();
88
public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber);
99
public abstract string[] GenerateSetParameterMessage(string name, string value);
10-
public abstract string GetCurrentBranch();
10+
11+
public virtual string GetCurrentBranch()
12+
{
13+
return null;
14+
}
1115

1216
public virtual void WriteIntegration(Action<string> writer, VersionVariables variables)
1317
{

src/GitVersionCore/BuildServers/ContinuaCi.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public override string[] GenerateSetParameterMessage(string name, string value)
2929
};
3030
}
3131

32-
public override string GetCurrentBranch() { return string.Empty; }
33-
3432
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
3533
{
3634
return string.Format("@@continua[setBuildVersion value='{0}']", versionToUseForBuildNumber);

src/GitVersionCore/BuildServers/Jenkins.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public override string GenerateSetVersionMessage(string versionToUseForBuildNumb
2626
return versionToUseForBuildNumber;
2727
}
2828

29-
public override string GetCurrentBranch() { return string.Empty; }
30-
3129
public override string[] GenerateSetParameterMessage(string name, string value)
3230
{
3331
return new[]

src/GitVersionCore/BuildServers/MyGet.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ public override bool CanApplyToCurrentContext()
1313
&& buildRunner.Equals("MyGet", StringComparison.InvariantCultureIgnoreCase);
1414
}
1515

16-
public override string GetCurrentBranch() { return string.Empty; }
17-
1816
public override string[] GenerateSetParameterMessage(string name, string value)
1917
{
2018
var messages = new List<string>

src/GitVersionCore/BuildServers/TeamCity.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ public override bool CanApplyToCurrentContext()
99
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
1010
}
1111

12-
public override string GetCurrentBranch() { return string.Empty; }
13-
1412
public override string[] GenerateSetParameterMessage(string name, string value)
1513
{
1614
return new[]

src/GitVersionCore/ExecuteCore.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
1212
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
1313
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
1414
var buildServer = applicableBuildServers.FirstOrDefault();
15-
var currentBranch = buildServer == null ? null : buildServer.GetCurrentBranch();
1615

17-
currentBranch = string.IsNullOrWhiteSpace(currentBranch) ? targetBranch : currentBranch;
18-
if (!string.IsNullOrEmpty(currentBranch))
19-
{
20-
Logger.WriteInfo("Branch from build environment: " + currentBranch);
21-
}
22-
gitPreparer.Initialise(buildServer != null, currentBranch ?? targetBranch);
16+
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));
17+
2318
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
2419
var projectRoot = gitPreparer.GetProjectRootDirectory();
2520
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));
@@ -42,5 +37,15 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
4237

4338
return variables;
4439
}
40+
41+
private static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
42+
{
43+
if (buildServer == null) return targetBranch;
44+
45+
var currentBranch = buildServer.GetCurrentBranch() ?? targetBranch;
46+
Logger.WriteInfo("Branch from build environment: " + currentBranch);
47+
48+
return currentBranch;
49+
}
4550
}
4651
}

0 commit comments

Comments
 (0)