@@ -510,7 +510,7 @@ def index(self):
510
510
#
511
511
# Merging
512
512
#
513
- def merge_commits (self , ours , theirs ):
513
+ def merge_commits (self , ours , theirs , favor = 'normal' ):
514
514
"""Merge two arbitrary commits
515
515
516
516
Arguments:
@@ -519,16 +519,37 @@ def merge_commits(self, ours, theirs):
519
519
The commit to take as "ours" or base.
520
520
theirs
521
521
The commit which will be merged into "ours"
522
+ favor
523
+ How to deal with file-level conflicts. Can be one of
522
524
523
- Both can be any object which peels to a commit or the id
525
+ * normal (default). Conflicts will be preserved.
526
+ * ours. The "ours" side of the conflict region is used.
527
+ * theirs. The "theirs" side of the conflict region is used.
528
+ * union. Unique lines from each side will be used.
529
+
530
+ for all but NORMAL, the index will not record a conflict.
531
+
532
+ Both "ours" and "theirs" can be any object which peels to a commit or the id
524
533
(string or Oid) of an object which peels to a commit.
525
534
526
535
Returns an index with the result of the merge
527
536
528
537
"""
538
+ def favor_to_enum (favor ):
539
+ if favor == 'normal' :
540
+ return C .GIT_MERGE_FILE_FAVOR_NORMAL
541
+ elif favor == 'ours' :
542
+ return C .GIT_MERGE_FILE_FAVOR_OURS
543
+ elif favor == 'theirs' :
544
+ return C .GIT_MERGE_FILE_FAVOR_THEIRS
545
+ elif favor == 'union' :
546
+ return C .GIT_MERGE_FILE_FAVOR_UNION
547
+ else :
548
+ return None
529
549
530
550
ours_ptr = ffi .new ('git_commit **' )
531
551
theirs_ptr = ffi .new ('git_commit **' )
552
+ opts = ffi .new ('git_merge_options *' )
532
553
cindex = ffi .new ('git_index **' )
533
554
534
555
if is_string (ours ) or isinstance (ours , Oid ):
@@ -539,10 +560,19 @@ def merge_commits(self, ours, theirs):
539
560
ours = ours .peel (Commit )
540
561
theirs = theirs .peel (Commit )
541
562
563
+ err = C .git_merge_init_options (opts , C .GIT_MERGE_OPTIONS_VERSION )
564
+ check_error (err )
565
+
566
+ favor_val = favor_to_enum (favor )
567
+ if favor_val is None :
568
+ raise ValueError ("unkown favor value %s" % favor )
569
+
570
+ opts .file_favor = favor_val
571
+
542
572
ffi .buffer (ours_ptr )[:] = ours ._pointer [:]
543
573
ffi .buffer (theirs_ptr )[:] = theirs ._pointer [:]
544
574
545
- err = C .git_merge_commits (cindex , self ._repo , ours_ptr [0 ], theirs_ptr [0 ], ffi . NULL )
575
+ err = C .git_merge_commits (cindex , self ._repo , ours_ptr [0 ], theirs_ptr [0 ], opts )
546
576
check_error (err )
547
577
548
578
return Index .from_c (self , cindex )
0 commit comments