@@ -40,103 +40,105 @@ static KeyValuePair<string, BranchConfig>[] LookupBranchConfiguration(Config con
4040
4141 static KeyValuePair < string , BranchConfig > InheritBranchConfiguration ( bool onlyEvaluateTrackedBranches , IRepository repository , Commit currentCommit , Branch currentBranch , KeyValuePair < string , BranchConfig > keyValuePair , BranchConfig branchConfiguration , Config config , IList < Branch > excludedInheritBranches )
4242 {
43- Logger . WriteInfo ( "Attempting to inherit branch configuration from parent branch" ) ;
44- var excludedBranches = new [ ] { currentBranch } ;
45- // Check if we are a merge commit. If so likely we are a pull request
46- var parentCount = currentCommit . Parents . Count ( ) ;
47- if ( parentCount == 2 )
43+ using ( Logger . IndentLog ( "Attempting to inherit branch configuration from parent branch" ) )
4844 {
49- var parents = currentCommit . Parents . ToArray ( ) ;
50- var branch = repository . Branches . SingleOrDefault ( b => ! b . IsRemote && b . Tip == parents [ 1 ] ) ;
51- if ( branch != null )
45+ var excludedBranches = new [ ] { currentBranch } ;
46+ // Check if we are a merge commit. If so likely we are a pull request
47+ var parentCount = currentCommit . Parents . Count ( ) ;
48+ if ( parentCount == 2 )
5249 {
53- excludedBranches = new [ ]
50+ var parents = currentCommit . Parents . ToArray ( ) ;
51+ var branch = repository . Branches . SingleOrDefault ( b => ! b . IsRemote && b . Tip == parents [ 1 ] ) ;
52+ if ( branch != null )
5453 {
54+ excludedBranches = new [ ]
55+ {
5556 currentBranch ,
5657 branch
5758 } ;
58- currentBranch = branch ;
59+ currentBranch = branch ;
60+ }
61+ else
62+ {
63+ var possibleTargetBranches = repository . Branches . Where ( b => ! b . IsRemote && b . Tip == parents [ 0 ] ) . ToList ( ) ;
64+ if ( possibleTargetBranches . Count ( ) > 1 )
65+ {
66+ currentBranch = possibleTargetBranches . FirstOrDefault ( b => b . Name == "master" ) ?? possibleTargetBranches . First ( ) ;
67+ }
68+ else
69+ {
70+ currentBranch = possibleTargetBranches . FirstOrDefault ( ) ?? currentBranch ;
71+ }
72+ }
73+
74+ Logger . WriteInfo ( "HEAD is merge commit, this is likely a pull request using " + currentBranch . Name + " as base" ) ;
75+ }
76+ if ( excludedInheritBranches == null )
77+ {
78+ excludedInheritBranches = repository . Branches . Where ( b =>
79+ {
80+ var branchConfig = LookupBranchConfiguration ( config , b ) ;
81+ return branchConfig . Length == 1 && branchConfig [ 0 ] . Value . Increment == IncrementStrategy . Inherit ;
82+ } ) . ToList ( ) ;
83+ }
84+ excludedBranches . ToList ( ) . ForEach ( excludedInheritBranches . Add ) ;
85+
86+ var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , excludedInheritBranches . ToArray ( ) ) ;
87+
88+ List < Branch > possibleParents ;
89+ if ( branchPoint == null )
90+ {
91+ possibleParents = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
5992 }
6093 else
6194 {
62- var possibleTargetBranches = repository . Branches . Where ( b => ! b . IsRemote && b . Tip == parents [ 0 ] ) . ToList ( ) ;
63- if ( possibleTargetBranches . Count ( ) > 1 )
95+ var branches = branchPoint . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
96+ if ( branches . Count > 1 )
6497 {
65- currentBranch = possibleTargetBranches . FirstOrDefault ( b => b . Name == "master" ) ?? possibleTargetBranches . First ( ) ;
98+ var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
99+ possibleParents = branches . Except ( currentTipBranches ) . ToList ( ) ;
66100 }
67101 else
68102 {
69- currentBranch = possibleTargetBranches . FirstOrDefault ( ) ?? currentBranch ;
103+ possibleParents = branches ;
70104 }
71105 }
72106
73- Logger . WriteInfo ( "HEAD is merge commit, this is likely a pull request using " + currentBranch . Name + " as base" ) ;
74- }
75- if ( excludedInheritBranches == null )
76- {
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 ( ) ;
82- }
83- excludedBranches . ToList ( ) . ForEach ( excludedInheritBranches . Add ) ;
84-
85- var branchPoint = currentBranch . FindCommitBranchWasBranchedFrom ( repository , excludedInheritBranches . ToArray ( ) ) ;
107+ Logger . WriteInfo ( "Found possible parent branches: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ) ;
86108
87- List < Branch > possibleParents ;
88- if ( branchPoint == null )
89- {
90- possibleParents = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
91- }
92- else
93- {
94- var branches = branchPoint . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
95- if ( branches . Count > 1 )
109+ if ( possibleParents . Count == 1 )
96110 {
97- var currentTipBranches = currentCommit . GetBranchesContainingCommit ( repository , true ) . Except ( excludedInheritBranches ) . ToList ( ) ;
98- possibleParents = branches . Except ( currentTipBranches ) . ToList ( ) ;
111+ var branchConfig = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , possibleParents [ 0 ] , excludedInheritBranches ) . Value ;
112+ return new KeyValuePair < string , BranchConfig > (
113+ keyValuePair . Key ,
114+ new BranchConfig ( branchConfiguration )
115+ {
116+ Increment = branchConfig . Increment ,
117+ PreventIncrementOfMergedBranchVersion = branchConfig . PreventIncrementOfMergedBranchVersion
118+ } ) ;
99119 }
120+
121+ // If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
122+ // if develop exists and master if not
123+ string errorMessage ;
124+ if ( possibleParents . Count == 0 )
125+ errorMessage = "Failed to inherit Increment branch configuration, no branches found." ;
100126 else
101- {
102- possibleParents = branches ;
103- }
104- }
127+ errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ;
105128
106- Logger . WriteInfo ( "Found possible parent branches: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ) ;
129+ var developBranch = repository . Branches . FirstOrDefault ( b => Regex . IsMatch ( b . Name , "^develop" , RegexOptions . IgnoreCase ) ) ;
130+ var branchName = developBranch != null ? developBranch . Name : "master" ;
107131
108- if ( possibleParents . Count == 1 )
109- {
110- var branchConfig = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , possibleParents [ 0 ] , excludedInheritBranches ) . Value ;
132+ Logger . WriteWarning ( errorMessage + Environment . NewLine + Environment . NewLine + "Falling back to " + branchName + " branch config" ) ;
133+ var value = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , repository . Branches [ branchName ] ) . Value ;
111134 return new KeyValuePair < string , BranchConfig > (
112135 keyValuePair . Key ,
113136 new BranchConfig ( branchConfiguration )
114137 {
115- Increment = branchConfig . Increment ,
116- PreventIncrementOfMergedBranchVersion = branchConfig . PreventIncrementOfMergedBranchVersion
138+ Increment = value . Increment ,
139+ PreventIncrementOfMergedBranchVersion = value . PreventIncrementOfMergedBranchVersion
117140 } ) ;
118141 }
119-
120- // If we fail to inherit it is probably because the branch has been merged and we can't do much. So we will fall back to develop's config
121- // if develop exists and master if not
122- string errorMessage ;
123- if ( possibleParents . Count == 0 )
124- errorMessage = "Failed to inherit Increment branch configuration, no branches found." ;
125- else
126- errorMessage = "Failed to inherit Increment branch configuration, ended up with: " + string . Join ( ", " , possibleParents . Select ( p => p . Name ) ) ;
127-
128- var developBranch = repository . Branches . FirstOrDefault ( b => Regex . IsMatch ( b . Name , "^develop" , RegexOptions . IgnoreCase ) ) ;
129- var branchName = developBranch != null ? developBranch . Name : "master" ;
130-
131- Logger . WriteWarning ( errorMessage + Environment . NewLine + Environment . NewLine + "Falling back to " + branchName + " branch config" ) ;
132- var value = GetBranchConfiguration ( currentCommit , repository , onlyEvaluateTrackedBranches , config , repository . Branches [ branchName ] ) . Value ;
133- return new KeyValuePair < string , BranchConfig > (
134- keyValuePair . Key ,
135- new BranchConfig ( branchConfiguration )
136- {
137- Increment = value . Increment ,
138- PreventIncrementOfMergedBranchVersion = value . PreventIncrementOfMergedBranchVersion
139- } ) ;
140142 }
141143 }
142144}
0 commit comments