]> git.proxmox.com Git - libgit2.git/blob - src/merge.c
Revert "Update d/ch for 0.28.5+dfsg.1-1 release -- from unstable branch"
[libgit2.git] / src / merge.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
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.
6 */
7
8 #include "merge.h"
9
10 #include "posix.h"
11 #include "buffer.h"
12 #include "repository.h"
13 #include "revwalk.h"
14 #include "commit_list.h"
15 #include "path.h"
16 #include "refs.h"
17 #include "object.h"
18 #include "iterator.h"
19 #include "refs.h"
20 #include "diff.h"
21 #include "diff_generate.h"
22 #include "diff_tform.h"
23 #include "checkout.h"
24 #include "tree.h"
25 #include "blob.h"
26 #include "oid.h"
27 #include "index.h"
28 #include "filebuf.h"
29 #include "config.h"
30 #include "oidarray.h"
31 #include "annotated_commit.h"
32 #include "commit.h"
33 #include "oidarray.h"
34 #include "merge_driver.h"
35 #include "oidmap.h"
36 #include "array.h"
37
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"
53
54 #define GIT_MERGE_INDEX_ENTRY_EXISTS(X) ((X).mode != 0)
55 #define GIT_MERGE_INDEX_ENTRY_ISFILE(X) S_ISREG((X).mode)
56
57
58 typedef enum {
59 TREE_IDX_ANCESTOR = 0,
60 TREE_IDX_OURS = 1,
61 TREE_IDX_THEIRS = 2
62 } merge_tree_index_t;
63
64 /* Tracks D/F conflicts */
65 struct merge_diff_df_data {
66 const char *df_path;
67 const char *prev_path;
68 git_merge_diff *prev_conflict;
69 };
70
71 /* Merge base computation */
72
73 int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, size_t length, const git_oid input_array[])
74 {
75 git_revwalk *walk = NULL;
76 git_vector list;
77 git_commit_list *result = NULL;
78 git_commit_list_node *commit;
79 int error = -1;
80 unsigned int i;
81
82 if (length < 2) {
83 git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor");
84 return -1;
85 }
86
87 if (git_vector_init(&list, length - 1, NULL) < 0)
88 return -1;
89
90 if (git_revwalk_new(&walk, repo) < 0)
91 goto on_error;
92
93 for (i = 1; i < length; i++) {
94 commit = git_revwalk__commit_lookup(walk, &input_array[i]);
95 if (commit == NULL)
96 goto on_error;
97
98 git_vector_insert(&list, commit);
99 }
100
101 commit = git_revwalk__commit_lookup(walk, &input_array[0]);
102 if (commit == NULL)
103 goto on_error;
104
105 if (git_merge__bases_many(&result, walk, commit, &list) < 0)
106 goto on_error;
107
108 if (!result) {
109 git_error_set(GIT_ERROR_MERGE, "no merge base found");
110 error = GIT_ENOTFOUND;
111 goto on_error;
112 }
113
114 *out = result;
115 *walk_out = walk;
116
117 git_vector_free(&list);
118 return 0;
119
120 on_error:
121 git_vector_free(&list);
122 git_revwalk_free(walk);
123 return error;
124 }
125
126 int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[])
127 {
128 git_revwalk *walk;
129 git_commit_list *result = NULL;
130 int error = 0;
131
132 assert(out && repo && input_array);
133
134 if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0)
135 return error;
136
137 git_oid_cpy(out, &result->item->oid);
138
139 git_commit_list_free(&result);
140 git_revwalk_free(walk);
141
142 return 0;
143 }
144
145 int git_merge_bases_many(git_oidarray *out, git_repository *repo, size_t length, const git_oid input_array[])
146 {
147 git_revwalk *walk;
148 git_commit_list *list, *result = NULL;
149 int error = 0;
150 git_array_oid_t array;
151
152 assert(out && repo && input_array);
153
154 if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0)
155 return error;
156
157 git_array_init(array);
158
159 list = result;
160 while (list) {
161 git_oid *id = git_array_alloc(array);
162 if (id == NULL) {
163 error = -1;
164 goto cleanup;
165 }
166
167 git_oid_cpy(id, &list->item->oid);
168 list = list->next;
169 }
170
171 git_oidarray__from_array(out, &array);
172
173 cleanup:
174 git_commit_list_free(&result);
175 git_revwalk_free(walk);
176
177 return error;
178 }
179
180 int git_merge_base_octopus(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[])
181 {
182 git_oid result;
183 unsigned int i;
184 int error = -1;
185
186 assert(out && repo && input_array);
187
188 if (length < 2) {
189 git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor");
190 return -1;
191 }
192
193 result = input_array[0];
194 for (i = 1; i < length; i++) {
195 error = git_merge_base(&result, repo, &result, &input_array[i]);
196 if (error < 0)
197 return error;
198 }
199
200 *out = result;
201
202 return 0;
203 }
204
205 static int merge_bases(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, const git_oid *one, const git_oid *two)
206 {
207 git_revwalk *walk;
208 git_vector list;
209 git_commit_list *result = NULL;
210 git_commit_list_node *commit;
211 void *contents[1];
212
213 if (git_revwalk_new(&walk, repo) < 0)
214 return -1;
215
216 commit = git_revwalk__commit_lookup(walk, two);
217 if (commit == NULL)
218 goto on_error;
219
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;
223 list.length = 1;
224 list.contents = contents;
225
226 commit = git_revwalk__commit_lookup(walk, one);
227 if (commit == NULL)
228 goto on_error;
229
230 if (git_merge__bases_many(&result, walk, commit, &list) < 0)
231 goto on_error;
232
233 if (!result) {
234 git_revwalk_free(walk);
235 git_error_set(GIT_ERROR_MERGE, "no merge base found");
236 return GIT_ENOTFOUND;
237 }
238
239 *out = result;
240 *walk_out = walk;
241
242 return 0;
243
244 on_error:
245 git_revwalk_free(walk);
246 return -1;
247
248 }
249
250 int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const git_oid *two)
251 {
252 int error;
253 git_revwalk *walk;
254 git_commit_list *result;
255
256 if ((error = merge_bases(&result, &walk, repo, one, two)) < 0)
257 return error;
258
259 git_oid_cpy(out, &result->item->oid);
260 git_commit_list_free(&result);
261 git_revwalk_free(walk);
262
263 return 0;
264 }
265
266 int git_merge_bases(git_oidarray *out, git_repository *repo, const git_oid *one, const git_oid *two)
267 {
268 int error;
269 git_revwalk *walk;
270 git_commit_list *result, *list;
271 git_array_oid_t array;
272
273 git_array_init(array);
274
275 if ((error = merge_bases(&result, &walk, repo, one, two)) < 0)
276 return error;
277
278 list = result;
279 while (list) {
280 git_oid *id = git_array_alloc(array);
281 if (id == NULL)
282 goto on_error;
283
284 git_oid_cpy(id, &list->item->oid);
285 list = list->next;
286 }
287
288 git_oidarray__from_array(out, &array);
289 git_commit_list_free(&result);
290 git_revwalk_free(walk);
291
292 return 0;
293
294 on_error:
295 git_commit_list_free(&result);
296 git_revwalk_free(walk);
297 return -1;
298 }
299
300 static int interesting(git_pqueue *list)
301 {
302 size_t i;
303
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)
307 return 1;
308 }
309
310 return 0;
311 }
312
313 static int clear_commit_marks_1(git_commit_list **plist,
314 git_commit_list_node *commit, unsigned int mark)
315 {
316 while (commit) {
317 unsigned int i;
318
319 if (!(mark & commit->flags))
320 return 0;
321
322 commit->flags &= ~mark;
323
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)
327 return -1;
328 }
329
330 commit = commit->out_degree ? commit->parents[0] : NULL;
331 }
332
333 return 0;
334 }
335
336 static int clear_commit_marks_many(git_vector *commits, unsigned int mark)
337 {
338 git_commit_list *list = NULL;
339 git_commit_list_node *c;
340 unsigned int i;
341
342 git_vector_foreach(commits, i, c) {
343 if (git_commit_list_insert(c, &list) == NULL)
344 return -1;
345 }
346
347 while (list)
348 if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0)
349 return -1;
350 return 0;
351 }
352
353 static int clear_commit_marks(git_commit_list_node *commit, unsigned int mark)
354 {
355 git_commit_list *list = NULL;
356 if (git_commit_list_insert(commit, &list) == NULL)
357 return -1;
358 while (list)
359 if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0)
360 return -1;
361 return 0;
362 }
363
364 static int paint_down_to_common(
365 git_commit_list **out, git_revwalk *walk, git_commit_list_node *one, git_vector *twos)
366 {
367 git_pqueue list;
368 git_commit_list *result = NULL;
369 git_commit_list_node *two;
370
371 int error;
372 unsigned int i;
373
374 if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_time_cmp) < 0)
375 return -1;
376
377 one->flags |= PARENT1;
378 if (git_pqueue_insert(&list, one) < 0)
379 return -1;
380
381 git_vector_foreach(twos, i, two) {
382 if (git_commit_list_parse(walk, two) < 0)
383 return -1;
384
385 two->flags |= PARENT2;
386
387 if (git_pqueue_insert(&list, two) < 0)
388 return -1;
389 }
390
391 /* as long as there are non-STALE commits */
392 while (interesting(&list)) {
393 git_commit_list_node *commit = git_pqueue_pop(&list);
394 int flags;
395
396 if (commit == NULL)
397 break;
398
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)
404 return -1;
405 }
406 /* we mark the parents of a merge stale */
407 flags |= STALE;
408 }
409
410 for (i = 0; i < commit->out_degree; i++) {
411 git_commit_list_node *p = commit->parents[i];
412 if ((p->flags & flags) == flags)
413 continue;
414
415 if ((error = git_commit_list_parse(walk, p)) < 0)
416 return error;
417
418 p->flags |= flags;
419 if (git_pqueue_insert(&list, p) < 0)
420 return -1;
421 }
422 }
423
424 git_pqueue_free(&list);
425 *out = result;
426 return 0;
427 }
428
429 static int remove_redundant(git_revwalk *walk, git_vector *commits)
430 {
431 git_vector work = GIT_VECTOR_INIT;
432 unsigned char *redundant;
433 unsigned int *filled_index;
434 unsigned int i, j;
435 int error = 0;
436
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);
441
442 for (i = 0; i < commits->length; ++i) {
443 if ((error = git_commit_list_parse(walk, commits->contents[i])) < 0)
444 goto done;
445 }
446
447 for (i = 0; i < commits->length; ++i) {
448 git_commit_list *common = NULL;
449 git_commit_list_node *commit = commits->contents[i];
450
451 if (redundant[i])
452 continue;
453
454 git_vector_clear(&work);
455
456 for (j = 0; j < commits->length; j++) {
457 if (i == j || redundant[j])
458 continue;
459
460 filled_index[work.length] = j;
461 if ((error = git_vector_insert(&work, commits->contents[j])) < 0)
462 goto done;
463 }
464
465 error = paint_down_to_common(&common, walk, commit, &work);
466 if (error < 0)
467 goto done;
468
469 if (commit->flags & PARENT2)
470 redundant[i] = 1;
471
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;
476 }
477
478 git_commit_list_free(&common);
479
480 if ((error = clear_commit_marks(commit, ALL_FLAGS)) < 0 ||
481 (error = clear_commit_marks_many(&work, ALL_FLAGS)) < 0)
482 goto done;
483 }
484
485 for (i = 0; i < commits->length; ++i) {
486 if (redundant[i])
487 commits->contents[i] = NULL;
488 }
489
490 done:
491 git__free(redundant);
492 git__free(filled_index);
493 git_vector_free(&work);
494 return error;
495 }
496
497 int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_list_node *one, git_vector *twos)
498 {
499 int error;
500 unsigned int i;
501 git_commit_list_node *two;
502 git_commit_list *result = NULL, *tmp = NULL;
503
504 /* If there's only the one commit, there can be no merge bases */
505 if (twos->length == 0) {
506 *out = NULL;
507 return 0;
508 }
509
510 /* if the commit is repeated, we have a our merge base already */
511 git_vector_foreach(twos, i, two) {
512 if (one == two)
513 return git_commit_list_insert(one, out) ? 0 : -1;
514 }
515
516 if (git_commit_list_parse(walk, one) < 0)
517 return -1;
518
519 error = paint_down_to_common(&result, walk, one, twos);
520 if (error < 0)
521 return error;
522
523 /* filter out any stale commits in the results */
524 tmp = result;
525 result = NULL;
526
527 while (tmp) {
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)
531 return -1;
532 }
533
534 /*
535 * more than one merge base -- see if there are redundant merge
536 * bases and remove them
537 */
538 if (result && result->next) {
539 git_vector redundant = GIT_VECTOR_INIT;
540
541 while (result)
542 git_vector_insert(&redundant, git_commit_list_pop(&result));
543
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);
548 return error;
549 }
550
551 git_vector_foreach(&redundant, i, two) {
552 if (two != NULL)
553 git_commit_list_insert_by_date(two, &result);
554 }
555
556 git_vector_free(&redundant);
557 }
558
559 *out = result;
560 return 0;
561 }
562
563 int git_repository_mergehead_foreach(
564 git_repository *repo,
565 git_repository_mergehead_foreach_cb cb,
566 void *payload)
567 {
568 git_buf merge_head_path = GIT_BUF_INIT, merge_head_file = GIT_BUF_INIT;
569 char *buffer, *line;
570 size_t line_num = 1;
571 git_oid oid;
572 int error = 0;
573
574 assert(repo && cb);
575
576 if ((error = git_buf_joinpath(&merge_head_path, repo->gitdir,
577 GIT_MERGE_HEAD_FILE)) < 0)
578 return error;
579
580 if ((error = git_futils_readbuffer(&merge_head_file,
581 git_buf_cstr(&merge_head_path))) < 0)
582 goto cleanup;
583
584 buffer = merge_head_file.ptr;
585
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");
589 error = -1;
590 goto cleanup;
591 }
592
593 if ((error = git_oid_fromstr(&oid, line)) < 0)
594 goto cleanup;
595
596 if ((error = cb(&oid, payload)) != 0) {
597 git_error_set_after_callback(error);
598 goto cleanup;
599 }
600
601 ++line_num;
602 }
603
604 if (*buffer) {
605 git_error_set(GIT_ERROR_MERGE, "no EOL at line %"PRIuZ, line_num);
606 error = -1;
607 goto cleanup;
608 }
609
610 cleanup:
611 git_buf_dispose(&merge_head_path);
612 git_buf_dispose(&merge_head_file);
613
614 return error;
615 }
616
617 GIT_INLINE(int) index_entry_cmp(const git_index_entry *a, const git_index_entry *b)
618 {
619 int value = 0;
620
621 if (a->path == NULL)
622 return (b->path == NULL) ? 0 : 1;
623
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);
627
628 return value;
629 }
630
631 /* Conflict resolution */
632
633 static int merge_conflict_resolve_trivial(
634 int *resolved,
635 git_merge_diff_list *diff_list,
636 const git_merge_diff *conflict)
637 {
638 int ours_empty, theirs_empty;
639 int ours_changed, theirs_changed, ours_theirs_differ;
640 git_index_entry const *result = NULL;
641 int error = 0;
642
643 assert(resolved && diff_list && conflict);
644
645 *resolved = 0;
646
647 if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
648 conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
649 return 0;
650
651 if (conflict->our_status == GIT_DELTA_RENAMED ||
652 conflict->their_status == GIT_DELTA_RENAMED)
653 return 0;
654
655 ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
656 theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
657
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);
662
663 /*
664 * Note: with only one ancestor, some cases are not distinct:
665 *
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
669 *
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:
673 *
674 * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
675 * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
676 *
677 * Note that many of these cases need not be explicitly tested, as
678 * they simply degrade to "all different" cases (eg, 11):
679 *
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
684 */
685
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)
691 *resolved = 0;
692 /* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
693 else if (ours_empty && !theirs_changed)
694 *resolved = 0;
695 /* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
696 else if (!ours_changed && theirs_empty)
697 *resolved = 0;
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;
704 else
705 *resolved = 0;
706
707 if (result != NULL &&
708 GIT_MERGE_INDEX_ENTRY_EXISTS(*result) &&
709 (error = git_vector_insert(&diff_list->staged, (void *)result)) >= 0)
710 *resolved = 1;
711
712 /* Note: trivial resolution does not update the REUC. */
713
714 return error;
715 }
716
717 static int merge_conflict_resolve_one_removed(
718 int *resolved,
719 git_merge_diff_list *diff_list,
720 const git_merge_diff *conflict)
721 {
722 int ours_empty, theirs_empty;
723 int ours_changed, theirs_changed;
724 int error = 0;
725
726 assert(resolved && diff_list && conflict);
727
728 *resolved = 0;
729
730 if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
731 conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
732 return 0;
733
734 ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
735 theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
736
737 ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED);
738 theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED);
739
740 /* Removed in both */
741 if (ours_changed && ours_empty && theirs_empty)
742 *resolved = 1;
743 /* Removed in ours */
744 else if (ours_empty && !theirs_changed)
745 *resolved = 1;
746 /* Removed in theirs */
747 else if (!ours_changed && theirs_empty)
748 *resolved = 1;
749
750 if (*resolved)
751 git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
752
753 return error;
754 }
755
756 static int merge_conflict_resolve_one_renamed(
757 int *resolved,
758 git_merge_diff_list *diff_list,
759 const git_merge_diff *conflict)
760 {
761 int ours_renamed, theirs_renamed;
762 int ours_changed, theirs_changed;
763 git_index_entry *merged;
764 int error = 0;
765
766 assert(resolved && diff_list && conflict);
767
768 *resolved = 0;
769
770 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
771 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
772 return 0;
773
774 ours_renamed = (conflict->our_status == GIT_DELTA_RENAMED);
775 theirs_renamed = (conflict->their_status == GIT_DELTA_RENAMED);
776
777 if (!ours_renamed && !theirs_renamed)
778 return 0;
779
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)
784 return 0;
785
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);
788
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)
792 return 0;
793
794 if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
795 return -1;
796
797 if (ours_changed)
798 memcpy(merged, &conflict->our_entry, sizeof(git_index_entry));
799 else
800 memcpy(merged, &conflict->their_entry, sizeof(git_index_entry));
801
802 if (ours_renamed)
803 merged->path = conflict->our_entry.path;
804 else
805 merged->path = conflict->their_entry.path;
806
807 *resolved = 1;
808
809 git_vector_insert(&diff_list->staged, merged);
810 git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
811
812 return error;
813 }
814
815 static bool merge_conflict_can_resolve_contents(
816 const git_merge_diff *conflict)
817 {
818 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
819 !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
820 return false;
821
822 /* Reject D/F conflicts */
823 if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE)
824 return false;
825
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))
830 return false;
831
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)))
837 return false;
838
839 /* Reject name conflicts */
840 if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 ||
841 conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
842 return false;
843
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)
847 return false;
848
849 return true;
850 }
851
852 static int merge_conflict_invoke_driver(
853 git_index_entry **out,
854 const char *name,
855 git_merge_driver *driver,
856 git_merge_diff_list *diff_list,
857 git_merge_driver_source *src)
858 {
859 git_index_entry *result;
860 git_buf buf = GIT_BUF_INIT;
861 const char *path;
862 uint32_t mode;
863 git_odb *odb = NULL;
864 git_oid oid;
865 int error;
866
867 *out = NULL;
868
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)
872 goto done;
873
874 result = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry));
875 GIT_ERROR_CHECK_ALLOC(result);
876
877 git_oid_cpy(&result->id, &oid);
878 result->mode = mode;
879 result->file_size = (uint32_t)buf.size;
880
881 result->path = git_pool_strdup(&diff_list->pool, path);
882 GIT_ERROR_CHECK_ALLOC(result->path);
883
884 *out = result;
885
886 done:
887 git_buf_dispose(&buf);
888 git_odb_free(odb);
889
890 return error;
891 }
892
893 static int merge_conflict_resolve_contents(
894 int *resolved,
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)
899 {
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;
905 git_odb *odb = NULL;
906 const char *name;
907 bool fallback = false;
908 int error;
909
910 assert(resolved && diff_list && conflict);
911
912 *resolved = 0;
913
914 if (!merge_conflict_can_resolve_contents(conflict))
915 return 0;
916
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;
926
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.
931 */
932 name = "text";
933 builtin.base.apply = git_merge_driver__builtin_apply;
934 builtin.favor = file_opts->favor;
935
936 driver = &builtin.base;
937 } else {
938 /* find the merge driver for this file */
939 if ((error = git_merge_driver_for_source(&name, &driver, &source)) < 0)
940 goto done;
941
942 if (driver == NULL)
943 fallback = true;
944 }
945
946 if (driver) {
947 error = merge_conflict_invoke_driver(&merge_result, name, driver,
948 diff_list, &source);
949
950 if (error == GIT_PASSTHROUGH)
951 fallback = true;
952 }
953
954 if (fallback) {
955 error = merge_conflict_invoke_driver(&merge_result, "text",
956 &git_merge_driver__text.base, diff_list, &source);
957 }
958
959 if (error < 0) {
960 if (error == GIT_EMERGECONFLICT)
961 error = 0;
962
963 goto done;
964 }
965
966 git_vector_insert(&diff_list->staged, merge_result);
967 git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
968
969 *resolved = 1;
970
971 done:
972 git_merge_file_result_free(&result);
973 git_odb_free(odb);
974
975 return error;
976 }
977
978 static int merge_conflict_resolve(
979 int *out,
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)
984 {
985 int resolved = 0;
986 int error = 0;
987
988 *out = 0;
989
990 if ((error = merge_conflict_resolve_trivial(
991 &resolved, diff_list, conflict)) < 0)
992 goto done;
993
994 if (!resolved && (error = merge_conflict_resolve_one_removed(
995 &resolved, diff_list, conflict)) < 0)
996 goto done;
997
998 if (!resolved && (error = merge_conflict_resolve_one_renamed(
999 &resolved, diff_list, conflict)) < 0)
1000 goto done;
1001
1002 if (!resolved && (error = merge_conflict_resolve_contents(
1003 &resolved, diff_list, conflict, merge_opts, file_opts)) < 0)
1004 goto done;
1005
1006 *out = resolved;
1007
1008 done:
1009 return error;
1010 }
1011
1012 /* Rename detection and coalescing */
1013
1014 struct merge_diff_similarity {
1015 unsigned char similarity;
1016 size_t other_idx;
1017 };
1018
1019 static int index_entry_similarity_calc(
1020 void **out,
1021 git_repository *repo,
1022 git_index_entry *entry,
1023 const git_merge_options *opts)
1024 {
1025 git_blob *blob;
1026 git_diff_file diff_file = {{{0}}};
1027 git_object_size_t blobsize;
1028 int error;
1029
1030 *out = NULL;
1031
1032 if ((error = git_blob_lookup(&blob, repo, &entry->id)) < 0)
1033 return error;
1034
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;
1040
1041 blobsize = git_blob_rawsize(blob);
1042
1043 /* file too big for rename processing */
1044 if (!git__is_sizet(blobsize))
1045 return 0;
1046
1047 error = opts->metric->buffer_signature(out, &diff_file,
1048 git_blob_rawcontent(blob), (size_t)blobsize,
1049 opts->metric->payload);
1050
1051 git_blob_free(blob);
1052
1053 return error;
1054 }
1055
1056 static int index_entry_similarity_inexact(
1057 git_repository *repo,
1058 git_index_entry *a,
1059 size_t a_idx,
1060 git_index_entry *b,
1061 size_t b_idx,
1062 void **cache,
1063 const git_merge_options *opts)
1064 {
1065 int score = 0;
1066 int error = 0;
1067
1068 if (!GIT_MODE_ISBLOB(a->mode) || !GIT_MODE_ISBLOB(b->mode))
1069 return 0;
1070
1071 /* update signature cache if needed */
1072 if (!cache[a_idx] && (error = index_entry_similarity_calc(&cache[a_idx], repo, a, opts)) < 0)
1073 return error;
1074 if (!cache[b_idx] && (error = index_entry_similarity_calc(&cache[b_idx], repo, b, opts)) < 0)
1075 return error;
1076
1077 /* some metrics may not wish to process this file (too big / too small) */
1078 if (!cache[a_idx] || !cache[b_idx])
1079 return 0;
1080
1081 /* compare signatures */
1082 if (opts->metric->similarity(
1083 &score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0)
1084 return -1;
1085
1086 /* clip score */
1087 if (score < 0)
1088 score = 0;
1089 else if (score > 100)
1090 score = 100;
1091
1092 return score;
1093 }
1094
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.
1097 */
1098 typedef struct {
1099 git_array_t(size_t) arr;
1100 size_t next_pos;
1101 size_t first_entry;
1102 } deletes_by_oid_queue;
1103
1104 static void deletes_by_oid_free(git_oidmap *map) {
1105 deletes_by_oid_queue *queue;
1106
1107 if (!map)
1108 return;
1109
1110 git_oidmap_foreach_value(map, queue, {
1111 git_array_clear(queue->arr);
1112 });
1113 git_oidmap_free(map);
1114 }
1115
1116 static int deletes_by_oid_enqueue(git_oidmap *map, git_pool* pool, const git_oid *id, size_t idx)
1117 {
1118 deletes_by_oid_queue *queue;
1119 size_t *array_entry;
1120
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);
1124
1125 git_array_init(queue->arr);
1126 queue->next_pos = 0;
1127 queue->first_entry = idx;
1128
1129 if (git_oidmap_set(map, id, queue) < 0)
1130 return -1;
1131 } else {
1132 array_entry = git_array_alloc(queue->arr);
1133 GIT_ERROR_CHECK_ALLOC(array_entry);
1134 *array_entry = idx;
1135 }
1136
1137 return 0;
1138 }
1139
1140 static int deletes_by_oid_dequeue(size_t *idx, git_oidmap *map, const git_oid *id)
1141 {
1142 deletes_by_oid_queue *queue;
1143 size_t *array_entry;
1144
1145 if ((queue = git_oidmap_get(map, id)) == NULL)
1146 return GIT_ENOTFOUND;
1147
1148 if (queue->next_pos == 0) {
1149 *idx = queue->first_entry;
1150 } else {
1151 array_entry = git_array_get(queue->arr, queue->next_pos - 1);
1152 if (array_entry == NULL)
1153 return GIT_ENOTFOUND;
1154
1155 *idx = *array_entry;
1156 }
1157
1158 queue->next_pos++;
1159 return 0;
1160 }
1161
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)
1166 {
1167 size_t i, j;
1168 git_merge_diff *conflict_src, *conflict_tgt;
1169 git_oidmap *ours_deletes_by_oid = NULL, *theirs_deletes_by_oid = NULL;
1170 int error = 0;
1171
1172 if (git_oidmap_new(&ours_deletes_by_oid) < 0 ||
1173 git_oidmap_new(&theirs_deletes_by_oid) < 0) {
1174 error = -1;
1175 goto done;
1176 }
1177
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))
1183 continue;
1184
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);
1187 if (error < 0)
1188 goto done;
1189 }
1190
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);
1193 if (error < 0)
1194 goto done;
1195 }
1196 }
1197
1198 git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) {
1199 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry))
1200 continue;
1201
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;
1206
1207 similarity_ours[j].similarity = 100;
1208 similarity_ours[j].other_idx = i;
1209 }
1210 }
1211
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;
1216
1217 similarity_theirs[j].similarity = 100;
1218 similarity_theirs[j].other_idx = i;
1219 }
1220 }
1221 }
1222
1223 done:
1224 deletes_by_oid_free(ours_deletes_by_oid);
1225 deletes_by_oid_free(theirs_deletes_by_oid);
1226
1227 return error;
1228 }
1229
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,
1235 void **cache,
1236 const git_merge_options *opts)
1237 {
1238 size_t i, j;
1239 git_merge_diff *conflict_src, *conflict_tgt;
1240 int similarity;
1241
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)))
1248 continue;
1249
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;
1253
1254 if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry))
1255 continue;
1256
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);
1260
1261 if (similarity == GIT_EBUFS)
1262 continue;
1263 else if (similarity < 0)
1264 return similarity;
1265
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;
1271
1272 if (similarity_ours[j].similarity > 0)
1273 similarity_ours[similarity_ours[j].other_idx].similarity = 0;
1274
1275 similarity_ours[i].similarity = similarity;
1276 similarity_ours[i].other_idx = j;
1277
1278 similarity_ours[j].similarity = similarity;
1279 similarity_ours[j].other_idx = i;
1280 }
1281 }
1282
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);
1286
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;
1292
1293 if (similarity_theirs[j].similarity > 0)
1294 similarity_theirs[similarity_theirs[j].other_idx].similarity = 0;
1295
1296 similarity_theirs[i].similarity = similarity;
1297 similarity_theirs[i].other_idx = j;
1298
1299 similarity_theirs[j].similarity = similarity;
1300 similarity_theirs[j].other_idx = i;
1301 }
1302 }
1303 }
1304 }
1305
1306 return 0;
1307 }
1308
1309 /*
1310 * Rename conflicts:
1311 *
1312 * Ancestor Ours Theirs
1313 *
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
1321 * b A B[A] (same)
1322 * 4a A B[A] B Rename/add [B~ours B~theirs]
1323 * b A B B[A] (same)
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)
1327 */
1328 static void merge_diff_mark_rename_conflict(
1329 git_merge_diff_list *diff_list,
1330 struct merge_diff_similarity *similarity_ours,
1331 bool ours_renamed,
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)
1338 {
1339 git_merge_diff *ours_source = NULL, *theirs_source = NULL;
1340
1341 if (ours_renamed)
1342 ours_source = diff_list->conflicts.contents[ours_source_idx];
1343
1344 if (theirs_renamed)
1345 theirs_source = diff_list->conflicts.contents[theirs_source_idx];
1346
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;
1352 else {
1353 ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
1354 theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
1355 }
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;
1360
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;
1364 }
1365
1366 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source->their_entry))
1367 ours_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
1368
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;
1375
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;
1379 }
1380
1381 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source->our_entry))
1382 theirs_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
1383
1384 else if (theirs_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED)
1385 theirs_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED;
1386 }
1387 }
1388
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)
1394 {
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;
1398
1399 memset(target_entry, 0x0, sizeof(git_index_entry));
1400 *target_status = GIT_DELTA_UNMODIFIED;
1401 }
1402
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)
1408 {
1409 size_t i;
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;
1413
1414 for (i = 0; i < diff_list->conflicts.length; i++) {
1415 target = diff_list->conflicts.contents[i];
1416
1417 ours_renamed = 0;
1418 theirs_renamed = 0;
1419
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;
1423
1424 ours_source = diff_list->conflicts.contents[ours_source_idx];
1425
1426 merge_diff_coalesce_rename(
1427 &ours_source->our_entry,
1428 &ours_source->our_status,
1429 &target->our_entry,
1430 &target->our_status);
1431
1432 similarity_ours[ours_source_idx].similarity = 0;
1433 similarity_ours[i].similarity = 0;
1434
1435 ours_renamed = 1;
1436 }
1437
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;
1442
1443 theirs_source = diff_list->conflicts.contents[theirs_source_idx];
1444
1445 merge_diff_coalesce_rename(
1446 &theirs_source->their_entry,
1447 &theirs_source->their_status,
1448 &target->their_entry,
1449 &target->their_status);
1450
1451 similarity_theirs[theirs_source_idx].similarity = 0;
1452 similarity_theirs[i].similarity = 0;
1453
1454 theirs_renamed = 1;
1455 }
1456
1457 merge_diff_mark_rename_conflict(diff_list,
1458 similarity_ours, ours_renamed, ours_source_idx,
1459 similarity_theirs, theirs_renamed, theirs_source_idx,
1460 target, opts);
1461 }
1462 }
1463
1464 static int merge_diff_empty(const git_vector *conflicts, size_t idx, void *p)
1465 {
1466 git_merge_diff *conflict = conflicts->contents[idx];
1467
1468 GIT_UNUSED(p);
1469
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));
1473 }
1474
1475 static void merge_diff_list_count_candidates(
1476 git_merge_diff_list *diff_list,
1477 size_t *src_count,
1478 size_t *tgt_count)
1479 {
1480 git_merge_diff *entry;
1481 size_t i;
1482
1483 *src_count = 0;
1484 *tgt_count = 0;
1485
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)))
1490 (*src_count)++;
1491 else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry))
1492 (*tgt_count)++;
1493 }
1494 }
1495
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)
1500 {
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;
1505 int error = 0;
1506
1507 assert(diff_list && opts);
1508
1509 if ((opts->flags & GIT_MERGE_FIND_RENAMES) == 0)
1510 return 0;
1511
1512 similarity_ours = git__calloc(diff_list->conflicts.length,
1513 sizeof(struct merge_diff_similarity));
1514 GIT_ERROR_CHECK_ALLOC(similarity_ours);
1515
1516 similarity_theirs = git__calloc(diff_list->conflicts.length,
1517 sizeof(struct merge_diff_similarity));
1518 GIT_ERROR_CHECK_ALLOC(similarity_theirs);
1519
1520 /* Calculate similarity between items that were deleted from the ancestor
1521 * and added in the other branch.
1522 */
1523 if ((error = merge_diff_mark_similarity_exact(diff_list, similarity_ours, similarity_theirs)) < 0)
1524 goto done;
1525
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);
1530
1531 merge_diff_list_count_candidates(diff_list, &src_count, &tgt_count);
1532
1533 if (src_count > opts->target_limit || tgt_count > opts->target_limit) {
1534 /* TODO: report! */
1535 } else {
1536 if ((error = merge_diff_mark_similarity_inexact(
1537 repo, diff_list, similarity_ours, similarity_theirs, cache, opts)) < 0)
1538 goto done;
1539 }
1540 }
1541
1542 /* For entries that are appropriately similar, merge the new name's entry
1543 * into the old name.
1544 */
1545 merge_diff_list_coalesce_renames(diff_list, similarity_ours, similarity_theirs, opts);
1546
1547 /* And remove any entries that were merged and are now empty. */
1548 git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty, NULL);
1549
1550 done:
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);
1555 }
1556
1557 git__free(cache);
1558 }
1559
1560 git__free(similarity_ours);
1561 git__free(similarity_theirs);
1562
1563 return error;
1564 }
1565
1566 /* Directory/file conflict handling */
1567
1568 GIT_INLINE(const char *) merge_diff_path(
1569 const git_merge_diff *conflict)
1570 {
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;
1577
1578 return NULL;
1579 }
1580
1581 GIT_INLINE(bool) merge_diff_any_side_added_or_modified(
1582 const git_merge_diff *conflict)
1583 {
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)
1588 return true;
1589
1590 return false;
1591 }
1592
1593 GIT_INLINE(bool) path_is_prefixed(const char *parent, const char *child)
1594 {
1595 size_t child_len = strlen(child);
1596 size_t parent_len = strlen(parent);
1597
1598 if (child_len < parent_len ||
1599 strncmp(parent, child, parent_len) != 0)
1600 return 0;
1601
1602 return (child[parent_len] == '/');
1603 }
1604
1605 GIT_INLINE(int) merge_diff_detect_df_conflict(
1606 struct merge_diff_df_data *df_data,
1607 git_merge_diff *conflict)
1608 {
1609 const char *cur_path = merge_diff_path(conflict);
1610
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;
1622
1623 df_data->prev_conflict->type = GIT_MERGE_DIFF_DIRECTORY_FILE;
1624 df_data->df_path = df_data->prev_path;
1625 }
1626
1627 df_data->prev_path = cur_path;
1628 df_data->prev_conflict = conflict;
1629
1630 return 0;
1631 }
1632
1633 /* Conflict handling */
1634
1635 GIT_INLINE(int) merge_diff_detect_type(
1636 git_merge_diff *conflict)
1637 {
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;
1653 else
1654 conflict->type = GIT_MERGE_DIFF_NONE;
1655
1656 return 0;
1657 }
1658
1659 GIT_INLINE(int) index_entry_dup_pool(
1660 git_index_entry *out,
1661 git_pool *pool,
1662 const git_index_entry *src)
1663 {
1664 if (src != NULL) {
1665 memcpy(out, src, sizeof(git_index_entry));
1666 if ((out->path = git_pool_strdup(pool, src->path)) == NULL)
1667 return -1;
1668 }
1669
1670 return 0;
1671 }
1672
1673 GIT_INLINE(int) merge_delta_type_from_index_entries(
1674 const git_index_entry *ancestor,
1675 const git_index_entry *other)
1676 {
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;
1690
1691 return GIT_DELTA_UNMODIFIED;
1692 }
1693
1694 static git_merge_diff *merge_diff_from_index_entries(
1695 git_merge_diff_list *diff_list,
1696 const git_index_entry **entries)
1697 {
1698 git_merge_diff *conflict;
1699 git_pool *pool = &diff_list->pool;
1700
1701 if ((conflict = git_pool_mallocz(pool, sizeof(git_merge_diff))) == NULL)
1702 return NULL;
1703
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)
1707 return NULL;
1708
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]);
1713
1714 return conflict;
1715 }
1716
1717 /* Merge trees */
1718
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])
1723 {
1724 git_merge_diff *conflict;
1725
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)
1730 return -1;
1731
1732 return 0;
1733 }
1734
1735 static int merge_diff_list_insert_unmodified(
1736 git_merge_diff_list *diff_list,
1737 const git_index_entry *tree_items[3])
1738 {
1739 int error = 0;
1740 git_index_entry *entry;
1741
1742 entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry));
1743 GIT_ERROR_CHECK_ALLOC(entry);
1744
1745 if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
1746 error = git_vector_insert(&diff_list->staged, entry);
1747
1748 return error;
1749 }
1750
1751 struct merge_diff_find_data {
1752 git_merge_diff_list *diff_list;
1753 struct merge_diff_df_data df_data;
1754 };
1755
1756 static int queue_difference(const git_index_entry **entries, void *data)
1757 {
1758 struct merge_diff_find_data *find_data = data;
1759 bool item_modified = false;
1760 size_t i;
1761
1762 if (!entries[0] || !entries[1] || !entries[2]) {
1763 item_modified = true;
1764 } else {
1765 for (i = 1; i < 3; i++) {
1766 if (index_entry_cmp(entries[0], entries[i]) != 0) {
1767 item_modified = true;
1768 break;
1769 }
1770 }
1771 }
1772
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);
1777 }
1778
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)
1784 {
1785 git_iterator *iterators[3] = { ancestor_iter, our_iter, their_iter };
1786 struct merge_diff_find_data find_data = { diff_list };
1787
1788 return git_iterator_walk(iterators, 3, queue_difference, &find_data);
1789 }
1790
1791 git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo)
1792 {
1793 git_merge_diff_list *diff_list = git__calloc(1, sizeof(git_merge_diff_list));
1794
1795 if (diff_list == NULL)
1796 return NULL;
1797
1798 diff_list->repo = repo;
1799
1800 git_pool_init(&diff_list->pool, 1);
1801
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);
1806 return NULL;
1807 }
1808
1809 return diff_list;
1810 }
1811
1812 void git_merge_diff_list__free(git_merge_diff_list *diff_list)
1813 {
1814 if (!diff_list)
1815 return;
1816
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);
1822 }
1823
1824 static int merge_normalize_opts(
1825 git_repository *repo,
1826 git_merge_options *opts,
1827 const git_merge_options *given)
1828 {
1829 git_config *cfg = NULL;
1830 git_config_entry *entry = NULL;
1831 int error = 0;
1832
1833 assert(repo && opts);
1834
1835 if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
1836 return error;
1837
1838 if (given != NULL) {
1839 memcpy(opts, given, sizeof(git_merge_options));
1840 } else {
1841 git_merge_options init = GIT_MERGE_OPTIONS_INIT;
1842 memcpy(opts, &init, sizeof(init));
1843 }
1844
1845 if ((opts->flags & GIT_MERGE_FIND_RENAMES) && !opts->rename_threshold)
1846 opts->rename_threshold = GIT_MERGE_DEFAULT_RENAME_THRESHOLD;
1847
1848 if (given && given->default_driver) {
1849 opts->default_driver = git__strdup(given->default_driver);
1850 GIT_ERROR_CHECK_ALLOC(opts->default_driver);
1851 } else {
1852 error = git_config_get_entry(&entry, cfg, "merge.default");
1853
1854 if (error == 0) {
1855 opts->default_driver = git__strdup(entry->value);
1856 GIT_ERROR_CHECK_ALLOC(opts->default_driver);
1857 } else if (error == GIT_ENOTFOUND) {
1858 error = 0;
1859 } else {
1860 goto done;
1861 }
1862 }
1863
1864 if (!opts->target_limit) {
1865 int limit = git_config__get_int_force(cfg, "merge.renamelimit", 0);
1866
1867 if (!limit)
1868 limit = git_config__get_int_force(cfg, "diff.renamelimit", 0);
1869
1870 opts->target_limit = (limit <= 0) ?
1871 GIT_MERGE_DEFAULT_TARGET_LIMIT : (unsigned int)limit;
1872 }
1873
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);
1878
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;
1884 }
1885
1886 done:
1887 git_config_entry_free(entry);
1888 return error;
1889 }
1890
1891
1892 static int merge_index_insert_reuc(
1893 git_index *index,
1894 size_t idx,
1895 const git_index_entry *entry)
1896 {
1897 const git_index_reuc_entry *reuc;
1898 int mode[3] = { 0, 0, 0 };
1899 git_oid const *oid[3] = { NULL, NULL, NULL };
1900 size_t i;
1901
1902 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry))
1903 return 0;
1904
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];
1909 }
1910 }
1911
1912 mode[idx] = entry->mode;
1913 oid[idx] = &entry->id;
1914
1915 return git_index_reuc_add(index, entry->path,
1916 mode[0], oid[0], mode[1], oid[1], mode[2], oid[2]);
1917 }
1918
1919 static int index_update_reuc(git_index *index, git_merge_diff_list *diff_list)
1920 {
1921 int error;
1922 size_t i;
1923 git_merge_diff *conflict;
1924
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;
1931
1932 const git_index_entry *ours =
1933 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
1934 &conflict->our_entry : NULL;
1935
1936 const git_index_entry *theirs =
1937 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
1938 &conflict->their_entry : NULL;
1939
1940 if (ancestor != NULL &&
1941 (error = merge_index_insert_reuc(index, TREE_IDX_ANCESTOR, ancestor)) < 0)
1942 return error;
1943
1944 if (ours != NULL &&
1945 (error = merge_index_insert_reuc(index, TREE_IDX_OURS, ours)) < 0)
1946 return error;
1947
1948 if (theirs != NULL &&
1949 (error = merge_index_insert_reuc(index, TREE_IDX_THEIRS, theirs)) < 0)
1950 return error;
1951 }
1952
1953 return 0;
1954 }
1955
1956 static int index_from_diff_list(git_index **out,
1957 git_merge_diff_list *diff_list, bool skip_reuc)
1958 {
1959 git_index *index;
1960 size_t i;
1961 git_merge_diff *conflict;
1962 int error = 0;
1963
1964 *out = NULL;
1965
1966 if ((error = git_index_new(&index)) < 0)
1967 return error;
1968
1969 if ((error = git_index__fill(index, &diff_list->staged)) < 0)
1970 goto on_error;
1971
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;
1976
1977 const git_index_entry *ours =
1978 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
1979 &conflict->our_entry : NULL;
1980
1981 const git_index_entry *theirs =
1982 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
1983 &conflict->their_entry : NULL;
1984
1985 if ((error = git_index_conflict_add(index, ancestor, ours, theirs)) < 0)
1986 goto on_error;
1987 }
1988
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;
1992
1993 if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry))
1994 continue;
1995
1996 ancestor_path = conflict->ancestor_entry.path;
1997
1998 our_path =
1999 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
2000 conflict->our_entry.path : NULL;
2001
2002 their_path =
2003 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
2004 conflict->their_entry.path : NULL;
2005
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)
2009 goto on_error;
2010 }
2011 }
2012
2013 if (!skip_reuc) {
2014 if ((error = index_update_reuc(index, diff_list)) < 0)
2015 goto on_error;
2016 }
2017
2018 *out = index;
2019 return 0;
2020
2021 on_error:
2022 git_index_free(index);
2023 return error;
2024 }
2025
2026 static git_iterator *iterator_given_or_empty(git_iterator **empty, git_iterator *given)
2027 {
2028 git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
2029
2030 if (given)
2031 return given;
2032
2033 opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2034
2035 if (git_iterator_for_nothing(empty, &opts) < 0)
2036 return NULL;
2037
2038 return *empty;
2039 }
2040
2041 int git_merge__iterators(
2042 git_index **out,
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)
2048 {
2049 git_iterator *empty_ancestor = NULL,
2050 *empty_ours = 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;
2056 git_vector changes;
2057 size_t i;
2058 int error = 0;
2059
2060 assert(out && repo);
2061
2062 *out = NULL;
2063
2064 GIT_ERROR_CHECK_VERSION(
2065 given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options");
2066
2067 if ((error = merge_normalize_opts(repo, &opts, given_opts)) < 0)
2068 return error;
2069
2070 file_opts.favor = opts.file_favor;
2071 file_opts.flags = opts.file_flags;
2072
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;
2080 }
2081
2082 diff_list = git_merge_diff_list__alloc(repo);
2083 GIT_ERROR_CHECK_ALLOC(diff_list);
2084
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);
2088
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)
2092 goto done;
2093
2094 memcpy(&changes, &diff_list->conflicts, sizeof(git_vector));
2095 git_vector_clear(&diff_list->conflicts);
2096
2097 git_vector_foreach(&changes, i, conflict) {
2098 int resolved = 0;
2099
2100 if ((error = merge_conflict_resolve(
2101 &resolved, diff_list, conflict, &opts, &file_opts)) < 0)
2102 goto done;
2103
2104 if (!resolved) {
2105 if ((opts.flags & GIT_MERGE_FAIL_ON_CONFLICT)) {
2106 git_error_set(GIT_ERROR_MERGE, "merge conflicts exist");
2107 error = GIT_EMERGECONFLICT;
2108 goto done;
2109 }
2110
2111 git_vector_insert(&diff_list->conflicts, conflict);
2112 }
2113 }
2114
2115 error = index_from_diff_list(out, diff_list,
2116 (opts.flags & GIT_MERGE_SKIP_REUC));
2117
2118 done:
2119 if (!given_opts || !given_opts->metric)
2120 git__free(opts.metric);
2121
2122 git__free((char *)opts.default_driver);
2123
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);
2128
2129 return error;
2130 }
2131
2132 int git_merge_trees(
2133 git_index **out,
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)
2139 {
2140 git_iterator *ancestor_iter = NULL, *our_iter = NULL, *their_iter = NULL;
2141 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
2142 int error;
2143
2144 assert(out && repo);
2145
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);
2150
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)))
2154 result = our_tree;
2155
2156 if (result) {
2157 if ((error = git_index_new(out)) == 0)
2158 error = git_index_read_tree(*out, result);
2159
2160 return error;
2161 }
2162 }
2163
2164 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2165
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)
2172 goto done;
2173
2174 error = git_merge__iterators(
2175 out, repo, ancestor_iter, our_iter, their_iter, merge_opts);
2176
2177 done:
2178 git_iterator_free(ancestor_iter);
2179 git_iterator_free(our_iter);
2180 git_iterator_free(their_iter);
2181
2182 return error;
2183 }
2184
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);
2193
2194 GIT_INLINE(int) insert_head_ids(
2195 git_array_oid_t *ids,
2196 const git_annotated_commit *annotated_commit)
2197 {
2198 git_oid *id;
2199 size_t i;
2200
2201 if (annotated_commit->type == GIT_ANNOTATED_COMMIT_REAL) {
2202 id = git_array_alloc(*ids);
2203 GIT_ERROR_CHECK_ALLOC(id);
2204
2205 git_oid_cpy(id, git_commit_id(annotated_commit->commit));
2206 } else {
2207 for (i = 0; i < annotated_commit->parents.size; i++) {
2208 id = git_array_alloc(*ids);
2209 GIT_ERROR_CHECK_ALLOC(id);
2210
2211 git_oid_cpy(id, &annotated_commit->parents.ptr[i]);
2212 }
2213 }
2214
2215 return 0;
2216 }
2217
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)
2225 {
2226 git_annotated_commit *result = NULL;
2227 git_index *index = NULL;
2228 git_merge_options virtual_opts = GIT_MERGE_OPTIONS_INIT;
2229
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.
2232 */
2233 if (opts)
2234 memcpy(&virtual_opts, opts, sizeof(git_merge_options));
2235
2236 virtual_opts.flags &= ~GIT_MERGE_FAIL_ON_CONFLICT;
2237 virtual_opts.flags |= GIT_MERGE__VIRTUAL_BASE;
2238
2239 if ((merge_annotated_commits(&index, NULL, repo, one, two,
2240 recursion_level + 1, &virtual_opts)) < 0)
2241 return -1;
2242
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;
2247
2248 insert_head_ids(&result->parents, one);
2249 insert_head_ids(&result->parents, two);
2250
2251 *out = result;
2252 return 0;
2253 }
2254
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)
2262 {
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;
2268 int error;
2269
2270 *out = NULL;
2271
2272 if (given_opts)
2273 memcpy(&opts, given_opts, sizeof(git_merge_options));
2274
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.
2280 */
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)
2285 goto done;
2286
2287 base_count = (opts.flags & GIT_MERGE_NO_RECURSIVE) ? 0 : bases.count;
2288
2289 if (base_count)
2290 git_oidarray__reverse(&bases);
2291
2292 if ((error = git_annotated_commit_lookup(&base, repo, &bases.ids[0])) < 0)
2293 goto done;
2294
2295 for (i = 1; i < base_count; i++) {
2296 recursion_level++;
2297
2298 if (opts.recursion_limit && recursion_level > opts.recursion_limit)
2299 break;
2300
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)
2305 goto done;
2306
2307 git_annotated_commit_free(base);
2308 git_annotated_commit_free(other);
2309
2310 base = new_base;
2311 new_base = NULL;
2312 other = NULL;
2313 }
2314
2315 done:
2316 if (error == 0)
2317 *out = base;
2318 else
2319 git_annotated_commit_free(base);
2320
2321 git_annotated_commit_free(other);
2322 git_annotated_commit_free(new_base);
2323 git_oidarray_free(&bases);
2324 git_array_clear(head_ids);
2325 return error;
2326 }
2327
2328 static int iterator_for_annotated_commit(
2329 git_iterator **out,
2330 git_annotated_commit *commit)
2331 {
2332 git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
2333 int error;
2334
2335 opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2336
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);
2341 } else {
2342 if (!commit->tree &&
2343 (error = git_commit_tree(&commit->tree, commit->commit)) < 0)
2344 goto done;
2345
2346 error = git_iterator_for_tree(out, commit->tree, &opts);
2347 }
2348
2349 done:
2350 return error;
2351 }
2352
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)
2361 {
2362 git_annotated_commit *base = NULL;
2363 git_iterator *base_iter = NULL, *our_iter = NULL, *their_iter = NULL;
2364 int error;
2365
2366 if ((error = compute_base(&base, repo, ours, theirs, opts,
2367 recursion_level)) < 0) {
2368
2369 if (error != GIT_ENOTFOUND)
2370 goto done;
2371
2372 git_error_clear();
2373 }
2374
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)
2380 goto done;
2381
2382 if (base_out) {
2383 *base_out = base;
2384 base = NULL;
2385 }
2386
2387 done:
2388 git_annotated_commit_free(base);
2389 git_iterator_free(base_iter);
2390 git_iterator_free(our_iter);
2391 git_iterator_free(their_iter);
2392 return error;
2393 }
2394
2395
2396 int git_merge_commits(
2397 git_index **out,
2398 git_repository *repo,
2399 const git_commit *our_commit,
2400 const git_commit *their_commit,
2401 const git_merge_options *opts)
2402 {
2403 git_annotated_commit *ours = NULL, *theirs = NULL, *base = NULL;
2404 int error = 0;
2405
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)
2408 goto done;
2409
2410 error = merge_annotated_commits(out, &base, repo, ours, theirs, 0, opts);
2411
2412 done:
2413 git_annotated_commit_free(ours);
2414 git_annotated_commit_free(theirs);
2415 git_annotated_commit_free(base);
2416 return error;
2417 }
2418
2419 /* Merge setup / cleanup */
2420
2421 static int write_merge_head(
2422 git_repository *repo,
2423 const git_annotated_commit *heads[],
2424 size_t heads_len)
2425 {
2426 git_filebuf file = GIT_FILEBUF_INIT;
2427 git_buf file_path = GIT_BUF_INIT;
2428 size_t i;
2429 int error = 0;
2430
2431 assert(repo && heads);
2432
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)
2435 goto cleanup;
2436
2437 for (i = 0; i < heads_len; i++) {
2438 if ((error = git_filebuf_printf(&file, "%s\n", heads[i]->id_str)) < 0)
2439 goto cleanup;
2440 }
2441
2442 error = git_filebuf_commit(&file);
2443
2444 cleanup:
2445 if (error < 0)
2446 git_filebuf_cleanup(&file);
2447
2448 git_buf_dispose(&file_path);
2449
2450 return error;
2451 }
2452
2453 static int write_merge_mode(git_repository *repo)
2454 {
2455 git_filebuf file = GIT_FILEBUF_INIT;
2456 git_buf file_path = GIT_BUF_INIT;
2457 int error = 0;
2458
2459 assert(repo);
2460
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)
2463 goto cleanup;
2464
2465 if ((error = git_filebuf_write(&file, "no-ff", 5)) < 0)
2466 goto cleanup;
2467
2468 error = git_filebuf_commit(&file);
2469
2470 cleanup:
2471 if (error < 0)
2472 git_filebuf_cleanup(&file);
2473
2474 git_buf_dispose(&file_path);
2475
2476 return error;
2477 }
2478
2479 struct merge_msg_entry {
2480 const git_annotated_commit *merge_head;
2481 bool written;
2482 };
2483
2484 static int msg_entry_is_branch(
2485 const struct merge_msg_entry *entry,
2486 git_vector *entries)
2487 {
2488 GIT_UNUSED(entries);
2489
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);
2494 }
2495
2496 static int msg_entry_is_tracking(
2497 const struct merge_msg_entry *entry,
2498 git_vector *entries)
2499 {
2500 GIT_UNUSED(entries);
2501
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);
2506 }
2507
2508 static int msg_entry_is_tag(
2509 const struct merge_msg_entry *entry,
2510 git_vector *entries)
2511 {
2512 GIT_UNUSED(entries);
2513
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);
2518 }
2519
2520 static int msg_entry_is_remote(
2521 const struct merge_msg_entry *entry,
2522 git_vector *entries)
2523 {
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)
2528 {
2529 struct merge_msg_entry *existing;
2530
2531 /* Match only branches from the same remote */
2532 if (entries->length == 0)
2533 return 1;
2534
2535 existing = git_vector_get(entries, 0);
2536
2537 return (git__strcmp(existing->merge_head->remote_url,
2538 entry->merge_head->remote_url) == 0);
2539 }
2540
2541 return 0;
2542 }
2543
2544 static int msg_entry_is_oid(
2545 const struct merge_msg_entry *merge_msg_entry)
2546 {
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);
2550 }
2551
2552 static int merge_msg_entry_written(
2553 const struct merge_msg_entry *merge_msg_entry)
2554 {
2555 return (merge_msg_entry->written == 1);
2556 }
2557
2558 static int merge_msg_entries(
2559 git_vector *v,
2560 const struct merge_msg_entry *entries,
2561 size_t len,
2562 int (*match)(const struct merge_msg_entry *entry, git_vector *entries))
2563 {
2564 size_t i;
2565 int matches, total = 0;
2566
2567 git_vector_clear(v);
2568
2569 for (i = 0; i < len; i++) {
2570 if ((matches = match(&entries[i], v)) < 0)
2571 return matches;
2572 else if (!matches)
2573 continue;
2574
2575 git_vector_insert(v, (struct merge_msg_entry *)&entries[i]);
2576 total++;
2577 }
2578
2579 return total;
2580 }
2581
2582 static int merge_msg_write_entries(
2583 git_filebuf *file,
2584 git_vector *entries,
2585 const char *item_name,
2586 const char *item_plural_name,
2587 size_t ref_name_skip,
2588 const char *source,
2589 char sep)
2590 {
2591 struct merge_msg_entry *entry;
2592 size_t i;
2593 int error = 0;
2594
2595 if (entries->length == 0)
2596 return 0;
2597
2598 if (sep && (error = git_filebuf_printf(file, "%c ", sep)) < 0)
2599 goto done;
2600
2601 if ((error = git_filebuf_printf(file, "%s ",
2602 (entries->length == 1) ? item_name : item_plural_name)) < 0)
2603 goto done;
2604
2605 git_vector_foreach(entries, i, entry) {
2606 if (i > 0 &&
2607 (error = git_filebuf_printf(file, "%s", (i == entries->length - 1) ? " and " : ", ")) < 0)
2608 goto done;
2609
2610 if ((error = git_filebuf_printf(file, "'%s'", entry->merge_head->ref_name + ref_name_skip)) < 0)
2611 goto done;
2612
2613 entry->written = 1;
2614 }
2615
2616 if (source)
2617 error = git_filebuf_printf(file, " of %s", source);
2618
2619 done:
2620 return error;
2621 }
2622
2623 static int merge_msg_write_branches(
2624 git_filebuf *file,
2625 git_vector *entries,
2626 char sep)
2627 {
2628 return merge_msg_write_entries(file, entries,
2629 "branch", "branches", strlen(GIT_REFS_HEADS_DIR), NULL, sep);
2630 }
2631
2632 static int merge_msg_write_tracking(
2633 git_filebuf *file,
2634 git_vector *entries,
2635 char sep)
2636 {
2637 return merge_msg_write_entries(file, entries,
2638 "remote-tracking branch", "remote-tracking branches", 0, NULL, sep);
2639 }
2640
2641 static int merge_msg_write_tags(
2642 git_filebuf *file,
2643 git_vector *entries,
2644 char sep)
2645 {
2646 return merge_msg_write_entries(file, entries,
2647 "tag", "tags", strlen(GIT_REFS_TAGS_DIR), NULL, sep);
2648 }
2649
2650 static int merge_msg_write_remotes(
2651 git_filebuf *file,
2652 git_vector *entries,
2653 char sep)
2654 {
2655 const char *source;
2656
2657 if (entries->length == 0)
2658 return 0;
2659
2660 source = ((struct merge_msg_entry *)entries->contents[0])->merge_head->remote_url;
2661
2662 return merge_msg_write_entries(file, entries,
2663 "branch", "branches", strlen(GIT_REFS_HEADS_DIR), source, sep);
2664 }
2665
2666 static int write_merge_msg(
2667 git_repository *repo,
2668 const git_annotated_commit *heads[],
2669 size_t heads_len)
2670 {
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;
2675 size_t i;
2676 char sep = 0;
2677 int error = 0;
2678
2679 assert(repo && heads);
2680
2681 entries = git__calloc(heads_len, sizeof(struct merge_msg_entry));
2682 GIT_ERROR_CHECK_ALLOC(entries);
2683
2684 if (git_vector_init(&matching, heads_len, NULL) < 0) {
2685 git__free(entries);
2686 return -1;
2687 }
2688
2689 for (i = 0; i < heads_len; i++)
2690 entries[i].merge_head = heads[i];
2691
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)
2695 goto cleanup;
2696
2697 /*
2698 * This is to emulate the format of MERGE_MSG by core git.
2699 *
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.
2705 *
2706 * Yes. Really.
2707 */
2708 for (i = 0; i < heads_len; i++) {
2709 if (!msg_entry_is_oid(&entries[i]))
2710 break;
2711
2712 if ((error = git_filebuf_printf(&file,
2713 "%scommit '%s'", (i > 0) ? "; " : "",
2714 entries[i].merge_head->id_str)) < 0)
2715 goto cleanup;
2716
2717 entries[i].written = 1;
2718 }
2719
2720 if (i)
2721 sep = ';';
2722
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)
2725 goto cleanup;
2726
2727 if (matching.length)
2728 sep =',';
2729
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)
2732 goto cleanup;
2733
2734 if (matching.length)
2735 sep =',';
2736
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)
2739 goto cleanup;
2740
2741 if (matching.length)
2742 sep =',';
2743
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)
2748 goto cleanup;
2749
2750 if (matching.length)
2751 sep =',';
2752 }
2753
2754 if (error < 0)
2755 goto cleanup;
2756
2757 for (i = 0; i < heads_len; i++) {
2758 if (merge_msg_entry_written(&entries[i]))
2759 continue;
2760
2761 if ((error = git_filebuf_printf(&file, "; commit '%s'",
2762 entries[i].merge_head->id_str)) < 0)
2763 goto cleanup;
2764 }
2765
2766 if ((error = git_filebuf_printf(&file, "\n")) < 0 ||
2767 (error = git_filebuf_commit(&file)) < 0)
2768 goto cleanup;
2769
2770 cleanup:
2771 if (error < 0)
2772 git_filebuf_cleanup(&file);
2773
2774 git_buf_dispose(&file_path);
2775
2776 git_vector_free(&matching);
2777 git__free(entries);
2778
2779 return error;
2780 }
2781
2782 int git_merge__setup(
2783 git_repository *repo,
2784 const git_annotated_commit *our_head,
2785 const git_annotated_commit *heads[],
2786 size_t heads_len)
2787 {
2788 int error = 0;
2789
2790 assert (repo && our_head && heads);
2791
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);
2796 }
2797
2798 return error;
2799 }
2800
2801 /* Merge branches */
2802
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)
2809 {
2810 git_oid *oids, ancestor_oid;
2811 size_t i, alloc_len;
2812 int error = 0;
2813
2814 assert(repo && our_head && their_heads);
2815
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);
2819
2820 git_oid_cpy(&oids[0], git_commit_id(our_head->commit));
2821
2822 for (i = 0; i < their_heads_len; i++)
2823 git_oid_cpy(&oids[i + 1], git_annotated_commit_id(their_heads[i]));
2824
2825 if ((error = git_merge_base_many(&ancestor_oid, repo, their_heads_len + 1, oids)) < 0)
2826 goto on_error;
2827
2828 error = git_annotated_commit_lookup(ancestor_head, repo, &ancestor_oid);
2829
2830 on_error:
2831 git__free(oids);
2832 return error;
2833 }
2834
2835 const char *merge_their_label(const char *branchname)
2836 {
2837 const char *slash;
2838
2839 if ((slash = strrchr(branchname, '/')) == NULL)
2840 return branchname;
2841
2842 if (*(slash+1) == '\0')
2843 return "theirs";
2844
2845 return slash+1;
2846 }
2847
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)
2857 {
2858 git_checkout_options default_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
2859 int error = 0;
2860
2861 GIT_UNUSED(repo);
2862
2863 if (given_checkout_opts != NULL)
2864 memcpy(out, given_checkout_opts, sizeof(git_checkout_options));
2865 else
2866 memcpy(out, &default_checkout_opts, sizeof(git_checkout_options));
2867
2868 out->checkout_strategy = checkout_strategy;
2869
2870 if (!out->ancestor_label) {
2871 if (ancestor && ancestor->type == GIT_ANNOTATED_COMMIT_REAL)
2872 out->ancestor_label = git_commit_summary(ancestor->commit);
2873 else if (ancestor)
2874 out->ancestor_label = "merged common ancestors";
2875 else
2876 out->ancestor_label = "empty base";
2877 }
2878
2879 if (!out->our_label) {
2880 if (our_head && our_head->ref_name)
2881 out->our_label = our_head->ref_name;
2882 else
2883 out->our_label = "ours";
2884 }
2885
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;
2891 else
2892 out->their_label = "theirs";
2893 }
2894
2895 return error;
2896 }
2897
2898 static int merge_check_index(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths)
2899 {
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;
2908 size_t i;
2909 int error = 0;
2910
2911 GIT_UNUSED(merged_paths);
2912
2913 *conflicts = 0;
2914
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
2918 * a reset merge.
2919 */
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)
2923 goto done;
2924
2925 if (staged_diff_list->deltas.length == 0)
2926 goto done;
2927
2928 git_vector_foreach(&staged_diff_list->deltas, i, delta) {
2929 if ((error = git_vector_insert(&staged_paths, (char *)delta->new_file.path)) < 0)
2930 goto done;
2931 }
2932
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;
2936
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)
2940 goto done;
2941
2942 *conflicts = index_diff_list->deltas.length;
2943
2944 done:
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);
2952
2953 return error;
2954 }
2955
2956 static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths)
2957 {
2958 git_diff *wd_diff_list = NULL;
2959 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
2960 int error = 0;
2961
2962 GIT_UNUSED(index_new);
2963
2964 *conflicts = 0;
2965
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.
2972 */
2973 if (merged_paths->length == 0)
2974 return 0;
2975
2976 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
2977
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.
2981 */
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;
2986
2987 if ((error = git_diff_index_to_workdir(&wd_diff_list, repo, NULL, &opts)) < 0)
2988 goto done;
2989
2990 *conflicts = wd_diff_list->deltas.length;
2991
2992 done:
2993 git_diff_free(wd_diff_list);
2994
2995 return error;
2996 }
2997
2998 int git_merge__check_result(git_repository *repo, git_index *index_new)
2999 {
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;
3009 int error = 0;
3010
3011 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
3012
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)
3017 goto done;
3018
3019 git_vector_foreach(&merged_list->deltas, i, delta) {
3020 if ((error = git_vector_insert(&paths, (char *)delta->new_file.path)) < 0)
3021 goto done;
3022 }
3023
3024 for (i = 0; i < git_index_entrycount(index_new); i++) {
3025 e = git_index_get_byindex(index_new, i);
3026
3027 if (git_index_entry_is_conflict(e) &&
3028 (git_vector_last(&paths) == NULL ||
3029 strcmp(git_vector_last(&paths), e->path) != 0)) {
3030
3031 if ((error = git_vector_insert(&paths, (char *)e->path)) < 0)
3032 goto done;
3033 }
3034 }
3035
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)
3039 goto done;
3040
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;
3045 }
3046
3047 done:
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);
3053
3054 return error;
3055 }
3056
3057 int git_merge__append_conflicts_to_merge_msg(
3058 git_repository *repo,
3059 git_index *index)
3060 {
3061 git_filebuf file = GIT_FILEBUF_INIT;
3062 git_buf file_path = GIT_BUF_INIT;
3063 const char *last = NULL;
3064 size_t i;
3065 int error;
3066
3067 if (!git_index_has_conflicts(index))
3068 return 0;
3069
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)
3072 goto cleanup;
3073
3074 git_filebuf_printf(&file, "\nConflicts:\n");
3075
3076 for (i = 0; i < git_index_entrycount(index); i++) {
3077 const git_index_entry *e = git_index_get_byindex(index, i);
3078
3079 if (!git_index_entry_is_conflict(e))
3080 continue;
3081
3082 if (last == NULL || strcmp(e->path, last) != 0)
3083 git_filebuf_printf(&file, "\t%s\n", e->path);
3084
3085 last = e->path;
3086 }
3087
3088 error = git_filebuf_commit(&file);
3089
3090 cleanup:
3091 if (error < 0)
3092 git_filebuf_cleanup(&file);
3093
3094 git_buf_dispose(&file_path);
3095
3096 return error;
3097 }
3098
3099 static int merge_state_cleanup(git_repository *repo)
3100 {
3101 const char *state_files[] = {
3102 GIT_MERGE_HEAD_FILE,
3103 GIT_MERGE_MODE_FILE,
3104 GIT_MERGE_MSG_FILE,
3105 };
3106
3107 return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
3108 }
3109
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)
3117 {
3118 git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
3119 int error = 0;
3120
3121 *ancestor_head_out = NULL;
3122 *our_head_out = NULL;
3123
3124 if ((error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0)
3125 goto done;
3126
3127 if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0) {
3128 if (error != GIT_ENOTFOUND)
3129 goto done;
3130
3131 git_error_clear();
3132 error = 0;
3133 }
3134
3135 *ancestor_head_out = ancestor_head;
3136 *our_head_out = our_head;
3137
3138 done:
3139 if (error < 0) {
3140 git_annotated_commit_free(ancestor_head);
3141 git_annotated_commit_free(our_head);
3142 }
3143
3144 return error;
3145 }
3146
3147 static int merge_preference(git_merge_preference_t *out, git_repository *repo)
3148 {
3149 git_config *config;
3150 const char *value;
3151 int bool_value, error = 0;
3152
3153 *out = GIT_MERGE_PREFERENCE_NONE;
3154
3155 if ((error = git_repository_config_snapshot(&config, repo)) < 0)
3156 goto done;
3157
3158 if ((error = git_config_get_string(&value, config, "merge.ff")) < 0) {
3159 if (error == GIT_ENOTFOUND) {
3160 git_error_clear();
3161 error = 0;
3162 }
3163
3164 goto done;
3165 }
3166
3167 if (git_config_parse_bool(&bool_value, value) == 0) {
3168 if (!bool_value)
3169 *out |= GIT_MERGE_PREFERENCE_NO_FASTFORWARD;
3170 } else {
3171 if (strcasecmp(value, "only") == 0)
3172 *out |= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY;
3173 }
3174
3175 done:
3176 git_config_free(config);
3177 return error;
3178 }
3179
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)
3187 {
3188 git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
3189 int error = 0;
3190 bool unborn;
3191
3192 assert(analysis_out && preference_out && repo && their_heads && their_heads_len > 0);
3193
3194 if (their_heads_len != 1) {
3195 git_error_set(GIT_ERROR_MERGE, "can only merge a single branch");
3196 error = -1;
3197 goto done;
3198 }
3199
3200 *analysis_out = GIT_MERGE_ANALYSIS_NONE;
3201
3202 if ((error = merge_preference(preference_out, repo)) < 0)
3203 goto done;
3204
3205 if ((error = git_reference__is_unborn_head(&unborn, our_ref, repo)) < 0)
3206 goto done;
3207
3208 if (unborn) {
3209 *analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_UNBORN;
3210 error = 0;
3211 goto done;
3212 }
3213
3214 if ((error = merge_heads(&ancestor_head, &our_head, repo, our_ref, their_heads, their_heads_len)) < 0)
3215 goto done;
3216
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;
3221
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;
3226
3227 /* Otherwise, just a normal merge is possible. */
3228 else
3229 *analysis_out |= GIT_MERGE_ANALYSIS_NORMAL;
3230
3231 done:
3232 git_annotated_commit_free(ancestor_head);
3233 git_annotated_commit_free(our_head);
3234 return error;
3235 }
3236
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)
3243 {
3244 git_reference *head_ref = NULL;
3245 int error = 0;
3246
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");
3249 return error;
3250 }
3251
3252 error = git_merge_analysis_for_ref(analysis_out, preference_out, repo, head_ref, their_heads, their_heads_len);
3253
3254 git_reference_free(head_ref);
3255
3256 return error;
3257 }
3258
3259 int git_merge(
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)
3265 {
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;
3272 int error = 0;
3273
3274 assert(repo && their_heads && their_heads_len > 0);
3275
3276 if (their_heads_len != 1) {
3277 git_error_set(GIT_ERROR_MERGE, "can only merge a single branch");
3278 return -1;
3279 }
3280
3281 if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
3282 goto done;
3283
3284 checkout_strategy = given_checkout_opts ?
3285 given_checkout_opts->checkout_strategy :
3286 GIT_CHECKOUT_SAFE;
3287
3288 if ((error = git_indexwriter_init_for_operation(&indexwriter, repo,
3289 &checkout_strategy)) < 0)
3290 goto done;
3291
3292 if ((error = git_repository_index(&repo_index, repo) < 0) ||
3293 (error = git_index_read(repo_index, 0) < 0))
3294 goto done;
3295
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)
3300 goto done;
3301
3302 /* TODO: octopus */
3303
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)
3308 goto done;
3309
3310 /* check out the merge results */
3311
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)
3316 goto done;
3317
3318 error = git_indexwriter_commit(&indexwriter);
3319
3320 done:
3321 if (error < 0)
3322 merge_state_cleanup(repo);
3323
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);
3330
3331 return error;
3332 }
3333
3334 int git_merge_options_init(git_merge_options *opts, unsigned int version)
3335 {
3336 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3337 opts, version, git_merge_options, GIT_MERGE_OPTIONS_INIT);
3338 return 0;
3339 }
3340
3341 int git_merge_init_options(git_merge_options *opts, unsigned int version)
3342 {
3343 return git_merge_options_init(opts, version);
3344 }
3345
3346 int git_merge_file_input_init(git_merge_file_input *input, unsigned int version)
3347 {
3348 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3349 input, version, git_merge_file_input, GIT_MERGE_FILE_INPUT_INIT);
3350 return 0;
3351 }
3352
3353 int git_merge_file_init_input(git_merge_file_input *input, unsigned int version)
3354 {
3355 return git_merge_file_input_init(input, version);
3356 }
3357
3358 int git_merge_file_options_init(
3359 git_merge_file_options *opts, unsigned int version)
3360 {
3361 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3362 opts, version, git_merge_file_options, GIT_MERGE_FILE_OPTIONS_INIT);
3363 return 0;
3364 }
3365
3366 int git_merge_file_init_options(
3367 git_merge_file_options *opts, unsigned int version)
3368 {
3369 return git_merge_file_options_init(opts, version);
3370 }