Skip to content

Commit c850a3c

Browse files
authored
Merge pull request #3988 from HHobeck/feature/ensure-pull-request
EnsurePullRequestWithIncrementMajorOnMainAndMinorOnFeatureBranch
2 parents 8bf4cac + 5765f74 commit c850a3c

File tree

9 files changed

+101
-31
lines changed

9 files changed

+101
-31
lines changed

src/GitVersion.Core.Tests/Core/RepositoryStoreTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ public void FindCommitBranchWasBranchedFromShouldReturnNullIfTheRemoteIsTheOnlyS
227227
branch.ShouldNotBeNull();
228228

229229
var configuration = GitFlowConfigurationBuilder.New.Build();
230-
var branchedCommit = gitRepoMetadataProvider.FindCommitBranchWasBranchedFrom(branch, configuration, []);
230+
var branchedCommit = gitRepoMetadataProvider.FindCommitBranchBranchedFrom(branch, configuration, []);
231231
branchedCommit.ShouldBe(BranchCommit.Empty);
232232

233-
var branchedCommits = gitRepoMetadataProvider.FindCommitBranchesWasBranchedFrom(branch, configuration).ToArray();
233+
var branchedCommits = gitRepoMetadataProvider.FindCommitBranchesBranchedFrom(branch, configuration).ToArray();
234234
branchedCommits.ShouldBeEmpty();
235235
}
236236
}

src/GitVersion.Core.Tests/IntegrationTests/HotfixBranchScenarios.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void FeatureOnHotfixFeatureBranchDeleted()
162162
fixture.Checkout(hotfix451);
163163
fixture.MergeNoFF(featureBranch); // commit 2
164164
fixture.Repository.Branches.Remove(featureBranch);
165-
fixture.AssertFullSemver("4.5.1-beta.1+3", configuration);
165+
fixture.AssertFullSemver("4.5.1-beta.1+4", configuration);
166166
}
167167

168168
/// <summary>
@@ -208,7 +208,7 @@ public void FeatureOnHotfixFeatureBranchNotDeleted()
208208
fixture.Checkout(hotfix451);
209209
fixture.MergeNoFF(featureBranch); // commit 2
210210

211-
fixture.AssertFullSemver("4.5.1-beta.1+3", configuration);
211+
fixture.AssertFullSemver("4.5.1-beta.1+4", configuration);
212212
}
213213

214214
[Test]

src/GitVersion.Core.Tests/IntegrationTests/PullRequestScenarios.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Configuration;
12
using GitVersion.Core.Tests.Helpers;
23
using LibGit2Sharp;
34

@@ -6,6 +7,48 @@ namespace GitVersion.Core.Tests.IntegrationTests;
67
[TestFixture]
78
public class PullRequestScenarios : TestBase
89
{
10+
/// <summary>
11+
/// GitHubFlow - Pull requests (increment major on main and minor on feature)
12+
/// </summary>
13+
[Test]
14+
public void EnsurePullRequestWithIncrementMajorOnMainAndMinorOnFeatureBranch()
15+
{
16+
var configuration = GitHubFlowConfigurationBuilder.New
17+
.WithBranch("main", _ => _
18+
.WithIncrement(IncrementStrategy.Major)
19+
).WithBranch("feature", _ => _
20+
.WithIncrement(IncrementStrategy.Minor)
21+
).Build();
22+
23+
using var fixture = new EmptyRepositoryFixture();
24+
25+
fixture.MakeACommit("A");
26+
27+
// ✅ succeeds as expected
28+
fixture.AssertFullSemver("1.0.0-1", configuration);
29+
30+
fixture.ApplyTag("1.0.0");
31+
fixture.BranchTo("feature/foo");
32+
fixture.MakeACommit("B");
33+
34+
// ✅ succeeds as expected
35+
fixture.AssertFullSemver("1.1.0-foo.1+1", configuration);
36+
37+
fixture.Checkout("main");
38+
fixture.BranchTo("pull/2/merge");
39+
fixture.MergeNoFF("feature/foo");
40+
41+
// ✅ succeeds as expected
42+
fixture.AssertFullSemver("2.0.0-PullRequest2.2", configuration);
43+
44+
fixture.Checkout("main");
45+
fixture.Remove("pull/2/merge");
46+
fixture.MergeNoFF("feature/foo");
47+
48+
// ✅ succeeds as expected
49+
fixture.AssertFullSemver("2.0.0-2", configuration);
50+
}
51+
952
[Test]
1053
public void CanCalculatePullRequestChanges()
1154
{

src/GitVersion.Core/Core/Abstractions/IRepositoryStore.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ public interface IRepositoryStore
2727
/// Find the commit where the given branch was branched from another branch.
2828
/// If there are multiple such commits and branches, tries to guess based on commit histories.
2929
/// </summary>
30-
BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
30+
BranchCommit FindCommitBranchBranchedFrom(IBranch? branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
3131

32-
IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
32+
IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
3333

34-
IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches);
34+
IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches);
3535

3636
IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches);
3737

src/GitVersion.Core/Core/MainlineBranchFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public BranchCommit BranchOrigin(IBranch branch)
8383
return mergeBase;
8484
}
8585

86-
var branchCommit = this.repositoryStore.FindCommitBranchWasBranchedFrom(branch, this.configuration);
86+
var branchCommit = this.repositoryStore.FindCommitBranchBranchedFrom(branch, this.configuration);
8787
if (branchCommit != BranchCommit.Empty)
8888
{
8989
this.log.Info($"Found parent commit {branchCommit.Commit.Sha} for '{branchName}'.");

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,47 @@ public IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfigu
106106
params IBranch[] excludedBranches)
107107
=> GetSourceBranches(branch, configuration, (IEnumerable<IBranch>)excludedBranches);
108108

109-
public IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches)
109+
public IEnumerable<IBranch> GetSourceBranches(
110+
IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches)
110111
{
111112
var returnedBranches = new HashSet<IBranch>();
112113

113114
var referenceLookup = this.repository.Refs.ToLookup(r => r.TargetIdentifier);
114115

115-
foreach (var branchGrouping in FindCommitBranchesWasBranchedFrom(branch, configuration, excludedBranches)
116-
.GroupBy(element => element.Commit, element => element.Branch))
116+
var commitBranches = FindCommitBranchesBranchedFrom(branch, configuration, excludedBranches).ToHashSet();
117+
118+
var ignore = new HashSet<BranchCommit>();
119+
foreach (var commitBranch in commitBranches)
120+
{
121+
foreach (var commit in branch.Commits.Where(element => element.When > commitBranch.Commit.When))
122+
{
123+
var parents = commit.Parents.ToArray();
124+
if (parents.Length > 1 && parents.Any(element => element.Equals(commitBranch.Commit)))
125+
{
126+
ignore.Add(commitBranch);
127+
}
128+
}
129+
}
130+
131+
foreach (var item in commitBranches.Skip(1).Reverse())
132+
{
133+
if (ignore.Contains(item)) continue;
134+
135+
foreach (var commitBranche in commitBranches)
136+
{
137+
if (item.Commit.Equals(commitBranche.Commit)) break;
138+
139+
foreach (var commit in commitBranche.Branch.Commits.Where(element => element.When >= item.Commit.When))
140+
{
141+
if (commit.Equals(item.Commit))
142+
{
143+
commitBranches.Remove(item);
144+
}
145+
}
146+
}
147+
}
148+
149+
foreach (var branchGrouping in commitBranches.GroupBy(element => element.Commit, element => element.Branch))
117150
{
118151
bool referenceMatchFound = false;
119152
var referenceNames = referenceLookup[branchGrouping.Key.Sha].Select(element => element.Name).ToHashSet();
@@ -141,7 +174,7 @@ public IEnumerable<IBranch> GetSourceBranches(IBranch branch, IGitVersionConfigu
141174
/// Find the commit where the given branch was branched from another branch.
142175
/// If there are multiple such commits and branches, tries to guess based on commit histories.
143176
/// </summary>
144-
public BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, IGitVersionConfiguration configuration,
177+
public BranchCommit FindCommitBranchBranchedFrom(IBranch? branch, IGitVersionConfiguration configuration,
145178
params IBranch[] excludedBranches)
146179
{
147180
branch = branch.NotNull();
@@ -170,28 +203,22 @@ public BranchCommit FindCommitBranchWasBranchedFrom(IBranch? branch, IGitVersion
170203
}
171204
}
172205

173-
public IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches)
174-
=> FindCommitBranchesWasBranchedFrom(branch, configuration, (IEnumerable<IBranch>)excludedBranches);
206+
public IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(
207+
IBranch branch, IGitVersionConfiguration configuration, params IBranch[] excludedBranches)
208+
=> FindCommitBranchesBranchedFrom(branch, configuration, (IEnumerable<IBranch>)excludedBranches);
175209

176-
public IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches)
210+
public IEnumerable<BranchCommit> FindCommitBranchesBranchedFrom(
211+
IBranch branch, IGitVersionConfiguration configuration, IEnumerable<IBranch> excludedBranches)
177212
{
178213
using (this.log.IndentLog($"Finding branches source of '{branch}'"))
179214
{
180215
if (branch.Tip == null)
181216
{
182217
this.log.Warning($"{branch} has no tip.");
183-
yield break;
218+
return [];
184219
}
185220

186-
DateTimeOffset? when = null;
187-
var branchCommits = new MergeCommitFinder(this, configuration, excludedBranches, this.log)
188-
.FindMergeCommitsFor(branch).ToList();
189-
foreach (var branchCommit in branchCommits)
190-
{
191-
if (when != null && branchCommit.Commit.When != when) break;
192-
yield return branchCommit;
193-
when = branchCommit.Commit.When;
194-
}
221+
return new MergeCommitFinder(this, configuration, excludedBranches, this.log).FindMergeCommitsFor(branch).ToList();
195222
}
196223
}
197224

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ GitVersion.Common.IRepositoryStore
2525
GitVersion.Common.IRepositoryStore.ExcludingBranches(System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>! branchesToExclude) -> System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>!
2626
GitVersion.Common.IRepositoryStore.FindBranch(GitVersion.Git.ReferenceName! branchName) -> GitVersion.Git.IBranch?
2727
GitVersion.Common.IRepositoryStore.FindBranch(string! branchName) -> GitVersion.Git.IBranch?
28-
GitVersion.Common.IRepositoryStore.FindCommitBranchesWasBranchedFrom(GitVersion.Git.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.Git.IBranch![]! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.Git.BranchCommit>!
29-
GitVersion.Common.IRepositoryStore.FindCommitBranchesWasBranchedFrom(GitVersion.Git.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.Git.BranchCommit>!
30-
GitVersion.Common.IRepositoryStore.FindCommitBranchWasBranchedFrom(GitVersion.Git.IBranch? branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.Git.IBranch![]! excludedBranches) -> GitVersion.Git.BranchCommit
28+
GitVersion.Common.IRepositoryStore.FindCommitBranchBranchedFrom(GitVersion.Git.IBranch? branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.Git.IBranch![]! excludedBranches) -> GitVersion.Git.BranchCommit
29+
GitVersion.Common.IRepositoryStore.FindCommitBranchesBranchedFrom(GitVersion.Git.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, params GitVersion.Git.IBranch![]! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.Git.BranchCommit>!
30+
GitVersion.Common.IRepositoryStore.FindCommitBranchesBranchedFrom(GitVersion.Git.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration, System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>! excludedBranches) -> System.Collections.Generic.IEnumerable<GitVersion.Git.BranchCommit>!
3131
GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.Git.IBranch? branch, GitVersion.Git.IBranch? otherBranch) -> GitVersion.Git.ICommit?
3232
GitVersion.Common.IRepositoryStore.FindMergeBase(GitVersion.Git.ICommit! commit, GitVersion.Git.ICommit! mainlineTip) -> GitVersion.Git.ICommit?
3333
GitVersion.Common.IRepositoryStore.GetBranchesContainingCommit(GitVersion.Git.ICommit! commit, System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>? branches = null, bool onlyTrackedBranches = false) -> System.Collections.Generic.IEnumerable<GitVersion.Git.IBranch!>!
@@ -829,4 +829,4 @@ static readonly GitVersion.Git.BranchCommit.Empty -> GitVersion.Git.BranchCommit
829829
static readonly GitVersion.Helpers.Disposable.Empty -> System.IDisposable!
830830
static readonly GitVersion.SemanticVersion.Empty -> GitVersion.SemanticVersion!
831831
static readonly GitVersion.SemanticVersionBuildMetaData.Empty -> GitVersion.SemanticVersionBuildMetaData!
832-
static readonly GitVersion.SemanticVersionPreReleaseTag.Empty -> GitVersion.SemanticVersionPreReleaseTag!
832+
static readonly GitVersion.SemanticVersionPreReleaseTag.Empty -> GitVersion.SemanticVersionPreReleaseTag!

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/TrunkBasedVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private IReadOnlyDictionary<ICommit, EffectiveBranchConfiguration> GetCommitsWas
230230
var branch = repositoryStore.FindBranch(branchName);
231231
if (branch is null) return result;
232232

233-
var branchCommits = repositoryStore.FindCommitBranchesWasBranchedFrom(
233+
var branchCommits = repositoryStore.FindCommitBranchesBranchedFrom(
234234
branch, Context.Configuration
235235
).ToList();
236236

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/VersionInBranchNameVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public bool TryGetBaseVersion(EffectiveBranchConfiguration configuration, [NotNu
4040
return false;
4141

4242
Lazy<BranchCommit> commitBranchWasBranchedFrom = new(
43-
() => this.repositoryStore.FindCommitBranchWasBranchedFrom(configuration.Branch, Context.Configuration)
43+
() => this.repositoryStore.FindCommitBranchBranchedFrom(configuration.Branch, Context.Configuration)
4444
);
4545
foreach (var branch in new[] { Context.CurrentBranch, configuration.Branch })
4646
{

0 commit comments

Comments
 (0)