Skip to content

Commit 6a5e6ae

Browse files
committed
Fix unit tests
1 parent 4ec6962 commit 6a5e6ae

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public void EnsurePullRequestWithIncrementMajorOnMainAndMinorOnFeatureBranch()
3838
fixture.BranchTo("pull/2/merge");
3939
fixture.MergeNoFF("feature/foo");
4040

41-
// ❌ expected: "2.0.0-PullRequest2.2"
42-
fixture.AssertFullSemver("1.1.0-PullRequest2.2", configuration);
41+
// ✅ succeeds as expected
42+
fixture.AssertFullSemver("2.0.0-PullRequest2.2", configuration);
4343

4444
fixture.Checkout("main");
4545
fixture.Remove("pull/2/merge");

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 38 additions & 13 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 = FindCommitBranchesWasBranchedFrom(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();
@@ -180,18 +213,10 @@ public IEnumerable<BranchCommit> FindCommitBranchesWasBranchedFrom(IBranch branc
180213
if (branch.Tip == null)
181214
{
182215
this.log.Warning($"{branch} has no tip.");
183-
yield break;
216+
return Enumerable.Empty<BranchCommit>();
184217
}
185218

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-
}
219+
return new MergeCommitFinder(this, configuration, excludedBranches, this.log).FindMergeCommitsFor(branch).ToList();
195220
}
196221
}
197222

0 commit comments

Comments
 (0)