-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-ort.c
5200 lines (4646 loc) · 166 KB
/
merge-ort.c
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
/*
* "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant
* as a drop-in replacement for the "recursive" merge strategy, allowing one
* to replace
*
* git merge [-s recursive]
*
* with
*
* git merge -s ort
*
* Note: git's parser allows the space between '-s' and its argument to be
* missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo",
* "cale", "peedy", or "ins" instead of "ort"?)
*/
#include "git-compat-util.h"
#include "merge-ort.h"
#include "alloc.h"
#include "advice.h"
#include "attr.h"
#include "cache-tree.h"
#include "commit.h"
#include "commit-reach.h"
#include "diff.h"
#include "diffcore.h"
#include "dir.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
#include "entry.h"
#include "merge-ll.h"
#include "match-trees.h"
#include "mem-pool.h"
#include "object-name.h"
#include "object-store-ll.h"
#include "oid-array.h"
#include "path.h"
#include "promisor-remote.h"
#include "read-cache-ll.h"
#include "refs.h"
#include "revision.h"
#include "sparse-index.h"
#include "strmap.h"
#include "trace2.h"
#include "tree.h"
#include "unpack-trees.h"
#include "xdiff-interface.h"
/*
* We have many arrays of size 3. Whenever we have such an array, the
* indices refer to one of the sides of the three-way merge. This is so
* pervasive that the constants 0, 1, and 2 are used in many places in the
* code (especially in arithmetic operations to find the other side's index
* or to compute a relevant mask), but sometimes these enum names are used
* to aid code clarity.
*
* See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
* referred to there is one of these three sides.
*/
enum merge_side {
MERGE_BASE = 0,
MERGE_SIDE1 = 1,
MERGE_SIDE2 = 2
};
static unsigned RESULT_INITIALIZED = 0x1abe11ed; /* unlikely accidental value */
struct traversal_callback_data {
unsigned long mask;
unsigned long dirmask;
struct name_entry names[3];
};
struct deferred_traversal_data {
/*
* possible_trivial_merges: directories to be explored only when needed
*
* possible_trivial_merges is a map of directory names to
* dir_rename_mask. When we detect that a directory is unchanged on
* one side, we can sometimes resolve the directory without recursing
* into it. Renames are the only things that can prevent such an
* optimization. However, for rename sources:
* - If no parent directory needed directory rename detection, then
* no path under such a directory can be a relevant_source.
* and for rename destinations:
* - If no cached rename has a target path under the directory AND
* - If there are no unpaired relevant_sources elsewhere in the
* repository
* then we don't need any path under this directory for a rename
* destination. The only way to know the last item above is to defer
* handling such directories until the end of collect_merge_info(),
* in handle_deferred_entries().
*
* For each we store dir_rename_mask, since that's the only bit of
* information we need, other than the path, to resume the recursive
* traversal.
*/
struct strintmap possible_trivial_merges;
/*
* trivial_merges_okay: if trivial directory merges are okay
*
* See possible_trivial_merges above. The "no unpaired
* relevant_sources elsewhere in the repository" is a single boolean
* per merge side, which we store here. Note that while 0 means no,
* 1 only means "maybe" rather than "yes"; we optimistically set it
* to 1 initially and only clear when we determine it is unsafe to
* do trivial directory merges.
*/
unsigned trivial_merges_okay;
/*
* target_dirs: ancestor directories of rename targets
*
* target_dirs contains all directory names that are an ancestor of
* any rename destination.
*/
struct strset target_dirs;
};
struct rename_info {
/*
* All variables that are arrays of size 3 correspond to data tracked
* for the sides in enum merge_side. Index 0 is almost always unused
* because we often only need to track information for MERGE_SIDE1 and
* MERGE_SIDE2 (MERGE_BASE can't have rename information since renames
* are determined relative to what changed since the MERGE_BASE).
*/
/*
* pairs: pairing of filenames from diffcore_rename()
*/
struct diff_queue_struct pairs[3];
/*
* dirs_removed: directories removed on a given side of history.
*
* The keys of dirs_removed[side] are the directories that were removed
* on the given side of history. The value of the strintmap for each
* directory is a value from enum dir_rename_relevance.
*/
struct strintmap dirs_removed[3];
/*
* dir_rename_count: tracking where parts of a directory were renamed to
*
* When files in a directory are renamed, they may not all go to the
* same location. Each strmap here tracks:
* old_dir => {new_dir => int}
* That is, dir_rename_count[side] is a strmap to a strintmap.
*/
struct strmap dir_rename_count[3];
/*
* dir_renames: computed directory renames
*
* This is a map of old_dir => new_dir and is derived in part from
* dir_rename_count.
*/
struct strmap dir_renames[3];
/*
* relevant_sources: deleted paths wanted in rename detection, and why
*
* relevant_sources is a set of deleted paths on each side of
* history for which we need rename detection. If a path is deleted
* on one side of history, we need to detect if it is part of a
* rename if either
* * the file is modified/deleted on the other side of history
* * we need to detect renames for an ancestor directory
* If neither of those are true, we can skip rename detection for
* that path. The reason is stored as a value from enum
* file_rename_relevance, as the reason can inform the algorithm in
* diffcore_rename_extended().
*/
struct strintmap relevant_sources[3];
struct deferred_traversal_data deferred[3];
/*
* dir_rename_mask:
* 0: optimization removing unmodified potential rename source okay
* 2 or 4: optimization okay, but must check for files added to dir
* 7: optimization forbidden; need rename source in case of dir rename
*/
unsigned dir_rename_mask:3;
/*
* callback_data_*: supporting data structures for alternate traversal
*
* We sometimes need to be able to traverse through all the files
* in a given tree before all immediate subdirectories within that
* tree. Since traverse_trees() doesn't do that naturally, we have
* a traverse_trees_wrapper() that stores any immediate
* subdirectories while traversing files, then traverses the
* immediate subdirectories later. These callback_data* variables
* store the information for the subdirectories so that we can do
* that traversal order.
*/
struct traversal_callback_data *callback_data;
int callback_data_nr, callback_data_alloc;
char *callback_data_traverse_path;
/*
* merge_trees: trees passed to the merge algorithm for the merge
*
* merge_trees records the trees passed to the merge algorithm. But,
* this data also is stored in merge_result->priv. If a sequence of
* merges are being done (such as when cherry-picking or rebasing),
* the next merge can look at this and re-use information from
* previous merges under certain circumstances.
*
* See also all the cached_* variables.
*/
struct tree *merge_trees[3];
/*
* cached_pairs_valid_side: which side's cached info can be reused
*
* See the description for merge_trees. For repeated merges, at most
* only one side's cached information can be used. Valid values:
* MERGE_SIDE2: cached data from side2 can be reused
* MERGE_SIDE1: cached data from side1 can be reused
* 0: no cached data can be reused
* -1: See redo_after_renames; both sides can be reused.
*/
int cached_pairs_valid_side;
/*
* cached_pairs: Caching of renames and deletions.
*
* These are mappings recording renames and deletions of individual
* files (not directories). They are thus a map from an old
* filename to either NULL (for deletions) or a new filename (for
* renames).
*/
struct strmap cached_pairs[3];
/*
* cached_target_names: just the destinations from cached_pairs
*
* We sometimes want a fast lookup to determine if a given filename
* is one of the destinations in cached_pairs. cached_target_names
* is thus duplicative information, but it provides a fast lookup.
*/
struct strset cached_target_names[3];
/*
* cached_irrelevant: Caching of rename_sources that aren't relevant.
*
* If we try to detect a rename for a source path and succeed, it's
* part of a rename. If we try to detect a rename for a source path
* and fail, then it's a delete. If we do not try to detect a rename
* for a path, then we don't know if it's a rename or a delete. If
* merge-ort doesn't think the path is relevant, then we just won't
* cache anything for that path. But there's a slight problem in
* that merge-ort can think a path is RELEVANT_LOCATION, but due to
* commit 9bd342137e ("diffcore-rename: determine which
* relevant_sources are no longer relevant", 2021-03-13),
* diffcore-rename can downgrade the path to RELEVANT_NO_MORE. To
* avoid excessive calls to diffcore_rename_extended() we still need
* to cache such paths, though we cannot record them as either
* renames or deletes. So we cache them here as a "turned out to be
* irrelevant *for this commit*" as they are often also irrelevant
* for subsequent commits, though we will have to do some extra
* checking to see whether such paths become relevant for rename
* detection when cherry-picking/rebasing subsequent commits.
*/
struct strset cached_irrelevant[3];
/*
* redo_after_renames: optimization flag for "restarting" the merge
*
* Sometimes it pays to detect renames, cache them, and then
* restart the merge operation from the beginning. The reason for
* this is that when we know where all the renames are, we know
* whether a certain directory has any paths under it affected --
* and if a directory is not affected then it permits us to do
* trivial tree merging in more cases. Doing trivial tree merging
* prevents the need to run process_entry() on every path
* underneath trees that can be trivially merged, and
* process_entry() is more expensive than collect_merge_info() --
* plus, the second collect_merge_info() will be much faster since
* it doesn't have to recurse into the relevant trees.
*
* Values for this flag:
* 0 = don't bother, not worth it (or conditions not yet checked)
* 1 = conditions for optimization met, optimization worthwhile
* 2 = we already did it (don't restart merge yet again)
*/
unsigned redo_after_renames;
/*
* needed_limit: value needed for inexact rename detection to run
*
* If the current rename limit wasn't high enough for inexact
* rename detection to run, this records the limit needed. Otherwise,
* this value remains 0.
*/
int needed_limit;
};
struct merge_options_internal {
/*
* paths: primary data structure in all of merge ort.
*
* The keys of paths:
* * are full relative paths from the toplevel of the repository
* (e.g. "drivers/firmware/raspberrypi.c").
* * store all relevant paths in the repo, both directories and
* files (e.g. drivers, drivers/firmware would also be included)
* * these keys serve to intern all the path strings, which allows
* us to do pointer comparison on directory names instead of
* strcmp; we just have to be careful to use the interned strings.
*
* The values of paths:
* * either a pointer to a merged_info, or a conflict_info struct
* * merged_info contains all relevant information for a
* non-conflicted entry.
* * conflict_info contains a merged_info, plus any additional
* information about a conflict such as the higher orders stages
* involved and the names of the paths those came from (handy
* once renames get involved).
* * a path may start "conflicted" (i.e. point to a conflict_info)
* and then a later step (e.g. three-way content merge) determines
* it can be cleanly merged, at which point it'll be marked clean
* and the algorithm will ignore any data outside the contained
* merged_info for that entry
* * If an entry remains conflicted, the merged_info portion of a
* conflict_info will later be filled with whatever version of
* the file should be placed in the working directory (e.g. an
* as-merged-as-possible variation that contains conflict markers).
*/
struct strmap paths;
/*
* conflicted: a subset of keys->values from "paths"
*
* conflicted is basically an optimization between process_entries()
* and record_conflicted_index_entries(); the latter could loop over
* ALL the entries in paths AGAIN and look for the ones that are
* still conflicted, but since process_entries() has to loop over
* all of them, it saves the ones it couldn't resolve in this strmap
* so that record_conflicted_index_entries() can iterate just the
* relevant entries.
*/
struct strmap conflicted;
/*
* pool: memory pool for fast allocation/deallocation
*
* We allocate room for lots of filenames and auxiliary data
* structures in merge_options_internal, and it tends to all be
* freed together too. Using a memory pool for these provides a
* nice speedup.
*/
struct mem_pool pool;
/*
* conflicts: logical conflicts and messages stored by _primary_ path
*
* This is a map of pathnames (a subset of the keys in "paths" above)
* to struct string_list, with each item's `util` containing a
* `struct logical_conflict_info`. Note, though, that for each path,
* it only stores the logical conflicts for which that path is the
* primary path; the path might be part of additional conflicts.
*/
struct strmap conflicts;
/*
* renames: various data relating to rename detection
*/
struct rename_info renames;
/*
* attr_index: hacky minimal index used for renormalization
*
* renormalization code _requires_ an index, though it only needs to
* find a .gitattributes file within the index. So, when
* renormalization is important, we create a special index with just
* that one file.
*/
struct index_state attr_index;
/*
* current_dir_name, toplevel_dir: temporary vars
*
* These are used in collect_merge_info_callback(), and will set the
* various merged_info.directory_name for the various paths we get;
* see documentation for that variable and the requirements placed on
* that field.
*/
const char *current_dir_name;
const char *toplevel_dir;
/* call_depth: recursion level counter for merging merge bases */
int call_depth;
/* field that holds submodule conflict information */
struct string_list conflicted_submodules;
};
struct conflicted_submodule_item {
char *abbrev;
int flag;
};
static void conflicted_submodule_item_free(void *util, const char *str UNUSED)
{
struct conflicted_submodule_item *item = util;
free(item->abbrev);
free(item);
}
struct version_info {
struct object_id oid;
unsigned short mode;
};
struct merged_info {
/* if is_null, ignore result. otherwise result has oid & mode */
struct version_info result;
unsigned is_null:1;
/*
* clean: whether the path in question is cleanly merged.
*
* see conflict_info.merged for more details.
*/
unsigned clean:1;
/*
* basename_offset: offset of basename of path.
*
* perf optimization to avoid recomputing offset of final '/'
* character in pathname (0 if no '/' in pathname).
*/
size_t basename_offset;
/*
* directory_name: containing directory name.
*
* Note that we assume directory_name is constructed such that
* strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
* i.e. string equality is equivalent to pointer equality. For this
* to hold, we have to be careful setting directory_name.
*/
const char *directory_name;
};
struct conflict_info {
/*
* merged: the version of the path that will be written to working tree
*
* WARNING: It is critical to check merged.clean and ensure it is 0
* before reading any conflict_info fields outside of merged.
* Allocated merge_info structs will always have clean set to 1.
* Allocated conflict_info structs will have merged.clean set to 0
* initially. The merged.clean field is how we know if it is safe
* to access other parts of conflict_info besides merged; if a
* conflict_info's merged.clean is changed to 1, the rest of the
* algorithm is not allowed to look at anything outside of the
* merged member anymore.
*/
struct merged_info merged;
/* oids & modes from each of the three trees for this path */
struct version_info stages[3];
/* pathnames for each stage; may differ due to rename detection */
const char *pathnames[3];
/* Whether this path is/was involved in a directory/file conflict */
unsigned df_conflict:1;
/*
* Whether this path is/was involved in a non-content conflict other
* than a directory/file conflict (e.g. rename/rename, rename/delete,
* file location based on possible directory rename).
*/
unsigned path_conflict:1;
/*
* For filemask and dirmask, the ith bit corresponds to whether the
* ith entry is a file (filemask) or a directory (dirmask). Thus,
* filemask & dirmask is always zero, and filemask | dirmask is at
* most 7 but can be less when a path does not appear as either a
* file or a directory on at least one side of history.
*
* Note that these masks are related to enum merge_side, as the ith
* entry corresponds to side i.
*
* These values come from a traverse_trees() call; more info may be
* found looking at tree-walk.h's struct traverse_info,
* particularly the documentation above the "fn" member (note that
* filemask = mask & ~dirmask from that documentation).
*/
unsigned filemask:3;
unsigned dirmask:3;
/*
* Optimization to track which stages match, to avoid the need to
* recompute it in multiple steps. Either 0 or at least 2 bits are
* set; if at least 2 bits are set, their corresponding stages match.
*/
unsigned match_mask:3;
};
enum conflict_and_info_types {
/* "Simple" conflicts and informational messages */
INFO_AUTO_MERGING = 0,
CONFLICT_CONTENTS, /* text file that failed to merge */
CONFLICT_BINARY,
CONFLICT_FILE_DIRECTORY,
CONFLICT_DISTINCT_MODES,
CONFLICT_MODIFY_DELETE,
/* Regular rename */
CONFLICT_RENAME_RENAME, /* same file renamed differently */
CONFLICT_RENAME_COLLIDES, /* rename/add or two files renamed to 1 */
CONFLICT_RENAME_DELETE,
/* Basic directory rename */
CONFLICT_DIR_RENAME_SUGGESTED,
INFO_DIR_RENAME_APPLIED,
/* Special directory rename cases */
INFO_DIR_RENAME_SKIPPED_DUE_TO_RERENAME,
CONFLICT_DIR_RENAME_FILE_IN_WAY,
CONFLICT_DIR_RENAME_COLLISION,
CONFLICT_DIR_RENAME_SPLIT,
/* Basic submodule */
INFO_SUBMODULE_FAST_FORWARDING,
CONFLICT_SUBMODULE_FAILED_TO_MERGE,
/* Special submodule cases broken out from FAILED_TO_MERGE */
CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION,
CONFLICT_SUBMODULE_NOT_INITIALIZED,
CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE,
CONFLICT_SUBMODULE_MAY_HAVE_REWINDS,
CONFLICT_SUBMODULE_NULL_MERGE_BASE,
CONFLICT_SUBMODULE_CORRUPT,
/* Keep this entry _last_ in the list */
NB_CONFLICT_TYPES,
};
/*
* Short description of conflict type, relied upon by external tools.
*
* We can add more entries, but DO NOT change any of these strings. Also,
* Order MUST match conflict_info_and_types.
*/
static const char *type_short_descriptions[] = {
/*** "Simple" conflicts and informational messages ***/
[INFO_AUTO_MERGING] = "Auto-merging",
[CONFLICT_CONTENTS] = "CONFLICT (contents)",
[CONFLICT_BINARY] = "CONFLICT (binary)",
[CONFLICT_FILE_DIRECTORY] = "CONFLICT (file/directory)",
[CONFLICT_DISTINCT_MODES] = "CONFLICT (distinct modes)",
[CONFLICT_MODIFY_DELETE] = "CONFLICT (modify/delete)",
/*** Regular rename ***/
[CONFLICT_RENAME_RENAME] = "CONFLICT (rename/rename)",
[CONFLICT_RENAME_COLLIDES] = "CONFLICT (rename involved in collision)",
[CONFLICT_RENAME_DELETE] = "CONFLICT (rename/delete)",
/*** Basic directory rename ***/
[CONFLICT_DIR_RENAME_SUGGESTED] =
"CONFLICT (directory rename suggested)",
[INFO_DIR_RENAME_APPLIED] = "Path updated due to directory rename",
/*** Special directory rename cases ***/
[INFO_DIR_RENAME_SKIPPED_DUE_TO_RERENAME] =
"Directory rename skipped since directory was renamed on both sides",
[CONFLICT_DIR_RENAME_FILE_IN_WAY] =
"CONFLICT (file in way of directory rename)",
[CONFLICT_DIR_RENAME_COLLISION] = "CONFLICT(directory rename collision)",
[CONFLICT_DIR_RENAME_SPLIT] = "CONFLICT(directory rename unclear split)",
/*** Basic submodule ***/
[INFO_SUBMODULE_FAST_FORWARDING] = "Fast forwarding submodule",
[CONFLICT_SUBMODULE_FAILED_TO_MERGE] = "CONFLICT (submodule)",
/*** Special submodule cases broken out from FAILED_TO_MERGE ***/
[CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION] =
"CONFLICT (submodule with possible resolution)",
[CONFLICT_SUBMODULE_NOT_INITIALIZED] =
"CONFLICT (submodule not initialized)",
[CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE] =
"CONFLICT (submodule history not available)",
[CONFLICT_SUBMODULE_MAY_HAVE_REWINDS] =
"CONFLICT (submodule may have rewinds)",
[CONFLICT_SUBMODULE_NULL_MERGE_BASE] =
"CONFLICT (submodule lacks merge base)",
[CONFLICT_SUBMODULE_CORRUPT] =
"CONFLICT (submodule corrupt)"
};
struct logical_conflict_info {
enum conflict_and_info_types type;
struct strvec paths;
};
/*** Function Grouping: various utility functions ***/
/*
* For the next three macros, see warning for conflict_info.merged.
*
* In each of the below, mi is a struct merged_info*, and ci was defined
* as a struct conflict_info* (but we need to verify ci isn't actually
* pointed at a struct merged_info*).
*
* INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
* VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
* ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
*/
#define INITIALIZE_CI(ci, mi) do { \
(ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
} while (0)
#define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
#define ASSIGN_AND_VERIFY_CI(ci, mi) do { \
(ci) = (struct conflict_info *)(mi); \
assert((ci) && !(mi)->clean); \
} while (0)
static void free_strmap_strings(struct strmap *map)
{
struct hashmap_iter iter;
struct strmap_entry *entry;
strmap_for_each_entry(map, &iter, entry) {
free((char*)entry->key);
}
}
static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
int reinitialize)
{
struct rename_info *renames = &opti->renames;
int i;
void (*strmap_clear_func)(struct strmap *, int) =
reinitialize ? strmap_partial_clear : strmap_clear;
void (*strintmap_clear_func)(struct strintmap *) =
reinitialize ? strintmap_partial_clear : strintmap_clear;
void (*strset_clear_func)(struct strset *) =
reinitialize ? strset_partial_clear : strset_clear;
strmap_clear_func(&opti->paths, 0);
/*
* All keys and values in opti->conflicted are a subset of those in
* opti->paths. We don't want to deallocate anything twice, so we
* don't free the keys and we pass 0 for free_values.
*/
strmap_clear_func(&opti->conflicted, 0);
if (opti->attr_index.cache_nr) /* true iff opt->renormalize */
discard_index(&opti->attr_index);
/* Free memory used by various renames maps */
for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) {
strintmap_clear_func(&renames->dirs_removed[i]);
strmap_clear_func(&renames->dir_renames[i], 0);
strintmap_clear_func(&renames->relevant_sources[i]);
if (!reinitialize)
assert(renames->cached_pairs_valid_side == 0);
if (i != renames->cached_pairs_valid_side &&
-1 != renames->cached_pairs_valid_side) {
strset_clear_func(&renames->cached_target_names[i]);
strmap_clear_func(&renames->cached_pairs[i], 1);
strset_clear_func(&renames->cached_irrelevant[i]);
partial_clear_dir_rename_count(&renames->dir_rename_count[i]);
if (!reinitialize)
strmap_clear(&renames->dir_rename_count[i], 1);
}
}
for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) {
strintmap_clear_func(&renames->deferred[i].possible_trivial_merges);
strset_clear_func(&renames->deferred[i].target_dirs);
renames->deferred[i].trivial_merges_okay = 1; /* 1 == maybe */
}
renames->cached_pairs_valid_side = 0;
renames->dir_rename_mask = 0;
if (!reinitialize) {
struct hashmap_iter iter;
struct strmap_entry *e;
/* Release and free each strbuf found in output */
strmap_for_each_entry(&opti->conflicts, &iter, e) {
struct string_list *list = e->value;
for (int i = 0; i < list->nr; i++) {
struct logical_conflict_info *info =
list->items[i].util;
strvec_clear(&info->paths);
}
/*
* While strictly speaking we don't need to
* free(conflicts) here because we could pass
* free_values=1 when calling strmap_clear() on
* opti->conflicts, that would require strmap_clear
* to do another strmap_for_each_entry() loop, so we
* just free it while we're iterating anyway.
*/
string_list_clear(list, 1);
free(list);
}
strmap_clear(&opti->conflicts, 0);
}
mem_pool_discard(&opti->pool, 0);
string_list_clear_func(&opti->conflicted_submodules,
conflicted_submodule_item_free);
/* Clean out callback_data as well. */
FREE_AND_NULL(renames->callback_data);
renames->callback_data_nr = renames->callback_data_alloc = 0;
}
static void format_commit(struct strbuf *sb,
int indent,
struct repository *repo,
struct commit *commit)
{
struct merge_remote_desc *desc;
struct pretty_print_context ctx = {0};
ctx.abbrev = DEFAULT_ABBREV;
strbuf_addchars(sb, ' ', indent);
desc = merge_remote_util(commit);
if (desc) {
strbuf_addf(sb, "virtual %s\n", desc->name);
return;
}
repo_format_commit_message(repo, commit, "%h %s", sb, &ctx);
strbuf_addch(sb, '\n');
}
__attribute__((format (printf, 8, 9)))
static void path_msg(struct merge_options *opt,
enum conflict_and_info_types type,
int omittable_hint, /* skippable under --remerge-diff */
const char *primary_path,
const char *other_path_1, /* may be NULL */
const char *other_path_2, /* may be NULL */
struct string_list *other_paths, /* may be NULL */
const char *fmt, ...)
{
va_list ap;
struct string_list *path_conflicts;
struct logical_conflict_info *info;
struct strbuf buf = STRBUF_INIT;
struct strbuf *dest;
struct strbuf tmp = STRBUF_INIT;
/* Sanity checks */
assert(omittable_hint ==
!starts_with(type_short_descriptions[type], "CONFLICT") ||
type == CONFLICT_DIR_RENAME_SUGGESTED);
if (opt->record_conflict_msgs_as_headers && omittable_hint)
return; /* Do not record mere hints in headers */
if (opt->priv->call_depth && opt->verbosity < 5)
return; /* Ignore messages from inner merges */
/* Ensure path_conflicts (ptr to array of logical_conflict) allocated */
path_conflicts = strmap_get(&opt->priv->conflicts, primary_path);
if (!path_conflicts) {
path_conflicts = xmalloc(sizeof(*path_conflicts));
string_list_init_dup(path_conflicts);
strmap_put(&opt->priv->conflicts, primary_path, path_conflicts);
}
/* Add a logical_conflict at the end to store info from this call */
info = xcalloc(1, sizeof(*info));
info->type = type;
strvec_init(&info->paths);
/* Handle the list of paths */
strvec_push(&info->paths, primary_path);
if (other_path_1)
strvec_push(&info->paths, other_path_1);
if (other_path_2)
strvec_push(&info->paths, other_path_2);
if (other_paths)
for (int i = 0; i < other_paths->nr; i++)
strvec_push(&info->paths, other_paths->items[i].string);
/* Handle message and its format, in normal case */
dest = (opt->record_conflict_msgs_as_headers ? &tmp : &buf);
va_start(ap, fmt);
if (opt->priv->call_depth) {
strbuf_addchars(dest, ' ', 2);
strbuf_addstr(dest, "From inner merge:");
strbuf_addchars(dest, ' ', opt->priv->call_depth * 2);
}
strbuf_vaddf(dest, fmt, ap);
va_end(ap);
/* Handle specialized formatting of message under --remerge-diff */
if (opt->record_conflict_msgs_as_headers) {
int i_sb = 0, i_tmp = 0;
/* Start with the specified prefix */
if (opt->msg_header_prefix)
strbuf_addf(&buf, "%s ", opt->msg_header_prefix);
/* Copy tmp to sb, adding spaces after newlines */
strbuf_grow(&buf, buf.len + 2*tmp.len); /* more than sufficient */
for (; i_tmp < tmp.len; i_tmp++, i_sb++) {
/* Copy next character from tmp to sb */
buf.buf[buf.len + i_sb] = tmp.buf[i_tmp];
/* If we copied a newline, add a space */
if (tmp.buf[i_tmp] == '\n')
buf.buf[++i_sb] = ' ';
}
/* Update length and ensure it's NUL-terminated */
buf.len += i_sb;
buf.buf[buf.len] = '\0';
strbuf_release(&tmp);
}
string_list_append_nodup(path_conflicts, strbuf_detach(&buf, NULL))
->util = info;
}
static struct diff_filespec *pool_alloc_filespec(struct mem_pool *pool,
const char *path)
{
/* Similar to alloc_filespec(), but allocate from pool and reuse path */
struct diff_filespec *spec;
spec = mem_pool_calloc(pool, 1, sizeof(*spec));
spec->path = (char*)path; /* spec won't modify it */
spec->count = 1;
spec->is_binary = -1;
return spec;
}
static struct diff_filepair *pool_diff_queue(struct mem_pool *pool,
struct diff_queue_struct *queue,
struct diff_filespec *one,
struct diff_filespec *two)
{
/* Same code as diff_queue(), except allocate from pool */
struct diff_filepair *dp;
dp = mem_pool_calloc(pool, 1, sizeof(*dp));
dp->one = one;
dp->two = two;
if (queue)
diff_q(queue, dp);
return dp;
}
/* add a string to a strbuf, but converting "/" to "_" */
static void add_flattened_path(struct strbuf *out, const char *s)
{
size_t i = out->len;
strbuf_addstr(out, s);
for (; i < out->len; i++)
if (out->buf[i] == '/')
out->buf[i] = '_';
}
static char *unique_path(struct merge_options *opt,
const char *path,
const char *branch)
{
char *ret = NULL;
struct strbuf newpath = STRBUF_INIT;
int suffix = 0;
size_t base_len;
struct strmap *existing_paths = &opt->priv->paths;
strbuf_addf(&newpath, "%s~", path);
add_flattened_path(&newpath, branch);
base_len = newpath.len;
while (strmap_contains(existing_paths, newpath.buf)) {
strbuf_setlen(&newpath, base_len);
strbuf_addf(&newpath, "_%d", suffix++);
}
/* Track the new path in our memory pool */
ret = mem_pool_alloc(&opt->priv->pool, newpath.len + 1);
memcpy(ret, newpath.buf, newpath.len + 1);
strbuf_release(&newpath);
return ret;
}
/*** Function Grouping: functions related to collect_merge_info() ***/
static int traverse_trees_wrapper_callback(int n,
unsigned long mask,
unsigned long dirmask,
struct name_entry *names,
struct traverse_info *info)
{
struct merge_options *opt = info->data;
struct rename_info *renames = &opt->priv->renames;
unsigned filemask = mask & ~dirmask;
assert(n==3);
if (!renames->callback_data_traverse_path)
renames->callback_data_traverse_path = xstrdup(info->traverse_path);
if (filemask && filemask == renames->dir_rename_mask)
renames->dir_rename_mask = 0x07;
ALLOC_GROW(renames->callback_data, renames->callback_data_nr + 1,
renames->callback_data_alloc);
renames->callback_data[renames->callback_data_nr].mask = mask;
renames->callback_data[renames->callback_data_nr].dirmask = dirmask;
COPY_ARRAY(renames->callback_data[renames->callback_data_nr].names,
names, 3);
renames->callback_data_nr++;
return mask;
}
/*
* Much like traverse_trees(), BUT:
* - read all the tree entries FIRST, saving them
* - note that the above step provides an opportunity to compute necessary
* additional details before the "real" traversal
* - loop through the saved entries and call the original callback on them
*/
static int traverse_trees_wrapper(struct index_state *istate,
int n,
struct tree_desc *t,
struct traverse_info *info)
{
int ret, i, old_offset;
traverse_callback_t old_fn;
char *old_callback_data_traverse_path;
struct merge_options *opt = info->data;
struct rename_info *renames = &opt->priv->renames;
assert(renames->dir_rename_mask == 2 || renames->dir_rename_mask == 4);
old_callback_data_traverse_path = renames->callback_data_traverse_path;
old_fn = info->fn;
old_offset = renames->callback_data_nr;
renames->callback_data_traverse_path = NULL;
info->fn = traverse_trees_wrapper_callback;
ret = traverse_trees(istate, n, t, info);
if (ret < 0)
return ret;
info->traverse_path = renames->callback_data_traverse_path;
info->fn = old_fn;
for (i = old_offset; i < renames->callback_data_nr; ++i) {
info->fn(n,
renames->callback_data[i].mask,
renames->callback_data[i].dirmask,
renames->callback_data[i].names,
info);
}
renames->callback_data_nr = old_offset;
free(renames->callback_data_traverse_path);
renames->callback_data_traverse_path = old_callback_data_traverse_path;
info->traverse_path = NULL;
return 0;
}
static void setup_path_info(struct merge_options *opt,
struct string_list_item *result,
const char *current_dir_name,
int current_dir_name_len,
char *fullpath, /* we'll take over ownership */
struct name_entry *names,
struct name_entry *merged_version,
unsigned is_null, /* boolean */
unsigned df_conflict, /* boolean */
unsigned filemask,
unsigned dirmask,
int resolved /* boolean */)
{
/* result->util is void*, so mi is a convenience typed variable */
struct merged_info *mi;
assert(!is_null || resolved);
assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
assert(resolved == (merged_version != NULL));
mi = mem_pool_calloc(&opt->priv->pool, 1,
resolved ? sizeof(struct merged_info) :