Skip to content

Commit 33f8921

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 d860793 commit 33f8921

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
@@ -65,29 +65,33 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
6565
{
6666
excludedBranches = CalculateWhenMultipleParents(repository, currentCommit, ref currentBranch, excludedBranches);
6767
}
68+
6869
if (excludedInheritBranches == null)
6970
{
7071
excludedInheritBranches = repository.Branches.Where(b =>
7172
{
7273
var branchConfig = LookupBranchConfiguration(config, b);
73-
return branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit;
74+
75+
// NOTE: if length is 0 we couldn't find the configuration for the branch e.g. "origin/master"
76+
// NOTE: if the length is greater than 1 we cannot decide which merge strategy to pick
77+
return (branchConfig.Length != 1) || (branchConfig.Length == 1 && branchConfig[0].Value.Increment == IncrementStrategy.Inherit);
7478
}).ToList();
7579
}
7680
excludedBranches.ToList().ForEach(excludedInheritBranches.Add);
81+
var branchesToEvaluate = repository.Branches.Except(excludedInheritBranches).ToList();
7782

7883
var branchPoint = currentBranch.FindCommitBranchWasBranchedFrom(repository, excludedInheritBranches.ToArray());
79-
8084
List<Branch> possibleParents;
8185
if (branchPoint == null)
8286
{
83-
possibleParents = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
87+
possibleParents = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
8488
}
8589
else
8690
{
87-
var branches = branchPoint.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
91+
var branches = branchPoint.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
8892
if (branches.Count > 1)
8993
{
90-
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, true).Except(excludedInheritBranches).ToList();
94+
var currentTipBranches = currentCommit.GetBranchesContainingCommit(repository, branchesToEvaluate, true).ToList();
9195
possibleParents = branches.Except(currentTipBranches).ToList();
9296
}
9397
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
@@ -92,15 +92,15 @@ static bool IsSameBranch(Branch branch, Branch b)
9292
return (b.IsRemote ? b.Name.Replace(b.Remote.Name + "/", string.Empty) : b.Name) != branch.Name;
9393
}
9494

95-
public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Commit commit, IRepository repository, bool onlyTrackedBranches)
95+
public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Commit commit, IRepository repository, IList<Branch> branches, bool onlyTrackedBranches)
9696
{
9797
if (commit == null)
9898
{
9999
throw new ArgumentNullException("commit");
100100
}
101101

102102
var directBranchHasBeenFound = false;
103-
foreach (var branch in repository.Branches)
103+
foreach (var branch in branches)
104104
{
105105
if (branch.Tip != null && branch.Tip.Sha != commit.Sha || (onlyTrackedBranches && !branch.IsTracking))
106106
{
@@ -116,7 +116,7 @@ public static IEnumerable<Branch> GetBranchesContainingCommit([NotNull] this Com
116116
yield break;
117117
}
118118

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

0 commit comments

Comments
 (0)