@@ -10,7 +10,7 @@ public class BranchConfigurationCalculator
1010 {
1111 public static KeyValuePair < string , BranchConfig > GetBranchConfiguration ( Commit currentCommit , IRepository repository , bool onlyEvaluateTrackedBranches , Config config , Branch currentBranch , IList < Branch > excludedInheritBranches = null )
1212 {
13- var matchingBranches = config . Branches . Where ( b => Regex . IsMatch ( currentBranch . Name , "^" + b . Key , RegexOptions . IgnoreCase ) ) . ToArray ( ) ;
13+ var matchingBranches = LookupBranchConfiguration ( config , currentBranch ) ;
1414
1515 if ( matchingBranches . Length == 0 )
1616 {
@@ -33,6 +33,11 @@ public static KeyValuePair<string, BranchConfig> GetBranchConfiguration(Commit c
3333 throw new Exception ( string . Format ( format , currentBranch . Name , string . Join ( ", " , matchingBranches . Select ( b => b . Key ) ) ) ) ;
3434 }
3535
36+ static KeyValuePair < string , BranchConfig > [ ] LookupBranchConfiguration ( Config config , Branch currentBranch )
37+ {
38+ return config . Branches . Where ( b => Regex . IsMatch ( currentBranch . Name , "^" + b . Key , RegexOptions . IgnoreCase ) ) . ToArray ( ) ;
39+ }
40+
3641 static KeyValuePair < string , BranchConfig > InheritBranchConfiguration ( bool onlyEvaluateTrackedBranches , IRepository repository , Commit currentCommit , Branch currentBranch , KeyValuePair < string , BranchConfig > keyValuePair , BranchConfig branchConfiguration , Config config , IList < Branch > excludedInheritBranches )
3742 {
3843 Logger . WriteInfo ( "Attempting to inherit branch configuration from parent branch" ) ;
@@ -69,34 +74,37 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
6974 }
7075 if ( excludedInheritBranches == null )
7176 {
72- excludedInheritBranches = new List < Branch > ( ) ;
77+ excludedInheritBranches = repository . Branches . Where ( b =>
78+ {
79+ var branchConfig = LookupBranchConfiguration ( config , b ) ;
80+ return branchConfig . Length == 1 && branchConfig [ 0 ] . Value . Increment == IncrementStrategy . Inherit ;
81+ } ) . ToList ( ) ;
7382 }
7483 excludedBranches . ToList ( ) . ForEach ( excludedInheritBranches . Add ) ;
7584
76- var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , onlyEvaluateTrackedBranches , excludedInheritBranches . ToArray ( ) ) ;
85+ var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , excludedInheritBranches . ToArray ( ) ) ;
7786
7887 List < Branch > possibleParents ;
79- if ( branchPoint . Sha == currentCommit . Sha )
88+ if ( branchPoint == null )
8089 {
8190 possibleParents = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
8291 }
8392 else
8493 {
8594 var branches = branchPoint . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
86- var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
87- possibleParents = branches
88- . Except ( currentTipBranches )
89- . ToList ( ) ;
95+ if ( branches . Count > 1 )
96+ {
97+ var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
98+ possibleParents = branches . Except ( currentTipBranches ) . ToList ( ) ;
99+ }
100+ else
101+ {
102+ possibleParents = branches ;
103+ }
90104 }
91105
92106 Logger . WriteInfo ( "Found possible parent branches: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ) ;
93107
94- // If it comes down to master and something, master is always first so we pick other branch
95- if ( possibleParents . Count == 2 && possibleParents . Any ( p => p . Name == "master" ) )
96- {
97- possibleParents . Remove ( possibleParents . Single ( p => p . Name == "master" ) ) ;
98- }
99-
100108 if ( possibleParents . Count == 1 )
101109 {
102110 var branchConfig = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , possibleParents [ 0 ] , excludedInheritBranches ) . Value ;
@@ -117,8 +125,8 @@ static KeyValuePair<string, BranchConfig> InheritBranchConfiguration(bool onlyEv
117125 else
118126 errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ;
119127
120- var hasDevelop = repository . Branches [ " develop"] != null ;
121- var branchName = hasDevelop ? "develop" : "master" ;
128+ var developBranch = repository . Branches . FirstOrDefault ( b => Regex . IsMatch ( b . Name , "^ develop", RegexOptions . IgnoreCase ) ) ;
129+ var branchName = developBranch != null ? developBranch . Name : "master" ;
122130
123131 Logger . WriteWarning ( errorMessage + Environment . NewLine + Environment . NewLine + "Falling back to " + branchName + " branch config" ) ;
124132 var value = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , repository . Branches [ branchName ] ) . Value ;
0 commit comments