forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.c
More file actions
2770 lines (2187 loc) · 72.2 KB
/
merge.c
File metadata and controls
2770 lines (2187 loc) · 72.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "posix.h"
#include "buffer.h"
#include "repository.h"
#include "revwalk.h"
#include "commit_list.h"
#include "merge.h"
#include "path.h"
#include "refs.h"
#include "object.h"
#include "iterator.h"
#include "refs.h"
#include "diff.h"
#include "checkout.h"
#include "tree.h"
#include "merge_file.h"
#include "blob.h"
#include "hashsig.h"
#include "oid.h"
#include "index.h"
#include "filebuf.h"
#include "config.h"
#include "git2/types.h"
#include "git2/repository.h"
#include "git2/object.h"
#include "git2/commit.h"
#include "git2/merge.h"
#include "git2/refs.h"
#include "git2/reset.h"
#include "git2/checkout.h"
#include "git2/signature.h"
#include "git2/config.h"
#include "git2/tree.h"
#include "git2/sys/index.h"
#define GIT_MERGE_INDEX_ENTRY_EXISTS(X) ((X).mode != 0)
#define GIT_MERGE_INDEX_ENTRY_ISFILE(X) S_ISREG((X).mode)
typedef enum {
TREE_IDX_ANCESTOR = 0,
TREE_IDX_OURS = 1,
TREE_IDX_THEIRS = 2
} merge_tree_index_t;
/* Tracks D/F conflicts */
struct merge_diff_df_data {
const char *df_path;
const char *prev_path;
git_merge_diff *prev_conflict;
};
/* Merge base computation */
int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[])
{
git_revwalk *walk;
git_vector list;
git_commit_list *result = NULL;
int error = -1;
unsigned int i;
git_commit_list_node *commit;
assert(out && repo && input_array);
if (length < 2) {
giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length);
return -1;
}
if (git_vector_init(&list, length - 1, NULL) < 0)
return -1;
if (git_revwalk_new(&walk, repo) < 0)
goto cleanup;
for (i = 1; i < length; i++) {
commit = git_revwalk__commit_lookup(walk, &input_array[i]);
if (commit == NULL)
goto cleanup;
git_vector_insert(&list, commit);
}
commit = git_revwalk__commit_lookup(walk, &input_array[0]);
if (commit == NULL)
goto cleanup;
if (git_merge__bases_many(&result, walk, commit, &list) < 0)
goto cleanup;
if (!result) {
giterr_set(GITERR_MERGE, "No merge base found");
error = GIT_ENOTFOUND;
goto cleanup;
}
git_oid_cpy(out, &result->item->oid);
error = 0;
cleanup:
git_commit_list_free(&result);
git_revwalk_free(walk);
git_vector_free(&list);
return error;
}
int git_merge_base(git_oid *out, git_repository *repo, const git_oid *one, const git_oid *two)
{
git_revwalk *walk;
git_vector list;
git_commit_list *result = NULL;
git_commit_list_node *commit;
void *contents[1];
if (git_revwalk_new(&walk, repo) < 0)
return -1;
commit = git_revwalk__commit_lookup(walk, two);
if (commit == NULL)
goto on_error;
/* This is just one value, so we can do it on the stack */
memset(&list, 0x0, sizeof(git_vector));
contents[0] = commit;
list.length = 1;
list.contents = contents;
commit = git_revwalk__commit_lookup(walk, one);
if (commit == NULL)
goto on_error;
if (git_merge__bases_many(&result, walk, commit, &list) < 0)
goto on_error;
if (!result) {
git_revwalk_free(walk);
giterr_set(GITERR_MERGE, "No merge base found");
return GIT_ENOTFOUND;
}
git_oid_cpy(out, &result->item->oid);
git_commit_list_free(&result);
git_revwalk_free(walk);
return 0;
on_error:
git_revwalk_free(walk);
return -1;
}
static int interesting(git_pqueue *list)
{
size_t i;
for (i = 0; i < git_pqueue_size(list); i++) {
git_commit_list_node *commit = git_pqueue_get(list, i);
if ((commit->flags & STALE) == 0)
return 1;
}
return 0;
}
int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_list_node *one, git_vector *twos)
{
int error;
unsigned int i;
git_commit_list_node *two;
git_commit_list *result = NULL, *tmp = NULL;
git_pqueue list;
/* if the commit is repeated, we have a our merge base already */
git_vector_foreach(twos, i, two) {
if (one == two)
return git_commit_list_insert(one, out) ? 0 : -1;
}
if (git_pqueue_init(&list, 0, twos->length * 2, git_commit_list_time_cmp) < 0)
return -1;
if (git_commit_list_parse(walk, one) < 0)
return -1;
one->flags |= PARENT1;
if (git_pqueue_insert(&list, one) < 0)
return -1;
git_vector_foreach(twos, i, two) {
git_commit_list_parse(walk, two);
two->flags |= PARENT2;
if (git_pqueue_insert(&list, two) < 0)
return -1;
}
/* as long as there are non-STALE commits */
while (interesting(&list)) {
git_commit_list_node *commit = git_pqueue_pop(&list);
int flags;
if (commit == NULL)
break;
flags = commit->flags & (PARENT1 | PARENT2 | STALE);
if (flags == (PARENT1 | PARENT2)) {
if (!(commit->flags & RESULT)) {
commit->flags |= RESULT;
if (git_commit_list_insert(commit, &result) == NULL)
return -1;
}
/* we mark the parents of a merge stale */
flags |= STALE;
}
for (i = 0; i < commit->out_degree; i++) {
git_commit_list_node *p = commit->parents[i];
if ((p->flags & flags) == flags)
continue;
if ((error = git_commit_list_parse(walk, p)) < 0)
return error;
p->flags |= flags;
if (git_pqueue_insert(&list, p) < 0)
return -1;
}
}
git_pqueue_free(&list);
/* filter out any stale commits in the results */
tmp = result;
result = NULL;
while (tmp) {
struct git_commit_list *next = tmp->next;
if (!(tmp->item->flags & STALE))
if (git_commit_list_insert_by_date(tmp->item, &result) == NULL)
return -1;
git__free(tmp);
tmp = next;
}
*out = result;
return 0;
}
int git_repository_mergehead_foreach(
git_repository *repo,
git_repository_mergehead_foreach_cb cb,
void *payload)
{
git_buf merge_head_path = GIT_BUF_INIT, merge_head_file = GIT_BUF_INIT;
char *buffer, *line;
size_t line_num = 1;
git_oid oid;
int error = 0;
assert(repo && cb);
if ((error = git_buf_joinpath(&merge_head_path, repo->path_repository,
GIT_MERGE_HEAD_FILE)) < 0)
return error;
if ((error = git_futils_readbuffer(&merge_head_file,
git_buf_cstr(&merge_head_path))) < 0)
goto cleanup;
buffer = merge_head_file.ptr;
while ((line = git__strsep(&buffer, "\n")) != NULL) {
if (strlen(line) != GIT_OID_HEXSZ) {
giterr_set(GITERR_INVALID, "Unable to parse OID - invalid length");
error = -1;
goto cleanup;
}
if ((error = git_oid_fromstr(&oid, line)) < 0)
goto cleanup;
if ((error = cb(&oid, payload)) != 0) {
giterr_set_after_callback(error);
goto cleanup;
}
++line_num;
}
if (*buffer) {
giterr_set(GITERR_MERGE, "No EOL at line %d", line_num);
error = -1;
goto cleanup;
}
cleanup:
git_buf_free(&merge_head_path);
git_buf_free(&merge_head_file);
return error;
}
GIT_INLINE(int) index_entry_cmp(const git_index_entry *a, const git_index_entry *b)
{
int value = 0;
if (a->path == NULL)
return (b->path == NULL) ? 0 : 1;
if ((value = a->mode - b->mode) == 0 &&
(value = git_oid__cmp(&a->id, &b->id)) == 0)
value = strcmp(a->path, b->path);
return value;
}
/* Conflict resolution */
static int merge_conflict_resolve_trivial(
int *resolved,
git_merge_diff_list *diff_list,
const git_merge_diff *conflict)
{
int ours_empty, theirs_empty;
int ours_changed, theirs_changed, ours_theirs_differ;
git_index_entry const *result = NULL;
int error = 0;
assert(resolved && diff_list && conflict);
*resolved = 0;
if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
return 0;
if (conflict->our_status == GIT_DELTA_RENAMED ||
conflict->their_status == GIT_DELTA_RENAMED)
return 0;
ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED);
theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED);
ours_theirs_differ = ours_changed && theirs_changed &&
index_entry_cmp(&conflict->our_entry, &conflict->their_entry);
/*
* Note: with only one ancestor, some cases are not distinct:
*
* 16: ancest:anc1/anc2, head:anc1, remote:anc2 = result:no merge
* 3: ancest:(empty)^, head:head, remote:(empty) = result:no merge
* 2: ancest:(empty)^, head:(empty), remote:remote = result:no merge
*
* Note that the two cases that take D/F conflicts into account
* specifically do not need to be explicitly tested, as D/F conflicts
* would fail the *empty* test:
*
* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head
* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote
*
* Note that many of these cases need not be explicitly tested, as
* they simply degrade to "all different" cases (eg, 11):
*
* 4: ancest:(empty)^, head:head, remote:remote = result:no merge
* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge
* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge
* 11: ancest:ancest+, head:head, remote:remote = result:no merge
*/
/* 5ALT: ancest:*, head:head, remote:head = result:head */
if (ours_changed && !ours_empty && !ours_theirs_differ)
result = &conflict->our_entry;
/* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
else if (ours_changed && ours_empty && theirs_empty)
*resolved = 0;
/* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
else if (ours_empty && !theirs_changed)
*resolved = 0;
/* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
else if (!ours_changed && theirs_empty)
*resolved = 0;
/* 13: ancest:ancest+, head:head, remote:ancest = result:head */
else if (ours_changed && !theirs_changed)
result = &conflict->our_entry;
/* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
else if (!ours_changed && theirs_changed)
result = &conflict->their_entry;
else
*resolved = 0;
if (result != NULL &&
GIT_MERGE_INDEX_ENTRY_EXISTS(*result) &&
(error = git_vector_insert(&diff_list->staged, (void *)result)) >= 0)
*resolved = 1;
/* Note: trivial resolution does not update the REUC. */
return error;
}
static int merge_conflict_resolve_one_removed(
int *resolved,
git_merge_diff_list *diff_list,
const git_merge_diff *conflict)
{
int ours_empty, theirs_empty;
int ours_changed, theirs_changed;
int error = 0;
assert(resolved && diff_list && conflict);
*resolved = 0;
if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE ||
conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
return 0;
ours_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry);
theirs_empty = !GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry);
ours_changed = (conflict->our_status != GIT_DELTA_UNMODIFIED);
theirs_changed = (conflict->their_status != GIT_DELTA_UNMODIFIED);
/* Removed in both */
if (ours_changed && ours_empty && theirs_empty)
*resolved = 1;
/* Removed in ours */
else if (ours_empty && !theirs_changed)
*resolved = 1;
/* Removed in theirs */
else if (!ours_changed && theirs_empty)
*resolved = 1;
if (*resolved)
git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
return error;
}
static int merge_conflict_resolve_one_renamed(
int *resolved,
git_merge_diff_list *diff_list,
const git_merge_diff *conflict)
{
int ours_renamed, theirs_renamed;
int ours_changed, theirs_changed;
git_index_entry *merged;
int error = 0;
assert(resolved && diff_list && conflict);
*resolved = 0;
if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) ||
!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry))
return 0;
ours_renamed = (conflict->our_status == GIT_DELTA_RENAMED);
theirs_renamed = (conflict->their_status == GIT_DELTA_RENAMED);
if (!ours_renamed && !theirs_renamed)
return 0;
/* Reject one file in a 2->1 conflict */
if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 ||
conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2 ||
conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
return 0;
ours_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->our_entry.id) != 0);
theirs_changed = (git_oid__cmp(&conflict->ancestor_entry.id, &conflict->their_entry.id) != 0);
/* if both are modified (and not to a common target) require a merge */
if (ours_changed && theirs_changed &&
git_oid__cmp(&conflict->our_entry.id, &conflict->their_entry.id) != 0)
return 0;
if ((merged = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
return -1;
if (ours_changed)
memcpy(merged, &conflict->our_entry, sizeof(git_index_entry));
else
memcpy(merged, &conflict->their_entry, sizeof(git_index_entry));
if (ours_renamed)
merged->path = conflict->our_entry.path;
else
merged->path = conflict->their_entry.path;
*resolved = 1;
git_vector_insert(&diff_list->staged, merged);
git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
return error;
}
static int merge_conflict_resolve_automerge(
int *resolved,
git_merge_diff_list *diff_list,
const git_merge_diff *conflict,
unsigned int merge_file_favor)
{
git_merge_file_options merge_file_opts = GIT_MERGE_FILE_OPTIONS_INIT;
git_merge_file_input ancestor = GIT_MERGE_FILE_INPUT_INIT,
ours = GIT_MERGE_FILE_INPUT_INIT,
theirs = GIT_MERGE_FILE_INPUT_INIT;
git_merge_file_result result = GIT_MERGE_FILE_RESULT_INIT;
git_index_entry *index_entry;
git_odb *odb = NULL;
git_oid automerge_oid;
int error = 0;
assert(resolved && diff_list && conflict);
*resolved = 0;
merge_file_opts.favor = merge_file_favor;
/* Reject D/F conflicts */
if (conflict->type == GIT_MERGE_DIFF_DIRECTORY_FILE)
return 0;
/* Reject submodules. */
if (S_ISGITLINK(conflict->ancestor_entry.mode) ||
S_ISGITLINK(conflict->our_entry.mode) ||
S_ISGITLINK(conflict->their_entry.mode))
return 0;
/* Reject link/file conflicts. */
if ((S_ISLNK(conflict->ancestor_entry.mode) ^ S_ISLNK(conflict->our_entry.mode)) ||
(S_ISLNK(conflict->ancestor_entry.mode) ^ S_ISLNK(conflict->their_entry.mode)))
return 0;
/* Reject name conflicts */
if (conflict->type == GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1 ||
conflict->type == GIT_MERGE_DIFF_RENAMED_ADDED)
return 0;
if ((conflict->our_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED &&
(conflict->their_status & GIT_DELTA_RENAMED) == GIT_DELTA_RENAMED &&
strcmp(conflict->ancestor_entry.path, conflict->their_entry.path) != 0)
return 0;
/* Reject binary conflicts */
if (conflict->binary)
return 0;
if ((error = git_repository_odb(&odb, diff_list->repo)) < 0 ||
(error = git_merge_file_input_from_index_entry(&ancestor, diff_list->repo, &conflict->ancestor_entry)) < 0 ||
(error = git_merge_file_input_from_index_entry(&ours, diff_list->repo, &conflict->our_entry)) < 0 ||
(error = git_merge_file_input_from_index_entry(&theirs, diff_list->repo, &conflict->their_entry)) < 0 ||
(error = git_merge_files(&result, &ancestor, &ours, &theirs, &merge_file_opts)) < 0 ||
!result.automergeable ||
(error = git_odb_write(&automerge_oid, odb, result.data, result.len, GIT_OBJ_BLOB)) < 0)
goto done;
if ((index_entry = git_pool_malloc(&diff_list->pool, sizeof(git_index_entry))) == NULL)
GITERR_CHECK_ALLOC(index_entry);
index_entry->path = git_pool_strdup(&diff_list->pool, result.path);
GITERR_CHECK_ALLOC(index_entry->path);
index_entry->file_size = result.len;
index_entry->mode = result.mode;
git_oid_cpy(&index_entry->id, &automerge_oid);
git_vector_insert(&diff_list->staged, index_entry);
git_vector_insert(&diff_list->resolved, (git_merge_diff *)conflict);
*resolved = 1;
done:
git_merge_file_input_free(&ancestor);
git_merge_file_input_free(&ours);
git_merge_file_input_free(&theirs);
git_merge_file_result_free(&result);
git_odb_free(odb);
return error;
}
static int merge_conflict_resolve(
int *out,
git_merge_diff_list *diff_list,
const git_merge_diff *conflict,
unsigned int merge_file_favor)
{
int resolved = 0;
int error = 0;
*out = 0;
if ((error = merge_conflict_resolve_trivial(&resolved, diff_list, conflict)) < 0)
goto done;
if (!resolved && (error = merge_conflict_resolve_one_removed(&resolved, diff_list, conflict)) < 0)
goto done;
if (!resolved && (error = merge_conflict_resolve_one_renamed(&resolved, diff_list, conflict)) < 0)
goto done;
if (!resolved && (error = merge_conflict_resolve_automerge(&resolved, diff_list, conflict, merge_file_favor)) < 0)
goto done;
*out = resolved;
done:
return error;
}
/* Rename detection and coalescing */
struct merge_diff_similarity {
unsigned char similarity;
size_t other_idx;
};
static int index_entry_similarity_exact(
git_repository *repo,
git_index_entry *a,
size_t a_idx,
git_index_entry *b,
size_t b_idx,
void **cache,
const git_merge_tree_opts *opts)
{
GIT_UNUSED(repo);
GIT_UNUSED(a_idx);
GIT_UNUSED(b_idx);
GIT_UNUSED(cache);
GIT_UNUSED(opts);
if (git_oid__cmp(&a->id, &b->id) == 0)
return 100;
return 0;
}
static int index_entry_similarity_calc(
void **out,
git_repository *repo,
git_index_entry *entry,
const git_merge_tree_opts *opts)
{
git_blob *blob;
git_diff_file diff_file = {{{0}}};
git_off_t blobsize;
int error;
*out = NULL;
if ((error = git_blob_lookup(&blob, repo, &entry->id)) < 0)
return error;
git_oid_cpy(&diff_file.id, &entry->id);
diff_file.path = entry->path;
diff_file.size = entry->file_size;
diff_file.mode = entry->mode;
diff_file.flags = 0;
blobsize = git_blob_rawsize(blob);
/* file too big for rename processing */
if (!git__is_sizet(blobsize))
return 0;
error = opts->metric->buffer_signature(out, &diff_file,
git_blob_rawcontent(blob), (size_t)blobsize,
opts->metric->payload);
git_blob_free(blob);
return error;
}
static int index_entry_similarity_inexact(
git_repository *repo,
git_index_entry *a,
size_t a_idx,
git_index_entry *b,
size_t b_idx,
void **cache,
const git_merge_tree_opts *opts)
{
int score = 0;
int error = 0;
if (GIT_MODE_TYPE(a->mode) != GIT_MODE_TYPE(b->mode))
return 0;
/* update signature cache if needed */
if (!cache[a_idx] && (error = index_entry_similarity_calc(&cache[a_idx], repo, a, opts)) < 0)
return error;
if (!cache[b_idx] && (error = index_entry_similarity_calc(&cache[b_idx], repo, b, opts)) < 0)
return error;
/* some metrics may not wish to process this file (too big / too small) */
if (!cache[a_idx] || !cache[b_idx])
return 0;
/* compare signatures */
if (opts->metric->similarity(
&score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0)
return -1;
/* clip score */
if (score < 0)
score = 0;
else if (score > 100)
score = 100;
return score;
}
static int merge_diff_mark_similarity(
git_repository *repo,
git_merge_diff_list *diff_list,
struct merge_diff_similarity *similarity_ours,
struct merge_diff_similarity *similarity_theirs,
int (*similarity_fn)(git_repository *, git_index_entry *, size_t, git_index_entry *, size_t, void **, const git_merge_tree_opts *),
void **cache,
const git_merge_tree_opts *opts)
{
size_t i, j;
git_merge_diff *conflict_src, *conflict_tgt;
int similarity;
git_vector_foreach(&diff_list->conflicts, i, conflict_src) {
/* Items can be the source of a rename iff they have an item in the
* ancestor slot and lack an item in the ours or theirs slot. */
if (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->ancestor_entry) ||
(GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry) &&
GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)))
continue;
git_vector_foreach(&diff_list->conflicts, j, conflict_tgt) {
size_t our_idx = diff_list->conflicts.length + j;
size_t their_idx = (diff_list->conflicts.length * 2) + j;
if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->ancestor_entry))
continue;
if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->our_entry) &&
!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->our_entry)) {
similarity = similarity_fn(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->our_entry, our_idx, cache, opts);
if (similarity == GIT_EBUFS)
continue;
else if (similarity < 0)
return similarity;
if (similarity > similarity_ours[i].similarity &&
similarity > similarity_ours[j].similarity) {
/* Clear previous best similarity */
if (similarity_ours[i].similarity > 0)
similarity_ours[similarity_ours[i].other_idx].similarity = 0;
if (similarity_ours[j].similarity > 0)
similarity_ours[similarity_ours[j].other_idx].similarity = 0;
similarity_ours[i].similarity = similarity;
similarity_ours[i].other_idx = j;
similarity_ours[j].similarity = similarity;
similarity_ours[j].other_idx = i;
}
}
if (GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_tgt->their_entry) &&
!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict_src->their_entry)) {
similarity = similarity_fn(repo, &conflict_src->ancestor_entry, i, &conflict_tgt->their_entry, their_idx, cache, opts);
if (similarity > similarity_theirs[i].similarity &&
similarity > similarity_theirs[j].similarity) {
/* Clear previous best similarity */
if (similarity_theirs[i].similarity > 0)
similarity_theirs[similarity_theirs[i].other_idx].similarity = 0;
if (similarity_theirs[j].similarity > 0)
similarity_theirs[similarity_theirs[j].other_idx].similarity = 0;
similarity_theirs[i].similarity = similarity;
similarity_theirs[i].other_idx = j;
similarity_theirs[j].similarity = similarity;
similarity_theirs[j].other_idx = i;
}
}
}
}
return 0;
}
/*
* Rename conflicts:
*
* Ancestor Ours Theirs
*
* 0a A A A No rename
* b A A* A No rename (ours was rewritten)
* c A A A* No rename (theirs rewritten)
* 1a A A B[A] Rename or rename/edit
* b A B[A] A (automergeable)
* 2 A B[A] B[A] Both renamed (automergeable)
* 3a A B[A] Rename/delete
* b A B[A] (same)
* 4a A B[A] B Rename/add [B~ours B~theirs]
* b A B B[A] (same)
* 5 A B[A] C[A] Both renamed ("1 -> 2")
* 6 A C[A] Both renamed ("2 -> 1")
* B C[B] [C~ours C~theirs] (automergeable)
*/
static void merge_diff_mark_rename_conflict(
git_merge_diff_list *diff_list,
struct merge_diff_similarity *similarity_ours,
bool ours_renamed,
size_t ours_source_idx,
struct merge_diff_similarity *similarity_theirs,
bool theirs_renamed,
size_t theirs_source_idx,
git_merge_diff *target,
const git_merge_tree_opts *opts)
{
git_merge_diff *ours_source = NULL, *theirs_source = NULL;
if (ours_renamed)
ours_source = diff_list->conflicts.contents[ours_source_idx];
if (theirs_renamed)
theirs_source = diff_list->conflicts.contents[theirs_source_idx];
/* Detect 2->1 conflicts */
if (ours_renamed && theirs_renamed) {
/* Both renamed to the same target name. */
if (ours_source_idx == theirs_source_idx)
ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED;
else {
ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_2_TO_1;
}
} else if (ours_renamed) {
/* If our source was also renamed in theirs, this is a 1->2 */
if (similarity_theirs[ours_source_idx].similarity >= opts->rename_threshold)
ours_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2;
else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry)) {
ours_source->type = GIT_MERGE_DIFF_RENAMED_ADDED;
target->type = GIT_MERGE_DIFF_RENAMED_ADDED;
}
else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(ours_source->their_entry))
ours_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
else if (ours_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED)
ours_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED;
} else if (theirs_renamed) {
/* If their source was also renamed in ours, this is a 1->2 */
if (similarity_ours[theirs_source_idx].similarity >= opts->rename_threshold)
theirs_source->type = GIT_MERGE_DIFF_BOTH_RENAMED_1_TO_2;
else if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry)) {
theirs_source->type = GIT_MERGE_DIFF_RENAMED_ADDED;
target->type = GIT_MERGE_DIFF_RENAMED_ADDED;
}
else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(theirs_source->our_entry))
theirs_source->type = GIT_MERGE_DIFF_RENAMED_DELETED;
else if (theirs_source->type == GIT_MERGE_DIFF_MODIFIED_DELETED)
theirs_source->type = GIT_MERGE_DIFF_RENAMED_MODIFIED;
}
}
GIT_INLINE(void) merge_diff_coalesce_rename(
git_index_entry *source_entry,
git_delta_t *source_status,
git_index_entry *target_entry,
git_delta_t *target_status)
{
/* Coalesce the rename target into the rename source. */
memcpy(source_entry, target_entry, sizeof(git_index_entry));
*source_status = GIT_DELTA_RENAMED;
memset(target_entry, 0x0, sizeof(git_index_entry));
*target_status = GIT_DELTA_UNMODIFIED;
}
static void merge_diff_list_coalesce_renames(
git_merge_diff_list *diff_list,
struct merge_diff_similarity *similarity_ours,
struct merge_diff_similarity *similarity_theirs,
const git_merge_tree_opts *opts)
{
size_t i;
bool ours_renamed = 0, theirs_renamed = 0;
size_t ours_source_idx = 0, theirs_source_idx = 0;
git_merge_diff *ours_source, *theirs_source, *target;
for (i = 0; i < diff_list->conflicts.length; i++) {
target = diff_list->conflicts.contents[i];
ours_renamed = 0;
theirs_renamed = 0;
if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->our_entry) &&
similarity_ours[i].similarity >= opts->rename_threshold) {
ours_source_idx = similarity_ours[i].other_idx;
ours_source = diff_list->conflicts.contents[ours_source_idx];
merge_diff_coalesce_rename(
&ours_source->our_entry,
&ours_source->our_status,
&target->our_entry,
&target->our_status);
similarity_ours[ours_source_idx].similarity = 0;
similarity_ours[i].similarity = 0;
ours_renamed = 1;
}
/* insufficient to determine direction */
if (GIT_MERGE_INDEX_ENTRY_EXISTS(target->their_entry) &&
similarity_theirs[i].similarity >= opts->rename_threshold) {
theirs_source_idx = similarity_theirs[i].other_idx;
theirs_source = diff_list->conflicts.contents[theirs_source_idx];
merge_diff_coalesce_rename(
&theirs_source->their_entry,
&theirs_source->their_status,
&target->their_entry,
&target->their_status);
similarity_theirs[theirs_source_idx].similarity = 0;
similarity_theirs[i].similarity = 0;
theirs_renamed = 1;
}
merge_diff_mark_rename_conflict(diff_list,
similarity_ours, ours_renamed, ours_source_idx,
similarity_theirs, theirs_renamed, theirs_source_idx,
target, opts);
}
}
static int merge_diff_empty(const git_vector *conflicts, size_t idx)
{
git_merge_diff *conflict = conflicts->contents[idx];
return (!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->ancestor_entry) &&
!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->our_entry) &&
!GIT_MERGE_INDEX_ENTRY_EXISTS(conflict->their_entry));
}
static void merge_diff_list_count_candidates(
git_merge_diff_list *diff_list,
size_t *src_count,
size_t *tgt_count)
{
git_merge_diff *entry;
size_t i;
*src_count = 0;
*tgt_count = 0;
git_vector_foreach(&diff_list->conflicts, i, entry) {
if (GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry) &&
(!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->our_entry) ||
!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->their_entry)))
src_count++;
else if (!GIT_MERGE_INDEX_ENTRY_EXISTS(entry->ancestor_entry))
tgt_count++;
}
}
int git_merge_diff_list__find_renames(
git_repository *repo,
git_merge_diff_list *diff_list,
const git_merge_tree_opts *opts)
{
struct merge_diff_similarity *similarity_ours, *similarity_theirs;
void **cache = NULL;