@@ -422,7 +422,7 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
422
422
using ( var ourHandle = Proxy . git_object_peel ( repo . Handle , one . Id , GitObjectType . Tree , true ) )
423
423
using ( var theirHandle = Proxy . git_object_peel ( repo . Handle , another . Id , GitObjectType . Tree , true ) )
424
424
{
425
- var ancestorCommit = repo . Commits . FindMergeBase ( one , another ) ;
425
+ var ancestorCommit = FindMergeBase ( one , another ) ;
426
426
427
427
var ancestorHandle = ancestorCommit != null
428
428
? Proxy . git_object_peel ( repo . Handle , ancestorCommit . Id , GitObjectType . Tree , false )
@@ -435,5 +435,63 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
435
435
}
436
436
}
437
437
}
438
+
439
+ /// <summary>
440
+ /// Find the best possible merge base given two <see cref="Commit"/>s.
441
+ /// </summary>
442
+ /// <param name="first">The first <see cref="Commit"/>.</param>
443
+ /// <param name="second">The second <see cref="Commit"/>.</param>
444
+ /// <returns>The merge base or null if none found.</returns>
445
+ public virtual Commit FindMergeBase ( Commit first , Commit second )
446
+ {
447
+ Ensure . ArgumentNotNull ( first , "first" ) ;
448
+ Ensure . ArgumentNotNull ( second , "second" ) ;
449
+
450
+ return FindMergeBase ( new [ ] { first , second } , MergeBaseFindingStrategy . Standard ) ;
451
+ }
452
+
453
+ /// <summary>
454
+ /// Find the best possible merge base given two or more <see cref="Commit"/> according to the <see cref="MergeBaseFindingStrategy"/>.
455
+ /// </summary>
456
+ /// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
457
+ /// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
458
+ /// <returns>The merge base or null if none found.</returns>
459
+ public virtual Commit FindMergeBase ( IEnumerable < Commit > commits , MergeBaseFindingStrategy strategy )
460
+ {
461
+ Ensure . ArgumentNotNull ( commits , "commits" ) ;
462
+
463
+ ObjectId id ;
464
+ List < GitOid > ids = new List < GitOid > ( 8 ) ;
465
+ int count = 0 ;
466
+
467
+ foreach ( var commit in commits )
468
+ {
469
+ if ( commit == null )
470
+ {
471
+ throw new ArgumentException ( "Enumerable contains null at position: " + count . ToString ( CultureInfo . InvariantCulture ) , "commits" ) ;
472
+ }
473
+ ids . Add ( commit . Id . Oid ) ;
474
+ count ++ ;
475
+ }
476
+
477
+ if ( count < 2 )
478
+ {
479
+ throw new ArgumentException ( "The enumerable must contains at least two commits." , "commits" ) ;
480
+ }
481
+
482
+ switch ( strategy )
483
+ {
484
+ case MergeBaseFindingStrategy . Standard :
485
+ id = Proxy . git_merge_base_many ( repo . Handle , ids . ToArray ( ) ) ;
486
+ break ;
487
+ case MergeBaseFindingStrategy . Octopus :
488
+ id = Proxy . git_merge_base_octopus ( repo . Handle , ids . ToArray ( ) ) ;
489
+ break ;
490
+ default :
491
+ throw new ArgumentException ( "" , "strategy" ) ;
492
+ }
493
+
494
+ return id == null ? null : repo . Lookup < Commit > ( id ) ;
495
+ }
438
496
}
439
497
}
0 commit comments