2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
12 #include "repository.h"
14 #include "commit_list.h"
21 #include "diff_generate.h"
22 #include "diff_tform.h"
31 #include "annotated_commit.h"
34 #include "merge_driver.h"
38 #include "git2/types.h"
39 #include "git2/repository.h"
40 #include "git2/object.h"
41 #include "git2/commit.h"
42 #include "git2/merge.h"
43 #include "git2/refs.h"
44 #include "git2/reset.h"
45 #include "git2/checkout.h"
46 #include "git2/signature.h"
47 #include "git2/config.h"
48 #include "git2/tree.h"
49 #include "git2/oidarray.h"
50 #include "git2/annotated_commit.h"
51 #include "git2/sys/index.h"
52 #include "git2/sys/hashsig.h"
54 #define GIT_MERGE_INDEX_ENTRY_EXISTS(X) ((X).mode != 0)
55 #define GIT_MERGE_INDEX_ENTRY_ISFILE(X) S_ISREG((X).mode)
59 TREE_IDX_ANCESTOR
= 0,
64 /* Tracks D/F conflicts */
65 struct merge_diff_df_data
{
67 const char *prev_path
;
68 git_merge_diff
*prev_conflict
;
72 * This acts as a negative cache entry marker. In case we've tried to calculate
73 * similarity metrics for a given blob already but `git_hashsig` determined
74 * that it's too small in order to have a meaningful hash signature, we will
75 * insert the address of this marker instead of `NULL`. Like this, we can
76 * easily check whether we have checked a gien entry already and skip doing the
77 * calculation again and again.
79 static int cache_invalid_marker
;
81 /* Merge base computation */
83 static int merge_bases_many(git_commit_list
**out
, git_revwalk
**walk_out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
85 git_revwalk
*walk
= NULL
;
87 git_commit_list
*result
= NULL
;
88 git_commit_list_node
*commit
;
93 git_error_set(GIT_ERROR_INVALID
, "at least two commits are required to find an ancestor");
97 if (git_vector_init(&list
, length
- 1, NULL
) < 0)
100 if (git_revwalk_new(&walk
, repo
) < 0)
103 for (i
= 1; i
< length
; i
++) {
104 commit
= git_revwalk__commit_lookup(walk
, &input_array
[i
]);
108 git_vector_insert(&list
, commit
);
111 commit
= git_revwalk__commit_lookup(walk
, &input_array
[0]);
115 if (git_merge__bases_many(&result
, walk
, commit
, &list
) < 0)
119 git_error_set(GIT_ERROR_MERGE
, "no merge base found");
120 error
= GIT_ENOTFOUND
;
127 git_vector_free(&list
);
131 git_vector_free(&list
);
132 git_revwalk_free(walk
);
136 int git_merge_base_many(git_oid
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
139 git_commit_list
*result
= NULL
;
142 assert(out
&& repo
&& input_array
);
144 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
147 git_oid_cpy(out
, &result
->item
->oid
);
149 git_commit_list_free(&result
);
150 git_revwalk_free(walk
);
155 int git_merge_bases_many(git_oidarray
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
158 git_commit_list
*list
, *result
= NULL
;
160 git_array_oid_t array
;
162 assert(out
&& repo
&& input_array
);
164 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
167 git_array_init(array
);
171 git_oid
*id
= git_array_alloc(array
);
177 git_oid_cpy(id
, &list
->item
->oid
);
181 git_oidarray__from_array(out
, &array
);
184 git_commit_list_free(&result
);
185 git_revwalk_free(walk
);
190 int git_merge_base_octopus(git_oid
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
196 assert(out
&& repo
&& input_array
);
199 git_error_set(GIT_ERROR_INVALID
, "at least two commits are required to find an ancestor");
203 result
= input_array
[0];
204 for (i
= 1; i
< length
; i
++) {
205 error
= git_merge_base(&result
, repo
, &result
, &input_array
[i
]);
215 static int merge_bases(git_commit_list
**out
, git_revwalk
**walk_out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
219 git_commit_list
*result
= NULL
;
220 git_commit_list_node
*commit
;
223 if (git_revwalk_new(&walk
, repo
) < 0)
226 commit
= git_revwalk__commit_lookup(walk
, two
);
230 /* This is just one value, so we can do it on the stack */
231 memset(&list
, 0x0, sizeof(git_vector
));
232 contents
[0] = commit
;
234 list
.contents
= contents
;
236 commit
= git_revwalk__commit_lookup(walk
, one
);
240 if (git_merge__bases_many(&result
, walk
, commit
, &list
) < 0)
244 git_revwalk_free(walk
);
245 git_error_set(GIT_ERROR_MERGE
, "no merge base found");
246 return GIT_ENOTFOUND
;
255 git_revwalk_free(walk
);
260 int git_merge_base(git_oid
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
264 git_commit_list
*result
;
266 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
269 git_oid_cpy(out
, &result
->item
->oid
);
270 git_commit_list_free(&result
);
271 git_revwalk_free(walk
);
276 int git_merge_bases(git_oidarray
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
280 git_commit_list
*result
, *list
;
281 git_array_oid_t array
;
283 git_array_init(array
);
285 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
290 git_oid
*id
= git_array_alloc(array
);
294 git_oid_cpy(id
, &list
->item
->oid
);
298 git_oidarray__from_array(out
, &array
);
299 git_commit_list_free(&result
);
300 git_revwalk_free(walk
);
305 git_commit_list_free(&result
);
306 git_revwalk_free(walk
);
310 static int interesting(git_pqueue
*list
)
314 for (i
= 0; i
< git_pqueue_size(list
); i
++) {
315 git_commit_list_node
*commit
= git_pqueue_get(list
, i
);
316 if ((commit
->flags
& STALE
) == 0)
323 static int clear_commit_marks_1(git_commit_list
**plist
,
324 git_commit_list_node
*commit
, unsigned int mark
)
329 if (!(mark
& commit
->flags
))
332 commit
->flags
&= ~mark
;
334 for (i
= 1; i
< commit
->out_degree
; i
++) {
335 git_commit_list_node
*p
= commit
->parents
[i
];
336 if (git_commit_list_insert(p
, plist
) == NULL
)
340 commit
= commit
->out_degree
? commit
->parents
[0] : NULL
;
346 static int clear_commit_marks_many(git_vector
*commits
, unsigned int mark
)
348 git_commit_list
*list
= NULL
;
349 git_commit_list_node
*c
;
352 git_vector_foreach(commits
, i
, c
) {
353 if (git_commit_list_insert(c
, &list
) == NULL
)
358 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
363 static int clear_commit_marks(git_commit_list_node
*commit
, unsigned int mark
)
365 git_commit_list
*list
= NULL
;
366 if (git_commit_list_insert(commit
, &list
) == NULL
)
369 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
374 static int paint_down_to_common(
375 git_commit_list
**out
, git_revwalk
*walk
, git_commit_list_node
*one
, git_vector
*twos
)
378 git_commit_list
*result
= NULL
;
379 git_commit_list_node
*two
;
384 if (git_pqueue_init(&list
, 0, twos
->length
* 2, git_commit_list_time_cmp
) < 0)
387 one
->flags
|= PARENT1
;
388 if (git_pqueue_insert(&list
, one
) < 0)
391 git_vector_foreach(twos
, i
, two
) {
392 if (git_commit_list_parse(walk
, two
) < 0)
395 two
->flags
|= PARENT2
;
397 if (git_pqueue_insert(&list
, two
) < 0)
401 /* as long as there are non-STALE commits */
402 while (interesting(&list
)) {
403 git_commit_list_node
*commit
= git_pqueue_pop(&list
);
409 flags
= commit
->flags
& (PARENT1
| PARENT2
| STALE
);
410 if (flags
== (PARENT1
| PARENT2
)) {
411 if (!(commit
->flags
& RESULT
)) {
412 commit
->flags
|= RESULT
;
413 if (git_commit_list_insert(commit
, &result
) == NULL
)
416 /* we mark the parents of a merge stale */
420 for (i
= 0; i
< commit
->out_degree
; i
++) {
421 git_commit_list_node
*p
= commit
->parents
[i
];
422 if ((p
->flags
& flags
) == flags
)
425 if ((error
= git_commit_list_parse(walk
, p
)) < 0)
429 if (git_pqueue_insert(&list
, p
) < 0)
434 git_pqueue_free(&list
);
439 static int remove_redundant(git_revwalk
*walk
, git_vector
*commits
)
441 git_vector work
= GIT_VECTOR_INIT
;
442 unsigned char *redundant
;
443 unsigned int *filled_index
;
447 redundant
= git__calloc(commits
->length
, 1);
448 GIT_ERROR_CHECK_ALLOC(redundant
);
449 filled_index
= git__calloc((commits
->length
- 1), sizeof(unsigned int));
450 GIT_ERROR_CHECK_ALLOC(filled_index
);
452 for (i
= 0; i
< commits
->length
; ++i
) {
453 if ((error
= git_commit_list_parse(walk
, commits
->contents
[i
])) < 0)
457 for (i
= 0; i
< commits
->length
; ++i
) {
458 git_commit_list
*common
= NULL
;
459 git_commit_list_node
*commit
= commits
->contents
[i
];
464 git_vector_clear(&work
);
466 for (j
= 0; j
< commits
->length
; j
++) {
467 if (i
== j
|| redundant
[j
])
470 filled_index
[work
.length
] = j
;
471 if ((error
= git_vector_insert(&work
, commits
->contents
[j
])) < 0)
475 error
= paint_down_to_common(&common
, walk
, commit
, &work
);
479 if (commit
->flags
& PARENT2
)
482 for (j
= 0; j
< work
.length
; j
++) {
483 git_commit_list_node
*w
= work
.contents
[j
];
484 if (w
->flags
& PARENT1
)
485 redundant
[filled_index
[j
]] = 1;
488 git_commit_list_free(&common
);
490 if ((error
= clear_commit_marks(commit
, ALL_FLAGS
)) < 0 ||
491 (error
= clear_commit_marks_many(&work
, ALL_FLAGS
)) < 0)
495 for (i
= 0; i
< commits
->length
; ++i
) {
497 commits
->contents
[i
] = NULL
;
501 git__free(redundant
);
502 git__free(filled_index
);
503 git_vector_free(&work
);
507 int git_merge__bases_many(git_commit_list
**out
, git_revwalk
*walk
, git_commit_list_node
*one
, git_vector
*twos
)
511 git_commit_list_node
*two
;
512 git_commit_list
*result
= NULL
, *tmp
= NULL
;
514 /* If there's only the one commit, there can be no merge bases */
515 if (twos
->length
== 0) {
520 /* if the commit is repeated, we have a our merge base already */
521 git_vector_foreach(twos
, i
, two
) {
523 return git_commit_list_insert(one
, out
) ? 0 : -1;
526 if (git_commit_list_parse(walk
, one
) < 0)
529 error
= paint_down_to_common(&result
, walk
, one
, twos
);
533 /* filter out any stale commits in the results */
538 git_commit_list_node
*c
= git_commit_list_pop(&tmp
);
539 if (!(c
->flags
& STALE
))
540 if (git_commit_list_insert_by_date(c
, &result
) == NULL
)
545 * more than one merge base -- see if there are redundant merge
546 * bases and remove them
548 if (result
&& result
->next
) {
549 git_vector redundant
= GIT_VECTOR_INIT
;
552 git_vector_insert(&redundant
, git_commit_list_pop(&result
));
554 if ((error
= clear_commit_marks(one
, ALL_FLAGS
)) < 0 ||
555 (error
= clear_commit_marks_many(twos
, ALL_FLAGS
)) < 0 ||
556 (error
= remove_redundant(walk
, &redundant
)) < 0) {
557 git_vector_free(&redundant
);
561 git_vector_foreach(&redundant
, i
, two
) {
563 git_commit_list_insert_by_date(two
, &result
);
566 git_vector_free(&redundant
);
573 int git_repository_mergehead_foreach(
574 git_repository
*repo
,
575 git_repository_mergehead_foreach_cb cb
,
578 git_buf merge_head_path
= GIT_BUF_INIT
, merge_head_file
= GIT_BUF_INIT
;
586 if ((error
= git_buf_joinpath(&merge_head_path
, repo
->gitdir
,
587 GIT_MERGE_HEAD_FILE
)) < 0)
590 if ((error
= git_futils_readbuffer(&merge_head_file
,
591 git_buf_cstr(&merge_head_path
))) < 0)
594 buffer
= merge_head_file
.ptr
;
596 while ((line
= git__strsep(&buffer
, "\n")) != NULL
) {
597 if (strlen(line
) != GIT_OID_HEXSZ
) {
598 git_error_set(GIT_ERROR_INVALID
, "unable to parse OID - invalid length");
603 if ((error
= git_oid_fromstr(&oid
, line
)) < 0)
606 if ((error
= cb(&oid
, payload
)) != 0) {
607 git_error_set_after_callback(error
);
615 git_error_set(GIT_ERROR_MERGE
, "no EOL at line %"PRIuZ
, line_num
);
621 git_buf_dispose(&merge_head_path
);
622 git_buf_dispose(&merge_head_file
);
627 GIT_INLINE(int) index_entry_cmp(const git_index_entry
*a
, const git_index_entry
*b
)
632 return (b
->path
== NULL
) ? 0 : 1;
634 if ((value
= a
->mode
- b
->mode
) == 0 &&
635 (value
= git_oid__cmp(&a
->id
, &b
->id
)) == 0)
636 value
= strcmp(a
->path
, b
->path
);
641 /* Conflict resolution */
643 static int merge_conflict_resolve_trivial(
645 git_merge_diff_list
*diff_list
,
646 const git_merge_diff
*conflict
)
648 int ours_empty
, theirs_empty
;
649 int ours_changed
, theirs_changed
, ours_theirs_differ
;
650 git_index_entry
const *result
= NULL
;
653 assert(resolved
&& diff_list
&& conflict
);
657 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
658 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
661 if (conflict
->our_status
== GIT_DELTA_RENAMED
||
662 conflict
->their_status
== GIT_DELTA_RENAMED
)
665 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
666 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
668 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
669 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
670 ours_theirs_differ
= ours_changed
&& theirs_changed
&&
671 index_entry_cmp(&conflict
->our_entry
, &conflict
->their_entry
);
674 * Note: with only one ancestor, some cases are not distinct:
676 * 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge
677 * 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge
678 * 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge
680 * Note that the two cases that take D/F conflicts into account
681 * specifically do not need to be explicitly tested, as D/F conflicts
682 * would fail the *empty* test:
684 * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
685 * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
687 * Note that many of these cases need not be explicitly tested, as
688 * they simply degrade to "all different" cases (eg, 11):
690 * 4: ancest:(empty)^, head:head, remote:remote = result:no merge
691 * 7: ancest:ancest+, head:(empty), remote:remote = result:no merge
692 * 9: ancest:ancest+, head:head, remote:(empty) = result:no merge
693 * 11: ancest:ancest+, head:head, remote:remote = result:no merge
696 /* 5ALT: ancest:*, head:head, remote:head = result:head */
697 if (ours_changed
&& !ours_empty
&& !ours_theirs_differ
)
698 result
= &conflict
->our_entry
;
699 /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
700 else if (ours_changed
&& ours_empty
&& theirs_empty
)
702 /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
703 else if (ours_empty
&& !theirs_changed
)
705 /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
706 else if (!ours_changed
&& theirs_empty
)
708 /* 13: ancest:ancest+, head:head, remote:ancest = result:head */
709 else if (ours_changed
&& !theirs_changed
)
710 result
= &conflict
->our_entry
;
711 /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
712 else if (!ours_changed
&& theirs_changed
)
713 result
= &conflict
->their_entry
;
717 if (result
!= NULL
&&
718 GIT_MERGE_INDEX_ENTRY_EXISTS(*result
) &&
719 (error
= git_vector_insert(&diff_list
->staged
, (void *)result
)) >= 0)
722 /* Note: trivial resolution does not update the REUC. */
727 static int merge_conflict_resolve_one_removed(
729 git_merge_diff_list
*diff_list
,
730 const git_merge_diff
*conflict
)
732 int ours_empty
, theirs_empty
;
733 int ours_changed
, theirs_changed
;
736 assert(resolved
&& diff_list
&& conflict
);
740 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
741 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
744 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
745 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
747 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
748 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
750 /* Removed in both */
751 if (ours_changed
&& ours_empty
&& theirs_empty
)
753 /* Removed in ours */
754 else if (ours_empty
&& !theirs_changed
)
756 /* Removed in theirs */
757 else if (!ours_changed
&& theirs_empty
)
761 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
766 static int merge_conflict_resolve_one_renamed(
768 git_merge_diff_list
*diff_list
,
769 const git_merge_diff
*conflict
)
771 int ours_renamed
, theirs_renamed
;
772 int ours_changed
, theirs_changed
;
773 git_index_entry
*merged
;
776 assert(resolved
&& diff_list
&& conflict
);
780 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
781 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
784 ours_renamed
= (conflict
->our_status
== GIT_DELTA_RENAMED
);
785 theirs_renamed
= (conflict
->their_status
== GIT_DELTA_RENAMED
);
787 if (!ours_renamed
&& !theirs_renamed
)
790 /* Reject one file in a 2->1 conflict */
791 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
792 conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
||
793 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
796 ours_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->our_entry
.id
) != 0);
797 theirs_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->their_entry
.id
) != 0);
799 /* if both are modified (and not to a common target) require a merge */
800 if (ours_changed
&& theirs_changed
&&
801 git_oid__cmp(&conflict
->our_entry
.id
, &conflict
->their_entry
.id
) != 0)
804 if ((merged
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
))) == NULL
)
808 memcpy(merged
, &conflict
->our_entry
, sizeof(git_index_entry
));
810 memcpy(merged
, &conflict
->their_entry
, sizeof(git_index_entry
));
813 merged
->path
= conflict
->our_entry
.path
;
815 merged
->path
= conflict
->their_entry
.path
;
819 git_vector_insert(&diff_list
->staged
, merged
);
820 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
825 static bool merge_conflict_can_resolve_contents(
826 const git_merge_diff
*conflict
)
828 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
829 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
832 /* Reject D/F conflicts */
833 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
)
836 /* Reject submodules. */
837 if (S_ISGITLINK(conflict
->ancestor_entry
.mode
) ||
838 S_ISGITLINK(conflict
->our_entry
.mode
) ||
839 S_ISGITLINK(conflict
->their_entry
.mode
))
842 /* Reject link/file conflicts. */
843 if ((S_ISLNK(conflict
->ancestor_entry
.mode
) ^
844 S_ISLNK(conflict
->our_entry
.mode
)) ||
845 (S_ISLNK(conflict
->ancestor_entry
.mode
) ^
846 S_ISLNK(conflict
->their_entry
.mode
)))
849 /* Reject name conflicts */
850 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
851 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
854 if ((conflict
->our_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
855 (conflict
->their_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
856 strcmp(conflict
->ancestor_entry
.path
, conflict
->their_entry
.path
) != 0)
862 static int merge_conflict_invoke_driver(
863 git_index_entry
**out
,
865 git_merge_driver
*driver
,
866 git_merge_diff_list
*diff_list
,
867 git_merge_driver_source
*src
)
869 git_index_entry
*result
;
870 git_buf buf
= GIT_BUF_INIT
;
879 if ((error
= driver
->apply(driver
, &path
, &mode
, &buf
, name
, src
)) < 0 ||
880 (error
= git_repository_odb(&odb
, src
->repo
)) < 0 ||
881 (error
= git_odb_write(&oid
, odb
, buf
.ptr
, buf
.size
, GIT_OBJECT_BLOB
)) < 0)
884 result
= git_pool_mallocz(&diff_list
->pool
, sizeof(git_index_entry
));
885 GIT_ERROR_CHECK_ALLOC(result
);
887 git_oid_cpy(&result
->id
, &oid
);
889 result
->file_size
= (uint32_t)buf
.size
;
891 result
->path
= git_pool_strdup(&diff_list
->pool
, path
);
892 GIT_ERROR_CHECK_ALLOC(result
->path
);
897 git_buf_dispose(&buf
);
903 static int merge_conflict_resolve_contents(
905 git_merge_diff_list
*diff_list
,
906 const git_merge_diff
*conflict
,
907 const git_merge_options
*merge_opts
,
908 const git_merge_file_options
*file_opts
)
910 git_merge_driver_source source
= {0};
911 git_merge_file_result result
= {0};
912 git_merge_driver
*driver
;
913 git_merge_driver__builtin builtin
= {{0}};
914 git_index_entry
*merge_result
;
917 bool fallback
= false;
920 assert(resolved
&& diff_list
&& conflict
);
924 if (!merge_conflict_can_resolve_contents(conflict
))
927 source
.repo
= diff_list
->repo
;
928 source
.default_driver
= merge_opts
->default_driver
;
929 source
.file_opts
= file_opts
;
930 source
.ancestor
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
931 &conflict
->ancestor_entry
: NULL
;
932 source
.ours
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
933 &conflict
->our_entry
: NULL
;
934 source
.theirs
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
935 &conflict
->their_entry
: NULL
;
937 if (file_opts
->favor
!= GIT_MERGE_FILE_FAVOR_NORMAL
) {
938 /* if the user requested a particular type of resolution (via the
939 * favor flag) then let that override the gitattributes and use
940 * the builtin driver.
943 builtin
.base
.apply
= git_merge_driver__builtin_apply
;
944 builtin
.favor
= file_opts
->favor
;
946 driver
= &builtin
.base
;
948 /* find the merge driver for this file */
949 if ((error
= git_merge_driver_for_source(&name
, &driver
, &source
)) < 0)
957 error
= merge_conflict_invoke_driver(&merge_result
, name
, driver
,
960 if (error
== GIT_PASSTHROUGH
)
965 error
= merge_conflict_invoke_driver(&merge_result
, "text",
966 &git_merge_driver__text
.base
, diff_list
, &source
);
970 if (error
== GIT_EMERGECONFLICT
)
976 git_vector_insert(&diff_list
->staged
, merge_result
);
977 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
982 git_merge_file_result_free(&result
);
988 static int merge_conflict_resolve(
990 git_merge_diff_list
*diff_list
,
991 const git_merge_diff
*conflict
,
992 const git_merge_options
*merge_opts
,
993 const git_merge_file_options
*file_opts
)
1000 if ((error
= merge_conflict_resolve_trivial(
1001 &resolved
, diff_list
, conflict
)) < 0)
1004 if (!resolved
&& (error
= merge_conflict_resolve_one_removed(
1005 &resolved
, diff_list
, conflict
)) < 0)
1008 if (!resolved
&& (error
= merge_conflict_resolve_one_renamed(
1009 &resolved
, diff_list
, conflict
)) < 0)
1012 if (!resolved
&& (error
= merge_conflict_resolve_contents(
1013 &resolved
, diff_list
, conflict
, merge_opts
, file_opts
)) < 0)
1022 /* Rename detection and coalescing */
1024 struct merge_diff_similarity
{
1025 unsigned char similarity
;
1029 static int index_entry_similarity_calc(
1031 git_repository
*repo
,
1032 git_index_entry
*entry
,
1033 const git_merge_options
*opts
)
1036 git_diff_file diff_file
= {{{0}}};
1037 git_object_size_t blobsize
;
1040 if (*out
|| *out
== &cache_invalid_marker
)
1045 if ((error
= git_blob_lookup(&blob
, repo
, &entry
->id
)) < 0)
1048 git_oid_cpy(&diff_file
.id
, &entry
->id
);
1049 diff_file
.path
= entry
->path
;
1050 diff_file
.size
= entry
->file_size
;
1051 diff_file
.mode
= entry
->mode
;
1052 diff_file
.flags
= 0;
1054 blobsize
= git_blob_rawsize(blob
);
1056 /* file too big for rename processing */
1057 if (!git__is_sizet(blobsize
))
1060 error
= opts
->metric
->buffer_signature(out
, &diff_file
,
1061 git_blob_rawcontent(blob
), (size_t)blobsize
,
1062 opts
->metric
->payload
);
1063 if (error
== GIT_EBUFS
)
1064 *out
= &cache_invalid_marker
;
1066 git_blob_free(blob
);
1071 static int index_entry_similarity_inexact(
1072 git_repository
*repo
,
1078 const git_merge_options
*opts
)
1083 if (!GIT_MODE_ISBLOB(a
->mode
) || !GIT_MODE_ISBLOB(b
->mode
))
1086 /* update signature cache if needed */
1087 if ((error
= index_entry_similarity_calc(&cache
[a_idx
], repo
, a
, opts
)) < 0 ||
1088 (error
= index_entry_similarity_calc(&cache
[b_idx
], repo
, b
, opts
)) < 0)
1091 /* some metrics may not wish to process this file (too big / too small) */
1092 if (cache
[a_idx
] == &cache_invalid_marker
|| cache
[b_idx
] == &cache_invalid_marker
)
1095 /* compare signatures */
1096 if (opts
->metric
->similarity(&score
, cache
[a_idx
], cache
[b_idx
], opts
->metric
->payload
) < 0)
1102 else if (score
> 100)
1108 /* Tracks deletes by oid for merge_diff_mark_similarity_exact(). This is a
1109 * non-shrinking queue where next_pos is the next position to dequeue.
1112 git_array_t(size_t) arr
;
1115 } deletes_by_oid_queue
;
1117 static void deletes_by_oid_free(git_oidmap
*map
) {
1118 deletes_by_oid_queue
*queue
;
1123 git_oidmap_foreach_value(map
, queue
, {
1124 git_array_clear(queue
->arr
);
1126 git_oidmap_free(map
);
1129 static int deletes_by_oid_enqueue(git_oidmap
*map
, git_pool
* pool
, const git_oid
*id
, size_t idx
)
1131 deletes_by_oid_queue
*queue
;
1132 size_t *array_entry
;
1134 if ((queue
= git_oidmap_get(map
, id
)) == NULL
) {
1135 queue
= git_pool_malloc(pool
, sizeof(deletes_by_oid_queue
));
1136 GIT_ERROR_CHECK_ALLOC(queue
);
1138 git_array_init(queue
->arr
);
1139 queue
->next_pos
= 0;
1140 queue
->first_entry
= idx
;
1142 if (git_oidmap_set(map
, id
, queue
) < 0)
1145 array_entry
= git_array_alloc(queue
->arr
);
1146 GIT_ERROR_CHECK_ALLOC(array_entry
);
1153 static int deletes_by_oid_dequeue(size_t *idx
, git_oidmap
*map
, const git_oid
*id
)
1155 deletes_by_oid_queue
*queue
;
1156 size_t *array_entry
;
1158 if ((queue
= git_oidmap_get(map
, id
)) == NULL
)
1159 return GIT_ENOTFOUND
;
1161 if (queue
->next_pos
== 0) {
1162 *idx
= queue
->first_entry
;
1164 array_entry
= git_array_get(queue
->arr
, queue
->next_pos
- 1);
1165 if (array_entry
== NULL
)
1166 return GIT_ENOTFOUND
;
1168 *idx
= *array_entry
;
1175 static int merge_diff_mark_similarity_exact(
1176 git_merge_diff_list
*diff_list
,
1177 struct merge_diff_similarity
*similarity_ours
,
1178 struct merge_diff_similarity
*similarity_theirs
)
1181 git_merge_diff
*conflict_src
, *conflict_tgt
;
1182 git_oidmap
*ours_deletes_by_oid
= NULL
, *theirs_deletes_by_oid
= NULL
;
1185 if (git_oidmap_new(&ours_deletes_by_oid
) < 0 ||
1186 git_oidmap_new(&theirs_deletes_by_oid
) < 0) {
1191 /* Build a map of object ids to conflicts */
1192 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1193 /* Items can be the source of a rename iff they have an item in the
1194 * ancestor slot and lack an item in the ours or theirs slot. */
1195 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
))
1198 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1199 error
= deletes_by_oid_enqueue(ours_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1204 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1205 error
= deletes_by_oid_enqueue(theirs_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1211 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1212 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1215 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
)) {
1216 if (deletes_by_oid_dequeue(&i
, ours_deletes_by_oid
, &conflict_tgt
->our_entry
.id
) == 0) {
1217 similarity_ours
[i
].similarity
= 100;
1218 similarity_ours
[i
].other_idx
= j
;
1220 similarity_ours
[j
].similarity
= 100;
1221 similarity_ours
[j
].other_idx
= i
;
1225 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
)) {
1226 if (deletes_by_oid_dequeue(&i
, theirs_deletes_by_oid
, &conflict_tgt
->their_entry
.id
) == 0) {
1227 similarity_theirs
[i
].similarity
= 100;
1228 similarity_theirs
[i
].other_idx
= j
;
1230 similarity_theirs
[j
].similarity
= 100;
1231 similarity_theirs
[j
].other_idx
= i
;
1237 deletes_by_oid_free(ours_deletes_by_oid
);
1238 deletes_by_oid_free(theirs_deletes_by_oid
);
1243 static int merge_diff_mark_similarity_inexact(
1244 git_repository
*repo
,
1245 git_merge_diff_list
*diff_list
,
1246 struct merge_diff_similarity
*similarity_ours
,
1247 struct merge_diff_similarity
*similarity_theirs
,
1249 const git_merge_options
*opts
)
1252 git_merge_diff
*conflict_src
, *conflict_tgt
;
1255 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1256 /* Items can be the source of a rename iff they have an item in the
1257 * ancestor slot and lack an item in the ours or theirs slot. */
1258 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
) ||
1259 (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
) &&
1260 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)))
1263 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1264 size_t our_idx
= diff_list
->conflicts
.length
+ j
;
1265 size_t their_idx
= (diff_list
->conflicts
.length
* 2) + j
;
1267 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1270 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
) &&
1271 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1272 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->our_entry
, our_idx
, cache
, opts
);
1274 if (similarity
== GIT_EBUFS
)
1276 else if (similarity
< 0)
1279 if (similarity
> similarity_ours
[i
].similarity
&&
1280 similarity
> similarity_ours
[j
].similarity
) {
1281 /* Clear previous best similarity */
1282 if (similarity_ours
[i
].similarity
> 0)
1283 similarity_ours
[similarity_ours
[i
].other_idx
].similarity
= 0;
1285 if (similarity_ours
[j
].similarity
> 0)
1286 similarity_ours
[similarity_ours
[j
].other_idx
].similarity
= 0;
1288 similarity_ours
[i
].similarity
= similarity
;
1289 similarity_ours
[i
].other_idx
= j
;
1291 similarity_ours
[j
].similarity
= similarity
;
1292 similarity_ours
[j
].other_idx
= i
;
1296 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
) &&
1297 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1298 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->their_entry
, their_idx
, cache
, opts
);
1300 if (similarity
> similarity_theirs
[i
].similarity
&&
1301 similarity
> similarity_theirs
[j
].similarity
) {
1302 /* Clear previous best similarity */
1303 if (similarity_theirs
[i
].similarity
> 0)
1304 similarity_theirs
[similarity_theirs
[i
].other_idx
].similarity
= 0;
1306 if (similarity_theirs
[j
].similarity
> 0)
1307 similarity_theirs
[similarity_theirs
[j
].other_idx
].similarity
= 0;
1309 similarity_theirs
[i
].similarity
= similarity
;
1310 similarity_theirs
[i
].other_idx
= j
;
1312 similarity_theirs
[j
].similarity
= similarity
;
1313 similarity_theirs
[j
].other_idx
= i
;
1325 * Ancestor Ours Theirs
1327 * 0a A A A No rename
1328 * b A A* A No rename (ours was rewritten)
1329 * c A A A* No rename (theirs rewritten)
1330 * 1a A A B[A] Rename or rename/edit
1331 * b A B[A] A (automergeable)
1332 * 2 A B[A] B[A] Both renamed (automergeable)
1333 * 3a A B[A] Rename/delete
1335 * 4a A B[A] B Rename/add [B~ours B~theirs]
1337 * 5 A B[A] C[A] Both renamed ("1 -> 2")
1338 * 6 A C[A] Both renamed ("2 -> 1")
1339 * B C[B] [C~ours C~theirs] (automergeable)
1341 static void merge_diff_mark_rename_conflict(
1342 git_merge_diff_list
*diff_list
,
1343 struct merge_diff_similarity
*similarity_ours
,
1345 size_t ours_source_idx
,
1346 struct merge_diff_similarity
*similarity_theirs
,
1347 bool theirs_renamed
,
1348 size_t theirs_source_idx
,
1349 git_merge_diff
*target
,
1350 const git_merge_options
*opts
)
1352 git_merge_diff
*ours_source
= NULL
, *theirs_source
= NULL
;
1355 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1358 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1360 /* Detect 2->1 conflicts */
1361 if (ours_renamed
&& theirs_renamed
) {
1362 /* Both renamed to the same target name. */
1363 if (ours_source_idx
== theirs_source_idx
)
1364 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED
;
1366 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1367 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1369 } else if (ours_renamed
) {
1370 /* If our source was also renamed in theirs, this is a 1->2 */
1371 if (similarity_theirs
[ours_source_idx
].similarity
>= opts
->rename_threshold
)
1372 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1374 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
)) {
1375 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1376 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1379 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source
->their_entry
))
1380 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1382 else if (ours_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1383 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1384 } else if (theirs_renamed
) {
1385 /* If their source was also renamed in ours, this is a 1->2 */
1386 if (similarity_ours
[theirs_source_idx
].similarity
>= opts
->rename_threshold
)
1387 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1389 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
)) {
1390 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1391 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1394 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source
->our_entry
))
1395 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1397 else if (theirs_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1398 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1402 GIT_INLINE(void) merge_diff_coalesce_rename(
1403 git_index_entry
*source_entry
,
1404 git_delta_t
*source_status
,
1405 git_index_entry
*target_entry
,
1406 git_delta_t
*target_status
)
1408 /* Coalesce the rename target into the rename source. */
1409 memcpy(source_entry
, target_entry
, sizeof(git_index_entry
));
1410 *source_status
= GIT_DELTA_RENAMED
;
1412 memset(target_entry
, 0x0, sizeof(git_index_entry
));
1413 *target_status
= GIT_DELTA_UNMODIFIED
;
1416 static void merge_diff_list_coalesce_renames(
1417 git_merge_diff_list
*diff_list
,
1418 struct merge_diff_similarity
*similarity_ours
,
1419 struct merge_diff_similarity
*similarity_theirs
,
1420 const git_merge_options
*opts
)
1423 bool ours_renamed
= 0, theirs_renamed
= 0;
1424 size_t ours_source_idx
= 0, theirs_source_idx
= 0;
1425 git_merge_diff
*ours_source
, *theirs_source
, *target
;
1427 for (i
= 0; i
< diff_list
->conflicts
.length
; i
++) {
1428 target
= diff_list
->conflicts
.contents
[i
];
1433 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
) &&
1434 similarity_ours
[i
].similarity
>= opts
->rename_threshold
) {
1435 ours_source_idx
= similarity_ours
[i
].other_idx
;
1437 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1439 merge_diff_coalesce_rename(
1440 &ours_source
->our_entry
,
1441 &ours_source
->our_status
,
1443 &target
->our_status
);
1445 similarity_ours
[ours_source_idx
].similarity
= 0;
1446 similarity_ours
[i
].similarity
= 0;
1451 /* insufficient to determine direction */
1452 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
) &&
1453 similarity_theirs
[i
].similarity
>= opts
->rename_threshold
) {
1454 theirs_source_idx
= similarity_theirs
[i
].other_idx
;
1456 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1458 merge_diff_coalesce_rename(
1459 &theirs_source
->their_entry
,
1460 &theirs_source
->their_status
,
1461 &target
->their_entry
,
1462 &target
->their_status
);
1464 similarity_theirs
[theirs_source_idx
].similarity
= 0;
1465 similarity_theirs
[i
].similarity
= 0;
1470 merge_diff_mark_rename_conflict(diff_list
,
1471 similarity_ours
, ours_renamed
, ours_source_idx
,
1472 similarity_theirs
, theirs_renamed
, theirs_source_idx
,
1477 static int merge_diff_empty(const git_vector
*conflicts
, size_t idx
, void *p
)
1479 git_merge_diff
*conflict
= conflicts
->contents
[idx
];
1483 return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) &&
1484 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) &&
1485 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
));
1488 static void merge_diff_list_count_candidates(
1489 git_merge_diff_list
*diff_list
,
1493 git_merge_diff
*entry
;
1499 git_vector_foreach(&diff_list
->conflicts
, i
, entry
) {
1500 if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
) &&
1501 (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->our_entry
) ||
1502 !GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->their_entry
)))
1504 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
))
1509 int git_merge_diff_list__find_renames(
1510 git_repository
*repo
,
1511 git_merge_diff_list
*diff_list
,
1512 const git_merge_options
*opts
)
1514 struct merge_diff_similarity
*similarity_ours
, *similarity_theirs
;
1515 void **cache
= NULL
;
1516 size_t cache_size
= 0;
1517 size_t src_count
, tgt_count
, i
;
1520 assert(diff_list
&& opts
);
1522 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) == 0)
1525 similarity_ours
= git__calloc(diff_list
->conflicts
.length
,
1526 sizeof(struct merge_diff_similarity
));
1527 GIT_ERROR_CHECK_ALLOC(similarity_ours
);
1529 similarity_theirs
= git__calloc(diff_list
->conflicts
.length
,
1530 sizeof(struct merge_diff_similarity
));
1531 GIT_ERROR_CHECK_ALLOC(similarity_theirs
);
1533 /* Calculate similarity between items that were deleted from the ancestor
1534 * and added in the other branch.
1536 if ((error
= merge_diff_mark_similarity_exact(diff_list
, similarity_ours
, similarity_theirs
)) < 0)
1539 if (opts
->rename_threshold
< 100 && diff_list
->conflicts
.length
<= opts
->target_limit
) {
1540 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&cache_size
, diff_list
->conflicts
.length
, 3);
1541 cache
= git__calloc(cache_size
, sizeof(void *));
1542 GIT_ERROR_CHECK_ALLOC(cache
);
1544 merge_diff_list_count_candidates(diff_list
, &src_count
, &tgt_count
);
1546 if (src_count
> opts
->target_limit
|| tgt_count
> opts
->target_limit
) {
1549 if ((error
= merge_diff_mark_similarity_inexact(
1550 repo
, diff_list
, similarity_ours
, similarity_theirs
, cache
, opts
)) < 0)
1555 /* For entries that are appropriately similar, merge the new name's entry
1556 * into the old name.
1558 merge_diff_list_coalesce_renames(diff_list
, similarity_ours
, similarity_theirs
, opts
);
1560 /* And remove any entries that were merged and are now empty. */
1561 git_vector_remove_matching(&diff_list
->conflicts
, merge_diff_empty
, NULL
);
1564 if (cache
!= NULL
) {
1565 for (i
= 0; i
< cache_size
; ++i
) {
1566 if (cache
[i
] != NULL
&& cache
[i
] != &cache_invalid_marker
)
1567 opts
->metric
->free_signature(cache
[i
], opts
->metric
->payload
);
1573 git__free(similarity_ours
);
1574 git__free(similarity_theirs
);
1579 /* Directory/file conflict handling */
1581 GIT_INLINE(const char *) merge_diff_path(
1582 const git_merge_diff
*conflict
)
1584 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
1585 return conflict
->ancestor_entry
.path
;
1586 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
))
1587 return conflict
->our_entry
.path
;
1588 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
1589 return conflict
->their_entry
.path
;
1594 GIT_INLINE(bool) merge_diff_any_side_added_or_modified(
1595 const git_merge_diff
*conflict
)
1597 if (conflict
->our_status
== GIT_DELTA_ADDED
||
1598 conflict
->our_status
== GIT_DELTA_MODIFIED
||
1599 conflict
->their_status
== GIT_DELTA_ADDED
||
1600 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1606 GIT_INLINE(bool) path_is_prefixed(const char *parent
, const char *child
)
1608 size_t child_len
= strlen(child
);
1609 size_t parent_len
= strlen(parent
);
1611 if (child_len
< parent_len
||
1612 strncmp(parent
, child
, parent_len
) != 0)
1615 return (child
[parent_len
] == '/');
1618 GIT_INLINE(int) merge_diff_detect_df_conflict(
1619 struct merge_diff_df_data
*df_data
,
1620 git_merge_diff
*conflict
)
1622 const char *cur_path
= merge_diff_path(conflict
);
1624 /* Determine if this is a D/F conflict or the child of one */
1625 if (df_data
->df_path
&&
1626 path_is_prefixed(df_data
->df_path
, cur_path
))
1627 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1628 else if(df_data
->df_path
)
1629 df_data
->df_path
= NULL
;
1630 else if (df_data
->prev_path
&&
1631 merge_diff_any_side_added_or_modified(df_data
->prev_conflict
) &&
1632 merge_diff_any_side_added_or_modified(conflict
) &&
1633 path_is_prefixed(df_data
->prev_path
, cur_path
)) {
1634 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1636 df_data
->prev_conflict
->type
= GIT_MERGE_DIFF_DIRECTORY_FILE
;
1637 df_data
->df_path
= df_data
->prev_path
;
1640 df_data
->prev_path
= cur_path
;
1641 df_data
->prev_conflict
= conflict
;
1646 /* Conflict handling */
1648 GIT_INLINE(int) merge_diff_detect_type(
1649 git_merge_diff
*conflict
)
1651 if (conflict
->our_status
== GIT_DELTA_ADDED
&&
1652 conflict
->their_status
== GIT_DELTA_ADDED
)
1653 conflict
->type
= GIT_MERGE_DIFF_BOTH_ADDED
;
1654 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1655 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1656 conflict
->type
= GIT_MERGE_DIFF_BOTH_MODIFIED
;
1657 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1658 conflict
->their_status
== GIT_DELTA_DELETED
)
1659 conflict
->type
= GIT_MERGE_DIFF_BOTH_DELETED
;
1660 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1661 conflict
->their_status
== GIT_DELTA_DELETED
)
1662 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1663 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1664 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1665 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1667 conflict
->type
= GIT_MERGE_DIFF_NONE
;
1672 GIT_INLINE(int) index_entry_dup_pool(
1673 git_index_entry
*out
,
1675 const git_index_entry
*src
)
1678 memcpy(out
, src
, sizeof(git_index_entry
));
1679 if ((out
->path
= git_pool_strdup(pool
, src
->path
)) == NULL
)
1686 GIT_INLINE(int) merge_delta_type_from_index_entries(
1687 const git_index_entry
*ancestor
,
1688 const git_index_entry
*other
)
1690 if (ancestor
== NULL
&& other
== NULL
)
1691 return GIT_DELTA_UNMODIFIED
;
1692 else if (ancestor
== NULL
&& other
!= NULL
)
1693 return GIT_DELTA_ADDED
;
1694 else if (ancestor
!= NULL
&& other
== NULL
)
1695 return GIT_DELTA_DELETED
;
1696 else if (S_ISDIR(ancestor
->mode
) ^ S_ISDIR(other
->mode
))
1697 return GIT_DELTA_TYPECHANGE
;
1698 else if(S_ISLNK(ancestor
->mode
) ^ S_ISLNK(other
->mode
))
1699 return GIT_DELTA_TYPECHANGE
;
1700 else if (git_oid__cmp(&ancestor
->id
, &other
->id
) ||
1701 ancestor
->mode
!= other
->mode
)
1702 return GIT_DELTA_MODIFIED
;
1704 return GIT_DELTA_UNMODIFIED
;
1707 static git_merge_diff
*merge_diff_from_index_entries(
1708 git_merge_diff_list
*diff_list
,
1709 const git_index_entry
**entries
)
1711 git_merge_diff
*conflict
;
1712 git_pool
*pool
= &diff_list
->pool
;
1714 if ((conflict
= git_pool_mallocz(pool
, sizeof(git_merge_diff
))) == NULL
)
1717 if (index_entry_dup_pool(&conflict
->ancestor_entry
, pool
, entries
[TREE_IDX_ANCESTOR
]) < 0 ||
1718 index_entry_dup_pool(&conflict
->our_entry
, pool
, entries
[TREE_IDX_OURS
]) < 0 ||
1719 index_entry_dup_pool(&conflict
->their_entry
, pool
, entries
[TREE_IDX_THEIRS
]) < 0)
1722 conflict
->our_status
= merge_delta_type_from_index_entries(
1723 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_OURS
]);
1724 conflict
->their_status
= merge_delta_type_from_index_entries(
1725 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_THEIRS
]);
1732 static int merge_diff_list_insert_conflict(
1733 git_merge_diff_list
*diff_list
,
1734 struct merge_diff_df_data
*merge_df_data
,
1735 const git_index_entry
*tree_items
[3])
1737 git_merge_diff
*conflict
;
1739 if ((conflict
= merge_diff_from_index_entries(diff_list
, tree_items
)) == NULL
||
1740 merge_diff_detect_type(conflict
) < 0 ||
1741 merge_diff_detect_df_conflict(merge_df_data
, conflict
) < 0 ||
1742 git_vector_insert(&diff_list
->conflicts
, conflict
) < 0)
1748 static int merge_diff_list_insert_unmodified(
1749 git_merge_diff_list
*diff_list
,
1750 const git_index_entry
*tree_items
[3])
1753 git_index_entry
*entry
;
1755 entry
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
));
1756 GIT_ERROR_CHECK_ALLOC(entry
);
1758 if ((error
= index_entry_dup_pool(entry
, &diff_list
->pool
, tree_items
[0])) >= 0)
1759 error
= git_vector_insert(&diff_list
->staged
, entry
);
1764 struct merge_diff_find_data
{
1765 git_merge_diff_list
*diff_list
;
1766 struct merge_diff_df_data df_data
;
1769 static int queue_difference(const git_index_entry
**entries
, void *data
)
1771 struct merge_diff_find_data
*find_data
= data
;
1772 bool item_modified
= false;
1775 if (!entries
[0] || !entries
[1] || !entries
[2]) {
1776 item_modified
= true;
1778 for (i
= 1; i
< 3; i
++) {
1779 if (index_entry_cmp(entries
[0], entries
[i
]) != 0) {
1780 item_modified
= true;
1786 return item_modified
?
1787 merge_diff_list_insert_conflict(
1788 find_data
->diff_list
, &find_data
->df_data
, entries
) :
1789 merge_diff_list_insert_unmodified(find_data
->diff_list
, entries
);
1792 int git_merge_diff_list__find_differences(
1793 git_merge_diff_list
*diff_list
,
1794 git_iterator
*ancestor_iter
,
1795 git_iterator
*our_iter
,
1796 git_iterator
*their_iter
)
1798 git_iterator
*iterators
[3] = { ancestor_iter
, our_iter
, their_iter
};
1799 struct merge_diff_find_data find_data
= { diff_list
};
1801 return git_iterator_walk(iterators
, 3, queue_difference
, &find_data
);
1804 git_merge_diff_list
*git_merge_diff_list__alloc(git_repository
*repo
)
1806 git_merge_diff_list
*diff_list
= git__calloc(1, sizeof(git_merge_diff_list
));
1808 if (diff_list
== NULL
)
1811 diff_list
->repo
= repo
;
1814 if (git_pool_init(&diff_list
->pool
, 1) < 0 ||
1815 git_vector_init(&diff_list
->staged
, 0, NULL
) < 0 ||
1816 git_vector_init(&diff_list
->conflicts
, 0, NULL
) < 0 ||
1817 git_vector_init(&diff_list
->resolved
, 0, NULL
) < 0) {
1818 git_merge_diff_list__free(diff_list
);
1825 void git_merge_diff_list__free(git_merge_diff_list
*diff_list
)
1830 git_vector_free(&diff_list
->staged
);
1831 git_vector_free(&diff_list
->conflicts
);
1832 git_vector_free(&diff_list
->resolved
);
1833 git_pool_clear(&diff_list
->pool
);
1834 git__free(diff_list
);
1837 static int merge_normalize_opts(
1838 git_repository
*repo
,
1839 git_merge_options
*opts
,
1840 const git_merge_options
*given
)
1842 git_config
*cfg
= NULL
;
1843 git_config_entry
*entry
= NULL
;
1846 assert(repo
&& opts
);
1848 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0)
1851 if (given
!= NULL
) {
1852 memcpy(opts
, given
, sizeof(git_merge_options
));
1854 git_merge_options init
= GIT_MERGE_OPTIONS_INIT
;
1855 memcpy(opts
, &init
, sizeof(init
));
1858 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) && !opts
->rename_threshold
)
1859 opts
->rename_threshold
= GIT_MERGE_DEFAULT_RENAME_THRESHOLD
;
1861 if (given
&& given
->default_driver
) {
1862 opts
->default_driver
= git__strdup(given
->default_driver
);
1863 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1865 error
= git_config_get_entry(&entry
, cfg
, "merge.default");
1868 opts
->default_driver
= git__strdup(entry
->value
);
1869 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1870 } else if (error
== GIT_ENOTFOUND
) {
1877 if (!opts
->target_limit
) {
1878 int limit
= git_config__get_int_force(cfg
, "merge.renamelimit", 0);
1881 limit
= git_config__get_int_force(cfg
, "diff.renamelimit", 0);
1883 opts
->target_limit
= (limit
<= 0) ?
1884 GIT_MERGE_DEFAULT_TARGET_LIMIT
: (unsigned int)limit
;
1887 /* assign the internal metric with whitespace flag as payload */
1888 if (!opts
->metric
) {
1889 opts
->metric
= git__malloc(sizeof(git_diff_similarity_metric
));
1890 GIT_ERROR_CHECK_ALLOC(opts
->metric
);
1892 opts
->metric
->file_signature
= git_diff_find_similar__hashsig_for_file
;
1893 opts
->metric
->buffer_signature
= git_diff_find_similar__hashsig_for_buf
;
1894 opts
->metric
->free_signature
= git_diff_find_similar__hashsig_free
;
1895 opts
->metric
->similarity
= git_diff_find_similar__calc_similarity
;
1896 opts
->metric
->payload
= (void *)GIT_HASHSIG_SMART_WHITESPACE
;
1900 git_config_entry_free(entry
);
1905 static int merge_index_insert_reuc(
1908 const git_index_entry
*entry
)
1910 const git_index_reuc_entry
*reuc
;
1911 int mode
[3] = { 0, 0, 0 };
1912 git_oid
const *oid
[3] = { NULL
, NULL
, NULL
};
1915 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry
))
1918 if ((reuc
= git_index_reuc_get_bypath(index
, entry
->path
)) != NULL
) {
1919 for (i
= 0; i
< 3; i
++) {
1920 mode
[i
] = reuc
->mode
[i
];
1921 oid
[i
] = &reuc
->oid
[i
];
1925 mode
[idx
] = entry
->mode
;
1926 oid
[idx
] = &entry
->id
;
1928 return git_index_reuc_add(index
, entry
->path
,
1929 mode
[0], oid
[0], mode
[1], oid
[1], mode
[2], oid
[2]);
1932 static int index_update_reuc(git_index
*index
, git_merge_diff_list
*diff_list
)
1936 git_merge_diff
*conflict
;
1938 /* Add each entry in the resolved conflict to the REUC independently, since
1939 * the paths may differ due to renames. */
1940 git_vector_foreach(&diff_list
->resolved
, i
, conflict
) {
1941 const git_index_entry
*ancestor
=
1942 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
1943 &conflict
->ancestor_entry
: NULL
;
1945 const git_index_entry
*ours
=
1946 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
1947 &conflict
->our_entry
: NULL
;
1949 const git_index_entry
*theirs
=
1950 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
1951 &conflict
->their_entry
: NULL
;
1953 if (ancestor
!= NULL
&&
1954 (error
= merge_index_insert_reuc(index
, TREE_IDX_ANCESTOR
, ancestor
)) < 0)
1958 (error
= merge_index_insert_reuc(index
, TREE_IDX_OURS
, ours
)) < 0)
1961 if (theirs
!= NULL
&&
1962 (error
= merge_index_insert_reuc(index
, TREE_IDX_THEIRS
, theirs
)) < 0)
1969 static int index_from_diff_list(git_index
**out
,
1970 git_merge_diff_list
*diff_list
, bool skip_reuc
)
1974 git_merge_diff
*conflict
;
1979 if ((error
= git_index_new(&index
)) < 0)
1982 if ((error
= git_index__fill(index
, &diff_list
->staged
)) < 0)
1985 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
1986 const git_index_entry
*ancestor
=
1987 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
1988 &conflict
->ancestor_entry
: NULL
;
1990 const git_index_entry
*ours
=
1991 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
1992 &conflict
->our_entry
: NULL
;
1994 const git_index_entry
*theirs
=
1995 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
1996 &conflict
->their_entry
: NULL
;
1998 if ((error
= git_index_conflict_add(index
, ancestor
, ours
, theirs
)) < 0)
2002 /* Add each rename entry to the rename portion of the index. */
2003 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
2004 const char *ancestor_path
, *our_path
, *their_path
;
2006 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
2009 ancestor_path
= conflict
->ancestor_entry
.path
;
2012 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
2013 conflict
->our_entry
.path
: NULL
;
2016 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
2017 conflict
->their_entry
.path
: NULL
;
2019 if ((our_path
&& strcmp(ancestor_path
, our_path
) != 0) ||
2020 (their_path
&& strcmp(ancestor_path
, their_path
) != 0)) {
2021 if ((error
= git_index_name_add(index
, ancestor_path
, our_path
, their_path
)) < 0)
2027 if ((error
= index_update_reuc(index
, diff_list
)) < 0)
2035 git_index_free(index
);
2039 static git_iterator
*iterator_given_or_empty(git_iterator
**empty
, git_iterator
*given
)
2041 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2046 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2048 if (git_iterator_for_nothing(empty
, &opts
) < 0)
2054 int git_merge__iterators(
2056 git_repository
*repo
,
2057 git_iterator
*ancestor_iter
,
2058 git_iterator
*our_iter
,
2059 git_iterator
*theirs_iter
,
2060 const git_merge_options
*given_opts
)
2062 git_iterator
*empty_ancestor
= NULL
,
2064 *empty_theirs
= NULL
;
2065 git_merge_diff_list
*diff_list
;
2066 git_merge_options opts
;
2067 git_merge_file_options file_opts
= GIT_MERGE_FILE_OPTIONS_INIT
;
2068 git_merge_diff
*conflict
;
2073 assert(out
&& repo
);
2077 GIT_ERROR_CHECK_VERSION(
2078 given_opts
, GIT_MERGE_OPTIONS_VERSION
, "git_merge_options");
2080 if ((error
= merge_normalize_opts(repo
, &opts
, given_opts
)) < 0)
2083 file_opts
.favor
= opts
.file_favor
;
2084 file_opts
.flags
= opts
.file_flags
;
2086 /* use the git-inspired labels when virtual base building */
2087 if (opts
.flags
& GIT_MERGE__VIRTUAL_BASE
) {
2088 file_opts
.ancestor_label
= "merged common ancestors";
2089 file_opts
.our_label
= "Temporary merge branch 1";
2090 file_opts
.their_label
= "Temporary merge branch 2";
2091 file_opts
.flags
|= GIT_MERGE_FILE_FAVOR__CONFLICTED
;
2092 file_opts
.marker_size
= GIT_MERGE_CONFLICT_MARKER_SIZE
+ 2;
2095 diff_list
= git_merge_diff_list__alloc(repo
);
2096 GIT_ERROR_CHECK_ALLOC(diff_list
);
2098 ancestor_iter
= iterator_given_or_empty(&empty_ancestor
, ancestor_iter
);
2099 our_iter
= iterator_given_or_empty(&empty_ours
, our_iter
);
2100 theirs_iter
= iterator_given_or_empty(&empty_theirs
, theirs_iter
);
2102 if ((error
= git_merge_diff_list__find_differences(
2103 diff_list
, ancestor_iter
, our_iter
, theirs_iter
)) < 0 ||
2104 (error
= git_merge_diff_list__find_renames(repo
, diff_list
, &opts
)) < 0)
2107 memcpy(&changes
, &diff_list
->conflicts
, sizeof(git_vector
));
2108 git_vector_clear(&diff_list
->conflicts
);
2110 git_vector_foreach(&changes
, i
, conflict
) {
2113 if ((error
= merge_conflict_resolve(
2114 &resolved
, diff_list
, conflict
, &opts
, &file_opts
)) < 0)
2118 if ((opts
.flags
& GIT_MERGE_FAIL_ON_CONFLICT
)) {
2119 git_error_set(GIT_ERROR_MERGE
, "merge conflicts exist");
2120 error
= GIT_EMERGECONFLICT
;
2124 git_vector_insert(&diff_list
->conflicts
, conflict
);
2128 error
= index_from_diff_list(out
, diff_list
,
2129 (opts
.flags
& GIT_MERGE_SKIP_REUC
));
2132 if (!given_opts
|| !given_opts
->metric
)
2133 git__free(opts
.metric
);
2135 git__free((char *)opts
.default_driver
);
2137 git_merge_diff_list__free(diff_list
);
2138 git_iterator_free(empty_ancestor
);
2139 git_iterator_free(empty_ours
);
2140 git_iterator_free(empty_theirs
);
2145 int git_merge_trees(
2147 git_repository
*repo
,
2148 const git_tree
*ancestor_tree
,
2149 const git_tree
*our_tree
,
2150 const git_tree
*their_tree
,
2151 const git_merge_options
*merge_opts
)
2153 git_iterator
*ancestor_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2154 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2157 assert(out
&& repo
);
2159 /* if one side is treesame to the ancestor, take the other side */
2160 if (ancestor_tree
&& merge_opts
&& (merge_opts
->flags
& GIT_MERGE_SKIP_REUC
)) {
2161 const git_tree
*result
= NULL
;
2162 const git_oid
*ancestor_tree_id
= git_tree_id(ancestor_tree
);
2164 if (our_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(our_tree
)))
2165 result
= their_tree
;
2166 else if (their_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(their_tree
)))
2170 if ((error
= git_index_new(out
)) == 0)
2171 error
= git_index_read_tree(*out
, result
);
2177 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2179 if ((error
= git_iterator_for_tree(
2180 &ancestor_iter
, (git_tree
*)ancestor_tree
, &iter_opts
)) < 0 ||
2181 (error
= git_iterator_for_tree(
2182 &our_iter
, (git_tree
*)our_tree
, &iter_opts
)) < 0 ||
2183 (error
= git_iterator_for_tree(
2184 &their_iter
, (git_tree
*)their_tree
, &iter_opts
)) < 0)
2187 error
= git_merge__iterators(
2188 out
, repo
, ancestor_iter
, our_iter
, their_iter
, merge_opts
);
2191 git_iterator_free(ancestor_iter
);
2192 git_iterator_free(our_iter
);
2193 git_iterator_free(their_iter
);
2198 static int merge_annotated_commits(
2199 git_index
**index_out
,
2200 git_annotated_commit
**base_out
,
2201 git_repository
*repo
,
2202 git_annotated_commit
*our_commit
,
2203 git_annotated_commit
*their_commit
,
2204 size_t recursion_level
,
2205 const git_merge_options
*opts
);
2207 GIT_INLINE(int) insert_head_ids(
2208 git_array_oid_t
*ids
,
2209 const git_annotated_commit
*annotated_commit
)
2214 if (annotated_commit
->type
== GIT_ANNOTATED_COMMIT_REAL
) {
2215 id
= git_array_alloc(*ids
);
2216 GIT_ERROR_CHECK_ALLOC(id
);
2218 git_oid_cpy(id
, git_commit_id(annotated_commit
->commit
));
2220 for (i
= 0; i
< annotated_commit
->parents
.size
; i
++) {
2221 id
= git_array_alloc(*ids
);
2222 GIT_ERROR_CHECK_ALLOC(id
);
2224 git_oid_cpy(id
, &annotated_commit
->parents
.ptr
[i
]);
2231 static int create_virtual_base(
2232 git_annotated_commit
**out
,
2233 git_repository
*repo
,
2234 git_annotated_commit
*one
,
2235 git_annotated_commit
*two
,
2236 const git_merge_options
*opts
,
2237 size_t recursion_level
)
2239 git_annotated_commit
*result
= NULL
;
2240 git_index
*index
= NULL
;
2241 git_merge_options virtual_opts
= GIT_MERGE_OPTIONS_INIT
;
2243 /* Conflicts in the merge base creation do not propagate to conflicts
2244 * in the result; the conflicted base will act as the common ancestor.
2247 memcpy(&virtual_opts
, opts
, sizeof(git_merge_options
));
2249 virtual_opts
.flags
&= ~GIT_MERGE_FAIL_ON_CONFLICT
;
2250 virtual_opts
.flags
|= GIT_MERGE__VIRTUAL_BASE
;
2252 if ((merge_annotated_commits(&index
, NULL
, repo
, one
, two
,
2253 recursion_level
+ 1, &virtual_opts
)) < 0)
2256 result
= git__calloc(1, sizeof(git_annotated_commit
));
2257 GIT_ERROR_CHECK_ALLOC(result
);
2258 result
->type
= GIT_ANNOTATED_COMMIT_VIRTUAL
;
2259 result
->index
= index
;
2261 insert_head_ids(&result
->parents
, one
);
2262 insert_head_ids(&result
->parents
, two
);
2268 static int compute_base(
2269 git_annotated_commit
**out
,
2270 git_repository
*repo
,
2271 const git_annotated_commit
*one
,
2272 const git_annotated_commit
*two
,
2273 const git_merge_options
*given_opts
,
2274 size_t recursion_level
)
2276 git_array_oid_t head_ids
= GIT_ARRAY_INIT
;
2277 git_oidarray bases
= {0};
2278 git_annotated_commit
*base
= NULL
, *other
= NULL
, *new_base
= NULL
;
2279 git_merge_options opts
= GIT_MERGE_OPTIONS_INIT
;
2280 size_t i
, base_count
;
2286 memcpy(&opts
, given_opts
, sizeof(git_merge_options
));
2288 /* With more than two commits, merge_bases_many finds the base of
2289 * the first commit and a hypothetical merge of the others. Since
2290 * "one" may itself be a virtual commit, which insert_head_ids
2291 * substitutes multiple ancestors for, it needs to be added
2292 * after "two" which is always a single real commit.
2294 if ((error
= insert_head_ids(&head_ids
, two
)) < 0 ||
2295 (error
= insert_head_ids(&head_ids
, one
)) < 0 ||
2296 (error
= git_merge_bases_many(&bases
, repo
,
2297 head_ids
.size
, head_ids
.ptr
)) < 0)
2300 base_count
= (opts
.flags
& GIT_MERGE_NO_RECURSIVE
) ? 0 : bases
.count
;
2303 git_oidarray__reverse(&bases
);
2305 if ((error
= git_annotated_commit_lookup(&base
, repo
, &bases
.ids
[0])) < 0)
2308 for (i
= 1; i
< base_count
; i
++) {
2311 if (opts
.recursion_limit
&& recursion_level
> opts
.recursion_limit
)
2314 if ((error
= git_annotated_commit_lookup(&other
, repo
,
2315 &bases
.ids
[i
])) < 0 ||
2316 (error
= create_virtual_base(&new_base
, repo
, base
, other
, &opts
,
2317 recursion_level
)) < 0)
2320 git_annotated_commit_free(base
);
2321 git_annotated_commit_free(other
);
2332 git_annotated_commit_free(base
);
2334 git_annotated_commit_free(other
);
2335 git_annotated_commit_free(new_base
);
2336 git_oidarray_free(&bases
);
2337 git_array_clear(head_ids
);
2341 static int iterator_for_annotated_commit(
2343 git_annotated_commit
*commit
)
2345 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2348 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2350 if (commit
== NULL
) {
2351 error
= git_iterator_for_nothing(out
, &opts
);
2352 } else if (commit
->type
== GIT_ANNOTATED_COMMIT_VIRTUAL
) {
2353 error
= git_iterator_for_index(out
, git_index_owner(commit
->index
), commit
->index
, &opts
);
2355 if (!commit
->tree
&&
2356 (error
= git_commit_tree(&commit
->tree
, commit
->commit
)) < 0)
2359 error
= git_iterator_for_tree(out
, commit
->tree
, &opts
);
2366 static int merge_annotated_commits(
2367 git_index
**index_out
,
2368 git_annotated_commit
**base_out
,
2369 git_repository
*repo
,
2370 git_annotated_commit
*ours
,
2371 git_annotated_commit
*theirs
,
2372 size_t recursion_level
,
2373 const git_merge_options
*opts
)
2375 git_annotated_commit
*base
= NULL
;
2376 git_iterator
*base_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2379 if ((error
= compute_base(&base
, repo
, ours
, theirs
, opts
,
2380 recursion_level
)) < 0) {
2382 if (error
!= GIT_ENOTFOUND
)
2388 if ((error
= iterator_for_annotated_commit(&base_iter
, base
)) < 0 ||
2389 (error
= iterator_for_annotated_commit(&our_iter
, ours
)) < 0 ||
2390 (error
= iterator_for_annotated_commit(&their_iter
, theirs
)) < 0 ||
2391 (error
= git_merge__iterators(index_out
, repo
, base_iter
, our_iter
,
2392 their_iter
, opts
)) < 0)
2401 git_annotated_commit_free(base
);
2402 git_iterator_free(base_iter
);
2403 git_iterator_free(our_iter
);
2404 git_iterator_free(their_iter
);
2409 int git_merge_commits(
2411 git_repository
*repo
,
2412 const git_commit
*our_commit
,
2413 const git_commit
*their_commit
,
2414 const git_merge_options
*opts
)
2416 git_annotated_commit
*ours
= NULL
, *theirs
= NULL
, *base
= NULL
;
2419 if ((error
= git_annotated_commit_from_commit(&ours
, (git_commit
*)our_commit
)) < 0 ||
2420 (error
= git_annotated_commit_from_commit(&theirs
, (git_commit
*)their_commit
)) < 0)
2423 error
= merge_annotated_commits(out
, &base
, repo
, ours
, theirs
, 0, opts
);
2426 git_annotated_commit_free(ours
);
2427 git_annotated_commit_free(theirs
);
2428 git_annotated_commit_free(base
);
2432 /* Merge setup / cleanup */
2434 static int write_merge_head(
2435 git_repository
*repo
,
2436 const git_annotated_commit
*heads
[],
2439 git_filebuf file
= GIT_FILEBUF_INIT
;
2440 git_buf file_path
= GIT_BUF_INIT
;
2444 assert(repo
&& heads
);
2446 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_HEAD_FILE
)) < 0 ||
2447 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2450 for (i
= 0; i
< heads_len
; i
++) {
2451 if ((error
= git_filebuf_printf(&file
, "%s\n", heads
[i
]->id_str
)) < 0)
2455 error
= git_filebuf_commit(&file
);
2459 git_filebuf_cleanup(&file
);
2461 git_buf_dispose(&file_path
);
2466 static int write_merge_mode(git_repository
*repo
)
2468 git_filebuf file
= GIT_FILEBUF_INIT
;
2469 git_buf file_path
= GIT_BUF_INIT
;
2474 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MODE_FILE
)) < 0 ||
2475 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2478 if ((error
= git_filebuf_write(&file
, "no-ff", 5)) < 0)
2481 error
= git_filebuf_commit(&file
);
2485 git_filebuf_cleanup(&file
);
2487 git_buf_dispose(&file_path
);
2492 struct merge_msg_entry
{
2493 const git_annotated_commit
*merge_head
;
2497 static int msg_entry_is_branch(
2498 const struct merge_msg_entry
*entry
,
2499 git_vector
*entries
)
2501 GIT_UNUSED(entries
);
2503 return (entry
->written
== 0 &&
2504 entry
->merge_head
->remote_url
== NULL
&&
2505 entry
->merge_head
->ref_name
!= NULL
&&
2506 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0);
2509 static int msg_entry_is_tracking(
2510 const struct merge_msg_entry
*entry
,
2511 git_vector
*entries
)
2513 GIT_UNUSED(entries
);
2515 return (entry
->written
== 0 &&
2516 entry
->merge_head
->remote_url
== NULL
&&
2517 entry
->merge_head
->ref_name
!= NULL
&&
2518 git__strncmp(GIT_REFS_REMOTES_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_REMOTES_DIR
)) == 0);
2521 static int msg_entry_is_tag(
2522 const struct merge_msg_entry
*entry
,
2523 git_vector
*entries
)
2525 GIT_UNUSED(entries
);
2527 return (entry
->written
== 0 &&
2528 entry
->merge_head
->remote_url
== NULL
&&
2529 entry
->merge_head
->ref_name
!= NULL
&&
2530 git__strncmp(GIT_REFS_TAGS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_TAGS_DIR
)) == 0);
2533 static int msg_entry_is_remote(
2534 const struct merge_msg_entry
*entry
,
2535 git_vector
*entries
)
2537 if (entry
->written
== 0 &&
2538 entry
->merge_head
->remote_url
!= NULL
&&
2539 entry
->merge_head
->ref_name
!= NULL
&&
2540 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0)
2542 struct merge_msg_entry
*existing
;
2544 /* Match only branches from the same remote */
2545 if (entries
->length
== 0)
2548 existing
= git_vector_get(entries
, 0);
2550 return (git__strcmp(existing
->merge_head
->remote_url
,
2551 entry
->merge_head
->remote_url
) == 0);
2557 static int msg_entry_is_oid(
2558 const struct merge_msg_entry
*merge_msg_entry
)
2560 return (merge_msg_entry
->written
== 0 &&
2561 merge_msg_entry
->merge_head
->ref_name
== NULL
&&
2562 merge_msg_entry
->merge_head
->remote_url
== NULL
);
2565 static int merge_msg_entry_written(
2566 const struct merge_msg_entry
*merge_msg_entry
)
2568 return (merge_msg_entry
->written
== 1);
2571 static int merge_msg_entries(
2573 const struct merge_msg_entry
*entries
,
2575 int (*match
)(const struct merge_msg_entry
*entry
, git_vector
*entries
))
2578 int matches
, total
= 0;
2580 git_vector_clear(v
);
2582 for (i
= 0; i
< len
; i
++) {
2583 if ((matches
= match(&entries
[i
], v
)) < 0)
2588 git_vector_insert(v
, (struct merge_msg_entry
*)&entries
[i
]);
2595 static int merge_msg_write_entries(
2597 git_vector
*entries
,
2598 const char *item_name
,
2599 const char *item_plural_name
,
2600 size_t ref_name_skip
,
2604 struct merge_msg_entry
*entry
;
2608 if (entries
->length
== 0)
2611 if (sep
&& (error
= git_filebuf_printf(file
, "%c ", sep
)) < 0)
2614 if ((error
= git_filebuf_printf(file
, "%s ",
2615 (entries
->length
== 1) ? item_name
: item_plural_name
)) < 0)
2618 git_vector_foreach(entries
, i
, entry
) {
2620 (error
= git_filebuf_printf(file
, "%s", (i
== entries
->length
- 1) ? " and " : ", ")) < 0)
2623 if ((error
= git_filebuf_printf(file
, "'%s'", entry
->merge_head
->ref_name
+ ref_name_skip
)) < 0)
2630 error
= git_filebuf_printf(file
, " of %s", source
);
2636 static int merge_msg_write_branches(
2638 git_vector
*entries
,
2641 return merge_msg_write_entries(file
, entries
,
2642 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), NULL
, sep
);
2645 static int merge_msg_write_tracking(
2647 git_vector
*entries
,
2650 return merge_msg_write_entries(file
, entries
,
2651 "remote-tracking branch", "remote-tracking branches", 0, NULL
, sep
);
2654 static int merge_msg_write_tags(
2656 git_vector
*entries
,
2659 return merge_msg_write_entries(file
, entries
,
2660 "tag", "tags", strlen(GIT_REFS_TAGS_DIR
), NULL
, sep
);
2663 static int merge_msg_write_remotes(
2665 git_vector
*entries
,
2670 if (entries
->length
== 0)
2673 source
= ((struct merge_msg_entry
*)entries
->contents
[0])->merge_head
->remote_url
;
2675 return merge_msg_write_entries(file
, entries
,
2676 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), source
, sep
);
2679 static int write_merge_msg(
2680 git_repository
*repo
,
2681 const git_annotated_commit
*heads
[],
2684 git_filebuf file
= GIT_FILEBUF_INIT
;
2685 git_buf file_path
= GIT_BUF_INIT
;
2686 struct merge_msg_entry
*entries
;
2687 git_vector matching
= GIT_VECTOR_INIT
;
2692 assert(repo
&& heads
);
2694 entries
= git__calloc(heads_len
, sizeof(struct merge_msg_entry
));
2695 GIT_ERROR_CHECK_ALLOC(entries
);
2697 if (git_vector_init(&matching
, heads_len
, NULL
) < 0) {
2702 for (i
= 0; i
< heads_len
; i
++)
2703 entries
[i
].merge_head
= heads
[i
];
2705 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
2706 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0 ||
2707 (error
= git_filebuf_write(&file
, "Merge ", 6)) < 0)
2711 * This is to emulate the format of MERGE_MSG by core git.
2713 * Core git will write all the commits specified by OID, in the order
2714 * provided, until the first named branch or tag is reached, at which
2715 * point all branches will be written in the order provided, then all
2716 * tags, then all remote tracking branches and finally all commits that
2717 * were specified by OID that were not already written.
2721 for (i
= 0; i
< heads_len
; i
++) {
2722 if (!msg_entry_is_oid(&entries
[i
]))
2725 if ((error
= git_filebuf_printf(&file
,
2726 "%scommit '%s'", (i
> 0) ? "; " : "",
2727 entries
[i
].merge_head
->id_str
)) < 0)
2730 entries
[i
].written
= 1;
2736 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_branch
)) < 0 ||
2737 (error
= merge_msg_write_branches(&file
, &matching
, sep
)) < 0)
2740 if (matching
.length
)
2743 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tracking
)) < 0 ||
2744 (error
= merge_msg_write_tracking(&file
, &matching
, sep
)) < 0)
2747 if (matching
.length
)
2750 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tag
)) < 0 ||
2751 (error
= merge_msg_write_tags(&file
, &matching
, sep
)) < 0)
2754 if (matching
.length
)
2757 /* We should never be called with multiple remote branches, but handle
2758 * it in case we are... */
2759 while ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_remote
)) > 0) {
2760 if ((error
= merge_msg_write_remotes(&file
, &matching
, sep
)) < 0)
2763 if (matching
.length
)
2770 for (i
= 0; i
< heads_len
; i
++) {
2771 if (merge_msg_entry_written(&entries
[i
]))
2774 if ((error
= git_filebuf_printf(&file
, "; commit '%s'",
2775 entries
[i
].merge_head
->id_str
)) < 0)
2779 if ((error
= git_filebuf_printf(&file
, "\n")) < 0 ||
2780 (error
= git_filebuf_commit(&file
)) < 0)
2785 git_filebuf_cleanup(&file
);
2787 git_buf_dispose(&file_path
);
2789 git_vector_free(&matching
);
2795 int git_merge__setup(
2796 git_repository
*repo
,
2797 const git_annotated_commit
*our_head
,
2798 const git_annotated_commit
*heads
[],
2803 assert (repo
&& our_head
&& heads
);
2805 if ((error
= git_repository__set_orig_head(repo
, git_annotated_commit_id(our_head
))) == 0 &&
2806 (error
= write_merge_head(repo
, heads
, heads_len
)) == 0 &&
2807 (error
= write_merge_mode(repo
)) == 0) {
2808 error
= write_merge_msg(repo
, heads
, heads_len
);
2814 /* Merge branches */
2816 static int merge_ancestor_head(
2817 git_annotated_commit
**ancestor_head
,
2818 git_repository
*repo
,
2819 const git_annotated_commit
*our_head
,
2820 const git_annotated_commit
**their_heads
,
2821 size_t their_heads_len
)
2823 git_oid
*oids
, ancestor_oid
;
2824 size_t i
, alloc_len
;
2827 assert(repo
&& our_head
&& their_heads
);
2829 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len
, their_heads_len
, 1);
2830 oids
= git__calloc(alloc_len
, sizeof(git_oid
));
2831 GIT_ERROR_CHECK_ALLOC(oids
);
2833 git_oid_cpy(&oids
[0], git_commit_id(our_head
->commit
));
2835 for (i
= 0; i
< their_heads_len
; i
++)
2836 git_oid_cpy(&oids
[i
+ 1], git_annotated_commit_id(their_heads
[i
]));
2838 if ((error
= git_merge_base_many(&ancestor_oid
, repo
, their_heads_len
+ 1, oids
)) < 0)
2841 error
= git_annotated_commit_lookup(ancestor_head
, repo
, &ancestor_oid
);
2848 static const char *merge_their_label(const char *branchname
)
2852 if ((slash
= strrchr(branchname
, '/')) == NULL
)
2855 if (*(slash
+1) == '\0')
2861 static int merge_normalize_checkout_opts(
2862 git_checkout_options
*out
,
2863 git_repository
*repo
,
2864 const git_checkout_options
*given_checkout_opts
,
2865 unsigned int checkout_strategy
,
2866 git_annotated_commit
*ancestor
,
2867 const git_annotated_commit
*our_head
,
2868 const git_annotated_commit
**their_heads
,
2869 size_t their_heads_len
)
2871 git_checkout_options default_checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
2876 if (given_checkout_opts
!= NULL
)
2877 memcpy(out
, given_checkout_opts
, sizeof(git_checkout_options
));
2879 memcpy(out
, &default_checkout_opts
, sizeof(git_checkout_options
));
2881 out
->checkout_strategy
= checkout_strategy
;
2883 if (!out
->ancestor_label
) {
2884 if (ancestor
&& ancestor
->type
== GIT_ANNOTATED_COMMIT_REAL
)
2885 out
->ancestor_label
= git_commit_summary(ancestor
->commit
);
2887 out
->ancestor_label
= "merged common ancestors";
2889 out
->ancestor_label
= "empty base";
2892 if (!out
->our_label
) {
2893 if (our_head
&& our_head
->ref_name
)
2894 out
->our_label
= our_head
->ref_name
;
2896 out
->our_label
= "ours";
2899 if (!out
->their_label
) {
2900 if (their_heads_len
== 1 && their_heads
[0]->ref_name
)
2901 out
->their_label
= merge_their_label(their_heads
[0]->ref_name
);
2902 else if (their_heads_len
== 1)
2903 out
->their_label
= their_heads
[0]->id_str
;
2905 out
->their_label
= "theirs";
2911 static int merge_check_index(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
2913 git_tree
*head_tree
= NULL
;
2914 git_index
*index_repo
= NULL
;
2915 git_iterator
*iter_repo
= NULL
, *iter_new
= NULL
;
2916 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2917 git_diff
*staged_diff_list
= NULL
, *index_diff_list
= NULL
;
2918 git_diff_delta
*delta
;
2919 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
2920 git_vector staged_paths
= GIT_VECTOR_INIT
;
2924 GIT_UNUSED(merged_paths
);
2928 /* No staged changes may exist unless the change staged is identical to
2929 * the result of the merge. This allows one to apply to merge manually,
2930 * then run merge. Any other staged change would be overwritten by
2933 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
2934 (error
= git_repository_index(&index_repo
, repo
)) < 0 ||
2935 (error
= git_diff_tree_to_index(&staged_diff_list
, repo
, head_tree
, index_repo
, &opts
)) < 0)
2938 if (staged_diff_list
->deltas
.length
== 0)
2941 git_vector_foreach(&staged_diff_list
->deltas
, i
, delta
) {
2942 if ((error
= git_vector_insert(&staged_paths
, (char *)delta
->new_file
.path
)) < 0)
2946 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2947 iter_opts
.pathlist
.strings
= (char **)staged_paths
.contents
;
2948 iter_opts
.pathlist
.count
= staged_paths
.length
;
2950 if ((error
= git_iterator_for_index(&iter_repo
, repo
, index_repo
, &iter_opts
)) < 0 ||
2951 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
2952 (error
= git_diff__from_iterators(&index_diff_list
, repo
, iter_repo
, iter_new
, &opts
)) < 0)
2955 *conflicts
= index_diff_list
->deltas
.length
;
2958 git_tree_free(head_tree
);
2959 git_index_free(index_repo
);
2960 git_iterator_free(iter_repo
);
2961 git_iterator_free(iter_new
);
2962 git_diff_free(staged_diff_list
);
2963 git_diff_free(index_diff_list
);
2964 git_vector_free(&staged_paths
);
2969 static int merge_check_workdir(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
2971 git_diff
*wd_diff_list
= NULL
;
2972 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
2975 GIT_UNUSED(index_new
);
2979 /* We need to have merged at least 1 file for the possibility to exist to
2980 * have conflicts with the workdir. Passing 0 as the pathspec count paramter
2981 * will consider all files in the working directory, that is, we may detect
2982 * a conflict if there were untracked files in the workdir prior to starting
2983 * the merge. This typically happens when cherry-picking a commmit whose
2984 * changes have already been applied.
2986 if (merged_paths
->length
== 0)
2989 opts
.flags
|= GIT_DIFF_INCLUDE_UNTRACKED
;
2991 /* Workdir changes may exist iff they do not conflict with changes that
2992 * will be applied by the merge (including conflicts). Ensure that there
2993 * are no changes in the workdir to these paths.
2995 opts
.flags
|= GIT_DIFF_DISABLE_PATHSPEC_MATCH
;
2996 opts
.pathspec
.count
= merged_paths
->length
;
2997 opts
.pathspec
.strings
= (char **)merged_paths
->contents
;
2998 opts
.ignore_submodules
= GIT_SUBMODULE_IGNORE_ALL
;
3000 if ((error
= git_diff_index_to_workdir(&wd_diff_list
, repo
, NULL
, &opts
)) < 0)
3003 *conflicts
= wd_diff_list
->deltas
.length
;
3006 git_diff_free(wd_diff_list
);
3011 int git_merge__check_result(git_repository
*repo
, git_index
*index_new
)
3013 git_tree
*head_tree
= NULL
;
3014 git_iterator
*iter_head
= NULL
, *iter_new
= NULL
;
3015 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
3016 git_diff
*merged_list
= NULL
;
3017 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
3018 git_diff_delta
*delta
;
3019 git_vector paths
= GIT_VECTOR_INIT
;
3020 size_t i
, index_conflicts
= 0, wd_conflicts
= 0, conflicts
;
3021 const git_index_entry
*e
;
3024 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
3026 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
3027 (error
= git_iterator_for_tree(&iter_head
, head_tree
, &iter_opts
)) < 0 ||
3028 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
3029 (error
= git_diff__from_iterators(&merged_list
, repo
, iter_head
, iter_new
, &opts
)) < 0)
3032 git_vector_foreach(&merged_list
->deltas
, i
, delta
) {
3033 if ((error
= git_vector_insert(&paths
, (char *)delta
->new_file
.path
)) < 0)
3037 for (i
= 0; i
< git_index_entrycount(index_new
); i
++) {
3038 e
= git_index_get_byindex(index_new
, i
);
3040 if (git_index_entry_is_conflict(e
) &&
3041 (git_vector_last(&paths
) == NULL
||
3042 strcmp(git_vector_last(&paths
), e
->path
) != 0)) {
3044 if ((error
= git_vector_insert(&paths
, (char *)e
->path
)) < 0)
3049 /* Make sure the index and workdir state do not prevent merging */
3050 if ((error
= merge_check_index(&index_conflicts
, repo
, index_new
, &paths
)) < 0 ||
3051 (error
= merge_check_workdir(&wd_conflicts
, repo
, index_new
, &paths
)) < 0)
3054 if ((conflicts
= index_conflicts
+ wd_conflicts
) > 0) {
3055 git_error_set(GIT_ERROR_MERGE
, "%" PRIuZ
" uncommitted change%s would be overwritten by merge",
3056 conflicts
, (conflicts
!= 1) ? "s" : "");
3057 error
= GIT_ECONFLICT
;
3061 git_vector_free(&paths
);
3062 git_tree_free(head_tree
);
3063 git_iterator_free(iter_head
);
3064 git_iterator_free(iter_new
);
3065 git_diff_free(merged_list
);
3070 int git_merge__append_conflicts_to_merge_msg(
3071 git_repository
*repo
,
3074 git_filebuf file
= GIT_FILEBUF_INIT
;
3075 git_buf file_path
= GIT_BUF_INIT
;
3076 const char *last
= NULL
;
3080 if (!git_index_has_conflicts(index
))
3083 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
3084 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_APPEND
, GIT_MERGE_FILE_MODE
)) < 0)
3087 git_filebuf_printf(&file
, "\nConflicts:\n");
3089 for (i
= 0; i
< git_index_entrycount(index
); i
++) {
3090 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
3092 if (!git_index_entry_is_conflict(e
))
3095 if (last
== NULL
|| strcmp(e
->path
, last
) != 0)
3096 git_filebuf_printf(&file
, "\t%s\n", e
->path
);
3101 error
= git_filebuf_commit(&file
);
3105 git_filebuf_cleanup(&file
);
3107 git_buf_dispose(&file_path
);
3112 static int merge_state_cleanup(git_repository
*repo
)
3114 const char *state_files
[] = {
3115 GIT_MERGE_HEAD_FILE
,
3116 GIT_MERGE_MODE_FILE
,
3120 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
3123 static int merge_heads(
3124 git_annotated_commit
**ancestor_head_out
,
3125 git_annotated_commit
**our_head_out
,
3126 git_repository
*repo
,
3127 git_reference
*our_ref
,
3128 const git_annotated_commit
**their_heads
,
3129 size_t their_heads_len
)
3131 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3134 *ancestor_head_out
= NULL
;
3135 *our_head_out
= NULL
;
3137 if ((error
= git_annotated_commit_from_ref(&our_head
, repo
, our_ref
)) < 0)
3140 if ((error
= merge_ancestor_head(&ancestor_head
, repo
, our_head
, their_heads
, their_heads_len
)) < 0) {
3141 if (error
!= GIT_ENOTFOUND
)
3148 *ancestor_head_out
= ancestor_head
;
3149 *our_head_out
= our_head
;
3153 git_annotated_commit_free(ancestor_head
);
3154 git_annotated_commit_free(our_head
);
3160 static int merge_preference(git_merge_preference_t
*out
, git_repository
*repo
)
3164 int bool_value
, error
= 0;
3166 *out
= GIT_MERGE_PREFERENCE_NONE
;
3168 if ((error
= git_repository_config_snapshot(&config
, repo
)) < 0)
3171 if ((error
= git_config_get_string(&value
, config
, "merge.ff")) < 0) {
3172 if (error
== GIT_ENOTFOUND
) {
3180 if (git_config_parse_bool(&bool_value
, value
) == 0) {
3182 *out
|= GIT_MERGE_PREFERENCE_NO_FASTFORWARD
;
3184 if (strcasecmp(value
, "only") == 0)
3185 *out
|= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
;
3189 git_config_free(config
);
3193 int git_merge_analysis_for_ref(
3194 git_merge_analysis_t
*analysis_out
,
3195 git_merge_preference_t
*preference_out
,
3196 git_repository
*repo
,
3197 git_reference
*our_ref
,
3198 const git_annotated_commit
**their_heads
,
3199 size_t their_heads_len
)
3201 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3205 assert(analysis_out
&& preference_out
&& repo
&& their_heads
&& their_heads_len
> 0);
3207 if (their_heads_len
!= 1) {
3208 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3213 *analysis_out
= GIT_MERGE_ANALYSIS_NONE
;
3215 if ((error
= merge_preference(preference_out
, repo
)) < 0)
3218 if ((error
= git_reference__is_unborn_head(&unborn
, our_ref
, repo
)) < 0)
3222 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_UNBORN
;
3227 if ((error
= merge_heads(&ancestor_head
, &our_head
, repo
, our_ref
, their_heads
, their_heads_len
)) < 0)
3230 /* We're up-to-date if we're trying to merge our own common ancestor. */
3231 if (ancestor_head
&& git_oid_equal(
3232 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(their_heads
[0])))
3233 *analysis_out
|= GIT_MERGE_ANALYSIS_UP_TO_DATE
;
3235 /* We're fastforwardable if we're our own common ancestor. */
3236 else if (ancestor_head
&& git_oid_equal(
3237 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(our_head
)))
3238 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_NORMAL
;
3240 /* Otherwise, just a normal merge is possible. */
3242 *analysis_out
|= GIT_MERGE_ANALYSIS_NORMAL
;
3245 git_annotated_commit_free(ancestor_head
);
3246 git_annotated_commit_free(our_head
);
3250 int git_merge_analysis(
3251 git_merge_analysis_t
*analysis_out
,
3252 git_merge_preference_t
*preference_out
,
3253 git_repository
*repo
,
3254 const git_annotated_commit
**their_heads
,
3255 size_t their_heads_len
)
3257 git_reference
*head_ref
= NULL
;
3260 if ((error
= git_reference_lookup(&head_ref
, repo
, GIT_HEAD_FILE
)) < 0) {
3261 git_error_set(GIT_ERROR_MERGE
, "failed to lookup HEAD reference");
3265 error
= git_merge_analysis_for_ref(analysis_out
, preference_out
, repo
, head_ref
, their_heads
, their_heads_len
);
3267 git_reference_free(head_ref
);
3273 git_repository
*repo
,
3274 const git_annotated_commit
**their_heads
,
3275 size_t their_heads_len
,
3276 const git_merge_options
*merge_opts
,
3277 const git_checkout_options
*given_checkout_opts
)
3279 git_reference
*our_ref
= NULL
;
3280 git_checkout_options checkout_opts
;
3281 git_annotated_commit
*our_head
= NULL
, *base
= NULL
;
3282 git_index
*repo_index
= NULL
, *index
= NULL
;
3283 git_indexwriter indexwriter
= GIT_INDEXWRITER_INIT
;
3284 unsigned int checkout_strategy
;
3287 assert(repo
&& their_heads
&& their_heads_len
> 0);
3289 if (their_heads_len
!= 1) {
3290 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3294 if ((error
= git_repository__ensure_not_bare(repo
, "merge")) < 0)
3297 checkout_strategy
= given_checkout_opts
?
3298 given_checkout_opts
->checkout_strategy
:
3301 if ((error
= git_indexwriter_init_for_operation(&indexwriter
, repo
,
3302 &checkout_strategy
)) < 0)
3305 if ((error
= git_repository_index(&repo_index
, repo
) < 0) ||
3306 (error
= git_index_read(repo_index
, 0) < 0))
3309 /* Write the merge setup files to the repository. */
3310 if ((error
= git_annotated_commit_from_head(&our_head
, repo
)) < 0 ||
3311 (error
= git_merge__setup(repo
, our_head
, their_heads
,
3312 their_heads_len
)) < 0)
3317 if ((error
= merge_annotated_commits(&index
, &base
, repo
, our_head
,
3318 (git_annotated_commit
*)their_heads
[0], 0, merge_opts
)) < 0 ||
3319 (error
= git_merge__check_result(repo
, index
)) < 0 ||
3320 (error
= git_merge__append_conflicts_to_merge_msg(repo
, index
)) < 0)
3323 /* check out the merge results */
3325 if ((error
= merge_normalize_checkout_opts(&checkout_opts
, repo
,
3326 given_checkout_opts
, checkout_strategy
,
3327 base
, our_head
, their_heads
, their_heads_len
)) < 0 ||
3328 (error
= git_checkout_index(repo
, index
, &checkout_opts
)) < 0)
3331 error
= git_indexwriter_commit(&indexwriter
);
3335 merge_state_cleanup(repo
);
3337 git_indexwriter_cleanup(&indexwriter
);
3338 git_index_free(index
);
3339 git_annotated_commit_free(our_head
);
3340 git_annotated_commit_free(base
);
3341 git_reference_free(our_ref
);
3342 git_index_free(repo_index
);
3347 int git_merge_options_init(git_merge_options
*opts
, unsigned int version
)
3349 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3350 opts
, version
, git_merge_options
, GIT_MERGE_OPTIONS_INIT
);
3354 #ifndef GIT_DEPRECATE_HARD
3355 int git_merge_init_options(git_merge_options
*opts
, unsigned int version
)
3357 return git_merge_options_init(opts
, version
);
3361 int git_merge_file_input_init(git_merge_file_input
*input
, unsigned int version
)
3363 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3364 input
, version
, git_merge_file_input
, GIT_MERGE_FILE_INPUT_INIT
);
3368 #ifndef GIT_DEPRECATE_HARD
3369 int git_merge_file_init_input(git_merge_file_input
*input
, unsigned int version
)
3371 return git_merge_file_input_init(input
, version
);
3375 int git_merge_file_options_init(
3376 git_merge_file_options
*opts
, unsigned int version
)
3378 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3379 opts
, version
, git_merge_file_options
, GIT_MERGE_FILE_OPTIONS_INIT
);
3383 #ifndef GIT_DEPRECATE_HARD
3384 int git_merge_file_init_options(
3385 git_merge_file_options
*opts
, unsigned int version
)
3387 return git_merge_file_options_init(opts
, version
);