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
;
71 /* Merge base computation */
73 int merge_bases_many(git_commit_list
**out
, git_revwalk
**walk_out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
75 git_revwalk
*walk
= NULL
;
77 git_commit_list
*result
= NULL
;
78 git_commit_list_node
*commit
;
83 git_error_set(GIT_ERROR_INVALID
, "at least two commits are required to find an ancestor");
87 if (git_vector_init(&list
, length
- 1, NULL
) < 0)
90 if (git_revwalk_new(&walk
, repo
) < 0)
93 for (i
= 1; i
< length
; i
++) {
94 commit
= git_revwalk__commit_lookup(walk
, &input_array
[i
]);
98 git_vector_insert(&list
, commit
);
101 commit
= git_revwalk__commit_lookup(walk
, &input_array
[0]);
105 if (git_merge__bases_many(&result
, walk
, commit
, &list
) < 0)
109 git_error_set(GIT_ERROR_MERGE
, "no merge base found");
110 error
= GIT_ENOTFOUND
;
117 git_vector_free(&list
);
121 git_vector_free(&list
);
122 git_revwalk_free(walk
);
126 int git_merge_base_many(git_oid
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
129 git_commit_list
*result
= NULL
;
132 assert(out
&& repo
&& input_array
);
134 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
137 git_oid_cpy(out
, &result
->item
->oid
);
139 git_commit_list_free(&result
);
140 git_revwalk_free(walk
);
145 int git_merge_bases_many(git_oidarray
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
148 git_commit_list
*list
, *result
= NULL
;
150 git_array_oid_t array
;
152 assert(out
&& repo
&& input_array
);
154 if ((error
= merge_bases_many(&result
, &walk
, repo
, length
, input_array
)) < 0)
157 git_array_init(array
);
161 git_oid
*id
= git_array_alloc(array
);
167 git_oid_cpy(id
, &list
->item
->oid
);
171 git_oidarray__from_array(out
, &array
);
174 git_commit_list_free(&result
);
175 git_revwalk_free(walk
);
180 int git_merge_base_octopus(git_oid
*out
, git_repository
*repo
, size_t length
, const git_oid input_array
[])
186 assert(out
&& repo
&& input_array
);
189 git_error_set(GIT_ERROR_INVALID
, "at least two commits are required to find an ancestor");
193 result
= input_array
[0];
194 for (i
= 1; i
< length
; i
++) {
195 error
= git_merge_base(&result
, repo
, &result
, &input_array
[i
]);
205 static int merge_bases(git_commit_list
**out
, git_revwalk
**walk_out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
209 git_commit_list
*result
= NULL
;
210 git_commit_list_node
*commit
;
213 if (git_revwalk_new(&walk
, repo
) < 0)
216 commit
= git_revwalk__commit_lookup(walk
, two
);
220 /* This is just one value, so we can do it on the stack */
221 memset(&list
, 0x0, sizeof(git_vector
));
222 contents
[0] = commit
;
224 list
.contents
= contents
;
226 commit
= git_revwalk__commit_lookup(walk
, one
);
230 if (git_merge__bases_many(&result
, walk
, commit
, &list
) < 0)
234 git_revwalk_free(walk
);
235 git_error_set(GIT_ERROR_MERGE
, "no merge base found");
236 return GIT_ENOTFOUND
;
245 git_revwalk_free(walk
);
250 int git_merge_base(git_oid
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
254 git_commit_list
*result
;
256 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
259 git_oid_cpy(out
, &result
->item
->oid
);
260 git_commit_list_free(&result
);
261 git_revwalk_free(walk
);
266 int git_merge_bases(git_oidarray
*out
, git_repository
*repo
, const git_oid
*one
, const git_oid
*two
)
270 git_commit_list
*result
, *list
;
271 git_array_oid_t array
;
273 git_array_init(array
);
275 if ((error
= merge_bases(&result
, &walk
, repo
, one
, two
)) < 0)
280 git_oid
*id
= git_array_alloc(array
);
284 git_oid_cpy(id
, &list
->item
->oid
);
288 git_oidarray__from_array(out
, &array
);
289 git_commit_list_free(&result
);
290 git_revwalk_free(walk
);
295 git_commit_list_free(&result
);
296 git_revwalk_free(walk
);
300 static int interesting(git_pqueue
*list
)
304 for (i
= 0; i
< git_pqueue_size(list
); i
++) {
305 git_commit_list_node
*commit
= git_pqueue_get(list
, i
);
306 if ((commit
->flags
& STALE
) == 0)
313 static int clear_commit_marks_1(git_commit_list
**plist
,
314 git_commit_list_node
*commit
, unsigned int mark
)
319 if (!(mark
& commit
->flags
))
322 commit
->flags
&= ~mark
;
324 for (i
= 1; i
< commit
->out_degree
; i
++) {
325 git_commit_list_node
*p
= commit
->parents
[i
];
326 if (git_commit_list_insert(p
, plist
) == NULL
)
330 commit
= commit
->out_degree
? commit
->parents
[0] : NULL
;
336 static int clear_commit_marks_many(git_vector
*commits
, unsigned int mark
)
338 git_commit_list
*list
= NULL
;
339 git_commit_list_node
*c
;
342 git_vector_foreach(commits
, i
, c
) {
343 if (git_commit_list_insert(c
, &list
) == NULL
)
348 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
353 static int clear_commit_marks(git_commit_list_node
*commit
, unsigned int mark
)
355 git_commit_list
*list
= NULL
;
356 if (git_commit_list_insert(commit
, &list
) == NULL
)
359 if (clear_commit_marks_1(&list
, git_commit_list_pop(&list
), mark
) < 0)
364 static int paint_down_to_common(
365 git_commit_list
**out
, git_revwalk
*walk
, git_commit_list_node
*one
, git_vector
*twos
)
368 git_commit_list
*result
= NULL
;
369 git_commit_list_node
*two
;
374 if (git_pqueue_init(&list
, 0, twos
->length
* 2, git_commit_list_time_cmp
) < 0)
377 one
->flags
|= PARENT1
;
378 if (git_pqueue_insert(&list
, one
) < 0)
381 git_vector_foreach(twos
, i
, two
) {
382 if (git_commit_list_parse(walk
, two
) < 0)
385 two
->flags
|= PARENT2
;
387 if (git_pqueue_insert(&list
, two
) < 0)
391 /* as long as there are non-STALE commits */
392 while (interesting(&list
)) {
393 git_commit_list_node
*commit
= git_pqueue_pop(&list
);
399 flags
= commit
->flags
& (PARENT1
| PARENT2
| STALE
);
400 if (flags
== (PARENT1
| PARENT2
)) {
401 if (!(commit
->flags
& RESULT
)) {
402 commit
->flags
|= RESULT
;
403 if (git_commit_list_insert(commit
, &result
) == NULL
)
406 /* we mark the parents of a merge stale */
410 for (i
= 0; i
< commit
->out_degree
; i
++) {
411 git_commit_list_node
*p
= commit
->parents
[i
];
412 if ((p
->flags
& flags
) == flags
)
415 if ((error
= git_commit_list_parse(walk
, p
)) < 0)
419 if (git_pqueue_insert(&list
, p
) < 0)
424 git_pqueue_free(&list
);
429 static int remove_redundant(git_revwalk
*walk
, git_vector
*commits
)
431 git_vector work
= GIT_VECTOR_INIT
;
432 unsigned char *redundant
;
433 unsigned int *filled_index
;
437 redundant
= git__calloc(commits
->length
, 1);
438 GIT_ERROR_CHECK_ALLOC(redundant
);
439 filled_index
= git__calloc((commits
->length
- 1), sizeof(unsigned int));
440 GIT_ERROR_CHECK_ALLOC(filled_index
);
442 for (i
= 0; i
< commits
->length
; ++i
) {
443 if ((error
= git_commit_list_parse(walk
, commits
->contents
[i
])) < 0)
447 for (i
= 0; i
< commits
->length
; ++i
) {
448 git_commit_list
*common
= NULL
;
449 git_commit_list_node
*commit
= commits
->contents
[i
];
454 git_vector_clear(&work
);
456 for (j
= 0; j
< commits
->length
; j
++) {
457 if (i
== j
|| redundant
[j
])
460 filled_index
[work
.length
] = j
;
461 if ((error
= git_vector_insert(&work
, commits
->contents
[j
])) < 0)
465 error
= paint_down_to_common(&common
, walk
, commit
, &work
);
469 if (commit
->flags
& PARENT2
)
472 for (j
= 0; j
< work
.length
; j
++) {
473 git_commit_list_node
*w
= work
.contents
[j
];
474 if (w
->flags
& PARENT1
)
475 redundant
[filled_index
[j
]] = 1;
478 git_commit_list_free(&common
);
480 if ((error
= clear_commit_marks(commit
, ALL_FLAGS
)) < 0 ||
481 (error
= clear_commit_marks_many(&work
, ALL_FLAGS
)) < 0)
485 for (i
= 0; i
< commits
->length
; ++i
) {
487 commits
->contents
[i
] = NULL
;
491 git__free(redundant
);
492 git__free(filled_index
);
493 git_vector_free(&work
);
497 int git_merge__bases_many(git_commit_list
**out
, git_revwalk
*walk
, git_commit_list_node
*one
, git_vector
*twos
)
501 git_commit_list_node
*two
;
502 git_commit_list
*result
= NULL
, *tmp
= NULL
;
504 /* If there's only the one commit, there can be no merge bases */
505 if (twos
->length
== 0) {
510 /* if the commit is repeated, we have a our merge base already */
511 git_vector_foreach(twos
, i
, two
) {
513 return git_commit_list_insert(one
, out
) ? 0 : -1;
516 if (git_commit_list_parse(walk
, one
) < 0)
519 error
= paint_down_to_common(&result
, walk
, one
, twos
);
523 /* filter out any stale commits in the results */
528 git_commit_list_node
*c
= git_commit_list_pop(&tmp
);
529 if (!(c
->flags
& STALE
))
530 if (git_commit_list_insert_by_date(c
, &result
) == NULL
)
535 * more than one merge base -- see if there are redundant merge
536 * bases and remove them
538 if (result
&& result
->next
) {
539 git_vector redundant
= GIT_VECTOR_INIT
;
542 git_vector_insert(&redundant
, git_commit_list_pop(&result
));
544 if ((error
= clear_commit_marks(one
, ALL_FLAGS
)) < 0 ||
545 (error
= clear_commit_marks_many(twos
, ALL_FLAGS
)) < 0 ||
546 (error
= remove_redundant(walk
, &redundant
)) < 0) {
547 git_vector_free(&redundant
);
551 git_vector_foreach(&redundant
, i
, two
) {
553 git_commit_list_insert_by_date(two
, &result
);
556 git_vector_free(&redundant
);
563 int git_repository_mergehead_foreach(
564 git_repository
*repo
,
565 git_repository_mergehead_foreach_cb cb
,
568 git_buf merge_head_path
= GIT_BUF_INIT
, merge_head_file
= GIT_BUF_INIT
;
576 if ((error
= git_buf_joinpath(&merge_head_path
, repo
->gitdir
,
577 GIT_MERGE_HEAD_FILE
)) < 0)
580 if ((error
= git_futils_readbuffer(&merge_head_file
,
581 git_buf_cstr(&merge_head_path
))) < 0)
584 buffer
= merge_head_file
.ptr
;
586 while ((line
= git__strsep(&buffer
, "\n")) != NULL
) {
587 if (strlen(line
) != GIT_OID_HEXSZ
) {
588 git_error_set(GIT_ERROR_INVALID
, "unable to parse OID - invalid length");
593 if ((error
= git_oid_fromstr(&oid
, line
)) < 0)
596 if ((error
= cb(&oid
, payload
)) != 0) {
597 git_error_set_after_callback(error
);
605 git_error_set(GIT_ERROR_MERGE
, "no EOL at line %"PRIuZ
, line_num
);
611 git_buf_dispose(&merge_head_path
);
612 git_buf_dispose(&merge_head_file
);
617 GIT_INLINE(int) index_entry_cmp(const git_index_entry
*a
, const git_index_entry
*b
)
622 return (b
->path
== NULL
) ? 0 : 1;
624 if ((value
= a
->mode
- b
->mode
) == 0 &&
625 (value
= git_oid__cmp(&a
->id
, &b
->id
)) == 0)
626 value
= strcmp(a
->path
, b
->path
);
631 /* Conflict resolution */
633 static int merge_conflict_resolve_trivial(
635 git_merge_diff_list
*diff_list
,
636 const git_merge_diff
*conflict
)
638 int ours_empty
, theirs_empty
;
639 int ours_changed
, theirs_changed
, ours_theirs_differ
;
640 git_index_entry
const *result
= NULL
;
643 assert(resolved
&& diff_list
&& conflict
);
647 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
648 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
651 if (conflict
->our_status
== GIT_DELTA_RENAMED
||
652 conflict
->their_status
== GIT_DELTA_RENAMED
)
655 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
656 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
658 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
659 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
660 ours_theirs_differ
= ours_changed
&& theirs_changed
&&
661 index_entry_cmp(&conflict
->our_entry
, &conflict
->their_entry
);
664 * Note: with only one ancestor, some cases are not distinct:
666 * 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge
667 * 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge
668 * 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge
670 * Note that the two cases that take D/F conflicts into account
671 * specifically do not need to be explicitly tested, as D/F conflicts
672 * would fail the *empty* test:
674 * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
675 * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
677 * Note that many of these cases need not be explicitly tested, as
678 * they simply degrade to "all different" cases (eg, 11):
680 * 4: ancest:(empty)^, head:head, remote:remote = result:no merge
681 * 7: ancest:ancest+, head:(empty), remote:remote = result:no merge
682 * 9: ancest:ancest+, head:head, remote:(empty) = result:no merge
683 * 11: ancest:ancest+, head:head, remote:remote = result:no merge
686 /* 5ALT: ancest:*, head:head, remote:head = result:head */
687 if (ours_changed
&& !ours_empty
&& !ours_theirs_differ
)
688 result
= &conflict
->our_entry
;
689 /* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
690 else if (ours_changed
&& ours_empty
&& theirs_empty
)
692 /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
693 else if (ours_empty
&& !theirs_changed
)
695 /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
696 else if (!ours_changed
&& theirs_empty
)
698 /* 13: ancest:ancest+, head:head, remote:ancest = result:head */
699 else if (ours_changed
&& !theirs_changed
)
700 result
= &conflict
->our_entry
;
701 /* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
702 else if (!ours_changed
&& theirs_changed
)
703 result
= &conflict
->their_entry
;
707 if (result
!= NULL
&&
708 GIT_MERGE_INDEX_ENTRY_EXISTS(*result
) &&
709 (error
= git_vector_insert(&diff_list
->staged
, (void *)result
)) >= 0)
712 /* Note: trivial resolution does not update the REUC. */
717 static int merge_conflict_resolve_one_removed(
719 git_merge_diff_list
*diff_list
,
720 const git_merge_diff
*conflict
)
722 int ours_empty
, theirs_empty
;
723 int ours_changed
, theirs_changed
;
726 assert(resolved
&& diff_list
&& conflict
);
730 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
||
731 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
734 ours_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
);
735 theirs_empty
= !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
);
737 ours_changed
= (conflict
->our_status
!= GIT_DELTA_UNMODIFIED
);
738 theirs_changed
= (conflict
->their_status
!= GIT_DELTA_UNMODIFIED
);
740 /* Removed in both */
741 if (ours_changed
&& ours_empty
&& theirs_empty
)
743 /* Removed in ours */
744 else if (ours_empty
&& !theirs_changed
)
746 /* Removed in theirs */
747 else if (!ours_changed
&& theirs_empty
)
751 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
756 static int merge_conflict_resolve_one_renamed(
758 git_merge_diff_list
*diff_list
,
759 const git_merge_diff
*conflict
)
761 int ours_renamed
, theirs_renamed
;
762 int ours_changed
, theirs_changed
;
763 git_index_entry
*merged
;
766 assert(resolved
&& diff_list
&& conflict
);
770 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
771 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
774 ours_renamed
= (conflict
->our_status
== GIT_DELTA_RENAMED
);
775 theirs_renamed
= (conflict
->their_status
== GIT_DELTA_RENAMED
);
777 if (!ours_renamed
&& !theirs_renamed
)
780 /* Reject one file in a 2->1 conflict */
781 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
782 conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
||
783 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
786 ours_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->our_entry
.id
) != 0);
787 theirs_changed
= (git_oid__cmp(&conflict
->ancestor_entry
.id
, &conflict
->their_entry
.id
) != 0);
789 /* if both are modified (and not to a common target) require a merge */
790 if (ours_changed
&& theirs_changed
&&
791 git_oid__cmp(&conflict
->our_entry
.id
, &conflict
->their_entry
.id
) != 0)
794 if ((merged
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
))) == NULL
)
798 memcpy(merged
, &conflict
->our_entry
, sizeof(git_index_entry
));
800 memcpy(merged
, &conflict
->their_entry
, sizeof(git_index_entry
));
803 merged
->path
= conflict
->our_entry
.path
;
805 merged
->path
= conflict
->their_entry
.path
;
809 git_vector_insert(&diff_list
->staged
, merged
);
810 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
815 static bool merge_conflict_can_resolve_contents(
816 const git_merge_diff
*conflict
)
818 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ||
819 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
822 /* Reject D/F conflicts */
823 if (conflict
->type
== GIT_MERGE_DIFF_DIRECTORY_FILE
)
826 /* Reject submodules. */
827 if (S_ISGITLINK(conflict
->ancestor_entry
.mode
) ||
828 S_ISGITLINK(conflict
->our_entry
.mode
) ||
829 S_ISGITLINK(conflict
->their_entry
.mode
))
832 /* Reject link/file conflicts. */
833 if ((S_ISLNK(conflict
->ancestor_entry
.mode
) ^
834 S_ISLNK(conflict
->our_entry
.mode
)) ||
835 (S_ISLNK(conflict
->ancestor_entry
.mode
) ^
836 S_ISLNK(conflict
->their_entry
.mode
)))
839 /* Reject name conflicts */
840 if (conflict
->type
== GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
||
841 conflict
->type
== GIT_MERGE_DIFF_RENAMED_ADDED
)
844 if ((conflict
->our_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
845 (conflict
->their_status
& GIT_DELTA_RENAMED
) == GIT_DELTA_RENAMED
&&
846 strcmp(conflict
->ancestor_entry
.path
, conflict
->their_entry
.path
) != 0)
852 static int merge_conflict_invoke_driver(
853 git_index_entry
**out
,
855 git_merge_driver
*driver
,
856 git_merge_diff_list
*diff_list
,
857 git_merge_driver_source
*src
)
859 git_index_entry
*result
;
860 git_buf buf
= GIT_BUF_INIT
;
869 if ((error
= driver
->apply(driver
, &path
, &mode
, &buf
, name
, src
)) < 0 ||
870 (error
= git_repository_odb(&odb
, src
->repo
)) < 0 ||
871 (error
= git_odb_write(&oid
, odb
, buf
.ptr
, buf
.size
, GIT_OBJECT_BLOB
)) < 0)
874 result
= git_pool_mallocz(&diff_list
->pool
, sizeof(git_index_entry
));
875 GIT_ERROR_CHECK_ALLOC(result
);
877 git_oid_cpy(&result
->id
, &oid
);
879 result
->file_size
= (uint32_t)buf
.size
;
881 result
->path
= git_pool_strdup(&diff_list
->pool
, path
);
882 GIT_ERROR_CHECK_ALLOC(result
->path
);
887 git_buf_dispose(&buf
);
893 static int merge_conflict_resolve_contents(
895 git_merge_diff_list
*diff_list
,
896 const git_merge_diff
*conflict
,
897 const git_merge_options
*merge_opts
,
898 const git_merge_file_options
*file_opts
)
900 git_merge_driver_source source
= {0};
901 git_merge_file_result result
= {0};
902 git_merge_driver
*driver
;
903 git_merge_driver__builtin builtin
= {{0}};
904 git_index_entry
*merge_result
;
907 bool fallback
= false;
910 assert(resolved
&& diff_list
&& conflict
);
914 if (!merge_conflict_can_resolve_contents(conflict
))
917 source
.repo
= diff_list
->repo
;
918 source
.default_driver
= merge_opts
->default_driver
;
919 source
.file_opts
= file_opts
;
920 source
.ancestor
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
921 &conflict
->ancestor_entry
: NULL
;
922 source
.ours
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
923 &conflict
->our_entry
: NULL
;
924 source
.theirs
= GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
925 &conflict
->their_entry
: NULL
;
927 if (file_opts
->favor
!= GIT_MERGE_FILE_FAVOR_NORMAL
) {
928 /* if the user requested a particular type of resolution (via the
929 * favor flag) then let that override the gitattributes and use
930 * the builtin driver.
933 builtin
.base
.apply
= git_merge_driver__builtin_apply
;
934 builtin
.favor
= file_opts
->favor
;
936 driver
= &builtin
.base
;
938 /* find the merge driver for this file */
939 if ((error
= git_merge_driver_for_source(&name
, &driver
, &source
)) < 0)
947 error
= merge_conflict_invoke_driver(&merge_result
, name
, driver
,
950 if (error
== GIT_PASSTHROUGH
)
955 error
= merge_conflict_invoke_driver(&merge_result
, "text",
956 &git_merge_driver__text
.base
, diff_list
, &source
);
960 if (error
== GIT_EMERGECONFLICT
)
966 git_vector_insert(&diff_list
->staged
, merge_result
);
967 git_vector_insert(&diff_list
->resolved
, (git_merge_diff
*)conflict
);
972 git_merge_file_result_free(&result
);
978 static int merge_conflict_resolve(
980 git_merge_diff_list
*diff_list
,
981 const git_merge_diff
*conflict
,
982 const git_merge_options
*merge_opts
,
983 const git_merge_file_options
*file_opts
)
990 if ((error
= merge_conflict_resolve_trivial(
991 &resolved
, diff_list
, conflict
)) < 0)
994 if (!resolved
&& (error
= merge_conflict_resolve_one_removed(
995 &resolved
, diff_list
, conflict
)) < 0)
998 if (!resolved
&& (error
= merge_conflict_resolve_one_renamed(
999 &resolved
, diff_list
, conflict
)) < 0)
1002 if (!resolved
&& (error
= merge_conflict_resolve_contents(
1003 &resolved
, diff_list
, conflict
, merge_opts
, file_opts
)) < 0)
1012 /* Rename detection and coalescing */
1014 struct merge_diff_similarity
{
1015 unsigned char similarity
;
1019 static int index_entry_similarity_calc(
1021 git_repository
*repo
,
1022 git_index_entry
*entry
,
1023 const git_merge_options
*opts
)
1026 git_diff_file diff_file
= {{{0}}};
1027 git_object_size_t blobsize
;
1032 if ((error
= git_blob_lookup(&blob
, repo
, &entry
->id
)) < 0)
1035 git_oid_cpy(&diff_file
.id
, &entry
->id
);
1036 diff_file
.path
= entry
->path
;
1037 diff_file
.size
= entry
->file_size
;
1038 diff_file
.mode
= entry
->mode
;
1039 diff_file
.flags
= 0;
1041 blobsize
= git_blob_rawsize(blob
);
1043 /* file too big for rename processing */
1044 if (!git__is_sizet(blobsize
))
1047 error
= opts
->metric
->buffer_signature(out
, &diff_file
,
1048 git_blob_rawcontent(blob
), (size_t)blobsize
,
1049 opts
->metric
->payload
);
1051 git_blob_free(blob
);
1056 static int index_entry_similarity_inexact(
1057 git_repository
*repo
,
1063 const git_merge_options
*opts
)
1068 if (!GIT_MODE_ISBLOB(a
->mode
) || !GIT_MODE_ISBLOB(b
->mode
))
1071 /* update signature cache if needed */
1072 if (!cache
[a_idx
] && (error
= index_entry_similarity_calc(&cache
[a_idx
], repo
, a
, opts
)) < 0)
1074 if (!cache
[b_idx
] && (error
= index_entry_similarity_calc(&cache
[b_idx
], repo
, b
, opts
)) < 0)
1077 /* some metrics may not wish to process this file (too big / too small) */
1078 if (!cache
[a_idx
] || !cache
[b_idx
])
1081 /* compare signatures */
1082 if (opts
->metric
->similarity(
1083 &score
, cache
[a_idx
], cache
[b_idx
], opts
->metric
->payload
) < 0)
1089 else if (score
> 100)
1095 /* Tracks deletes by oid for merge_diff_mark_similarity_exact(). This is a
1096 * non-shrinking queue where next_pos is the next position to dequeue.
1099 git_array_t(size_t) arr
;
1102 } deletes_by_oid_queue
;
1104 static void deletes_by_oid_free(git_oidmap
*map
) {
1105 deletes_by_oid_queue
*queue
;
1110 git_oidmap_foreach_value(map
, queue
, {
1111 git_array_clear(queue
->arr
);
1113 git_oidmap_free(map
);
1116 static int deletes_by_oid_enqueue(git_oidmap
*map
, git_pool
* pool
, const git_oid
*id
, size_t idx
)
1118 deletes_by_oid_queue
*queue
;
1119 size_t *array_entry
;
1121 if ((queue
= git_oidmap_get(map
, id
)) == NULL
) {
1122 queue
= git_pool_malloc(pool
, sizeof(deletes_by_oid_queue
));
1123 GIT_ERROR_CHECK_ALLOC(queue
);
1125 git_array_init(queue
->arr
);
1126 queue
->next_pos
= 0;
1127 queue
->first_entry
= idx
;
1129 if (git_oidmap_set(map
, id
, queue
) < 0)
1132 array_entry
= git_array_alloc(queue
->arr
);
1133 GIT_ERROR_CHECK_ALLOC(array_entry
);
1140 static int deletes_by_oid_dequeue(size_t *idx
, git_oidmap
*map
, const git_oid
*id
)
1142 deletes_by_oid_queue
*queue
;
1143 size_t *array_entry
;
1145 if ((queue
= git_oidmap_get(map
, id
)) == NULL
)
1146 return GIT_ENOTFOUND
;
1148 if (queue
->next_pos
== 0) {
1149 *idx
= queue
->first_entry
;
1151 array_entry
= git_array_get(queue
->arr
, queue
->next_pos
- 1);
1152 if (array_entry
== NULL
)
1153 return GIT_ENOTFOUND
;
1155 *idx
= *array_entry
;
1162 static int merge_diff_mark_similarity_exact(
1163 git_merge_diff_list
*diff_list
,
1164 struct merge_diff_similarity
*similarity_ours
,
1165 struct merge_diff_similarity
*similarity_theirs
)
1168 git_merge_diff
*conflict_src
, *conflict_tgt
;
1169 git_oidmap
*ours_deletes_by_oid
= NULL
, *theirs_deletes_by_oid
= NULL
;
1172 if (git_oidmap_new(&ours_deletes_by_oid
) < 0 ||
1173 git_oidmap_new(&theirs_deletes_by_oid
) < 0) {
1178 /* Build a map of object ids to conflicts */
1179 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1180 /* Items can be the source of a rename iff they have an item in the
1181 * ancestor slot and lack an item in the ours or theirs slot. */
1182 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
))
1185 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1186 error
= deletes_by_oid_enqueue(ours_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1191 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1192 error
= deletes_by_oid_enqueue(theirs_deletes_by_oid
, &diff_list
->pool
, &conflict_src
->ancestor_entry
.id
, i
);
1198 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1199 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1202 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
)) {
1203 if (deletes_by_oid_dequeue(&i
, ours_deletes_by_oid
, &conflict_tgt
->our_entry
.id
) == 0) {
1204 similarity_ours
[i
].similarity
= 100;
1205 similarity_ours
[i
].other_idx
= j
;
1207 similarity_ours
[j
].similarity
= 100;
1208 similarity_ours
[j
].other_idx
= i
;
1212 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
)) {
1213 if (deletes_by_oid_dequeue(&i
, theirs_deletes_by_oid
, &conflict_tgt
->their_entry
.id
) == 0) {
1214 similarity_theirs
[i
].similarity
= 100;
1215 similarity_theirs
[i
].other_idx
= j
;
1217 similarity_theirs
[j
].similarity
= 100;
1218 similarity_theirs
[j
].other_idx
= i
;
1224 deletes_by_oid_free(ours_deletes_by_oid
);
1225 deletes_by_oid_free(theirs_deletes_by_oid
);
1230 static int merge_diff_mark_similarity_inexact(
1231 git_repository
*repo
,
1232 git_merge_diff_list
*diff_list
,
1233 struct merge_diff_similarity
*similarity_ours
,
1234 struct merge_diff_similarity
*similarity_theirs
,
1236 const git_merge_options
*opts
)
1239 git_merge_diff
*conflict_src
, *conflict_tgt
;
1242 git_vector_foreach(&diff_list
->conflicts
, i
, conflict_src
) {
1243 /* Items can be the source of a rename iff they have an item in the
1244 * ancestor slot and lack an item in the ours or theirs slot. */
1245 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->ancestor_entry
) ||
1246 (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
) &&
1247 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)))
1250 git_vector_foreach(&diff_list
->conflicts
, j
, conflict_tgt
) {
1251 size_t our_idx
= diff_list
->conflicts
.length
+ j
;
1252 size_t their_idx
= (diff_list
->conflicts
.length
* 2) + j
;
1254 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->ancestor_entry
))
1257 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->our_entry
) &&
1258 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->our_entry
)) {
1259 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->our_entry
, our_idx
, cache
, opts
);
1261 if (similarity
== GIT_EBUFS
)
1263 else if (similarity
< 0)
1266 if (similarity
> similarity_ours
[i
].similarity
&&
1267 similarity
> similarity_ours
[j
].similarity
) {
1268 /* Clear previous best similarity */
1269 if (similarity_ours
[i
].similarity
> 0)
1270 similarity_ours
[similarity_ours
[i
].other_idx
].similarity
= 0;
1272 if (similarity_ours
[j
].similarity
> 0)
1273 similarity_ours
[similarity_ours
[j
].other_idx
].similarity
= 0;
1275 similarity_ours
[i
].similarity
= similarity
;
1276 similarity_ours
[i
].other_idx
= j
;
1278 similarity_ours
[j
].similarity
= similarity
;
1279 similarity_ours
[j
].other_idx
= i
;
1283 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt
->their_entry
) &&
1284 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src
->their_entry
)) {
1285 similarity
= index_entry_similarity_inexact(repo
, &conflict_src
->ancestor_entry
, i
, &conflict_tgt
->their_entry
, their_idx
, cache
, opts
);
1287 if (similarity
> similarity_theirs
[i
].similarity
&&
1288 similarity
> similarity_theirs
[j
].similarity
) {
1289 /* Clear previous best similarity */
1290 if (similarity_theirs
[i
].similarity
> 0)
1291 similarity_theirs
[similarity_theirs
[i
].other_idx
].similarity
= 0;
1293 if (similarity_theirs
[j
].similarity
> 0)
1294 similarity_theirs
[similarity_theirs
[j
].other_idx
].similarity
= 0;
1296 similarity_theirs
[i
].similarity
= similarity
;
1297 similarity_theirs
[i
].other_idx
= j
;
1299 similarity_theirs
[j
].similarity
= similarity
;
1300 similarity_theirs
[j
].other_idx
= i
;
1312 * Ancestor Ours Theirs
1314 * 0a A A A No rename
1315 * b A A* A No rename (ours was rewritten)
1316 * c A A A* No rename (theirs rewritten)
1317 * 1a A A B[A] Rename or rename/edit
1318 * b A B[A] A (automergeable)
1319 * 2 A B[A] B[A] Both renamed (automergeable)
1320 * 3a A B[A] Rename/delete
1322 * 4a A B[A] B Rename/add [B~ours B~theirs]
1324 * 5 A B[A] C[A] Both renamed ("1 -> 2")
1325 * 6 A C[A] Both renamed ("2 -> 1")
1326 * B C[B] [C~ours C~theirs] (automergeable)
1328 static void merge_diff_mark_rename_conflict(
1329 git_merge_diff_list
*diff_list
,
1330 struct merge_diff_similarity
*similarity_ours
,
1332 size_t ours_source_idx
,
1333 struct merge_diff_similarity
*similarity_theirs
,
1334 bool theirs_renamed
,
1335 size_t theirs_source_idx
,
1336 git_merge_diff
*target
,
1337 const git_merge_options
*opts
)
1339 git_merge_diff
*ours_source
= NULL
, *theirs_source
= NULL
;
1342 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1345 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1347 /* Detect 2->1 conflicts */
1348 if (ours_renamed
&& theirs_renamed
) {
1349 /* Both renamed to the same target name. */
1350 if (ours_source_idx
== theirs_source_idx
)
1351 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED
;
1353 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1354 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1
;
1356 } else if (ours_renamed
) {
1357 /* If our source was also renamed in theirs, this is a 1->2 */
1358 if (similarity_theirs
[ours_source_idx
].similarity
>= opts
->rename_threshold
)
1359 ours_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1361 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
)) {
1362 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1363 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1366 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source
->their_entry
))
1367 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1369 else if (ours_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1370 ours_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1371 } else if (theirs_renamed
) {
1372 /* If their source was also renamed in ours, this is a 1->2 */
1373 if (similarity_ours
[theirs_source_idx
].similarity
>= opts
->rename_threshold
)
1374 theirs_source
->type
= GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2
;
1376 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
)) {
1377 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1378 target
->type
= GIT_MERGE_DIFF_RENAMED_ADDED
;
1381 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source
->our_entry
))
1382 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_DELETED
;
1384 else if (theirs_source
->type
== GIT_MERGE_DIFF_MODIFIED_DELETED
)
1385 theirs_source
->type
= GIT_MERGE_DIFF_RENAMED_MODIFIED
;
1389 GIT_INLINE(void) merge_diff_coalesce_rename(
1390 git_index_entry
*source_entry
,
1391 git_delta_t
*source_status
,
1392 git_index_entry
*target_entry
,
1393 git_delta_t
*target_status
)
1395 /* Coalesce the rename target into the rename source. */
1396 memcpy(source_entry
, target_entry
, sizeof(git_index_entry
));
1397 *source_status
= GIT_DELTA_RENAMED
;
1399 memset(target_entry
, 0x0, sizeof(git_index_entry
));
1400 *target_status
= GIT_DELTA_UNMODIFIED
;
1403 static void merge_diff_list_coalesce_renames(
1404 git_merge_diff_list
*diff_list
,
1405 struct merge_diff_similarity
*similarity_ours
,
1406 struct merge_diff_similarity
*similarity_theirs
,
1407 const git_merge_options
*opts
)
1410 bool ours_renamed
= 0, theirs_renamed
= 0;
1411 size_t ours_source_idx
= 0, theirs_source_idx
= 0;
1412 git_merge_diff
*ours_source
, *theirs_source
, *target
;
1414 for (i
= 0; i
< diff_list
->conflicts
.length
; i
++) {
1415 target
= diff_list
->conflicts
.contents
[i
];
1420 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->our_entry
) &&
1421 similarity_ours
[i
].similarity
>= opts
->rename_threshold
) {
1422 ours_source_idx
= similarity_ours
[i
].other_idx
;
1424 ours_source
= diff_list
->conflicts
.contents
[ours_source_idx
];
1426 merge_diff_coalesce_rename(
1427 &ours_source
->our_entry
,
1428 &ours_source
->our_status
,
1430 &target
->our_status
);
1432 similarity_ours
[ours_source_idx
].similarity
= 0;
1433 similarity_ours
[i
].similarity
= 0;
1438 /* insufficient to determine direction */
1439 if (GIT_MERGE_INDEX_ENTRY_EXISTS(target
->their_entry
) &&
1440 similarity_theirs
[i
].similarity
>= opts
->rename_threshold
) {
1441 theirs_source_idx
= similarity_theirs
[i
].other_idx
;
1443 theirs_source
= diff_list
->conflicts
.contents
[theirs_source_idx
];
1445 merge_diff_coalesce_rename(
1446 &theirs_source
->their_entry
,
1447 &theirs_source
->their_status
,
1448 &target
->their_entry
,
1449 &target
->their_status
);
1451 similarity_theirs
[theirs_source_idx
].similarity
= 0;
1452 similarity_theirs
[i
].similarity
= 0;
1457 merge_diff_mark_rename_conflict(diff_list
,
1458 similarity_ours
, ours_renamed
, ours_source_idx
,
1459 similarity_theirs
, theirs_renamed
, theirs_source_idx
,
1464 static int merge_diff_empty(const git_vector
*conflicts
, size_t idx
, void *p
)
1466 git_merge_diff
*conflict
= conflicts
->contents
[idx
];
1470 return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) &&
1471 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) &&
1472 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
));
1475 static void merge_diff_list_count_candidates(
1476 git_merge_diff_list
*diff_list
,
1480 git_merge_diff
*entry
;
1486 git_vector_foreach(&diff_list
->conflicts
, i
, entry
) {
1487 if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
) &&
1488 (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->our_entry
) ||
1489 !GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->their_entry
)))
1491 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry
->ancestor_entry
))
1496 int git_merge_diff_list__find_renames(
1497 git_repository
*repo
,
1498 git_merge_diff_list
*diff_list
,
1499 const git_merge_options
*opts
)
1501 struct merge_diff_similarity
*similarity_ours
, *similarity_theirs
;
1502 void **cache
= NULL
;
1503 size_t cache_size
= 0;
1504 size_t src_count
, tgt_count
, i
;
1507 assert(diff_list
&& opts
);
1509 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) == 0)
1512 similarity_ours
= git__calloc(diff_list
->conflicts
.length
,
1513 sizeof(struct merge_diff_similarity
));
1514 GIT_ERROR_CHECK_ALLOC(similarity_ours
);
1516 similarity_theirs
= git__calloc(diff_list
->conflicts
.length
,
1517 sizeof(struct merge_diff_similarity
));
1518 GIT_ERROR_CHECK_ALLOC(similarity_theirs
);
1520 /* Calculate similarity between items that were deleted from the ancestor
1521 * and added in the other branch.
1523 if ((error
= merge_diff_mark_similarity_exact(diff_list
, similarity_ours
, similarity_theirs
)) < 0)
1526 if (opts
->rename_threshold
< 100 && diff_list
->conflicts
.length
<= opts
->target_limit
) {
1527 GIT_ERROR_CHECK_ALLOC_MULTIPLY(&cache_size
, diff_list
->conflicts
.length
, 3);
1528 cache
= git__calloc(cache_size
, sizeof(void *));
1529 GIT_ERROR_CHECK_ALLOC(cache
);
1531 merge_diff_list_count_candidates(diff_list
, &src_count
, &tgt_count
);
1533 if (src_count
> opts
->target_limit
|| tgt_count
> opts
->target_limit
) {
1536 if ((error
= merge_diff_mark_similarity_inexact(
1537 repo
, diff_list
, similarity_ours
, similarity_theirs
, cache
, opts
)) < 0)
1542 /* For entries that are appropriately similar, merge the new name's entry
1543 * into the old name.
1545 merge_diff_list_coalesce_renames(diff_list
, similarity_ours
, similarity_theirs
, opts
);
1547 /* And remove any entries that were merged and are now empty. */
1548 git_vector_remove_matching(&diff_list
->conflicts
, merge_diff_empty
, NULL
);
1551 if (cache
!= NULL
) {
1552 for (i
= 0; i
< cache_size
; ++i
) {
1553 if (cache
[i
] != NULL
)
1554 opts
->metric
->free_signature(cache
[i
], opts
->metric
->payload
);
1560 git__free(similarity_ours
);
1561 git__free(similarity_theirs
);
1566 /* Directory/file conflict handling */
1568 GIT_INLINE(const char *) merge_diff_path(
1569 const git_merge_diff
*conflict
)
1571 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
1572 return conflict
->ancestor_entry
.path
;
1573 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
))
1574 return conflict
->our_entry
.path
;
1575 else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
))
1576 return conflict
->their_entry
.path
;
1581 GIT_INLINE(bool) merge_diff_any_side_added_or_modified(
1582 const git_merge_diff
*conflict
)
1584 if (conflict
->our_status
== GIT_DELTA_ADDED
||
1585 conflict
->our_status
== GIT_DELTA_MODIFIED
||
1586 conflict
->their_status
== GIT_DELTA_ADDED
||
1587 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1593 GIT_INLINE(bool) path_is_prefixed(const char *parent
, const char *child
)
1595 size_t child_len
= strlen(child
);
1596 size_t parent_len
= strlen(parent
);
1598 if (child_len
< parent_len
||
1599 strncmp(parent
, child
, parent_len
) != 0)
1602 return (child
[parent_len
] == '/');
1605 GIT_INLINE(int) merge_diff_detect_df_conflict(
1606 struct merge_diff_df_data
*df_data
,
1607 git_merge_diff
*conflict
)
1609 const char *cur_path
= merge_diff_path(conflict
);
1611 /* Determine if this is a D/F conflict or the child of one */
1612 if (df_data
->df_path
&&
1613 path_is_prefixed(df_data
->df_path
, cur_path
))
1614 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1615 else if(df_data
->df_path
)
1616 df_data
->df_path
= NULL
;
1617 else if (df_data
->prev_path
&&
1618 merge_diff_any_side_added_or_modified(df_data
->prev_conflict
) &&
1619 merge_diff_any_side_added_or_modified(conflict
) &&
1620 path_is_prefixed(df_data
->prev_path
, cur_path
)) {
1621 conflict
->type
= GIT_MERGE_DIFF_DF_CHILD
;
1623 df_data
->prev_conflict
->type
= GIT_MERGE_DIFF_DIRECTORY_FILE
;
1624 df_data
->df_path
= df_data
->prev_path
;
1627 df_data
->prev_path
= cur_path
;
1628 df_data
->prev_conflict
= conflict
;
1633 /* Conflict handling */
1635 GIT_INLINE(int) merge_diff_detect_type(
1636 git_merge_diff
*conflict
)
1638 if (conflict
->our_status
== GIT_DELTA_ADDED
&&
1639 conflict
->their_status
== GIT_DELTA_ADDED
)
1640 conflict
->type
= GIT_MERGE_DIFF_BOTH_ADDED
;
1641 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1642 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1643 conflict
->type
= GIT_MERGE_DIFF_BOTH_MODIFIED
;
1644 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1645 conflict
->their_status
== GIT_DELTA_DELETED
)
1646 conflict
->type
= GIT_MERGE_DIFF_BOTH_DELETED
;
1647 else if (conflict
->our_status
== GIT_DELTA_MODIFIED
&&
1648 conflict
->their_status
== GIT_DELTA_DELETED
)
1649 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1650 else if (conflict
->our_status
== GIT_DELTA_DELETED
&&
1651 conflict
->their_status
== GIT_DELTA_MODIFIED
)
1652 conflict
->type
= GIT_MERGE_DIFF_MODIFIED_DELETED
;
1654 conflict
->type
= GIT_MERGE_DIFF_NONE
;
1659 GIT_INLINE(int) index_entry_dup_pool(
1660 git_index_entry
*out
,
1662 const git_index_entry
*src
)
1665 memcpy(out
, src
, sizeof(git_index_entry
));
1666 if ((out
->path
= git_pool_strdup(pool
, src
->path
)) == NULL
)
1673 GIT_INLINE(int) merge_delta_type_from_index_entries(
1674 const git_index_entry
*ancestor
,
1675 const git_index_entry
*other
)
1677 if (ancestor
== NULL
&& other
== NULL
)
1678 return GIT_DELTA_UNMODIFIED
;
1679 else if (ancestor
== NULL
&& other
!= NULL
)
1680 return GIT_DELTA_ADDED
;
1681 else if (ancestor
!= NULL
&& other
== NULL
)
1682 return GIT_DELTA_DELETED
;
1683 else if (S_ISDIR(ancestor
->mode
) ^ S_ISDIR(other
->mode
))
1684 return GIT_DELTA_TYPECHANGE
;
1685 else if(S_ISLNK(ancestor
->mode
) ^ S_ISLNK(other
->mode
))
1686 return GIT_DELTA_TYPECHANGE
;
1687 else if (git_oid__cmp(&ancestor
->id
, &other
->id
) ||
1688 ancestor
->mode
!= other
->mode
)
1689 return GIT_DELTA_MODIFIED
;
1691 return GIT_DELTA_UNMODIFIED
;
1694 static git_merge_diff
*merge_diff_from_index_entries(
1695 git_merge_diff_list
*diff_list
,
1696 const git_index_entry
**entries
)
1698 git_merge_diff
*conflict
;
1699 git_pool
*pool
= &diff_list
->pool
;
1701 if ((conflict
= git_pool_mallocz(pool
, sizeof(git_merge_diff
))) == NULL
)
1704 if (index_entry_dup_pool(&conflict
->ancestor_entry
, pool
, entries
[TREE_IDX_ANCESTOR
]) < 0 ||
1705 index_entry_dup_pool(&conflict
->our_entry
, pool
, entries
[TREE_IDX_OURS
]) < 0 ||
1706 index_entry_dup_pool(&conflict
->their_entry
, pool
, entries
[TREE_IDX_THEIRS
]) < 0)
1709 conflict
->our_status
= merge_delta_type_from_index_entries(
1710 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_OURS
]);
1711 conflict
->their_status
= merge_delta_type_from_index_entries(
1712 entries
[TREE_IDX_ANCESTOR
], entries
[TREE_IDX_THEIRS
]);
1719 static int merge_diff_list_insert_conflict(
1720 git_merge_diff_list
*diff_list
,
1721 struct merge_diff_df_data
*merge_df_data
,
1722 const git_index_entry
*tree_items
[3])
1724 git_merge_diff
*conflict
;
1726 if ((conflict
= merge_diff_from_index_entries(diff_list
, tree_items
)) == NULL
||
1727 merge_diff_detect_type(conflict
) < 0 ||
1728 merge_diff_detect_df_conflict(merge_df_data
, conflict
) < 0 ||
1729 git_vector_insert(&diff_list
->conflicts
, conflict
) < 0)
1735 static int merge_diff_list_insert_unmodified(
1736 git_merge_diff_list
*diff_list
,
1737 const git_index_entry
*tree_items
[3])
1740 git_index_entry
*entry
;
1742 entry
= git_pool_malloc(&diff_list
->pool
, sizeof(git_index_entry
));
1743 GIT_ERROR_CHECK_ALLOC(entry
);
1745 if ((error
= index_entry_dup_pool(entry
, &diff_list
->pool
, tree_items
[0])) >= 0)
1746 error
= git_vector_insert(&diff_list
->staged
, entry
);
1751 struct merge_diff_find_data
{
1752 git_merge_diff_list
*diff_list
;
1753 struct merge_diff_df_data df_data
;
1756 static int queue_difference(const git_index_entry
**entries
, void *data
)
1758 struct merge_diff_find_data
*find_data
= data
;
1759 bool item_modified
= false;
1762 if (!entries
[0] || !entries
[1] || !entries
[2]) {
1763 item_modified
= true;
1765 for (i
= 1; i
< 3; i
++) {
1766 if (index_entry_cmp(entries
[0], entries
[i
]) != 0) {
1767 item_modified
= true;
1773 return item_modified
?
1774 merge_diff_list_insert_conflict(
1775 find_data
->diff_list
, &find_data
->df_data
, entries
) :
1776 merge_diff_list_insert_unmodified(find_data
->diff_list
, entries
);
1779 int git_merge_diff_list__find_differences(
1780 git_merge_diff_list
*diff_list
,
1781 git_iterator
*ancestor_iter
,
1782 git_iterator
*our_iter
,
1783 git_iterator
*their_iter
)
1785 git_iterator
*iterators
[3] = { ancestor_iter
, our_iter
, their_iter
};
1786 struct merge_diff_find_data find_data
= { diff_list
};
1788 return git_iterator_walk(iterators
, 3, queue_difference
, &find_data
);
1791 git_merge_diff_list
*git_merge_diff_list__alloc(git_repository
*repo
)
1793 git_merge_diff_list
*diff_list
= git__calloc(1, sizeof(git_merge_diff_list
));
1795 if (diff_list
== NULL
)
1798 diff_list
->repo
= repo
;
1800 git_pool_init(&diff_list
->pool
, 1);
1802 if (git_vector_init(&diff_list
->staged
, 0, NULL
) < 0 ||
1803 git_vector_init(&diff_list
->conflicts
, 0, NULL
) < 0 ||
1804 git_vector_init(&diff_list
->resolved
, 0, NULL
) < 0) {
1805 git_merge_diff_list__free(diff_list
);
1812 void git_merge_diff_list__free(git_merge_diff_list
*diff_list
)
1817 git_vector_free(&diff_list
->staged
);
1818 git_vector_free(&diff_list
->conflicts
);
1819 git_vector_free(&diff_list
->resolved
);
1820 git_pool_clear(&diff_list
->pool
);
1821 git__free(diff_list
);
1824 static int merge_normalize_opts(
1825 git_repository
*repo
,
1826 git_merge_options
*opts
,
1827 const git_merge_options
*given
)
1829 git_config
*cfg
= NULL
;
1830 git_config_entry
*entry
= NULL
;
1833 assert(repo
&& opts
);
1835 if ((error
= git_repository_config__weakptr(&cfg
, repo
)) < 0)
1838 if (given
!= NULL
) {
1839 memcpy(opts
, given
, sizeof(git_merge_options
));
1841 git_merge_options init
= GIT_MERGE_OPTIONS_INIT
;
1842 memcpy(opts
, &init
, sizeof(init
));
1845 if ((opts
->flags
& GIT_MERGE_FIND_RENAMES
) && !opts
->rename_threshold
)
1846 opts
->rename_threshold
= GIT_MERGE_DEFAULT_RENAME_THRESHOLD
;
1848 if (given
&& given
->default_driver
) {
1849 opts
->default_driver
= git__strdup(given
->default_driver
);
1850 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1852 error
= git_config_get_entry(&entry
, cfg
, "merge.default");
1855 opts
->default_driver
= git__strdup(entry
->value
);
1856 GIT_ERROR_CHECK_ALLOC(opts
->default_driver
);
1857 } else if (error
== GIT_ENOTFOUND
) {
1864 if (!opts
->target_limit
) {
1865 int limit
= git_config__get_int_force(cfg
, "merge.renamelimit", 0);
1868 limit
= git_config__get_int_force(cfg
, "diff.renamelimit", 0);
1870 opts
->target_limit
= (limit
<= 0) ?
1871 GIT_MERGE_DEFAULT_TARGET_LIMIT
: (unsigned int)limit
;
1874 /* assign the internal metric with whitespace flag as payload */
1875 if (!opts
->metric
) {
1876 opts
->metric
= git__malloc(sizeof(git_diff_similarity_metric
));
1877 GIT_ERROR_CHECK_ALLOC(opts
->metric
);
1879 opts
->metric
->file_signature
= git_diff_find_similar__hashsig_for_file
;
1880 opts
->metric
->buffer_signature
= git_diff_find_similar__hashsig_for_buf
;
1881 opts
->metric
->free_signature
= git_diff_find_similar__hashsig_free
;
1882 opts
->metric
->similarity
= git_diff_find_similar__calc_similarity
;
1883 opts
->metric
->payload
= (void *)GIT_HASHSIG_SMART_WHITESPACE
;
1887 git_config_entry_free(entry
);
1892 static int merge_index_insert_reuc(
1895 const git_index_entry
*entry
)
1897 const git_index_reuc_entry
*reuc
;
1898 int mode
[3] = { 0, 0, 0 };
1899 git_oid
const *oid
[3] = { NULL
, NULL
, NULL
};
1902 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry
))
1905 if ((reuc
= git_index_reuc_get_bypath(index
, entry
->path
)) != NULL
) {
1906 for (i
= 0; i
< 3; i
++) {
1907 mode
[i
] = reuc
->mode
[i
];
1908 oid
[i
] = &reuc
->oid
[i
];
1912 mode
[idx
] = entry
->mode
;
1913 oid
[idx
] = &entry
->id
;
1915 return git_index_reuc_add(index
, entry
->path
,
1916 mode
[0], oid
[0], mode
[1], oid
[1], mode
[2], oid
[2]);
1919 static int index_update_reuc(git_index
*index
, git_merge_diff_list
*diff_list
)
1923 git_merge_diff
*conflict
;
1925 /* Add each entry in the resolved conflict to the REUC independently, since
1926 * the paths may differ due to renames. */
1927 git_vector_foreach(&diff_list
->resolved
, i
, conflict
) {
1928 const git_index_entry
*ancestor
=
1929 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
1930 &conflict
->ancestor_entry
: NULL
;
1932 const git_index_entry
*ours
=
1933 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
1934 &conflict
->our_entry
: NULL
;
1936 const git_index_entry
*theirs
=
1937 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
1938 &conflict
->their_entry
: NULL
;
1940 if (ancestor
!= NULL
&&
1941 (error
= merge_index_insert_reuc(index
, TREE_IDX_ANCESTOR
, ancestor
)) < 0)
1945 (error
= merge_index_insert_reuc(index
, TREE_IDX_OURS
, ours
)) < 0)
1948 if (theirs
!= NULL
&&
1949 (error
= merge_index_insert_reuc(index
, TREE_IDX_THEIRS
, theirs
)) < 0)
1956 static int index_from_diff_list(git_index
**out
,
1957 git_merge_diff_list
*diff_list
, bool skip_reuc
)
1961 git_merge_diff
*conflict
;
1966 if ((error
= git_index_new(&index
)) < 0)
1969 if ((error
= git_index__fill(index
, &diff_list
->staged
)) < 0)
1972 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
1973 const git_index_entry
*ancestor
=
1974 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
) ?
1975 &conflict
->ancestor_entry
: NULL
;
1977 const git_index_entry
*ours
=
1978 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
1979 &conflict
->our_entry
: NULL
;
1981 const git_index_entry
*theirs
=
1982 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
1983 &conflict
->their_entry
: NULL
;
1985 if ((error
= git_index_conflict_add(index
, ancestor
, ours
, theirs
)) < 0)
1989 /* Add each rename entry to the rename portion of the index. */
1990 git_vector_foreach(&diff_list
->conflicts
, i
, conflict
) {
1991 const char *ancestor_path
, *our_path
, *their_path
;
1993 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->ancestor_entry
))
1996 ancestor_path
= conflict
->ancestor_entry
.path
;
1999 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->our_entry
) ?
2000 conflict
->our_entry
.path
: NULL
;
2003 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict
->their_entry
) ?
2004 conflict
->their_entry
.path
: NULL
;
2006 if ((our_path
&& strcmp(ancestor_path
, our_path
) != 0) ||
2007 (their_path
&& strcmp(ancestor_path
, their_path
) != 0)) {
2008 if ((error
= git_index_name_add(index
, ancestor_path
, our_path
, their_path
)) < 0)
2014 if ((error
= index_update_reuc(index
, diff_list
)) < 0)
2022 git_index_free(index
);
2026 static git_iterator
*iterator_given_or_empty(git_iterator
**empty
, git_iterator
*given
)
2028 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2033 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2035 if (git_iterator_for_nothing(empty
, &opts
) < 0)
2041 int git_merge__iterators(
2043 git_repository
*repo
,
2044 git_iterator
*ancestor_iter
,
2045 git_iterator
*our_iter
,
2046 git_iterator
*theirs_iter
,
2047 const git_merge_options
*given_opts
)
2049 git_iterator
*empty_ancestor
= NULL
,
2051 *empty_theirs
= NULL
;
2052 git_merge_diff_list
*diff_list
;
2053 git_merge_options opts
;
2054 git_merge_file_options file_opts
= GIT_MERGE_FILE_OPTIONS_INIT
;
2055 git_merge_diff
*conflict
;
2060 assert(out
&& repo
);
2064 GIT_ERROR_CHECK_VERSION(
2065 given_opts
, GIT_MERGE_OPTIONS_VERSION
, "git_merge_options");
2067 if ((error
= merge_normalize_opts(repo
, &opts
, given_opts
)) < 0)
2070 file_opts
.favor
= opts
.file_favor
;
2071 file_opts
.flags
= opts
.file_flags
;
2073 /* use the git-inspired labels when virtual base building */
2074 if (opts
.flags
& GIT_MERGE__VIRTUAL_BASE
) {
2075 file_opts
.ancestor_label
= "merged common ancestors";
2076 file_opts
.our_label
= "Temporary merge branch 1";
2077 file_opts
.their_label
= "Temporary merge branch 2";
2078 file_opts
.flags
|= GIT_MERGE_FILE_FAVOR__CONFLICTED
;
2079 file_opts
.marker_size
= GIT_MERGE_CONFLICT_MARKER_SIZE
+ 2;
2082 diff_list
= git_merge_diff_list__alloc(repo
);
2083 GIT_ERROR_CHECK_ALLOC(diff_list
);
2085 ancestor_iter
= iterator_given_or_empty(&empty_ancestor
, ancestor_iter
);
2086 our_iter
= iterator_given_or_empty(&empty_ours
, our_iter
);
2087 theirs_iter
= iterator_given_or_empty(&empty_theirs
, theirs_iter
);
2089 if ((error
= git_merge_diff_list__find_differences(
2090 diff_list
, ancestor_iter
, our_iter
, theirs_iter
)) < 0 ||
2091 (error
= git_merge_diff_list__find_renames(repo
, diff_list
, &opts
)) < 0)
2094 memcpy(&changes
, &diff_list
->conflicts
, sizeof(git_vector
));
2095 git_vector_clear(&diff_list
->conflicts
);
2097 git_vector_foreach(&changes
, i
, conflict
) {
2100 if ((error
= merge_conflict_resolve(
2101 &resolved
, diff_list
, conflict
, &opts
, &file_opts
)) < 0)
2105 if ((opts
.flags
& GIT_MERGE_FAIL_ON_CONFLICT
)) {
2106 git_error_set(GIT_ERROR_MERGE
, "merge conflicts exist");
2107 error
= GIT_EMERGECONFLICT
;
2111 git_vector_insert(&diff_list
->conflicts
, conflict
);
2115 error
= index_from_diff_list(out
, diff_list
,
2116 (opts
.flags
& GIT_MERGE_SKIP_REUC
));
2119 if (!given_opts
|| !given_opts
->metric
)
2120 git__free(opts
.metric
);
2122 git__free((char *)opts
.default_driver
);
2124 git_merge_diff_list__free(diff_list
);
2125 git_iterator_free(empty_ancestor
);
2126 git_iterator_free(empty_ours
);
2127 git_iterator_free(empty_theirs
);
2132 int git_merge_trees(
2134 git_repository
*repo
,
2135 const git_tree
*ancestor_tree
,
2136 const git_tree
*our_tree
,
2137 const git_tree
*their_tree
,
2138 const git_merge_options
*merge_opts
)
2140 git_iterator
*ancestor_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2141 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2144 assert(out
&& repo
);
2146 /* if one side is treesame to the ancestor, take the other side */
2147 if (ancestor_tree
&& merge_opts
&& (merge_opts
->flags
& GIT_MERGE_SKIP_REUC
)) {
2148 const git_tree
*result
= NULL
;
2149 const git_oid
*ancestor_tree_id
= git_tree_id(ancestor_tree
);
2151 if (our_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(our_tree
)))
2152 result
= their_tree
;
2153 else if (their_tree
&& !git_oid_cmp(ancestor_tree_id
, git_tree_id(their_tree
)))
2157 if ((error
= git_index_new(out
)) == 0)
2158 error
= git_index_read_tree(*out
, result
);
2164 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2166 if ((error
= git_iterator_for_tree(
2167 &ancestor_iter
, (git_tree
*)ancestor_tree
, &iter_opts
)) < 0 ||
2168 (error
= git_iterator_for_tree(
2169 &our_iter
, (git_tree
*)our_tree
, &iter_opts
)) < 0 ||
2170 (error
= git_iterator_for_tree(
2171 &their_iter
, (git_tree
*)their_tree
, &iter_opts
)) < 0)
2174 error
= git_merge__iterators(
2175 out
, repo
, ancestor_iter
, our_iter
, their_iter
, merge_opts
);
2178 git_iterator_free(ancestor_iter
);
2179 git_iterator_free(our_iter
);
2180 git_iterator_free(their_iter
);
2185 static int merge_annotated_commits(
2186 git_index
**index_out
,
2187 git_annotated_commit
**base_out
,
2188 git_repository
*repo
,
2189 git_annotated_commit
*our_commit
,
2190 git_annotated_commit
*their_commit
,
2191 size_t recursion_level
,
2192 const git_merge_options
*opts
);
2194 GIT_INLINE(int) insert_head_ids(
2195 git_array_oid_t
*ids
,
2196 const git_annotated_commit
*annotated_commit
)
2201 if (annotated_commit
->type
== GIT_ANNOTATED_COMMIT_REAL
) {
2202 id
= git_array_alloc(*ids
);
2203 GIT_ERROR_CHECK_ALLOC(id
);
2205 git_oid_cpy(id
, git_commit_id(annotated_commit
->commit
));
2207 for (i
= 0; i
< annotated_commit
->parents
.size
; i
++) {
2208 id
= git_array_alloc(*ids
);
2209 GIT_ERROR_CHECK_ALLOC(id
);
2211 git_oid_cpy(id
, &annotated_commit
->parents
.ptr
[i
]);
2218 static int create_virtual_base(
2219 git_annotated_commit
**out
,
2220 git_repository
*repo
,
2221 git_annotated_commit
*one
,
2222 git_annotated_commit
*two
,
2223 const git_merge_options
*opts
,
2224 size_t recursion_level
)
2226 git_annotated_commit
*result
= NULL
;
2227 git_index
*index
= NULL
;
2228 git_merge_options virtual_opts
= GIT_MERGE_OPTIONS_INIT
;
2230 /* Conflicts in the merge base creation do not propagate to conflicts
2231 * in the result; the conflicted base will act as the common ancestor.
2234 memcpy(&virtual_opts
, opts
, sizeof(git_merge_options
));
2236 virtual_opts
.flags
&= ~GIT_MERGE_FAIL_ON_CONFLICT
;
2237 virtual_opts
.flags
|= GIT_MERGE__VIRTUAL_BASE
;
2239 if ((merge_annotated_commits(&index
, NULL
, repo
, one
, two
,
2240 recursion_level
+ 1, &virtual_opts
)) < 0)
2243 result
= git__calloc(1, sizeof(git_annotated_commit
));
2244 GIT_ERROR_CHECK_ALLOC(result
);
2245 result
->type
= GIT_ANNOTATED_COMMIT_VIRTUAL
;
2246 result
->index
= index
;
2248 insert_head_ids(&result
->parents
, one
);
2249 insert_head_ids(&result
->parents
, two
);
2255 static int compute_base(
2256 git_annotated_commit
**out
,
2257 git_repository
*repo
,
2258 const git_annotated_commit
*one
,
2259 const git_annotated_commit
*two
,
2260 const git_merge_options
*given_opts
,
2261 size_t recursion_level
)
2263 git_array_oid_t head_ids
= GIT_ARRAY_INIT
;
2264 git_oidarray bases
= {0};
2265 git_annotated_commit
*base
= NULL
, *other
= NULL
, *new_base
= NULL
;
2266 git_merge_options opts
= GIT_MERGE_OPTIONS_INIT
;
2267 size_t i
, base_count
;
2273 memcpy(&opts
, given_opts
, sizeof(git_merge_options
));
2275 /* With more than two commits, merge_bases_many finds the base of
2276 * the first commit and a hypothetical merge of the others. Since
2277 * "one" may itself be a virtual commit, which insert_head_ids
2278 * substitutes multiple ancestors for, it needs to be added
2279 * after "two" which is always a single real commit.
2281 if ((error
= insert_head_ids(&head_ids
, two
)) < 0 ||
2282 (error
= insert_head_ids(&head_ids
, one
)) < 0 ||
2283 (error
= git_merge_bases_many(&bases
, repo
,
2284 head_ids
.size
, head_ids
.ptr
)) < 0)
2287 base_count
= (opts
.flags
& GIT_MERGE_NO_RECURSIVE
) ? 0 : bases
.count
;
2290 git_oidarray__reverse(&bases
);
2292 if ((error
= git_annotated_commit_lookup(&base
, repo
, &bases
.ids
[0])) < 0)
2295 for (i
= 1; i
< base_count
; i
++) {
2298 if (opts
.recursion_limit
&& recursion_level
> opts
.recursion_limit
)
2301 if ((error
= git_annotated_commit_lookup(&other
, repo
,
2302 &bases
.ids
[i
])) < 0 ||
2303 (error
= create_virtual_base(&new_base
, repo
, base
, other
, &opts
,
2304 recursion_level
)) < 0)
2307 git_annotated_commit_free(base
);
2308 git_annotated_commit_free(other
);
2319 git_annotated_commit_free(base
);
2321 git_annotated_commit_free(other
);
2322 git_annotated_commit_free(new_base
);
2323 git_oidarray_free(&bases
);
2324 git_array_clear(head_ids
);
2328 static int iterator_for_annotated_commit(
2330 git_annotated_commit
*commit
)
2332 git_iterator_options opts
= GIT_ITERATOR_OPTIONS_INIT
;
2335 opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2337 if (commit
== NULL
) {
2338 error
= git_iterator_for_nothing(out
, &opts
);
2339 } else if (commit
->type
== GIT_ANNOTATED_COMMIT_VIRTUAL
) {
2340 error
= git_iterator_for_index(out
, git_index_owner(commit
->index
), commit
->index
, &opts
);
2342 if (!commit
->tree
&&
2343 (error
= git_commit_tree(&commit
->tree
, commit
->commit
)) < 0)
2346 error
= git_iterator_for_tree(out
, commit
->tree
, &opts
);
2353 static int merge_annotated_commits(
2354 git_index
**index_out
,
2355 git_annotated_commit
**base_out
,
2356 git_repository
*repo
,
2357 git_annotated_commit
*ours
,
2358 git_annotated_commit
*theirs
,
2359 size_t recursion_level
,
2360 const git_merge_options
*opts
)
2362 git_annotated_commit
*base
= NULL
;
2363 git_iterator
*base_iter
= NULL
, *our_iter
= NULL
, *their_iter
= NULL
;
2366 if ((error
= compute_base(&base
, repo
, ours
, theirs
, opts
,
2367 recursion_level
)) < 0) {
2369 if (error
!= GIT_ENOTFOUND
)
2375 if ((error
= iterator_for_annotated_commit(&base_iter
, base
)) < 0 ||
2376 (error
= iterator_for_annotated_commit(&our_iter
, ours
)) < 0 ||
2377 (error
= iterator_for_annotated_commit(&their_iter
, theirs
)) < 0 ||
2378 (error
= git_merge__iterators(index_out
, repo
, base_iter
, our_iter
,
2379 their_iter
, opts
)) < 0)
2388 git_annotated_commit_free(base
);
2389 git_iterator_free(base_iter
);
2390 git_iterator_free(our_iter
);
2391 git_iterator_free(their_iter
);
2396 int git_merge_commits(
2398 git_repository
*repo
,
2399 const git_commit
*our_commit
,
2400 const git_commit
*their_commit
,
2401 const git_merge_options
*opts
)
2403 git_annotated_commit
*ours
= NULL
, *theirs
= NULL
, *base
= NULL
;
2406 if ((error
= git_annotated_commit_from_commit(&ours
, (git_commit
*)our_commit
)) < 0 ||
2407 (error
= git_annotated_commit_from_commit(&theirs
, (git_commit
*)their_commit
)) < 0)
2410 error
= merge_annotated_commits(out
, &base
, repo
, ours
, theirs
, 0, opts
);
2413 git_annotated_commit_free(ours
);
2414 git_annotated_commit_free(theirs
);
2415 git_annotated_commit_free(base
);
2419 /* Merge setup / cleanup */
2421 static int write_merge_head(
2422 git_repository
*repo
,
2423 const git_annotated_commit
*heads
[],
2426 git_filebuf file
= GIT_FILEBUF_INIT
;
2427 git_buf file_path
= GIT_BUF_INIT
;
2431 assert(repo
&& heads
);
2433 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_HEAD_FILE
)) < 0 ||
2434 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2437 for (i
= 0; i
< heads_len
; i
++) {
2438 if ((error
= git_filebuf_printf(&file
, "%s\n", heads
[i
]->id_str
)) < 0)
2442 error
= git_filebuf_commit(&file
);
2446 git_filebuf_cleanup(&file
);
2448 git_buf_dispose(&file_path
);
2453 static int write_merge_mode(git_repository
*repo
)
2455 git_filebuf file
= GIT_FILEBUF_INIT
;
2456 git_buf file_path
= GIT_BUF_INIT
;
2461 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MODE_FILE
)) < 0 ||
2462 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0)
2465 if ((error
= git_filebuf_write(&file
, "no-ff", 5)) < 0)
2468 error
= git_filebuf_commit(&file
);
2472 git_filebuf_cleanup(&file
);
2474 git_buf_dispose(&file_path
);
2479 struct merge_msg_entry
{
2480 const git_annotated_commit
*merge_head
;
2484 static int msg_entry_is_branch(
2485 const struct merge_msg_entry
*entry
,
2486 git_vector
*entries
)
2488 GIT_UNUSED(entries
);
2490 return (entry
->written
== 0 &&
2491 entry
->merge_head
->remote_url
== NULL
&&
2492 entry
->merge_head
->ref_name
!= NULL
&&
2493 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0);
2496 static int msg_entry_is_tracking(
2497 const struct merge_msg_entry
*entry
,
2498 git_vector
*entries
)
2500 GIT_UNUSED(entries
);
2502 return (entry
->written
== 0 &&
2503 entry
->merge_head
->remote_url
== NULL
&&
2504 entry
->merge_head
->ref_name
!= NULL
&&
2505 git__strncmp(GIT_REFS_REMOTES_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_REMOTES_DIR
)) == 0);
2508 static int msg_entry_is_tag(
2509 const struct merge_msg_entry
*entry
,
2510 git_vector
*entries
)
2512 GIT_UNUSED(entries
);
2514 return (entry
->written
== 0 &&
2515 entry
->merge_head
->remote_url
== NULL
&&
2516 entry
->merge_head
->ref_name
!= NULL
&&
2517 git__strncmp(GIT_REFS_TAGS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_TAGS_DIR
)) == 0);
2520 static int msg_entry_is_remote(
2521 const struct merge_msg_entry
*entry
,
2522 git_vector
*entries
)
2524 if (entry
->written
== 0 &&
2525 entry
->merge_head
->remote_url
!= NULL
&&
2526 entry
->merge_head
->ref_name
!= NULL
&&
2527 git__strncmp(GIT_REFS_HEADS_DIR
, entry
->merge_head
->ref_name
, strlen(GIT_REFS_HEADS_DIR
)) == 0)
2529 struct merge_msg_entry
*existing
;
2531 /* Match only branches from the same remote */
2532 if (entries
->length
== 0)
2535 existing
= git_vector_get(entries
, 0);
2537 return (git__strcmp(existing
->merge_head
->remote_url
,
2538 entry
->merge_head
->remote_url
) == 0);
2544 static int msg_entry_is_oid(
2545 const struct merge_msg_entry
*merge_msg_entry
)
2547 return (merge_msg_entry
->written
== 0 &&
2548 merge_msg_entry
->merge_head
->ref_name
== NULL
&&
2549 merge_msg_entry
->merge_head
->remote_url
== NULL
);
2552 static int merge_msg_entry_written(
2553 const struct merge_msg_entry
*merge_msg_entry
)
2555 return (merge_msg_entry
->written
== 1);
2558 static int merge_msg_entries(
2560 const struct merge_msg_entry
*entries
,
2562 int (*match
)(const struct merge_msg_entry
*entry
, git_vector
*entries
))
2565 int matches
, total
= 0;
2567 git_vector_clear(v
);
2569 for (i
= 0; i
< len
; i
++) {
2570 if ((matches
= match(&entries
[i
], v
)) < 0)
2575 git_vector_insert(v
, (struct merge_msg_entry
*)&entries
[i
]);
2582 static int merge_msg_write_entries(
2584 git_vector
*entries
,
2585 const char *item_name
,
2586 const char *item_plural_name
,
2587 size_t ref_name_skip
,
2591 struct merge_msg_entry
*entry
;
2595 if (entries
->length
== 0)
2598 if (sep
&& (error
= git_filebuf_printf(file
, "%c ", sep
)) < 0)
2601 if ((error
= git_filebuf_printf(file
, "%s ",
2602 (entries
->length
== 1) ? item_name
: item_plural_name
)) < 0)
2605 git_vector_foreach(entries
, i
, entry
) {
2607 (error
= git_filebuf_printf(file
, "%s", (i
== entries
->length
- 1) ? " and " : ", ")) < 0)
2610 if ((error
= git_filebuf_printf(file
, "'%s'", entry
->merge_head
->ref_name
+ ref_name_skip
)) < 0)
2617 error
= git_filebuf_printf(file
, " of %s", source
);
2623 static int merge_msg_write_branches(
2625 git_vector
*entries
,
2628 return merge_msg_write_entries(file
, entries
,
2629 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), NULL
, sep
);
2632 static int merge_msg_write_tracking(
2634 git_vector
*entries
,
2637 return merge_msg_write_entries(file
, entries
,
2638 "remote-tracking branch", "remote-tracking branches", 0, NULL
, sep
);
2641 static int merge_msg_write_tags(
2643 git_vector
*entries
,
2646 return merge_msg_write_entries(file
, entries
,
2647 "tag", "tags", strlen(GIT_REFS_TAGS_DIR
), NULL
, sep
);
2650 static int merge_msg_write_remotes(
2652 git_vector
*entries
,
2657 if (entries
->length
== 0)
2660 source
= ((struct merge_msg_entry
*)entries
->contents
[0])->merge_head
->remote_url
;
2662 return merge_msg_write_entries(file
, entries
,
2663 "branch", "branches", strlen(GIT_REFS_HEADS_DIR
), source
, sep
);
2666 static int write_merge_msg(
2667 git_repository
*repo
,
2668 const git_annotated_commit
*heads
[],
2671 git_filebuf file
= GIT_FILEBUF_INIT
;
2672 git_buf file_path
= GIT_BUF_INIT
;
2673 struct merge_msg_entry
*entries
;
2674 git_vector matching
= GIT_VECTOR_INIT
;
2679 assert(repo
&& heads
);
2681 entries
= git__calloc(heads_len
, sizeof(struct merge_msg_entry
));
2682 GIT_ERROR_CHECK_ALLOC(entries
);
2684 if (git_vector_init(&matching
, heads_len
, NULL
) < 0) {
2689 for (i
= 0; i
< heads_len
; i
++)
2690 entries
[i
].merge_head
= heads
[i
];
2692 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
2693 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_CREATE_LEADING_DIRS
, GIT_MERGE_FILE_MODE
)) < 0 ||
2694 (error
= git_filebuf_write(&file
, "Merge ", 6)) < 0)
2698 * This is to emulate the format of MERGE_MSG by core git.
2700 * Core git will write all the commits specified by OID, in the order
2701 * provided, until the first named branch or tag is reached, at which
2702 * point all branches will be written in the order provided, then all
2703 * tags, then all remote tracking branches and finally all commits that
2704 * were specified by OID that were not already written.
2708 for (i
= 0; i
< heads_len
; i
++) {
2709 if (!msg_entry_is_oid(&entries
[i
]))
2712 if ((error
= git_filebuf_printf(&file
,
2713 "%scommit '%s'", (i
> 0) ? "; " : "",
2714 entries
[i
].merge_head
->id_str
)) < 0)
2717 entries
[i
].written
= 1;
2723 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_branch
)) < 0 ||
2724 (error
= merge_msg_write_branches(&file
, &matching
, sep
)) < 0)
2727 if (matching
.length
)
2730 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tracking
)) < 0 ||
2731 (error
= merge_msg_write_tracking(&file
, &matching
, sep
)) < 0)
2734 if (matching
.length
)
2737 if ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_tag
)) < 0 ||
2738 (error
= merge_msg_write_tags(&file
, &matching
, sep
)) < 0)
2741 if (matching
.length
)
2744 /* We should never be called with multiple remote branches, but handle
2745 * it in case we are... */
2746 while ((error
= merge_msg_entries(&matching
, entries
, heads_len
, msg_entry_is_remote
)) > 0) {
2747 if ((error
= merge_msg_write_remotes(&file
, &matching
, sep
)) < 0)
2750 if (matching
.length
)
2757 for (i
= 0; i
< heads_len
; i
++) {
2758 if (merge_msg_entry_written(&entries
[i
]))
2761 if ((error
= git_filebuf_printf(&file
, "; commit '%s'",
2762 entries
[i
].merge_head
->id_str
)) < 0)
2766 if ((error
= git_filebuf_printf(&file
, "\n")) < 0 ||
2767 (error
= git_filebuf_commit(&file
)) < 0)
2772 git_filebuf_cleanup(&file
);
2774 git_buf_dispose(&file_path
);
2776 git_vector_free(&matching
);
2782 int git_merge__setup(
2783 git_repository
*repo
,
2784 const git_annotated_commit
*our_head
,
2785 const git_annotated_commit
*heads
[],
2790 assert (repo
&& our_head
&& heads
);
2792 if ((error
= git_repository__set_orig_head(repo
, git_annotated_commit_id(our_head
))) == 0 &&
2793 (error
= write_merge_head(repo
, heads
, heads_len
)) == 0 &&
2794 (error
= write_merge_mode(repo
)) == 0) {
2795 error
= write_merge_msg(repo
, heads
, heads_len
);
2801 /* Merge branches */
2803 static int merge_ancestor_head(
2804 git_annotated_commit
**ancestor_head
,
2805 git_repository
*repo
,
2806 const git_annotated_commit
*our_head
,
2807 const git_annotated_commit
**their_heads
,
2808 size_t their_heads_len
)
2810 git_oid
*oids
, ancestor_oid
;
2811 size_t i
, alloc_len
;
2814 assert(repo
&& our_head
&& their_heads
);
2816 GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len
, their_heads_len
, 1);
2817 oids
= git__calloc(alloc_len
, sizeof(git_oid
));
2818 GIT_ERROR_CHECK_ALLOC(oids
);
2820 git_oid_cpy(&oids
[0], git_commit_id(our_head
->commit
));
2822 for (i
= 0; i
< their_heads_len
; i
++)
2823 git_oid_cpy(&oids
[i
+ 1], git_annotated_commit_id(their_heads
[i
]));
2825 if ((error
= git_merge_base_many(&ancestor_oid
, repo
, their_heads_len
+ 1, oids
)) < 0)
2828 error
= git_annotated_commit_lookup(ancestor_head
, repo
, &ancestor_oid
);
2835 const char *merge_their_label(const char *branchname
)
2839 if ((slash
= strrchr(branchname
, '/')) == NULL
)
2842 if (*(slash
+1) == '\0')
2848 static int merge_normalize_checkout_opts(
2849 git_checkout_options
*out
,
2850 git_repository
*repo
,
2851 const git_checkout_options
*given_checkout_opts
,
2852 unsigned int checkout_strategy
,
2853 git_annotated_commit
*ancestor
,
2854 const git_annotated_commit
*our_head
,
2855 const git_annotated_commit
**their_heads
,
2856 size_t their_heads_len
)
2858 git_checkout_options default_checkout_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
2863 if (given_checkout_opts
!= NULL
)
2864 memcpy(out
, given_checkout_opts
, sizeof(git_checkout_options
));
2866 memcpy(out
, &default_checkout_opts
, sizeof(git_checkout_options
));
2868 out
->checkout_strategy
= checkout_strategy
;
2870 if (!out
->ancestor_label
) {
2871 if (ancestor
&& ancestor
->type
== GIT_ANNOTATED_COMMIT_REAL
)
2872 out
->ancestor_label
= git_commit_summary(ancestor
->commit
);
2874 out
->ancestor_label
= "merged common ancestors";
2876 out
->ancestor_label
= "empty base";
2879 if (!out
->our_label
) {
2880 if (our_head
&& our_head
->ref_name
)
2881 out
->our_label
= our_head
->ref_name
;
2883 out
->our_label
= "ours";
2886 if (!out
->their_label
) {
2887 if (their_heads_len
== 1 && their_heads
[0]->ref_name
)
2888 out
->their_label
= merge_their_label(their_heads
[0]->ref_name
);
2889 else if (their_heads_len
== 1)
2890 out
->their_label
= their_heads
[0]->id_str
;
2892 out
->their_label
= "theirs";
2898 static int merge_check_index(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
2900 git_tree
*head_tree
= NULL
;
2901 git_index
*index_repo
= NULL
;
2902 git_iterator
*iter_repo
= NULL
, *iter_new
= NULL
;
2903 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
2904 git_diff
*staged_diff_list
= NULL
, *index_diff_list
= NULL
;
2905 git_diff_delta
*delta
;
2906 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
2907 git_vector staged_paths
= GIT_VECTOR_INIT
;
2911 GIT_UNUSED(merged_paths
);
2915 /* No staged changes may exist unless the change staged is identical to
2916 * the result of the merge. This allows one to apply to merge manually,
2917 * then run merge. Any other staged change would be overwritten by
2920 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
2921 (error
= git_repository_index(&index_repo
, repo
)) < 0 ||
2922 (error
= git_diff_tree_to_index(&staged_diff_list
, repo
, head_tree
, index_repo
, &opts
)) < 0)
2925 if (staged_diff_list
->deltas
.length
== 0)
2928 git_vector_foreach(&staged_diff_list
->deltas
, i
, delta
) {
2929 if ((error
= git_vector_insert(&staged_paths
, (char *)delta
->new_file
.path
)) < 0)
2933 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
2934 iter_opts
.pathlist
.strings
= (char **)staged_paths
.contents
;
2935 iter_opts
.pathlist
.count
= staged_paths
.length
;
2937 if ((error
= git_iterator_for_index(&iter_repo
, repo
, index_repo
, &iter_opts
)) < 0 ||
2938 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
2939 (error
= git_diff__from_iterators(&index_diff_list
, repo
, iter_repo
, iter_new
, &opts
)) < 0)
2942 *conflicts
= index_diff_list
->deltas
.length
;
2945 git_tree_free(head_tree
);
2946 git_index_free(index_repo
);
2947 git_iterator_free(iter_repo
);
2948 git_iterator_free(iter_new
);
2949 git_diff_free(staged_diff_list
);
2950 git_diff_free(index_diff_list
);
2951 git_vector_free(&staged_paths
);
2956 static int merge_check_workdir(size_t *conflicts
, git_repository
*repo
, git_index
*index_new
, git_vector
*merged_paths
)
2958 git_diff
*wd_diff_list
= NULL
;
2959 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
2962 GIT_UNUSED(index_new
);
2966 /* We need to have merged at least 1 file for the possibility to exist to
2967 * have conflicts with the workdir. Passing 0 as the pathspec count paramter
2968 * will consider all files in the working directory, that is, we may detect
2969 * a conflict if there were untracked files in the workdir prior to starting
2970 * the merge. This typically happens when cherry-picking a commmit whose
2971 * changes have already been applied.
2973 if (merged_paths
->length
== 0)
2976 opts
.flags
|= GIT_DIFF_INCLUDE_UNTRACKED
;
2978 /* Workdir changes may exist iff they do not conflict with changes that
2979 * will be applied by the merge (including conflicts). Ensure that there
2980 * are no changes in the workdir to these paths.
2982 opts
.flags
|= GIT_DIFF_DISABLE_PATHSPEC_MATCH
;
2983 opts
.pathspec
.count
= merged_paths
->length
;
2984 opts
.pathspec
.strings
= (char **)merged_paths
->contents
;
2985 opts
.ignore_submodules
= GIT_SUBMODULE_IGNORE_ALL
;
2987 if ((error
= git_diff_index_to_workdir(&wd_diff_list
, repo
, NULL
, &opts
)) < 0)
2990 *conflicts
= wd_diff_list
->deltas
.length
;
2993 git_diff_free(wd_diff_list
);
2998 int git_merge__check_result(git_repository
*repo
, git_index
*index_new
)
3000 git_tree
*head_tree
= NULL
;
3001 git_iterator
*iter_head
= NULL
, *iter_new
= NULL
;
3002 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
3003 git_diff
*merged_list
= NULL
;
3004 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
3005 git_diff_delta
*delta
;
3006 git_vector paths
= GIT_VECTOR_INIT
;
3007 size_t i
, index_conflicts
= 0, wd_conflicts
= 0, conflicts
;
3008 const git_index_entry
*e
;
3011 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
3013 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
3014 (error
= git_iterator_for_tree(&iter_head
, head_tree
, &iter_opts
)) < 0 ||
3015 (error
= git_iterator_for_index(&iter_new
, repo
, index_new
, &iter_opts
)) < 0 ||
3016 (error
= git_diff__from_iterators(&merged_list
, repo
, iter_head
, iter_new
, &opts
)) < 0)
3019 git_vector_foreach(&merged_list
->deltas
, i
, delta
) {
3020 if ((error
= git_vector_insert(&paths
, (char *)delta
->new_file
.path
)) < 0)
3024 for (i
= 0; i
< git_index_entrycount(index_new
); i
++) {
3025 e
= git_index_get_byindex(index_new
, i
);
3027 if (git_index_entry_is_conflict(e
) &&
3028 (git_vector_last(&paths
) == NULL
||
3029 strcmp(git_vector_last(&paths
), e
->path
) != 0)) {
3031 if ((error
= git_vector_insert(&paths
, (char *)e
->path
)) < 0)
3036 /* Make sure the index and workdir state do not prevent merging */
3037 if ((error
= merge_check_index(&index_conflicts
, repo
, index_new
, &paths
)) < 0 ||
3038 (error
= merge_check_workdir(&wd_conflicts
, repo
, index_new
, &paths
)) < 0)
3041 if ((conflicts
= index_conflicts
+ wd_conflicts
) > 0) {
3042 git_error_set(GIT_ERROR_MERGE
, "%" PRIuZ
" uncommitted change%s would be overwritten by merge",
3043 conflicts
, (conflicts
!= 1) ? "s" : "");
3044 error
= GIT_ECONFLICT
;
3048 git_vector_free(&paths
);
3049 git_tree_free(head_tree
);
3050 git_iterator_free(iter_head
);
3051 git_iterator_free(iter_new
);
3052 git_diff_free(merged_list
);
3057 int git_merge__append_conflicts_to_merge_msg(
3058 git_repository
*repo
,
3061 git_filebuf file
= GIT_FILEBUF_INIT
;
3062 git_buf file_path
= GIT_BUF_INIT
;
3063 const char *last
= NULL
;
3067 if (!git_index_has_conflicts(index
))
3070 if ((error
= git_buf_joinpath(&file_path
, repo
->gitdir
, GIT_MERGE_MSG_FILE
)) < 0 ||
3071 (error
= git_filebuf_open(&file
, file_path
.ptr
, GIT_FILEBUF_APPEND
, GIT_MERGE_FILE_MODE
)) < 0)
3074 git_filebuf_printf(&file
, "\nConflicts:\n");
3076 for (i
= 0; i
< git_index_entrycount(index
); i
++) {
3077 const git_index_entry
*e
= git_index_get_byindex(index
, i
);
3079 if (!git_index_entry_is_conflict(e
))
3082 if (last
== NULL
|| strcmp(e
->path
, last
) != 0)
3083 git_filebuf_printf(&file
, "\t%s\n", e
->path
);
3088 error
= git_filebuf_commit(&file
);
3092 git_filebuf_cleanup(&file
);
3094 git_buf_dispose(&file_path
);
3099 static int merge_state_cleanup(git_repository
*repo
)
3101 const char *state_files
[] = {
3102 GIT_MERGE_HEAD_FILE
,
3103 GIT_MERGE_MODE_FILE
,
3107 return git_repository__cleanup_files(repo
, state_files
, ARRAY_SIZE(state_files
));
3110 static int merge_heads(
3111 git_annotated_commit
**ancestor_head_out
,
3112 git_annotated_commit
**our_head_out
,
3113 git_repository
*repo
,
3114 git_reference
*our_ref
,
3115 const git_annotated_commit
**their_heads
,
3116 size_t their_heads_len
)
3118 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3121 *ancestor_head_out
= NULL
;
3122 *our_head_out
= NULL
;
3124 if ((error
= git_annotated_commit_from_ref(&our_head
, repo
, our_ref
)) < 0)
3127 if ((error
= merge_ancestor_head(&ancestor_head
, repo
, our_head
, their_heads
, their_heads_len
)) < 0) {
3128 if (error
!= GIT_ENOTFOUND
)
3135 *ancestor_head_out
= ancestor_head
;
3136 *our_head_out
= our_head
;
3140 git_annotated_commit_free(ancestor_head
);
3141 git_annotated_commit_free(our_head
);
3147 static int merge_preference(git_merge_preference_t
*out
, git_repository
*repo
)
3151 int bool_value
, error
= 0;
3153 *out
= GIT_MERGE_PREFERENCE_NONE
;
3155 if ((error
= git_repository_config_snapshot(&config
, repo
)) < 0)
3158 if ((error
= git_config_get_string(&value
, config
, "merge.ff")) < 0) {
3159 if (error
== GIT_ENOTFOUND
) {
3167 if (git_config_parse_bool(&bool_value
, value
) == 0) {
3169 *out
|= GIT_MERGE_PREFERENCE_NO_FASTFORWARD
;
3171 if (strcasecmp(value
, "only") == 0)
3172 *out
|= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY
;
3176 git_config_free(config
);
3180 int git_merge_analysis_for_ref(
3181 git_merge_analysis_t
*analysis_out
,
3182 git_merge_preference_t
*preference_out
,
3183 git_repository
*repo
,
3184 git_reference
*our_ref
,
3185 const git_annotated_commit
**their_heads
,
3186 size_t their_heads_len
)
3188 git_annotated_commit
*ancestor_head
= NULL
, *our_head
= NULL
;
3192 assert(analysis_out
&& preference_out
&& repo
&& their_heads
&& their_heads_len
> 0);
3194 if (their_heads_len
!= 1) {
3195 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3200 *analysis_out
= GIT_MERGE_ANALYSIS_NONE
;
3202 if ((error
= merge_preference(preference_out
, repo
)) < 0)
3205 if ((error
= git_reference__is_unborn_head(&unborn
, our_ref
, repo
)) < 0)
3209 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_UNBORN
;
3214 if ((error
= merge_heads(&ancestor_head
, &our_head
, repo
, our_ref
, their_heads
, their_heads_len
)) < 0)
3217 /* We're up-to-date if we're trying to merge our own common ancestor. */
3218 if (ancestor_head
&& git_oid_equal(
3219 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(their_heads
[0])))
3220 *analysis_out
|= GIT_MERGE_ANALYSIS_UP_TO_DATE
;
3222 /* We're fastforwardable if we're our own common ancestor. */
3223 else if (ancestor_head
&& git_oid_equal(
3224 git_annotated_commit_id(ancestor_head
), git_annotated_commit_id(our_head
)))
3225 *analysis_out
|= GIT_MERGE_ANALYSIS_FASTFORWARD
| GIT_MERGE_ANALYSIS_NORMAL
;
3227 /* Otherwise, just a normal merge is possible. */
3229 *analysis_out
|= GIT_MERGE_ANALYSIS_NORMAL
;
3232 git_annotated_commit_free(ancestor_head
);
3233 git_annotated_commit_free(our_head
);
3237 int git_merge_analysis(
3238 git_merge_analysis_t
*analysis_out
,
3239 git_merge_preference_t
*preference_out
,
3240 git_repository
*repo
,
3241 const git_annotated_commit
**their_heads
,
3242 size_t their_heads_len
)
3244 git_reference
*head_ref
= NULL
;
3247 if ((error
= git_reference_lookup(&head_ref
, repo
, GIT_HEAD_FILE
)) < 0) {
3248 git_error_set(GIT_ERROR_MERGE
, "failed to lookup HEAD reference");
3252 error
= git_merge_analysis_for_ref(analysis_out
, preference_out
, repo
, head_ref
, their_heads
, their_heads_len
);
3254 git_reference_free(head_ref
);
3260 git_repository
*repo
,
3261 const git_annotated_commit
**their_heads
,
3262 size_t their_heads_len
,
3263 const git_merge_options
*merge_opts
,
3264 const git_checkout_options
*given_checkout_opts
)
3266 git_reference
*our_ref
= NULL
;
3267 git_checkout_options checkout_opts
;
3268 git_annotated_commit
*our_head
= NULL
, *base
= NULL
;
3269 git_index
*repo_index
= NULL
, *index
= NULL
;
3270 git_indexwriter indexwriter
= GIT_INDEXWRITER_INIT
;
3271 unsigned int checkout_strategy
;
3274 assert(repo
&& their_heads
&& their_heads_len
> 0);
3276 if (their_heads_len
!= 1) {
3277 git_error_set(GIT_ERROR_MERGE
, "can only merge a single branch");
3281 if ((error
= git_repository__ensure_not_bare(repo
, "merge")) < 0)
3284 checkout_strategy
= given_checkout_opts
?
3285 given_checkout_opts
->checkout_strategy
:
3288 if ((error
= git_indexwriter_init_for_operation(&indexwriter
, repo
,
3289 &checkout_strategy
)) < 0)
3292 if ((error
= git_repository_index(&repo_index
, repo
) < 0) ||
3293 (error
= git_index_read(repo_index
, 0) < 0))
3296 /* Write the merge setup files to the repository. */
3297 if ((error
= git_annotated_commit_from_head(&our_head
, repo
)) < 0 ||
3298 (error
= git_merge__setup(repo
, our_head
, their_heads
,
3299 their_heads_len
)) < 0)
3304 if ((error
= merge_annotated_commits(&index
, &base
, repo
, our_head
,
3305 (git_annotated_commit
*)their_heads
[0], 0, merge_opts
)) < 0 ||
3306 (error
= git_merge__check_result(repo
, index
)) < 0 ||
3307 (error
= git_merge__append_conflicts_to_merge_msg(repo
, index
)) < 0)
3310 /* check out the merge results */
3312 if ((error
= merge_normalize_checkout_opts(&checkout_opts
, repo
,
3313 given_checkout_opts
, checkout_strategy
,
3314 base
, our_head
, their_heads
, their_heads_len
)) < 0 ||
3315 (error
= git_checkout_index(repo
, index
, &checkout_opts
)) < 0)
3318 error
= git_indexwriter_commit(&indexwriter
);
3322 merge_state_cleanup(repo
);
3324 git_indexwriter_cleanup(&indexwriter
);
3325 git_index_free(index
);
3326 git_annotated_commit_free(our_head
);
3327 git_annotated_commit_free(base
);
3328 git_reference_free(our_ref
);
3329 git_index_free(repo_index
);
3334 int git_merge_options_init(git_merge_options
*opts
, unsigned int version
)
3336 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3337 opts
, version
, git_merge_options
, GIT_MERGE_OPTIONS_INIT
);
3341 int git_merge_init_options(git_merge_options
*opts
, unsigned int version
)
3343 return git_merge_options_init(opts
, version
);
3346 int git_merge_file_input_init(git_merge_file_input
*input
, unsigned int version
)
3348 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3349 input
, version
, git_merge_file_input
, GIT_MERGE_FILE_INPUT_INIT
);
3353 int git_merge_file_init_input(git_merge_file_input
*input
, unsigned int version
)
3355 return git_merge_file_input_init(input
, version
);
3358 int git_merge_file_options_init(
3359 git_merge_file_options
*opts
, unsigned int version
)
3361 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3362 opts
, version
, git_merge_file_options
, GIT_MERGE_FILE_OPTIONS_INIT
);
3366 int git_merge_file_init_options(
3367 git_merge_file_options
*opts
, unsigned int version
)
3369 return git_merge_file_options_init(opts
, version
);