@@ -166,27 +166,44 @@ static Commit GetMainlineTip(GitVersionContext context)
166
166
{
167
167
var mainlineBranchConfigs = context . FullConfiguration . Branches . Where ( b => b . Value . IsMainline == true ) . ToList ( ) ;
168
168
var seenMainlineTips = new List < string > ( ) ;
169
- var mainlineBranches = context . Repository . Branches . Where ( b =>
170
- {
171
- return mainlineBranchConfigs . Any ( c => Regex . IsMatch ( b . FriendlyName , c . Key ) ) ;
172
- } ) . Where ( b =>
173
- {
174
- if ( seenMainlineTips . Contains ( b . Tip . Sha ) )
169
+ var mainlineBranches = context . Repository . Branches
170
+ . Where ( b =>
175
171
{
176
- Logger . WriteInfo ( "Multiple possible mainlines pointing at the same commit, dropping " + b . FriendlyName ) ;
177
- return false ;
178
- }
179
- seenMainlineTips . Add ( b . Tip . Sha ) ;
180
- return true ;
181
- } ) . ToDictionary ( b => context . Repository . ObjectDatabase . FindMergeBase ( b . Tip , context . CurrentCommit ) . Sha , b => b ) ;
182
- Logger . WriteInfo ( "Found possible mainline branches: " + string . Join ( ", " , mainlineBranches . Values . Select ( b => b . FriendlyName ) ) ) ;
172
+ return mainlineBranchConfigs . Any ( c => Regex . IsMatch ( b . FriendlyName , c . Key ) ) ;
173
+ } )
174
+ . Where ( b =>
175
+ {
176
+ if ( seenMainlineTips . Contains ( b . Tip . Sha ) )
177
+ {
178
+ Logger . WriteInfo ( "Multiple possible mainlines pointing at the same commit, dropping " + b . FriendlyName ) ;
179
+ return false ;
180
+ }
181
+ seenMainlineTips . Add ( b . Tip . Sha ) ;
182
+ return true ;
183
+ } )
184
+ . GroupBy ( b => context . Repository . ObjectDatabase . FindMergeBase ( b . Tip , context . CurrentCommit ) . Sha )
185
+ . ToDictionary ( b => b . Key , b => b . ToList ( ) ) ;
186
+
187
+ var allMainlines = mainlineBranches . Values . SelectMany ( branches => branches . Select ( b => b . FriendlyName ) ) ;
188
+ Logger . WriteInfo ( "Found possible mainline branches: " + string . Join ( ", " , allMainlines ) ) ;
183
189
184
190
// Find closest mainline branch
185
191
var firstMatchingCommit = context . CurrentBranch . Commits . First ( c => mainlineBranches . ContainsKey ( c . Sha ) ) ;
186
- var mainlineBranch = mainlineBranches [ firstMatchingCommit . Sha ] ;
187
- Logger . WriteInfo ( "Mainline for current branch is " + mainlineBranch . FriendlyName ) ;
192
+ var possibleMainlineBranches = mainlineBranches [ firstMatchingCommit . Sha ] ;
193
+
194
+ if ( possibleMainlineBranches . Count == 1 )
195
+ {
196
+ var mainlineBranch = possibleMainlineBranches [ 0 ] ;
197
+ Logger . WriteInfo ( "Mainline for current branch is " + mainlineBranch . FriendlyName ) ;
198
+ return mainlineBranch . Tip ;
199
+ }
188
200
189
- return mainlineBranch . Tip ;
201
+ var chosenMainline = possibleMainlineBranches [ 0 ] ;
202
+ Logger . WriteInfo ( string . Format (
203
+ "Multiple mainlines ({0}) have the same merge base for the current branch, choosing {1} because we found that branch first..." ,
204
+ string . Join ( ", " , possibleMainlineBranches . Select ( b => b . FriendlyName ) ) ,
205
+ chosenMainline . FriendlyName ) ) ;
206
+ return chosenMainline . Tip ;
190
207
}
191
208
192
209
private static SemanticVersion IncrementForEachCommit ( GitVersionContext context , List < Commit > directCommits , SemanticVersion mainlineVersion )
0 commit comments