Skip to content

Commit 6130867

Browse files
authored
Merge pull request #1541 from bert2/bugfix/issue-1536-ignore-versions-in-branch-names
Ignore version numbers in branch names that are not release branches.
2 parents 81c8c84 + 426de26 commit 6130867

File tree

13 files changed

+378
-73
lines changed

13 files changed

+378
-73
lines changed

src/GitVersionCore.Tests/IntegrationTests/OtherBranchScenarios.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using GitTools.Testing;
1+
using GitTools.Testing;
22
using GitVersionCore.Tests;
33
using LibGit2Sharp;
44
using NUnit.Framework;
@@ -15,10 +15,10 @@ public void CanTakeVersionFromReleaseBranch()
1515
const string TaggedVersion = "1.0.3";
1616
fixture.Repository.MakeATaggedCommit(TaggedVersion);
1717
fixture.Repository.MakeCommits(5);
18-
fixture.Repository.CreateBranch("alpha-2.0.0");
19-
Commands.Checkout(fixture.Repository, "alpha-2.0.0");
18+
fixture.Repository.CreateBranch("release/beta-2.0.0");
19+
Commands.Checkout(fixture.Repository, "release/beta-2.0.0");
2020

21-
fixture.AssertFullSemver("2.0.0-alpha.1+0");
21+
fixture.AssertFullSemver("2.0.0-beta.1+0");
2222
}
2323
}
2424

@@ -57,4 +57,4 @@ public void ShouldNotGetVersionFromFeatureBranchIfNotMerged()
5757
version.SemVer.ShouldBe("1.0.0-alpha.1");
5858
}
5959
}
60-
}
60+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
namespace GitVersionCore.Tests.IntegrationTests
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using GitTools.Testing;
7+
8+
using GitVersion;
9+
10+
using GitVersionCore.Tests;
11+
12+
using LibGit2Sharp;
13+
14+
using NUnit.Framework;
15+
16+
[TestFixture]
17+
public class VersionInCurrentBranchNameScenarios : TestBase
18+
{
19+
[Test]
20+
public void TakesVersionFromNameOfReleaseBranch()
21+
{
22+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
23+
{
24+
fixture.BranchTo("release/2.0.0");
25+
26+
fixture.AssertFullSemver("2.0.0-beta.1+0");
27+
}
28+
}
29+
30+
[Test]
31+
public void DoesNotTakeVersionFromNameOfNonReleaseBranch()
32+
{
33+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
34+
{
35+
fixture.BranchTo("feature/upgrade-power-level-to-9000.0.1");
36+
37+
fixture.AssertFullSemver("1.1.0-upgrade-power-level-to-9000-0-1.1+1");
38+
}
39+
}
40+
41+
[Test]
42+
public void TakesVersionFromNameOfBranchThatIsReleaseByConfig()
43+
{
44+
var config = new Config
45+
{
46+
Branches = new Dictionary<string, BranchConfig> { { "support", new BranchConfig { IsReleaseBranch = true } } }
47+
};
48+
49+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
50+
{
51+
fixture.BranchTo("support/2.0.0");
52+
53+
fixture.AssertFullSemver(config, "2.0.0+1");
54+
}
55+
}
56+
57+
[Test]
58+
public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin()
59+
{
60+
using (var fixture = new RemoteRepositoryFixture())
61+
{
62+
fixture.BranchTo("release/2.0.0");
63+
fixture.MakeACommit();
64+
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, new string[0], new FetchOptions(), null);
65+
66+
fixture.LocalRepositoryFixture.Checkout("origin/release/2.0.0");
67+
68+
fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0-beta.1+1");
69+
}
70+
}
71+
72+
[Test]
73+
public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote()
74+
{
75+
using (var fixture = new RemoteRepositoryFixture())
76+
{
77+
fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", "upstream");
78+
fixture.BranchTo("release/2.0.0");
79+
fixture.MakeACommit();
80+
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, new string[0], new FetchOptions(), null);
81+
82+
fixture.LocalRepositoryFixture.Checkout("upstream/release/2.0.0");
83+
84+
fixture.LocalRepositoryFixture.AssertFullSemver("0.1.0-beta.1+5");
85+
}
86+
}
87+
}
88+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
namespace GitVersionCore.Tests.IntegrationTests
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
using GitTools.Testing;
7+
8+
using GitVersion;
9+
10+
using GitVersionCore.Tests;
11+
12+
using LibGit2Sharp;
13+
14+
using NUnit.Framework;
15+
16+
[TestFixture]
17+
public class VersionInMergedBranchNameScenarios : TestBase
18+
{
19+
[Test]
20+
public void TakesVersionFromNameOfReleaseBranch()
21+
{
22+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
23+
{
24+
fixture.CreateAndMergeBranchIntoDevelop("release/2.0.0");
25+
26+
fixture.AssertFullSemver("2.1.0-alpha.2");
27+
}
28+
}
29+
30+
[Test]
31+
public void DoesNotTakeVersionFromNameOfNonReleaseBranch()
32+
{
33+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
34+
{
35+
fixture.CreateAndMergeBranchIntoDevelop("pull-request/improved-by-upgrading-some-lib-to-4.5.6");
36+
fixture.CreateAndMergeBranchIntoDevelop("hotfix/downgrade-some-lib-to-3.2.1-to-avoid-breaking-changes");
37+
38+
fixture.AssertFullSemver("1.1.0-alpha.5");
39+
}
40+
}
41+
42+
[Test]
43+
public void TakesVersionFromNameOfBranchThatIsReleaseByConfig()
44+
{
45+
var config = new Config
46+
{
47+
Branches = new Dictionary<string, BranchConfig> { { "support", new BranchConfig { IsReleaseBranch = true } } }
48+
};
49+
50+
using (var fixture = new BaseGitFlowRepositoryFixture("1.0.0"))
51+
{
52+
fixture.CreateAndMergeBranchIntoDevelop("support/2.0.0");
53+
54+
fixture.AssertFullSemver(config, "2.1.0-alpha.2");
55+
}
56+
}
57+
58+
[Test]
59+
public void TakesVersionFromNameOfRemoteReleaseBranchInOrigin()
60+
{
61+
using (var fixture = new RemoteRepositoryFixture())
62+
{
63+
fixture.BranchTo("release/2.0.0");
64+
fixture.MakeACommit();
65+
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, new string[0], new FetchOptions(), null);
66+
67+
fixture.LocalRepositoryFixture.MergeNoFF("origin/release/2.0.0");
68+
69+
fixture.LocalRepositoryFixture.AssertFullSemver("2.0.0+0");
70+
}
71+
}
72+
73+
[Test]
74+
public void DoesNotTakeVersionFromNameOfRemoteReleaseBranchInCustomRemote()
75+
{
76+
using (var fixture = new RemoteRepositoryFixture())
77+
{
78+
fixture.LocalRepositoryFixture.Repository.Network.Remotes.Rename("origin", "upstream");
79+
fixture.BranchTo("release/2.0.0");
80+
fixture.MakeACommit();
81+
Commands.Fetch((Repository)fixture.LocalRepositoryFixture.Repository, fixture.LocalRepositoryFixture.Repository.Network.Remotes.First().Name, new string[0], new FetchOptions(), null);
82+
83+
fixture.LocalRepositoryFixture.MergeNoFF("upstream/release/2.0.0");
84+
85+
fixture.LocalRepositoryFixture.AssertFullSemver("0.1.0+6");
86+
}
87+
}
88+
}
89+
90+
internal static class BaseGitFlowRepositoryFixtureExtensions
91+
{
92+
public static void CreateAndMergeBranchIntoDevelop(this BaseGitFlowRepositoryFixture fixture, string branchName)
93+
{
94+
fixture.BranchTo(branchName);
95+
fixture.MakeACommit();
96+
fixture.Checkout("develop");
97+
fixture.MergeNoFF(branchName);
98+
}
99+
}
100+
}

src/GitVersionCore.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
namespace GitVersionCore.Tests.VersionCalculation.Strategies
1+
namespace GitVersionCore.Tests.VersionCalculation.Strategies
22
{
33
using System.Collections.Generic;
44
using System.Linq;
5+
6+
using GitVersion;
57
using GitVersion.VersionCalculation.BaseVersionCalculators;
68
using LibGit2Sharp;
79
using NUnit.Framework;
@@ -19,7 +21,7 @@ public void ShouldNotAllowIncrementOfVersion()
1921
{
2022
Head = new MockBranch("master") { new MockCommit
2123
{
22-
MessageEx = "Merge branch 'hotfix-0.1.5'",
24+
MessageEx = "Merge branch 'release-0.1.5'",
2325
ParentsEx = GetParents(true)
2426
} }
2527
}).Build();
@@ -30,38 +32,22 @@ public void ShouldNotAllowIncrementOfVersion()
3032
baseVersion.ShouldIncrement.ShouldBe(false);
3133
}
3234

33-
[TestCase("Merge branch 'hotfix-0.1.5'", false, null)]
34-
[TestCase("Merge branch 'develop' of github.com:Particular/NServiceBus into develop", true, null)]
35-
[TestCase("Merge branch '4.0.3'", true, "4.0.3")] //TODO: possible make it a config option to support this
3635
[TestCase("Merge branch 'release-10.10.50'", true, "10.10.50")]
37-
[TestCase("Merge branch 's'", true, null)] // Must start with a number
38-
[TestCase("Merge tag '10.10.50'", true, "10.10.50")]
3936
[TestCase("Merge branch 'release-0.2.0'", true, "0.2.0")]
4037
[TestCase("Merge branch 'Release-0.2.0'", true, "0.2.0")]
4138
[TestCase("Merge branch 'Release/0.2.0'", true, "0.2.0")]
42-
[TestCase("Merge branch 'hotfix-4.6.6' into support-4.6", true, "4.6.6")]
43-
[TestCase("Merge branch 'hotfix-10.10.50'", true, "10.10.50")]
44-
[TestCase("Merge branch 'Hotfix-10.10.50'", true, "10.10.50")]
45-
[TestCase("Merge branch 'Hotfix/10.10.50'", true, "10.10.50")]
46-
[TestCase("Merge branch 'hotfix-0.1.5'", true, "0.1.5")]
47-
[TestCase("Merge branch 'hotfix-4.2.2' into support-4.2", true, "4.2.2")]
48-
[TestCase("Merge branch 'somebranch' into release-3.0.0", true, null)]
49-
[TestCase("Merge branch 'hotfix-0.1.5'\n\nRelates to: TicketId", true, "0.1.5")]
50-
[TestCase("Merge branch 'alpha-0.1.5'", true, "0.1.5")]
51-
[TestCase("Merge pull request #165 from Particular/release-1.0.0", true, "1.0.0")]
52-
[TestCase("Merge pull request #95 from Particular/issue-94", false, null)]
53-
[TestCase("Merge pull request #165 in Particular/release-1.0.0", true, "1.0.0")]
54-
[TestCase("Merge pull request #95 in Particular/issue-94", true, null)]
55-
[TestCase("Merge pull request #95 in Particular/issue-94", false, null)]
56-
[TestCase("Merge pull request #64 from arledesma/feature-VS2013_3rd_party_test_framework_support", true, null)]
57-
[TestCase("Merge pull request #500 in FOO/bar from Particular/release-1.0.0 to develop)", true, "1.0.0")]
58-
[TestCase("Merge pull request #500 in FOO/bar from feature/new-service to develop)", true, null)]
39+
[TestCase("Merge branch 'releases-0.2.0'", true, "0.2.0")]
40+
[TestCase("Merge branch 'Releases-0.2.0'", true, "0.2.0")]
41+
[TestCase("Merge branch 'Releases/0.2.0'", true, "0.2.0")]
42+
[TestCase("Merge branch 'release-4.6.6' into support-4.6", true, "4.6.6")]
43+
[TestCase("Merge branch 'release-10.10.50'", true, "10.10.50")]
44+
[TestCase("Merge branch 'release-0.1.5'\n\nRelates to: TicketId", true, "0.1.5")]
5945
[TestCase("Finish Release-0.12.0", true, "0.12.0")] //Support Syntevo SmartGit/Hg's Gitflow merge commit messages for finishing a 'Release' branch
60-
[TestCase("Finish 0.14.1", true, "0.14.1")] //Support Syntevo SmartGit/Hg's Gitflow merge commit messages for finishing a 'Hotfix' branch
6146
[TestCase("Merge branch 'Release-v0.2.0'", true, "0.2.0")]
6247
[TestCase("Merge branch 'Release-v2.2'", true, "2.2.0")]
6348
[TestCase("Merge remote-tracking branch 'origin/release/0.8.0' into develop/master", true, "0.8.0")]
64-
public void AssertMergeMessage(string message, bool isMergeCommit, string expectedVersion)
49+
[TestCase("Merge remote-tracking branch 'refs/remotes/origin/release/2.0.0'", true, "2.0.0")]
50+
public void TakesVersionFromMergeOfReleaseBranch(string message, bool isMergeCommit, string expectedVersion)
6551
{
6652
var parents = GetParents(isMergeCommit);
6753
AssertMergeMessage(message, expectedVersion, parents);
@@ -74,6 +60,56 @@ public void AssertMergeMessage(string message, bool isMergeCommit, string expect
7460
AssertMergeMessage(message + "\n ", expectedVersion, parents);
7561
}
7662

63+
[TestCase("Merge branch 'hotfix-0.1.5'", false)]
64+
[TestCase("Merge branch 'develop' of github.com:Particular/NServiceBus into develop", true)]
65+
[TestCase("Merge branch '4.0.3'", true)]
66+
[TestCase("Merge branch 's'", true)]
67+
[TestCase("Merge tag '10.10.50'", true)]
68+
[TestCase("Merge branch 'hotfix-4.6.6' into support-4.6", true)]
69+
[TestCase("Merge branch 'hotfix-10.10.50'", true)]
70+
[TestCase("Merge branch 'Hotfix-10.10.50'", true)]
71+
[TestCase("Merge branch 'Hotfix/10.10.50'", true)]
72+
[TestCase("Merge branch 'hotfix-0.1.5'", true)]
73+
[TestCase("Merge branch 'hotfix-4.2.2' into support-4.2", true)]
74+
[TestCase("Merge branch 'somebranch' into release-3.0.0", true)]
75+
[TestCase("Merge branch 'hotfix-0.1.5'\n\nRelates to: TicketId", true)]
76+
[TestCase("Merge branch 'alpha-0.1.5'", true)]
77+
[TestCase("Merge pull request #95 from Particular/issue-94", false)]
78+
[TestCase("Merge pull request #95 in Particular/issue-94", true)]
79+
[TestCase("Merge pull request #95 in Particular/issue-94", false)]
80+
[TestCase("Merge pull request #64 from arledesma/feature-VS2013_3rd_party_test_framework_support", true)]
81+
[TestCase("Merge pull request #500 in FOO/bar from Particular/release-1.0.0 to develop)", true)]
82+
[TestCase("Merge pull request #500 in FOO/bar from feature/new-service to develop)", true)]
83+
[TestCase("Finish 0.14.1", true)] // Don't support Syntevo SmartGit/Hg's Gitflow merge commit messages for finishing a 'Hotfix' branch
84+
public void ShouldNotTakeVersionFromMergeOfNonReleaseBranch(string message, bool isMergeCommit)
85+
{
86+
var parents = GetParents(isMergeCommit);
87+
AssertMergeMessage(message, null, parents);
88+
AssertMergeMessage(message + " ", null, parents);
89+
AssertMergeMessage(message + "\r ", null, parents);
90+
AssertMergeMessage(message + "\r", null, parents);
91+
AssertMergeMessage(message + "\r\n", null, parents);
92+
AssertMergeMessage(message + "\r\n ", null, parents);
93+
AssertMergeMessage(message + "\n", null, parents);
94+
AssertMergeMessage(message + "\n ", null, parents);
95+
}
96+
97+
[TestCase("Merge pull request #165 from Particular/release-1.0.0", true)]
98+
[TestCase("Merge pull request #165 in Particular/release-1.0.0", true)]
99+
[TestCase("Merge pull request #500 in FOO/bar from Particular/release-1.0.0 to develop)", true)]
100+
public void ShouldNotTakeVersionFromMergeOfReleaseBranchWithRemoteOtherThanOrigin(string message, bool isMergeCommit)
101+
{
102+
var parents = GetParents(isMergeCommit);
103+
AssertMergeMessage(message, null, parents);
104+
AssertMergeMessage(message + " ", null, parents);
105+
AssertMergeMessage(message + "\r ", null, parents);
106+
AssertMergeMessage(message + "\r", null, parents);
107+
AssertMergeMessage(message + "\r\n", null, parents);
108+
AssertMergeMessage(message + "\r\n ", null, parents);
109+
AssertMergeMessage(message + "\n", null, parents);
110+
AssertMergeMessage(message + "\n ", null, parents);
111+
}
112+
77113
[TestCase(@"Merge pull request #1 in FOO/bar from feature/ISSUE-1 to develop
78114
79115
* commit '38560a7eed06e8d3f3f1aaf091befcdf8bf50fea':
@@ -89,14 +125,26 @@ Another commit message
89125
[TestCase(@"Merge branch 'master' of http://172.16.3.10:8082/r/asu_tk/p_sd")]
90126
[TestCase(@"Merge branch 'master' of http://212.248.89.56:8082/r/asu_tk/p_sd")]
91127
[TestCase(@"Merge branch 'DEMO' of http://10.10.10.121/gitlab/mtolland/orcid into DEMO")]
92-
public void MergeMessagesThatIsNotRelatedToGitVersion(string commitMessage)
128+
public void ShouldNotTakeVersionFromUnrelatedMerge(string commitMessage)
93129
{
94130
var parents = GetParents(true);
95131

96132
AssertMergeMessage(commitMessage, null, parents);
97133
}
98134

99-
static void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents)
135+
[TestCase("Merge branch 'support/0.2.0'", "support", "0.2.0")]
136+
[TestCase("Merge branch 'support/0.2.0'", null, null)]
137+
[TestCase("Merge branch 'release/2.0.0'", null, "2.0.0")]
138+
public void TakesVersionFromMergeOfConfiguredReleaseBranch(string message, string releaseBranch, string expectedVersion)
139+
{
140+
var config = new Config();
141+
if (releaseBranch != null) config.Branches[releaseBranch] = new BranchConfig { IsReleaseBranch = true };
142+
var parents = GetParents(true);
143+
144+
AssertMergeMessage(message, expectedVersion, parents, config);
145+
}
146+
147+
static void AssertMergeMessage(string message, string expectedVersion, List<Commit> parents, Config config = null)
100148
{
101149
var commit = new MockCommit
102150
{
@@ -105,6 +153,7 @@ static void AssertMergeMessage(string message, string expectedVersion, List<Comm
105153
};
106154

107155
var context = new GitVersionContextBuilder()
156+
.WithConfig(config ?? new Config())
108157
.WithRepository(new MockRepository
109158
{
110159
Head = new MockBranch("master")
@@ -145,4 +194,4 @@ static List<Commit> GetParents(bool isMergeCommit)
145194
};
146195
}
147196
}
148-
}
197+
}

0 commit comments

Comments
 (0)