Skip to content

Commit 0eb9eb5

Browse files
committed
Make sure that we're not evaluating excluded branches.
This commit removes the excluded branches before we're trying to find branches containing the commit. This will also exclude branches we it's not possible to find a configuration. Fixes #684
1 parent 9da7b3b commit 0eb9eb5

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

src/GitVersionCore/BranchConfigurationCalculator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,33 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
5151
{
5252
excludedBranches = CalculateWhenMultipleParents(repository, currentCommit, ref currentBranch, excludedBranches);
5353
}
54+
5455
if (excludedInheritBranches == null)
5556
{
5657
excludedInheritBranches = repository.Branches.Where(b =>
5758
{
5859
var branchConfig = LookupBranchConfiguration(config, b);
59-
return branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit;
60+
61+
// NOTE: if length is 0 we couldn't find the configuration for the branch e.g. "origin/master"
62+
// NOTE: if the length is greater than 1 we cannot decide which merge strategy to pick
63+
return (branchConfig.Length != 1) || (branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit);
6064
}).ToList();
6165
}
6266
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);
67+
var branchesToEvaluate = repository.Branches.Except(excludedInheritBranches).ToList();
6368

6469
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, excludedInheritBranches.ToArray());
65-
6670
List<Branch> possibleParents;
6771
if (branchPoint == null)
6872
{
69-
possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
73+
possibleParents = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
7074
}
7175
else
7276
{
73-
var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
77+
var branches = branchPoint.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
7478
if (branches.Count > 1)
7579
{
76-
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
80+
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
7781
possibleParents = branches.Except(currentTipBranches).ToList();
7882
}
7983
else

src/GitVersionCore/GitVersionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public GitVersionContext(IRepository repository, Branch currentBranch, Config co
4848

4949
if (currentBranch.IsDetachedHead())
5050
{
51-
CurrentBranch = CurrentCommit.GetBranchesContainingCommit(repository, OnlyEvaluateTrackedBranches).OnlyOrDefault() ?? currentBranch;
51+
CurrentBranch = CurrentCommit.GetBranchesContainingCommit(repository, repository.Branches.ToList(), OnlyEvaluateTrackedBranches).OnlyOrDefault() ?? currentBranch;
5252
}
5353
else
5454
{

src/GitVersionCore/LibGitExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ static bool IsSameBranch(Branch branch, Branch b)
6868
return (b.IsRemote ? b.Name.Replace(b.Remote.Name + "/", string.Empty) : b.Name) != branch.Name;
6969
}
7070

71-
public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, bool onlyTrackedBranches)
71+
public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit, IRepository repository, IList<Branch> branches, bool onlyTrackedBranches)
7272
{
7373
var directBranchHasBeenFound = false;
74-
foreach (var branch in repository.Branches)
74+
foreach (var branch in branches)
7575
{
7676
if (branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
7777
{
@@ -87,7 +87,7 @@ public static IEnumerable<Branch> GetBranchesContainingCommit(this Commit commit
8787
yield break;
8888
}
8989

90-
foreach (var branch in repository.Branches.Where(b => (onlyTrackedBranches && !b.IsTracking)))
90+
foreach (var branch in branches.Where(b => (onlyTrackedBranches && !b.IsTracking)))
9191
{
9292
var commits = repository.Commits.QueryBy(new CommitFilter { Since = branch }).Where(c => c.Sha == commit.Sha);
9393

0 commit comments

Comments
 (0)