Skip to content

Commit 38b8fb1

Browse files
authored
Merge pull request #911 from bdbehrho/feature/DontUseRemoteNameWhenCopyingRemoteBranch
Fix to stop GitVersion from introducing the remote name into branch name
2 parents 391f5f3 + 1efa2b7 commit 38b8fb1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/GitVersionCore.Tests/BuildServers/EnvironmentVariableJenkinsTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
public class EnvironmentVariableJenkinsTests
88
{
99
string key = "JENKINS_URL";
10+
string branch = "GIT_BRANCH";
11+
string localBranch = "GIT_LOCAL_BRANCH";
1012

1113
private void SetEnvironmentVariableForDetection()
1214
{
@@ -33,4 +35,29 @@ public void CanNotApplyCurrentContextWhenEnvironmentVariableIsNotSet()
3335
var j = new Jenkins();
3436
j.CanApplyToCurrentContext().ShouldBe(false);
3537
}
38+
39+
[Test]
40+
public void JenkinsTakesLocalBranchNameNotRemoteName()
41+
{
42+
// Save original values so they can be restored
43+
string branchOrig = Environment.GetEnvironmentVariable(branch);
44+
string localBranchOrig = Environment.GetEnvironmentVariable(localBranch);
45+
46+
// Set GIT_BRANCH for testing
47+
Environment.SetEnvironmentVariable(branch, "origin/master");
48+
49+
// Test Jenkins that GetCurrentBranch falls back to GIT_BRANCH if GIT_LOCAL_BRANCH undefined
50+
var j = new Jenkins();
51+
j.GetCurrentBranch(true).ShouldBe("origin/master");
52+
53+
// Set GIT_LOCAL_BRANCH
54+
Environment.SetEnvironmentVariable(localBranch, "master");
55+
56+
// Test Jenkins GetCurrentBranch method now returns GIT_LOCAL_BRANCH
57+
j.GetCurrentBranch(true).ShouldBe("master");
58+
59+
// Restore environment variables
60+
Environment.SetEnvironmentVariable(branch, branchOrig);
61+
Environment.SetEnvironmentVariable(localBranch, localBranchOrig);
62+
}
3663
}

src/GitVersionCore/BuildServers/Jenkins.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3636

3737
public override string GetCurrentBranch(bool usingDynamicRepos)
3838
{
39-
return Environment.GetEnvironmentVariable("GIT_BRANCH");
39+
return Environment.GetEnvironmentVariable("GIT_LOCAL_BRANCH") ?? Environment.GetEnvironmentVariable("GIT_BRANCH");
4040
}
4141

4242
public override bool PreventFetch()

0 commit comments

Comments
 (0)