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 /*
72  * This acts as a negative cache entry marker. In case we've tried to calculate
73  * similarity metrics for a given blob already but `git_hashsig` determined
74  * that it's too small in order to have a meaningful hash signature, we will
75  * insert the address of this marker instead of `NULL`. Like this, we can
76  * easily check whether we have checked a gien entry already and skip doing the
77  * calculation again and again.
78  */
79 static int cache_invalid_marker;
80 
81 /* Merge base computation */
82 
merge_bases_many(git_commit_list ** out,git_revwalk ** walk_out,git_repository * repo,size_t length,const git_oid input_array[])83 static int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, size_t length, const git_oid input_array[])
84 {
85 	git_revwalk *walk = NULL;
86 	git_vector list;
87 	git_commit_list *result = NULL;
88 	git_commit_list_node *commit;
89 	int error = -1;
90 	unsigned int i;
91 
92 	if (length < 2) {
93 		git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor");
94 		return -1;
95 	}
96 
97 	if (git_vector_init(&list, length - 1, NULL) < 0)
98 		return -1;
99 
100 	if (git_revwalk_new(&walk, repo) < 0)
101 		goto on_error;
102 
103 	for (i = 1; i < length; i++) {
104 		commit = git_revwalk__commit_lookup(walk, &input_array[i]);
105 		if (commit == NULL)
106 			goto on_error;
107 
108 		git_vector_insert(&list, commit);
109 	}
110 
111 	commit = git_revwalk__commit_lookup(walk, &input_array[0]);
112 	if (commit == NULL)
113 		goto on_error;
114 
115 	if (git_merge__bases_many(&result, walk, commit, &list, 0) < 0)
116 		goto on_error;
117 
118 	if (!result) {
119 		git_error_set(GIT_ERROR_MERGE, "no merge base found");
120 		error = GIT_ENOTFOUND;
121 		goto on_error;
122 	}
123 
124 	*out = result;
125 	*walk_out = walk;
126 
127 	git_vector_free(&list);
128 	return 0;
129 
130 on_error:
131 	git_vector_free(&list);
132 	git_revwalk_free(walk);
133 	return error;
134 }
135 
git_merge_base_many(git_oid * out,git_repository * repo,size_t length,const git_oid input_array[])136 int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[])
137 {
138 	git_revwalk *walk;
139 	git_commit_list *result = NULL;
140 	int error = 0;
141 
142 	GIT_ASSERT_ARG(out);
143 	GIT_ASSERT_ARG(repo);
144 	GIT_ASSERT_ARG(input_array);
145 
146 	if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0)
147 		return error;
148 
149 	git_oid_cpy(out, &result->item->oid);
150 
151 	git_commit_list_free(&result);
152 	git_revwalk_free(walk);
153 
154 	return 0;
155 }
156 
git_merge_bases_many(git_oidarray * out,git_repository * repo,size_t length,const git_oid input_array[])157 int git_merge_bases_many(git_oidarray *out, git_repository *repo, size_t length, const git_oid input_array[])
158 {
159 	git_revwalk *walk;
160 	git_commit_list *list, *result = NULL;
161 	int error = 0;
162 	git_array_oid_t array;
163 
164 	GIT_ASSERT_ARG(out);
165 	GIT_ASSERT_ARG(repo);
166 	GIT_ASSERT_ARG(input_array);
167 
168 	if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0)
169 		return error;
170 
171 	git_array_init(array);
172 
173 	list = result;
174 	while (list) {
175 		git_oid *id = git_array_alloc(array);
176 		if (id == NULL) {
177 			error = -1;
178 			goto cleanup;
179 		}
180 
181 		git_oid_cpy(id, &list->item->oid);
182 		list = list->next;
183 	}
184 
185 	git_oidarray__from_array(out, &array);
186 
187 cleanup:
188 	git_commit_list_free(&result);
189 	git_revwalk_free(walk);
190 
191 	return error;
192 }
193 
git_merge_base_octopus(git_oid * out,git_repository * repo,size_t length,const git_oid input_array[])194 int git_merge_base_octopus(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[])
195 {
196 	git_oid result;
197 	unsigned int i;
198 	int error = -1;
199 
200 	GIT_ASSERT_ARG(out);
201 	GIT_ASSERT_ARG(repo);
202 	GIT_ASSERT_ARG(input_array);
203 
204 	if (length < 2) {
205 		git_error_set(GIT_ERROR_INVALID, "at least two commits are required to find an ancestor");
206 		return -1;
207 	}
208 
209 	result = input_array[0];
210 	for (i = 1; i < length; i++) {
211 		error = git_merge_base(&result, repo, &result, &input_array[i]);
212 		if (error < 0)
213 			return error;
214 	}
215 
216 	*out = result;
217 
218 	return 0;
219 }
220 
merge_bases(git_commit_list ** out,git_revwalk ** walk_out,git_repository * repo,const git_oid * one,const git_oid * two)221 static int merge_bases(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, const git_oid *one, const git_oid *two)
222 {
223 	git_revwalk *walk;
224 	git_vector list;
225 	git_commit_list *result = NULL;
226 	git_commit_list_node *commit;
227 	void *contents[1];
228 
229 	if (git_revwalk_new(&walk, repo) < 0)
230 		return -1;
231 
232 	commit = git_revwalk__commit_lookup(walk, two);
233 	if (commit == NULL)
234 		goto on_error;
235 
236 	/* This is just one value, so we can do it on the stack */
237 	memset(&list, 0x0, sizeof(git_vector));
238 	contents[0] = commit;
239 	list.length = 1;
240 	list.contents = contents;
241 
242 	commit = git_revwalk__commit_lookup(walk, one);
243 	if (commit == NULL)
244 		goto on_error;
245 
246 	if (git_merge__bases_many(&result, walk, commit, &list, 0) < 0)
247 		goto on_error;
248 
249 	if (!result) {
250 		git_revwalk_free(walk);
251 		git_error_set(GIT_ERROR_MERGE, "no merge base found");
252 		return GIT_ENOTFOUND;
253 	}
254 
255 	*out = result;
256 	*walk_out = walk;
257 
258 	return 0;
259 
260 on_error:
261 	git_revwalk_free(walk);
262 	return -1;
263 
264 }
265 
git_merge_base(git_oid * out,git_repository * repo,const git_oid * one,const git_oid * two)266 int git_merge_base(git_oid *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;
271 
272 	if ((error = merge_bases(&result, &walk, repo, one, two)) < 0)
273 		return error;
274 
275 	git_oid_cpy(out, &result->item->oid);
276 	git_commit_list_free(&result);
277 	git_revwalk_free(walk);
278 
279 	return 0;
280 }
281 
git_merge_bases(git_oidarray * out,git_repository * repo,const git_oid * one,const git_oid * two)282 int git_merge_bases(git_oidarray *out, git_repository *repo, const git_oid *one, const git_oid *two)
283 {
284 	int error;
285 	git_revwalk *walk;
286 	git_commit_list *result, *list;
287 	git_array_oid_t array;
288 
289 	git_array_init(array);
290 
291 	if ((error = merge_bases(&result, &walk, repo, one, two)) < 0)
292 		return error;
293 
294 	list = result;
295 	while (list) {
296 		git_oid *id = git_array_alloc(array);
297 		if (id == NULL)
298 			goto on_error;
299 
300 		git_oid_cpy(id, &list->item->oid);
301 		list = list->next;
302 	}
303 
304 	git_oidarray__from_array(out, &array);
305 	git_commit_list_free(&result);
306 	git_revwalk_free(walk);
307 
308 	return 0;
309 
310 on_error:
311 	git_commit_list_free(&result);
312 	git_revwalk_free(walk);
313 	return -1;
314 }
315 
interesting(git_pqueue * list)316 static int interesting(git_pqueue *list)
317 {
318 	size_t i;
319 
320 	for (i = 0; i < git_pqueue_size(list); i++) {
321 		git_commit_list_node *commit = git_pqueue_get(list, i);
322 		if ((commit->flags & STALE) == 0)
323 			return 1;
324 	}
325 
326 	return 0;
327 }
328 
clear_commit_marks_1(git_commit_list ** plist,git_commit_list_node * commit,unsigned int mark)329 static int clear_commit_marks_1(git_commit_list **plist,
330 		git_commit_list_node *commit, unsigned int mark)
331 {
332 	while (commit) {
333 		unsigned int i;
334 
335 		if (!(mark & commit->flags))
336 			return 0;
337 
338 		commit->flags &= ~mark;
339 
340 		for (i = 1; i < commit->out_degree; i++) {
341 			git_commit_list_node *p = commit->parents[i];
342 			if (git_commit_list_insert(p, plist) == NULL)
343 				return -1;
344 		}
345 
346 		commit = commit->out_degree ? commit->parents[0] : NULL;
347 	}
348 
349 	return 0;
350 }
351 
clear_commit_marks_many(git_vector * commits,unsigned int mark)352 static int clear_commit_marks_many(git_vector *commits, unsigned int mark)
353 {
354 	git_commit_list *list = NULL;
355 	git_commit_list_node *c;
356 	unsigned int i;
357 
358 	git_vector_foreach(commits, i, c) {
359 		if (git_commit_list_insert(c, &list) == NULL)
360 			return -1;
361 	}
362 
363 	while (list)
364 		if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0)
365 			return -1;
366 	return 0;
367 }
368 
clear_commit_marks(git_commit_list_node * commit,unsigned int mark)369 static int clear_commit_marks(git_commit_list_node *commit, unsigned int mark)
370 {
371 	git_commit_list *list = NULL;
372 	if (git_commit_list_insert(commit, &list) == NULL)
373 		return -1;
374 	while (list)
375 		if (clear_commit_marks_1(&list, git_commit_list_pop(&list), mark) < 0)
376 			return -1;
377 	return 0;
378 }
379 
paint_down_to_common(git_commit_list ** out,git_revwalk * walk,git_commit_list_node * one,git_vector * twos,uint32_t minimum_generation)380 static int paint_down_to_common(
381 		git_commit_list **out,
382 		git_revwalk *walk,
383 		git_commit_list_node *one,
384 		git_vector *twos,
385 		uint32_t minimum_generation)
386 {
387 	git_pqueue list;
388 	git_commit_list *result = NULL;
389 	git_commit_list_node *two;
390 
391 	int error;
392 	unsigned int i;
393 
394 	if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_generation_cmp) < 0)
395 		return -1;
396 
397 	one->flags |= PARENT1;
398 	if (git_pqueue_insert(&list, one) < 0)
399 		return -1;
400 
401 	git_vector_foreach(twos, i, two) {
402 		if (git_commit_list_parse(walk, two) < 0)
403 			return -1;
404 
405 		two->flags |= PARENT2;
406 		if (git_pqueue_insert(&list, two) < 0)
407 			return -1;
408 	}
409 
410 	/* as long as there are non-STALE commits */
411 	while (interesting(&list)) {
412 		git_commit_list_node *commit = git_pqueue_pop(&list);
413 		int flags;
414 
415 		if (commit == NULL)
416 			break;
417 
418 		flags = commit->flags & (PARENT1 | PARENT2 | STALE);
419 		if (flags == (PARENT1 | PARENT2)) {
420 			if (!(commit->flags & RESULT)) {
421 				commit->flags |= RESULT;
422 				if (git_commit_list_insert(commit, &result) == NULL)
423 					return -1;
424 			}
425 			/* we mark the parents of a merge stale */
426 			flags |= STALE;
427 		}
428 
429 		for (i = 0; i < commit->out_degree; i++) {
430 			git_commit_list_node *p = commit->parents[i];
431 			if ((p->flags & flags) == flags)
432 				continue;
433 			if (p->generation < minimum_generation)
434 				continue;
435 
436 			if ((error = git_commit_list_parse(walk, p)) < 0)
437 				return error;
438 
439 			p->flags |= flags;
440 			if (git_pqueue_insert(&list, p) < 0)
441 				return -1;
442 		}
443 	}
444 
445 	git_pqueue_free(&list);
446 	*out = result;
447 	return 0;
448 }
449 
remove_redundant(git_revwalk * walk,git_vector * commits,uint32_t minimum_generation)450 static int remove_redundant(git_revwalk *walk, git_vector *commits, uint32_t minimum_generation)
451 {
452 	git_vector work = GIT_VECTOR_INIT;
453 	unsigned char *redundant;
454 	unsigned int *filled_index;
455 	unsigned int i, j;
456 	int error = 0;
457 
458 	redundant = git__calloc(commits->length, 1);
459 	GIT_ERROR_CHECK_ALLOC(redundant);
460 	filled_index = git__calloc((commits->length - 1), sizeof(unsigned int));
461 	GIT_ERROR_CHECK_ALLOC(filled_index);
462 
463 	for (i = 0; i < commits->length; ++i) {
464 		if ((error = git_commit_list_parse(walk, commits->contents[i])) < 0)
465 			goto done;
466 	}
467 
468 	for (i = 0; i < commits->length; ++i) {
469 		git_commit_list *common = NULL;
470 		git_commit_list_node *commit = commits->contents[i];
471 
472 		if (redundant[i])
473 			continue;
474 
475 		git_vector_clear(&work);
476 
477 		for (j = 0; j < commits->length; j++) {
478 			if (i == j || redundant[j])
479 				continue;
480 
481 			filled_index[work.length] = j;
482 			if ((error = git_vector_insert(&work, commits->contents[j])) < 0)
483 				goto done;
484 		}
485 
486 		error = paint_down_to_common(&common, walk, commit, &work, minimum_generation);
487 		if (error < 0)
488 			goto done;
489 
490 		if (commit->flags & PARENT2)
491 			redundant[i] = 1;
492 
493 		for (j = 0; j < work.length; j++) {
494 			git_commit_list_node *w = work.contents[j];
495 			if (w->flags & PARENT1)
496 				redundant[filled_index[j]] = 1;
497 		}
498 
499 		git_commit_list_free(&common);
500 
501 		if ((error = clear_commit_marks(commit, ALL_FLAGS)) < 0 ||
502 		    (error = clear_commit_marks_many(&work, ALL_FLAGS)) < 0)
503 				goto done;
504 	}
505 
506 	for (i = 0; i < commits->length; ++i) {
507 		if (redundant[i])
508 			commits->contents[i] = NULL;
509 	}
510 
511 done:
512 	git__free(redundant);
513 	git__free(filled_index);
514 	git_vector_free(&work);
515 	return error;
516 }
517 
git_merge__bases_many(git_commit_list ** out,git_revwalk * walk,git_commit_list_node * one,git_vector * twos,uint32_t minimum_generation)518 int git_merge__bases_many(
519 		git_commit_list **out,
520 		git_revwalk *walk,
521 		git_commit_list_node *one,
522 		git_vector *twos,
523 		uint32_t minimum_generation)
524 {
525 	int error;
526 	unsigned int i;
527 	git_commit_list_node *two;
528 	git_commit_list *result = NULL, *tmp = NULL;
529 
530 	/* If there's only the one commit, there can be no merge bases */
531 	if (twos->length == 0) {
532 		*out = NULL;
533 		return 0;
534 	}
535 
536 	/* if the commit is repeated, we have a our merge base already */
537 	git_vector_foreach(twos, i, two) {
538 		if (one == two)
539 			return git_commit_list_insert(one, out) ? 0 : -1;
540 	}
541 
542 	if (git_commit_list_parse(walk, one) < 0)
543 		return -1;
544 
545 	error = paint_down_to_common(&result, walk, one, twos, minimum_generation);
546 	if (error < 0)
547 		return error;
548 
549 	/* filter out any stale commits in the results */
550 	tmp = result;
551 	result = NULL;
552 
553 	while (tmp) {
554 		git_commit_list_node *c = git_commit_list_pop(&tmp);
555 		if (!(c->flags & STALE))
556 			if (git_commit_list_insert_by_date(c, &result) == NULL)
557 				return -1;
558 	}
559 
560 	/*
561 	 * more than one merge base -- see if there are redundant merge
562 	 * bases and remove them
563 	 */
564 	if (result && result->next) {
565 		git_vector redundant = GIT_VECTOR_INIT;
566 
567 		while (result)
568 			git_vector_insert(&redundant, git_commit_list_pop(&result));
569 
570 		if ((error = clear_commit_marks(one, ALL_FLAGS)) < 0 ||
571 		    (error = clear_commit_marks_many(twos, ALL_FLAGS)) < 0 ||
572 		    (error = remove_redundant(walk, &redundant, minimum_generation)) < 0) {
573 			git_vector_free(&redundant);
574 			return error;
575 		}
576 
577 		git_vector_foreach(&redundant, i, two) {
578 			if (two != NULL)
579 				git_commit_list_insert_by_date(two, &result);
580 		}
581 
582 		git_vector_free(&redundant);
583 	}
584 
585 	*out = result;
586 	return 0;
587 }
588 
git_repository_mergehead_foreach(git_repository * repo,git_repository_mergehead_foreach_cb cb,void * payload)589 int git_repository_mergehead_foreach(
590 	git_repository *repo,
591 	git_repository_mergehead_foreach_cb cb,
592 	void *payload)
593 {
594 	git_buf merge_head_path = GIT_BUF_INIT, merge_head_file = GIT_BUF_INIT;
595 	char *buffer, *line;
596 	size_t line_num = 1;
597 	git_oid oid;
598 	int error = 0;
599 
600 	GIT_ASSERT_ARG(repo);
601 	GIT_ASSERT_ARG(cb);
602 
603 	if ((error = git_buf_joinpath(&merge_head_path, repo->gitdir,
604 		GIT_MERGE_HEAD_FILE)) < 0)
605 		return error;
606 
607 	if ((error = git_futils_readbuffer(&merge_head_file,
608 		git_buf_cstr(&merge_head_path))) < 0)
609 		goto cleanup;
610 
611 	buffer = merge_head_file.ptr;
612 
613 	while ((line = git__strsep(&buffer, "\n")) != NULL) {
614 		if (strlen(line) != GIT_OID_HEXSZ) {
615 			git_error_set(GIT_ERROR_INVALID, "unable to parse OID - invalid length");
616 			error = -1;
617 			goto cleanup;
618 		}
619 
620 		if ((error = git_oid_fromstr(&oid, line)) < 0)
621 			goto cleanup;
622 
623 		if ((error = cb(&oid, payload)) != 0) {
624 			git_error_set_after_callback(error);
625 			goto cleanup;
626 		}
627 
628 		++line_num;
629 	}
630 
631 	if (*buffer) {
632 		git_error_set(GIT_ERROR_MERGE, "no EOL at line %"PRIuZ, line_num);
633 		error = -1;
634 		goto cleanup;
635 	}
636 
637 cleanup:
638 	git_buf_dispose(&merge_head_path);
639 	git_buf_dispose(&merge_head_file);
640 
641 	return error;
642 }
643 
index_entry_cmp(const git_index_entry * a,const git_index_entry * b)644 GIT_INLINE(int) index_entry_cmp(const git_index_entry *a, const git_index_entry *b)
645 {
646 	int value = 0;
647 
648 	if (a->path == NULL)
649 		return (b->path == NULL) ? 0 : 1;
650 
651 	if ((value = a->mode - b->mode) == 0 &&
652 		(value = git_oid__cmp(&a->id, &b->id)) == 0)
653 		value = strcmp(a->path, b->path);
654 
655 	return value;
656 }
657 
658 /* Conflict resolution */
659 
merge_conflict_resolve_trivial(int * resolved,git_merge_diff_list * diff_list,const git_merge_diff * conflict)660 static int merge_conflict_resolve_trivial(
661 	int *resolved,
662 	git_merge_diff_list *diff_list,
663 	const git_merge_diff *conflict)
664 {
665 	int ours_empty, theirs_empty;
666 	int ours_changed, theirs_changed, ours_theirs_differ;
667 	git_index_entry const *result = NULL;
668 	int error = 0;
669 
670 	GIT_ASSERT_ARG(resolved);
671 	GIT_ASSERT_ARG(diff_list);
672 	GIT_ASSERT_ARG(conflict);
673 
674 	*resolved = 0;
675 
676 	if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
677 		conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
678 		return 0;
679 
680 	if (conflict->our_status == GIT_DELTA_RENAMED ||
681 		conflict->their_status == GIT_DELTA_RENAMED)
682 		return 0;
683 
684 	ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
685 	theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
686 
687 	ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED);
688 	theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED);
689 	ours_theirs_differ = ours_changed && theirs_changed &&
690 		index_entry_cmp(&conflict->our_entry, &conflict->their_entry);
691 
692 	/*
693 	 * Note: with only one ancestor, some cases are not distinct:
694 	 *
695 	 * 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge
696 	 * 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge
697 	 * 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge
698 	 *
699 	 * Note that the two cases that take D/F conflicts into account
700 	 * specifically do not need to be explicitly tested, as D/F conflicts
701 	 * would fail the *empty* test:
702 	 *
703 	 * 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
704 	 * 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
705 	 *
706 	 * Note that many of these cases need not be explicitly tested, as
707 	 * they simply degrade to "all different" cases (eg, 11):
708 	 *
709 	 * 4: ancest:(empty)^, head:head, remote:remote = result:no merge
710 	 * 7: ancest:ancest+, head:(empty), remote:remote = result:no merge
711 	 * 9: ancest:ancest+, head:head, remote:(empty) = result:no merge
712 	 * 11: ancest:ancest+, head:head, remote:remote = result:no merge
713 	 */
714 
715 	/* 5ALT: ancest:*, head:head, remote:head = result:head */
716 	if (ours_changed && !ours_empty && !ours_theirs_differ)
717 		result = &conflict->our_entry;
718 	/* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
719 	else if (ours_changed && ours_empty && theirs_empty)
720 		*resolved = 0;
721 	/* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
722 	else if (ours_empty && !theirs_changed)
723 		*resolved = 0;
724 	/* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
725 	else if (!ours_changed && theirs_empty)
726 		*resolved = 0;
727 	/* 13: ancest:ancest+, head:head, remote:ancest = result:head */
728 	else if (ours_changed && !theirs_changed)
729 		result = &conflict->our_entry;
730 	/* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
731 	else if (!ours_changed && theirs_changed)
732 		result = &conflict->their_entry;
733 	else
734 		*resolved = 0;
735 
736 	if (result != NULL &&
737 		GIT_MERGE_INDEX_ENTRY_EXISTS(*result) &&
738 		(error = git_vector_insert(&diff_list->staged, (void *)result)) >= 0)
739 		*resolved = 1;
740 
741 	/* Note: trivial resolution does not update the REUC. */
742 
743 	return error;
744 }
745 
merge_conflict_resolve_one_removed(int * resolved,git_merge_diff_list * diff_list,const git_merge_diff * conflict)746 static int merge_conflict_resolve_one_removed(
747 	int *resolved,
748 	git_merge_diff_list *diff_list,
749 	const git_merge_diff *conflict)
750 {
751 	int ours_empty, theirs_empty;
752 	int ours_changed, theirs_changed;
753 	int error = 0;
754 
755 	GIT_ASSERT_ARG(resolved);
756 	GIT_ASSERT_ARG(diff_list);
757 	GIT_ASSERT_ARG(conflict);
758 
759 	*resolved = 0;
760 
761 	if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
762 		conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
763 		return 0;
764 
765 	ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
766 	theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
767 
768 	ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED);
769 	theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED);
770 
771 	/* Removed in both */
772 	if (ours_changed && ours_empty && theirs_empty)
773 		*resolved = 1;
774 	/* Removed in ours */
775 	else if (ours_empty && !theirs_changed)
776 		*resolved = 1;
777 	/* Removed in theirs */
778 	else if (!ours_changed && theirs_empty)
779 		*resolved = 1;
780 
781 	if (*resolved)
782 		git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
783 
784 	return error;
785 }
786 
merge_conflict_resolve_one_renamed(int * resolved,git_merge_diff_list * diff_list,const git_merge_diff * conflict)787 static int merge_conflict_resolve_one_renamed(
788 	int *resolved,
789 	git_merge_diff_list *diff_list,
790 	const git_merge_diff *conflict)
791 {
792 	int ours_renamed, theirs_renamed;
793 	int ours_changed, theirs_changed;
794 	git_index_entry *merged;
795 	int error = 0;
796 
797 	GIT_ASSERT_ARG(resolved);
798 	GIT_ASSERT_ARG(diff_list);
799 	GIT_ASSERT_ARG(conflict);
800 
801 	*resolved = 0;
802 
803 	if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
804 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
805 		return 0;
806 
807 	ours_renamed = (conflict->our_status == GIT_DELTA_RENAMED);
808 	theirs_renamed = (conflict->their_status == GIT_DELTA_RENAMED);
809 
810 	if (!ours_renamed && !theirs_renamed)
811 		return 0;
812 
813 	/* Reject one file in a 2->1 conflict */
814 	if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 ||
815 		conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 ||
816 		conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
817 		return 0;
818 
819 	ours_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->our_entry.id) != 0) ||
820 		(conflict->ancestor_entry.mode != conflict->our_entry.mode);
821 
822 	theirs_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->their_entry.id) != 0) ||
823 		(conflict->ancestor_entry.mode != conflict->their_entry.mode);
824 
825 	/* if both are modified (and not to a common target) require a merge */
826 	if (ours_changed && theirs_changed &&
827 		git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
828 		return 0;
829 
830 	if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
831 		return -1;
832 
833 	if (ours_changed)
834 		memcpy(merged, &conflict->our_entry, sizeof(git_index_entry));
835 	else
836 		memcpy(merged, &conflict->their_entry, sizeof(git_index_entry));
837 
838 	if (ours_renamed)
839 		merged->path = conflict->our_entry.path;
840 	else
841 		merged->path = conflict->their_entry.path;
842 
843 	*resolved = 1;
844 
845 	git_vector_insert(&diff_list->staged, merged);
846 	git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
847 
848 	return error;
849 }
850 
merge_conflict_can_resolve_contents(const git_merge_diff * conflict)851 static bool merge_conflict_can_resolve_contents(
852 	const git_merge_diff *conflict)
853 {
854 	if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
855 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
856 		return false;
857 
858 	/* Reject D/F conflicts */
859 	if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE)
860 		return false;
861 
862 	/* Reject submodules. */
863 	if (S_ISGITLINK(conflict->ancestor_entry.mode) ||
864 		S_ISGITLINK(conflict->our_entry.mode) ||
865 		S_ISGITLINK(conflict->their_entry.mode))
866 		return false;
867 
868 	/* Reject link/file conflicts. */
869 	if ((S_ISLNK(conflict->ancestor_entry.mode) ^
870 			S_ISLNK(conflict->our_entry.mode)) ||
871 		(S_ISLNK(conflict->ancestor_entry.mode) ^
872 			S_ISLNK(conflict->their_entry.mode)))
873 		return false;
874 
875 	/* Reject name conflicts */
876 	if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 ||
877 		conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
878 		return false;
879 
880 	if ((conflict->our_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED &&
881 		(conflict->their_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED &&
882 		strcmp(conflict->ancestor_entry.path, conflict->their_entry.path) != 0)
883 		return false;
884 
885 	return true;
886 }
887 
merge_conflict_invoke_driver(git_index_entry ** out,const char * name,git_merge_driver * driver,git_merge_diff_list * diff_list,git_merge_driver_source * src)888 static int merge_conflict_invoke_driver(
889 	git_index_entry **out,
890 	const char *name,
891 	git_merge_driver *driver,
892 	git_merge_diff_list *diff_list,
893 	git_merge_driver_source *src)
894 {
895 	git_index_entry *result;
896 	git_buf buf = GIT_BUF_INIT;
897 	const char *path;
898 	uint32_t mode;
899 	git_odb *odb = NULL;
900 	git_oid oid;
901 	int error;
902 
903 	*out = NULL;
904 
905 	if ((error = driver->apply(driver, &path, &mode, &buf, name, src)) < 0 ||
906 		(error = git_repository_odb(&odb, src->repo)) < 0 ||
907 		(error = git_odb_write(&oid, odb, buf.ptr, buf.size, GIT_OBJECT_BLOB)) < 0)
908 		goto done;
909 
910 	result = git_pool_mallocz(&diff_list->pool, sizeof(git_index_entry));
911 	GIT_ERROR_CHECK_ALLOC(result);
912 
913 	git_oid_cpy(&result->id, &oid);
914 	result->mode = mode;
915 	result->file_size = (uint32_t)buf.size;
916 
917 	result->path = git_pool_strdup(&diff_list->pool, path);
918 	GIT_ERROR_CHECK_ALLOC(result->path);
919 
920 	*out = result;
921 
922 done:
923 	git_buf_dispose(&buf);
924 	git_odb_free(odb);
925 
926 	return error;
927 }
928 
merge_conflict_resolve_contents(int * resolved,git_merge_diff_list * diff_list,const git_merge_diff * conflict,const git_merge_options * merge_opts,const git_merge_file_options * file_opts)929 static int merge_conflict_resolve_contents(
930 	int *resolved,
931 	git_merge_diff_list *diff_list,
932 	const git_merge_diff *conflict,
933 	const git_merge_options *merge_opts,
934 	const git_merge_file_options *file_opts)
935 {
936 	git_merge_driver_source source = {0};
937 	git_merge_file_result result = {0};
938 	git_merge_driver *driver;
939 	git_merge_driver__builtin builtin = {{0}};
940 	git_index_entry *merge_result;
941 	git_odb *odb = NULL;
942 	const char *name;
943 	bool fallback = false;
944 	int error;
945 
946 	GIT_ASSERT_ARG(resolved);
947 	GIT_ASSERT_ARG(diff_list);
948 	GIT_ASSERT_ARG(conflict);
949 
950 	*resolved = 0;
951 
952 	if (!merge_conflict_can_resolve_contents(conflict))
953 		return 0;
954 
955 	source.repo = diff_list->repo;
956 	source.default_driver = merge_opts->default_driver;
957 	source.file_opts = file_opts;
958 	source.ancestor = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ?
959 		&conflict->ancestor_entry : NULL;
960 	source.ours = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
961 		&conflict->our_entry : NULL;
962 	source.theirs = GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
963 		&conflict->their_entry : NULL;
964 
965 	if (file_opts->favor != GIT_MERGE_FILE_FAVOR_NORMAL) {
966 		/* if the user requested a particular type of resolution (via the
967 		 * favor flag) then let that override the gitattributes and use
968 		 * the builtin driver.
969 		 */
970 		name = "text";
971 		builtin.base.apply = git_merge_driver__builtin_apply;
972 		builtin.favor = file_opts->favor;
973 
974 		driver = &builtin.base;
975 	} else {
976 		/* find the merge driver for this file */
977 		if ((error = git_merge_driver_for_source(&name, &driver, &source)) < 0)
978 			goto done;
979 
980 		if (driver == NULL)
981 			fallback = true;
982 	}
983 
984 	if (driver) {
985 		error = merge_conflict_invoke_driver(&merge_result, name, driver,
986 			diff_list, &source);
987 
988 		if (error == GIT_PASSTHROUGH)
989 			fallback = true;
990 	}
991 
992 	if (fallback) {
993 		error = merge_conflict_invoke_driver(&merge_result, "text",
994 			&git_merge_driver__text.base, diff_list, &source);
995 	}
996 
997 	if (error < 0) {
998 		if (error == GIT_EMERGECONFLICT)
999 			error = 0;
1000 
1001 		goto done;
1002 	}
1003 
1004 	git_vector_insert(&diff_list->staged, merge_result);
1005 	git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
1006 
1007 	*resolved = 1;
1008 
1009 done:
1010 	git_merge_file_result_free(&result);
1011 	git_odb_free(odb);
1012 
1013 	return error;
1014 }
1015 
merge_conflict_resolve(int * out,git_merge_diff_list * diff_list,const git_merge_diff * conflict,const git_merge_options * merge_opts,const git_merge_file_options * file_opts)1016 static int merge_conflict_resolve(
1017 	int *out,
1018 	git_merge_diff_list *diff_list,
1019 	const git_merge_diff *conflict,
1020 	const git_merge_options *merge_opts,
1021 	const git_merge_file_options *file_opts)
1022 {
1023 	int resolved = 0;
1024 	int error = 0;
1025 
1026 	*out = 0;
1027 
1028 	if ((error = merge_conflict_resolve_trivial(
1029 			&resolved, diff_list, conflict)) < 0)
1030 		goto done;
1031 
1032 	if (!resolved && (error = merge_conflict_resolve_one_removed(
1033 			&resolved, diff_list, conflict)) < 0)
1034 		goto done;
1035 
1036 	if (!resolved && (error = merge_conflict_resolve_one_renamed(
1037 			&resolved, diff_list, conflict)) < 0)
1038 		goto done;
1039 
1040 	if (!resolved && (error = merge_conflict_resolve_contents(
1041 			&resolved, diff_list, conflict, merge_opts, file_opts)) < 0)
1042 		goto done;
1043 
1044 	*out = resolved;
1045 
1046 done:
1047 	return error;
1048 }
1049 
1050 /* Rename detection and coalescing */
1051 
1052 struct merge_diff_similarity {
1053 	unsigned char similarity;
1054 	size_t other_idx;
1055 };
1056 
index_entry_similarity_calc(void ** out,git_repository * repo,git_index_entry * entry,const git_merge_options * opts)1057 static int index_entry_similarity_calc(
1058 	void **out,
1059 	git_repository *repo,
1060 	git_index_entry *entry,
1061 	const git_merge_options *opts)
1062 {
1063 	git_blob *blob;
1064 	git_diff_file diff_file = {{{0}}};
1065 	git_object_size_t blobsize;
1066 	int error;
1067 
1068 	if (*out || *out == &cache_invalid_marker)
1069 		return 0;
1070 
1071 	*out = NULL;
1072 
1073 	if ((error = git_blob_lookup(&blob, repo, &entry->id)) < 0)
1074 		return error;
1075 
1076 	git_oid_cpy(&diff_file.id, &entry->id);
1077 	diff_file.path = entry->path;
1078 	diff_file.size = entry->file_size;
1079 	diff_file.mode = entry->mode;
1080 	diff_file.flags = 0;
1081 
1082 	blobsize = git_blob_rawsize(blob);
1083 
1084 	/* file too big for rename processing */
1085 	if (!git__is_sizet(blobsize))
1086 		return 0;
1087 
1088 	error = opts->metric->buffer_signature(out, &diff_file,
1089 		git_blob_rawcontent(blob), (size_t)blobsize,
1090 		opts->metric->payload);
1091 	if (error == GIT_EBUFS)
1092 		*out = &cache_invalid_marker;
1093 
1094 	git_blob_free(blob);
1095 
1096 	return error;
1097 }
1098 
index_entry_similarity_inexact(git_repository * repo,git_index_entry * a,size_t a_idx,git_index_entry * b,size_t b_idx,void ** cache,const git_merge_options * opts)1099 static int index_entry_similarity_inexact(
1100 	git_repository *repo,
1101 	git_index_entry *a,
1102 	size_t a_idx,
1103 	git_index_entry *b,
1104 	size_t b_idx,
1105 	void **cache,
1106 	const git_merge_options *opts)
1107 {
1108 	int score = 0;
1109 	int error = 0;
1110 
1111 	if (!GIT_MODE_ISBLOB(a->mode) || !GIT_MODE_ISBLOB(b->mode))
1112 		return 0;
1113 
1114 	/* update signature cache if needed */
1115 	if ((error = index_entry_similarity_calc(&cache[a_idx], repo, a, opts)) < 0 ||
1116 	    (error = index_entry_similarity_calc(&cache[b_idx], repo, b, opts)) < 0)
1117 		return error;
1118 
1119 	/* some metrics may not wish to process this file (too big / too small) */
1120 	if (cache[a_idx] == &cache_invalid_marker || cache[b_idx] == &cache_invalid_marker)
1121 		return 0;
1122 
1123 	/* compare signatures */
1124 	if (opts->metric->similarity(&score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0)
1125 		return -1;
1126 
1127 	/* clip score */
1128 	if (score < 0)
1129 		score = 0;
1130 	else if (score > 100)
1131 		score = 100;
1132 
1133 	return score;
1134 }
1135 
1136 /* Tracks deletes by oid for merge_diff_mark_similarity_exact().  This is a
1137 * non-shrinking queue where next_pos is the next position to dequeue.
1138 */
1139 typedef struct {
1140 	git_array_t(size_t) arr;
1141 	size_t next_pos;
1142 	size_t first_entry;
1143 } deletes_by_oid_queue;
1144 
deletes_by_oid_free(git_oidmap * map)1145 static void deletes_by_oid_free(git_oidmap *map) {
1146 	deletes_by_oid_queue *queue;
1147 
1148 	if (!map)
1149 		return;
1150 
1151 	git_oidmap_foreach_value(map, queue, {
1152 		git_array_clear(queue->arr);
1153 	});
1154 	git_oidmap_free(map);
1155 }
1156 
deletes_by_oid_enqueue(git_oidmap * map,git_pool * pool,const git_oid * id,size_t idx)1157 static int deletes_by_oid_enqueue(git_oidmap *map, git_pool *pool, const git_oid *id, size_t idx)
1158 {
1159 	deletes_by_oid_queue *queue;
1160 	size_t *array_entry;
1161 
1162 	if ((queue = git_oidmap_get(map, id)) == NULL) {
1163 		queue = git_pool_malloc(pool, sizeof(deletes_by_oid_queue));
1164 		GIT_ERROR_CHECK_ALLOC(queue);
1165 
1166 		git_array_init(queue->arr);
1167 		queue->next_pos = 0;
1168 		queue->first_entry = idx;
1169 
1170 		if (git_oidmap_set(map, id, queue) < 0)
1171 			return -1;
1172 	} else {
1173 		array_entry = git_array_alloc(queue->arr);
1174 		GIT_ERROR_CHECK_ALLOC(array_entry);
1175 		*array_entry = idx;
1176 	}
1177 
1178 	return 0;
1179 }
1180 
deletes_by_oid_dequeue(size_t * idx,git_oidmap * map,const git_oid * id)1181 static int deletes_by_oid_dequeue(size_t *idx, git_oidmap *map, const git_oid *id)
1182 {
1183 	deletes_by_oid_queue *queue;
1184 	size_t *array_entry;
1185 
1186 	if ((queue = git_oidmap_get(map, id)) == NULL)
1187 		return GIT_ENOTFOUND;
1188 
1189 	if (queue->next_pos == 0) {
1190 		*idx = queue->first_entry;
1191 	} else {
1192 		array_entry = git_array_get(queue->arr, queue->next_pos - 1);
1193 		if (array_entry == NULL)
1194 			return GIT_ENOTFOUND;
1195 
1196 		*idx = *array_entry;
1197 	}
1198 
1199 	queue->next_pos++;
1200 	return 0;
1201 }
1202 
merge_diff_mark_similarity_exact(git_merge_diff_list * diff_list,struct merge_diff_similarity * similarity_ours,struct merge_diff_similarity * similarity_theirs)1203 static int merge_diff_mark_similarity_exact(
1204 	git_merge_diff_list *diff_list,
1205 	struct merge_diff_similarity *similarity_ours,
1206 	struct merge_diff_similarity *similarity_theirs)
1207 {
1208 	size_t i, j;
1209 	git_merge_diff *conflict_src, *conflict_tgt;
1210 	git_oidmap *ours_deletes_by_oid = NULL, *theirs_deletes_by_oid = NULL;
1211 	int error = 0;
1212 
1213 	if (git_oidmap_new(&ours_deletes_by_oid) < 0 ||
1214 	    git_oidmap_new(&theirs_deletes_by_oid) < 0) {
1215 		error = -1;
1216 		goto done;
1217 	}
1218 
1219 	/* Build a map of object ids to conflicts */
1220 	git_vector_foreach(&diff_list->conflicts, i, conflict_src) {
1221 		/* Items can be the source of a rename iff they have an item in the
1222 		* ancestor slot and lack an item in the ours or theirs slot. */
1223 		if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->ancestor_entry))
1224 			continue;
1225 
1226 		if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry)) {
1227 			error = deletes_by_oid_enqueue(ours_deletes_by_oid, &diff_list->pool, &conflict_src->ancestor_entry.id, i);
1228 			if (error < 0)
1229 				goto done;
1230 		}
1231 
1232 		if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)) {
1233 			error = deletes_by_oid_enqueue(theirs_deletes_by_oid, &diff_list->pool, &conflict_src->ancestor_entry.id, i);
1234 			if (error < 0)
1235 				goto done;
1236 		}
1237 	}
1238 
1239 	git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) {
1240 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry))
1241 			continue;
1242 
1243 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->our_entry)) {
1244 			if (deletes_by_oid_dequeue(&i, ours_deletes_by_oid, &conflict_tgt->our_entry.id) == 0) {
1245 				similarity_ours[i].similarity = 100;
1246 				similarity_ours[i].other_idx = j;
1247 
1248 				similarity_ours[j].similarity = 100;
1249 				similarity_ours[j].other_idx = i;
1250 			}
1251 		}
1252 
1253 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->their_entry)) {
1254 			if (deletes_by_oid_dequeue(&i, theirs_deletes_by_oid, &conflict_tgt->their_entry.id) == 0) {
1255 				similarity_theirs[i].similarity = 100;
1256 				similarity_theirs[i].other_idx = j;
1257 
1258 				similarity_theirs[j].similarity = 100;
1259 				similarity_theirs[j].other_idx = i;
1260 			}
1261 		}
1262 	}
1263 
1264 done:
1265 	deletes_by_oid_free(ours_deletes_by_oid);
1266 	deletes_by_oid_free(theirs_deletes_by_oid);
1267 
1268 	return error;
1269 }
1270 
merge_diff_mark_similarity_inexact(git_repository * repo,git_merge_diff_list * diff_list,struct merge_diff_similarity * similarity_ours,struct merge_diff_similarity * similarity_theirs,void ** cache,const git_merge_options * opts)1271 static int merge_diff_mark_similarity_inexact(
1272 	git_repository *repo,
1273 	git_merge_diff_list *diff_list,
1274 	struct merge_diff_similarity *similarity_ours,
1275 	struct merge_diff_similarity *similarity_theirs,
1276 	void **cache,
1277 	const git_merge_options *opts)
1278 {
1279 	size_t i, j;
1280 	git_merge_diff *conflict_src, *conflict_tgt;
1281 	int similarity;
1282 
1283 	git_vector_foreach(&diff_list->conflicts, i, conflict_src) {
1284 		/* Items can be the source of a rename iff they have an item in the
1285 		 * ancestor slot and lack an item in the ours or theirs slot. */
1286 		if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->ancestor_entry) ||
1287 			(GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry) &&
1288 			 GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)))
1289 			continue;
1290 
1291 		git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) {
1292 			size_t our_idx = diff_list->conflicts.length + j;
1293 			size_t their_idx = (diff_list->conflicts.length * 2) + j;
1294 
1295 			if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry))
1296 				continue;
1297 
1298 			if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->our_entry) &&
1299 				!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry)) {
1300 				similarity = index_entry_similarity_inexact(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->our_entry, our_idx, cache, opts);
1301 
1302 				if (similarity == GIT_EBUFS)
1303 					continue;
1304 				else if (similarity < 0)
1305 					return similarity;
1306 
1307 				if (similarity > similarity_ours[i].similarity &&
1308 					similarity > similarity_ours[j].similarity) {
1309 					/* Clear previous best similarity */
1310 					if (similarity_ours[i].similarity > 0)
1311 						similarity_ours[similarity_ours[i].other_idx].similarity = 0;
1312 
1313 					if (similarity_ours[j].similarity > 0)
1314 						similarity_ours[similarity_ours[j].other_idx].similarity = 0;
1315 
1316 					similarity_ours[i].similarity = similarity;
1317 					similarity_ours[i].other_idx = j;
1318 
1319 					similarity_ours[j].similarity = similarity;
1320 					similarity_ours[j].other_idx = i;
1321 				}
1322 			}
1323 
1324 			if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->their_entry) &&
1325 				!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)) {
1326 				similarity = index_entry_similarity_inexact(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->their_entry, their_idx, cache, opts);
1327 
1328 				if (similarity > similarity_theirs[i].similarity &&
1329 					similarity > similarity_theirs[j].similarity) {
1330 					/* Clear previous best similarity */
1331 					if (similarity_theirs[i].similarity > 0)
1332 						similarity_theirs[similarity_theirs[i].other_idx].similarity = 0;
1333 
1334 					if (similarity_theirs[j].similarity > 0)
1335 						similarity_theirs[similarity_theirs[j].other_idx].similarity = 0;
1336 
1337 					similarity_theirs[i].similarity = similarity;
1338 					similarity_theirs[i].other_idx = j;
1339 
1340 					similarity_theirs[j].similarity = similarity;
1341 					similarity_theirs[j].other_idx = i;
1342 				}
1343 			}
1344 		}
1345 	}
1346 
1347 	return 0;
1348 }
1349 
1350 /*
1351  * Rename conflicts:
1352  *
1353  *      Ancestor   Ours   Theirs
1354  *
1355  * 0a   A          A      A        No rename
1356  *  b   A          A*     A        No rename (ours was rewritten)
1357  *  c   A          A      A*       No rename (theirs rewritten)
1358  * 1a   A          A      B[A]     Rename or rename/edit
1359  *  b   A          B[A]   A        (automergeable)
1360  * 2    A          B[A]   B[A]     Both renamed (automergeable)
1361  * 3a   A          B[A]            Rename/delete
1362  *  b   A                 B[A]      (same)
1363  * 4a   A          B[A]   B        Rename/add [B~ours B~theirs]
1364  *  b   A          B      B[A]      (same)
1365  * 5    A          B[A]   C[A]     Both renamed ("1 -> 2")
1366  * 6    A          C[A]            Both renamed ("2 -> 1")
1367  *      B                 C[B]     [C~ours C~theirs]    (automergeable)
1368  */
merge_diff_mark_rename_conflict(git_merge_diff_list * diff_list,struct merge_diff_similarity * similarity_ours,bool ours_renamed,size_t ours_source_idx,struct merge_diff_similarity * similarity_theirs,bool theirs_renamed,size_t theirs_source_idx,git_merge_diff * target,const git_merge_options * opts)1369 static void merge_diff_mark_rename_conflict(
1370 	git_merge_diff_list *diff_list,
1371 	struct merge_diff_similarity *similarity_ours,
1372 	bool ours_renamed,
1373 	size_t ours_source_idx,
1374 	struct merge_diff_similarity *similarity_theirs,
1375 	bool theirs_renamed,
1376 	size_t theirs_source_idx,
1377 	git_merge_diff *target,
1378 	const git_merge_options *opts)
1379 {
1380 	git_merge_diff *ours_source = NULL, *theirs_source = NULL;
1381 
1382 	if (ours_renamed)
1383 		ours_source = diff_list->conflicts.contents[ours_source_idx];
1384 
1385 	if (theirs_renamed)
1386 		theirs_source = diff_list->conflicts.contents[theirs_source_idx];
1387 
1388 	/* Detect 2->1 conflicts */
1389 	if (ours_renamed && theirs_renamed) {
1390 		/* Both renamed to the same target name. */
1391 		if (ours_source_idx == theirs_source_idx)
1392 			ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED;
1393 		else {
1394 			ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
1395 			theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
1396 		}
1397 	} else if (ours_renamed) {
1398 		/* If our source was also renamed in theirs, this is a 1->2 */
1399 		if (similarity_theirs[ours_source_idx].similarity >= opts->rename_threshold)
1400 			ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2;
1401 
1402 		else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry)) {
1403 			ours_source->type = GIT_MERGE_DIFF_RENAMED_ADDED;
1404 			target->type = GIT_MERGE_DIFF_RENAMED_ADDED;
1405 		}
1406 
1407 		else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source->their_entry))
1408 			ours_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
1409 
1410 		else if (ours_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED)
1411 			ours_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED;
1412 	} else if (theirs_renamed) {
1413 		/* If their source was also renamed in ours, this is a 1->2 */
1414 		if (similarity_ours[theirs_source_idx].similarity >= opts->rename_threshold)
1415 			theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2;
1416 
1417 		else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry)) {
1418 			theirs_source->type = GIT_MERGE_DIFF_RENAMED_ADDED;
1419 			target->type = GIT_MERGE_DIFF_RENAMED_ADDED;
1420 		}
1421 
1422 		else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source->our_entry))
1423 			theirs_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
1424 
1425 		else if (theirs_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED)
1426 			theirs_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED;
1427 	}
1428 }
1429 
merge_diff_coalesce_rename(git_index_entry * source_entry,git_delta_t * source_status,git_index_entry * target_entry,git_delta_t * target_status)1430 GIT_INLINE(void) merge_diff_coalesce_rename(
1431 	git_index_entry *source_entry,
1432 	git_delta_t *source_status,
1433 	git_index_entry *target_entry,
1434 	git_delta_t *target_status)
1435 {
1436 	/* Coalesce the rename target into the rename source. */
1437 	memcpy(source_entry, target_entry, sizeof(git_index_entry));
1438 	*source_status = GIT_DELTA_RENAMED;
1439 
1440 	memset(target_entry, 0x0, sizeof(git_index_entry));
1441 	*target_status = GIT_DELTA_UNMODIFIED;
1442 }
1443 
merge_diff_list_coalesce_renames(git_merge_diff_list * diff_list,struct merge_diff_similarity * similarity_ours,struct merge_diff_similarity * similarity_theirs,const git_merge_options * opts)1444 static void merge_diff_list_coalesce_renames(
1445 	git_merge_diff_list *diff_list,
1446 	struct merge_diff_similarity *similarity_ours,
1447 	struct merge_diff_similarity *similarity_theirs,
1448 	const git_merge_options *opts)
1449 {
1450 	size_t i;
1451 	bool ours_renamed = 0, theirs_renamed = 0;
1452 	size_t ours_source_idx = 0, theirs_source_idx = 0;
1453 	git_merge_diff *ours_source, *theirs_source, *target;
1454 
1455 	for (i = 0; i < diff_list->conflicts.length; i++) {
1456 		target = diff_list->conflicts.contents[i];
1457 
1458 		ours_renamed = 0;
1459 		theirs_renamed = 0;
1460 
1461 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry) &&
1462 			similarity_ours[i].similarity >= opts->rename_threshold) {
1463 			ours_source_idx = similarity_ours[i].other_idx;
1464 
1465 			ours_source = diff_list->conflicts.contents[ours_source_idx];
1466 
1467 			merge_diff_coalesce_rename(
1468 				&ours_source->our_entry,
1469 				&ours_source->our_status,
1470 				&target->our_entry,
1471 				&target->our_status);
1472 
1473 			similarity_ours[ours_source_idx].similarity = 0;
1474 			similarity_ours[i].similarity = 0;
1475 
1476 			ours_renamed = 1;
1477 		}
1478 
1479 		/* insufficient to determine direction */
1480 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry) &&
1481 			similarity_theirs[i].similarity >= opts->rename_threshold) {
1482 			theirs_source_idx = similarity_theirs[i].other_idx;
1483 
1484 			theirs_source = diff_list->conflicts.contents[theirs_source_idx];
1485 
1486 			merge_diff_coalesce_rename(
1487 				&theirs_source->their_entry,
1488 				&theirs_source->their_status,
1489 				&target->their_entry,
1490 				&target->their_status);
1491 
1492 			similarity_theirs[theirs_source_idx].similarity = 0;
1493 			similarity_theirs[i].similarity = 0;
1494 
1495 			theirs_renamed = 1;
1496 		}
1497 
1498 		merge_diff_mark_rename_conflict(diff_list,
1499 			similarity_ours, ours_renamed, ours_source_idx,
1500 			similarity_theirs, theirs_renamed, theirs_source_idx,
1501 			target, opts);
1502 	}
1503 }
1504 
merge_diff_empty(const git_vector * conflicts,size_t idx,void * p)1505 static int merge_diff_empty(const git_vector *conflicts, size_t idx, void *p)
1506 {
1507 	git_merge_diff *conflict = conflicts->contents[idx];
1508 
1509 	GIT_UNUSED(p);
1510 
1511 	return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) &&
1512 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) &&
1513 		!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry));
1514 }
1515 
merge_diff_list_count_candidates(git_merge_diff_list * diff_list,size_t * src_count,size_t * tgt_count)1516 static void merge_diff_list_count_candidates(
1517 	git_merge_diff_list *diff_list,
1518 	size_t *src_count,
1519 	size_t *tgt_count)
1520 {
1521 	git_merge_diff *entry;
1522 	size_t i;
1523 
1524 	*src_count = 0;
1525 	*tgt_count = 0;
1526 
1527 	git_vector_foreach(&diff_list->conflicts, i, entry) {
1528 		if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry) &&
1529 			(!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->our_entry) ||
1530 			!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->their_entry)))
1531 			(*src_count)++;
1532 		else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry))
1533 			(*tgt_count)++;
1534 	}
1535 }
1536 
git_merge_diff_list__find_renames(git_repository * repo,git_merge_diff_list * diff_list,const git_merge_options * opts)1537 int git_merge_diff_list__find_renames(
1538 	git_repository *repo,
1539 	git_merge_diff_list *diff_list,
1540 	const git_merge_options *opts)
1541 {
1542 	struct merge_diff_similarity *similarity_ours, *similarity_theirs;
1543 	void **cache = NULL;
1544 	size_t cache_size = 0;
1545 	size_t src_count, tgt_count, i;
1546 	int error = 0;
1547 
1548 	GIT_ASSERT_ARG(diff_list);
1549 	GIT_ASSERT_ARG(opts);
1550 
1551 	if ((opts->flags & GIT_MERGE_FIND_RENAMES) == 0 ||
1552 	    !diff_list->conflicts.length)
1553 		return 0;
1554 
1555 	similarity_ours = git__calloc(diff_list->conflicts.length,
1556 		sizeof(struct merge_diff_similarity));
1557 	GIT_ERROR_CHECK_ALLOC(similarity_ours);
1558 
1559 	similarity_theirs = git__calloc(diff_list->conflicts.length,
1560 		sizeof(struct merge_diff_similarity));
1561 	GIT_ERROR_CHECK_ALLOC(similarity_theirs);
1562 
1563 	/* Calculate similarity between items that were deleted from the ancestor
1564 	 * and added in the other branch.
1565 	 */
1566 	if ((error = merge_diff_mark_similarity_exact(diff_list, similarity_ours, similarity_theirs)) < 0)
1567 		goto done;
1568 
1569 	if (opts->rename_threshold < 100 && diff_list->conflicts.length <= opts->target_limit) {
1570 		GIT_ERROR_CHECK_ALLOC_MULTIPLY(&cache_size, diff_list->conflicts.length, 3);
1571 		cache = git__calloc(cache_size, sizeof(void *));
1572 		GIT_ERROR_CHECK_ALLOC(cache);
1573 
1574 		merge_diff_list_count_candidates(diff_list, &src_count, &tgt_count);
1575 
1576 		if (src_count > opts->target_limit || tgt_count > opts->target_limit) {
1577 			/* TODO: report! */
1578 		} else {
1579 			if ((error = merge_diff_mark_similarity_inexact(
1580 				repo, diff_list, similarity_ours, similarity_theirs, cache, opts)) < 0)
1581 				goto done;
1582 		}
1583 	}
1584 
1585 	/* For entries that are appropriately similar, merge the new name's entry
1586 	 * into the old name.
1587 	 */
1588 	merge_diff_list_coalesce_renames(diff_list, similarity_ours, similarity_theirs, opts);
1589 
1590 	/* And remove any entries that were merged and are now empty. */
1591 	git_vector_remove_matching(&diff_list->conflicts, merge_diff_empty, NULL);
1592 
1593 done:
1594 	if (cache != NULL) {
1595 		for (i = 0; i < cache_size; ++i) {
1596 			if (cache[i] != NULL && cache[i] != &cache_invalid_marker)
1597 				opts->metric->free_signature(cache[i], opts->metric->payload);
1598 		}
1599 
1600 		git__free(cache);
1601 	}
1602 
1603 	git__free(similarity_ours);
1604 	git__free(similarity_theirs);
1605 
1606 	return error;
1607 }
1608 
1609 /* Directory/file conflict handling */
1610 
merge_diff_path(const git_merge_diff * conflict)1611 GIT_INLINE(const char *) merge_diff_path(
1612 	const git_merge_diff *conflict)
1613 {
1614 	if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry))
1615 		return conflict->ancestor_entry.path;
1616 	else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry))
1617 		return conflict->our_entry.path;
1618 	else if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
1619 		return conflict->their_entry.path;
1620 
1621 	return NULL;
1622 }
1623 
merge_diff_any_side_added_or_modified(const git_merge_diff * conflict)1624 GIT_INLINE(bool) merge_diff_any_side_added_or_modified(
1625 	const git_merge_diff *conflict)
1626 {
1627 	if (conflict->our_status == GIT_DELTA_ADDED ||
1628 		conflict->our_status == GIT_DELTA_MODIFIED ||
1629 		conflict->their_status == GIT_DELTA_ADDED ||
1630 		conflict->their_status == GIT_DELTA_MODIFIED)
1631 		return true;
1632 
1633 	return false;
1634 }
1635 
path_is_prefixed(const char * parent,const char * child)1636 GIT_INLINE(bool) path_is_prefixed(const char *parent, const char *child)
1637 {
1638 	size_t child_len = strlen(child);
1639 	size_t parent_len = strlen(parent);
1640 
1641 	if (child_len < parent_len ||
1642 		strncmp(parent, child, parent_len) != 0)
1643 		return 0;
1644 
1645 	return (child[parent_len] == '/');
1646 }
1647 
merge_diff_detect_df_conflict(struct merge_diff_df_data * df_data,git_merge_diff * conflict)1648 GIT_INLINE(int) merge_diff_detect_df_conflict(
1649 	struct merge_diff_df_data *df_data,
1650 	git_merge_diff *conflict)
1651 {
1652 	const char *cur_path = merge_diff_path(conflict);
1653 
1654 	/* Determine if this is a D/F conflict or the child of one */
1655 	if (df_data->df_path &&
1656 		path_is_prefixed(df_data->df_path, cur_path))
1657 		conflict->type = GIT_MERGE_DIFF_DF_CHILD;
1658 	else if(df_data->df_path)
1659 		df_data->df_path = NULL;
1660 	else if (df_data->prev_path &&
1661 		merge_diff_any_side_added_or_modified(df_data->prev_conflict) &&
1662 		merge_diff_any_side_added_or_modified(conflict) &&
1663 		path_is_prefixed(df_data->prev_path, cur_path)) {
1664 		conflict->type = GIT_MERGE_DIFF_DF_CHILD;
1665 
1666 		df_data->prev_conflict->type = GIT_MERGE_DIFF_DIRECTORY_FILE;
1667 		df_data->df_path = df_data->prev_path;
1668 	}
1669 
1670 	df_data->prev_path = cur_path;
1671 	df_data->prev_conflict = conflict;
1672 
1673 	return 0;
1674 }
1675 
1676 /* Conflict handling */
1677 
merge_diff_detect_type(git_merge_diff * conflict)1678 GIT_INLINE(int) merge_diff_detect_type(
1679 	git_merge_diff *conflict)
1680 {
1681 	if (conflict->our_status == GIT_DELTA_ADDED &&
1682 		conflict->their_status == GIT_DELTA_ADDED)
1683 		conflict->type = GIT_MERGE_DIFF_BOTH_ADDED;
1684 	else if (conflict->our_status == GIT_DELTA_MODIFIED &&
1685 			 conflict->their_status == GIT_DELTA_MODIFIED)
1686 		conflict->type = GIT_MERGE_DIFF_BOTH_MODIFIED;
1687 	else if (conflict->our_status == GIT_DELTA_DELETED &&
1688 			 conflict->their_status == GIT_DELTA_DELETED)
1689 		conflict->type = GIT_MERGE_DIFF_BOTH_DELETED;
1690 	else if (conflict->our_status == GIT_DELTA_MODIFIED &&
1691 			 conflict->their_status == GIT_DELTA_DELETED)
1692 		conflict->type = GIT_MERGE_DIFF_MODIFIED_DELETED;
1693 	else if (conflict->our_status == GIT_DELTA_DELETED &&
1694 			 conflict->their_status == GIT_DELTA_MODIFIED)
1695 		conflict->type = GIT_MERGE_DIFF_MODIFIED_DELETED;
1696 	else
1697 		conflict->type = GIT_MERGE_DIFF_NONE;
1698 
1699 	return 0;
1700 }
1701 
index_entry_dup_pool(git_index_entry * out,git_pool * pool,const git_index_entry * src)1702 GIT_INLINE(int) index_entry_dup_pool(
1703 	git_index_entry *out,
1704 	git_pool *pool,
1705 	const git_index_entry *src)
1706 {
1707 	if (src != NULL) {
1708 		memcpy(out, src, sizeof(git_index_entry));
1709 		if ((out->path = git_pool_strdup(pool, src->path)) == NULL)
1710 			return -1;
1711 	}
1712 
1713 	return 0;
1714 }
1715 
merge_delta_type_from_index_entries(const git_index_entry * ancestor,const git_index_entry * other)1716 GIT_INLINE(int) merge_delta_type_from_index_entries(
1717 	const git_index_entry *ancestor,
1718 	const git_index_entry *other)
1719 {
1720 	if (ancestor == NULL && other == NULL)
1721 		return GIT_DELTA_UNMODIFIED;
1722 	else if (ancestor == NULL && other != NULL)
1723 		return GIT_DELTA_ADDED;
1724 	else if (ancestor != NULL && other == NULL)
1725 		return GIT_DELTA_DELETED;
1726 	else if (S_ISDIR(ancestor->mode) ^ S_ISDIR(other->mode))
1727 		return GIT_DELTA_TYPECHANGE;
1728 	else if(S_ISLNK(ancestor->mode) ^ S_ISLNK(other->mode))
1729 		return GIT_DELTA_TYPECHANGE;
1730 	else if (git_oid__cmp(&ancestor->id, &other->id) ||
1731 			 ancestor->mode != other->mode)
1732 		return GIT_DELTA_MODIFIED;
1733 
1734 	return GIT_DELTA_UNMODIFIED;
1735 }
1736 
merge_diff_from_index_entries(git_merge_diff_list * diff_list,const git_index_entry ** entries)1737 static git_merge_diff *merge_diff_from_index_entries(
1738 	git_merge_diff_list *diff_list,
1739 	const git_index_entry **entries)
1740 {
1741 	git_merge_diff *conflict;
1742 	git_pool *pool = &diff_list->pool;
1743 
1744 	if ((conflict = git_pool_mallocz(pool, sizeof(git_merge_diff))) == NULL)
1745 		return NULL;
1746 
1747 	if (index_entry_dup_pool(&conflict->ancestor_entry, pool, entries[TREE_IDX_ANCESTOR]) < 0 ||
1748 		index_entry_dup_pool(&conflict->our_entry, pool, entries[TREE_IDX_OURS]) < 0 ||
1749 		index_entry_dup_pool(&conflict->their_entry, pool, entries[TREE_IDX_THEIRS]) < 0)
1750 		return NULL;
1751 
1752 	conflict->our_status = merge_delta_type_from_index_entries(
1753 		entries[TREE_IDX_ANCESTOR], entries[TREE_IDX_OURS]);
1754 	conflict->their_status = merge_delta_type_from_index_entries(
1755 		entries[TREE_IDX_ANCESTOR], entries[TREE_IDX_THEIRS]);
1756 
1757 	return conflict;
1758 }
1759 
1760 /* Merge trees */
1761 
merge_diff_list_insert_conflict(git_merge_diff_list * diff_list,struct merge_diff_df_data * merge_df_data,const git_index_entry * tree_items[3])1762 static int merge_diff_list_insert_conflict(
1763 	git_merge_diff_list *diff_list,
1764 	struct merge_diff_df_data *merge_df_data,
1765 	const git_index_entry *tree_items[3])
1766 {
1767 	git_merge_diff *conflict;
1768 
1769 	if ((conflict = merge_diff_from_index_entries(diff_list, tree_items)) == NULL ||
1770 		merge_diff_detect_type(conflict) < 0 ||
1771 		merge_diff_detect_df_conflict(merge_df_data, conflict) < 0 ||
1772 		git_vector_insert(&diff_list->conflicts, conflict) < 0)
1773 		return -1;
1774 
1775 	return 0;
1776 }
1777 
merge_diff_list_insert_unmodified(git_merge_diff_list * diff_list,const git_index_entry * tree_items[3])1778 static int merge_diff_list_insert_unmodified(
1779 	git_merge_diff_list *diff_list,
1780 	const git_index_entry *tree_items[3])
1781 {
1782 	int error = 0;
1783 	git_index_entry *entry;
1784 
1785 	entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry));
1786 	GIT_ERROR_CHECK_ALLOC(entry);
1787 
1788 	if ((error = index_entry_dup_pool(entry, &diff_list->pool, tree_items[0])) >= 0)
1789 		error = git_vector_insert(&diff_list->staged, entry);
1790 
1791 	return error;
1792 }
1793 
1794 struct merge_diff_find_data {
1795 	git_merge_diff_list *diff_list;
1796 	struct merge_diff_df_data df_data;
1797 };
1798 
queue_difference(const git_index_entry ** entries,void * data)1799 static int queue_difference(const git_index_entry **entries, void *data)
1800 {
1801 	struct merge_diff_find_data *find_data = data;
1802 	bool item_modified = false;
1803 	size_t i;
1804 
1805 	if (!entries[0] || !entries[1] || !entries[2]) {
1806 		item_modified = true;
1807 	} else {
1808 		for (i = 1; i < 3; i++) {
1809 			if (index_entry_cmp(entries[0], entries[i]) != 0) {
1810 				item_modified = true;
1811 				break;
1812 			}
1813 		}
1814 	}
1815 
1816 	return item_modified ?
1817 		merge_diff_list_insert_conflict(
1818 			find_data->diff_list, &find_data->df_data, entries) :
1819 		merge_diff_list_insert_unmodified(find_data->diff_list, entries);
1820 }
1821 
git_merge_diff_list__find_differences(git_merge_diff_list * diff_list,git_iterator * ancestor_iter,git_iterator * our_iter,git_iterator * their_iter)1822 int git_merge_diff_list__find_differences(
1823 	git_merge_diff_list *diff_list,
1824 	git_iterator *ancestor_iter,
1825 	git_iterator *our_iter,
1826 	git_iterator *their_iter)
1827 {
1828 	git_iterator *iterators[3] = { ancestor_iter, our_iter, their_iter };
1829 	struct merge_diff_find_data find_data = { diff_list };
1830 
1831 	return git_iterator_walk(iterators, 3, queue_difference, &find_data);
1832 }
1833 
git_merge_diff_list__alloc(git_repository * repo)1834 git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo)
1835 {
1836 	git_merge_diff_list *diff_list = git__calloc(1, sizeof(git_merge_diff_list));
1837 
1838 	if (diff_list == NULL)
1839 		return NULL;
1840 
1841 	diff_list->repo = repo;
1842 
1843 
1844 	if (git_pool_init(&diff_list->pool, 1) < 0 ||
1845 	    git_vector_init(&diff_list->staged, 0, NULL) < 0 ||
1846 	    git_vector_init(&diff_list->conflicts, 0, NULL) < 0 ||
1847 	    git_vector_init(&diff_list->resolved, 0, NULL) < 0) {
1848 	    git_merge_diff_list__free(diff_list);
1849 		return NULL;
1850 	}
1851 
1852 	return diff_list;
1853 }
1854 
git_merge_diff_list__free(git_merge_diff_list * diff_list)1855 void git_merge_diff_list__free(git_merge_diff_list *diff_list)
1856 {
1857 	if (!diff_list)
1858 		return;
1859 
1860 	git_vector_free(&diff_list->staged);
1861 	git_vector_free(&diff_list->conflicts);
1862 	git_vector_free(&diff_list->resolved);
1863 	git_pool_clear(&diff_list->pool);
1864 	git__free(diff_list);
1865 }
1866 
merge_normalize_opts(git_repository * repo,git_merge_options * opts,const git_merge_options * given)1867 static int merge_normalize_opts(
1868 	git_repository *repo,
1869 	git_merge_options *opts,
1870 	const git_merge_options *given)
1871 {
1872 	git_config *cfg = NULL;
1873 	git_config_entry *entry = NULL;
1874 	int error = 0;
1875 
1876 	GIT_ASSERT_ARG(repo);
1877 	GIT_ASSERT_ARG(opts);
1878 
1879 	if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
1880 		return error;
1881 
1882 	if (given != NULL) {
1883 		memcpy(opts, given, sizeof(git_merge_options));
1884 	} else {
1885 		git_merge_options init = GIT_MERGE_OPTIONS_INIT;
1886 		memcpy(opts, &init, sizeof(init));
1887 	}
1888 
1889 	if ((opts->flags & GIT_MERGE_FIND_RENAMES) && !opts->rename_threshold)
1890 		opts->rename_threshold = GIT_MERGE_DEFAULT_RENAME_THRESHOLD;
1891 
1892 	if (given && given->default_driver) {
1893 		opts->default_driver = git__strdup(given->default_driver);
1894 		GIT_ERROR_CHECK_ALLOC(opts->default_driver);
1895 	} else {
1896 		error = git_config_get_entry(&entry, cfg, "merge.default");
1897 
1898 		if (error == 0) {
1899 			opts->default_driver = git__strdup(entry->value);
1900 			GIT_ERROR_CHECK_ALLOC(opts->default_driver);
1901 		} else if (error == GIT_ENOTFOUND) {
1902 			error = 0;
1903 		} else {
1904 			goto done;
1905 		}
1906 	}
1907 
1908 	if (!opts->target_limit) {
1909 		int limit = git_config__get_int_force(cfg, "merge.renamelimit", 0);
1910 
1911 		if (!limit)
1912 			limit = git_config__get_int_force(cfg, "diff.renamelimit", 0);
1913 
1914 		opts->target_limit = (limit <= 0) ?
1915 			GIT_MERGE_DEFAULT_TARGET_LIMIT : (unsigned int)limit;
1916 	}
1917 
1918 	/* assign the internal metric with whitespace flag as payload */
1919 	if (!opts->metric) {
1920 		opts->metric = git__malloc(sizeof(git_diff_similarity_metric));
1921 		GIT_ERROR_CHECK_ALLOC(opts->metric);
1922 
1923 		opts->metric->file_signature = git_diff_find_similar__hashsig_for_file;
1924 		opts->metric->buffer_signature = git_diff_find_similar__hashsig_for_buf;
1925 		opts->metric->free_signature = git_diff_find_similar__hashsig_free;
1926 		opts->metric->similarity = git_diff_find_similar__calc_similarity;
1927 		opts->metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE;
1928 	}
1929 
1930 done:
1931 	git_config_entry_free(entry);
1932 	return error;
1933 }
1934 
1935 
merge_index_insert_reuc(git_index * index,size_t idx,const git_index_entry * entry)1936 static int merge_index_insert_reuc(
1937 	git_index *index,
1938 	size_t idx,
1939 	const git_index_entry *entry)
1940 {
1941 	const git_index_reuc_entry *reuc;
1942 	int mode[3] = { 0, 0, 0 };
1943 	git_oid const *oid[3] = { NULL, NULL, NULL };
1944 	size_t i;
1945 
1946 	if (!GIT_MERGE_INDEX_ENTRY_EXISTS(*entry))
1947 		return 0;
1948 
1949 	if ((reuc = git_index_reuc_get_bypath(index, entry->path)) != NULL) {
1950 		for (i = 0; i < 3; i++) {
1951 			mode[i] = reuc->mode[i];
1952 			oid[i] = &reuc->oid[i];
1953 		}
1954 	}
1955 
1956 	mode[idx] = entry->mode;
1957 	oid[idx] = &entry->id;
1958 
1959 	return git_index_reuc_add(index, entry->path,
1960 		mode[0], oid[0], mode[1], oid[1], mode[2], oid[2]);
1961 }
1962 
index_update_reuc(git_index * index,git_merge_diff_list * diff_list)1963 static int index_update_reuc(git_index *index, git_merge_diff_list *diff_list)
1964 {
1965 	int error;
1966 	size_t i;
1967 	git_merge_diff *conflict;
1968 
1969 	/* Add each entry in the resolved conflict to the REUC independently, since
1970 	 * the paths may differ due to renames. */
1971 	git_vector_foreach(&diff_list->resolved, i, conflict) {
1972 		const git_index_entry *ancestor =
1973 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ?
1974 			&conflict->ancestor_entry : NULL;
1975 
1976 		const git_index_entry *ours =
1977 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
1978 			&conflict->our_entry : NULL;
1979 
1980 		const git_index_entry *theirs =
1981 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
1982 			&conflict->their_entry : NULL;
1983 
1984 		if (ancestor != NULL &&
1985 			(error = merge_index_insert_reuc(index, TREE_IDX_ANCESTOR, ancestor)) < 0)
1986 			return error;
1987 
1988 		if (ours != NULL &&
1989 			(error = merge_index_insert_reuc(index, TREE_IDX_OURS, ours)) < 0)
1990 			return error;
1991 
1992 		if (theirs != NULL &&
1993 			(error = merge_index_insert_reuc(index, TREE_IDX_THEIRS, theirs)) < 0)
1994 			return error;
1995 	}
1996 
1997 	return 0;
1998 }
1999 
index_from_diff_list(git_index ** out,git_merge_diff_list * diff_list,bool skip_reuc)2000 static int index_from_diff_list(git_index **out,
2001 	git_merge_diff_list *diff_list, bool skip_reuc)
2002 {
2003 	git_index *index;
2004 	size_t i;
2005 	git_merge_diff *conflict;
2006 	int error = 0;
2007 
2008 	*out = NULL;
2009 
2010 	if ((error = git_index_new(&index)) < 0)
2011 		return error;
2012 
2013 	if ((error = git_index__fill(index, &diff_list->staged)) < 0)
2014 		goto on_error;
2015 
2016 	git_vector_foreach(&diff_list->conflicts, i, conflict) {
2017 		const git_index_entry *ancestor =
2018 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) ?
2019 			&conflict->ancestor_entry : NULL;
2020 
2021 		const git_index_entry *ours =
2022 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
2023 			&conflict->our_entry : NULL;
2024 
2025 		const git_index_entry *theirs =
2026 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
2027 			&conflict->their_entry : NULL;
2028 
2029 		if ((error = git_index_conflict_add(index, ancestor, ours, theirs)) < 0)
2030 			goto on_error;
2031 	}
2032 
2033 	/* Add each rename entry to the rename portion of the index. */
2034 	git_vector_foreach(&diff_list->conflicts, i, conflict) {
2035 		const char *ancestor_path, *our_path, *their_path;
2036 
2037 		if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry))
2038 			continue;
2039 
2040 		ancestor_path = conflict->ancestor_entry.path;
2041 
2042 		our_path =
2043 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ?
2044 			conflict->our_entry.path : NULL;
2045 
2046 		their_path =
2047 			GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry) ?
2048 			conflict->their_entry.path : NULL;
2049 
2050 		if ((our_path && strcmp(ancestor_path, our_path) != 0) ||
2051 			(their_path && strcmp(ancestor_path, their_path) != 0)) {
2052 			if ((error = git_index_name_add(index, ancestor_path, our_path, their_path)) < 0)
2053 				goto on_error;
2054 		}
2055 	}
2056 
2057 	if (!skip_reuc) {
2058 		if ((error = index_update_reuc(index, diff_list)) < 0)
2059 			goto on_error;
2060 	}
2061 
2062 	*out = index;
2063 	return 0;
2064 
2065 on_error:
2066 	git_index_free(index);
2067 	return error;
2068 }
2069 
iterator_given_or_empty(git_iterator ** empty,git_iterator * given)2070 static git_iterator *iterator_given_or_empty(git_iterator **empty, git_iterator *given)
2071 {
2072 	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
2073 
2074 	if (given)
2075 		return given;
2076 
2077 	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2078 
2079 	if (git_iterator_for_nothing(empty, &opts) < 0)
2080 		return NULL;
2081 
2082 	return *empty;
2083 }
2084 
git_merge__iterators(git_index ** out,git_repository * repo,git_iterator * ancestor_iter,git_iterator * our_iter,git_iterator * theirs_iter,const git_merge_options * given_opts)2085 int git_merge__iterators(
2086 	git_index **out,
2087 	git_repository *repo,
2088 	git_iterator *ancestor_iter,
2089 	git_iterator *our_iter,
2090 	git_iterator *theirs_iter,
2091 	const git_merge_options *given_opts)
2092 {
2093 	git_iterator *empty_ancestor = NULL,
2094 		*empty_ours = NULL,
2095 		*empty_theirs = NULL;
2096 	git_merge_diff_list *diff_list;
2097 	git_merge_options opts;
2098 	git_merge_file_options file_opts = GIT_MERGE_FILE_OPTIONS_INIT;
2099 	git_merge_diff *conflict;
2100 	git_vector changes;
2101 	size_t i;
2102 	int error = 0;
2103 
2104 	GIT_ASSERT_ARG(out);
2105 	GIT_ASSERT_ARG(repo);
2106 
2107 	*out = NULL;
2108 
2109 	GIT_ERROR_CHECK_VERSION(
2110 		given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options");
2111 
2112 	if ((error = merge_normalize_opts(repo, &opts, given_opts)) < 0)
2113 		return error;
2114 
2115 	file_opts.favor = opts.file_favor;
2116 	file_opts.flags = opts.file_flags;
2117 
2118 	/* use the git-inspired labels when virtual base building */
2119 	if (opts.flags & GIT_MERGE__VIRTUAL_BASE) {
2120 		file_opts.ancestor_label = "merged common ancestors";
2121 		file_opts.our_label = "Temporary merge branch 1";
2122 		file_opts.their_label = "Temporary merge branch 2";
2123 		file_opts.flags |= GIT_MERGE_FILE_FAVOR__CONFLICTED;
2124 		file_opts.marker_size = GIT_MERGE_CONFLICT_MARKER_SIZE + 2;
2125 	}
2126 
2127 	diff_list = git_merge_diff_list__alloc(repo);
2128 	GIT_ERROR_CHECK_ALLOC(diff_list);
2129 
2130 	ancestor_iter = iterator_given_or_empty(&empty_ancestor, ancestor_iter);
2131 	our_iter = iterator_given_or_empty(&empty_ours, our_iter);
2132 	theirs_iter = iterator_given_or_empty(&empty_theirs, theirs_iter);
2133 
2134 	if ((error = git_merge_diff_list__find_differences(
2135 			diff_list, ancestor_iter, our_iter, theirs_iter)) < 0 ||
2136 		(error = git_merge_diff_list__find_renames(repo, diff_list, &opts)) < 0)
2137 		goto done;
2138 
2139 	memcpy(&changes, &diff_list->conflicts, sizeof(git_vector));
2140 	git_vector_clear(&diff_list->conflicts);
2141 
2142 	git_vector_foreach(&changes, i, conflict) {
2143 		int resolved = 0;
2144 
2145 		if ((error = merge_conflict_resolve(
2146 			&resolved, diff_list, conflict, &opts, &file_opts)) < 0)
2147 			goto done;
2148 
2149 		if (!resolved) {
2150 			if ((opts.flags & GIT_MERGE_FAIL_ON_CONFLICT)) {
2151 				git_error_set(GIT_ERROR_MERGE, "merge conflicts exist");
2152 				error = GIT_EMERGECONFLICT;
2153 				goto done;
2154 			}
2155 
2156 			git_vector_insert(&diff_list->conflicts, conflict);
2157 		}
2158 	}
2159 
2160 	error = index_from_diff_list(out, diff_list,
2161 		(opts.flags & GIT_MERGE_SKIP_REUC));
2162 
2163 done:
2164 	if (!given_opts || !given_opts->metric)
2165 		git__free(opts.metric);
2166 
2167 	git__free((char *)opts.default_driver);
2168 
2169 	git_merge_diff_list__free(diff_list);
2170 	git_iterator_free(empty_ancestor);
2171 	git_iterator_free(empty_ours);
2172 	git_iterator_free(empty_theirs);
2173 
2174 	return error;
2175 }
2176 
git_merge_trees(git_index ** out,git_repository * repo,const git_tree * ancestor_tree,const git_tree * our_tree,const git_tree * their_tree,const git_merge_options * merge_opts)2177 int git_merge_trees(
2178 	git_index **out,
2179 	git_repository *repo,
2180 	const git_tree *ancestor_tree,
2181 	const git_tree *our_tree,
2182 	const git_tree *their_tree,
2183 	const git_merge_options *merge_opts)
2184 {
2185 	git_iterator *ancestor_iter = NULL, *our_iter = NULL, *their_iter = NULL;
2186 	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
2187 	int error;
2188 
2189 	GIT_ASSERT_ARG(out);
2190 	GIT_ASSERT_ARG(repo);
2191 
2192 	/* if one side is treesame to the ancestor, take the other side */
2193 	if (ancestor_tree && merge_opts && (merge_opts->flags & GIT_MERGE_SKIP_REUC)) {
2194 		const git_tree *result = NULL;
2195 		const git_oid *ancestor_tree_id = git_tree_id(ancestor_tree);
2196 
2197 		if (our_tree && !git_oid_cmp(ancestor_tree_id, git_tree_id(our_tree)))
2198 			result = their_tree;
2199 		else if (their_tree && !git_oid_cmp(ancestor_tree_id, git_tree_id(their_tree)))
2200 			result = our_tree;
2201 
2202 		if (result) {
2203 			if ((error = git_index_new(out)) == 0)
2204     			error = git_index_read_tree(*out, result);
2205 
2206 			return error;
2207 		}
2208 	}
2209 
2210 	iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2211 
2212 	if ((error = git_iterator_for_tree(
2213 			&ancestor_iter, (git_tree *)ancestor_tree, &iter_opts)) < 0 ||
2214 		(error = git_iterator_for_tree(
2215 			&our_iter, (git_tree *)our_tree, &iter_opts)) < 0 ||
2216 		(error = git_iterator_for_tree(
2217 			&their_iter, (git_tree *)their_tree, &iter_opts)) < 0)
2218 		goto done;
2219 
2220 	error = git_merge__iterators(
2221 		out, repo, ancestor_iter, our_iter, their_iter, merge_opts);
2222 
2223 done:
2224 	git_iterator_free(ancestor_iter);
2225 	git_iterator_free(our_iter);
2226 	git_iterator_free(their_iter);
2227 
2228 	return error;
2229 }
2230 
2231 static int merge_annotated_commits(
2232 	git_index **index_out,
2233 	git_annotated_commit **base_out,
2234 	git_repository *repo,
2235 	git_annotated_commit *our_commit,
2236 	git_annotated_commit *their_commit,
2237 	size_t recursion_level,
2238 	const git_merge_options *opts);
2239 
insert_head_ids(git_array_oid_t * ids,const git_annotated_commit * annotated_commit)2240 GIT_INLINE(int) insert_head_ids(
2241 	git_array_oid_t *ids,
2242 	const git_annotated_commit *annotated_commit)
2243 {
2244 	git_oid *id;
2245 	size_t i;
2246 
2247 	if (annotated_commit->type == GIT_ANNOTATED_COMMIT_REAL) {
2248 		id = git_array_alloc(*ids);
2249 		GIT_ERROR_CHECK_ALLOC(id);
2250 
2251 		git_oid_cpy(id, git_commit_id(annotated_commit->commit));
2252 	} else {
2253 		for (i = 0; i < annotated_commit->parents.size; i++) {
2254 			id = git_array_alloc(*ids);
2255 			GIT_ERROR_CHECK_ALLOC(id);
2256 
2257 			git_oid_cpy(id, &annotated_commit->parents.ptr[i]);
2258 		}
2259 	}
2260 
2261 	return 0;
2262 }
2263 
create_virtual_base(git_annotated_commit ** out,git_repository * repo,git_annotated_commit * one,git_annotated_commit * two,const git_merge_options * opts,size_t recursion_level)2264 static int create_virtual_base(
2265 	git_annotated_commit **out,
2266 	git_repository *repo,
2267 	git_annotated_commit *one,
2268 	git_annotated_commit *two,
2269 	const git_merge_options *opts,
2270 	size_t recursion_level)
2271 {
2272 	git_annotated_commit *result = NULL;
2273 	git_index *index = NULL;
2274 	git_merge_options virtual_opts = GIT_MERGE_OPTIONS_INIT;
2275 
2276 	/* Conflicts in the merge base creation do not propagate to conflicts
2277 	 * in the result; the conflicted base will act as the common ancestor.
2278 	 */
2279 	if (opts)
2280 		memcpy(&virtual_opts, opts, sizeof(git_merge_options));
2281 
2282 	virtual_opts.flags &= ~GIT_MERGE_FAIL_ON_CONFLICT;
2283 	virtual_opts.flags |= GIT_MERGE__VIRTUAL_BASE;
2284 
2285 	if ((merge_annotated_commits(&index, NULL, repo, one, two,
2286 			recursion_level + 1, &virtual_opts)) < 0)
2287 		return -1;
2288 
2289 	result = git__calloc(1, sizeof(git_annotated_commit));
2290 	GIT_ERROR_CHECK_ALLOC(result);
2291 	result->type = GIT_ANNOTATED_COMMIT_VIRTUAL;
2292 	result->index = index;
2293 
2294 	if (insert_head_ids(&result->parents, one) < 0 ||
2295 		insert_head_ids(&result->parents, two) < 0) {
2296 		git_annotated_commit_free(result);
2297 		return -1;
2298 	}
2299 
2300 	*out = result;
2301 	return 0;
2302 }
2303 
compute_base(git_annotated_commit ** out,git_repository * repo,const git_annotated_commit * one,const git_annotated_commit * two,const git_merge_options * given_opts,size_t recursion_level)2304 static int compute_base(
2305 	git_annotated_commit **out,
2306 	git_repository *repo,
2307 	const git_annotated_commit *one,
2308 	const git_annotated_commit *two,
2309 	const git_merge_options *given_opts,
2310 	size_t recursion_level)
2311 {
2312 	git_array_oid_t head_ids = GIT_ARRAY_INIT;
2313 	git_oidarray bases = {0};
2314 	git_annotated_commit *base = NULL, *other = NULL, *new_base = NULL;
2315 	git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
2316 	size_t i, base_count;
2317 	int error;
2318 
2319 	*out = NULL;
2320 
2321 	if (given_opts)
2322 		memcpy(&opts, given_opts, sizeof(git_merge_options));
2323 
2324 	/* With more than two commits, merge_bases_many finds the base of
2325 	 * the first commit and a hypothetical merge of the others. Since
2326 	 * "one" may itself be a virtual commit, which insert_head_ids
2327 	 * substitutes multiple ancestors for, it needs to be added
2328 	 * after "two" which is always a single real commit.
2329 	 */
2330 	if ((error = insert_head_ids(&head_ids, two)) < 0 ||
2331 		(error = insert_head_ids(&head_ids, one)) < 0 ||
2332 		(error = git_merge_bases_many(&bases, repo,
2333 			head_ids.size, head_ids.ptr)) < 0)
2334 		goto done;
2335 
2336 	base_count = (opts.flags & GIT_MERGE_NO_RECURSIVE) ? 0 : bases.count;
2337 
2338 	if (base_count)
2339 		git_oidarray__reverse(&bases);
2340 
2341 	if ((error = git_annotated_commit_lookup(&base, repo, &bases.ids[0])) < 0)
2342 		goto done;
2343 
2344 	for (i = 1; i < base_count; i++) {
2345 		recursion_level++;
2346 
2347 		if (opts.recursion_limit && recursion_level > opts.recursion_limit)
2348 			break;
2349 
2350 		if ((error = git_annotated_commit_lookup(&other, repo,
2351 				&bases.ids[i])) < 0 ||
2352 			(error = create_virtual_base(&new_base, repo, base, other, &opts,
2353 				recursion_level)) < 0)
2354 			goto done;
2355 
2356 		git_annotated_commit_free(base);
2357 		git_annotated_commit_free(other);
2358 
2359 		base = new_base;
2360 		new_base = NULL;
2361 		other = NULL;
2362 	}
2363 
2364 done:
2365 	if (error == 0)
2366 		*out = base;
2367 	else
2368 		git_annotated_commit_free(base);
2369 
2370 	git_annotated_commit_free(other);
2371 	git_annotated_commit_free(new_base);
2372 	git_oidarray_dispose(&bases);
2373 	git_array_clear(head_ids);
2374 	return error;
2375 }
2376 
iterator_for_annotated_commit(git_iterator ** out,git_annotated_commit * commit)2377 static int iterator_for_annotated_commit(
2378 	git_iterator **out,
2379 	git_annotated_commit *commit)
2380 {
2381 	git_iterator_options opts = GIT_ITERATOR_OPTIONS_INIT;
2382 	int error;
2383 
2384 	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2385 
2386 	if (commit == NULL) {
2387 		error = git_iterator_for_nothing(out, &opts);
2388 	} else if (commit->type == GIT_ANNOTATED_COMMIT_VIRTUAL) {
2389 		error = git_iterator_for_index(out, git_index_owner(commit->index), commit->index, &opts);
2390 	} else {
2391 		if (!commit->tree &&
2392 			(error = git_commit_tree(&commit->tree, commit->commit)) < 0)
2393 			goto done;
2394 
2395 		error = git_iterator_for_tree(out, commit->tree, &opts);
2396 	}
2397 
2398 done:
2399 	return error;
2400 }
2401 
merge_annotated_commits(git_index ** index_out,git_annotated_commit ** base_out,git_repository * repo,git_annotated_commit * ours,git_annotated_commit * theirs,size_t recursion_level,const git_merge_options * opts)2402 static int merge_annotated_commits(
2403 	git_index **index_out,
2404 	git_annotated_commit **base_out,
2405 	git_repository *repo,
2406 	git_annotated_commit *ours,
2407 	git_annotated_commit *theirs,
2408 	size_t recursion_level,
2409 	const git_merge_options *opts)
2410 {
2411 	git_annotated_commit *base = NULL;
2412 	git_iterator *base_iter = NULL, *our_iter = NULL, *their_iter = NULL;
2413 	int error;
2414 
2415 	if ((error = compute_base(&base, repo, ours, theirs, opts,
2416 		recursion_level)) < 0) {
2417 
2418 		if (error != GIT_ENOTFOUND)
2419 			goto done;
2420 
2421 		git_error_clear();
2422 	}
2423 
2424 	if ((error = iterator_for_annotated_commit(&base_iter, base)) < 0 ||
2425 		(error = iterator_for_annotated_commit(&our_iter, ours)) < 0 ||
2426 		(error = iterator_for_annotated_commit(&their_iter, theirs)) < 0 ||
2427 		(error = git_merge__iterators(index_out, repo, base_iter, our_iter,
2428 			their_iter, opts)) < 0)
2429 		goto done;
2430 
2431 	if (base_out) {
2432 		*base_out = base;
2433 		base = NULL;
2434 	}
2435 
2436 done:
2437 	git_annotated_commit_free(base);
2438 	git_iterator_free(base_iter);
2439 	git_iterator_free(our_iter);
2440 	git_iterator_free(their_iter);
2441 	return error;
2442 }
2443 
2444 
git_merge_commits(git_index ** out,git_repository * repo,const git_commit * our_commit,const git_commit * their_commit,const git_merge_options * opts)2445 int git_merge_commits(
2446 	git_index **out,
2447 	git_repository *repo,
2448 	const git_commit *our_commit,
2449 	const git_commit *their_commit,
2450 	const git_merge_options *opts)
2451 {
2452 	git_annotated_commit *ours = NULL, *theirs = NULL, *base = NULL;
2453 	int error = 0;
2454 
2455 	if ((error = git_annotated_commit_from_commit(&ours, (git_commit *)our_commit)) < 0 ||
2456 		(error = git_annotated_commit_from_commit(&theirs, (git_commit *)their_commit)) < 0)
2457 		goto done;
2458 
2459 	error = merge_annotated_commits(out, &base, repo, ours, theirs, 0, opts);
2460 
2461 done:
2462 	git_annotated_commit_free(ours);
2463 	git_annotated_commit_free(theirs);
2464 	git_annotated_commit_free(base);
2465 	return error;
2466 }
2467 
2468 /* Merge setup / cleanup */
2469 
write_merge_head(git_repository * repo,const git_annotated_commit * heads[],size_t heads_len)2470 static int write_merge_head(
2471 	git_repository *repo,
2472 	const git_annotated_commit *heads[],
2473 	size_t heads_len)
2474 {
2475 	git_filebuf file = GIT_FILEBUF_INIT;
2476 	git_buf file_path = GIT_BUF_INIT;
2477 	size_t i;
2478 	int error = 0;
2479 
2480 	GIT_ASSERT_ARG(repo);
2481 	GIT_ASSERT_ARG(heads);
2482 
2483 	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_HEAD_FILE)) < 0 ||
2484 		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0)
2485 		goto cleanup;
2486 
2487 	for (i = 0; i < heads_len; i++) {
2488 		if ((error = git_filebuf_printf(&file, "%s\n", heads[i]->id_str)) < 0)
2489 			goto cleanup;
2490 	}
2491 
2492 	error = git_filebuf_commit(&file);
2493 
2494 cleanup:
2495 	if (error < 0)
2496 		git_filebuf_cleanup(&file);
2497 
2498 	git_buf_dispose(&file_path);
2499 
2500 	return error;
2501 }
2502 
write_merge_mode(git_repository * repo)2503 static int write_merge_mode(git_repository *repo)
2504 {
2505 	git_filebuf file = GIT_FILEBUF_INIT;
2506 	git_buf file_path = GIT_BUF_INIT;
2507 	int error = 0;
2508 
2509 	GIT_ASSERT_ARG(repo);
2510 
2511 	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MODE_FILE)) < 0 ||
2512 		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0)
2513 		goto cleanup;
2514 
2515 	if ((error = git_filebuf_write(&file, "no-ff", 5)) < 0)
2516 		goto cleanup;
2517 
2518 	error = git_filebuf_commit(&file);
2519 
2520 cleanup:
2521 	if (error < 0)
2522 		git_filebuf_cleanup(&file);
2523 
2524 	git_buf_dispose(&file_path);
2525 
2526 	return error;
2527 }
2528 
2529 struct merge_msg_entry {
2530 	const git_annotated_commit *merge_head;
2531 	bool written;
2532 };
2533 
msg_entry_is_branch(const struct merge_msg_entry * entry,git_vector * entries)2534 static int msg_entry_is_branch(
2535 	const struct merge_msg_entry *entry,
2536 	git_vector *entries)
2537 {
2538 	GIT_UNUSED(entries);
2539 
2540 	return (entry->written == 0 &&
2541 		entry->merge_head->remote_url == NULL &&
2542 		entry->merge_head->ref_name != NULL &&
2543 		git__strncmp(GIT_REFS_HEADS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_HEADS_DIR)) == 0);
2544 }
2545 
msg_entry_is_tracking(const struct merge_msg_entry * entry,git_vector * entries)2546 static int msg_entry_is_tracking(
2547 	const struct merge_msg_entry *entry,
2548 	git_vector *entries)
2549 {
2550 	GIT_UNUSED(entries);
2551 
2552 	return (entry->written == 0 &&
2553 		entry->merge_head->remote_url == NULL &&
2554 		entry->merge_head->ref_name != NULL &&
2555 		git__strncmp(GIT_REFS_REMOTES_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_REMOTES_DIR)) == 0);
2556 }
2557 
msg_entry_is_tag(const struct merge_msg_entry * entry,git_vector * entries)2558 static int msg_entry_is_tag(
2559 	const struct merge_msg_entry *entry,
2560 	git_vector *entries)
2561 {
2562 	GIT_UNUSED(entries);
2563 
2564 	return (entry->written == 0 &&
2565 		entry->merge_head->remote_url == NULL &&
2566 		entry->merge_head->ref_name != NULL &&
2567 		git__strncmp(GIT_REFS_TAGS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_TAGS_DIR)) == 0);
2568 }
2569 
msg_entry_is_remote(const struct merge_msg_entry * entry,git_vector * entries)2570 static int msg_entry_is_remote(
2571 	const struct merge_msg_entry *entry,
2572 	git_vector *entries)
2573 {
2574 	if (entry->written == 0 &&
2575 		entry->merge_head->remote_url != NULL &&
2576 		entry->merge_head->ref_name != NULL &&
2577 		git__strncmp(GIT_REFS_HEADS_DIR, entry->merge_head->ref_name, strlen(GIT_REFS_HEADS_DIR)) == 0)
2578 	{
2579 		struct merge_msg_entry *existing;
2580 
2581 		/* Match only branches from the same remote */
2582 		if (entries->length == 0)
2583 			return 1;
2584 
2585 		existing = git_vector_get(entries, 0);
2586 
2587 		return (git__strcmp(existing->merge_head->remote_url,
2588 			entry->merge_head->remote_url) == 0);
2589 	}
2590 
2591 	return 0;
2592 }
2593 
msg_entry_is_oid(const struct merge_msg_entry * merge_msg_entry)2594 static int msg_entry_is_oid(
2595 	const struct merge_msg_entry *merge_msg_entry)
2596 {
2597 	return (merge_msg_entry->written == 0 &&
2598 		merge_msg_entry->merge_head->ref_name == NULL &&
2599 		merge_msg_entry->merge_head->remote_url == NULL);
2600 }
2601 
merge_msg_entry_written(const struct merge_msg_entry * merge_msg_entry)2602 static int merge_msg_entry_written(
2603 	const struct merge_msg_entry *merge_msg_entry)
2604 {
2605 	return (merge_msg_entry->written == 1);
2606 }
2607 
merge_msg_entries(git_vector * v,const struct merge_msg_entry * entries,size_t len,int (* match)(const struct merge_msg_entry * entry,git_vector * entries))2608 static int merge_msg_entries(
2609 	git_vector *v,
2610 	const struct merge_msg_entry *entries,
2611 	size_t len,
2612 	int (*match)(const struct merge_msg_entry *entry, git_vector *entries))
2613 {
2614 	size_t i;
2615 	int matches, total = 0;
2616 
2617 	git_vector_clear(v);
2618 
2619 	for (i = 0; i < len; i++) {
2620 		if ((matches = match(&entries[i], v)) < 0)
2621 			return matches;
2622 		else if (!matches)
2623 			continue;
2624 
2625 		git_vector_insert(v, (struct merge_msg_entry *)&entries[i]);
2626 		total++;
2627 	}
2628 
2629 	return total;
2630 }
2631 
merge_msg_write_entries(git_filebuf * file,git_vector * entries,const char * item_name,const char * item_plural_name,size_t ref_name_skip,const char * source,char sep)2632 static int merge_msg_write_entries(
2633 	git_filebuf *file,
2634 	git_vector *entries,
2635 	const char *item_name,
2636 	const char *item_plural_name,
2637 	size_t ref_name_skip,
2638 	const char *source,
2639 	char sep)
2640 {
2641 	struct merge_msg_entry *entry;
2642 	size_t i;
2643 	int error = 0;
2644 
2645 	if (entries->length == 0)
2646 		return 0;
2647 
2648 	if (sep && (error = git_filebuf_printf(file, "%c ", sep)) < 0)
2649 		goto done;
2650 
2651 	if ((error = git_filebuf_printf(file, "%s ",
2652 		(entries->length == 1) ? item_name : item_plural_name)) < 0)
2653 		goto done;
2654 
2655 	git_vector_foreach(entries, i, entry) {
2656 		if (i > 0 &&
2657 			(error = git_filebuf_printf(file, "%s", (i == entries->length - 1) ? " and " : ", ")) < 0)
2658 			goto done;
2659 
2660 		if ((error = git_filebuf_printf(file, "'%s'", entry->merge_head->ref_name + ref_name_skip)) < 0)
2661 			goto done;
2662 
2663 		entry->written = 1;
2664 	}
2665 
2666 	if (source)
2667 		error = git_filebuf_printf(file, " of %s", source);
2668 
2669 done:
2670 	return error;
2671 }
2672 
merge_msg_write_branches(git_filebuf * file,git_vector * entries,char sep)2673 static int merge_msg_write_branches(
2674 	git_filebuf *file,
2675 	git_vector *entries,
2676 	char sep)
2677 {
2678 	return merge_msg_write_entries(file, entries,
2679 		"branch", "branches", strlen(GIT_REFS_HEADS_DIR), NULL, sep);
2680 }
2681 
merge_msg_write_tracking(git_filebuf * file,git_vector * entries,char sep)2682 static int merge_msg_write_tracking(
2683 	git_filebuf *file,
2684 	git_vector *entries,
2685 	char sep)
2686 {
2687 	return merge_msg_write_entries(file, entries,
2688 		"remote-tracking branch", "remote-tracking branches", 0, NULL, sep);
2689 }
2690 
merge_msg_write_tags(git_filebuf * file,git_vector * entries,char sep)2691 static int merge_msg_write_tags(
2692 	git_filebuf *file,
2693 	git_vector *entries,
2694 	char sep)
2695 {
2696 	return merge_msg_write_entries(file, entries,
2697 		"tag", "tags", strlen(GIT_REFS_TAGS_DIR), NULL, sep);
2698 }
2699 
merge_msg_write_remotes(git_filebuf * file,git_vector * entries,char sep)2700 static int merge_msg_write_remotes(
2701 	git_filebuf *file,
2702 	git_vector *entries,
2703 	char sep)
2704 {
2705 	const char *source;
2706 
2707 	if (entries->length == 0)
2708 		return 0;
2709 
2710 	source = ((struct merge_msg_entry *)entries->contents[0])->merge_head->remote_url;
2711 
2712 	return merge_msg_write_entries(file, entries,
2713 		"branch", "branches", strlen(GIT_REFS_HEADS_DIR), source, sep);
2714 }
2715 
write_merge_msg(git_repository * repo,const git_annotated_commit * heads[],size_t heads_len)2716 static int write_merge_msg(
2717 	git_repository *repo,
2718 	const git_annotated_commit *heads[],
2719 	size_t heads_len)
2720 {
2721 	git_filebuf file = GIT_FILEBUF_INIT;
2722 	git_buf file_path = GIT_BUF_INIT;
2723 	struct merge_msg_entry *entries;
2724 	git_vector matching = GIT_VECTOR_INIT;
2725 	size_t i;
2726 	char sep = 0;
2727 	int error = 0;
2728 
2729 	GIT_ASSERT_ARG(repo);
2730 	GIT_ASSERT_ARG(heads);
2731 
2732 	entries = git__calloc(heads_len, sizeof(struct merge_msg_entry));
2733 	GIT_ERROR_CHECK_ALLOC(entries);
2734 
2735 	if (git_vector_init(&matching, heads_len, NULL) < 0) {
2736 		git__free(entries);
2737 		return -1;
2738 	}
2739 
2740 	for (i = 0; i < heads_len; i++)
2741 		entries[i].merge_head = heads[i];
2742 
2743 	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
2744 		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_CREATE_LEADING_DIRS, GIT_MERGE_FILE_MODE)) < 0 ||
2745 		(error = git_filebuf_write(&file, "Merge ", 6)) < 0)
2746 		goto cleanup;
2747 
2748 	/*
2749 	 * This is to emulate the format of MERGE_MSG by core git.
2750 	 *
2751 	 * Core git will write all the commits specified by OID, in the order
2752 	 * provided, until the first named branch or tag is reached, at which
2753 	 * point all branches will be written in the order provided, then all
2754 	 * tags, then all remote tracking branches and finally all commits that
2755 	 * were specified by OID that were not already written.
2756 	 *
2757 	 * Yes.  Really.
2758 	 */
2759 	for (i = 0; i < heads_len; i++) {
2760 		if (!msg_entry_is_oid(&entries[i]))
2761 			break;
2762 
2763 		if ((error = git_filebuf_printf(&file,
2764 			"%scommit '%s'", (i > 0) ? "; " : "",
2765 			entries[i].merge_head->id_str)) < 0)
2766 			goto cleanup;
2767 
2768 		entries[i].written = 1;
2769 	}
2770 
2771 	if (i)
2772 		sep = ';';
2773 
2774 	if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_branch)) < 0 ||
2775 		(error = merge_msg_write_branches(&file, &matching, sep)) < 0)
2776 		goto cleanup;
2777 
2778 	if (matching.length)
2779 		sep =',';
2780 
2781 	if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_tracking)) < 0 ||
2782 		(error = merge_msg_write_tracking(&file, &matching, sep)) < 0)
2783 		goto cleanup;
2784 
2785 	if (matching.length)
2786 		sep =',';
2787 
2788 	if ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_tag)) < 0 ||
2789 		(error = merge_msg_write_tags(&file, &matching, sep)) < 0)
2790 		goto cleanup;
2791 
2792 	if (matching.length)
2793 		sep =',';
2794 
2795 	/* We should never be called with multiple remote branches, but handle
2796 	 * it in case we are... */
2797 	while ((error = merge_msg_entries(&matching, entries, heads_len, msg_entry_is_remote)) > 0) {
2798 		if ((error = merge_msg_write_remotes(&file, &matching, sep)) < 0)
2799 			goto cleanup;
2800 
2801 		if (matching.length)
2802 			sep =',';
2803 	}
2804 
2805 	if (error < 0)
2806 		goto cleanup;
2807 
2808 	for (i = 0; i < heads_len; i++) {
2809 		if (merge_msg_entry_written(&entries[i]))
2810 			continue;
2811 
2812 		if ((error = git_filebuf_printf(&file, "; commit '%s'",
2813 			entries[i].merge_head->id_str)) < 0)
2814 			goto cleanup;
2815 	}
2816 
2817 	if ((error = git_filebuf_printf(&file, "\n")) < 0 ||
2818 		(error = git_filebuf_commit(&file)) < 0)
2819 		goto cleanup;
2820 
2821 cleanup:
2822 	if (error < 0)
2823 		git_filebuf_cleanup(&file);
2824 
2825 	git_buf_dispose(&file_path);
2826 
2827 	git_vector_free(&matching);
2828 	git__free(entries);
2829 
2830 	return error;
2831 }
2832 
git_merge__setup(git_repository * repo,const git_annotated_commit * our_head,const git_annotated_commit * heads[],size_t heads_len)2833 int git_merge__setup(
2834 	git_repository *repo,
2835 	const git_annotated_commit *our_head,
2836 	const git_annotated_commit *heads[],
2837 	size_t heads_len)
2838 {
2839 	int error = 0;
2840 
2841 	GIT_ASSERT_ARG(repo);
2842 	GIT_ASSERT_ARG(our_head);
2843 	GIT_ASSERT_ARG(heads);
2844 
2845 	if ((error = git_repository__set_orig_head(repo, git_annotated_commit_id(our_head))) == 0 &&
2846 		(error = write_merge_head(repo, heads, heads_len)) == 0 &&
2847 		(error = write_merge_mode(repo)) == 0) {
2848 		error = write_merge_msg(repo, heads, heads_len);
2849 	}
2850 
2851 	return error;
2852 }
2853 
2854 /* Merge branches */
2855 
merge_ancestor_head(git_annotated_commit ** ancestor_head,git_repository * repo,const git_annotated_commit * our_head,const git_annotated_commit ** their_heads,size_t their_heads_len)2856 static int merge_ancestor_head(
2857 	git_annotated_commit **ancestor_head,
2858 	git_repository *repo,
2859 	const git_annotated_commit *our_head,
2860 	const git_annotated_commit **their_heads,
2861 	size_t their_heads_len)
2862 {
2863 	git_oid *oids, ancestor_oid;
2864 	size_t i, alloc_len;
2865 	int error = 0;
2866 
2867 	GIT_ASSERT_ARG(repo);
2868 	GIT_ASSERT_ARG(our_head);
2869 	GIT_ASSERT_ARG(their_heads);
2870 
2871 	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, their_heads_len, 1);
2872 	oids = git__calloc(alloc_len, sizeof(git_oid));
2873 	GIT_ERROR_CHECK_ALLOC(oids);
2874 
2875 	git_oid_cpy(&oids[0], git_commit_id(our_head->commit));
2876 
2877 	for (i = 0; i < their_heads_len; i++)
2878 		git_oid_cpy(&oids[i + 1], git_annotated_commit_id(their_heads[i]));
2879 
2880 	if ((error = git_merge_base_many(&ancestor_oid, repo, their_heads_len + 1, oids)) < 0)
2881 		goto on_error;
2882 
2883 	error = git_annotated_commit_lookup(ancestor_head, repo, &ancestor_oid);
2884 
2885 on_error:
2886 	git__free(oids);
2887 	return error;
2888 }
2889 
merge_their_label(const char * branchname)2890 static const char *merge_their_label(const char *branchname)
2891 {
2892 	const char *slash;
2893 
2894 	if ((slash = strrchr(branchname, '/')) == NULL)
2895 		return branchname;
2896 
2897 	if (*(slash+1) == '\0')
2898 		return "theirs";
2899 
2900 	return slash+1;
2901 }
2902 
merge_normalize_checkout_opts(git_checkout_options * out,git_repository * repo,const git_checkout_options * given_checkout_opts,unsigned int checkout_strategy,git_annotated_commit * ancestor,const git_annotated_commit * our_head,const git_annotated_commit ** their_heads,size_t their_heads_len)2903 static int merge_normalize_checkout_opts(
2904 	git_checkout_options *out,
2905 	git_repository *repo,
2906 	const git_checkout_options *given_checkout_opts,
2907 	unsigned int checkout_strategy,
2908 	git_annotated_commit *ancestor,
2909 	const git_annotated_commit *our_head,
2910 	const git_annotated_commit **their_heads,
2911 	size_t their_heads_len)
2912 {
2913 	git_checkout_options default_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
2914 	int error = 0;
2915 
2916 	GIT_UNUSED(repo);
2917 
2918 	if (given_checkout_opts != NULL)
2919 		memcpy(out, given_checkout_opts, sizeof(git_checkout_options));
2920 	else
2921 		memcpy(out, &default_checkout_opts, sizeof(git_checkout_options));
2922 
2923 	out->checkout_strategy = checkout_strategy;
2924 
2925 	if (!out->ancestor_label) {
2926 		if (ancestor && ancestor->type == GIT_ANNOTATED_COMMIT_REAL)
2927 			out->ancestor_label = git_commit_summary(ancestor->commit);
2928 		else if (ancestor)
2929 			out->ancestor_label = "merged common ancestors";
2930 		else
2931 			out->ancestor_label = "empty base";
2932 	}
2933 
2934 	if (!out->our_label) {
2935 		if (our_head && our_head->ref_name)
2936 			out->our_label = our_head->ref_name;
2937 		else
2938 			out->our_label = "ours";
2939 	}
2940 
2941 	if (!out->their_label) {
2942 		if (their_heads_len == 1 && their_heads[0]->ref_name)
2943 			out->their_label = merge_their_label(their_heads[0]->ref_name);
2944 		else if (their_heads_len == 1)
2945 			out->their_label = their_heads[0]->id_str;
2946 		else
2947 			out->their_label = "theirs";
2948 	}
2949 
2950 	return error;
2951 }
2952 
merge_check_index(size_t * conflicts,git_repository * repo,git_index * index_new,git_vector * merged_paths)2953 static int merge_check_index(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths)
2954 {
2955 	git_tree *head_tree = NULL;
2956 	git_index *index_repo = NULL;
2957 	git_iterator *iter_repo = NULL, *iter_new = NULL;
2958 	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
2959 	git_diff *staged_diff_list = NULL, *index_diff_list = NULL;
2960 	git_diff_delta *delta;
2961 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
2962 	git_vector staged_paths = GIT_VECTOR_INIT;
2963 	size_t i;
2964 	int error = 0;
2965 
2966 	GIT_UNUSED(merged_paths);
2967 
2968 	*conflicts = 0;
2969 
2970 	/* No staged changes may exist unless the change staged is identical to
2971 	 * the result of the merge.  This allows one to apply to merge manually,
2972 	 * then run merge.  Any other staged change would be overwritten by
2973 	 * a reset merge.
2974 	 */
2975 	if ((error = git_repository_head_tree(&head_tree, repo)) < 0 ||
2976 		(error = git_repository_index(&index_repo, repo)) < 0 ||
2977 		(error = git_diff_tree_to_index(&staged_diff_list, repo, head_tree, index_repo, &opts)) < 0)
2978 		goto done;
2979 
2980 	if (staged_diff_list->deltas.length == 0)
2981 		goto done;
2982 
2983 	git_vector_foreach(&staged_diff_list->deltas, i, delta) {
2984 		if ((error = git_vector_insert(&staged_paths, (char *)delta->new_file.path)) < 0)
2985 			goto done;
2986 	}
2987 
2988 	iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
2989 	iter_opts.pathlist.strings = (char **)staged_paths.contents;
2990 	iter_opts.pathlist.count = staged_paths.length;
2991 
2992 	if ((error = git_iterator_for_index(&iter_repo, repo, index_repo, &iter_opts)) < 0 ||
2993 		(error = git_iterator_for_index(&iter_new, repo, index_new, &iter_opts)) < 0 ||
2994 		(error = git_diff__from_iterators(&index_diff_list, repo, iter_repo, iter_new, &opts)) < 0)
2995 		goto done;
2996 
2997 	*conflicts = index_diff_list->deltas.length;
2998 
2999 done:
3000 	git_tree_free(head_tree);
3001 	git_index_free(index_repo);
3002 	git_iterator_free(iter_repo);
3003 	git_iterator_free(iter_new);
3004 	git_diff_free(staged_diff_list);
3005 	git_diff_free(index_diff_list);
3006 	git_vector_free(&staged_paths);
3007 
3008 	return error;
3009 }
3010 
merge_check_workdir(size_t * conflicts,git_repository * repo,git_index * index_new,git_vector * merged_paths)3011 static int merge_check_workdir(size_t *conflicts, git_repository *repo, git_index *index_new, git_vector *merged_paths)
3012 {
3013 	git_diff *wd_diff_list = NULL;
3014 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3015 	int error = 0;
3016 
3017 	GIT_UNUSED(index_new);
3018 
3019 	*conflicts = 0;
3020 
3021 	/* We need to have merged at least 1 file for the possibility to exist to
3022 	 * have conflicts with the workdir. Passing 0 as the pathspec count paramter
3023 	 * will consider all files in the working directory, that is, we may detect
3024 	 * a conflict if there were untracked files in the workdir prior to starting
3025 	 * the merge. This typically happens when cherry-picking a commmit whose
3026 	 * changes have already been applied.
3027 	 */
3028 	if (merged_paths->length == 0)
3029 		return 0;
3030 
3031 	opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
3032 
3033 	/* Workdir changes may exist iff they do not conflict with changes that
3034 	 * will be applied by the merge (including conflicts).  Ensure that there
3035 	 * are no changes in the workdir to these paths.
3036 	 */
3037 	opts.flags |= GIT_DIFF_DISABLE_PATHSPEC_MATCH;
3038 	opts.pathspec.count = merged_paths->length;
3039 	opts.pathspec.strings = (char **)merged_paths->contents;
3040 	opts.ignore_submodules = GIT_SUBMODULE_IGNORE_ALL;
3041 
3042 	if ((error = git_diff_index_to_workdir(&wd_diff_list, repo, NULL, &opts)) < 0)
3043 		goto done;
3044 
3045 	*conflicts = wd_diff_list->deltas.length;
3046 
3047 done:
3048 	git_diff_free(wd_diff_list);
3049 
3050 	return error;
3051 }
3052 
git_merge__check_result(git_repository * repo,git_index * index_new)3053 int git_merge__check_result(git_repository *repo, git_index *index_new)
3054 {
3055 	git_tree *head_tree = NULL;
3056 	git_iterator *iter_head = NULL, *iter_new = NULL;
3057 	git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
3058 	git_diff *merged_list = NULL;
3059 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
3060 	git_diff_delta *delta;
3061 	git_vector paths = GIT_VECTOR_INIT;
3062 	size_t i, index_conflicts = 0, wd_conflicts = 0, conflicts;
3063 	const git_index_entry *e;
3064 	int error = 0;
3065 
3066 	iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
3067 
3068 	if ((error = git_repository_head_tree(&head_tree, repo)) < 0 ||
3069 		(error = git_iterator_for_tree(&iter_head, head_tree, &iter_opts)) < 0 ||
3070 		(error = git_iterator_for_index(&iter_new, repo, index_new, &iter_opts)) < 0 ||
3071 		(error = git_diff__from_iterators(&merged_list, repo, iter_head, iter_new, &opts)) < 0)
3072 		goto done;
3073 
3074 	git_vector_foreach(&merged_list->deltas, i, delta) {
3075 		if ((error = git_vector_insert(&paths, (char *)delta->new_file.path)) < 0)
3076 			goto done;
3077 	}
3078 
3079 	for (i = 0; i < git_index_entrycount(index_new); i++) {
3080 		e = git_index_get_byindex(index_new, i);
3081 
3082 		if (git_index_entry_is_conflict(e) &&
3083 			(git_vector_last(&paths) == NULL ||
3084 			strcmp(git_vector_last(&paths), e->path) != 0)) {
3085 
3086 			if ((error = git_vector_insert(&paths, (char *)e->path)) < 0)
3087 				goto done;
3088 		}
3089 	}
3090 
3091 	/* Make sure the index and workdir state do not prevent merging */
3092 	if ((error = merge_check_index(&index_conflicts, repo, index_new, &paths)) < 0 ||
3093 		(error = merge_check_workdir(&wd_conflicts, repo, index_new, &paths)) < 0)
3094 		goto done;
3095 
3096 	if ((conflicts = index_conflicts + wd_conflicts) > 0) {
3097 		git_error_set(GIT_ERROR_MERGE, "%" PRIuZ " uncommitted change%s would be overwritten by merge",
3098 			conflicts, (conflicts != 1) ? "s" : "");
3099 		error = GIT_ECONFLICT;
3100 	}
3101 
3102 done:
3103 	git_vector_free(&paths);
3104 	git_tree_free(head_tree);
3105 	git_iterator_free(iter_head);
3106 	git_iterator_free(iter_new);
3107 	git_diff_free(merged_list);
3108 
3109 	return error;
3110 }
3111 
git_merge__append_conflicts_to_merge_msg(git_repository * repo,git_index * index)3112 int git_merge__append_conflicts_to_merge_msg(
3113 	git_repository *repo,
3114 	git_index *index)
3115 {
3116 	git_filebuf file = GIT_FILEBUF_INIT;
3117 	git_buf file_path = GIT_BUF_INIT;
3118 	const char *last = NULL;
3119 	size_t i;
3120 	int error;
3121 
3122 	if (!git_index_has_conflicts(index))
3123 		return 0;
3124 
3125 	if ((error = git_buf_joinpath(&file_path, repo->gitdir, GIT_MERGE_MSG_FILE)) < 0 ||
3126 		(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_APPEND, GIT_MERGE_FILE_MODE)) < 0)
3127 		goto cleanup;
3128 
3129 	git_filebuf_printf(&file, "\nConflicts:\n");
3130 
3131 	for (i = 0; i < git_index_entrycount(index); i++) {
3132 		const git_index_entry *e = git_index_get_byindex(index, i);
3133 
3134 		if (!git_index_entry_is_conflict(e))
3135 			continue;
3136 
3137 		if (last == NULL || strcmp(e->path, last) != 0)
3138 			git_filebuf_printf(&file, "\t%s\n", e->path);
3139 
3140 		last = e->path;
3141 	}
3142 
3143 	error = git_filebuf_commit(&file);
3144 
3145 cleanup:
3146 	if (error < 0)
3147 		git_filebuf_cleanup(&file);
3148 
3149 	git_buf_dispose(&file_path);
3150 
3151 	return error;
3152 }
3153 
merge_state_cleanup(git_repository * repo)3154 static int merge_state_cleanup(git_repository *repo)
3155 {
3156 	const char *state_files[] = {
3157 		GIT_MERGE_HEAD_FILE,
3158 		GIT_MERGE_MODE_FILE,
3159 		GIT_MERGE_MSG_FILE,
3160 	};
3161 
3162 	return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
3163 }
3164 
merge_heads(git_annotated_commit ** ancestor_head_out,git_annotated_commit ** our_head_out,git_repository * repo,git_reference * our_ref,const git_annotated_commit ** their_heads,size_t their_heads_len)3165 static int merge_heads(
3166 	git_annotated_commit **ancestor_head_out,
3167 	git_annotated_commit **our_head_out,
3168 	git_repository *repo,
3169 	git_reference *our_ref,
3170 	const git_annotated_commit **their_heads,
3171 	size_t their_heads_len)
3172 {
3173 	git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
3174 	int error = 0;
3175 
3176 	*ancestor_head_out = NULL;
3177 	*our_head_out = NULL;
3178 
3179 	if ((error = git_annotated_commit_from_ref(&our_head, repo, our_ref)) < 0)
3180 		goto done;
3181 
3182 	if ((error = merge_ancestor_head(&ancestor_head, repo, our_head, their_heads, their_heads_len)) < 0) {
3183 		if (error != GIT_ENOTFOUND)
3184 			goto done;
3185 
3186 		git_error_clear();
3187 		error = 0;
3188 	}
3189 
3190 	*ancestor_head_out = ancestor_head;
3191 	*our_head_out = our_head;
3192 
3193 done:
3194 	if (error < 0) {
3195 		git_annotated_commit_free(ancestor_head);
3196 		git_annotated_commit_free(our_head);
3197 	}
3198 
3199 	return error;
3200 }
3201 
merge_preference(git_merge_preference_t * out,git_repository * repo)3202 static int merge_preference(git_merge_preference_t *out, git_repository *repo)
3203 {
3204 	git_config *config;
3205 	const char *value;
3206 	int bool_value, error = 0;
3207 
3208 	*out = GIT_MERGE_PREFERENCE_NONE;
3209 
3210 	if ((error = git_repository_config_snapshot(&config, repo)) < 0)
3211 		goto done;
3212 
3213 	if ((error = git_config_get_string(&value, config, "merge.ff")) < 0) {
3214 		if (error == GIT_ENOTFOUND) {
3215 			git_error_clear();
3216 			error = 0;
3217 		}
3218 
3219 		goto done;
3220 	}
3221 
3222 	if (git_config_parse_bool(&bool_value, value) == 0) {
3223 		if (!bool_value)
3224 			*out |= GIT_MERGE_PREFERENCE_NO_FASTFORWARD;
3225 	} else {
3226 		if (strcasecmp(value, "only") == 0)
3227 			*out |= GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY;
3228 	}
3229 
3230 done:
3231 	git_config_free(config);
3232 	return error;
3233 }
3234 
git_merge_analysis_for_ref(git_merge_analysis_t * analysis_out,git_merge_preference_t * preference_out,git_repository * repo,git_reference * our_ref,const git_annotated_commit ** their_heads,size_t their_heads_len)3235 int git_merge_analysis_for_ref(
3236 	git_merge_analysis_t *analysis_out,
3237 	git_merge_preference_t *preference_out,
3238 	git_repository *repo,
3239 	git_reference *our_ref,
3240 	const git_annotated_commit **their_heads,
3241 	size_t their_heads_len)
3242 {
3243 	git_annotated_commit *ancestor_head = NULL, *our_head = NULL;
3244 	int error = 0;
3245 	bool unborn;
3246 
3247 	GIT_ASSERT_ARG(analysis_out);
3248 	GIT_ASSERT_ARG(preference_out);
3249 	GIT_ASSERT_ARG(repo);
3250 	GIT_ASSERT_ARG(their_heads && their_heads_len > 0);
3251 
3252 	if (their_heads_len != 1) {
3253 		git_error_set(GIT_ERROR_MERGE, "can only merge a single branch");
3254 		error = -1;
3255 		goto done;
3256 	}
3257 
3258 	*analysis_out = GIT_MERGE_ANALYSIS_NONE;
3259 
3260 	if ((error = merge_preference(preference_out, repo)) < 0)
3261 		goto done;
3262 
3263 	if ((error = git_reference__is_unborn_head(&unborn, our_ref, repo)) < 0)
3264 		goto done;
3265 
3266 	if (unborn) {
3267 		*analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_UNBORN;
3268 		error = 0;
3269 		goto done;
3270 	}
3271 
3272 	if ((error = merge_heads(&ancestor_head, &our_head, repo, our_ref, their_heads, their_heads_len)) < 0)
3273 		goto done;
3274 
3275 	/* We're up-to-date if we're trying to merge our own common ancestor. */
3276 	if (ancestor_head && git_oid_equal(
3277 		git_annotated_commit_id(ancestor_head), git_annotated_commit_id(their_heads[0])))
3278 		*analysis_out |= GIT_MERGE_ANALYSIS_UP_TO_DATE;
3279 
3280 	/* We're fastforwardable if we're our own common ancestor. */
3281 	else if (ancestor_head && git_oid_equal(
3282 		git_annotated_commit_id(ancestor_head), git_annotated_commit_id(our_head)))
3283 		*analysis_out |= GIT_MERGE_ANALYSIS_FASTFORWARD | GIT_MERGE_ANALYSIS_NORMAL;
3284 
3285 	/* Otherwise, just a normal merge is possible. */
3286 	else
3287 		*analysis_out |= GIT_MERGE_ANALYSIS_NORMAL;
3288 
3289 done:
3290 	git_annotated_commit_free(ancestor_head);
3291 	git_annotated_commit_free(our_head);
3292 	return error;
3293 }
3294 
git_merge_analysis(git_merge_analysis_t * analysis_out,git_merge_preference_t * preference_out,git_repository * repo,const git_annotated_commit ** their_heads,size_t their_heads_len)3295 int git_merge_analysis(
3296 	git_merge_analysis_t *analysis_out,
3297 	git_merge_preference_t *preference_out,
3298 	git_repository *repo,
3299 	const git_annotated_commit **their_heads,
3300 	size_t their_heads_len)
3301 {
3302 	git_reference *head_ref = NULL;
3303 	int error = 0;
3304 
3305 	if ((error = git_reference_lookup(&head_ref, repo, GIT_HEAD_FILE)) < 0) {
3306 		git_error_set(GIT_ERROR_MERGE, "failed to lookup HEAD reference");
3307 		return error;
3308 	}
3309 
3310 	error = git_merge_analysis_for_ref(analysis_out, preference_out, repo, head_ref, their_heads, their_heads_len);
3311 
3312 	git_reference_free(head_ref);
3313 
3314 	return error;
3315 }
3316 
git_merge(git_repository * repo,const git_annotated_commit ** their_heads,size_t their_heads_len,const git_merge_options * merge_opts,const git_checkout_options * given_checkout_opts)3317 int git_merge(
3318 	git_repository *repo,
3319 	const git_annotated_commit **their_heads,
3320 	size_t their_heads_len,
3321 	const git_merge_options *merge_opts,
3322 	const git_checkout_options *given_checkout_opts)
3323 {
3324 	git_reference *our_ref = NULL;
3325 	git_checkout_options checkout_opts;
3326 	git_annotated_commit *our_head = NULL, *base = NULL;
3327 	git_index *repo_index = NULL, *index = NULL;
3328 	git_indexwriter indexwriter = GIT_INDEXWRITER_INIT;
3329 	unsigned int checkout_strategy;
3330 	int error = 0;
3331 
3332 	GIT_ASSERT_ARG(repo);
3333 	GIT_ASSERT_ARG(their_heads && their_heads_len > 0);
3334 
3335 	if (their_heads_len != 1) {
3336 		git_error_set(GIT_ERROR_MERGE, "can only merge a single branch");
3337 		return -1;
3338 	}
3339 
3340 	if ((error = git_repository__ensure_not_bare(repo, "merge")) < 0)
3341 		goto done;
3342 
3343 	checkout_strategy = given_checkout_opts ?
3344 		given_checkout_opts->checkout_strategy :
3345 		GIT_CHECKOUT_SAFE;
3346 
3347 	if ((error = git_indexwriter_init_for_operation(&indexwriter, repo,
3348 		&checkout_strategy)) < 0)
3349 		goto done;
3350 
3351 	if ((error = git_repository_index(&repo_index, repo) < 0) ||
3352 	    (error = git_index_read(repo_index, 0) < 0))
3353 		goto done;
3354 
3355 	/* Write the merge setup files to the repository. */
3356 	if ((error = git_annotated_commit_from_head(&our_head, repo)) < 0 ||
3357 		(error = git_merge__setup(repo, our_head, their_heads,
3358 			their_heads_len)) < 0)
3359 		goto done;
3360 
3361 	/* TODO: octopus */
3362 
3363 	if ((error = merge_annotated_commits(&index, &base, repo, our_head,
3364 			(git_annotated_commit *)their_heads[0], 0, merge_opts)) < 0 ||
3365 		(error = git_merge__check_result(repo, index)) < 0 ||
3366 		(error = git_merge__append_conflicts_to_merge_msg(repo, index)) < 0)
3367 		goto done;
3368 
3369 	/* check out the merge results */
3370 
3371 	if ((error = merge_normalize_checkout_opts(&checkout_opts, repo,
3372 			given_checkout_opts, checkout_strategy,
3373 			base, our_head, their_heads, their_heads_len)) < 0 ||
3374 		(error = git_checkout_index(repo, index, &checkout_opts)) < 0)
3375 		goto done;
3376 
3377 	error = git_indexwriter_commit(&indexwriter);
3378 
3379 done:
3380 	if (error < 0)
3381 		merge_state_cleanup(repo);
3382 
3383 	git_indexwriter_cleanup(&indexwriter);
3384 	git_index_free(index);
3385 	git_annotated_commit_free(our_head);
3386 	git_annotated_commit_free(base);
3387 	git_reference_free(our_ref);
3388 	git_index_free(repo_index);
3389 
3390 	return error;
3391 }
3392 
git_merge_options_init(git_merge_options * opts,unsigned int version)3393 int git_merge_options_init(git_merge_options *opts, unsigned int version)
3394 {
3395 	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3396 		opts, version, git_merge_options, GIT_MERGE_OPTIONS_INIT);
3397 	return 0;
3398 }
3399 
3400 #ifndef GIT_DEPRECATE_HARD
git_merge_init_options(git_merge_options * opts,unsigned int version)3401 int git_merge_init_options(git_merge_options *opts, unsigned int version)
3402 {
3403 	return git_merge_options_init(opts, version);
3404 }
3405 #endif
3406 
git_merge_file_input_init(git_merge_file_input * input,unsigned int version)3407 int git_merge_file_input_init(git_merge_file_input *input, unsigned int version)
3408 {
3409 	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3410 		input, version, git_merge_file_input, GIT_MERGE_FILE_INPUT_INIT);
3411 	return 0;
3412 }
3413 
3414 #ifndef GIT_DEPRECATE_HARD
git_merge_file_init_input(git_merge_file_input * input,unsigned int version)3415 int git_merge_file_init_input(git_merge_file_input *input, unsigned int version)
3416 {
3417 	return git_merge_file_input_init(input, version);
3418 }
3419 #endif
3420 
git_merge_file_options_init(git_merge_file_options * opts,unsigned int version)3421 int git_merge_file_options_init(
3422 	git_merge_file_options *opts, unsigned int version)
3423 {
3424 	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
3425 		opts, version, git_merge_file_options, GIT_MERGE_FILE_OPTIONS_INIT);
3426 	return 0;
3427 }
3428 
3429 #ifndef GIT_DEPRECATE_HARD
git_merge_file_init_options(git_merge_file_options * opts,unsigned int version)3430 int git_merge_file_init_options(
3431 	git_merge_file_options *opts, unsigned int version)
3432 {
3433 	return git_merge_file_options_init(opts, version);
3434 }
3435 #endif
3436