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) < 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
;
143 GIT_ASSERT_ARG(repo
);
144 GIT_ASSERT_ARG(input_array
);
146 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
149 git_oid_cpy(out
, &result
->item
->oid
);
151 git_commit_list_free(&result
);
152 git_revwalk_free(walk
);
157 int git_merge_bases_many(git_oidarray
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
160 git_commit_list
*list
, *result
= NULL
;
162 git_array_oid_t array
;
165 GIT_ASSERT_ARG(repo
);
166 GIT_ASSERT_ARG(input_array
);
168 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
171 git_array_init(array
);
175 git_oid
*id
= git_array_alloc(array
);
181 git_oid_cpy(id
, &list
->item
->oid
);
185 git_oidarray__from_array(out
, &array
);
188 git_commit_list_free(&result
);
189 git_revwalk_free(walk
);
194 int git_merge_base_octopus(git_oid
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
201 GIT_ASSERT_ARG(repo
);
202 GIT_ASSERT_ARG(input_array
);
205 git_error_set(GIT_ERROR_INVALID
, "at least two commits are required to find an ancestor");
209 result
= input_array
[0];
210 for (i
= 1; i
< length
; i
++) {
211 error
= git_merge_base(&result
, repo
, &result
, &input_array
[i
]);
221 static int merge_bases(git_commit_list
**out
, git_revwalk
**walk_out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
225 git_commit_list
*result
= NULL
;
226 git_commit_list_node
*commit
;
229 if (git_revwalk_new(&walk
, repo
) < 0)
232 commit
= git_revwalk__commit_lookup(walk
, two
);
236 /* This is just one value, so we can do it on the stack */
237 memset(&list
, 0x0, sizeof(git_vector
));
238 contents
[0] = commit
;
240 list
.contents
= contents
;
242 commit
= git_revwalk__commit_lookup(walk
, one
);
246 if (git_merge__bases_many(&result
, walk
, commit
, &list
, 0) < 0)
250 git_revwalk_free(walk
);
251 git_error_set(GIT_ERROR_MERGE
, "no merge base found");
252 return GIT_ENOTFOUND
;
261 git_revwalk_free(walk
);
266 int git_merge_base(git_oid
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
270 git_commit_list
*result
;
272 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
275 git_oid_cpy(out
, &result
->item
->oid
);
276 git_commit_list_free(&result
);
277 git_revwalk_free(walk
);
282 int git_merge_bases(git_oidarray
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
286 git_commit_list
*result
, *list
;
287 git_array_oid_t array
;
289 git_array_init(array
);
291 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
296 git_oid
*id
= git_array_alloc(array
);
300 git_oid_cpy(id
, &list
->item
->oid
);
304 git_oidarray__from_array(out
, &array
);
305 git_commit_list_free(&result
);
306 git_revwalk_free(walk
);
311 git_commit_list_free(&result
);
312 git_revwalk_free(walk
);
316 static int interesting(git_pqueue
*list
)
320 for (i
= 0; i
< git_pqueue_size(list
); i
++) {
321 git_commit_list_node
*commit
= git_pqueue_get(list
, i
);
322 if ((commit
->flags
& STALE
) == 0)
329 static int clear_commit_marks_1(git_commit_list
**plist
,
330 git_commit_list_node
*commit
, unsigned int mark
)
335 if (!(mark
& commit
->flags
))
338 commit
->flags
&= ~mark
;
340 for (i
= 1; i
< commit
->out_degree
; i
++) {
341 git_commit_list_node
*p
= commit
->parents
[i
];
342 if (git_commit_list_insert(p
, plist
) == NULL
)
346 commit
= commit
->out_degree
? commit
->parents
[0] : NULL
;
352 static int clear_commit_marks_many(git_vector
*commits
, unsigned int mark
)
354 git_commit_list
*list
= NULL
;
355 git_commit_list_node
*c
;
358 git_vector_foreach(commits
, i
, c
) {
359 if (git_commit_list_insert(c
, &list
) == NULL
)
364 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
369 static int clear_commit_marks(git_commit_list_node
*commit
, unsigned int mark
)
371 git_commit_list
*list
= NULL
;
372 if (git_commit_list_insert(commit
, &list
) == NULL
)
375 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
380 static int paint_down_to_common(
381 git_commit_list
**out
,
383 git_commit_list_node
*one
,
385 uint32_t minimum_generation
)
388 git_commit_list
*result
= NULL
;
389 git_commit_list_node
*two
;
394 if (git_pqueue_init(&list
, 0, twos
->length
* 2, git_commit_list_generation_cmp
) < 0)
397 one
->flags
|= PARENT1
;
398 if (git_pqueue_insert(&list
, one
) < 0)
401 git_vector_foreach(twos
, i
, two
) {
402 if (git_commit_list_parse(walk
, two
) < 0)
405 two
->flags
|= PARENT2
;
406 if (git_pqueue_insert(&list
, two
) < 0)
410 /* as long as there are non-STALE commits */
411 while (interesting(&list
)) {
412 git_commit_list_node
*commit
= git_pqueue_pop(&list
);
418 flags
= commit
->flags
& (PARENT1
| PARENT2
| STALE
);
419 if (flags
== (PARENT1
| PARENT2
)) {
420 if (!(commit
->flags
& RESULT
)) {
421 commit
->flags
|= RESULT
;
422 if (git_commit_list_insert(commit
, &result
) == NULL
)
425 /* we mark the parents of a merge stale */
429 for (i
= 0; i
< commit
->out_degree
; i
++) {
430 git_commit_list_node
*p
= commit
->parents
[i
];
431 if ((p
->flags
& flags
) == flags
)
433 if (p
->generation
< minimum_generation
)
436 if ((error
= git_commit_list_parse(walk
, p
)) < 0)
440 if (git_pqueue_insert(&list
, p
) < 0)
445 git_pqueue_free(&list
);
450 static int remove_redundant(git_revwalk
*walk
, git_vector
*commits
, uint32_t minimum_generation
)
452 git_vector work
= GIT_VECTOR_INIT
;
453 unsigned char *redundant
;
454 unsigned int *filled_index
;
458 redundant
= git__calloc(commits
->length
, 1);
459 GIT_ERROR_CHECK_ALLOC(redundant
);
460 filled_index
= git__calloc((commits
->length
- 1), sizeof(unsigned int));
461 GIT_ERROR_CHECK_ALLOC(filled_index
);
463 for (i
= 0; i
< commits
->length
; ++i
) {
464 if ((error
= git_commit_list_parse(walk
, commits
->contents
[i
])) < 0)
468 for (i
= 0; i
< commits
->length
; ++i
) {
469 git_commit_list
*common
= NULL
;
470 git_commit_list_node
*commit
= commits
->contents
[i
];
475 git_vector_clear(&work
);
477 for (j
= 0; j
< commits
->length
; j
++) {
478 if (i
== j
|| redundant
[j
])
481 filled_index
[work
.length
] = j
;
482 if ((error
= git_vector_insert(&work
, commits
->contents
[j
])) < 0)
486 error
= paint_down_to_common(&common
, walk
, commit
, &work
, minimum_generation
);
490 if (commit
->flags
& PARENT2
)
493 for (j
= 0; j
< work
.length
; j
++) {
494 git_commit_list_node
*w
= work
.contents
[j
];
495 if (w
->flags
& PARENT1
)
496 redundant
[filled_index
[j
]] = 1;
499 git_commit_list_free(&common
);
501 if ((error
= clear_commit_marks(commit
, ALL_FLAGS
)) < 0 ||
502 (error
= clear_commit_marks_many(&work
, ALL_FLAGS
)) < 0)
506 for (i
= 0; i
< commits
->length
; ++i
) {
508 commits
->contents
[i
] = NULL
;
512 git__free(redundant
);
513 git__free(filled_index
);
514 git_vector_free(&work
);
518 int git_merge__bases_many(
519 git_commit_list
**out
,
521 git_commit_list_node
*one
,
523 uint32_t minimum_generation
)
527 git_commit_list_node
*two
;
528 git_commit_list
*result
= NULL
, *tmp
= NULL
;
530 /* If there's only the one commit, there can be no merge bases */
531 if (twos
->length
== 0) {
536 /* if the commit is repeated, we have a our merge base already */
537 git_vector_foreach(twos
, i
, two
) {
539 return git_commit_list_insert(one
, out
) ? 0 : -1;
542 if (git_commit_list_parse(walk
, one
) < 0)
545 error
= paint_down_to_common(&result
, walk
, one
, twos
, minimum_generation
);
549 /* filter out any stale commits in the results */
554 git_commit_list_node
*c
= git_commit_list_pop(&tmp
);
555 if (!(c
->flags
& STALE
))
556 if (git_commit_list_insert_by_date(c
, &result
) == NULL
)
561 * more than one merge base -- see if there are redundant merge
562 * bases and remove them
564 if (result
&& result
->next
) {
565 git_vector redundant
= GIT_VECTOR_INIT
;
568 git_vector_insert(&redundant
, git_commit_list_pop(&result
));
570 if ((error
= clear_commit_marks(one
, ALL_FLAGS
)) < 0 ||
571 (error
= clear_commit_marks_many(twos
, ALL_FLAGS
)) < 0 ||
572 (error
= remove_redundant(walk
, &redundant
, minimum_generation
)) < 0) {
573 git_vector_free(&redundant
);
577 git_vector_foreach(&redundant
, i
, two
) {
579 git_commit_list_insert_by_date(two
, &result
);
582 git_vector_free(&redundant
);
589 int git_repository_mergehead_foreach(
590 git_repository
*repo
,
591 git_repository_mergehead_foreach_cb cb
,
594 git_buf merge_head_path
= GIT_BUF_INIT
, merge_head_file
= GIT_BUF_INIT
;
600 GIT_ASSERT_ARG(repo
);
603 if ((error
= git_buf_joinpath(&merge_head_path
, repo
->gitdir
,
604 GIT_MERGE_HEAD_FILE
)) < 0)
607 if ((error
= git_futils_readbuffer(&merge_head_file
,
608 git_buf_cstr(&merge_head_path
))) < 0)
611 buffer
= merge_head_file
.ptr
;
613 while ((line
= git__strsep(&buffer
, "\n")) != NULL
) {
614 if (strlen(line
) != GIT_OID_HEXSZ
) {
615 git_error_set(GIT_ERROR_INVALID
, "unable to parse OID - invalid length");
620 if ((error
= git_oid_fromstr(&oid
, line
)) < 0)
623 if ((error
= cb(&oid
, payload
)) != 0) {
624 git_error_set_after_callback(error
);
632 git_error_set(GIT_ERROR_MERGE
, "no EOL at line %"PRIuZ
, line_num
);
638 git_buf_dispose(&merge_head_path
);
639 git_buf_dispose(&merge_head_file
);
644 GIT_INLINE(int) index_entry_cmp(const git_index_entry
*a
, const git_index_entry
*b
)
649 return (b
->path
== NULL
) ? 0 : 1;
651 if ((value
= a
->mode
- b
->mode
) == 0 &&
652 (value
= git_oid__cmp(&a
->id
, &b
->id
)) == 0)
653 value
= strcmp(a
->path
, b
->path
);
658 /* Conflict resolution */
660 static int merge_conflict_resolve_trivial(
662 git_merge_diff_list
*diff_list
,
663 const git_merge_diff
*conflict
)
665 int ours_empty
, theirs_empty
;
666 int ours_changed
, theirs_changed
, ours_theirs_differ
;
667 git_index_entry
const *result
= NULL
;
670 GIT_ASSERT_ARG(resolved
);
671 GIT_ASSERT_ARG(diff_list
);
672 GIT_ASSERT_ARG(conflict
);
676 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
677 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
680 if (conflict
->our_status
== GIT_DELTA_RENAMED
||
681 conflict
->their_status
== GIT_DELTA_RENAMED
)
684 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
685 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
687 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
688 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
689 ours_theirs_differ
= ours_changed
&& theirs_changed
&&
690 index_entry_cmp(&conflict
->our_entry
, &conflict
->their_entry
);
693 * Note: with only one ancestor, some cases are not distinct:
695 * 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge
696 * 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge
697 * 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge
699 * Note that the two cases that take D/F conflicts into account
700 * specifically do not need to be explicitly tested, as D/F conflicts
701 * would fail the *empty* test:
703 * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
704 * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
706 * Note that many of these cases need not be explicitly tested, as
707 * they simply degrade to "all different" cases (eg, 11):
709 * 4: ancest:(empty)^, head:head, remote:remote = result:no merge
710 * 7: ancest:ancest+, head:(empty), remote:remote = result:no merge
711 * 9: ancest:ancest+, head:head, remote:(empty) = result:no merge
712 * 11: ancest:ancest+, head:head, remote:remote = result:no merge
715 /* 5ALT: ancest:*, head:head, remote:head = result:head */
716 if (ours_changed
&& !ours_empty
&& !ours_theirs_differ
)
717 result
= &conflict
->our_entry
;
718 /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
719 else if (ours_changed
&& ours_empty
&& theirs_empty
)
721 /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
722 else if (ours_empty
&& !theirs_changed
)
724 /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
725 else if (!ours_changed
&& theirs_empty
)
727 /* 13: ancest:ancest+, head:head, remote:ancest = result:head */
728 else if (ours_changed
&& !theirs_changed
)
729 result
= &conflict
->our_entry
;
730 /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
731 else if (!ours_changed
&& theirs_changed
)
732 result
= &conflict
->their_entry
;
736 if (result
!= NULL
&&
737 GIT_MERGE_INDEX_ENTRY_EXISTS(*result
) &&
738 (error
= git_vector_insert(&diff_list
->staged
, (void *)result
)) >= 0)
741 /* Note: trivial resolution does not update the REUC. */
746 static int merge_conflict_resolve_one_removed(
748 git_merge_diff_list
*diff_list
,
749 const git_merge_diff
*conflict
)
751 int ours_empty
, theirs_empty
;
752 int ours_changed
, theirs_changed
;
755 GIT_ASSERT_ARG(resolved
);
756 GIT_ASSERT_ARG(diff_list
);
757 GIT_ASSERT_ARG(conflict
);
761 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
762 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
765 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
766 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
768 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
769 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
771 /* Removed in both */
772 if (ours_changed
&& ours_empty
&& theirs_empty
)
774 /* Removed in ours */
775 else if (ours_empty
&& !theirs_changed
)
777 /* Removed in theirs */
778 else if (!ours_changed
&& theirs_empty
)
782 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
787 static int merge_conflict_resolve_one_renamed(
789 git_merge_diff_list
*diff_list
,
790 const git_merge_diff
*conflict
)
792 int ours_renamed
, theirs_renamed
;
793 int ours_changed
, theirs_changed
;
794 git_index_entry
*merged
;
797 GIT_ASSERT_ARG(resolved
);
798 GIT_ASSERT_ARG(diff_list
);
799 GIT_ASSERT_ARG(conflict
);
803 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
804 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
807 ours_renamed
= (conflict
->our_status
== GIT_DELTA_RENAMED
);
808 theirs_renamed
= (conflict
->their_status
== GIT_DELTA_RENAMED
);
810 if (!ours_renamed
&& !theirs_renamed
)
813 /* Reject one file in a 2->1 conflict */
814 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
815 conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
||
816 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
819 ours_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->our_entry
.id
) != 0) ||
820 (conflict
->ancestor_entry
.mode
!= conflict
->our_entry
.mode
);
822 theirs_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->their_entry
.id
) != 0) ||
823 (conflict
->ancestor_entry
.mode
!= conflict
->their_entry
.mode
);
825 /* if both are modified (and not to a common target) require a merge */
826 if (ours_changed
&& theirs_changed
&&
827 git_oid__cmp(&conflict
->our_entry
.id
, &conflict
->their_entry
.id
) != 0)
830 if ((merged
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
))) == NULL
)
834 memcpy(merged
, &conflict
->our_entry
, sizeof(git_index_entry
));
836 memcpy(merged
, &conflict
->their_entry
, sizeof(git_index_entry
));
839 merged
->path
= conflict
->our_entry
.path
;
841 merged
->path
= conflict
->their_entry
.path
;
845 git_vector_insert(&diff_list
->staged
, merged
);
846 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
851 static bool merge_conflict_can_resolve_contents(
852 const git_merge_diff
*conflict
)
854 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
855 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
858 /* Reject D/F conflicts */
859 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
)
862 /* Reject submodules. */
863 if (S_ISGITLINK(conflict
->ancestor_entry
.mode
) ||
864 S_ISGITLINK(conflict
->our_entry
.mode
) ||
865 S_ISGITLINK(conflict
->their_entry
.mode
))
868 /* Reject link/file conflicts. */
869 if ((S_ISLNK(conflict
->ancestor_entry
.mode
) ^
870 S_ISLNK(conflict
->our_entry
.mode
)) ||
871 (S_ISLNK(conflict
->ancestor_entry
.mode
) ^
872 S_ISLNK(conflict
->their_entry
.mode
)))
875 /* Reject name conflicts */
876 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
877 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
880 if ((conflict
->our_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
881 (conflict
->their_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
882 strcmp(conflict
->ancestor_entry
.path
, conflict
->their_entry
.path
) != 0)
888 static int merge_conflict_invoke_driver(
889 git_index_entry
**out
,
891 git_merge_driver
*driver
,
892 git_merge_diff_list
*diff_list
,
893 git_merge_driver_source
*src
)
895 git_index_entry
*result
;
896 git_buf buf
= GIT_BUF_INIT
;
905 if ((error
= driver
->apply(driver
, &path
, &mode
, &buf
, name
, src
)) < 0 ||
906 (error
= git_repository_odb(&odb
, src
->repo
)) < 0 ||
907 (error
= git_odb_write(&oid
, odb
, buf
.ptr
, buf
.size
, GIT_OBJECT_BLOB
)) < 0)
910 result
= git_pool_mallocz(&diff_list
->pool
, sizeof(git_index_entry
));
911 GIT_ERROR_CHECK_ALLOC(result
);
913 git_oid_cpy(&result
->id
, &oid
);
915 result
->file_size
= (uint32_t)buf
.size
;
917 result
->path
= git_pool_strdup(&diff_list
->pool
, path
);
918 GIT_ERROR_CHECK_ALLOC(result
->path
);
923 git_buf_dispose(&buf
);
929 static int merge_conflict_resolve_contents(
931 git_merge_diff_list
*diff_list
,
932 const git_merge_diff
*conflict
,
933 const git_merge_options
*merge_opts
,
934 const git_merge_file_options
*file_opts
)
936 git_merge_driver_source source
= {0};
937 git_merge_file_result result
= {0};
938 git_merge_driver
*driver
;
939 git_merge_driver__builtin builtin
= {{0}};
940 git_index_entry
*merge_result
;
943 bool fallback
= false;
946 GIT_ASSERT_ARG(resolved
);
947 GIT_ASSERT_ARG(diff_list
);
948 GIT_ASSERT_ARG(conflict
);
952 if (!merge_conflict_can_resolve_contents(conflict
))
955 source
.repo
= diff_list
->repo
;
956 source
.default_driver
= merge_opts
->default_driver
;
957 source
.file_opts
= file_opts
;
958 source
.ancestor
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
959 &conflict
->ancestor_entry
: NULL
;
960 source
.ours
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
961 &conflict
->our_entry
: NULL
;
962 source
.theirs
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
963 &conflict
->their_entry
: NULL
;
965 if (file_opts
->favor
!= GIT_MERGE_FILE_FAVOR_NORMAL
) {
966 /* if the user requested a particular type of resolution (via the
967 * favor flag) then let that override the gitattributes and use
968 * the builtin driver.
971 builtin
.base
.apply
= git_merge_driver__builtin_apply
;
972 builtin
.favor
= file_opts
->favor
;
974 driver
= &builtin
.base
;
976 /* find the merge driver for this file */
977 if ((error
= git_merge_driver_for_source(&name
, &driver
, &source
)) < 0)
985 error
= merge_conflict_invoke_driver(&merge_result
, name
, driver
,
988 if (error
== GIT_PASSTHROUGH
)
993 error
= merge_conflict_invoke_driver(&merge_result
, "text",
994 &git_merge_driver__text
.base
, diff_list
, &source
);
998 if (error
== GIT_EMERGECONFLICT
)
1004 git_vector_insert(&diff_list
->staged
, merge_result
);
1005 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
1010 git_merge_file_result_free(&result
);
1016 static int merge_conflict_resolve(
1018 git_merge_diff_list
*diff_list
,
1019 const git_merge_diff
*conflict
,
1020 const git_merge_options
*merge_opts
,
1021 const git_merge_file_options
*file_opts
)
1028 if ((error
= merge_conflict_resolve_trivial(
1029 &resolved
, diff_list
, conflict
)) < 0)
1032 if (!resolved
&& (error
= merge_conflict_resolve_one_removed(
1033 &resolved
, diff_list
, conflict
)) < 0)
1036 if (!resolved
&& (error
= merge_conflict_resolve_one_renamed(
1037 &resolved
, diff_list
, conflict
)) < 0)
1040 if (!resolved
&& (error
= merge_conflict_resolve_contents(
1041 &resolved
, diff_list
, conflict
, merge_opts
, file_opts
)) < 0)
1050 /* Rename detection and coalescing */
1052 struct merge_diff_similarity
{
1053 unsigned char similarity
;
1057 static int index_entry_similarity_calc(
1059 git_repository
*repo
,
1060 git_index_entry
*entry
,
1061 const git_merge_options
*opts
)
1064 git_diff_file diff_file
= {{{0}}};
1065 git_object_size_t blobsize
;
1068 if (*out
|| *out
== &cache_invalid_marker
)
1073 if ((error
= git_blob_lookup(&blob
, repo
, &entry
->id
)) < 0)
1076 git_oid_cpy(&diff_file
.id
, &entry
->id
);
1077 diff_file
.path
= entry
->path
;
1078 diff_file
.size
= entry
->file_size
;
1079 diff_file
.mode
= entry
->mode
;
1080 diff_file
.flags
= 0;
1082 blobsize
= git_blob_rawsize(blob
);
1084 /* file too big for rename processing */
1085 if (!git__is_sizet(blobsize
))
1088 error
= opts
->metric
->buffer_signature(out
, &diff_file
,
1089 git_blob_rawcontent(blob
), (size_t)blobsize
,
1090 opts
->metric
->payload
);
1091 if (error
== GIT_EBUFS
)
1092 *out
= &cache_invalid_marker
;
1094 git_blob_free(blob
);
1099 static int index_entry_similarity_inexact(
1100 git_repository
*repo
,
1106 const git_merge_options
*opts
)
1111 if (!GIT_MODE_ISBLOB(a
->mode
) || !GIT_MODE_ISBLOB(b
->mode
))
1114 /* update signature cache if needed */
1115 if ((error
= index_entry_similarity_calc(&cache
[a_idx
], repo
, a
, opts
)) < 0 ||
1116 (error
= index_entry_similarity_calc(&cache
[b_idx
], repo
, b
, opts
)) < 0)
1119 /* some metrics may not wish to process this file (too big / too small) */
1120 if (cache
[a_idx
] == &cache_invalid_marker
|| cache
[b_idx
] == &cache_invalid_marker
)
1123 /* compare signatures */
1124 if (opts
->metric
->similarity(&score
, cache
[a_idx
], cache
[b_idx
], opts
->metric
->payload
) < 0)
1130 else if (score
> 100)
1136 /* Tracks deletes by oid for merge_diff_mark_similarity_exact(). This is a
1137 * non-shrinking queue where next_pos is the next position to dequeue.
1140 git_array_t(size_t) arr
;
1143 } deletes_by_oid_queue
;
1145 static void deletes_by_oid_free(git_oidmap
*map
) {
1146 deletes_by_oid_queue
*queue
;
1151 git_oidmap_foreach_value(map
, queue
, {
1152 git_array_clear(queue
->arr
);
1154 git_oidmap_free(map
);
1157 static int deletes_by_oid_enqueue(git_oidmap
*map
, git_pool
*pool
, const git_oid
*id
, size_t idx
)
1159 deletes_by_oid_queue
*queue
;
1160 size_t *array_entry
;
1162 if ((queue
= git_oidmap_get(map
, id
)) == NULL
) {
1163 queue
= git_pool_malloc(pool
, sizeof(deletes_by_oid_queue
));
1164 GIT_ERROR_CHECK_ALLOC(queue
);
1166 git_array_init(queue
->arr
);
1167 queue
->next_pos
= 0;
1168 queue
->first_entry
= idx
;
1170 if (git_oidmap_set(map
, id
, queue
) < 0)
1173 array_entry
= git_array_alloc(queue
->arr
);
1174 GIT_ERROR_CHECK_ALLOC(array_entry
);
1181 static int deletes_by_oid_dequeue(size_t *idx
, git_oidmap
*map
, const git_oid
*id
)
1183 deletes_by_oid_queue
*queue
;
1184 size_t *array_entry
;
1186 if ((queue
= git_oidmap_get(map
, id
)) == NULL
)
1187 return GIT_ENOTFOUND
;
1189 if (queue
->next_pos
== 0) {
1190 *idx
= queue
->first_entry
;
1192 array_entry
= git_array_get(queue
->arr
, queue
->next_pos
- 1);
1193 if (array_entry
== NULL
)
1194 return GIT_ENOTFOUND
;
1196 *idx
= *array_entry
;
1203 static int merge_diff_mark_similarity_exact(
1204 git_merge_diff_list
*diff_list
,
1205 struct merge_diff_similarity
*similarity_ours
,
1206 struct merge_diff_similarity
*similarity_theirs
)
1209 git_merge_diff
*conflict_src
, *conflict_tgt
;
1210 git_oidmap
*ours_deletes_by_oid
= NULL
, *theirs_deletes_by_oid
= NULL
;
1213 if (git_oidmap_new(&ours_deletes_by_oid
) < 0 ||
1214 git_oidmap_new(&theirs_deletes_by_oid
) < 0) {
1219 /* Build a map of object ids to conflicts */
1220 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1221 /* Items can be the source of a rename iff they have an item in the
1222 * ancestor slot and lack an item in the ours or theirs slot. */
1223 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
))
1226 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1227 error
= deletes_by_oid_enqueue(ours_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1232 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1233 error
= deletes_by_oid_enqueue(theirs_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1239 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1240 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1243 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
)) {
1244 if (deletes_by_oid_dequeue(&i
, ours_deletes_by_oid
, &conflict_tgt
->our_entry
.id
) == 0) {
1245 similarity_ours
[i
].similarity
= 100;
1246 similarity_ours
[i
].other_idx
= j
;
1248 similarity_ours
[j
].similarity
= 100;
1249 similarity_ours
[j
].other_idx
= i
;
1253 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
)) {
1254 if (deletes_by_oid_dequeue(&i
, theirs_deletes_by_oid
, &conflict_tgt
->their_entry
.id
) == 0) {
1255 similarity_theirs
[i
].similarity
= 100;
1256 similarity_theirs
[i
].other_idx
= j
;
1258 similarity_theirs
[j
].similarity
= 100;
1259 similarity_theirs
[j
].other_idx
= i
;
1265 deletes_by_oid_free(ours_deletes_by_oid
);
1266 deletes_by_oid_free(theirs_deletes_by_oid
);
1271 static int merge_diff_mark_similarity_inexact(
1272 git_repository
*repo
,
1273 git_merge_diff_list
*diff_list
,
1274 struct merge_diff_similarity
*similarity_ours
,
1275 struct merge_diff_similarity
*similarity_theirs
,
1277 const git_merge_options
*opts
)
1280 git_merge_diff
*conflict_src
, *conflict_tgt
;
1283 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1284 /* Items can be the source of a rename iff they have an item in the
1285 * ancestor slot and lack an item in the ours or theirs slot. */
1286 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
) ||
1287 (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
) &&
1288 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)))
1291 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1292 size_t our_idx
= diff_list
->conflicts
.length
+ j
;
1293 size_t their_idx
= (diff_list
->conflicts
.length
* 2) + j
;
1295 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1298 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
) &&
1299 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1300 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->our_entry
, our_idx
, cache
, opts
);
1302 if (similarity
== GIT_EBUFS
)
1304 else if (similarity
< 0)
1307 if (similarity
> similarity_ours
[i
].similarity
&&
1308 similarity
> similarity_ours
[j
].similarity
) {
1309 /* Clear previous best similarity */
1310 if (similarity_ours
[i
].similarity
> 0)
1311 similarity_ours
[similarity_ours
[i
].other_idx
].similarity
= 0;
1313 if (similarity_ours
[j
].similarity
> 0)
1314 similarity_ours
[similarity_ours
[j
].other_idx
].similarity
= 0;
1316 similarity_ours
[i
].similarity
= similarity
;
1317 similarity_ours
[i
].other_idx
= j
;
1319 similarity_ours
[j
].similarity
= similarity
;
1320 similarity_ours
[j
].other_idx
= i
;
1324 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
) &&
1325 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1326 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->their_entry
, their_idx
, cache
, opts
);
1328 if (similarity
> similarity_theirs
[i
].similarity
&&
1329 similarity
> similarity_theirs
[j
].similarity
) {
1330 /* Clear previous best similarity */
1331 if (similarity_theirs
[i
].similarity
> 0)
1332 similarity_theirs
[similarity_theirs
[i
].other_idx
].similarity
= 0;
1334 if (similarity_theirs
[j
].similarity
> 0)
1335 similarity_theirs
[similarity_theirs
[j
].other_idx
].similarity
= 0;
1337 similarity_theirs
[i
].similarity
= similarity
;
1338 similarity_theirs
[i
].other_idx
= j
;
1340 similarity_theirs
[j
].similarity
= similarity
;
1341 similarity_theirs
[j
].other_idx
= i
;
1353 * Ancestor Ours Theirs
1355 * 0a A A A No rename
1356 * b A A* A No rename (ours was rewritten)
1357 * c A A A* No rename (theirs rewritten)
1358 * 1a A A B[A] Rename or rename/edit
1359 * b A B[A] A (automergeable)
1360 * 2 A B[A] B[A] Both renamed (automergeable)
1361 * 3a A B[A] Rename/delete
1363 * 4a A B[A] B Rename/add [B~ours B~theirs]
1365 * 5 A B[A] C[A] Both renamed ("1 -> 2")
1366 * 6 A C[A] Both renamed ("2 -> 1")
1367 * B C[B] [C~ours C~theirs] (automergeable)
1369 static void merge_diff_mark_rename_conflict(
1370 git_merge_diff_list
*diff_list
,
1371 struct merge_diff_similarity
*similarity_ours
,
1373 size_t ours_source_idx
,
1374 struct merge_diff_similarity
*similarity_theirs
,
1375 bool theirs_renamed
,
1376 size_t theirs_source_idx
,
1377 git_merge_diff
*target
,
1378 const git_merge_options
*opts
)
1380 git_merge_diff
*ours_source
= NULL
, *theirs_source
= NULL
;
1383 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1386 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1388 /* Detect 2->1 conflicts */
1389 if (ours_renamed
&& theirs_renamed
) {
1390 /* Both renamed to the same target name. */
1391 if (ours_source_idx
== theirs_source_idx
)
1392 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED
;
1394 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1395 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1397 } else if (ours_renamed
) {
1398 /* If our source was also renamed in theirs, this is a 1->2 */
1399 if (similarity_theirs
[ours_source_idx
].similarity
>= opts
->rename_threshold
)
1400 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1402 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
)) {
1403 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1404 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1407 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source
->their_entry
))
1408 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1410 else if (ours_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1411 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1412 } else if (theirs_renamed
) {
1413 /* If their source was also renamed in ours, this is a 1->2 */
1414 if (similarity_ours
[theirs_source_idx
].similarity
>= opts
->rename_threshold
)
1415 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1417 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
)) {
1418 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1419 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1422 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source
->our_entry
))
1423 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1425 else if (theirs_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1426 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1430 GIT_INLINE(void) merge_diff_coalesce_rename(
1431 git_index_entry
*source_entry
,
1432 git_delta_t
*source_status
,
1433 git_index_entry
*target_entry
,
1434 git_delta_t
*target_status
)
1436 /* Coalesce the rename target into the rename source. */
1437 memcpy(source_entry
, target_entry
, sizeof(git_index_entry
));
1438 *source_status
= GIT_DELTA_RENAMED
;
1440 memset(target_entry
, 0x0, sizeof(git_index_entry
));
1441 *target_status
= GIT_DELTA_UNMODIFIED
;
1444 static void merge_diff_list_coalesce_renames(
1445 git_merge_diff_list
*diff_list
,
1446 struct merge_diff_similarity
*similarity_ours
,
1447 struct merge_diff_similarity
*similarity_theirs
,
1448 const git_merge_options
*opts
)
1451 bool ours_renamed
= 0, theirs_renamed
= 0;
1452 size_t ours_source_idx
= 0, theirs_source_idx
= 0;
1453 git_merge_diff
*ours_source
, *theirs_source
, *target
;
1455 for (i
= 0; i
< diff_list
->conflicts
.length
; i
++) {
1456 target
= diff_list
->conflicts
.contents
[i
];
1461 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
) &&
1462 similarity_ours
[i
].similarity
>= opts
->rename_threshold
) {
1463 ours_source_idx
= similarity_ours
[i
].other_idx
;
1465 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1467 merge_diff_coalesce_rename(
1468 &ours_source
->our_entry
,
1469 &ours_source
->our_status
,
1471 &target
->our_status
);
1473 similarity_ours
[ours_source_idx
].similarity
= 0;
1474 similarity_ours
[i
].similarity
= 0;
1479 /* insufficient to determine direction */
1480 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
) &&
1481 similarity_theirs
[i
].similarity
>= opts
->rename_threshold
) {
1482 theirs_source_idx
= similarity_theirs
[i
].other_idx
;
1484 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1486 merge_diff_coalesce_rename(
1487 &theirs_source
->their_entry
,
1488 &theirs_source
->their_status
,
1489 &target
->their_entry
,
1490 &target
->their_status
);
1492 similarity_theirs
[theirs_source_idx
].similarity
= 0;
1493 similarity_theirs
[i
].similarity
= 0;
1498 merge_diff_mark_rename_conflict(diff_list
,
1499 similarity_ours
, ours_renamed
, ours_source_idx
,
1500 similarity_theirs
, theirs_renamed
, theirs_source_idx
,
1505 static int merge_diff_empty(const git_vector
*conflicts
, size_t idx
, void *p
)
1507 git_merge_diff
*conflict
= conflicts
->contents
[idx
];
1511 return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) &&
1512 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) &&
1513 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
));
1516 static void merge_diff_list_count_candidates(
1517 git_merge_diff_list
*diff_list
,
1521 git_merge_diff
*entry
;
1527 git_vector_foreach(&diff_list
->conflicts
, i
, entry
) {
1528 if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
) &&
1529 (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->our_entry
) ||
1530 !GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->their_entry
)))
1532 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
))
1537 int git_merge_diff_list__find_renames(
1538 git_repository
*repo
,
1539 git_merge_diff_list
*diff_list
,
1540 const git_merge_options
*opts
)
1542 struct merge_diff_similarity
*similarity_ours
, *similarity_theirs
;
1543 void **cache
= NULL
;
1544 size_t cache_size
= 0;
1545 size_t src_count
, tgt_count
, i
;
1548 GIT_ASSERT_ARG(diff_list
);
1549 GIT_ASSERT_ARG(opts
);
1551 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) == 0 ||
1552 !diff_list
->conflicts
.length
)
1555 similarity_ours
= git__calloc(diff_list
->conflicts
.length
,
1556 sizeof(struct merge_diff_similarity
));
1557 GIT_ERROR_CHECK_ALLOC(similarity_ours
);
1559 similarity_theirs
= git__calloc(diff_list
->conflicts
.length
,
1560 sizeof(struct merge_diff_similarity
));
1561 GIT_ERROR_CHECK_ALLOC(similarity_theirs
);
1563 /* Calculate similarity between items that were deleted from the ancestor
1564 * and added in the other branch.
1566 if ((error
= merge_diff_mark_similarity_exact(diff_list
, similarity_ours
, similarity_theirs
)) < 0)
1569 if (opts
->rename_threshold
< 100 && diff_list
->conflicts
.length
<= opts
->target_limit
) {
1570 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&cache_size
, diff_list
->conflicts
.length
, 3);
1571 cache
= git__calloc(cache_size
, sizeof(void *));
1572 GIT_ERROR_CHECK_ALLOC(cache
);
1574 merge_diff_list_count_candidates(diff_list
, &src_count
, &tgt_count
);
1576 if (src_count
> opts
->target_limit
|| tgt_count
> opts
->target_limit
) {
1579 if ((error
= merge_diff_mark_similarity_inexact(
1580 repo
, diff_list
, similarity_ours
, similarity_theirs
, cache
, opts
)) < 0)
1585 /* For entries that are appropriately similar, merge the new name's entry
1586 * into the old name.
1588 merge_diff_list_coalesce_renames(diff_list
, similarity_ours
, similarity_theirs
, opts
);
1590 /* And remove any entries that were merged and are now empty. */
1591 git_vector_remove_matching(&diff_list
->conflicts
, merge_diff_empty
, NULL
);
1594 if (cache
!= NULL
) {
1595 for (i
= 0; i
< cache_size
; ++i
) {
1596 if (cache
[i
] != NULL
&& cache
[i
] != &cache_invalid_marker
)
1597 opts
->metric
->free_signature(cache
[i
], opts
->metric
->payload
);
1603 git__free(similarity_ours
);
1604 git__free(similarity_theirs
);
1609 /* Directory/file conflict handling */
1611 GIT_INLINE(const char *) merge_diff_path(
1612 const git_merge_diff
*conflict
)
1614 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
1615 return conflict
->ancestor_entry
.path
;
1616 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
))
1617 return conflict
->our_entry
.path
;
1618 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
1619 return conflict
->their_entry
.path
;
1624 GIT_INLINE(bool) merge_diff_any_side_added_or_modified(
1625 const git_merge_diff
*conflict
)
1627 if (conflict
->our_status
== GIT_DELTA_ADDED
||
1628 conflict
->our_status
== GIT_DELTA_MODIFIED
||
1629 conflict
->their_status
== GIT_DELTA_ADDED
||
1630 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1636 GIT_INLINE(bool) path_is_prefixed(const char *parent
, const char *child
)
1638 size_t child_len
= strlen(child
);
1639 size_t parent_len
= strlen(parent
);
1641 if (child_len
< parent_len
||
1642 strncmp(parent
, child
, parent_len
) != 0)
1645 return (child
[parent_len
] == '/');
1648 GIT_INLINE(int) merge_diff_detect_df_conflict(
1649 struct merge_diff_df_data
*df_data
,
1650 git_merge_diff
*conflict
)
1652 const char *cur_path
= merge_diff_path(conflict
);
1654 /* Determine if this is a D/F conflict or the child of one */
1655 if (df_data
->df_path
&&
1656 path_is_prefixed(df_data
->df_path
, cur_path
))
1657 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1658 else if(df_data
->df_path
)
1659 df_data
->df_path
= NULL
;
1660 else if (df_data
->prev_path
&&
1661 merge_diff_any_side_added_or_modified(df_data
->prev_conflict
) &&
1662 merge_diff_any_side_added_or_modified(conflict
) &&
1663 path_is_prefixed(df_data
->prev_path
, cur_path
)) {
1664 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1666 df_data
->prev_conflict
->type
= GIT_MERGE_DIFF_DIRECTORY_FILE
;
1667 df_data
->df_path
= df_data
->prev_path
;
1670 df_data
->prev_path
= cur_path
;
1671 df_data
->prev_conflict
= conflict
;
1676 /* Conflict handling */
1678 GIT_INLINE(int) merge_diff_detect_type(
1679 git_merge_diff
*conflict
)
1681 if (conflict
->our_status
== GIT_DELTA_ADDED
&&
1682 conflict
->their_status
== GIT_DELTA_ADDED
)
1683 conflict
->type
= GIT_MERGE_DIFF_BOTH_ADDED
;
1684 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1685 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1686 conflict
->type
= GIT_MERGE_DIFF_BOTH_MODIFIED
;
1687 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1688 conflict
->their_status
== GIT_DELTA_DELETED
)
1689 conflict
->type
= GIT_MERGE_DIFF_BOTH_DELETED
;
1690 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1691 conflict
->their_status
== GIT_DELTA_DELETED
)
1692 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1693 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1694 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1695 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1697 conflict
->type
= GIT_MERGE_DIFF_NONE
;
1702 GIT_INLINE(int) index_entry_dup_pool(
1703 git_index_entry
*out
,
1705 const git_index_entry
*src
)
1708 memcpy(out
, src
, sizeof(git_index_entry
));
1709 if ((out
->path
= git_pool_strdup(pool
, src
->path
)) == NULL
)
1716 GIT_INLINE(int) merge_delta_type_from_index_entries(
1717 const git_index_entry
*ancestor
,
1718 const git_index_entry
*other
)
1720 if (ancestor
== NULL
&& other
== NULL
)
1721 return GIT_DELTA_UNMODIFIED
;
1722 else if (ancestor
== NULL
&& other
!= NULL
)
1723 return GIT_DELTA_ADDED
;
1724 else if (ancestor
!= NULL
&& other
== NULL
)
1725 return GIT_DELTA_DELETED
;
1726 else if (S_ISDIR(ancestor
->mode
) ^ S_ISDIR(other
->mode
))
1727 return GIT_DELTA_TYPECHANGE
;
1728 else if(S_ISLNK(ancestor
->mode
) ^ S_ISLNK(other
->mode
))
1729 return GIT_DELTA_TYPECHANGE
;
1730 else if (git_oid__cmp(&ancestor
->id
, &other
->id
) ||
1731 ancestor
->mode
!= other
->mode
)
1732 return GIT_DELTA_MODIFIED
;
1734 return GIT_DELTA_UNMODIFIED
;
1737 static git_merge_diff
*merge_diff_from_index_entries(
1738 git_merge_diff_list
*diff_list
,
1739 const git_index_entry
**entries
)
1741 git_merge_diff
*conflict
;
1742 git_pool
*pool
= &diff_list
->pool
;
1744 if ((conflict
= git_pool_mallocz(pool
, sizeof(git_merge_diff
))) == NULL
)
1747 if (index_entry_dup_pool(&conflict
->ancestor_entry
, pool
, entries
[TREE_IDX_ANCESTOR
]) < 0 ||
1748 index_entry_dup_pool(&conflict
->our_entry
, pool
, entries
[TREE_IDX_OURS
]) < 0 ||
1749 index_entry_dup_pool(&conflict
->their_entry
, pool
, entries
[TREE_IDX_THEIRS
]) < 0)
1752 conflict
->our_status
= merge_delta_type_from_index_entries(
1753 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_OURS
]);
1754 conflict
->their_status
= merge_delta_type_from_index_entries(
1755 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_THEIRS
]);
1762 static int merge_diff_list_insert_conflict(
1763 git_merge_diff_list
*diff_list
,
1764 struct merge_diff_df_data
*merge_df_data
,
1765 const git_index_entry
*tree_items
[3])
1767 git_merge_diff
*conflict
;
1769 if ((conflict
= merge_diff_from_index_entries(diff_list
, tree_items
)) == NULL
||
1770 merge_diff_detect_type(conflict
) < 0 ||
1771 merge_diff_detect_df_conflict(merge_df_data
, conflict
) < 0 ||
1772 git_vector_insert(&diff_list
->conflicts
, conflict
) < 0)
1778 static int merge_diff_list_insert_unmodified(
1779 git_merge_diff_list
*diff_list
,
1780 const git_index_entry
*tree_items
[3])
1783 git_index_entry
*entry
;
1785 entry
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
));
1786 GIT_ERROR_CHECK_ALLOC(entry
);
1788 if ((error
= index_entry_dup_pool(entry
, &diff_list
->pool
, tree_items
[0])) >= 0)
1789 error
= git_vector_insert(&diff_list
->staged
, entry
);
1794 struct merge_diff_find_data
{
1795 git_merge_diff_list
*diff_list
;
1796 struct merge_diff_df_data df_data
;
1799 static int queue_difference(const git_index_entry
**entries
, void *data
)
1801 struct merge_diff_find_data
*find_data
= data
;
1802 bool item_modified
= false;
1805 if (!entries
[0] || !entries
[1] || !entries
[2]) {
1806 item_modified
= true;
1808 for (i
= 1; i
< 3; i
++) {
1809 if (index_entry_cmp(entries
[0], entries
[i
]) != 0) {
1810 item_modified
= true;
1816 return item_modified
?
1817 merge_diff_list_insert_conflict(
1818 find_data
->diff_list
, &find_data
->df_data
, entries
) :
1819 merge_diff_list_insert_unmodified(find_data
->diff_list
, entries
);
1822 int git_merge_diff_list__find_differences(
1823 git_merge_diff_list
*diff_list
,
1824 git_iterator
*ancestor_iter
,
1825 git_iterator
*our_iter
,
1826 git_iterator
*their_iter
)
1828 git_iterator
*iterators
[3] = { ancestor_iter
, our_iter
, their_iter
};
1829 struct merge_diff_find_data find_data
= { diff_list
};
1831 return git_iterator_walk(iterators
, 3, queue_difference
, &find_data
);
1834 git_merge_diff_list
*git_merge_diff_list__alloc(git_repository
*repo
)
1836 git_merge_diff_list
*diff_list
= git__calloc(1, sizeof(git_merge_diff_list
));
1838 if (diff_list
== NULL
)
1841 diff_list
->repo
= repo
;
1844 if (git_pool_init(&diff_list
->pool
, 1) < 0 ||
1845 git_vector_init(&diff_list
->staged
, 0, NULL
) < 0 ||
1846 git_vector_init(&diff_list
->conflicts
, 0, NULL
) < 0 ||
1847 git_vector_init(&diff_list
->resolved
, 0, NULL
) < 0) {
1848 git_merge_diff_list__free(diff_list
);
1855 void git_merge_diff_list__free(git_merge_diff_list
*diff_list
)
1860 git_vector_free(&diff_list
->staged
);
1861 git_vector_free(&diff_list
->conflicts
);
1862 git_vector_free(&diff_list
->resolved
);
1863 git_pool_clear(&diff_list
->pool
);
1864 git__free(diff_list
);
1867 static int merge_normalize_opts(
1868 git_repository
*repo
,
1869 git_merge_options
*opts
,
1870 const git_merge_options
*given
)
1872 git_config
*cfg
= NULL
;
1873 git_config_entry
*entry
= NULL
;
1876 GIT_ASSERT_ARG(repo
);
1877 GIT_ASSERT_ARG(opts
);
1879 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0)
1882 if (given
!= NULL
) {
1883 memcpy(opts
, given
, sizeof(git_merge_options
));
1885 git_merge_options init
= GIT_MERGE_OPTIONS_INIT
;
1886 memcpy(opts
, &init
, sizeof(init
));
1889 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) && !opts
->rename_threshold
)
1890 opts
->rename_threshold
= GIT_MERGE_DEFAULT_RENAME_THRESHOLD
;
1892 if (given
&& given
->default_driver
) {
1893 opts
->default_driver
= git__strdup(given
->default_driver
);
1894 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1896 error
= git_config_get_entry(&entry
, cfg
, "merge.default");
1899 opts
->default_driver
= git__strdup(entry
->value
);
1900 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1901 } else if (error
== GIT_ENOTFOUND
) {
1908 if (!opts
->target_limit
) {
1909 int limit
= git_config__get_int_force(cfg
, "merge.renamelimit", 0);
1912 limit
= git_config__get_int_force(cfg
, "diff.renamelimit", 0);
1914 opts
->target_limit
= (limit
<= 0) ?
1915 GIT_MERGE_DEFAULT_TARGET_LIMIT
: (unsigned int)limit
;
1918 /* assign the internal metric with whitespace flag as payload */
1919 if (!opts
->metric
) {
1920 opts
->metric
= git__malloc(sizeof(git_diff_similarity_metric
));
1921 GIT_ERROR_CHECK_ALLOC(opts
->metric
);
1923 opts
->metric
->file_signature
= git_diff_find_similar__hashsig_for_file
;
1924 opts
->metric
->buffer_signature
= git_diff_find_similar__hashsig_for_buf
;
1925 opts
->metric
->free_signature
= git_diff_find_similar__hashsig_free
;
1926 opts
->metric
->similarity
= git_diff_find_similar__calc_similarity
;
1927 opts
->metric
->payload
= (void *)GIT_HASHSIG_SMART_WHITESPACE
;
1931 git_config_entry_free(entry
);
1936 static int merge_index_insert_reuc(
1939 const git_index_entry
*entry
)
1941 const git_index_reuc_entry
*reuc
;
1942 int mode
[3] = { 0, 0, 0 };
1943 git_oid
const *oid
[3] = { NULL
, NULL
, NULL
};
1946 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry
))
1949 if ((reuc
= git_index_reuc_get_bypath(index
, entry
->path
)) != NULL
) {
1950 for (i
= 0; i
< 3; i
++) {
1951 mode
[i
] = reuc
->mode
[i
];
1952 oid
[i
] = &reuc
->oid
[i
];
1956 mode
[idx
] = entry
->mode
;
1957 oid
[idx
] = &entry
->id
;
1959 return git_index_reuc_add(index
, entry
->path
,
1960 mode
[0], oid
[0], mode
[1], oid
[1], mode
[2], oid
[2]);
1963 static int index_update_reuc(git_index
*index
, git_merge_diff_list
*diff_list
)
1967 git_merge_diff
*conflict
;
1969 /* Add each entry in the resolved conflict to the REUC independently, since
1970 * the paths may differ due to renames. */
1971 git_vector_foreach(&diff_list
->resolved
, i
, conflict
) {
1972 const git_index_entry
*ancestor
=
1973 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
1974 &conflict
->ancestor_entry
: NULL
;
1976 const git_index_entry
*ours
=
1977 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
1978 &conflict
->our_entry
: NULL
;
1980 const git_index_entry
*theirs
=
1981 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
1982 &conflict
->their_entry
: NULL
;
1984 if (ancestor
!= NULL
&&
1985 (error
= merge_index_insert_reuc(index
, TREE_IDX_ANCESTOR
, ancestor
)) < 0)
1989 (error
= merge_index_insert_reuc(index
, TREE_IDX_OURS
, ours
)) < 0)
1992 if (theirs
!= NULL
&&
1993 (error
= merge_index_insert_reuc(index
, TREE_IDX_THEIRS
, theirs
)) < 0)
2000 static int index_from_diff_list(git_index
**out
,
2001 git_merge_diff_list
*diff_list
, bool skip_reuc
)
2005 git_merge_diff
*conflict
;
2010 if ((error
= git_index_new(&index
)) < 0)
2013 if ((error
= git_index__fill(index
, &diff_list
->staged
)) < 0)
2016 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
2017 const git_index_entry
*ancestor
=
2018 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
2019 &conflict
->ancestor_entry
: NULL
;
2021 const git_index_entry
*ours
=
2022 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
2023 &conflict
->our_entry
: NULL
;
2025 const git_index_entry
*theirs
=
2026 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
2027 &conflict
->their_entry
: NULL
;
2029 if ((error
= git_index_conflict_add(index
, ancestor
, ours
, theirs
)) < 0)
2033 /* Add each rename entry to the rename portion of the index. */
2034 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
2035 const char *ancestor_path
, *our_path
, *their_path
;
2037 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
2040 ancestor_path
= conflict
->ancestor_entry
.path
;
2043 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
2044 conflict
->our_entry
.path
: NULL
;
2047 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
2048 conflict
->their_entry
.path
: NULL
;
2050 if ((our_path
&& strcmp(ancestor_path
, our_path
) != 0) ||
2051 (their_path
&& strcmp(ancestor_path
, their_path
) != 0)) {
2052 if ((error
= git_index_name_add(index
, ancestor_path
, our_path
, their_path
)) < 0)
2058 if ((error
= index_update_reuc(index
, diff_list
)) < 0)
2066 git_index_free(index
);
2070 static git_iterator
*iterator_given_or_empty(git_iterator
**empty
, git_iterator
*given
)
2072 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2077 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2079 if (git_iterator_for_nothing(empty
, &opts
) < 0)
2085 int git_merge__iterators(
2087 git_repository
*repo
,
2088 git_iterator
*ancestor_iter
,
2089 git_iterator
*our_iter
,
2090 git_iterator
*theirs_iter
,
2091 const git_merge_options
*given_opts
)
2093 git_iterator
*empty_ancestor
= NULL
,
2095 *empty_theirs
= NULL
;
2096 git_merge_diff_list
*diff_list
;
2097 git_merge_options opts
;
2098 git_merge_file_options file_opts
= GIT_MERGE_FILE_OPTIONS_INIT
;
2099 git_merge_diff
*conflict
;
2104 GIT_ASSERT_ARG(out
);
2105 GIT_ASSERT_ARG(repo
);
2109 GIT_ERROR_CHECK_VERSION(
2110 given_opts
, GIT_MERGE_OPTIONS_VERSION
, "git_merge_options");
2112 if ((error
= merge_normalize_opts(repo
, &opts
, given_opts
)) < 0)
2115 file_opts
.favor
= opts
.file_favor
;
2116 file_opts
.flags
= opts
.file_flags
;
2118 /* use the git-inspired labels when virtual base building */
2119 if (opts
.flags
& GIT_MERGE__VIRTUAL_BASE
) {
2120 file_opts
.ancestor_label
= "merged common ancestors";
2121 file_opts
.our_label
= "Temporary merge branch 1";
2122 file_opts
.their_label
= "Temporary merge branch 2";
2123 file_opts
.flags
|= GIT_MERGE_FILE_FAVOR__CONFLICTED
;
2124 file_opts
.marker_size
= GIT_MERGE_CONFLICT_MARKER_SIZE
+ 2;
2127 diff_list
= git_merge_diff_list__alloc(repo
);
2128 GIT_ERROR_CHECK_ALLOC(diff_list
);
2130 ancestor_iter
= iterator_given_or_empty(&empty_ancestor
, ancestor_iter
);
2131 our_iter
= iterator_given_or_empty(&empty_ours
, our_iter
);
2132 theirs_iter
= iterator_given_or_empty(&empty_theirs
, theirs_iter
);
2134 if ((error
= git_merge_diff_list__find_differences(
2135 diff_list
, ancestor_iter
, our_iter
, theirs_iter
)) < 0 ||
2136 (error
= git_merge_diff_list__find_renames(repo
, diff_list
, &opts
)) < 0)
2139 memcpy(&changes
, &diff_list
->conflicts
, sizeof(git_vector
));
2140 git_vector_clear(&diff_list
->conflicts
);
2142 git_vector_foreach(&changes
, i
, conflict
) {
2145 if ((error
= merge_conflict_resolve(
2146 &resolved
, diff_list
, conflict
, &opts
, &file_opts
)) < 0)
2150 if ((opts
.flags
& GIT_MERGE_FAIL_ON_CONFLICT
)) {
2151 git_error_set(GIT_ERROR_MERGE
, "merge conflicts exist");
2152 error
= GIT_EMERGECONFLICT
;
2156 git_vector_insert(&diff_list
->conflicts
, conflict
);
2160 error
= index_from_diff_list(out
, diff_list
,
2161 (opts
.flags
& GIT_MERGE_SKIP_REUC
));
2164 if (!given_opts
|| !given_opts
->metric
)
2165 git__free(opts
.metric
);
2167 git__free((char *)opts
.default_driver
);
2169 git_merge_diff_list__free(diff_list
);
2170 git_iterator_free(empty_ancestor
);
2171 git_iterator_free(empty_ours
);
2172 git_iterator_free(empty_theirs
);
2177 int git_merge_trees(
2179 git_repository
*repo
,
2180 const git_tree
*ancestor_tree
,
2181 const git_tree
*our_tree
,
2182 const git_tree
*their_tree
,
2183 const git_merge_options
*merge_opts
)
2185 git_iterator
*ancestor_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2186 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2189 GIT_ASSERT_ARG(out
);
2190 GIT_ASSERT_ARG(repo
);
2192 /* if one side is treesame to the ancestor, take the other side */
2193 if (ancestor_tree
&& merge_opts
&& (merge_opts
->flags
& GIT_MERGE_SKIP_REUC
)) {
2194 const git_tree
*result
= NULL
;
2195 const git_oid
*ancestor_tree_id
= git_tree_id(ancestor_tree
);
2197 if (our_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(our_tree
)))
2198 result
= their_tree
;
2199 else if (their_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(their_tree
)))
2203 if ((error
= git_index_new(out
)) == 0)
2204 error
= git_index_read_tree(*out
, result
);
2210 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2212 if ((error
= git_iterator_for_tree(
2213 &ancestor_iter
, (git_tree
*)ancestor_tree
, &iter_opts
)) < 0 ||
2214 (error
= git_iterator_for_tree(
2215 &our_iter
, (git_tree
*)our_tree
, &iter_opts
)) < 0 ||
2216 (error
= git_iterator_for_tree(
2217 &their_iter
, (git_tree
*)their_tree
, &iter_opts
)) < 0)
2220 error
= git_merge__iterators(
2221 out
, repo
, ancestor_iter
, our_iter
, their_iter
, merge_opts
);
2224 git_iterator_free(ancestor_iter
);
2225 git_iterator_free(our_iter
);
2226 git_iterator_free(their_iter
);
2231 static int merge_annotated_commits(
2232 git_index
**index_out
,
2233 git_annotated_commit
**base_out
,
2234 git_repository
*repo
,
2235 git_annotated_commit
*our_commit
,
2236 git_annotated_commit
*their_commit
,
2237 size_t recursion_level
,
2238 const git_merge_options
*opts
);
2240 GIT_INLINE(int) insert_head_ids(
2241 git_array_oid_t
*ids
,
2242 const git_annotated_commit
*annotated_commit
)
2247 if (annotated_commit
->type
== GIT_ANNOTATED_COMMIT_REAL
) {
2248 id
= git_array_alloc(*ids
);
2249 GIT_ERROR_CHECK_ALLOC(id
);
2251 git_oid_cpy(id
, git_commit_id(annotated_commit
->commit
));
2253 for (i
= 0; i
< annotated_commit
->parents
.size
; i
++) {
2254 id
= git_array_alloc(*ids
);
2255 GIT_ERROR_CHECK_ALLOC(id
);
2257 git_oid_cpy(id
, &annotated_commit
->parents
.ptr
[i
]);
2264 static int create_virtual_base(
2265 git_annotated_commit
**out
,
2266 git_repository
*repo
,
2267 git_annotated_commit
*one
,
2268 git_annotated_commit
*two
,
2269 const git_merge_options
*opts
,
2270 size_t recursion_level
)
2272 git_annotated_commit
*result
= NULL
;
2273 git_index
*index
= NULL
;
2274 git_merge_options virtual_opts
= GIT_MERGE_OPTIONS_INIT
;
2276 /* Conflicts in the merge base creation do not propagate to conflicts
2277 * in the result; the conflicted base will act as the common ancestor.
2280 memcpy(&virtual_opts
, opts
, sizeof(git_merge_options
));
2282 virtual_opts
.flags
&= ~GIT_MERGE_FAIL_ON_CONFLICT
;
2283 virtual_opts
.flags
|= GIT_MERGE__VIRTUAL_BASE
;
2285 if ((merge_annotated_commits(&index
, NULL
, repo
, one
, two
,
2286 recursion_level
+ 1, &virtual_opts
)) < 0)
2289 result
= git__calloc(1, sizeof(git_annotated_commit
));
2290 GIT_ERROR_CHECK_ALLOC(result
);
2291 result
->type
= GIT_ANNOTATED_COMMIT_VIRTUAL
;
2292 result
->index
= index
;
2294 if (insert_head_ids(&result
->parents
, one
) < 0 ||
2295 insert_head_ids(&result
->parents
, two
) < 0) {
2296 git_annotated_commit_free(result
);
2304 static int compute_base(
2305 git_annotated_commit
**out
,
2306 git_repository
*repo
,
2307 const git_annotated_commit
*one
,
2308 const git_annotated_commit
*two
,
2309 const git_merge_options
*given_opts
,
2310 size_t recursion_level
)
2312 git_array_oid_t head_ids
= GIT_ARRAY_INIT
;
2313 git_oidarray bases
= {0};
2314 git_annotated_commit
*base
= NULL
, *other
= NULL
, *new_base
= NULL
;
2315 git_merge_options opts
= GIT_MERGE_OPTIONS_INIT
;
2316 size_t i
, base_count
;
2322 memcpy(&opts
, given_opts
, sizeof(git_merge_options
));
2324 /* With more than two commits, merge_bases_many finds the base of
2325 * the first commit and a hypothetical merge of the others. Since
2326 * "one" may itself be a virtual commit, which insert_head_ids
2327 * substitutes multiple ancestors for, it needs to be added
2328 * after "two" which is always a single real commit.
2330 if ((error
= insert_head_ids(&head_ids
, two
)) < 0 ||
2331 (error
= insert_head_ids(&head_ids
, one
)) < 0 ||
2332 (error
= git_merge_bases_many(&bases
, repo
,
2333 head_ids
.size
, head_ids
.ptr
)) < 0)
2336 base_count
= (opts
.flags
& GIT_MERGE_NO_RECURSIVE
) ? 0 : bases
.count
;
2339 git_oidarray__reverse(&bases
);
2341 if ((error
= git_annotated_commit_lookup(&base
, repo
, &bases
.ids
[0])) < 0)
2344 for (i
= 1; i
< base_count
; i
++) {
2347 if (opts
.recursion_limit
&& recursion_level
> opts
.recursion_limit
)
2350 if ((error
= git_annotated_commit_lookup(&other
, repo
,
2351 &bases
.ids
[i
])) < 0 ||
2352 (error
= create_virtual_base(&new_base
, repo
, base
, other
, &opts
,
2353 recursion_level
)) < 0)
2356 git_annotated_commit_free(base
);
2357 git_annotated_commit_free(other
);
2368 git_annotated_commit_free(base
);
2370 git_annotated_commit_free(other
);
2371 git_annotated_commit_free(new_base
);
2372 git_oidarray_dispose(&bases
);
2373 git_array_clear(head_ids
);
2377 static int iterator_for_annotated_commit(
2379 git_annotated_commit
*commit
)
2381 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2384 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2386 if (commit
== NULL
) {
2387 error
= git_iterator_for_nothing(out
, &opts
);
2388 } else if (commit
->type
== GIT_ANNOTATED_COMMIT_VIRTUAL
) {
2389 error
= git_iterator_for_index(out
, git_index_owner(commit
->index
), commit
->index
, &opts
);
2391 if (!commit
->tree
&&
2392 (error
= git_commit_tree(&commit
->tree
, commit
->commit
)) < 0)
2395 error
= git_iterator_for_tree(out
, commit
->tree
, &opts
);
2402 static int merge_annotated_commits(
2403 git_index
**index_out
,
2404 git_annotated_commit
**base_out
,
2405 git_repository
*repo
,
2406 git_annotated_commit
*ours
,
2407 git_annotated_commit
*theirs
,
2408 size_t recursion_level
,
2409 const git_merge_options
*opts
)
2411 git_annotated_commit
*base
= NULL
;
2412 git_iterator
*base_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2415 if ((error
= compute_base(&base
, repo
, ours
, theirs
, opts
,
2416 recursion_level
)) < 0) {
2418 if (error
!= GIT_ENOTFOUND
)
2424 if ((error
= iterator_for_annotated_commit(&base_iter
, base
)) < 0 ||
2425 (error
= iterator_for_annotated_commit(&our_iter
, ours
)) < 0 ||
2426 (error
= iterator_for_annotated_commit(&their_iter
, theirs
)) < 0 ||
2427 (error
= git_merge__iterators(index_out
, repo
, base_iter
, our_iter
,
2428 their_iter
, opts
)) < 0)
2437 git_annotated_commit_free(base
);
2438 git_iterator_free(base_iter
);
2439 git_iterator_free(our_iter
);
2440 git_iterator_free(their_iter
);
2445 int git_merge_commits(
2447 git_repository
*repo
,
2448 const git_commit
*our_commit
,
2449 const git_commit
*their_commit
,
2450 const git_merge_options
*opts
)
2452 git_annotated_commit
*ours
= NULL
, *theirs
= NULL
, *base
= NULL
;
2455 if ((error
= git_annotated_commit_from_commit(&ours
, (git_commit
*)our_commit
)) < 0 ||
2456 (error
= git_annotated_commit_from_commit(&theirs
, (git_commit
*)their_commit
)) < 0)
2459 error
= merge_annotated_commits(out
, &base
, repo
, ours
, theirs
, 0, opts
);
2462 git_annotated_commit_free(ours
);
2463 git_annotated_commit_free(theirs
);
2464 git_annotated_commit_free(base
);
2468 /* Merge setup / cleanup */
2470 static int write_merge_head(
2471 git_repository
*repo
,
2472 const git_annotated_commit
*heads
[],
2475 git_filebuf file
= GIT_FILEBUF_INIT
;
2476 git_buf file_path
= GIT_BUF_INIT
;
2480 GIT_ASSERT_ARG(repo
);
2481 GIT_ASSERT_ARG(heads
);
2483 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_HEAD_FILE
)) < 0 ||
2484 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2487 for (i
= 0; i
< heads_len
; i
++) {
2488 if ((error
= git_filebuf_printf(&file
, "%s\n", heads
[i
]->id_str
)) < 0)
2492 error
= git_filebuf_commit(&file
);
2496 git_filebuf_cleanup(&file
);
2498 git_buf_dispose(&file_path
);
2503 static int write_merge_mode(git_repository
*repo
)
2505 git_filebuf file
= GIT_FILEBUF_INIT
;
2506 git_buf file_path
= GIT_BUF_INIT
;
2509 GIT_ASSERT_ARG(repo
);
2511 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MODE_FILE
)) < 0 ||
2512 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2515 if ((error
= git_filebuf_write(&file
, "no-ff", 5)) < 0)
2518 error
= git_filebuf_commit(&file
);
2522 git_filebuf_cleanup(&file
);
2524 git_buf_dispose(&file_path
);
2529 struct merge_msg_entry
{
2530 const git_annotated_commit
*merge_head
;
2534 static int msg_entry_is_branch(
2535 const struct merge_msg_entry
*entry
,
2536 git_vector
*entries
)
2538 GIT_UNUSED(entries
);
2540 return (entry
->written
== 0 &&
2541 entry
->merge_head
->remote_url
== NULL
&&
2542 entry
->merge_head
->ref_name
!= NULL
&&
2543 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0);
2546 static int msg_entry_is_tracking(
2547 const struct merge_msg_entry
*entry
,
2548 git_vector
*entries
)
2550 GIT_UNUSED(entries
);
2552 return (entry
->written
== 0 &&
2553 entry
->merge_head
->remote_url
== NULL
&&
2554 entry
->merge_head
->ref_name
!= NULL
&&
2555 git__strncmp(GIT_REFS_REMOTES_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_REMOTES_DIR
)) == 0);
2558 static int msg_entry_is_tag(
2559 const struct merge_msg_entry
*entry
,
2560 git_vector
*entries
)
2562 GIT_UNUSED(entries
);
2564 return (entry
->written
== 0 &&
2565 entry
->merge_head
->remote_url
== NULL
&&
2566 entry
->merge_head
->ref_name
!= NULL
&&
2567 git__strncmp(GIT_REFS_TAGS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_TAGS_DIR
)) == 0);
2570 static int msg_entry_is_remote(
2571 const struct merge_msg_entry
*entry
,
2572 git_vector
*entries
)
2574 if (entry
->written
== 0 &&
2575 entry
->merge_head
->remote_url
!= NULL
&&
2576 entry
->merge_head
->ref_name
!= NULL
&&
2577 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0)
2579 struct merge_msg_entry
*existing
;
2581 /* Match only branches from the same remote */
2582 if (entries
->length
== 0)
2585 existing
= git_vector_get(entries
, 0);
2587 return (git__strcmp(existing
->merge_head
->remote_url
,
2588 entry
->merge_head
->remote_url
) == 0);
2594 static int msg_entry_is_oid(
2595 const struct merge_msg_entry
*merge_msg_entry
)
2597 return (merge_msg_entry
->written
== 0 &&
2598 merge_msg_entry
->merge_head
->ref_name
== NULL
&&
2599 merge_msg_entry
->merge_head
->remote_url
== NULL
);
2602 static int merge_msg_entry_written(
2603 const struct merge_msg_entry
*merge_msg_entry
)
2605 return (merge_msg_entry
->written
== 1);
2608 static int merge_msg_entries(
2610 const struct merge_msg_entry
*entries
,
2612 int (*match
)(const struct merge_msg_entry
*entry
, git_vector
*entries
))
2615 int matches
, total
= 0;
2617 git_vector_clear(v
);
2619 for (i
= 0; i
< len
; i
++) {
2620 if ((matches
= match(&entries
[i
], v
)) < 0)
2625 git_vector_insert(v
, (struct merge_msg_entry
*)&entries
[i
]);
2632 static int merge_msg_write_entries(
2634 git_vector
*entries
,
2635 const char *item_name
,
2636 const char *item_plural_name
,
2637 size_t ref_name_skip
,
2641 struct merge_msg_entry
*entry
;
2645 if (entries
->length
== 0)
2648 if (sep
&& (error
= git_filebuf_printf(file
, "%c ", sep
)) < 0)
2651 if ((error
= git_filebuf_printf(file
, "%s ",
2652 (entries
->length
== 1) ? item_name
: item_plural_name
)) < 0)
2655 git_vector_foreach(entries
, i
, entry
) {
2657 (error
= git_filebuf_printf(file
, "%s", (i
== entries
->length
- 1) ? " and " : ", ")) < 0)
2660 if ((error
= git_filebuf_printf(file
, "'%s'", entry
->merge_head
->ref_name
+ ref_name_skip
)) < 0)
2667 error
= git_filebuf_printf(file
, " of %s", source
);
2673 static int merge_msg_write_branches(
2675 git_vector
*entries
,
2678 return merge_msg_write_entries(file
, entries
,
2679 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), NULL
, sep
);
2682 static int merge_msg_write_tracking(
2684 git_vector
*entries
,
2687 return merge_msg_write_entries(file
, entries
,
2688 "remote-tracking branch", "remote-tracking branches", 0, NULL
, sep
);
2691 static int merge_msg_write_tags(
2693 git_vector
*entries
,
2696 return merge_msg_write_entries(file
, entries
,
2697 "tag", "tags", strlen(GIT_REFS_TAGS_DIR
), NULL
, sep
);
2700 static int merge_msg_write_remotes(
2702 git_vector
*entries
,
2707 if (entries
->length
== 0)
2710 source
= ((struct merge_msg_entry
*)entries
->contents
[0])->merge_head
->remote_url
;
2712 return merge_msg_write_entries(file
, entries
,
2713 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), source
, sep
);
2716 static int write_merge_msg(
2717 git_repository
*repo
,
2718 const git_annotated_commit
*heads
[],
2721 git_filebuf file
= GIT_FILEBUF_INIT
;
2722 git_buf file_path
= GIT_BUF_INIT
;
2723 struct merge_msg_entry
*entries
;
2724 git_vector matching
= GIT_VECTOR_INIT
;
2729 GIT_ASSERT_ARG(repo
);
2730 GIT_ASSERT_ARG(heads
);
2732 entries
= git__calloc(heads_len
, sizeof(struct merge_msg_entry
));
2733 GIT_ERROR_CHECK_ALLOC(entries
);
2735 if (git_vector_init(&matching
, heads_len
, NULL
) < 0) {
2740 for (i
= 0; i
< heads_len
; i
++)
2741 entries
[i
].merge_head
= heads
[i
];
2743 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
2744 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0 ||
2745 (error
= git_filebuf_write(&file
, "Merge ", 6)) < 0)
2749 * This is to emulate the format of MERGE_MSG by core git.
2751 * Core git will write all the commits specified by OID, in the order
2752 * provided, until the first named branch or tag is reached, at which
2753 * point all branches will be written in the order provided, then all
2754 * tags, then all remote tracking branches and finally all commits that
2755 * were specified by OID that were not already written.
2759 for (i
= 0; i
< heads_len
; i
++) {
2760 if (!msg_entry_is_oid(&entries
[i
]))
2763 if ((error
= git_filebuf_printf(&file
,
2764 "%scommit '%s'", (i
> 0) ? "; " : "",
2765 entries
[i
].merge_head
->id_str
)) < 0)
2768 entries
[i
].written
= 1;
2774 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_branch
)) < 0 ||
2775 (error
= merge_msg_write_branches(&file
, &matching
, sep
)) < 0)
2778 if (matching
.length
)
2781 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tracking
)) < 0 ||
2782 (error
= merge_msg_write_tracking(&file
, &matching
, sep
)) < 0)
2785 if (matching
.length
)
2788 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tag
)) < 0 ||
2789 (error
= merge_msg_write_tags(&file
, &matching
, sep
)) < 0)
2792 if (matching
.length
)
2795 /* We should never be called with multiple remote branches, but handle
2796 * it in case we are... */
2797 while ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_remote
)) > 0) {
2798 if ((error
= merge_msg_write_remotes(&file
, &matching
, sep
)) < 0)
2801 if (matching
.length
)
2808 for (i
= 0; i
< heads_len
; i
++) {
2809 if (merge_msg_entry_written(&entries
[i
]))
2812 if ((error
= git_filebuf_printf(&file
, "; commit '%s'",
2813 entries
[i
].merge_head
->id_str
)) < 0)
2817 if ((error
= git_filebuf_printf(&file
, "\n")) < 0 ||
2818 (error
= git_filebuf_commit(&file
)) < 0)
2823 git_filebuf_cleanup(&file
);
2825 git_buf_dispose(&file_path
);
2827 git_vector_free(&matching
);
2833 int git_merge__setup(
2834 git_repository
*repo
,
2835 const git_annotated_commit
*our_head
,
2836 const git_annotated_commit
*heads
[],
2841 GIT_ASSERT_ARG(repo
);
2842 GIT_ASSERT_ARG(our_head
);
2843 GIT_ASSERT_ARG(heads
);
2845 if ((error
= git_repository__set_orig_head(repo
, git_annotated_commit_id(our_head
))) == 0 &&
2846 (error
= write_merge_head(repo
, heads
, heads_len
)) == 0 &&
2847 (error
= write_merge_mode(repo
)) == 0) {
2848 error
= write_merge_msg(repo
, heads
, heads_len
);
2854 /* Merge branches */
2856 static int merge_ancestor_head(
2857 git_annotated_commit
**ancestor_head
,
2858 git_repository
*repo
,
2859 const git_annotated_commit
*our_head
,
2860 const git_annotated_commit
**their_heads
,
2861 size_t their_heads_len
)
2863 git_oid
*oids
, ancestor_oid
;
2864 size_t i
, alloc_len
;
2867 GIT_ASSERT_ARG(repo
);
2868 GIT_ASSERT_ARG(our_head
);
2869 GIT_ASSERT_ARG(their_heads
);
2871 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len
, their_heads_len
, 1);
2872 oids
= git__calloc(alloc_len
, sizeof(git_oid
));
2873 GIT_ERROR_CHECK_ALLOC(oids
);
2875 git_oid_cpy(&oids
[0], git_commit_id(our_head
->commit
));
2877 for (i
= 0; i
< their_heads_len
; i
++)
2878 git_oid_cpy(&oids
[i
+ 1], git_annotated_commit_id(their_heads
[i
]));
2880 if ((error
= git_merge_base_many(&ancestor_oid
, repo
, their_heads_len
+ 1, oids
)) < 0)
2883 error
= git_annotated_commit_lookup(ancestor_head
, repo
, &ancestor_oid
);
2890 static const char *merge_their_label(const char *branchname
)
2894 if ((slash
= strrchr(branchname
, '/')) == NULL
)
2897 if (*(slash
+1) == '\0')
2903 static int merge_normalize_checkout_opts(
2904 git_checkout_options
*out
,
2905 git_repository
*repo
,
2906 const git_checkout_options
*given_checkout_opts
,
2907 unsigned int checkout_strategy
,
2908 git_annotated_commit
*ancestor
,
2909 const git_annotated_commit
*our_head
,
2910 const git_annotated_commit
**their_heads
,
2911 size_t their_heads_len
)
2913 git_checkout_options default_checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
2918 if (given_checkout_opts
!= NULL
)
2919 memcpy(out
, given_checkout_opts
, sizeof(git_checkout_options
));
2921 memcpy(out
, &default_checkout_opts
, sizeof(git_checkout_options
));
2923 out
->checkout_strategy
= checkout_strategy
;
2925 if (!out
->ancestor_label
) {
2926 if (ancestor
&& ancestor
->type
== GIT_ANNOTATED_COMMIT_REAL
)
2927 out
->ancestor_label
= git_commit_summary(ancestor
->commit
);
2929 out
->ancestor_label
= "merged common ancestors";
2931 out
->ancestor_label
= "empty base";
2934 if (!out
->our_label
) {
2935 if (our_head
&& our_head
->ref_name
)
2936 out
->our_label
= our_head
->ref_name
;
2938 out
->our_label
= "ours";
2941 if (!out
->their_label
) {
2942 if (their_heads_len
== 1 && their_heads
[0]->ref_name
)
2943 out
->their_label
= merge_their_label(their_heads
[0]->ref_name
);
2944 else if (their_heads_len
== 1)
2945 out
->their_label
= their_heads
[0]->id_str
;
2947 out
->their_label
= "theirs";
2953 static int merge_check_index(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
2955 git_tree
*head_tree
= NULL
;
2956 git_index
*index_repo
= NULL
;
2957 git_iterator
*iter_repo
= NULL
, *iter_new
= NULL
;
2958 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2959 git_diff
*staged_diff_list
= NULL
, *index_diff_list
= NULL
;
2960 git_diff_delta
*delta
;
2961 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
2962 git_vector staged_paths
= GIT_VECTOR_INIT
;
2966 GIT_UNUSED(merged_paths
);
2970 /* No staged changes may exist unless the change staged is identical to
2971 * the result of the merge. This allows one to apply to merge manually,
2972 * then run merge. Any other staged change would be overwritten by
2975 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
2976 (error
= git_repository_index(&index_repo
, repo
)) < 0 ||
2977 (error
= git_diff_tree_to_index(&staged_diff_list
, repo
, head_tree
, index_repo
, &opts
)) < 0)
2980 if (staged_diff_list
->deltas
.length
== 0)
2983 git_vector_foreach(&staged_diff_list
->deltas
, i
, delta
) {
2984 if ((error
= git_vector_insert(&staged_paths
, (char *)delta
->new_file
.path
)) < 0)
2988 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2989 iter_opts
.pathlist
.strings
= (char **)staged_paths
.contents
;
2990 iter_opts
.pathlist
.count
= staged_paths
.length
;
2992 if ((error
= git_iterator_for_index(&iter_repo
, repo
, index_repo
, &iter_opts
)) < 0 ||
2993 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
2994 (error
= git_diff__from_iterators(&index_diff_list
, repo
, iter_repo
, iter_new
, &opts
)) < 0)
2997 *conflicts
= index_diff_list
->deltas
.length
;
3000 git_tree_free(head_tree
);
3001 git_index_free(index_repo
);
3002 git_iterator_free(iter_repo
);
3003 git_iterator_free(iter_new
);
3004 git_diff_free(staged_diff_list
);
3005 git_diff_free(index_diff_list
);
3006 git_vector_free(&staged_paths
);
3011 static int merge_check_workdir(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
3013 git_diff
*wd_diff_list
= NULL
;
3014 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
3017 GIT_UNUSED(index_new
);
3021 /* We need to have merged at least 1 file for the possibility to exist to
3022 * have conflicts with the workdir. Passing 0 as the pathspec count paramter
3023 * will consider all files in the working directory, that is, we may detect
3024 * a conflict if there were untracked files in the workdir prior to starting
3025 * the merge. This typically happens when cherry-picking a commmit whose
3026 * changes have already been applied.
3028 if (merged_paths
->length
== 0)
3031 opts
.flags
|= GIT_DIFF_INCLUDE_UNTRACKED
;
3033 /* Workdir changes may exist iff they do not conflict with changes that
3034 * will be applied by the merge (including conflicts). Ensure that there
3035 * are no changes in the workdir to these paths.
3037 opts
.flags
|= GIT_DIFF_DISABLE_PATHSPEC_MATCH
;
3038 opts
.pathspec
.count
= merged_paths
->length
;
3039 opts
.pathspec
.strings
= (char **)merged_paths
->contents
;
3040 opts
.ignore_submodules
= GIT_SUBMODULE_IGNORE_ALL
;
3042 if ((error
= git_diff_index_to_workdir(&wd_diff_list
, repo
, NULL
, &opts
)) < 0)
3045 *conflicts
= wd_diff_list
->deltas
.length
;
3048 git_diff_free(wd_diff_list
);
3053 int git_merge__check_result(git_repository
*repo
, git_index
*index_new
)
3055 git_tree
*head_tree
= NULL
;
3056 git_iterator
*iter_head
= NULL
, *iter_new
= NULL
;
3057 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
3058 git_diff
*merged_list
= NULL
;
3059 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
3060 git_diff_delta
*delta
;
3061 git_vector paths
= GIT_VECTOR_INIT
;
3062 size_t i
, index_conflicts
= 0, wd_conflicts
= 0, conflicts
;
3063 const git_index_entry
*e
;
3066 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
3068 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
3069 (error
= git_iterator_for_tree(&iter_head
, head_tree
, &iter_opts
)) < 0 ||
3070 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
3071 (error
= git_diff__from_iterators(&merged_list
, repo
, iter_head
, iter_new
, &opts
)) < 0)
3074 git_vector_foreach(&merged_list
->deltas
, i
, delta
) {
3075 if ((error
= git_vector_insert(&paths
, (char *)delta
->new_file
.path
)) < 0)
3079 for (i
= 0; i
< git_index_entrycount(index_new
); i
++) {
3080 e
= git_index_get_byindex(index_new
, i
);
3082 if (git_index_entry_is_conflict(e
) &&
3083 (git_vector_last(&paths
) == NULL
||
3084 strcmp(git_vector_last(&paths
), e
->path
) != 0)) {
3086 if ((error
= git_vector_insert(&paths
, (char *)e
->path
)) < 0)
3091 /* Make sure the index and workdir state do not prevent merging */
3092 if ((error
= merge_check_index(&index_conflicts
, repo
, index_new
, &paths
)) < 0 ||
3093 (error
= merge_check_workdir(&wd_conflicts
, repo
, index_new
, &paths
)) < 0)
3096 if ((conflicts
= index_conflicts
+ wd_conflicts
) > 0) {
3097 git_error_set(GIT_ERROR_MERGE
, "%" PRIuZ
" uncommitted change%s would be overwritten by merge",
3098 conflicts
, (conflicts
!= 1) ? "s" : "");
3099 error
= GIT_ECONFLICT
;
3103 git_vector_free(&paths
);
3104 git_tree_free(head_tree
);
3105 git_iterator_free(iter_head
);
3106 git_iterator_free(iter_new
);
3107 git_diff_free(merged_list
);
3112 int git_merge__append_conflicts_to_merge_msg(
3113 git_repository
*repo
,
3116 git_filebuf file
= GIT_FILEBUF_INIT
;
3117 git_buf file_path
= GIT_BUF_INIT
;
3118 const char *last
= NULL
;
3122 if (!git_index_has_conflicts(index
))
3125 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
3126 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_APPEND
, GIT_MERGE_FILE_MODE
)) < 0)
3129 git_filebuf_printf(&file
, "\nConflicts:\n");
3131 for (i
= 0; i
< git_index_entrycount(index
); i
++) {
3132 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
3134 if (!git_index_entry_is_conflict(e
))
3137 if (last
== NULL
|| strcmp(e
->path
, last
) != 0)
3138 git_filebuf_printf(&file
, "\t%s\n", e
->path
);
3143 error
= git_filebuf_commit(&file
);
3147 git_filebuf_cleanup(&file
);
3149 git_buf_dispose(&file_path
);
3154 static int merge_state_cleanup(git_repository
*repo
)
3156 const char *state_files
[] = {
3157 GIT_MERGE_HEAD_FILE
,
3158 GIT_MERGE_MODE_FILE
,
3162 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
3165 static int merge_heads(
3166 git_annotated_commit
**ancestor_head_out
,
3167 git_annotated_commit
**our_head_out
,
3168 git_repository
*repo
,
3169 git_reference
*our_ref
,
3170 const git_annotated_commit
**their_heads
,
3171 size_t their_heads_len
)
3173 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3176 *ancestor_head_out
= NULL
;
3177 *our_head_out
= NULL
;
3179 if ((error
= git_annotated_commit_from_ref(&our_head
, repo
, our_ref
)) < 0)
3182 if ((error
= merge_ancestor_head(&ancestor_head
, repo
, our_head
, their_heads
, their_heads_len
)) < 0) {
3183 if (error
!= GIT_ENOTFOUND
)
3190 *ancestor_head_out
= ancestor_head
;
3191 *our_head_out
= our_head
;
3195 git_annotated_commit_free(ancestor_head
);
3196 git_annotated_commit_free(our_head
);
3202 static int merge_preference(git_merge_preference_t
*out
, git_repository
*repo
)
3206 int bool_value
, error
= 0;
3208 *out
= GIT_MERGE_PREFERENCE_NONE
;
3210 if ((error
= git_repository_config_snapshot(&config
, repo
)) < 0)
3213 if ((error
= git_config_get_string(&value
, config
, "merge.ff")) < 0) {
3214 if (error
== GIT_ENOTFOUND
) {
3222 if (git_config_parse_bool(&bool_value
, value
) == 0) {
3224 *out
|= GIT_MERGE_PREFERENCE_NO_FASTFORWARD
;
3226 if (strcasecmp(value
, "only") == 0)
3227 *out
|= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
;
3231 git_config_free(config
);
3235 int git_merge_analysis_for_ref(
3236 git_merge_analysis_t
*analysis_out
,
3237 git_merge_preference_t
*preference_out
,
3238 git_repository
*repo
,
3239 git_reference
*our_ref
,
3240 const git_annotated_commit
**their_heads
,
3241 size_t their_heads_len
)
3243 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3247 GIT_ASSERT_ARG(analysis_out
);
3248 GIT_ASSERT_ARG(preference_out
);
3249 GIT_ASSERT_ARG(repo
);
3250 GIT_ASSERT_ARG(their_heads
&& their_heads_len
> 0);
3252 if (their_heads_len
!= 1) {
3253 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3258 *analysis_out
= GIT_MERGE_ANALYSIS_NONE
;
3260 if ((error
= merge_preference(preference_out
, repo
)) < 0)
3263 if ((error
= git_reference__is_unborn_head(&unborn
, our_ref
, repo
)) < 0)
3267 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_UNBORN
;
3272 if ((error
= merge_heads(&ancestor_head
, &our_head
, repo
, our_ref
, their_heads
, their_heads_len
)) < 0)
3275 /* We're up-to-date if we're trying to merge our own common ancestor. */
3276 if (ancestor_head
&& git_oid_equal(
3277 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(their_heads
[0])))
3278 *analysis_out
|= GIT_MERGE_ANALYSIS_UP_TO_DATE
;
3280 /* We're fastforwardable if we're our own common ancestor. */
3281 else if (ancestor_head
&& git_oid_equal(
3282 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(our_head
)))
3283 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_NORMAL
;
3285 /* Otherwise, just a normal merge is possible. */
3287 *analysis_out
|= GIT_MERGE_ANALYSIS_NORMAL
;
3290 git_annotated_commit_free(ancestor_head
);
3291 git_annotated_commit_free(our_head
);
3295 int git_merge_analysis(
3296 git_merge_analysis_t
*analysis_out
,
3297 git_merge_preference_t
*preference_out
,
3298 git_repository
*repo
,
3299 const git_annotated_commit
**their_heads
,
3300 size_t their_heads_len
)
3302 git_reference
*head_ref
= NULL
;
3305 if ((error
= git_reference_lookup(&head_ref
, repo
, GIT_HEAD_FILE
)) < 0) {
3306 git_error_set(GIT_ERROR_MERGE
, "failed to lookup HEAD reference");
3310 error
= git_merge_analysis_for_ref(analysis_out
, preference_out
, repo
, head_ref
, their_heads
, their_heads_len
);
3312 git_reference_free(head_ref
);
3318 git_repository
*repo
,
3319 const git_annotated_commit
**their_heads
,
3320 size_t their_heads_len
,
3321 const git_merge_options
*merge_opts
,
3322 const git_checkout_options
*given_checkout_opts
)
3324 git_reference
*our_ref
= NULL
;
3325 git_checkout_options checkout_opts
;
3326 git_annotated_commit
*our_head
= NULL
, *base
= NULL
;
3327 git_index
*repo_index
= NULL
, *index
= NULL
;
3328 git_indexwriter indexwriter
= GIT_INDEXWRITER_INIT
;
3329 unsigned int checkout_strategy
;
3332 GIT_ASSERT_ARG(repo
);
3333 GIT_ASSERT_ARG(their_heads
&& their_heads_len
> 0);
3335 if (their_heads_len
!= 1) {
3336 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3340 if ((error
= git_repository__ensure_not_bare(repo
, "merge")) < 0)
3343 checkout_strategy
= given_checkout_opts
?
3344 given_checkout_opts
->checkout_strategy
:
3347 if ((error
= git_indexwriter_init_for_operation(&indexwriter
, repo
,
3348 &checkout_strategy
)) < 0)
3351 if ((error
= git_repository_index(&repo_index
, repo
) < 0) ||
3352 (error
= git_index_read(repo_index
, 0) < 0))
3355 /* Write the merge setup files to the repository. */
3356 if ((error
= git_annotated_commit_from_head(&our_head
, repo
)) < 0 ||
3357 (error
= git_merge__setup(repo
, our_head
, their_heads
,
3358 their_heads_len
)) < 0)
3363 if ((error
= merge_annotated_commits(&index
, &base
, repo
, our_head
,
3364 (git_annotated_commit
*)their_heads
[0], 0, merge_opts
)) < 0 ||
3365 (error
= git_merge__check_result(repo
, index
)) < 0 ||
3366 (error
= git_merge__append_conflicts_to_merge_msg(repo
, index
)) < 0)
3369 /* check out the merge results */
3371 if ((error
= merge_normalize_checkout_opts(&checkout_opts
, repo
,
3372 given_checkout_opts
, checkout_strategy
,
3373 base
, our_head
, their_heads
, their_heads_len
)) < 0 ||
3374 (error
= git_checkout_index(repo
, index
, &checkout_opts
)) < 0)
3377 error
= git_indexwriter_commit(&indexwriter
);
3381 merge_state_cleanup(repo
);
3383 git_indexwriter_cleanup(&indexwriter
);
3384 git_index_free(index
);
3385 git_annotated_commit_free(our_head
);
3386 git_annotated_commit_free(base
);
3387 git_reference_free(our_ref
);
3388 git_index_free(repo_index
);
3393 int git_merge_options_init(git_merge_options
*opts
, unsigned int version
)
3395 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3396 opts
, version
, git_merge_options
, GIT_MERGE_OPTIONS_INIT
);
3400 #ifndef GIT_DEPRECATE_HARD
3401 int git_merge_init_options(git_merge_options
*opts
, unsigned int version
)
3403 return git_merge_options_init(opts
, version
);
3407 int git_merge_file_input_init(git_merge_file_input
*input
, unsigned int version
)
3409 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3410 input
, version
, git_merge_file_input
, GIT_MERGE_FILE_INPUT_INIT
);
3414 #ifndef GIT_DEPRECATE_HARD
3415 int git_merge_file_init_input(git_merge_file_input
*input
, unsigned int version
)
3417 return git_merge_file_input_init(input
, version
);
3421 int git_merge_file_options_init(
3422 git_merge_file_options
*opts
, unsigned int version
)
3424 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3425 opts
, version
, git_merge_file_options
, GIT_MERGE_FILE_OPTIONS_INIT
);
3429 #ifndef GIT_DEPRECATE_HARD
3430 int git_merge_file_init_options(
3431 git_merge_file_options
*opts
, unsigned int version
)
3433 return git_merge_file_options_init(opts
, version
);