deps/libgit2/src/libgit2/merge.c

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