]>
Commit | Line | Data |
---|---|---|
17e5574b EN |
1 | /* |
2 | * "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant | |
3 | * as a drop-in replacement for the "recursive" merge strategy, allowing one | |
4 | * to replace | |
5 | * | |
6 | * git merge [-s recursive] | |
7 | * | |
8 | * with | |
9 | * | |
10 | * git merge -s ort | |
11 | * | |
12 | * Note: git's parser allows the space between '-s' and its argument to be | |
13 | * missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo", | |
14 | * "cale", "peedy", or "ins" instead of "ort"?) | |
15 | */ | |
16 | ||
17 | #include "cache.h" | |
18 | #include "merge-ort.h" | |
19 | ||
4296d8f1 | 20 | #include "alloc.h" |
ea305a68 | 21 | #include "attr.h" |
67845745 | 22 | #include "blob.h" |
ef2b3693 | 23 | #include "cache-tree.h" |
4296d8f1 | 24 | #include "commit.h" |
67845745 | 25 | #include "commit-reach.h" |
e4171b1b EN |
26 | #include "diff.h" |
27 | #include "diffcore.h" | |
6681ce5c | 28 | #include "dir.h" |
32a8f510 | 29 | #include "environment.h" |
f394e093 | 30 | #include "gettext.h" |
41771fa4 | 31 | #include "hex.h" |
7bec8e7f | 32 | #include "entry.h" |
f591c472 | 33 | #include "ll-merge.h" |
5bc07225 | 34 | #include "mem-pool.h" |
dabab1d6 | 35 | #include "object-name.h" |
ee4012dc | 36 | #include "object-store.h" |
6f2d7430 | 37 | #include "oid-array.h" |
2bff554b | 38 | #include "promisor-remote.h" |
4204cd59 | 39 | #include "revision.h" |
5b59c3db | 40 | #include "strmap.h" |
10a0d6ae | 41 | #include "submodule-config.h" |
c73cda76 | 42 | #include "submodule.h" |
74ea5c95 | 43 | #include "trace2.h" |
231e2dd4 | 44 | #include "tree.h" |
6681ce5c | 45 | #include "unpack-trees.h" |
c8017176 | 46 | #include "xdiff-interface.h" |
5b59c3db | 47 | |
d2bc1994 EN |
48 | /* |
49 | * We have many arrays of size 3. Whenever we have such an array, the | |
50 | * indices refer to one of the sides of the three-way merge. This is so | |
51 | * pervasive that the constants 0, 1, and 2 are used in many places in the | |
52 | * code (especially in arithmetic operations to find the other side's index | |
53 | * or to compute a relevant mask), but sometimes these enum names are used | |
54 | * to aid code clarity. | |
55 | * | |
56 | * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side" | |
57 | * referred to there is one of these three sides. | |
58 | */ | |
59 | enum merge_side { | |
60 | MERGE_BASE = 0, | |
61 | MERGE_SIDE1 = 1, | |
62 | MERGE_SIDE2 = 2 | |
63 | }; | |
64 | ||
19ceb486 EN |
65 | static unsigned RESULT_INITIALIZED = 0x1abe11ed; /* unlikely accidental value */ |
66 | ||
beb06145 EN |
67 | struct traversal_callback_data { |
68 | unsigned long mask; | |
69 | unsigned long dirmask; | |
70 | struct name_entry names[3]; | |
71 | }; | |
72 | ||
d478f567 EN |
73 | struct deferred_traversal_data { |
74 | /* | |
75 | * possible_trivial_merges: directories to be explored only when needed | |
76 | * | |
77 | * possible_trivial_merges is a map of directory names to | |
78 | * dir_rename_mask. When we detect that a directory is unchanged on | |
79 | * one side, we can sometimes resolve the directory without recursing | |
80 | * into it. Renames are the only things that can prevent such an | |
81 | * optimization. However, for rename sources: | |
82 | * - If no parent directory needed directory rename detection, then | |
83 | * no path under such a directory can be a relevant_source. | |
84 | * and for rename destinations: | |
85 | * - If no cached rename has a target path under the directory AND | |
86 | * - If there are no unpaired relevant_sources elsewhere in the | |
87 | * repository | |
88 | * then we don't need any path under this directory for a rename | |
89 | * destination. The only way to know the last item above is to defer | |
90 | * handling such directories until the end of collect_merge_info(), | |
91 | * in handle_deferred_entries(). | |
92 | * | |
93 | * For each we store dir_rename_mask, since that's the only bit of | |
94 | * information we need, other than the path, to resume the recursive | |
95 | * traversal. | |
96 | */ | |
97 | struct strintmap possible_trivial_merges; | |
98 | ||
99 | /* | |
100 | * trivial_merges_okay: if trivial directory merges are okay | |
101 | * | |
102 | * See possible_trivial_merges above. The "no unpaired | |
103 | * relevant_sources elsewhere in the repository" is a single boolean | |
104 | * per merge side, which we store here. Note that while 0 means no, | |
105 | * 1 only means "maybe" rather than "yes"; we optimistically set it | |
106 | * to 1 initially and only clear when we determine it is unsafe to | |
107 | * do trivial directory merges. | |
108 | */ | |
109 | unsigned trivial_merges_okay; | |
110 | ||
111 | /* | |
112 | * target_dirs: ancestor directories of rename targets | |
113 | * | |
114 | * target_dirs contains all directory names that are an ancestor of | |
115 | * any rename destination. | |
116 | */ | |
117 | struct strset target_dirs; | |
118 | }; | |
119 | ||
864075ec | 120 | struct rename_info { |
c09376d5 EN |
121 | /* |
122 | * All variables that are arrays of size 3 correspond to data tracked | |
123 | * for the sides in enum merge_side. Index 0 is almost always unused | |
124 | * because we often only need to track information for MERGE_SIDE1 and | |
125 | * MERGE_SIDE2 (MERGE_BASE can't have rename information since renames | |
126 | * are determined relative to what changed since the MERGE_BASE). | |
127 | */ | |
128 | ||
864075ec EN |
129 | /* |
130 | * pairs: pairing of filenames from diffcore_rename() | |
864075ec EN |
131 | */ |
132 | struct diff_queue_struct pairs[3]; | |
133 | ||
c09376d5 EN |
134 | /* |
135 | * dirs_removed: directories removed on a given side of history. | |
fb52938e EN |
136 | * |
137 | * The keys of dirs_removed[side] are the directories that were removed | |
138 | * on the given side of history. The value of the strintmap for each | |
139 | * directory is a value from enum dir_rename_relevance. | |
c09376d5 | 140 | */ |
a49b55d5 | 141 | struct strintmap dirs_removed[3]; |
c09376d5 EN |
142 | |
143 | /* | |
144 | * dir_rename_count: tracking where parts of a directory were renamed to | |
145 | * | |
146 | * When files in a directory are renamed, they may not all go to the | |
147 | * same location. Each strmap here tracks: | |
148 | * old_dir => {new_dir => int} | |
149 | * That is, dir_rename_count[side] is a strmap to a strintmap. | |
150 | */ | |
151 | struct strmap dir_rename_count[3]; | |
152 | ||
153 | /* | |
154 | * dir_renames: computed directory renames | |
155 | * | |
156 | * This is a map of old_dir => new_dir and is derived in part from | |
157 | * dir_rename_count. | |
158 | */ | |
159 | struct strmap dir_renames[3]; | |
160 | ||
32a56dfb | 161 | /* |
ec59da60 | 162 | * relevant_sources: deleted paths wanted in rename detection, and why |
32a56dfb EN |
163 | * |
164 | * relevant_sources is a set of deleted paths on each side of | |
165 | * history for which we need rename detection. If a path is deleted | |
166 | * on one side of history, we need to detect if it is part of a | |
167 | * rename if either | |
32a56dfb | 168 | * * the file is modified/deleted on the other side of history |
ec59da60 | 169 | * * we need to detect renames for an ancestor directory |
32a56dfb | 170 | * If neither of those are true, we can skip rename detection for |
ec59da60 EN |
171 | * that path. The reason is stored as a value from enum |
172 | * file_rename_relevance, as the reason can inform the algorithm in | |
173 | * diffcore_rename_extended(). | |
32a56dfb | 174 | */ |
a49b55d5 | 175 | struct strintmap relevant_sources[3]; |
32a56dfb | 176 | |
d478f567 EN |
177 | struct deferred_traversal_data deferred[3]; |
178 | ||
2fd9eda4 EN |
179 | /* |
180 | * dir_rename_mask: | |
181 | * 0: optimization removing unmodified potential rename source okay | |
182 | * 2 or 4: optimization okay, but must check for files added to dir | |
183 | * 7: optimization forbidden; need rename source in case of dir rename | |
184 | */ | |
185 | unsigned dir_rename_mask:3; | |
186 | ||
beb06145 EN |
187 | /* |
188 | * callback_data_*: supporting data structures for alternate traversal | |
189 | * | |
190 | * We sometimes need to be able to traverse through all the files | |
191 | * in a given tree before all immediate subdirectories within that | |
192 | * tree. Since traverse_trees() doesn't do that naturally, we have | |
193 | * a traverse_trees_wrapper() that stores any immediate | |
194 | * subdirectories while traversing files, then traverses the | |
195 | * immediate subdirectories later. These callback_data* variables | |
196 | * store the information for the subdirectories so that we can do | |
197 | * that traversal order. | |
198 | */ | |
199 | struct traversal_callback_data *callback_data; | |
200 | int callback_data_nr, callback_data_alloc; | |
201 | char *callback_data_traverse_path; | |
202 | ||
64aceb6d EN |
203 | /* |
204 | * merge_trees: trees passed to the merge algorithm for the merge | |
205 | * | |
206 | * merge_trees records the trees passed to the merge algorithm. But, | |
207 | * this data also is stored in merge_result->priv. If a sequence of | |
208 | * merges are being done (such as when cherry-picking or rebasing), | |
209 | * the next merge can look at this and re-use information from | |
210 | * previous merges under certain circumstances. | |
211 | * | |
212 | * See also all the cached_* variables. | |
213 | */ | |
214 | struct tree *merge_trees[3]; | |
215 | ||
216 | /* | |
217 | * cached_pairs_valid_side: which side's cached info can be reused | |
218 | * | |
219 | * See the description for merge_trees. For repeated merges, at most | |
220 | * only one side's cached information can be used. Valid values: | |
221 | * MERGE_SIDE2: cached data from side2 can be reused | |
222 | * MERGE_SIDE1: cached data from side1 can be reused | |
223 | * 0: no cached data can be reused | |
8b09a900 | 224 | * -1: See redo_after_renames; both sides can be reused. |
64aceb6d EN |
225 | */ |
226 | int cached_pairs_valid_side; | |
227 | ||
d29bd6d7 EN |
228 | /* |
229 | * cached_pairs: Caching of renames and deletions. | |
230 | * | |
231 | * These are mappings recording renames and deletions of individual | |
232 | * files (not directories). They are thus a map from an old | |
233 | * filename to either NULL (for deletions) or a new filename (for | |
234 | * renames). | |
235 | */ | |
236 | struct strmap cached_pairs[3]; | |
237 | ||
238 | /* | |
239 | * cached_target_names: just the destinations from cached_pairs | |
240 | * | |
241 | * We sometimes want a fast lookup to determine if a given filename | |
242 | * is one of the destinations in cached_pairs. cached_target_names | |
243 | * is thus duplicative information, but it provides a fast lookup. | |
244 | */ | |
245 | struct strset cached_target_names[3]; | |
246 | ||
247 | /* | |
248 | * cached_irrelevant: Caching of rename_sources that aren't relevant. | |
249 | * | |
250 | * If we try to detect a rename for a source path and succeed, it's | |
251 | * part of a rename. If we try to detect a rename for a source path | |
252 | * and fail, then it's a delete. If we do not try to detect a rename | |
253 | * for a path, then we don't know if it's a rename or a delete. If | |
254 | * merge-ort doesn't think the path is relevant, then we just won't | |
255 | * cache anything for that path. But there's a slight problem in | |
256 | * that merge-ort can think a path is RELEVANT_LOCATION, but due to | |
257 | * commit 9bd342137e ("diffcore-rename: determine which | |
258 | * relevant_sources are no longer relevant", 2021-03-13), | |
259 | * diffcore-rename can downgrade the path to RELEVANT_NO_MORE. To | |
260 | * avoid excessive calls to diffcore_rename_extended() we still need | |
261 | * to cache such paths, though we cannot record them as either | |
262 | * renames or deletes. So we cache them here as a "turned out to be | |
263 | * irrelevant *for this commit*" as they are often also irrelevant | |
264 | * for subsequent commits, though we will have to do some extra | |
265 | * checking to see whether such paths become relevant for rename | |
266 | * detection when cherry-picking/rebasing subsequent commits. | |
267 | */ | |
268 | struct strset cached_irrelevant[3]; | |
269 | ||
8b09a900 EN |
270 | /* |
271 | * redo_after_renames: optimization flag for "restarting" the merge | |
272 | * | |
273 | * Sometimes it pays to detect renames, cache them, and then | |
274 | * restart the merge operation from the beginning. The reason for | |
275 | * this is that when we know where all the renames are, we know | |
276 | * whether a certain directory has any paths under it affected -- | |
277 | * and if a directory is not affected then it permits us to do | |
278 | * trivial tree merging in more cases. Doing trivial tree merging | |
279 | * prevents the need to run process_entry() on every path | |
280 | * underneath trees that can be trivially merged, and | |
281 | * process_entry() is more expensive than collect_merge_info() -- | |
282 | * plus, the second collect_merge_info() will be much faster since | |
283 | * it doesn't have to recurse into the relevant trees. | |
284 | * | |
285 | * Values for this flag: | |
286 | * 0 = don't bother, not worth it (or conditions not yet checked) | |
287 | * 1 = conditions for optimization met, optimization worthwhile | |
288 | * 2 = we already did it (don't restart merge yet again) | |
289 | */ | |
290 | unsigned redo_after_renames; | |
291 | ||
864075ec EN |
292 | /* |
293 | * needed_limit: value needed for inexact rename detection to run | |
294 | * | |
295 | * If the current rename limit wasn't high enough for inexact | |
296 | * rename detection to run, this records the limit needed. Otherwise, | |
297 | * this value remains 0. | |
298 | */ | |
299 | int needed_limit; | |
300 | }; | |
301 | ||
5b59c3db EN |
302 | struct merge_options_internal { |
303 | /* | |
304 | * paths: primary data structure in all of merge ort. | |
305 | * | |
306 | * The keys of paths: | |
307 | * * are full relative paths from the toplevel of the repository | |
308 | * (e.g. "drivers/firmware/raspberrypi.c"). | |
309 | * * store all relevant paths in the repo, both directories and | |
310 | * files (e.g. drivers, drivers/firmware would also be included) | |
311 | * * these keys serve to intern all the path strings, which allows | |
312 | * us to do pointer comparison on directory names instead of | |
313 | * strcmp; we just have to be careful to use the interned strings. | |
314 | * | |
315 | * The values of paths: | |
316 | * * either a pointer to a merged_info, or a conflict_info struct | |
317 | * * merged_info contains all relevant information for a | |
318 | * non-conflicted entry. | |
319 | * * conflict_info contains a merged_info, plus any additional | |
320 | * information about a conflict such as the higher orders stages | |
321 | * involved and the names of the paths those came from (handy | |
322 | * once renames get involved). | |
323 | * * a path may start "conflicted" (i.e. point to a conflict_info) | |
324 | * and then a later step (e.g. three-way content merge) determines | |
325 | * it can be cleanly merged, at which point it'll be marked clean | |
326 | * and the algorithm will ignore any data outside the contained | |
327 | * merged_info for that entry | |
328 | * * If an entry remains conflicted, the merged_info portion of a | |
329 | * conflict_info will later be filled with whatever version of | |
330 | * the file should be placed in the working directory (e.g. an | |
331 | * as-merged-as-possible variation that contains conflict markers). | |
332 | */ | |
333 | struct strmap paths; | |
334 | ||
335 | /* | |
336 | * conflicted: a subset of keys->values from "paths" | |
337 | * | |
338 | * conflicted is basically an optimization between process_entries() | |
339 | * and record_conflicted_index_entries(); the latter could loop over | |
340 | * ALL the entries in paths AGAIN and look for the ones that are | |
341 | * still conflicted, but since process_entries() has to loop over | |
342 | * all of them, it saves the ones it couldn't resolve in this strmap | |
343 | * so that record_conflicted_index_entries() can iterate just the | |
344 | * relevant entries. | |
345 | */ | |
346 | struct strmap conflicted; | |
347 | ||
43c1dccb | 348 | /* |
4137c54b | 349 | * pool: memory pool for fast allocation/deallocation |
43c1dccb | 350 | * |
4137c54b EN |
351 | * We allocate room for lots of filenames and auxiliary data |
352 | * structures in merge_options_internal, and it tends to all be | |
353 | * freed together too. Using a memory pool for these provides a | |
354 | * nice speedup. | |
43c1dccb | 355 | */ |
62a15162 | 356 | struct mem_pool pool; |
43c1dccb | 357 | |
c5a6f655 | 358 | /* |
6debb752 | 359 | * conflicts: logical conflicts and messages stored by _primary_ path |
c5a6f655 EN |
360 | * |
361 | * This is a map of pathnames (a subset of the keys in "paths" above) | |
6debb752 JS |
362 | * to struct string_list, with each item's `util` containing a |
363 | * `struct logical_conflict_info`. Note, though, that for each path, | |
364 | * it only stores the logical conflicts for which that path is the | |
365 | * primary path; the path might be part of additional conflicts. | |
c5a6f655 | 366 | */ |
6debb752 | 367 | struct strmap conflicts; |
c5a6f655 | 368 | |
5b59c3db | 369 | /* |
864075ec EN |
370 | * renames: various data relating to rename detection |
371 | */ | |
372 | struct rename_info renames; | |
373 | ||
ea305a68 EN |
374 | /* |
375 | * attr_index: hacky minimal index used for renormalization | |
376 | * | |
377 | * renormalization code _requires_ an index, though it only needs to | |
378 | * find a .gitattributes file within the index. So, when | |
379 | * renormalization is important, we create a special index with just | |
380 | * that one file. | |
381 | */ | |
382 | struct index_state attr_index; | |
383 | ||
5b59c3db | 384 | /* |
05b85c6e | 385 | * current_dir_name, toplevel_dir: temporary vars |
5b59c3db | 386 | * |
05b85c6e EN |
387 | * These are used in collect_merge_info_callback(), and will set the |
388 | * various merged_info.directory_name for the various paths we get; | |
389 | * see documentation for that variable and the requirements placed on | |
390 | * that field. | |
5b59c3db EN |
391 | */ |
392 | const char *current_dir_name; | |
05b85c6e | 393 | const char *toplevel_dir; |
5b59c3db EN |
394 | |
395 | /* call_depth: recursion level counter for merging merge bases */ | |
396 | int call_depth; | |
4057523a CW |
397 | |
398 | /* field that holds submodule conflict information */ | |
399 | struct string_list conflicted_submodules; | |
400 | }; | |
401 | ||
402 | struct conflicted_submodule_item { | |
403 | char *abbrev; | |
404 | int flag; | |
5b59c3db EN |
405 | }; |
406 | ||
1ee34710 | 407 | static void conflicted_submodule_item_free(void *util, const char *str UNUSED) |
4057523a CW |
408 | { |
409 | struct conflicted_submodule_item *item = util; | |
410 | ||
411 | free(item->abbrev); | |
412 | free(item); | |
413 | } | |
414 | ||
5b59c3db EN |
415 | struct version_info { |
416 | struct object_id oid; | |
417 | unsigned short mode; | |
418 | }; | |
419 | ||
420 | struct merged_info { | |
421 | /* if is_null, ignore result. otherwise result has oid & mode */ | |
422 | struct version_info result; | |
423 | unsigned is_null:1; | |
424 | ||
425 | /* | |
426 | * clean: whether the path in question is cleanly merged. | |
427 | * | |
428 | * see conflict_info.merged for more details. | |
429 | */ | |
430 | unsigned clean:1; | |
431 | ||
432 | /* | |
433 | * basename_offset: offset of basename of path. | |
434 | * | |
435 | * perf optimization to avoid recomputing offset of final '/' | |
436 | * character in pathname (0 if no '/' in pathname). | |
437 | */ | |
438 | size_t basename_offset; | |
439 | ||
440 | /* | |
441 | * directory_name: containing directory name. | |
442 | * | |
443 | * Note that we assume directory_name is constructed such that | |
444 | * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name, | |
445 | * i.e. string equality is equivalent to pointer equality. For this | |
446 | * to hold, we have to be careful setting directory_name. | |
447 | */ | |
448 | const char *directory_name; | |
449 | }; | |
450 | ||
451 | struct conflict_info { | |
452 | /* | |
453 | * merged: the version of the path that will be written to working tree | |
454 | * | |
455 | * WARNING: It is critical to check merged.clean and ensure it is 0 | |
456 | * before reading any conflict_info fields outside of merged. | |
457 | * Allocated merge_info structs will always have clean set to 1. | |
458 | * Allocated conflict_info structs will have merged.clean set to 0 | |
459 | * initially. The merged.clean field is how we know if it is safe | |
460 | * to access other parts of conflict_info besides merged; if a | |
461 | * conflict_info's merged.clean is changed to 1, the rest of the | |
462 | * algorithm is not allowed to look at anything outside of the | |
463 | * merged member anymore. | |
464 | */ | |
465 | struct merged_info merged; | |
466 | ||
467 | /* oids & modes from each of the three trees for this path */ | |
468 | struct version_info stages[3]; | |
469 | ||
470 | /* pathnames for each stage; may differ due to rename detection */ | |
471 | const char *pathnames[3]; | |
472 | ||
473 | /* Whether this path is/was involved in a directory/file conflict */ | |
474 | unsigned df_conflict:1; | |
475 | ||
1c7873cd EN |
476 | /* |
477 | * Whether this path is/was involved in a non-content conflict other | |
478 | * than a directory/file conflict (e.g. rename/rename, rename/delete, | |
479 | * file location based on possible directory rename). | |
480 | */ | |
481 | unsigned path_conflict:1; | |
482 | ||
5b59c3db EN |
483 | /* |
484 | * For filemask and dirmask, the ith bit corresponds to whether the | |
485 | * ith entry is a file (filemask) or a directory (dirmask). Thus, | |
486 | * filemask & dirmask is always zero, and filemask | dirmask is at | |
487 | * most 7 but can be less when a path does not appear as either a | |
488 | * file or a directory on at least one side of history. | |
489 | * | |
490 | * Note that these masks are related to enum merge_side, as the ith | |
491 | * entry corresponds to side i. | |
492 | * | |
493 | * These values come from a traverse_trees() call; more info may be | |
494 | * found looking at tree-walk.h's struct traverse_info, | |
495 | * particularly the documentation above the "fn" member (note that | |
496 | * filemask = mask & ~dirmask from that documentation). | |
497 | */ | |
498 | unsigned filemask:3; | |
499 | unsigned dirmask:3; | |
500 | ||
501 | /* | |
502 | * Optimization to track which stages match, to avoid the need to | |
503 | * recompute it in multiple steps. Either 0 or at least 2 bits are | |
504 | * set; if at least 2 bits are set, their corresponding stages match. | |
505 | */ | |
506 | unsigned match_mask:3; | |
507 | }; | |
508 | ||
cb260775 EN |
509 | enum conflict_and_info_types { |
510 | /* "Simple" conflicts and informational messages */ | |
511 | INFO_AUTO_MERGING = 0, | |
512 | CONFLICT_CONTENTS, /* text file that failed to merge */ | |
513 | CONFLICT_BINARY, | |
514 | CONFLICT_FILE_DIRECTORY, | |
515 | CONFLICT_DISTINCT_MODES, | |
516 | CONFLICT_MODIFY_DELETE, | |
cb260775 EN |
517 | |
518 | /* Regular rename */ | |
519 | CONFLICT_RENAME_RENAME, /* same file renamed differently */ | |
520 | CONFLICT_RENAME_COLLIDES, /* rename/add or two files renamed to 1 */ | |
521 | CONFLICT_RENAME_DELETE, | |
522 | ||
523 | /* Basic directory rename */ | |
524 | CONFLICT_DIR_RENAME_SUGGESTED, | |
525 | INFO_DIR_RENAME_APPLIED, | |
526 | ||
527 | /* Special directory rename cases */ | |
528 | INFO_DIR_RENAME_SKIPPED_DUE_TO_RERENAME, | |
529 | CONFLICT_DIR_RENAME_FILE_IN_WAY, | |
530 | CONFLICT_DIR_RENAME_COLLISION, | |
531 | CONFLICT_DIR_RENAME_SPLIT, | |
532 | ||
533 | /* Basic submodule */ | |
534 | INFO_SUBMODULE_FAST_FORWARDING, | |
535 | CONFLICT_SUBMODULE_FAILED_TO_MERGE, | |
536 | ||
537 | /* Special submodule cases broken out from FAILED_TO_MERGE */ | |
538 | CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION, | |
539 | CONFLICT_SUBMODULE_NOT_INITIALIZED, | |
540 | CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE, | |
541 | CONFLICT_SUBMODULE_MAY_HAVE_REWINDS, | |
4057523a | 542 | CONFLICT_SUBMODULE_NULL_MERGE_BASE, |
cb260775 EN |
543 | |
544 | /* Keep this entry _last_ in the list */ | |
545 | NB_CONFLICT_TYPES, | |
546 | }; | |
547 | ||
548 | /* | |
549 | * Short description of conflict type, relied upon by external tools. | |
550 | * | |
551 | * We can add more entries, but DO NOT change any of these strings. Also, | |
552 | * Order MUST match conflict_info_and_types. | |
553 | */ | |
554 | static const char *type_short_descriptions[] = { | |
555 | /*** "Simple" conflicts and informational messages ***/ | |
556 | [INFO_AUTO_MERGING] = "Auto-merging", | |
557 | [CONFLICT_CONTENTS] = "CONFLICT (contents)", | |
558 | [CONFLICT_BINARY] = "CONFLICT (binary)", | |
559 | [CONFLICT_FILE_DIRECTORY] = "CONFLICT (file/directory)", | |
560 | [CONFLICT_DISTINCT_MODES] = "CONFLICT (distinct modes)", | |
561 | [CONFLICT_MODIFY_DELETE] = "CONFLICT (modify/delete)", | |
cb260775 EN |
562 | |
563 | /*** Regular rename ***/ | |
564 | [CONFLICT_RENAME_RENAME] = "CONFLICT (rename/rename)", | |
565 | [CONFLICT_RENAME_COLLIDES] = "CONFLICT (rename involved in collision)", | |
566 | [CONFLICT_RENAME_DELETE] = "CONFLICT (rename/delete)", | |
567 | ||
568 | /*** Basic directory rename ***/ | |
569 | [CONFLICT_DIR_RENAME_SUGGESTED] = | |
570 | "CONFLICT (directory rename suggested)", | |
571 | [INFO_DIR_RENAME_APPLIED] = "Path updated due to directory rename", | |
572 | ||
573 | /*** Special directory rename cases ***/ | |
574 | [INFO_DIR_RENAME_SKIPPED_DUE_TO_RERENAME] = | |
575 | "Directory rename skipped since directory was renamed on both sides", | |
576 | [CONFLICT_DIR_RENAME_FILE_IN_WAY] = | |
577 | "CONFLICT (file in way of directory rename)", | |
578 | [CONFLICT_DIR_RENAME_COLLISION] = "CONFLICT(directory rename collision)", | |
579 | [CONFLICT_DIR_RENAME_SPLIT] = "CONFLICT(directory rename unclear split)", | |
580 | ||
581 | /*** Basic submodule ***/ | |
582 | [INFO_SUBMODULE_FAST_FORWARDING] = "Fast forwarding submodule", | |
583 | [CONFLICT_SUBMODULE_FAILED_TO_MERGE] = "CONFLICT (submodule)", | |
584 | ||
585 | /*** Special submodule cases broken out from FAILED_TO_MERGE ***/ | |
586 | [CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION] = | |
587 | "CONFLICT (submodule with possible resolution)", | |
588 | [CONFLICT_SUBMODULE_NOT_INITIALIZED] = | |
589 | "CONFLICT (submodule not initialized)", | |
590 | [CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE] = | |
591 | "CONFLICT (submodule history not available)", | |
592 | [CONFLICT_SUBMODULE_MAY_HAVE_REWINDS] = | |
593 | "CONFLICT (submodule may have rewinds)", | |
4057523a CW |
594 | [CONFLICT_SUBMODULE_NULL_MERGE_BASE] = |
595 | "CONFLICT (submodule lacks merge base)" | |
cb260775 EN |
596 | }; |
597 | ||
598 | struct logical_conflict_info { | |
599 | enum conflict_and_info_types type; | |
600 | struct strvec paths; | |
601 | }; | |
602 | ||
04af1879 EN |
603 | /*** Function Grouping: various utility functions ***/ |
604 | ||
98bf9841 EN |
605 | /* |
606 | * For the next three macros, see warning for conflict_info.merged. | |
607 | * | |
608 | * In each of the below, mi is a struct merged_info*, and ci was defined | |
609 | * as a struct conflict_info* (but we need to verify ci isn't actually | |
610 | * pointed at a struct merged_info*). | |
611 | * | |
612 | * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise. | |
613 | * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one. | |
614 | * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first. | |
615 | */ | |
616 | #define INITIALIZE_CI(ci, mi) do { \ | |
617 | (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \ | |
618 | } while (0) | |
619 | #define VERIFY_CI(ci) assert(ci && !ci->merged.clean); | |
620 | #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \ | |
621 | (ci) = (struct conflict_info *)(mi); \ | |
622 | assert((ci) && !(mi)->clean); \ | |
623 | } while (0) | |
624 | ||
89422d29 EN |
625 | static void free_strmap_strings(struct strmap *map) |
626 | { | |
627 | struct hashmap_iter iter; | |
628 | struct strmap_entry *entry; | |
629 | ||
630 | strmap_for_each_entry(map, &iter, entry) { | |
631 | free((char*)entry->key); | |
632 | } | |
633 | } | |
634 | ||
43e9c4ee EN |
635 | static void clear_or_reinit_internal_opts(struct merge_options_internal *opti, |
636 | int reinitialize) | |
101bc5bc | 637 | { |
f5d9fbc2 EN |
638 | struct rename_info *renames = &opti->renames; |
639 | int i; | |
7afc0b03 | 640 | void (*strmap_clear_func)(struct strmap *, int) = |
43e9c4ee | 641 | reinitialize ? strmap_partial_clear : strmap_clear; |
7afc0b03 | 642 | void (*strintmap_clear_func)(struct strintmap *) = |
a49b55d5 | 643 | reinitialize ? strintmap_partial_clear : strintmap_clear; |
7afc0b03 | 644 | void (*strset_clear_func)(struct strset *) = |
d29bd6d7 | 645 | reinitialize ? strset_partial_clear : strset_clear; |
101bc5bc | 646 | |
62a15162 | 647 | strmap_clear_func(&opti->paths, 0); |
101bc5bc EN |
648 | |
649 | /* | |
650 | * All keys and values in opti->conflicted are a subset of those in | |
651 | * opti->paths. We don't want to deallocate anything twice, so we | |
652 | * don't free the keys and we pass 0 for free_values. | |
653 | */ | |
7afc0b03 | 654 | strmap_clear_func(&opti->conflicted, 0); |
c5a6f655 | 655 | |
1218b3ab | 656 | if (opti->attr_index.cache_nr) /* true iff opt->renormalize */ |
ea305a68 EN |
657 | discard_index(&opti->attr_index); |
658 | ||
f5d9fbc2 EN |
659 | /* Free memory used by various renames maps */ |
660 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) { | |
7afc0b03 EN |
661 | strintmap_clear_func(&renames->dirs_removed[i]); |
662 | strmap_clear_func(&renames->dir_renames[i], 0); | |
663 | strintmap_clear_func(&renames->relevant_sources[i]); | |
d5098029 EN |
664 | if (!reinitialize) |
665 | assert(renames->cached_pairs_valid_side == 0); | |
8b09a900 EN |
666 | if (i != renames->cached_pairs_valid_side && |
667 | -1 != renames->cached_pairs_valid_side) { | |
7afc0b03 EN |
668 | strset_clear_func(&renames->cached_target_names[i]); |
669 | strmap_clear_func(&renames->cached_pairs[i], 1); | |
670 | strset_clear_func(&renames->cached_irrelevant[i]); | |
d5098029 EN |
671 | partial_clear_dir_rename_count(&renames->dir_rename_count[i]); |
672 | if (!reinitialize) | |
673 | strmap_clear(&renames->dir_rename_count[i], 1); | |
674 | } | |
f5d9fbc2 | 675 | } |
d478f567 | 676 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) { |
7afc0b03 EN |
677 | strintmap_clear_func(&renames->deferred[i].possible_trivial_merges); |
678 | strset_clear_func(&renames->deferred[i].target_dirs); | |
d478f567 EN |
679 | renames->deferred[i].trivial_merges_okay = 1; /* 1 == maybe */ |
680 | } | |
64aceb6d EN |
681 | renames->cached_pairs_valid_side = 0; |
682 | renames->dir_rename_mask = 0; | |
f5d9fbc2 | 683 | |
c5a6f655 EN |
684 | if (!reinitialize) { |
685 | struct hashmap_iter iter; | |
686 | struct strmap_entry *e; | |
687 | ||
688 | /* Release and free each strbuf found in output */ | |
6debb752 JS |
689 | strmap_for_each_entry(&opti->conflicts, &iter, e) { |
690 | struct string_list *list = e->value; | |
cb260775 EN |
691 | for (int i = 0; i < list->nr; i++) { |
692 | struct logical_conflict_info *info = | |
693 | list->items[i].util; | |
694 | strvec_clear(&info->paths); | |
695 | } | |
c5a6f655 | 696 | /* |
6debb752 JS |
697 | * While strictly speaking we don't need to |
698 | * free(conflicts) here because we could pass | |
699 | * free_values=1 when calling strmap_clear() on | |
700 | * opti->conflicts, that would require strmap_clear | |
701 | * to do another strmap_for_each_entry() loop, so we | |
702 | * just free it while we're iterating anyway. | |
c5a6f655 | 703 | */ |
6debb752 JS |
704 | string_list_clear(list, 1); |
705 | free(list); | |
c5a6f655 | 706 | } |
6debb752 | 707 | strmap_clear(&opti->conflicts, 0); |
c5a6f655 | 708 | } |
beb06145 | 709 | |
62a15162 | 710 | mem_pool_discard(&opti->pool, 0); |
4137c54b | 711 | |
4057523a CW |
712 | string_list_clear_func(&opti->conflicted_submodules, |
713 | conflicted_submodule_item_free); | |
714 | ||
beb06145 EN |
715 | /* Clean out callback_data as well. */ |
716 | FREE_AND_NULL(renames->callback_data); | |
717 | renames->callback_data_nr = renames->callback_data_alloc = 0; | |
101bc5bc EN |
718 | } |
719 | ||
48ca53ca | 720 | __attribute__((format (printf, 2, 3))) |
0c0d705b EN |
721 | static int err(struct merge_options *opt, const char *err, ...) |
722 | { | |
723 | va_list params; | |
724 | struct strbuf sb = STRBUF_INIT; | |
725 | ||
726 | strbuf_addstr(&sb, "error: "); | |
727 | va_start(params, err); | |
728 | strbuf_vaddf(&sb, err, params); | |
729 | va_end(params); | |
730 | ||
731 | error("%s", sb.buf); | |
732 | strbuf_release(&sb); | |
733 | ||
734 | return -1; | |
735 | } | |
736 | ||
c73cda76 EN |
737 | static void format_commit(struct strbuf *sb, |
738 | int indent, | |
155b517d | 739 | struct repository *repo, |
c73cda76 EN |
740 | struct commit *commit) |
741 | { | |
70f19c7f EN |
742 | struct merge_remote_desc *desc; |
743 | struct pretty_print_context ctx = {0}; | |
744 | ctx.abbrev = DEFAULT_ABBREV; | |
745 | ||
746 | strbuf_addchars(sb, ' ', indent); | |
747 | desc = merge_remote_util(commit); | |
748 | if (desc) { | |
749 | strbuf_addf(sb, "virtual %s\n", desc->name); | |
750 | return; | |
751 | } | |
752 | ||
155b517d | 753 | repo_format_commit_message(repo, commit, "%h %s", sb, &ctx); |
70f19c7f | 754 | strbuf_addch(sb, '\n'); |
c73cda76 EN |
755 | } |
756 | ||
cb260775 | 757 | __attribute__((format (printf, 8, 9))) |
c5a6f655 | 758 | static void path_msg(struct merge_options *opt, |
cb260775 | 759 | enum conflict_and_info_types type, |
c5a6f655 | 760 | int omittable_hint, /* skippable under --remerge-diff */ |
cb260775 EN |
761 | const char *primary_path, |
762 | const char *other_path_1, /* may be NULL */ | |
763 | const char *other_path_2, /* may be NULL */ | |
764 | struct string_list *other_paths, /* may be NULL */ | |
c5a6f655 EN |
765 | const char *fmt, ...) |
766 | { | |
767 | va_list ap; | |
6debb752 | 768 | struct string_list *path_conflicts; |
cb260775 | 769 | struct logical_conflict_info *info; |
6debb752 JS |
770 | struct strbuf buf = STRBUF_INIT; |
771 | struct strbuf *dest; | |
6054d1aa EN |
772 | struct strbuf tmp = STRBUF_INIT; |
773 | ||
cb260775 EN |
774 | /* Sanity checks */ |
775 | assert(omittable_hint == | |
776 | !starts_with(type_short_descriptions[type], "CONFLICT") || | |
ff033db7 | 777 | type == CONFLICT_DIR_RENAME_SUGGESTED); |
6054d1aa | 778 | if (opt->record_conflict_msgs_as_headers && omittable_hint) |
0d83d824 | 779 | return; /* Do not record mere hints in headers */ |
624a9350 EN |
780 | if (opt->priv->call_depth && opt->verbosity < 5) |
781 | return; /* Ignore messages from inner merges */ | |
782 | ||
6debb752 | 783 | /* Ensure path_conflicts (ptr to array of logical_conflict) allocated */ |
cb260775 | 784 | path_conflicts = strmap_get(&opt->priv->conflicts, primary_path); |
6debb752 JS |
785 | if (!path_conflicts) { |
786 | path_conflicts = xmalloc(sizeof(*path_conflicts)); | |
787 | string_list_init_dup(path_conflicts); | |
cb260775 | 788 | strmap_put(&opt->priv->conflicts, primary_path, path_conflicts); |
c5a6f655 EN |
789 | } |
790 | ||
cb260775 EN |
791 | /* Add a logical_conflict at the end to store info from this call */ |
792 | info = xcalloc(1, sizeof(*info)); | |
793 | info->type = type; | |
794 | strvec_init(&info->paths); | |
795 | ||
796 | /* Handle the list of paths */ | |
797 | strvec_push(&info->paths, primary_path); | |
798 | if (other_path_1) | |
799 | strvec_push(&info->paths, other_path_1); | |
800 | if (other_path_2) | |
801 | strvec_push(&info->paths, other_path_2); | |
802 | if (other_paths) | |
803 | for (int i = 0; i < other_paths->nr; i++) | |
804 | strvec_push(&info->paths, other_paths->items[i].string); | |
805 | ||
6debb752 JS |
806 | /* Handle message and its format, in normal case */ |
807 | dest = (opt->record_conflict_msgs_as_headers ? &tmp : &buf); | |
6054d1aa | 808 | |
c5a6f655 | 809 | va_start(ap, fmt); |
4a3d86e1 EN |
810 | if (opt->priv->call_depth) { |
811 | strbuf_addchars(dest, ' ', 2); | |
812 | strbuf_addstr(dest, "From inner merge:"); | |
813 | strbuf_addchars(dest, ' ', opt->priv->call_depth * 2); | |
814 | } | |
6054d1aa | 815 | strbuf_vaddf(dest, fmt, ap); |
c5a6f655 EN |
816 | va_end(ap); |
817 | ||
6debb752 | 818 | /* Handle specialized formatting of message under --remerge-diff */ |
6054d1aa EN |
819 | if (opt->record_conflict_msgs_as_headers) { |
820 | int i_sb = 0, i_tmp = 0; | |
821 | ||
822 | /* Start with the specified prefix */ | |
823 | if (opt->msg_header_prefix) | |
6debb752 | 824 | strbuf_addf(&buf, "%s ", opt->msg_header_prefix); |
6054d1aa EN |
825 | |
826 | /* Copy tmp to sb, adding spaces after newlines */ | |
6debb752 | 827 | strbuf_grow(&buf, buf.len + 2*tmp.len); /* more than sufficient */ |
6054d1aa EN |
828 | for (; i_tmp < tmp.len; i_tmp++, i_sb++) { |
829 | /* Copy next character from tmp to sb */ | |
6debb752 | 830 | buf.buf[buf.len + i_sb] = tmp.buf[i_tmp]; |
6054d1aa EN |
831 | |
832 | /* If we copied a newline, add a space */ | |
833 | if (tmp.buf[i_tmp] == '\n') | |
6debb752 | 834 | buf.buf[++i_sb] = ' '; |
6054d1aa EN |
835 | } |
836 | /* Update length and ensure it's NUL-terminated */ | |
6debb752 JS |
837 | buf.len += i_sb; |
838 | buf.buf[buf.len] = '\0'; | |
6054d1aa EN |
839 | |
840 | strbuf_release(&tmp); | |
841 | } | |
cb260775 EN |
842 | string_list_append_nodup(path_conflicts, strbuf_detach(&buf, NULL)) |
843 | ->util = info; | |
c5a6f655 EN |
844 | } |
845 | ||
a8791ef6 EN |
846 | static struct diff_filespec *pool_alloc_filespec(struct mem_pool *pool, |
847 | const char *path) | |
848 | { | |
62a15162 | 849 | /* Similar to alloc_filespec(), but allocate from pool and reuse path */ |
a8791ef6 | 850 | struct diff_filespec *spec; |
a8791ef6 | 851 | |
092e5115 EN |
852 | spec = mem_pool_calloc(pool, 1, sizeof(*spec)); |
853 | spec->path = (char*)path; /* spec won't modify it */ | |
a8791ef6 EN |
854 | |
855 | spec->count = 1; | |
856 | spec->is_binary = -1; | |
857 | return spec; | |
858 | } | |
859 | ||
a8791ef6 EN |
860 | static struct diff_filepair *pool_diff_queue(struct mem_pool *pool, |
861 | struct diff_queue_struct *queue, | |
862 | struct diff_filespec *one, | |
863 | struct diff_filespec *two) | |
864 | { | |
62a15162 | 865 | /* Same code as diff_queue(), except allocate from pool */ |
a8791ef6 EN |
866 | struct diff_filepair *dp; |
867 | ||
a8791ef6 EN |
868 | dp = mem_pool_calloc(pool, 1, sizeof(*dp)); |
869 | dp->one = one; | |
870 | dp->two = two; | |
871 | if (queue) | |
872 | diff_q(queue, dp); | |
873 | return dp; | |
874 | } | |
875 | ||
5a1a1e8e EN |
876 | /* add a string to a strbuf, but converting "/" to "_" */ |
877 | static void add_flattened_path(struct strbuf *out, const char *s) | |
878 | { | |
879 | size_t i = out->len; | |
880 | strbuf_addstr(out, s); | |
881 | for (; i < out->len; i++) | |
882 | if (out->buf[i] == '/') | |
883 | out->buf[i] = '_'; | |
884 | } | |
885 | ||
81afc794 | 886 | static char *unique_path(struct merge_options *opt, |
23366d2a EN |
887 | const char *path, |
888 | const char *branch) | |
889 | { | |
81afc794 | 890 | char *ret = NULL; |
5a1a1e8e EN |
891 | struct strbuf newpath = STRBUF_INIT; |
892 | int suffix = 0; | |
893 | size_t base_len; | |
81afc794 | 894 | struct strmap *existing_paths = &opt->priv->paths; |
5a1a1e8e EN |
895 | |
896 | strbuf_addf(&newpath, "%s~", path); | |
897 | add_flattened_path(&newpath, branch); | |
898 | ||
899 | base_len = newpath.len; | |
900 | while (strmap_contains(existing_paths, newpath.buf)) { | |
901 | strbuf_setlen(&newpath, base_len); | |
902 | strbuf_addf(&newpath, "_%d", suffix++); | |
903 | } | |
904 | ||
81afc794 EN |
905 | /* Track the new path in our memory pool */ |
906 | ret = mem_pool_alloc(&opt->priv->pool, newpath.len + 1); | |
907 | memcpy(ret, newpath.buf, newpath.len + 1); | |
908 | strbuf_release(&newpath); | |
909 | return ret; | |
23366d2a EN |
910 | } |
911 | ||
04af1879 EN |
912 | /*** Function Grouping: functions related to collect_merge_info() ***/ |
913 | ||
a68e6cea EN |
914 | static int traverse_trees_wrapper_callback(int n, |
915 | unsigned long mask, | |
916 | unsigned long dirmask, | |
917 | struct name_entry *names, | |
918 | struct traverse_info *info) | |
919 | { | |
920 | struct merge_options *opt = info->data; | |
921 | struct rename_info *renames = &opt->priv->renames; | |
2fd9eda4 | 922 | unsigned filemask = mask & ~dirmask; |
a68e6cea EN |
923 | |
924 | assert(n==3); | |
925 | ||
926 | if (!renames->callback_data_traverse_path) | |
927 | renames->callback_data_traverse_path = xstrdup(info->traverse_path); | |
928 | ||
2fd9eda4 EN |
929 | if (filemask && filemask == renames->dir_rename_mask) |
930 | renames->dir_rename_mask = 0x07; | |
931 | ||
a68e6cea EN |
932 | ALLOC_GROW(renames->callback_data, renames->callback_data_nr + 1, |
933 | renames->callback_data_alloc); | |
934 | renames->callback_data[renames->callback_data_nr].mask = mask; | |
935 | renames->callback_data[renames->callback_data_nr].dirmask = dirmask; | |
936 | COPY_ARRAY(renames->callback_data[renames->callback_data_nr].names, | |
937 | names, 3); | |
938 | renames->callback_data_nr++; | |
939 | ||
940 | return mask; | |
941 | } | |
942 | ||
943 | /* | |
944 | * Much like traverse_trees(), BUT: | |
945 | * - read all the tree entries FIRST, saving them | |
946 | * - note that the above step provides an opportunity to compute necessary | |
947 | * additional details before the "real" traversal | |
948 | * - loop through the saved entries and call the original callback on them | |
949 | */ | |
a68e6cea EN |
950 | static int traverse_trees_wrapper(struct index_state *istate, |
951 | int n, | |
952 | struct tree_desc *t, | |
953 | struct traverse_info *info) | |
954 | { | |
955 | int ret, i, old_offset; | |
956 | traverse_callback_t old_fn; | |
957 | char *old_callback_data_traverse_path; | |
958 | struct merge_options *opt = info->data; | |
959 | struct rename_info *renames = &opt->priv->renames; | |
960 | ||
2fd9eda4 EN |
961 | assert(renames->dir_rename_mask == 2 || renames->dir_rename_mask == 4); |
962 | ||
a68e6cea EN |
963 | old_callback_data_traverse_path = renames->callback_data_traverse_path; |
964 | old_fn = info->fn; | |
965 | old_offset = renames->callback_data_nr; | |
966 | ||
967 | renames->callback_data_traverse_path = NULL; | |
968 | info->fn = traverse_trees_wrapper_callback; | |
969 | ret = traverse_trees(istate, n, t, info); | |
970 | if (ret < 0) | |
971 | return ret; | |
972 | ||
973 | info->traverse_path = renames->callback_data_traverse_path; | |
974 | info->fn = old_fn; | |
975 | for (i = old_offset; i < renames->callback_data_nr; ++i) { | |
976 | info->fn(n, | |
977 | renames->callback_data[i].mask, | |
978 | renames->callback_data[i].dirmask, | |
979 | renames->callback_data[i].names, | |
980 | info); | |
981 | } | |
982 | ||
983 | renames->callback_data_nr = old_offset; | |
984 | free(renames->callback_data_traverse_path); | |
985 | renames->callback_data_traverse_path = old_callback_data_traverse_path; | |
986 | info->traverse_path = NULL; | |
987 | return 0; | |
988 | } | |
989 | ||
98bf9841 EN |
990 | static void setup_path_info(struct merge_options *opt, |
991 | struct string_list_item *result, | |
992 | const char *current_dir_name, | |
993 | int current_dir_name_len, | |
994 | char *fullpath, /* we'll take over ownership */ | |
995 | struct name_entry *names, | |
996 | struct name_entry *merged_version, | |
997 | unsigned is_null, /* boolean */ | |
998 | unsigned df_conflict, /* boolean */ | |
999 | unsigned filemask, | |
1000 | unsigned dirmask, | |
1001 | int resolved /* boolean */) | |
1002 | { | |
1003 | /* result->util is void*, so mi is a convenience typed variable */ | |
1004 | struct merged_info *mi; | |
1005 | ||
1006 | assert(!is_null || resolved); | |
1007 | assert(!df_conflict || !resolved); /* df_conflict implies !resolved */ | |
1008 | assert(resolved == (merged_version != NULL)); | |
1009 | ||
62a15162 EN |
1010 | mi = mem_pool_calloc(&opt->priv->pool, 1, |
1011 | resolved ? sizeof(struct merged_info) : | |
1012 | sizeof(struct conflict_info)); | |
98bf9841 EN |
1013 | mi->directory_name = current_dir_name; |
1014 | mi->basename_offset = current_dir_name_len; | |
1015 | mi->clean = !!resolved; | |
1016 | if (resolved) { | |
1017 | mi->result.mode = merged_version->mode; | |
1018 | oidcpy(&mi->result.oid, &merged_version->oid); | |
1019 | mi->is_null = !!is_null; | |
1020 | } else { | |
1021 | int i; | |
1022 | struct conflict_info *ci; | |
1023 | ||
1024 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
1025 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
1026 | ci->pathnames[i] = fullpath; | |
1027 | ci->stages[i].mode = names[i].mode; | |
1028 | oidcpy(&ci->stages[i].oid, &names[i].oid); | |
1029 | } | |
1030 | ci->filemask = filemask; | |
1031 | ci->dirmask = dirmask; | |
1032 | ci->df_conflict = !!df_conflict; | |
1033 | if (dirmask) | |
1034 | /* | |
1035 | * Assume is_null for now, but if we have entries | |
1036 | * under the directory then when it is complete in | |
1037 | * write_completed_directory() it'll update this. | |
1038 | * Also, for D/F conflicts, we have to handle the | |
1039 | * directory first, then clear this bit and process | |
1040 | * the file to see how it is handled -- that occurs | |
1041 | * near the top of process_entry(). | |
1042 | */ | |
1043 | mi->is_null = 1; | |
1044 | } | |
1045 | strmap_put(&opt->priv->paths, fullpath, mi); | |
1046 | result->string = fullpath; | |
1047 | result->util = mi; | |
1048 | } | |
1049 | ||
f78cf976 EN |
1050 | static void add_pair(struct merge_options *opt, |
1051 | struct name_entry *names, | |
1052 | const char *pathname, | |
1053 | unsigned side, | |
32a56dfb | 1054 | unsigned is_add /* if false, is_delete */, |
2fd9eda4 EN |
1055 | unsigned match_mask, |
1056 | unsigned dir_rename_mask) | |
f78cf976 EN |
1057 | { |
1058 | struct diff_filespec *one, *two; | |
1059 | struct rename_info *renames = &opt->priv->renames; | |
1060 | int names_idx = is_add ? side : 0; | |
1061 | ||
25e65b6d | 1062 | if (is_add) { |
ef68c3d8 | 1063 | assert(match_mask == 0 || match_mask == 6); |
25e65b6d EN |
1064 | if (strset_contains(&renames->cached_target_names[side], |
1065 | pathname)) | |
1066 | return; | |
1067 | } else { | |
32a56dfb | 1068 | unsigned content_relevant = (match_mask == 0); |
2fd9eda4 | 1069 | unsigned location_relevant = (dir_rename_mask == 0x07); |
32a56dfb | 1070 | |
ef68c3d8 EN |
1071 | assert(match_mask == 0 || match_mask == 3 || match_mask == 5); |
1072 | ||
25e65b6d EN |
1073 | /* |
1074 | * If pathname is found in cached_irrelevant[side] due to | |
1075 | * previous pick but for this commit content is relevant, | |
1076 | * then we need to remove it from cached_irrelevant. | |
1077 | */ | |
1078 | if (content_relevant) | |
1079 | /* strset_remove is no-op if strset doesn't have key */ | |
1080 | strset_remove(&renames->cached_irrelevant[side], | |
1081 | pathname); | |
1082 | ||
1083 | /* | |
1084 | * We do not need to re-detect renames for paths that we already | |
1085 | * know the pairing, i.e. for cached_pairs (or | |
1086 | * cached_irrelevant). However, handle_deferred_entries() needs | |
1087 | * to loop over the union of keys from relevant_sources[side] and | |
1088 | * cached_pairs[side], so for simplicity we set relevant_sources | |
1089 | * for all the cached_pairs too and then strip them back out in | |
1090 | * prune_cached_from_relevant() at the beginning of | |
1091 | * detect_regular_renames(). | |
1092 | */ | |
ec59da60 EN |
1093 | if (content_relevant || location_relevant) { |
1094 | /* content_relevant trumps location_relevant */ | |
1095 | strintmap_set(&renames->relevant_sources[side], pathname, | |
1096 | content_relevant ? RELEVANT_CONTENT : RELEVANT_LOCATION); | |
1097 | } | |
25e65b6d EN |
1098 | |
1099 | /* | |
1100 | * Avoid creating pair if we've already cached rename results. | |
1101 | * Note that we do this after setting relevant_sources[side] | |
1102 | * as noted in the comment above. | |
1103 | */ | |
1104 | if (strmap_contains(&renames->cached_pairs[side], pathname) || | |
1105 | strset_contains(&renames->cached_irrelevant[side], pathname)) | |
1106 | return; | |
32a56dfb EN |
1107 | } |
1108 | ||
62a15162 EN |
1109 | one = pool_alloc_filespec(&opt->priv->pool, pathname); |
1110 | two = pool_alloc_filespec(&opt->priv->pool, pathname); | |
f78cf976 EN |
1111 | fill_filespec(is_add ? two : one, |
1112 | &names[names_idx].oid, 1, names[names_idx].mode); | |
62a15162 | 1113 | pool_diff_queue(&opt->priv->pool, &renames->pairs[side], one, two); |
f78cf976 EN |
1114 | } |
1115 | ||
eb3e3e1d EN |
1116 | static void collect_rename_info(struct merge_options *opt, |
1117 | struct name_entry *names, | |
1118 | const char *dirname, | |
1119 | const char *fullname, | |
1120 | unsigned filemask, | |
1121 | unsigned dirmask, | |
1122 | unsigned match_mask) | |
1123 | { | |
1124 | struct rename_info *renames = &opt->priv->renames; | |
f78cf976 | 1125 | unsigned side; |
eb3e3e1d | 1126 | |
2fd9eda4 EN |
1127 | /* |
1128 | * Update dir_rename_mask (determines ignore-rename-source validity) | |
1129 | * | |
1130 | * dir_rename_mask helps us keep track of when directory rename | |
1131 | * detection may be relevant. Basically, whenver a directory is | |
1132 | * removed on one side of history, and a file is added to that | |
1133 | * directory on the other side of history, directory rename | |
1134 | * detection is relevant (meaning we have to detect renames for all | |
1135 | * files within that directory to deduce where the directory | |
1136 | * moved). Also, whenever a directory needs directory rename | |
1137 | * detection, due to the "majority rules" choice for where to move | |
1138 | * it (see t6423 testcase 1f), we also need to detect renames for | |
1139 | * all files within subdirectories of that directory as well. | |
1140 | * | |
1141 | * Here we haven't looked at files within the directory yet, we are | |
1142 | * just looking at the directory itself. So, if we aren't yet in | |
1143 | * a case where a parent directory needed directory rename detection | |
1144 | * (i.e. dir_rename_mask != 0x07), and if the directory was removed | |
1145 | * on one side of history, record the mask of the other side of | |
1146 | * history in dir_rename_mask. | |
1147 | */ | |
1148 | if (renames->dir_rename_mask != 0x07 && | |
1149 | (dirmask == 3 || dirmask == 5)) { | |
1150 | /* simple sanity check */ | |
1151 | assert(renames->dir_rename_mask == 0 || | |
1152 | renames->dir_rename_mask == (dirmask & ~1)); | |
1153 | /* update dir_rename_mask; have it record mask of new side */ | |
1154 | renames->dir_rename_mask = (dirmask & ~1); | |
1155 | } | |
1156 | ||
eb3e3e1d EN |
1157 | /* Update dirs_removed, as needed */ |
1158 | if (dirmask == 1 || dirmask == 3 || dirmask == 5) { | |
1159 | /* absent_mask = 0x07 - dirmask; sides = absent_mask/2 */ | |
1160 | unsigned sides = (0x07 - dirmask)/2; | |
fb52938e EN |
1161 | unsigned relevance = (renames->dir_rename_mask == 0x07) ? |
1162 | RELEVANT_FOR_ANCESTOR : NOT_RELEVANT; | |
1163 | /* | |
1164 | * Record relevance of this directory. However, note that | |
1165 | * when collect_merge_info_callback() recurses into this | |
1166 | * directory and calls collect_rename_info() on paths | |
1167 | * within that directory, if we find a path that was added | |
1168 | * to this directory on the other side of history, we will | |
1169 | * upgrade this value to RELEVANT_FOR_SELF; see below. | |
1170 | */ | |
eb3e3e1d | 1171 | if (sides & 1) |
fb52938e EN |
1172 | strintmap_set(&renames->dirs_removed[1], fullname, |
1173 | relevance); | |
eb3e3e1d | 1174 | if (sides & 2) |
fb52938e EN |
1175 | strintmap_set(&renames->dirs_removed[2], fullname, |
1176 | relevance); | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * Here's the block that potentially upgrades to RELEVANT_FOR_SELF. | |
1181 | * When we run across a file added to a directory. In such a case, | |
1182 | * find the directory of the file and upgrade its relevance. | |
1183 | */ | |
1184 | if (renames->dir_rename_mask == 0x07 && | |
1185 | (filemask == 2 || filemask == 4)) { | |
1186 | /* | |
1187 | * Need directory rename for parent directory on other side | |
1188 | * of history from added file. Thus | |
1189 | * side = (~filemask & 0x06) >> 1 | |
1190 | * or | |
1191 | * side = 3 - (filemask/2). | |
1192 | */ | |
1193 | unsigned side = 3 - (filemask >> 1); | |
1194 | strintmap_set(&renames->dirs_removed[side], dirname, | |
1195 | RELEVANT_FOR_SELF); | |
eb3e3e1d | 1196 | } |
f78cf976 EN |
1197 | |
1198 | if (filemask == 0 || filemask == 7) | |
1199 | return; | |
1200 | ||
1201 | for (side = MERGE_SIDE1; side <= MERGE_SIDE2; ++side) { | |
1202 | unsigned side_mask = (1 << side); | |
1203 | ||
1204 | /* Check for deletion on side */ | |
1205 | if ((filemask & 1) && !(filemask & side_mask)) | |
32a56dfb | 1206 | add_pair(opt, names, fullname, side, 0 /* delete */, |
2fd9eda4 EN |
1207 | match_mask & filemask, |
1208 | renames->dir_rename_mask); | |
f78cf976 EN |
1209 | |
1210 | /* Check for addition on side */ | |
1211 | if (!(filemask & 1) && (filemask & side_mask)) | |
32a56dfb | 1212 | add_pair(opt, names, fullname, side, 1 /* add */, |
2fd9eda4 EN |
1213 | match_mask & filemask, |
1214 | renames->dir_rename_mask); | |
f78cf976 | 1215 | } |
eb3e3e1d EN |
1216 | } |
1217 | ||
d2bc1994 EN |
1218 | static int collect_merge_info_callback(int n, |
1219 | unsigned long mask, | |
1220 | unsigned long dirmask, | |
1221 | struct name_entry *names, | |
1222 | struct traverse_info *info) | |
1223 | { | |
1224 | /* | |
1225 | * n is 3. Always. | |
1226 | * common ancestor (mbase) has mask 1, and stored in index 0 of names | |
1227 | * head of side 1 (side1) has mask 2, and stored in index 1 of names | |
1228 | * head of side 2 (side2) has mask 4, and stored in index 2 of names | |
1229 | */ | |
1230 | struct merge_options *opt = info->data; | |
1231 | struct merge_options_internal *opti = opt->priv; | |
2fd9eda4 | 1232 | struct rename_info *renames = &opt->priv->renames; |
98bf9841 EN |
1233 | struct string_list_item pi; /* Path Info */ |
1234 | struct conflict_info *ci; /* typed alias to pi.util (which is void*) */ | |
d2bc1994 EN |
1235 | struct name_entry *p; |
1236 | size_t len; | |
1237 | char *fullpath; | |
98bf9841 | 1238 | const char *dirname = opti->current_dir_name; |
2fd9eda4 | 1239 | unsigned prev_dir_rename_mask = renames->dir_rename_mask; |
d2bc1994 | 1240 | unsigned filemask = mask & ~dirmask; |
34e557af | 1241 | unsigned match_mask = 0; /* will be updated below */ |
d2bc1994 EN |
1242 | unsigned mbase_null = !(mask & 1); |
1243 | unsigned side1_null = !(mask & 2); | |
1244 | unsigned side2_null = !(mask & 4); | |
885f0063 EN |
1245 | unsigned side1_matches_mbase = (!side1_null && !mbase_null && |
1246 | names[0].mode == names[1].mode && | |
1247 | oideq(&names[0].oid, &names[1].oid)); | |
1248 | unsigned side2_matches_mbase = (!side2_null && !mbase_null && | |
1249 | names[0].mode == names[2].mode && | |
1250 | oideq(&names[0].oid, &names[2].oid)); | |
1251 | unsigned sides_match = (!side1_null && !side2_null && | |
1252 | names[1].mode == names[2].mode && | |
1253 | oideq(&names[1].oid, &names[2].oid)); | |
d2bc1994 | 1254 | |
34e557af EN |
1255 | /* |
1256 | * Note: When a path is a file on one side of history and a directory | |
1257 | * in another, we have a directory/file conflict. In such cases, if | |
1258 | * the conflict doesn't resolve from renames and deletions, then we | |
1259 | * always leave directories where they are and move files out of the | |
1260 | * way. Thus, while struct conflict_info has a df_conflict field to | |
1261 | * track such conflicts, we ignore that field for any directories at | |
1262 | * a path and only pay attention to it for files at the given path. | |
1263 | * The fact that we leave directories were they are also means that | |
1264 | * we do not need to worry about getting additional df_conflict | |
1265 | * information propagated from parent directories down to children | |
1266 | * (unlike, say traverse_trees_recursive() in unpack-trees.c, which | |
1267 | * sets a newinfo.df_conflicts field specifically to propagate it). | |
1268 | */ | |
1269 | unsigned df_conflict = (filemask != 0) && (dirmask != 0); | |
1270 | ||
d2bc1994 EN |
1271 | /* n = 3 is a fundamental assumption. */ |
1272 | if (n != 3) | |
1273 | BUG("Called collect_merge_info_callback wrong"); | |
1274 | ||
1275 | /* | |
1276 | * A bunch of sanity checks verifying that traverse_trees() calls | |
1277 | * us the way I expect. Could just remove these at some point, | |
1278 | * though maybe they are helpful to future code readers. | |
1279 | */ | |
1280 | assert(mbase_null == is_null_oid(&names[0].oid)); | |
1281 | assert(side1_null == is_null_oid(&names[1].oid)); | |
1282 | assert(side2_null == is_null_oid(&names[2].oid)); | |
1283 | assert(!mbase_null || !side1_null || !side2_null); | |
1284 | assert(mask > 0 && mask < 8); | |
1285 | ||
34e557af EN |
1286 | /* Determine match_mask */ |
1287 | if (side1_matches_mbase) | |
1288 | match_mask = (side2_matches_mbase ? 7 : 3); | |
1289 | else if (side2_matches_mbase) | |
1290 | match_mask = 5; | |
1291 | else if (sides_match) | |
1292 | match_mask = 6; | |
1293 | ||
d2bc1994 EN |
1294 | /* |
1295 | * Get the name of the relevant filepath, which we'll pass to | |
1296 | * setup_path_info() for tracking. | |
1297 | */ | |
1298 | p = names; | |
1299 | while (!p->mode) | |
1300 | p++; | |
1301 | len = traverse_path_len(info, p->pathlen); | |
1302 | ||
1303 | /* +1 in both of the following lines to include the NUL byte */ | |
62a15162 | 1304 | fullpath = mem_pool_alloc(&opt->priv->pool, len + 1); |
d2bc1994 EN |
1305 | make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen); |
1306 | ||
291f29ca EN |
1307 | /* |
1308 | * If mbase, side1, and side2 all match, we can resolve early. Even | |
1309 | * if these are trees, there will be no renames or anything | |
1310 | * underneath. | |
1311 | */ | |
1312 | if (side1_matches_mbase && side2_matches_mbase) { | |
1313 | /* mbase, side1, & side2 all match; use mbase as resolution */ | |
1314 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
528fc51b EN |
1315 | names, names+0, mbase_null, 0 /* df_conflict */, |
1316 | filemask, dirmask, 1 /* resolved */); | |
291f29ca EN |
1317 | return mask; |
1318 | } | |
1319 | ||
785bf208 EN |
1320 | /* |
1321 | * If the sides match, and all three paths are present and are | |
1322 | * files, then we can take either as the resolution. We can't do | |
1323 | * this with trees, because there may be rename sources from the | |
1324 | * merge_base. | |
1325 | */ | |
1326 | if (sides_match && filemask == 0x07) { | |
1327 | /* use side1 (== side2) version as resolution */ | |
1328 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
1329 | names, names+1, side1_null, 0, | |
1330 | filemask, dirmask, 1); | |
1331 | return mask; | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * If side1 matches mbase and all three paths are present and are | |
1336 | * files, then we can use side2 as the resolution. We cannot | |
1337 | * necessarily do so this for trees, because there may be rename | |
1338 | * destinations within side2. | |
1339 | */ | |
1340 | if (side1_matches_mbase && filemask == 0x07) { | |
1341 | /* use side2 version as resolution */ | |
1342 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
1343 | names, names+2, side2_null, 0, | |
1344 | filemask, dirmask, 1); | |
1345 | return mask; | |
1346 | } | |
1347 | ||
1348 | /* Similar to above but swapping sides 1 and 2 */ | |
1349 | if (side2_matches_mbase && filemask == 0x07) { | |
1350 | /* use side1 version as resolution */ | |
1351 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
1352 | names, names+1, side1_null, 0, | |
291f29ca EN |
1353 | filemask, dirmask, 1); |
1354 | return mask; | |
1355 | } | |
1356 | ||
eb3e3e1d | 1357 | /* |
528fc51b EN |
1358 | * Sometimes we can tell that a source path need not be included in |
1359 | * rename detection -- namely, whenever either | |
1360 | * side1_matches_mbase && side2_null | |
1361 | * or | |
1362 | * side2_matches_mbase && side1_null | |
1363 | * However, we call collect_rename_info() even in those cases, | |
1364 | * because exact renames are cheap and would let us remove both a | |
1365 | * source and destination path. We'll cull the unneeded sources | |
1366 | * later. | |
eb3e3e1d EN |
1367 | */ |
1368 | collect_rename_info(opt, names, dirname, fullpath, | |
1369 | filemask, dirmask, match_mask); | |
1370 | ||
d2bc1994 | 1371 | /* |
528fc51b EN |
1372 | * None of the special cases above matched, so we have a |
1373 | * provisional conflict. (Rename detection might allow us to | |
1374 | * unconflict some more cases, but that comes later so all we can | |
1375 | * do now is record the different non-null file hashes.) | |
d2bc1994 | 1376 | */ |
98bf9841 EN |
1377 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, |
1378 | names, NULL, 0, df_conflict, filemask, dirmask, 0); | |
1379 | ||
1380 | ci = pi.util; | |
1381 | VERIFY_CI(ci); | |
34e557af | 1382 | ci->match_mask = match_mask; |
d2bc1994 EN |
1383 | |
1384 | /* If dirmask, recurse into subdirectories */ | |
1385 | if (dirmask) { | |
1386 | struct traverse_info newinfo; | |
1387 | struct tree_desc t[3]; | |
1388 | void *buf[3] = {NULL, NULL, NULL}; | |
1389 | const char *original_dir_name; | |
5e1ca57a | 1390 | int i, ret, side; |
d2bc1994 | 1391 | |
5e1ca57a EN |
1392 | /* |
1393 | * Check for whether we can avoid recursing due to one side | |
1394 | * matching the merge base. The side that does NOT match is | |
1395 | * the one that might have a rename destination we need. | |
1396 | */ | |
1397 | assert(!side1_matches_mbase || !side2_matches_mbase); | |
1398 | side = side1_matches_mbase ? MERGE_SIDE2 : | |
1399 | side2_matches_mbase ? MERGE_SIDE1 : MERGE_BASE; | |
1400 | if (filemask == 0 && (dirmask == 2 || dirmask == 4)) { | |
1401 | /* | |
1402 | * Also defer recursing into new directories; set up a | |
1403 | * few variables to let us do so. | |
1404 | */ | |
1405 | ci->match_mask = (7 - dirmask); | |
1406 | side = dirmask / 2; | |
1407 | } | |
1408 | if (renames->dir_rename_mask != 0x07 && | |
1409 | side != MERGE_BASE && | |
1410 | renames->deferred[side].trivial_merges_okay && | |
1411 | !strset_contains(&renames->deferred[side].target_dirs, | |
1412 | pi.string)) { | |
1413 | strintmap_set(&renames->deferred[side].possible_trivial_merges, | |
1414 | pi.string, renames->dir_rename_mask); | |
1415 | renames->dir_rename_mask = prev_dir_rename_mask; | |
1416 | return mask; | |
1417 | } | |
1418 | ||
1419 | /* We need to recurse */ | |
d2bc1994 EN |
1420 | ci->match_mask &= filemask; |
1421 | newinfo = *info; | |
1422 | newinfo.prev = info; | |
1423 | newinfo.name = p->path; | |
1424 | newinfo.namelen = p->pathlen; | |
1425 | newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1); | |
34e557af EN |
1426 | /* |
1427 | * If this directory we are about to recurse into cared about | |
1428 | * its parent directory (the current directory) having a D/F | |
1429 | * conflict, then we'd propagate the masks in this way: | |
1430 | * newinfo.df_conflicts |= (mask & ~dirmask); | |
1431 | * But we don't worry about propagating D/F conflicts. (See | |
1432 | * comment near setting of local df_conflict variable near | |
1433 | * the beginning of this function). | |
1434 | */ | |
d2bc1994 EN |
1435 | |
1436 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
885f0063 EN |
1437 | if (i == 1 && side1_matches_mbase) |
1438 | t[1] = t[0]; | |
1439 | else if (i == 2 && side2_matches_mbase) | |
1440 | t[2] = t[0]; | |
1441 | else if (i == 2 && sides_match) | |
1442 | t[2] = t[1]; | |
1443 | else { | |
1444 | const struct object_id *oid = NULL; | |
1445 | if (dirmask & 1) | |
1446 | oid = &names[i].oid; | |
1447 | buf[i] = fill_tree_descriptor(opt->repo, | |
1448 | t + i, oid); | |
1449 | } | |
d2bc1994 EN |
1450 | dirmask >>= 1; |
1451 | } | |
1452 | ||
1453 | original_dir_name = opti->current_dir_name; | |
98bf9841 | 1454 | opti->current_dir_name = pi.string; |
2fd9eda4 EN |
1455 | if (renames->dir_rename_mask == 0 || |
1456 | renames->dir_rename_mask == 0x07) | |
1457 | ret = traverse_trees(NULL, 3, t, &newinfo); | |
1458 | else | |
1459 | ret = traverse_trees_wrapper(NULL, 3, t, &newinfo); | |
d2bc1994 | 1460 | opti->current_dir_name = original_dir_name; |
2fd9eda4 | 1461 | renames->dir_rename_mask = prev_dir_rename_mask; |
d2bc1994 EN |
1462 | |
1463 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) | |
1464 | free(buf[i]); | |
1465 | ||
1466 | if (ret < 0) | |
1467 | return -1; | |
1468 | } | |
1469 | ||
1470 | return mask; | |
1471 | } | |
1472 | ||
7bee6c10 EN |
1473 | static void resolve_trivial_directory_merge(struct conflict_info *ci, int side) |
1474 | { | |
1475 | VERIFY_CI(ci); | |
1476 | assert((side == 1 && ci->match_mask == 5) || | |
1477 | (side == 2 && ci->match_mask == 3)); | |
1478 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
1479 | ci->merged.result.mode = ci->stages[side].mode; | |
1480 | ci->merged.is_null = is_null_oid(&ci->stages[side].oid); | |
1481 | ci->match_mask = 0; | |
1482 | ci->merged.clean = 1; /* (ci->filemask == 0); */ | |
1483 | } | |
1484 | ||
e0ef578e EN |
1485 | static int handle_deferred_entries(struct merge_options *opt, |
1486 | struct traverse_info *info) | |
1487 | { | |
1488 | struct rename_info *renames = &opt->priv->renames; | |
1489 | struct hashmap_iter iter; | |
1490 | struct strmap_entry *entry; | |
1491 | int side, ret = 0; | |
8b09a900 | 1492 | int path_count_before, path_count_after = 0; |
e0ef578e | 1493 | |
8b09a900 | 1494 | path_count_before = strmap_get_size(&opt->priv->paths); |
e0ef578e | 1495 | for (side = MERGE_SIDE1; side <= MERGE_SIDE2; side++) { |
7bee6c10 EN |
1496 | unsigned optimization_okay = 1; |
1497 | struct strintmap copy; | |
1498 | ||
1499 | /* Loop over the set of paths we need to know rename info for */ | |
1500 | strset_for_each_entry(&renames->relevant_sources[side], | |
1501 | &iter, entry) { | |
1502 | char *rename_target, *dir, *dir_marker; | |
1503 | struct strmap_entry *e; | |
1504 | ||
1505 | /* | |
1506 | * If we don't know delete/rename info for this path, | |
1507 | * then we need to recurse into all trees to get all | |
1508 | * adds to make sure we have it. | |
1509 | */ | |
1510 | if (strset_contains(&renames->cached_irrelevant[side], | |
1511 | entry->key)) | |
1512 | continue; | |
1513 | e = strmap_get_entry(&renames->cached_pairs[side], | |
1514 | entry->key); | |
1515 | if (!e) { | |
1516 | optimization_okay = 0; | |
1517 | break; | |
1518 | } | |
1519 | ||
1520 | /* If this is a delete, we have enough info already */ | |
1521 | rename_target = e->value; | |
1522 | if (!rename_target) | |
1523 | continue; | |
1524 | ||
1525 | /* If we already walked the rename target, we're good */ | |
1526 | if (strmap_contains(&opt->priv->paths, rename_target)) | |
1527 | continue; | |
1528 | ||
1529 | /* | |
1530 | * Otherwise, we need to get a list of directories that | |
1531 | * will need to be recursed into to get this | |
1532 | * rename_target. | |
1533 | */ | |
1534 | dir = xstrdup(rename_target); | |
1535 | while ((dir_marker = strrchr(dir, '/'))) { | |
1536 | *dir_marker = '\0'; | |
1537 | if (strset_contains(&renames->deferred[side].target_dirs, | |
1538 | dir)) | |
1539 | break; | |
1540 | strset_add(&renames->deferred[side].target_dirs, | |
1541 | dir); | |
1542 | } | |
1543 | free(dir); | |
1544 | } | |
1545 | renames->deferred[side].trivial_merges_okay = optimization_okay; | |
1546 | /* | |
1547 | * We need to recurse into any directories in | |
1548 | * possible_trivial_merges[side] found in target_dirs[side]. | |
1549 | * But when we recurse, we may need to queue up some of the | |
1550 | * subdirectories for possible_trivial_merges[side]. Since | |
1551 | * we can't safely iterate through a hashmap while also adding | |
1552 | * entries, move the entries into 'copy', iterate over 'copy', | |
1553 | * and then we'll also iterate anything added into | |
1554 | * possible_trivial_merges[side] once this loop is done. | |
1555 | */ | |
1556 | copy = renames->deferred[side].possible_trivial_merges; | |
1557 | strintmap_init_with_options(&renames->deferred[side].possible_trivial_merges, | |
1558 | 0, | |
62a15162 | 1559 | &opt->priv->pool, |
7bee6c10 EN |
1560 | 0); |
1561 | strintmap_for_each_entry(©, &iter, entry) { | |
e0ef578e EN |
1562 | const char *path = entry->key; |
1563 | unsigned dir_rename_mask = (intptr_t)entry->value; | |
1564 | struct conflict_info *ci; | |
1565 | unsigned dirmask; | |
1566 | struct tree_desc t[3]; | |
1567 | void *buf[3] = {NULL,}; | |
1568 | int i; | |
1569 | ||
1570 | ci = strmap_get(&opt->priv->paths, path); | |
1571 | VERIFY_CI(ci); | |
1572 | dirmask = ci->dirmask; | |
1573 | ||
7bee6c10 EN |
1574 | if (optimization_okay && |
1575 | !strset_contains(&renames->deferred[side].target_dirs, | |
1576 | path)) { | |
1577 | resolve_trivial_directory_merge(ci, side); | |
1578 | continue; | |
1579 | } | |
1580 | ||
e0ef578e EN |
1581 | info->name = path; |
1582 | info->namelen = strlen(path); | |
1583 | info->pathlen = info->namelen + 1; | |
1584 | ||
1585 | for (i = 0; i < 3; i++, dirmask >>= 1) { | |
1586 | if (i == 1 && ci->match_mask == 3) | |
1587 | t[1] = t[0]; | |
1588 | else if (i == 2 && ci->match_mask == 5) | |
1589 | t[2] = t[0]; | |
1590 | else if (i == 2 && ci->match_mask == 6) | |
1591 | t[2] = t[1]; | |
1592 | else { | |
1593 | const struct object_id *oid = NULL; | |
1594 | if (dirmask & 1) | |
1595 | oid = &ci->stages[i].oid; | |
1596 | buf[i] = fill_tree_descriptor(opt->repo, | |
1597 | t+i, oid); | |
1598 | } | |
1599 | } | |
1600 | ||
1601 | ci->match_mask &= ci->filemask; | |
1602 | opt->priv->current_dir_name = path; | |
1603 | renames->dir_rename_mask = dir_rename_mask; | |
1604 | if (renames->dir_rename_mask == 0 || | |
1605 | renames->dir_rename_mask == 0x07) | |
1606 | ret = traverse_trees(NULL, 3, t, info); | |
1607 | else | |
1608 | ret = traverse_trees_wrapper(NULL, 3, t, info); | |
1609 | ||
1610 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) | |
1611 | free(buf[i]); | |
1612 | ||
1613 | if (ret < 0) | |
1614 | return ret; | |
1615 | } | |
7bee6c10 EN |
1616 | strintmap_clear(©); |
1617 | strintmap_for_each_entry(&renames->deferred[side].possible_trivial_merges, | |
1618 | &iter, entry) { | |
1619 | const char *path = entry->key; | |
1620 | struct conflict_info *ci; | |
1621 | ||
1622 | ci = strmap_get(&opt->priv->paths, path); | |
1623 | VERIFY_CI(ci); | |
1624 | ||
1625 | assert(renames->deferred[side].trivial_merges_okay && | |
1626 | !strset_contains(&renames->deferred[side].target_dirs, | |
1627 | path)); | |
1628 | resolve_trivial_directory_merge(ci, side); | |
1629 | } | |
8b09a900 EN |
1630 | if (!optimization_okay || path_count_after) |
1631 | path_count_after = strmap_get_size(&opt->priv->paths); | |
e0ef578e | 1632 | } |
8b09a900 EN |
1633 | if (path_count_after) { |
1634 | /* | |
1635 | * The choice of wanted_factor here does not affect | |
1636 | * correctness, only performance. When the | |
1637 | * path_count_after / path_count_before | |
1638 | * ratio is high, redoing after renames is a big | |
1639 | * performance boost. I suspect that redoing is a wash | |
1640 | * somewhere near a value of 2, and below that redoing will | |
1641 | * slow things down. I applied a fudge factor and picked | |
1642 | * 3; see the commit message when this was introduced for | |
1643 | * back of the envelope calculations for this ratio. | |
1644 | */ | |
1645 | const int wanted_factor = 3; | |
1646 | ||
1647 | /* We should only redo collect_merge_info one time */ | |
1648 | assert(renames->redo_after_renames == 0); | |
1649 | ||
1650 | if (path_count_after / path_count_before >= wanted_factor) { | |
1651 | renames->redo_after_renames = 1; | |
1652 | renames->cached_pairs_valid_side = -1; | |
1653 | } | |
1654 | } else if (renames->redo_after_renames == 2) | |
1655 | renames->redo_after_renames = 0; | |
e0ef578e EN |
1656 | return ret; |
1657 | } | |
1658 | ||
231e2dd4 EN |
1659 | static int collect_merge_info(struct merge_options *opt, |
1660 | struct tree *merge_base, | |
1661 | struct tree *side1, | |
1662 | struct tree *side2) | |
1663 | { | |
d2bc1994 EN |
1664 | int ret; |
1665 | struct tree_desc t[3]; | |
1666 | struct traverse_info info; | |
d2bc1994 | 1667 | |
05b85c6e EN |
1668 | opt->priv->toplevel_dir = ""; |
1669 | opt->priv->current_dir_name = opt->priv->toplevel_dir; | |
1670 | setup_traverse_info(&info, opt->priv->toplevel_dir); | |
d2bc1994 EN |
1671 | info.fn = collect_merge_info_callback; |
1672 | info.data = opt; | |
1673 | info.show_all_errors = 1; | |
1674 | ||
1675 | parse_tree(merge_base); | |
1676 | parse_tree(side1); | |
1677 | parse_tree(side2); | |
1678 | init_tree_desc(t + 0, merge_base->buffer, merge_base->size); | |
1679 | init_tree_desc(t + 1, side1->buffer, side1->size); | |
1680 | init_tree_desc(t + 2, side2->buffer, side2->size); | |
1681 | ||
557ac035 | 1682 | trace2_region_enter("merge", "traverse_trees", opt->repo); |
d2bc1994 | 1683 | ret = traverse_trees(NULL, 3, t, &info); |
5e1ca57a EN |
1684 | if (ret == 0) |
1685 | ret = handle_deferred_entries(opt, &info); | |
557ac035 | 1686 | trace2_region_leave("merge", "traverse_trees", opt->repo); |
d2bc1994 EN |
1687 | |
1688 | return ret; | |
231e2dd4 EN |
1689 | } |
1690 | ||
04af1879 EN |
1691 | /*** Function Grouping: functions related to threeway content merges ***/ |
1692 | ||
c73cda76 EN |
1693 | static int find_first_merges(struct repository *repo, |
1694 | const char *path, | |
1695 | struct commit *a, | |
1696 | struct commit *b, | |
1697 | struct object_array *result) | |
1698 | { | |
4204cd59 EN |
1699 | int i, j; |
1700 | struct object_array merges = OBJECT_ARRAY_INIT; | |
1701 | struct commit *commit; | |
1702 | int contains_another; | |
1703 | ||
1704 | char merged_revision[GIT_MAX_HEXSZ + 2]; | |
1705 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
1706 | "--all", merged_revision, NULL }; | |
1707 | struct rev_info revs; | |
1708 | struct setup_revision_opt rev_opts; | |
1709 | ||
1710 | memset(result, 0, sizeof(struct object_array)); | |
1711 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
1712 | ||
1713 | /* get all revisions that merge commit a */ | |
1714 | xsnprintf(merged_revision, sizeof(merged_revision), "^%s", | |
1715 | oid_to_hex(&a->object.oid)); | |
1716 | repo_init_revisions(repo, &revs, NULL); | |
4204cd59 EN |
1717 | /* FIXME: can't handle linked worktrees in submodules yet */ |
1718 | revs.single_worktree = path != NULL; | |
1719 | setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); | |
1720 | ||
1721 | /* save all revisions from the above list that contain b */ | |
1722 | if (prepare_revision_walk(&revs)) | |
1723 | die("revision walk setup failed"); | |
1724 | while ((commit = get_revision(&revs)) != NULL) { | |
1725 | struct object *o = &(commit->object); | |
10a0d6ae | 1726 | if (repo_in_merge_bases(repo, b, commit)) |
4204cd59 EN |
1727 | add_object_array(o, NULL, &merges); |
1728 | } | |
1729 | reset_revision_walk(); | |
1730 | ||
1731 | /* Now we've got all merges that contain a and b. Prune all | |
1732 | * merges that contain another found merge and save them in | |
1733 | * result. | |
1734 | */ | |
1735 | for (i = 0; i < merges.nr; i++) { | |
1736 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
1737 | ||
1738 | contains_another = 0; | |
1739 | for (j = 0; j < merges.nr; j++) { | |
1740 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
10a0d6ae | 1741 | if (i != j && repo_in_merge_bases(repo, m2, m1)) { |
4204cd59 EN |
1742 | contains_another = 1; |
1743 | break; | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | if (!contains_another) | |
1748 | add_object_array(merges.objects[i].item, NULL, result); | |
1749 | } | |
1750 | ||
1751 | object_array_clear(&merges); | |
2108fe4a | 1752 | release_revisions(&revs); |
4204cd59 | 1753 | return result->nr; |
c73cda76 EN |
1754 | } |
1755 | ||
62fdec17 EN |
1756 | static int merge_submodule(struct merge_options *opt, |
1757 | const char *path, | |
1758 | const struct object_id *o, | |
1759 | const struct object_id *a, | |
1760 | const struct object_id *b, | |
1761 | struct object_id *result) | |
1762 | { | |
10a0d6ae JT |
1763 | struct repository subrepo; |
1764 | struct strbuf sb = STRBUF_INIT; | |
1765 | int ret = 0; | |
c73cda76 EN |
1766 | struct commit *commit_o, *commit_a, *commit_b; |
1767 | int parent_count; | |
1768 | struct object_array merges; | |
c73cda76 EN |
1769 | |
1770 | int i; | |
1771 | int search = !opt->priv->call_depth; | |
4057523a | 1772 | int sub_not_initialized = 1; |
34ce504a | 1773 | int sub_flag = CONFLICT_SUBMODULE_FAILED_TO_MERGE; |
c73cda76 EN |
1774 | |
1775 | /* store fallback answer in result in case we fail */ | |
1776 | oidcpy(result, opt->priv->call_depth ? o : a); | |
1777 | ||
1778 | /* we can not handle deletion conflicts */ | |
4057523a CW |
1779 | if (is_null_oid(a) || is_null_oid(b)) |
1780 | BUG("submodule deleted on one side; this should be handled outside of merge_submodule()"); | |
c73cda76 | 1781 | |
4057523a CW |
1782 | if ((sub_not_initialized = repo_submodule_init(&subrepo, |
1783 | opt->repo, path, null_oid()))) { | |
cb260775 EN |
1784 | path_msg(opt, CONFLICT_SUBMODULE_NOT_INITIALIZED, 0, |
1785 | path, NULL, NULL, NULL, | |
1786 | _("Failed to merge submodule %s (not checked out)"), | |
1787 | path); | |
4057523a CW |
1788 | sub_flag = CONFLICT_SUBMODULE_NOT_INITIALIZED; |
1789 | goto cleanup; | |
1790 | } | |
1791 | ||
1792 | if (is_null_oid(o)) { | |
1793 | path_msg(opt, CONFLICT_SUBMODULE_NULL_MERGE_BASE, 0, | |
1794 | path, NULL, NULL, NULL, | |
1795 | _("Failed to merge submodule %s (no merge base)"), | |
1796 | path); | |
1797 | goto cleanup; | |
10a0d6ae JT |
1798 | } |
1799 | ||
1800 | if (!(commit_o = lookup_commit_reference(&subrepo, o)) || | |
1801 | !(commit_a = lookup_commit_reference(&subrepo, a)) || | |
1802 | !(commit_b = lookup_commit_reference(&subrepo, b))) { | |
cb260775 EN |
1803 | path_msg(opt, CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE, 0, |
1804 | path, NULL, NULL, NULL, | |
c73cda76 EN |
1805 | _("Failed to merge submodule %s (commits not present)"), |
1806 | path); | |
4057523a | 1807 | sub_flag = CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE; |
10a0d6ae | 1808 | goto cleanup; |
c73cda76 EN |
1809 | } |
1810 | ||
1811 | /* check whether both changes are forward */ | |
10a0d6ae JT |
1812 | if (!repo_in_merge_bases(&subrepo, commit_o, commit_a) || |
1813 | !repo_in_merge_bases(&subrepo, commit_o, commit_b)) { | |
cb260775 EN |
1814 | path_msg(opt, CONFLICT_SUBMODULE_MAY_HAVE_REWINDS, 0, |
1815 | path, NULL, NULL, NULL, | |
c73cda76 EN |
1816 | _("Failed to merge submodule %s " |
1817 | "(commits don't follow merge-base)"), | |
1818 | path); | |
10a0d6ae | 1819 | goto cleanup; |
c73cda76 EN |
1820 | } |
1821 | ||
1822 | /* Case #1: a is contained in b or vice versa */ | |
10a0d6ae | 1823 | if (repo_in_merge_bases(&subrepo, commit_a, commit_b)) { |
c73cda76 | 1824 | oidcpy(result, b); |
cb260775 EN |
1825 | path_msg(opt, INFO_SUBMODULE_FAST_FORWARDING, 1, |
1826 | path, NULL, NULL, NULL, | |
c73cda76 EN |
1827 | _("Note: Fast-forwarding submodule %s to %s"), |
1828 | path, oid_to_hex(b)); | |
10a0d6ae JT |
1829 | ret = 1; |
1830 | goto cleanup; | |
c73cda76 | 1831 | } |
10a0d6ae | 1832 | if (repo_in_merge_bases(&subrepo, commit_b, commit_a)) { |
c73cda76 | 1833 | oidcpy(result, a); |
cb260775 EN |
1834 | path_msg(opt, INFO_SUBMODULE_FAST_FORWARDING, 1, |
1835 | path, NULL, NULL, NULL, | |
c73cda76 EN |
1836 | _("Note: Fast-forwarding submodule %s to %s"), |
1837 | path, oid_to_hex(a)); | |
10a0d6ae JT |
1838 | ret = 1; |
1839 | goto cleanup; | |
c73cda76 EN |
1840 | } |
1841 | ||
1842 | /* | |
1843 | * Case #2: There are one or more merges that contain a and b in | |
1844 | * the submodule. If there is only one, then present it as a | |
1845 | * suggestion to the user, but leave it marked unmerged so the | |
1846 | * user needs to confirm the resolution. | |
1847 | */ | |
1848 | ||
1849 | /* Skip the search if makes no sense to the calling context. */ | |
1850 | if (!search) | |
10a0d6ae | 1851 | goto cleanup; |
c73cda76 EN |
1852 | |
1853 | /* find commit which merges them */ | |
10a0d6ae | 1854 | parent_count = find_first_merges(&subrepo, path, commit_a, commit_b, |
c73cda76 EN |
1855 | &merges); |
1856 | switch (parent_count) { | |
1857 | case 0: | |
cb260775 EN |
1858 | path_msg(opt, CONFLICT_SUBMODULE_FAILED_TO_MERGE, 0, |
1859 | path, NULL, NULL, NULL, | |
1860 | _("Failed to merge submodule %s"), path); | |
c73cda76 EN |
1861 | break; |
1862 | ||
1863 | case 1: | |
155b517d | 1864 | format_commit(&sb, 4, &subrepo, |
c73cda76 | 1865 | (struct commit *)merges.objects[0].item); |
cb260775 EN |
1866 | path_msg(opt, CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION, 0, |
1867 | path, NULL, NULL, NULL, | |
c73cda76 | 1868 | _("Failed to merge submodule %s, but a possible merge " |
a4040cfa | 1869 | "resolution exists: %s"), |
c73cda76 | 1870 | path, sb.buf); |
c73cda76 EN |
1871 | strbuf_release(&sb); |
1872 | break; | |
1873 | default: | |
1874 | for (i = 0; i < merges.nr; i++) | |
155b517d | 1875 | format_commit(&sb, 4, &subrepo, |
c73cda76 | 1876 | (struct commit *)merges.objects[i].item); |
cb260775 EN |
1877 | path_msg(opt, CONFLICT_SUBMODULE_FAILED_TO_MERGE_BUT_POSSIBLE_RESOLUTION, 0, |
1878 | path, NULL, NULL, NULL, | |
c73cda76 EN |
1879 | _("Failed to merge submodule %s, but multiple " |
1880 | "possible merges exist:\n%s"), path, sb.buf); | |
1881 | strbuf_release(&sb); | |
1882 | } | |
1883 | ||
1884 | object_array_clear(&merges); | |
10a0d6ae | 1885 | cleanup: |
4057523a CW |
1886 | if (!opt->priv->call_depth && !ret) { |
1887 | struct string_list *csub = &opt->priv->conflicted_submodules; | |
1888 | struct conflicted_submodule_item *util; | |
1889 | const char *abbrev; | |
1890 | ||
1891 | util = xmalloc(sizeof(*util)); | |
1892 | util->flag = sub_flag; | |
1893 | util->abbrev = NULL; | |
1894 | if (!sub_not_initialized) { | |
1895 | abbrev = repo_find_unique_abbrev(&subrepo, b, DEFAULT_ABBREV); | |
1896 | util->abbrev = xstrdup(abbrev); | |
1897 | } | |
1898 | string_list_append(csub, path)->util = util; | |
1899 | } | |
1900 | ||
1901 | if (!sub_not_initialized) | |
1902 | repo_clear(&subrepo); | |
10a0d6ae | 1903 | return ret; |
62fdec17 EN |
1904 | } |
1905 | ||
1218b3ab EN |
1906 | static void initialize_attr_index(struct merge_options *opt) |
1907 | { | |
1908 | /* | |
1909 | * The renormalize_buffer() functions require attributes, and | |
1910 | * annoyingly those can only be read from the working tree or from | |
1911 | * an index_state. merge-ort doesn't have an index_state, so we | |
1912 | * generate a fake one containing only attribute information. | |
1913 | */ | |
1914 | struct merged_info *mi; | |
1915 | struct index_state *attr_index = &opt->priv->attr_index; | |
1916 | struct cache_entry *ce; | |
1917 | ||
1918 | attr_index->initialized = 1; | |
1919 | ||
1920 | if (!opt->renormalize) | |
1921 | return; | |
1922 | ||
1923 | mi = strmap_get(&opt->priv->paths, GITATTRIBUTES_FILE); | |
1924 | if (!mi) | |
1925 | return; | |
1926 | ||
1927 | if (mi->clean) { | |
1928 | int len = strlen(GITATTRIBUTES_FILE); | |
1929 | ce = make_empty_cache_entry(attr_index, len); | |
1930 | ce->ce_mode = create_ce_mode(mi->result.mode); | |
1931 | ce->ce_flags = create_ce_flags(0); | |
1932 | ce->ce_namelen = len; | |
1933 | oidcpy(&ce->oid, &mi->result.oid); | |
1934 | memcpy(ce->name, GITATTRIBUTES_FILE, len); | |
1935 | add_index_entry(attr_index, ce, | |
1936 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
1937 | get_stream_filter(attr_index, GITATTRIBUTES_FILE, &ce->oid); | |
1938 | } else { | |
1939 | int stage, len; | |
1940 | struct conflict_info *ci; | |
1941 | ||
1942 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
1943 | for (stage = 0; stage < 3; stage++) { | |
1944 | unsigned stage_mask = (1 << stage); | |
1945 | ||
1946 | if (!(ci->filemask & stage_mask)) | |
1947 | continue; | |
1948 | len = strlen(GITATTRIBUTES_FILE); | |
1949 | ce = make_empty_cache_entry(attr_index, len); | |
1950 | ce->ce_mode = create_ce_mode(ci->stages[stage].mode); | |
1951 | ce->ce_flags = create_ce_flags(stage); | |
1952 | ce->ce_namelen = len; | |
1953 | oidcpy(&ce->oid, &ci->stages[stage].oid); | |
1954 | memcpy(ce->name, GITATTRIBUTES_FILE, len); | |
1955 | add_index_entry(attr_index, ce, | |
1956 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
1957 | get_stream_filter(attr_index, GITATTRIBUTES_FILE, | |
1958 | &ce->oid); | |
1959 | } | |
1960 | } | |
1961 | } | |
1962 | ||
62fdec17 EN |
1963 | static int merge_3way(struct merge_options *opt, |
1964 | const char *path, | |
1965 | const struct object_id *o, | |
1966 | const struct object_id *a, | |
1967 | const struct object_id *b, | |
1968 | const char *pathnames[3], | |
1969 | const int extra_marker_size, | |
1970 | mmbuffer_t *result_buf) | |
1971 | { | |
f591c472 EN |
1972 | mmfile_t orig, src1, src2; |
1973 | struct ll_merge_options ll_opts = {0}; | |
1974 | char *base, *name1, *name2; | |
35f69671 | 1975 | enum ll_merge_result merge_status; |
f591c472 | 1976 | |
1218b3ab EN |
1977 | if (!opt->priv->attr_index.initialized) |
1978 | initialize_attr_index(opt); | |
1979 | ||
f591c472 EN |
1980 | ll_opts.renormalize = opt->renormalize; |
1981 | ll_opts.extra_marker_size = extra_marker_size; | |
1982 | ll_opts.xdl_opts = opt->xdl_opts; | |
1983 | ||
1984 | if (opt->priv->call_depth) { | |
1985 | ll_opts.virtual_ancestor = 1; | |
1986 | ll_opts.variant = 0; | |
1987 | } else { | |
1988 | switch (opt->recursive_variant) { | |
1989 | case MERGE_VARIANT_OURS: | |
1990 | ll_opts.variant = XDL_MERGE_FAVOR_OURS; | |
1991 | break; | |
1992 | case MERGE_VARIANT_THEIRS: | |
1993 | ll_opts.variant = XDL_MERGE_FAVOR_THEIRS; | |
1994 | break; | |
1995 | default: | |
1996 | ll_opts.variant = 0; | |
1997 | break; | |
1998 | } | |
1999 | } | |
2000 | ||
2001 | assert(pathnames[0] && pathnames[1] && pathnames[2] && opt->ancestor); | |
2002 | if (pathnames[0] == pathnames[1] && pathnames[1] == pathnames[2]) { | |
2003 | base = mkpathdup("%s", opt->ancestor); | |
2004 | name1 = mkpathdup("%s", opt->branch1); | |
2005 | name2 = mkpathdup("%s", opt->branch2); | |
2006 | } else { | |
2007 | base = mkpathdup("%s:%s", opt->ancestor, pathnames[0]); | |
2008 | name1 = mkpathdup("%s:%s", opt->branch1, pathnames[1]); | |
2009 | name2 = mkpathdup("%s:%s", opt->branch2, pathnames[2]); | |
2010 | } | |
2011 | ||
2012 | read_mmblob(&orig, o); | |
2013 | read_mmblob(&src1, a); | |
2014 | read_mmblob(&src2, b); | |
2015 | ||
2016 | merge_status = ll_merge(result_buf, path, &orig, base, | |
2017 | &src1, name1, &src2, name2, | |
1218b3ab | 2018 | &opt->priv->attr_index, &ll_opts); |
35f69671 | 2019 | if (merge_status == LL_MERGE_BINARY_CONFLICT) |
cb260775 EN |
2020 | path_msg(opt, CONFLICT_BINARY, 0, |
2021 | path, NULL, NULL, NULL, | |
24dbdab5 EN |
2022 | "warning: Cannot merge binary files: %s (%s vs. %s)", |
2023 | path, name1, name2); | |
f591c472 EN |
2024 | |
2025 | free(base); | |
2026 | free(name1); | |
2027 | free(name2); | |
2028 | free(orig.ptr); | |
2029 | free(src1.ptr); | |
2030 | free(src2.ptr); | |
2031 | return merge_status; | |
62fdec17 EN |
2032 | } |
2033 | ||
e2e9dc03 EN |
2034 | static int handle_content_merge(struct merge_options *opt, |
2035 | const char *path, | |
2036 | const struct version_info *o, | |
2037 | const struct version_info *a, | |
2038 | const struct version_info *b, | |
2039 | const char *pathnames[3], | |
2040 | const int extra_marker_size, | |
2041 | struct version_info *result) | |
2042 | { | |
991bbdca | 2043 | /* |
62fdec17 EN |
2044 | * path is the target location where we want to put the file, and |
2045 | * is used to determine any normalization rules in ll_merge. | |
2046 | * | |
2047 | * The normal case is that path and all entries in pathnames are | |
2048 | * identical, though renames can affect which path we got one of | |
2049 | * the three blobs to merge on various sides of history. | |
2050 | * | |
2051 | * extra_marker_size is the amount to extend conflict markers in | |
2052 | * ll_merge; this is neeed if we have content merges of content | |
2053 | * merges, which happens for example with rename/rename(2to1) and | |
2054 | * rename/add conflicts. | |
2055 | */ | |
2056 | unsigned clean = 1; | |
2057 | ||
2058 | /* | |
2059 | * handle_content_merge() needs both files to be of the same type, i.e. | |
2060 | * both files OR both submodules OR both symlinks. Conflicting types | |
2061 | * needs to be handled elsewhere. | |
991bbdca | 2062 | */ |
62fdec17 EN |
2063 | assert((S_IFMT & a->mode) == (S_IFMT & b->mode)); |
2064 | ||
2065 | /* Merge modes */ | |
2066 | if (a->mode == b->mode || a->mode == o->mode) | |
2067 | result->mode = b->mode; | |
2068 | else { | |
2069 | /* must be the 100644/100755 case */ | |
2070 | assert(S_ISREG(a->mode)); | |
2071 | result->mode = a->mode; | |
2072 | clean = (b->mode == o->mode); | |
2073 | /* | |
2074 | * FIXME: If opt->priv->call_depth && !clean, then we really | |
2075 | * should not make result->mode match either a->mode or | |
2076 | * b->mode; that causes t6036 "check conflicting mode for | |
2077 | * regular file" to fail. It would be best to use some other | |
2078 | * mode, but we'll confuse all kinds of stuff if we use one | |
2079 | * where S_ISREG(result->mode) isn't true, and if we use | |
2080 | * something like 0100666, then tree-walk.c's calls to | |
2081 | * canon_mode() will just normalize that to 100644 for us and | |
2082 | * thus not solve anything. | |
2083 | * | |
2084 | * Figure out if there's some kind of way we can work around | |
2085 | * this... | |
2086 | */ | |
2087 | } | |
2088 | ||
2089 | /* | |
2090 | * Trivial oid merge. | |
2091 | * | |
2092 | * Note: While one might assume that the next four lines would | |
2093 | * be unnecessary due to the fact that match_mask is often | |
2094 | * setup and already handled, renames don't always take care | |
2095 | * of that. | |
2096 | */ | |
2097 | if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid)) | |
2098 | oidcpy(&result->oid, &b->oid); | |
2099 | else if (oideq(&b->oid, &o->oid)) | |
2100 | oidcpy(&result->oid, &a->oid); | |
2101 | ||
2102 | /* Remaining rules depend on file vs. submodule vs. symlink. */ | |
2103 | else if (S_ISREG(a->mode)) { | |
2104 | mmbuffer_t result_buf; | |
2105 | int ret = 0, merge_status; | |
2106 | int two_way; | |
2107 | ||
2108 | /* | |
2109 | * If 'o' is different type, treat it as null so we do a | |
2110 | * two-way merge. | |
2111 | */ | |
2112 | two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
2113 | ||
2114 | merge_status = merge_3way(opt, path, | |
14228447 | 2115 | two_way ? null_oid() : &o->oid, |
62fdec17 EN |
2116 | &a->oid, &b->oid, |
2117 | pathnames, extra_marker_size, | |
2118 | &result_buf); | |
2119 | ||
2120 | if ((merge_status < 0) || !result_buf.ptr) | |
2121 | ret = err(opt, _("Failed to execute internal merge")); | |
2122 | ||
2123 | if (!ret && | |
2124 | write_object_file(result_buf.ptr, result_buf.size, | |
c80d226a | 2125 | OBJ_BLOB, &result->oid)) |
62fdec17 EN |
2126 | ret = err(opt, _("Unable to add %s to database"), |
2127 | path); | |
2128 | ||
2129 | free(result_buf.ptr); | |
2130 | if (ret) | |
2131 | return -1; | |
2132 | clean &= (merge_status == 0); | |
cb260775 EN |
2133 | path_msg(opt, INFO_AUTO_MERGING, 1, path, NULL, NULL, NULL, |
2134 | _("Auto-merging %s"), path); | |
62fdec17 EN |
2135 | } else if (S_ISGITLINK(a->mode)) { |
2136 | int two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
2137 | clean = merge_submodule(opt, pathnames[0], | |
14228447 | 2138 | two_way ? null_oid() : &o->oid, |
62fdec17 EN |
2139 | &a->oid, &b->oid, &result->oid); |
2140 | if (opt->priv->call_depth && two_way && !clean) { | |
2141 | result->mode = o->mode; | |
2142 | oidcpy(&result->oid, &o->oid); | |
2143 | } | |
2144 | } else if (S_ISLNK(a->mode)) { | |
2145 | if (opt->priv->call_depth) { | |
2146 | clean = 0; | |
2147 | result->mode = o->mode; | |
2148 | oidcpy(&result->oid, &o->oid); | |
2149 | } else { | |
2150 | switch (opt->recursive_variant) { | |
2151 | case MERGE_VARIANT_NORMAL: | |
2152 | clean = 0; | |
2153 | oidcpy(&result->oid, &a->oid); | |
2154 | break; | |
2155 | case MERGE_VARIANT_OURS: | |
2156 | oidcpy(&result->oid, &a->oid); | |
2157 | break; | |
2158 | case MERGE_VARIANT_THEIRS: | |
2159 | oidcpy(&result->oid, &b->oid); | |
2160 | break; | |
2161 | } | |
2162 | } | |
2163 | } else | |
2164 | BUG("unsupported object type in the tree: %06o for %s", | |
2165 | a->mode, path); | |
2166 | ||
991bbdca | 2167 | return clean; |
e2e9dc03 EN |
2168 | } |
2169 | ||
04af1879 EN |
2170 | /*** Function Grouping: functions related to detect_and_process_renames(), *** |
2171 | *** which are split into directory and regular rename detection sections. ***/ | |
2172 | ||
2173 | /*** Function Grouping: functions related to directory rename detection ***/ | |
2174 | ||
fa5e06d6 EN |
2175 | struct collision_info { |
2176 | struct string_list source_files; | |
2177 | unsigned reported_already:1; | |
2178 | }; | |
2179 | ||
d9d015df EN |
2180 | /* |
2181 | * Return a new string that replaces the beginning portion (which matches | |
2182 | * rename_info->key), with rename_info->util.new_dir. In perl-speak: | |
2183 | * new_path_name = (old_path =~ s/rename_info->key/rename_info->value/); | |
2184 | * NOTE: | |
2185 | * Caller must ensure that old_path starts with rename_info->key + '/'. | |
2186 | */ | |
2187 | static char *apply_dir_rename(struct strmap_entry *rename_info, | |
2188 | const char *old_path) | |
2189 | { | |
fbcfc0cc EN |
2190 | struct strbuf new_path = STRBUF_INIT; |
2191 | const char *old_dir = rename_info->key; | |
2192 | const char *new_dir = rename_info->value; | |
2193 | int oldlen, newlen, new_dir_len; | |
2194 | ||
2195 | oldlen = strlen(old_dir); | |
2196 | if (*new_dir == '\0') | |
2197 | /* | |
2198 | * If someone renamed/merged a subdirectory into the root | |
2199 | * directory (e.g. 'some/subdir' -> ''), then we want to | |
2200 | * avoid returning | |
2201 | * '' + '/filename' | |
2202 | * as the rename; we need to make old_path + oldlen advance | |
2203 | * past the '/' character. | |
2204 | */ | |
2205 | oldlen++; | |
2206 | new_dir_len = strlen(new_dir); | |
2207 | newlen = new_dir_len + (strlen(old_path) - oldlen) + 1; | |
2208 | strbuf_grow(&new_path, newlen); | |
2209 | strbuf_add(&new_path, new_dir, new_dir_len); | |
2210 | strbuf_addstr(&new_path, &old_path[oldlen]); | |
2211 | ||
2212 | return strbuf_detach(&new_path, NULL); | |
d9d015df EN |
2213 | } |
2214 | ||
bea43365 EN |
2215 | static int path_in_way(struct strmap *paths, const char *path, unsigned side_mask) |
2216 | { | |
2217 | struct merged_info *mi = strmap_get(paths, path); | |
2218 | struct conflict_info *ci; | |
2219 | if (!mi) | |
2220 | return 0; | |
2221 | INITIALIZE_CI(ci, mi); | |
2222 | return mi->clean || (side_mask & (ci->filemask | ci->dirmask)); | |
2223 | } | |
2224 | ||
47325e85 EN |
2225 | /* |
2226 | * See if there is a directory rename for path, and if there are any file | |
2227 | * level conflicts on the given side for the renamed location. If there is | |
2228 | * a rename and there are no conflicts, return the new name. Otherwise, | |
2229 | * return NULL. | |
2230 | */ | |
2231 | static char *handle_path_level_conflicts(struct merge_options *opt, | |
2232 | const char *path, | |
2233 | unsigned side_index, | |
2234 | struct strmap_entry *rename_info, | |
2235 | struct strmap *collisions) | |
2236 | { | |
bea43365 EN |
2237 | char *new_path = NULL; |
2238 | struct collision_info *c_info; | |
2239 | int clean = 1; | |
2240 | struct strbuf collision_paths = STRBUF_INIT; | |
2241 | ||
2242 | /* | |
2243 | * entry has the mapping of old directory name to new directory name | |
2244 | * that we want to apply to path. | |
2245 | */ | |
2246 | new_path = apply_dir_rename(rename_info, path); | |
2247 | if (!new_path) | |
2248 | BUG("Failed to apply directory rename!"); | |
2249 | ||
2250 | /* | |
2251 | * The caller needs to have ensured that it has pre-populated | |
2252 | * collisions with all paths that map to new_path. Do a quick check | |
2253 | * to ensure that's the case. | |
2254 | */ | |
2255 | c_info = strmap_get(collisions, new_path); | |
afe8a907 | 2256 | if (!c_info) |
bea43365 EN |
2257 | BUG("c_info is NULL"); |
2258 | ||
2259 | /* | |
2260 | * Check for one-sided add/add/.../add conflicts, i.e. | |
2261 | * where implicit renames from the other side doing | |
2262 | * directory rename(s) can affect this side of history | |
2263 | * to put multiple paths into the same location. Warn | |
2264 | * and bail on directory renames for such paths. | |
2265 | */ | |
2266 | if (c_info->reported_already) { | |
2267 | clean = 0; | |
2268 | } else if (path_in_way(&opt->priv->paths, new_path, 1 << side_index)) { | |
2269 | c_info->reported_already = 1; | |
2270 | strbuf_add_separated_string_list(&collision_paths, ", ", | |
2271 | &c_info->source_files); | |
cb260775 EN |
2272 | path_msg(opt, CONFLICT_DIR_RENAME_FILE_IN_WAY, 0, |
2273 | new_path, NULL, NULL, &c_info->source_files, | |
2274 | _("CONFLICT (implicit dir rename): Existing " | |
2275 | "file/dir at %s in the way of implicit " | |
2276 | "directory rename(s) putting the following " | |
2277 | "path(s) there: %s."), | |
2278 | new_path, collision_paths.buf); | |
bea43365 EN |
2279 | clean = 0; |
2280 | } else if (c_info->source_files.nr > 1) { | |
2281 | c_info->reported_already = 1; | |
2282 | strbuf_add_separated_string_list(&collision_paths, ", ", | |
2283 | &c_info->source_files); | |
cb260775 EN |
2284 | path_msg(opt, CONFLICT_DIR_RENAME_COLLISION, 0, |
2285 | new_path, NULL, NULL, &c_info->source_files, | |
2286 | _("CONFLICT (implicit dir rename): Cannot map " | |
2287 | "more than one path to %s; implicit directory " | |
2288 | "renames tried to put these paths there: %s"), | |
2289 | new_path, collision_paths.buf); | |
bea43365 EN |
2290 | clean = 0; |
2291 | } | |
2292 | ||
2293 | /* Free memory we no longer need */ | |
2294 | strbuf_release(&collision_paths); | |
2295 | if (!clean && new_path) { | |
2296 | free(new_path); | |
2297 | return NULL; | |
2298 | } | |
2299 | ||
2300 | return new_path; | |
47325e85 EN |
2301 | } |
2302 | ||
112e1112 EN |
2303 | static void get_provisional_directory_renames(struct merge_options *opt, |
2304 | unsigned side, | |
2305 | int *clean) | |
2306 | { | |
04264d40 EN |
2307 | struct hashmap_iter iter; |
2308 | struct strmap_entry *entry; | |
2309 | struct rename_info *renames = &opt->priv->renames; | |
2310 | ||
04264d40 EN |
2311 | /* |
2312 | * Collapse | |
2313 | * dir_rename_count: old_directory -> {new_directory -> count} | |
2314 | * down to | |
2315 | * dir_renames: old_directory -> best_new_directory | |
2316 | * where best_new_directory is the one with the unique highest count. | |
2317 | */ | |
2318 | strmap_for_each_entry(&renames->dir_rename_count[side], &iter, entry) { | |
2319 | const char *source_dir = entry->key; | |
2320 | struct strintmap *counts = entry->value; | |
2321 | struct hashmap_iter count_iter; | |
2322 | struct strmap_entry *count_entry; | |
2323 | int max = 0; | |
2324 | int bad_max = 0; | |
2325 | const char *best = NULL; | |
2326 | ||
2327 | strintmap_for_each_entry(counts, &count_iter, count_entry) { | |
2328 | const char *target_dir = count_entry->key; | |
2329 | intptr_t count = (intptr_t)count_entry->value; | |
2330 | ||
2331 | if (count == max) | |
2332 | bad_max = max; | |
2333 | else if (count > max) { | |
2334 | max = count; | |
2335 | best = target_dir; | |
2336 | } | |
2337 | } | |
2338 | ||
bf238b71 EN |
2339 | if (max == 0) |
2340 | continue; | |
2341 | ||
04264d40 | 2342 | if (bad_max == max) { |
cb260775 EN |
2343 | path_msg(opt, CONFLICT_DIR_RENAME_SPLIT, 0, |
2344 | source_dir, NULL, NULL, NULL, | |
2345 | _("CONFLICT (directory rename split): " | |
2346 | "Unclear where to rename %s to; it was " | |
2347 | "renamed to multiple other directories, " | |
2348 | "with no destination getting a majority of " | |
2349 | "the files."), | |
2350 | source_dir); | |
41376b58 | 2351 | *clean = 0; |
04264d40 EN |
2352 | } else { |
2353 | strmap_put(&renames->dir_renames[side], | |
2354 | source_dir, (void*)best); | |
2355 | } | |
2356 | } | |
112e1112 EN |
2357 | } |
2358 | ||
2359 | static void handle_directory_level_conflicts(struct merge_options *opt) | |
2360 | { | |
98d0d081 EN |
2361 | struct hashmap_iter iter; |
2362 | struct strmap_entry *entry; | |
2363 | struct string_list duplicated = STRING_LIST_INIT_NODUP; | |
2364 | struct rename_info *renames = &opt->priv->renames; | |
2365 | struct strmap *side1_dir_renames = &renames->dir_renames[MERGE_SIDE1]; | |
2366 | struct strmap *side2_dir_renames = &renames->dir_renames[MERGE_SIDE2]; | |
2367 | int i; | |
2368 | ||
2369 | strmap_for_each_entry(side1_dir_renames, &iter, entry) { | |
2370 | if (strmap_contains(side2_dir_renames, entry->key)) | |
2371 | string_list_append(&duplicated, entry->key); | |
2372 | } | |
2373 | ||
2374 | for (i = 0; i < duplicated.nr; i++) { | |
2375 | strmap_remove(side1_dir_renames, duplicated.items[i].string, 0); | |
2376 | strmap_remove(side2_dir_renames, duplicated.items[i].string, 0); | |
2377 | } | |
2378 | string_list_clear(&duplicated, 0); | |
112e1112 EN |
2379 | } |
2380 | ||
d9d015df EN |
2381 | static struct strmap_entry *check_dir_renamed(const char *path, |
2382 | struct strmap *dir_renames) | |
2383 | { | |
fbcfc0cc EN |
2384 | char *temp = xstrdup(path); |
2385 | char *end; | |
2386 | struct strmap_entry *e = NULL; | |
2387 | ||
2388 | while ((end = strrchr(temp, '/'))) { | |
2389 | *end = '\0'; | |
2390 | e = strmap_get_entry(dir_renames, temp); | |
2391 | if (e) | |
2392 | break; | |
2393 | } | |
2394 | free(temp); | |
2395 | return e; | |
d9d015df EN |
2396 | } |
2397 | ||
fa5e06d6 EN |
2398 | static void compute_collisions(struct strmap *collisions, |
2399 | struct strmap *dir_renames, | |
2400 | struct diff_queue_struct *pairs) | |
2401 | { | |
d9d015df EN |
2402 | int i; |
2403 | ||
2404 | strmap_init_with_options(collisions, NULL, 0); | |
2405 | if (strmap_empty(dir_renames)) | |
2406 | return; | |
2407 | ||
2408 | /* | |
2409 | * Multiple files can be mapped to the same path due to directory | |
2410 | * renames done by the other side of history. Since that other | |
2411 | * side of history could have merged multiple directories into one, | |
2412 | * if our side of history added the same file basename to each of | |
2413 | * those directories, then all N of them would get implicitly | |
2414 | * renamed by the directory rename detection into the same path, | |
2415 | * and we'd get an add/add/.../add conflict, and all those adds | |
2416 | * from *this* side of history. This is not representable in the | |
2417 | * index, and users aren't going to easily be able to make sense of | |
2418 | * it. So we need to provide a good warning about what's | |
2419 | * happening, and fall back to no-directory-rename detection | |
2420 | * behavior for those paths. | |
2421 | * | |
2422 | * See testcases 9e and all of section 5 from t6043 for examples. | |
2423 | */ | |
2424 | for (i = 0; i < pairs->nr; ++i) { | |
2425 | struct strmap_entry *rename_info; | |
2426 | struct collision_info *collision_info; | |
2427 | char *new_path; | |
2428 | struct diff_filepair *pair = pairs->queue[i]; | |
2429 | ||
2430 | if (pair->status != 'A' && pair->status != 'R') | |
2431 | continue; | |
2432 | rename_info = check_dir_renamed(pair->two->path, dir_renames); | |
2433 | if (!rename_info) | |
2434 | continue; | |
2435 | ||
2436 | new_path = apply_dir_rename(rename_info, pair->two->path); | |
2437 | assert(new_path); | |
2438 | collision_info = strmap_get(collisions, new_path); | |
2439 | if (collision_info) { | |
2440 | free(new_path); | |
2441 | } else { | |
ca56dadb | 2442 | CALLOC_ARRAY(collision_info, 1); |
bc40dfb1 | 2443 | string_list_init_nodup(&collision_info->source_files); |
d9d015df EN |
2444 | strmap_put(collisions, new_path, collision_info); |
2445 | } | |
2446 | string_list_insert(&collision_info->source_files, | |
2447 | pair->two->path); | |
2448 | } | |
fa5e06d6 EN |
2449 | } |
2450 | ||
6dd1f0e9 EN |
2451 | static void free_collisions(struct strmap *collisions) |
2452 | { | |
2453 | struct hashmap_iter iter; | |
2454 | struct strmap_entry *entry; | |
2455 | ||
2456 | /* Free each value in the collisions map */ | |
2457 | strmap_for_each_entry(collisions, &iter, entry) { | |
2458 | struct collision_info *info = entry->value; | |
2459 | string_list_clear(&info->source_files, 0); | |
2460 | } | |
2461 | /* | |
2462 | * In compute_collisions(), we set collisions.strdup_strings to 0 | |
2463 | * so that we wouldn't have to make another copy of the new_path | |
2464 | * allocated by apply_dir_rename(). But now that we've used them | |
2465 | * and have no other references to these strings, it is time to | |
2466 | * deallocate them. | |
2467 | */ | |
2468 | free_strmap_strings(collisions); | |
2469 | strmap_clear(collisions, 1); | |
2470 | } | |
2471 | ||
fa5e06d6 EN |
2472 | static char *check_for_directory_rename(struct merge_options *opt, |
2473 | const char *path, | |
2474 | unsigned side_index, | |
2475 | struct strmap *dir_renames, | |
2476 | struct strmap *dir_rename_exclusions, | |
2477 | struct strmap *collisions, | |
2478 | int *clean_merge) | |
2479 | { | |
51e41e4e | 2480 | char *new_path; |
47325e85 | 2481 | struct strmap_entry *rename_info; |
51e41e4e | 2482 | struct strmap_entry *otherinfo; |
47325e85 | 2483 | const char *new_dir; |
751e1654 | 2484 | int other_side = 3 - side_index; |
47325e85 | 2485 | |
751e1654 EN |
2486 | /* |
2487 | * Cases where we don't have or don't want a directory rename for | |
2488 | * this path. | |
2489 | */ | |
47325e85 | 2490 | if (strmap_empty(dir_renames)) |
51e41e4e | 2491 | return NULL; |
751e1654 EN |
2492 | if (strmap_get(&collisions[other_side], path)) |
2493 | return NULL; | |
47325e85 EN |
2494 | rename_info = check_dir_renamed(path, dir_renames); |
2495 | if (!rename_info) | |
51e41e4e | 2496 | return NULL; |
47325e85 EN |
2497 | |
2498 | /* | |
2499 | * This next part is a little weird. We do not want to do an | |
2500 | * implicit rename into a directory we renamed on our side, because | |
2501 | * that will result in a spurious rename/rename(1to2) conflict. An | |
2502 | * example: | |
2503 | * Base commit: dumbdir/afile, otherdir/bfile | |
2504 | * Side 1: smrtdir/afile, otherdir/bfile | |
2505 | * Side 2: dumbdir/afile, dumbdir/bfile | |
2506 | * Here, while working on Side 1, we could notice that otherdir was | |
2507 | * renamed/merged to dumbdir, and change the diff_filepair for | |
2508 | * otherdir/bfile into a rename into dumbdir/bfile. However, Side | |
2509 | * 2 will notice the rename from dumbdir to smrtdir, and do the | |
2510 | * transitive rename to move it from dumbdir/bfile to | |
2511 | * smrtdir/bfile. That gives us bfile in dumbdir vs being in | |
2512 | * smrtdir, a rename/rename(1to2) conflict. We really just want | |
2513 | * the file to end up in smrtdir. And the way to achieve that is | |
2514 | * to not let Side1 do the rename to dumbdir, since we know that is | |
2515 | * the source of one of our directory renames. | |
2516 | * | |
2517 | * That's why otherinfo and dir_rename_exclusions is here. | |
2518 | * | |
2519 | * As it turns out, this also prevents N-way transient rename | |
2520 | * confusion; See testcases 9c and 9d of t6043. | |
2521 | */ | |
51e41e4e | 2522 | new_dir = rename_info->value; /* old_dir = rename_info->key; */ |
47325e85 EN |
2523 | otherinfo = strmap_get_entry(dir_rename_exclusions, new_dir); |
2524 | if (otherinfo) { | |
cb260775 EN |
2525 | path_msg(opt, INFO_DIR_RENAME_SKIPPED_DUE_TO_RERENAME, 1, |
2526 | rename_info->key, path, new_dir, NULL, | |
47325e85 EN |
2527 | _("WARNING: Avoiding applying %s -> %s rename " |
2528 | "to %s, because %s itself was renamed."), | |
2529 | rename_info->key, new_dir, path, new_dir); | |
2530 | return NULL; | |
2531 | } | |
2532 | ||
2533 | new_path = handle_path_level_conflicts(opt, path, side_index, | |
3ffbe5a2 EN |
2534 | rename_info, |
2535 | &collisions[side_index]); | |
47325e85 EN |
2536 | *clean_merge &= (new_path != NULL); |
2537 | ||
2538 | return new_path; | |
fa5e06d6 EN |
2539 | } |
2540 | ||
2541 | static void apply_directory_rename_modifications(struct merge_options *opt, | |
2542 | struct diff_filepair *pair, | |
2543 | char *new_path) | |
2544 | { | |
089d82bc EN |
2545 | /* |
2546 | * The basic idea is to get the conflict_info from opt->priv->paths | |
2547 | * at old path, and insert it into new_path; basically just this: | |
2548 | * ci = strmap_get(&opt->priv->paths, old_path); | |
2549 | * strmap_remove(&opt->priv->paths, old_path, 0); | |
2550 | * strmap_put(&opt->priv->paths, new_path, ci); | |
2551 | * However, there are some factors complicating this: | |
2552 | * - opt->priv->paths may already have an entry at new_path | |
2553 | * - Each ci tracks its containing directory, so we need to | |
2554 | * update that | |
2555 | * - If another ci has the same containing directory, then | |
2556 | * the two char*'s MUST point to the same location. See the | |
2557 | * comment in struct merged_info. strcmp equality is not | |
2558 | * enough; we need pointer equality. | |
2559 | * - opt->priv->paths must hold the parent directories of any | |
2560 | * entries that are added. So, if this directory rename | |
2561 | * causes entirely new directories, we must recursively add | |
2562 | * parent directories. | |
2563 | * - For each parent directory added to opt->priv->paths, we | |
2564 | * also need to get its parent directory stored in its | |
2565 | * conflict_info->merged.directory_name with all the same | |
2566 | * requirements about pointer equality. | |
2567 | */ | |
2568 | struct string_list dirs_to_insert = STRING_LIST_INIT_NODUP; | |
2569 | struct conflict_info *ci, *new_ci; | |
2570 | struct strmap_entry *entry; | |
2571 | const char *branch_with_new_path, *branch_with_dir_rename; | |
2572 | const char *old_path = pair->two->path; | |
2573 | const char *parent_name; | |
2574 | const char *cur_path; | |
2575 | int i, len; | |
2576 | ||
2577 | entry = strmap_get_entry(&opt->priv->paths, old_path); | |
2578 | old_path = entry->key; | |
2579 | ci = entry->value; | |
2580 | VERIFY_CI(ci); | |
2581 | ||
2582 | /* Find parent directories missing from opt->priv->paths */ | |
62a15162 EN |
2583 | cur_path = mem_pool_strdup(&opt->priv->pool, new_path); |
2584 | free((char*)new_path); | |
2585 | new_path = (char *)cur_path; | |
6697ee01 | 2586 | |
089d82bc EN |
2587 | while (1) { |
2588 | /* Find the parent directory of cur_path */ | |
2589 | char *last_slash = strrchr(cur_path, '/'); | |
2590 | if (last_slash) { | |
62a15162 EN |
2591 | parent_name = mem_pool_strndup(&opt->priv->pool, |
2592 | cur_path, | |
2593 | last_slash - cur_path); | |
089d82bc EN |
2594 | } else { |
2595 | parent_name = opt->priv->toplevel_dir; | |
2596 | break; | |
2597 | } | |
2598 | ||
2599 | /* Look it up in opt->priv->paths */ | |
2600 | entry = strmap_get_entry(&opt->priv->paths, parent_name); | |
2601 | if (entry) { | |
089d82bc EN |
2602 | parent_name = entry->key; /* reuse known pointer */ |
2603 | break; | |
2604 | } | |
2605 | ||
2606 | /* Record this is one of the directories we need to insert */ | |
2607 | string_list_append(&dirs_to_insert, parent_name); | |
2608 | cur_path = parent_name; | |
2609 | } | |
2610 | ||
2611 | /* Traverse dirs_to_insert and insert them into opt->priv->paths */ | |
2612 | for (i = dirs_to_insert.nr-1; i >= 0; --i) { | |
2613 | struct conflict_info *dir_ci; | |
2614 | char *cur_dir = dirs_to_insert.items[i].string; | |
2615 | ||
ca56dadb | 2616 | CALLOC_ARRAY(dir_ci, 1); |
089d82bc EN |
2617 | |
2618 | dir_ci->merged.directory_name = parent_name; | |
2619 | len = strlen(parent_name); | |
2620 | /* len+1 because of trailing '/' character */ | |
2621 | dir_ci->merged.basename_offset = (len > 0 ? len+1 : len); | |
2622 | dir_ci->dirmask = ci->filemask; | |
2623 | strmap_put(&opt->priv->paths, cur_dir, dir_ci); | |
2624 | ||
2625 | parent_name = cur_dir; | |
2626 | } | |
2627 | ||
089d82bc | 2628 | assert(ci->filemask == 2 || ci->filemask == 4); |
2b86c100 EN |
2629 | assert(ci->dirmask == 0 || ci->dirmask == 1); |
2630 | if (ci->dirmask == 0) | |
2631 | strmap_remove(&opt->priv->paths, old_path, 0); | |
2632 | else { | |
2633 | /* | |
2634 | * This file exists on one side, but we still had a directory | |
2635 | * at the old location that we can't remove until after | |
2636 | * processing all paths below it. So, make a copy of ci in | |
2637 | * new_ci and only put the file information into it. | |
2638 | */ | |
2639 | new_ci = mem_pool_calloc(&opt->priv->pool, 1, sizeof(*new_ci)); | |
2640 | memcpy(new_ci, ci, sizeof(*ci)); | |
2641 | assert(!new_ci->match_mask); | |
2642 | new_ci->dirmask = 0; | |
2643 | new_ci->stages[1].mode = 0; | |
2644 | oidcpy(&new_ci->stages[1].oid, null_oid()); | |
2645 | ||
2646 | /* | |
2647 | * Now that we have the file information in new_ci, make sure | |
2648 | * ci only has the directory information. | |
2649 | */ | |
2650 | ci->filemask = 0; | |
2651 | ci->merged.clean = 1; | |
2652 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
2653 | if (ci->dirmask & (1 << i)) | |
2654 | continue; | |
2655 | /* zero out any entries related to files */ | |
2656 | ci->stages[i].mode = 0; | |
2657 | oidcpy(&ci->stages[i].oid, null_oid()); | |
2658 | } | |
2659 | ||
2660 | // Now we want to focus on new_ci, so reassign ci to it | |
2661 | ci = new_ci; | |
2662 | } | |
089d82bc EN |
2663 | |
2664 | branch_with_new_path = (ci->filemask == 2) ? opt->branch1 : opt->branch2; | |
2665 | branch_with_dir_rename = (ci->filemask == 2) ? opt->branch2 : opt->branch1; | |
2666 | ||
2667 | /* Now, finally update ci and stick it into opt->priv->paths */ | |
2668 | ci->merged.directory_name = parent_name; | |
2669 | len = strlen(parent_name); | |
2670 | ci->merged.basename_offset = (len > 0 ? len+1 : len); | |
2671 | new_ci = strmap_get(&opt->priv->paths, new_path); | |
2672 | if (!new_ci) { | |
2673 | /* Place ci back into opt->priv->paths, but at new_path */ | |
2674 | strmap_put(&opt->priv->paths, new_path, ci); | |
2675 | } else { | |
2676 | int index; | |
2677 | ||
2678 | /* A few sanity checks */ | |
2679 | VERIFY_CI(new_ci); | |
2680 | assert(ci->filemask == 2 || ci->filemask == 4); | |
2681 | assert((new_ci->filemask & ci->filemask) == 0); | |
2682 | assert(!new_ci->merged.clean); | |
2683 | ||
2684 | /* Copy stuff from ci into new_ci */ | |
2685 | new_ci->filemask |= ci->filemask; | |
2686 | if (new_ci->dirmask) | |
2687 | new_ci->df_conflict = 1; | |
2688 | index = (ci->filemask >> 1); | |
2689 | new_ci->pathnames[index] = ci->pathnames[index]; | |
2690 | new_ci->stages[index].mode = ci->stages[index].mode; | |
2691 | oidcpy(&new_ci->stages[index].oid, &ci->stages[index].oid); | |
2692 | ||
089d82bc EN |
2693 | ci = new_ci; |
2694 | } | |
2695 | ||
2696 | if (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) { | |
2697 | /* Notify user of updated path */ | |
2698 | if (pair->status == 'A') | |
cb260775 EN |
2699 | path_msg(opt, INFO_DIR_RENAME_APPLIED, 1, |
2700 | new_path, old_path, NULL, NULL, | |
089d82bc EN |
2701 | _("Path updated: %s added in %s inside a " |
2702 | "directory that was renamed in %s; moving " | |
2703 | "it to %s."), | |
2704 | old_path, branch_with_new_path, | |
2705 | branch_with_dir_rename, new_path); | |
2706 | else | |
cb260775 EN |
2707 | path_msg(opt, INFO_DIR_RENAME_APPLIED, 1, |
2708 | new_path, old_path, NULL, NULL, | |
089d82bc EN |
2709 | _("Path updated: %s renamed to %s in %s, " |
2710 | "inside a directory that was renamed in %s; " | |
2711 | "moving it to %s."), | |
2712 | pair->one->path, old_path, branch_with_new_path, | |
2713 | branch_with_dir_rename, new_path); | |
2714 | } else { | |
2715 | /* | |
2716 | * opt->detect_directory_renames has the value | |
2717 | * MERGE_DIRECTORY_RENAMES_CONFLICT, so mark these as conflicts. | |
2718 | */ | |
2719 | ci->path_conflict = 1; | |
2720 | if (pair->status == 'A') | |
cb260775 EN |
2721 | path_msg(opt, CONFLICT_DIR_RENAME_SUGGESTED, 1, |
2722 | new_path, old_path, NULL, NULL, | |
089d82bc EN |
2723 | _("CONFLICT (file location): %s added in %s " |
2724 | "inside a directory that was renamed in %s, " | |
2725 | "suggesting it should perhaps be moved to " | |
2726 | "%s."), | |
2727 | old_path, branch_with_new_path, | |
2728 | branch_with_dir_rename, new_path); | |
2729 | else | |
cb260775 EN |
2730 | path_msg(opt, CONFLICT_DIR_RENAME_SUGGESTED, 1, |
2731 | new_path, old_path, NULL, NULL, | |
089d82bc EN |
2732 | _("CONFLICT (file location): %s renamed to %s " |
2733 | "in %s, inside a directory that was renamed " | |
2734 | "in %s, suggesting it should perhaps be " | |
2735 | "moved to %s."), | |
2736 | pair->one->path, old_path, branch_with_new_path, | |
2737 | branch_with_dir_rename, new_path); | |
2738 | } | |
2739 | ||
2740 | /* | |
2741 | * Finally, record the new location. | |
2742 | */ | |
2743 | pair->two->path = new_path; | |
fa5e06d6 EN |
2744 | } |
2745 | ||
04af1879 EN |
2746 | /*** Function Grouping: functions related to regular rename detection ***/ |
2747 | ||
e1a124e8 EN |
2748 | static int process_renames(struct merge_options *opt, |
2749 | struct diff_queue_struct *renames) | |
2750 | { | |
c2d267df EN |
2751 | int clean_merge = 1, i; |
2752 | ||
2753 | for (i = 0; i < renames->nr; ++i) { | |
2754 | const char *oldpath = NULL, *newpath; | |
2755 | struct diff_filepair *pair = renames->queue[i]; | |
2756 | struct conflict_info *oldinfo = NULL, *newinfo = NULL; | |
2757 | struct strmap_entry *old_ent, *new_ent; | |
2758 | unsigned int old_sidemask; | |
2759 | int target_index, other_source_index; | |
2760 | int source_deleted, collision, type_changed; | |
2e91ddd2 | 2761 | const char *rename_branch = NULL, *delete_branch = NULL; |
c2d267df EN |
2762 | |
2763 | old_ent = strmap_get_entry(&opt->priv->paths, pair->one->path); | |
c2d267df | 2764 | new_ent = strmap_get_entry(&opt->priv->paths, pair->two->path); |
1b6b902d EN |
2765 | if (old_ent) { |
2766 | oldpath = old_ent->key; | |
2767 | oldinfo = old_ent->value; | |
2768 | } | |
2769 | newpath = pair->two->path; | |
2770 | if (new_ent) { | |
2771 | newpath = new_ent->key; | |
2772 | newinfo = new_ent->value; | |
2773 | } | |
2774 | ||
2775 | /* | |
2776 | * If pair->one->path isn't in opt->priv->paths, that means | |
2777 | * that either directory rename detection removed that | |
2778 | * path, or a parent directory of oldpath was resolved and | |
2779 | * we don't even need the rename; in either case, we can | |
2780 | * skip it. If oldinfo->merged.clean, then the other side | |
2781 | * of history had no changes to oldpath and we don't need | |
2782 | * the rename and can skip it. | |
2783 | */ | |
2784 | if (!oldinfo || oldinfo->merged.clean) | |
2785 | continue; | |
c2d267df EN |
2786 | |
2787 | /* | |
2788 | * diff_filepairs have copies of pathnames, thus we have to | |
2789 | * use standard 'strcmp()' (negated) instead of '=='. | |
2790 | */ | |
2791 | if (i + 1 < renames->nr && | |
2792 | !strcmp(oldpath, renames->queue[i+1]->one->path)) { | |
2793 | /* Handle rename/rename(1to2) or rename/rename(1to1) */ | |
2794 | const char *pathnames[3]; | |
af1e56c4 EN |
2795 | struct version_info merged; |
2796 | struct conflict_info *base, *side1, *side2; | |
53e88a03 | 2797 | unsigned was_binary_blob = 0; |
c2d267df EN |
2798 | |
2799 | pathnames[0] = oldpath; | |
2800 | pathnames[1] = newpath; | |
2801 | pathnames[2] = renames->queue[i+1]->two->path; | |
2802 | ||
af1e56c4 EN |
2803 | base = strmap_get(&opt->priv->paths, pathnames[0]); |
2804 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
2805 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
2806 | ||
2807 | VERIFY_CI(base); | |
2808 | VERIFY_CI(side1); | |
2809 | VERIFY_CI(side2); | |
2810 | ||
c2d267df | 2811 | if (!strcmp(pathnames[1], pathnames[2])) { |
cbdca289 EN |
2812 | struct rename_info *ri = &opt->priv->renames; |
2813 | int j; | |
2814 | ||
af1e56c4 EN |
2815 | /* Both sides renamed the same way */ |
2816 | assert(side1 == side2); | |
2817 | memcpy(&side1->stages[0], &base->stages[0], | |
2818 | sizeof(merged)); | |
2819 | side1->filemask |= (1 << MERGE_BASE); | |
2820 | /* Mark base as resolved by removal */ | |
2821 | base->merged.is_null = 1; | |
2822 | base->merged.clean = 1; | |
c2d267df | 2823 | |
cbdca289 EN |
2824 | /* |
2825 | * Disable remembering renames optimization; | |
2826 | * rename/rename(1to1) is incredibly rare, and | |
2827 | * just disabling the optimization is easier | |
2828 | * than purging cached_pairs, | |
2829 | * cached_target_names, and dir_rename_counts. | |
2830 | */ | |
2831 | for (j = 0; j < 3; j++) | |
2832 | ri->merge_trees[j] = NULL; | |
2833 | ||
c2d267df EN |
2834 | /* We handled both renames, i.e. i+1 handled */ |
2835 | i++; | |
2836 | /* Move to next rename */ | |
2837 | continue; | |
2838 | } | |
2839 | ||
2840 | /* This is a rename/rename(1to2) */ | |
53e88a03 EN |
2841 | clean_merge = handle_content_merge(opt, |
2842 | pair->one->path, | |
2843 | &base->stages[0], | |
2844 | &side1->stages[1], | |
2845 | &side2->stages[2], | |
2846 | pathnames, | |
2847 | 1 + 2 * opt->priv->call_depth, | |
2848 | &merged); | |
92481d1b JS |
2849 | if (clean_merge < 0) |
2850 | return -1; | |
53e88a03 EN |
2851 | if (!clean_merge && |
2852 | merged.mode == side1->stages[1].mode && | |
2853 | oideq(&merged.oid, &side1->stages[1].oid)) | |
2854 | was_binary_blob = 1; | |
2855 | memcpy(&side1->stages[1], &merged, sizeof(merged)); | |
2856 | if (was_binary_blob) { | |
2857 | /* | |
2858 | * Getting here means we were attempting to | |
2859 | * merge a binary blob. | |
2860 | * | |
2861 | * Since we can't merge binaries, | |
2862 | * handle_content_merge() just takes one | |
2863 | * side. But we don't want to copy the | |
2864 | * contents of one side to both paths. We | |
2865 | * used the contents of side1 above for | |
2866 | * side1->stages, let's use the contents of | |
2867 | * side2 for side2->stages below. | |
2868 | */ | |
2869 | oidcpy(&merged.oid, &side2->stages[2].oid); | |
2870 | merged.mode = side2->stages[2].mode; | |
2871 | } | |
2872 | memcpy(&side2->stages[2], &merged, sizeof(merged)); | |
2873 | ||
2874 | side1->path_conflict = 1; | |
2875 | side2->path_conflict = 1; | |
2876 | /* | |
2877 | * TODO: For renames we normally remove the path at the | |
2878 | * old name. It would thus seem consistent to do the | |
2879 | * same for rename/rename(1to2) cases, but we haven't | |
2880 | * done so traditionally and a number of the regression | |
2881 | * tests now encode an expectation that the file is | |
2882 | * left there at stage 1. If we ever decide to change | |
2883 | * this, add the following two lines here: | |
2884 | * base->merged.is_null = 1; | |
2885 | * base->merged.clean = 1; | |
2886 | * and remove the setting of base->path_conflict to 1. | |
2887 | */ | |
2888 | base->path_conflict = 1; | |
cb260775 EN |
2889 | path_msg(opt, CONFLICT_RENAME_RENAME, 0, |
2890 | pathnames[0], pathnames[1], pathnames[2], NULL, | |
53e88a03 EN |
2891 | _("CONFLICT (rename/rename): %s renamed to " |
2892 | "%s in %s and to %s in %s."), | |
2893 | pathnames[0], | |
2894 | pathnames[1], opt->branch1, | |
2895 | pathnames[2], opt->branch2); | |
c2d267df EN |
2896 | |
2897 | i++; /* We handled both renames, i.e. i+1 handled */ | |
2898 | continue; | |
2899 | } | |
2900 | ||
2901 | VERIFY_CI(oldinfo); | |
2902 | VERIFY_CI(newinfo); | |
2903 | target_index = pair->score; /* from collect_renames() */ | |
2904 | assert(target_index == 1 || target_index == 2); | |
2905 | other_source_index = 3 - target_index; | |
2906 | old_sidemask = (1 << other_source_index); /* 2 or 4 */ | |
2907 | source_deleted = (oldinfo->filemask == 1); | |
2908 | collision = ((newinfo->filemask & old_sidemask) != 0); | |
2909 | type_changed = !source_deleted && | |
2910 | (S_ISREG(oldinfo->stages[other_source_index].mode) != | |
2911 | S_ISREG(newinfo->stages[target_index].mode)); | |
2912 | if (type_changed && collision) { | |
6fcccbd7 EN |
2913 | /* |
2914 | * special handling so later blocks can handle this... | |
2915 | * | |
2916 | * if type_changed && collision are both true, then this | |
2917 | * was really a double rename, but one side wasn't | |
2918 | * detected due to lack of break detection. I.e. | |
2919 | * something like | |
2920 | * orig: has normal file 'foo' | |
2921 | * side1: renames 'foo' to 'bar', adds 'foo' symlink | |
2922 | * side2: renames 'foo' to 'bar' | |
2923 | * In this case, the foo->bar rename on side1 won't be | |
2924 | * detected because the new symlink named 'foo' is | |
2925 | * there and we don't do break detection. But we detect | |
2926 | * this here because we don't want to merge the content | |
2927 | * of the foo symlink with the foo->bar file, so we | |
2928 | * have some logic to handle this special case. The | |
2929 | * easiest way to do that is make 'bar' on side1 not | |
2930 | * be considered a colliding file but the other part | |
2931 | * of a normal rename. If the file is very different, | |
2932 | * well we're going to get content merge conflicts | |
2933 | * anyway so it doesn't hurt. And if the colliding | |
2934 | * file also has a different type, that'll be handled | |
2935 | * by the content merge logic in process_entry() too. | |
2936 | * | |
2937 | * See also t6430, 'rename vs. rename/symlink' | |
2938 | */ | |
2939 | collision = 0; | |
c2d267df | 2940 | } |
2e91ddd2 EN |
2941 | if (source_deleted) { |
2942 | if (target_index == 1) { | |
2943 | rename_branch = opt->branch1; | |
2944 | delete_branch = opt->branch2; | |
2945 | } else { | |
2946 | rename_branch = opt->branch2; | |
2947 | delete_branch = opt->branch1; | |
2948 | } | |
2949 | } | |
c2d267df EN |
2950 | |
2951 | assert(source_deleted || oldinfo->filemask & old_sidemask); | |
2952 | ||
2953 | /* Need to check for special types of rename conflicts... */ | |
2954 | if (collision && !source_deleted) { | |
2955 | /* collision: rename/add or rename/rename(2to1) */ | |
35e47e35 EN |
2956 | const char *pathnames[3]; |
2957 | struct version_info merged; | |
2958 | ||
2959 | struct conflict_info *base, *side1, *side2; | |
92481d1b | 2960 | int clean; |
35e47e35 EN |
2961 | |
2962 | pathnames[0] = oldpath; | |
2963 | pathnames[other_source_index] = oldpath; | |
2964 | pathnames[target_index] = newpath; | |
2965 | ||
2966 | base = strmap_get(&opt->priv->paths, pathnames[0]); | |
2967 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
2968 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
2969 | ||
2970 | VERIFY_CI(base); | |
2971 | VERIFY_CI(side1); | |
2972 | VERIFY_CI(side2); | |
2973 | ||
2974 | clean = handle_content_merge(opt, pair->one->path, | |
2975 | &base->stages[0], | |
2976 | &side1->stages[1], | |
2977 | &side2->stages[2], | |
2978 | pathnames, | |
2979 | 1 + 2 * opt->priv->call_depth, | |
2980 | &merged); | |
92481d1b JS |
2981 | if (clean < 0) |
2982 | return -1; | |
35e47e35 EN |
2983 | |
2984 | memcpy(&newinfo->stages[target_index], &merged, | |
2985 | sizeof(merged)); | |
2986 | if (!clean) { | |
cb260775 EN |
2987 | path_msg(opt, CONFLICT_RENAME_COLLIDES, 0, |
2988 | newpath, oldpath, NULL, NULL, | |
35e47e35 EN |
2989 | _("CONFLICT (rename involved in " |
2990 | "collision): rename of %s -> %s has " | |
2991 | "content conflicts AND collides " | |
2992 | "with another path; this may result " | |
2993 | "in nested conflict markers."), | |
2994 | oldpath, newpath); | |
2995 | } | |
c2d267df | 2996 | } else if (collision && source_deleted) { |
35e47e35 EN |
2997 | /* |
2998 | * rename/add/delete or rename/rename(2to1)/delete: | |
2999 | * since oldpath was deleted on the side that didn't | |
3000 | * do the rename, there's not much of a content merge | |
3001 | * we can do for the rename. oldinfo->merged.is_null | |
3002 | * was already set, so we just leave things as-is so | |
3003 | * they look like an add/add conflict. | |
3004 | */ | |
3005 | ||
3006 | newinfo->path_conflict = 1; | |
cb260775 EN |
3007 | path_msg(opt, CONFLICT_RENAME_DELETE, 0, |
3008 | newpath, oldpath, NULL, NULL, | |
35e47e35 EN |
3009 | _("CONFLICT (rename/delete): %s renamed " |
3010 | "to %s in %s, but deleted in %s."), | |
3011 | oldpath, newpath, rename_branch, delete_branch); | |
c2d267df | 3012 | } else { |
2e91ddd2 EN |
3013 | /* |
3014 | * a few different cases...start by copying the | |
3015 | * existing stage(s) from oldinfo over the newinfo | |
3016 | * and update the pathname(s). | |
3017 | */ | |
3018 | memcpy(&newinfo->stages[0], &oldinfo->stages[0], | |
3019 | sizeof(newinfo->stages[0])); | |
3020 | newinfo->filemask |= (1 << MERGE_BASE); | |
3021 | newinfo->pathnames[0] = oldpath; | |
c2d267df EN |
3022 | if (type_changed) { |
3023 | /* rename vs. typechange */ | |
6fcccbd7 | 3024 | /* Mark the original as resolved by removal */ |
14228447 | 3025 | memcpy(&oldinfo->stages[0].oid, null_oid(), |
6fcccbd7 EN |
3026 | sizeof(oldinfo->stages[0].oid)); |
3027 | oldinfo->stages[0].mode = 0; | |
3028 | oldinfo->filemask &= 0x06; | |
c2d267df EN |
3029 | } else if (source_deleted) { |
3030 | /* rename/delete */ | |
2e91ddd2 | 3031 | newinfo->path_conflict = 1; |
cb260775 EN |
3032 | path_msg(opt, CONFLICT_RENAME_DELETE, 0, |
3033 | newpath, oldpath, NULL, NULL, | |
2e91ddd2 EN |
3034 | _("CONFLICT (rename/delete): %s renamed" |
3035 | " to %s in %s, but deleted in %s."), | |
3036 | oldpath, newpath, | |
3037 | rename_branch, delete_branch); | |
c2d267df EN |
3038 | } else { |
3039 | /* normal rename */ | |
f1665e69 EN |
3040 | memcpy(&newinfo->stages[other_source_index], |
3041 | &oldinfo->stages[other_source_index], | |
3042 | sizeof(newinfo->stages[0])); | |
3043 | newinfo->filemask |= (1 << other_source_index); | |
3044 | newinfo->pathnames[other_source_index] = oldpath; | |
c2d267df EN |
3045 | } |
3046 | } | |
3047 | ||
3048 | if (!type_changed) { | |
3049 | /* Mark the original as resolved by removal */ | |
3050 | oldinfo->merged.is_null = 1; | |
3051 | oldinfo->merged.clean = 1; | |
3052 | } | |
3053 | ||
3054 | } | |
3055 | ||
3056 | return clean_merge; | |
e1a124e8 EN |
3057 | } |
3058 | ||
f89b4f2b EN |
3059 | static inline int possible_side_renames(struct rename_info *renames, |
3060 | unsigned side_index) | |
3061 | { | |
3062 | return renames->pairs[side_index].nr > 0 && | |
a49b55d5 | 3063 | !strintmap_empty(&renames->relevant_sources[side_index]); |
f89b4f2b EN |
3064 | } |
3065 | ||
3066 | static inline int possible_renames(struct rename_info *renames) | |
3067 | { | |
3068 | return possible_side_renames(renames, 1) || | |
25e65b6d EN |
3069 | possible_side_renames(renames, 2) || |
3070 | !strmap_empty(&renames->cached_pairs[1]) || | |
3071 | !strmap_empty(&renames->cached_pairs[2]); | |
f89b4f2b EN |
3072 | } |
3073 | ||
f78cf976 EN |
3074 | static void resolve_diffpair_statuses(struct diff_queue_struct *q) |
3075 | { | |
3076 | /* | |
3077 | * A simplified version of diff_resolve_rename_copy(); would probably | |
3078 | * just use that function but it's static... | |
3079 | */ | |
3080 | int i; | |
3081 | struct diff_filepair *p; | |
3082 | ||
3083 | for (i = 0; i < q->nr; ++i) { | |
3084 | p = q->queue[i]; | |
3085 | p->status = 0; /* undecided */ | |
3086 | if (!DIFF_FILE_VALID(p->one)) | |
3087 | p->status = DIFF_STATUS_ADDED; | |
3088 | else if (!DIFF_FILE_VALID(p->two)) | |
3089 | p->status = DIFF_STATUS_DELETED; | |
3090 | else if (DIFF_PAIR_RENAME(p)) | |
3091 | p->status = DIFF_STATUS_RENAMED; | |
3092 | } | |
3093 | } | |
3094 | ||
86b41b38 EN |
3095 | static void prune_cached_from_relevant(struct rename_info *renames, |
3096 | unsigned side) | |
3097 | { | |
3098 | /* Reason for this function described in add_pair() */ | |
3099 | struct hashmap_iter iter; | |
3100 | struct strmap_entry *entry; | |
3101 | ||
3102 | /* Remove from relevant_sources all entries in cached_pairs[side] */ | |
3103 | strmap_for_each_entry(&renames->cached_pairs[side], &iter, entry) { | |
3104 | strintmap_remove(&renames->relevant_sources[side], | |
3105 | entry->key); | |
3106 | } | |
3107 | /* Remove from relevant_sources all entries in cached_irrelevant[side] */ | |
3108 | strset_for_each_entry(&renames->cached_irrelevant[side], &iter, entry) { | |
3109 | strintmap_remove(&renames->relevant_sources[side], | |
3110 | entry->key); | |
3111 | } | |
3112 | } | |
3113 | ||
86b41b38 EN |
3114 | static void use_cached_pairs(struct merge_options *opt, |
3115 | struct strmap *cached_pairs, | |
3116 | struct diff_queue_struct *pairs) | |
3117 | { | |
3118 | struct hashmap_iter iter; | |
3119 | struct strmap_entry *entry; | |
3120 | ||
3121 | /* | |
3122 | * Add to side_pairs all entries from renames->cached_pairs[side_index]. | |
3123 | * (Info in cached_irrelevant[side_index] is not relevant here.) | |
3124 | */ | |
3125 | strmap_for_each_entry(cached_pairs, &iter, entry) { | |
3126 | struct diff_filespec *one, *two; | |
3127 | const char *old_name = entry->key; | |
3128 | const char *new_name = entry->value; | |
3129 | if (!new_name) | |
3130 | new_name = old_name; | |
3131 | ||
62a15162 EN |
3132 | /* |
3133 | * cached_pairs has *copies* of old_name and new_name, | |
3134 | * because it has to persist across merges. Since | |
3135 | * pool_alloc_filespec() will just re-use the existing | |
3136 | * filenames, which will also get re-used by | |
3137 | * opt->priv->paths if they become renames, and then | |
3138 | * get freed at the end of the merge, that would leave | |
3139 | * the copy in cached_pairs dangling. Avoid this by | |
3140 | * making a copy here. | |
3141 | */ | |
3142 | old_name = mem_pool_strdup(&opt->priv->pool, old_name); | |
3143 | new_name = mem_pool_strdup(&opt->priv->pool, new_name); | |
86b41b38 EN |
3144 | |
3145 | /* We don't care about oid/mode, only filenames and status */ | |
62a15162 EN |
3146 | one = pool_alloc_filespec(&opt->priv->pool, old_name); |
3147 | two = pool_alloc_filespec(&opt->priv->pool, new_name); | |
3148 | pool_diff_queue(&opt->priv->pool, pairs, one, two); | |
86b41b38 EN |
3149 | pairs->queue[pairs->nr-1]->status = entry->value ? 'R' : 'D'; |
3150 | } | |
3151 | } | |
3152 | ||
2734f2e3 EN |
3153 | static void cache_new_pair(struct rename_info *renames, |
3154 | int side, | |
3155 | char *old_path, | |
3156 | char *new_path, | |
3157 | int free_old_value) | |
3158 | { | |
3159 | char *old_value; | |
3160 | new_path = xstrdup(new_path); | |
3161 | old_value = strmap_put(&renames->cached_pairs[side], | |
3162 | old_path, new_path); | |
3163 | strset_add(&renames->cached_target_names[side], new_path); | |
3164 | if (free_old_value) | |
3165 | free(old_value); | |
3166 | else | |
3167 | assert(!old_value); | |
3168 | } | |
3169 | ||
3170 | static void possibly_cache_new_pair(struct rename_info *renames, | |
3171 | struct diff_filepair *p, | |
3172 | unsigned side, | |
3173 | char *new_path) | |
3174 | { | |
3175 | int dir_renamed_side = 0; | |
3176 | ||
3177 | if (new_path) { | |
3178 | /* | |
3179 | * Directory renames happen on the other side of history from | |
3180 | * the side that adds new files to the old directory. | |
3181 | */ | |
3182 | dir_renamed_side = 3 - side; | |
3183 | } else { | |
3184 | int val = strintmap_get(&renames->relevant_sources[side], | |
3185 | p->one->path); | |
3186 | if (val == RELEVANT_NO_MORE) { | |
3187 | assert(p->status == 'D'); | |
3188 | strset_add(&renames->cached_irrelevant[side], | |
3189 | p->one->path); | |
3190 | } | |
3191 | if (val <= 0) | |
3192 | return; | |
3193 | } | |
3194 | ||
3195 | if (p->status == 'D') { | |
3196 | /* | |
3197 | * If we already had this delete, we'll just set it's value | |
3198 | * to NULL again, so no harm. | |
3199 | */ | |
3200 | strmap_put(&renames->cached_pairs[side], p->one->path, NULL); | |
3201 | } else if (p->status == 'R') { | |
3202 | if (!new_path) | |
3203 | new_path = p->two->path; | |
3204 | else | |
3205 | cache_new_pair(renames, dir_renamed_side, | |
3206 | p->two->path, new_path, 0); | |
3207 | cache_new_pair(renames, side, p->one->path, new_path, 1); | |
3208 | } else if (p->status == 'A' && new_path) { | |
3209 | cache_new_pair(renames, dir_renamed_side, | |
3210 | p->two->path, new_path, 0); | |
3211 | } | |
3212 | } | |
3213 | ||
e1a124e8 EN |
3214 | static int compare_pairs(const void *a_, const void *b_) |
3215 | { | |
965a7bc2 EN |
3216 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
3217 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
3218 | ||
3219 | return strcmp(a->one->path, b->one->path); | |
e1a124e8 EN |
3220 | } |
3221 | ||
356da0f9 | 3222 | /* Call diffcore_rename() to update deleted/added pairs into rename pairs */ |
8b09a900 EN |
3223 | static int detect_regular_renames(struct merge_options *opt, |
3224 | unsigned side_index) | |
e1a124e8 | 3225 | { |
f39d05ca EN |
3226 | struct diff_options diff_opts; |
3227 | struct rename_info *renames = &opt->priv->renames; | |
3228 | ||
25e65b6d | 3229 | prune_cached_from_relevant(renames, side_index); |
f89b4f2b EN |
3230 | if (!possible_side_renames(renames, side_index)) { |
3231 | /* | |
3232 | * No rename detection needed for this side, but we still need | |
3233 | * to make sure 'adds' are marked correctly in case the other | |
3234 | * side had directory renames. | |
3235 | */ | |
3236 | resolve_diffpair_statuses(&renames->pairs[side_index]); | |
8b09a900 | 3237 | return 0; |
f89b4f2b EN |
3238 | } |
3239 | ||
d5098029 | 3240 | partial_clear_dir_rename_count(&renames->dir_rename_count[side_index]); |
f39d05ca EN |
3241 | repo_diff_setup(opt->repo, &diff_opts); |
3242 | diff_opts.flags.recursive = 1; | |
3243 | diff_opts.flags.rename_empty = 0; | |
3244 | diff_opts.detect_rename = DIFF_DETECT_RENAME; | |
3245 | diff_opts.rename_limit = opt->rename_limit; | |
3246 | if (opt->rename_limit <= 0) | |
94b82d56 | 3247 | diff_opts.rename_limit = 7000; |
f39d05ca EN |
3248 | diff_opts.rename_score = opt->rename_score; |
3249 | diff_opts.show_rename_progress = opt->show_rename_progress; | |
3250 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
3251 | diff_setup_done(&diff_opts); | |
557ac035 | 3252 | |
f78cf976 | 3253 | diff_queued_diff = renames->pairs[side_index]; |
557ac035 | 3254 | trace2_region_enter("diff", "diffcore_rename", opt->repo); |
0c4fd732 | 3255 | diffcore_rename_extended(&diff_opts, |
62a15162 | 3256 | &opt->priv->pool, |
174791f0 | 3257 | &renames->relevant_sources[side_index], |
0c4fd732 | 3258 | &renames->dirs_removed[side_index], |
25e65b6d EN |
3259 | &renames->dir_rename_count[side_index], |
3260 | &renames->cached_pairs[side_index]); | |
557ac035 | 3261 | trace2_region_leave("diff", "diffcore_rename", opt->repo); |
f78cf976 | 3262 | resolve_diffpair_statuses(&diff_queued_diff); |
f39d05ca | 3263 | |
8b09a900 EN |
3264 | if (diff_opts.needed_rename_limit > 0) |
3265 | renames->redo_after_renames = 0; | |
f39d05ca EN |
3266 | if (diff_opts.needed_rename_limit > renames->needed_limit) |
3267 | renames->needed_limit = diff_opts.needed_rename_limit; | |
3268 | ||
3269 | renames->pairs[side_index] = diff_queued_diff; | |
3270 | ||
3271 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
3272 | diff_queued_diff.nr = 0; | |
3273 | diff_queued_diff.queue = NULL; | |
3274 | diff_flush(&diff_opts); | |
8b09a900 EN |
3275 | |
3276 | return 1; | |
e1a124e8 EN |
3277 | } |
3278 | ||
3279 | /* | |
356da0f9 EN |
3280 | * Get information of all renames which occurred in 'side_pairs', making use |
3281 | * of any implicit directory renames in side_dir_renames (also making use of | |
3282 | * implicit directory renames rename_exclusions as needed by | |
3283 | * check_for_directory_rename()). Add all (updated) renames into result. | |
e1a124e8 EN |
3284 | */ |
3285 | static int collect_renames(struct merge_options *opt, | |
3286 | struct diff_queue_struct *result, | |
fa5e06d6 | 3287 | unsigned side_index, |
3ffbe5a2 | 3288 | struct strmap *collisions, |
fa5e06d6 EN |
3289 | struct strmap *dir_renames_for_side, |
3290 | struct strmap *rename_exclusions) | |
e1a124e8 | 3291 | { |
965a7bc2 EN |
3292 | int i, clean = 1; |
3293 | struct diff_queue_struct *side_pairs; | |
3294 | struct rename_info *renames = &opt->priv->renames; | |
3295 | ||
3296 | side_pairs = &renames->pairs[side_index]; | |
3297 | ||
3298 | for (i = 0; i < side_pairs->nr; ++i) { | |
3299 | struct diff_filepair *p = side_pairs->queue[i]; | |
fa5e06d6 | 3300 | char *new_path; /* non-NULL only with directory renames */ |
965a7bc2 | 3301 | |
fa5e06d6 | 3302 | if (p->status != 'A' && p->status != 'R') { |
2734f2e3 | 3303 | possibly_cache_new_pair(renames, p, side_index, NULL); |
62a15162 | 3304 | pool_diff_free_filepair(&opt->priv->pool, p); |
965a7bc2 EN |
3305 | continue; |
3306 | } | |
3307 | ||
fa5e06d6 EN |
3308 | new_path = check_for_directory_rename(opt, p->two->path, |
3309 | side_index, | |
3310 | dir_renames_for_side, | |
3311 | rename_exclusions, | |
3ffbe5a2 | 3312 | collisions, |
fa5e06d6 EN |
3313 | &clean); |
3314 | ||
2734f2e3 | 3315 | possibly_cache_new_pair(renames, p, side_index, new_path); |
fa5e06d6 | 3316 | if (p->status != 'R' && !new_path) { |
62a15162 | 3317 | pool_diff_free_filepair(&opt->priv->pool, p); |
fa5e06d6 EN |
3318 | continue; |
3319 | } | |
3320 | ||
3321 | if (new_path) | |
3322 | apply_directory_rename_modifications(opt, p, new_path); | |
3323 | ||
965a7bc2 EN |
3324 | /* |
3325 | * p->score comes back from diffcore_rename_extended() with | |
3326 | * the similarity of the renamed file. The similarity is | |
3327 | * was used to determine that the two files were related | |
3328 | * and are a rename, which we have already used, but beyond | |
3329 | * that we have no use for the similarity. So p->score is | |
3330 | * now irrelevant. However, process_renames() will need to | |
3331 | * know which side of the merge this rename was associated | |
3332 | * with, so overwrite p->score with that value. | |
3333 | */ | |
3334 | p->score = side_index; | |
3335 | result->queue[result->nr++] = p; | |
3336 | } | |
3337 | ||
3338 | return clean; | |
e1a124e8 EN |
3339 | } |
3340 | ||
231e2dd4 EN |
3341 | static int detect_and_process_renames(struct merge_options *opt, |
3342 | struct tree *merge_base, | |
3343 | struct tree *side1, | |
3344 | struct tree *side2) | |
3345 | { | |
8d60e9d2 | 3346 | struct diff_queue_struct combined = { 0 }; |
e1a124e8 | 3347 | struct rename_info *renames = &opt->priv->renames; |
3ffbe5a2 | 3348 | struct strmap collisions[3]; |
8d60e9d2 | 3349 | int need_dir_renames, s, i, clean = 1; |
8b09a900 | 3350 | unsigned detection_run = 0; |
e1a124e8 | 3351 | |
f89b4f2b EN |
3352 | if (!possible_renames(renames)) |
3353 | goto cleanup; | |
e1a124e8 | 3354 | |
557ac035 | 3355 | trace2_region_enter("merge", "regular renames", opt->repo); |
8b09a900 EN |
3356 | detection_run |= detect_regular_renames(opt, MERGE_SIDE1); |
3357 | detection_run |= detect_regular_renames(opt, MERGE_SIDE2); | |
9ae39fef EN |
3358 | if (renames->needed_limit) { |
3359 | renames->cached_pairs_valid_side = 0; | |
3360 | renames->redo_after_renames = 0; | |
3361 | } | |
8b09a900 EN |
3362 | if (renames->redo_after_renames && detection_run) { |
3363 | int i, side; | |
3364 | struct diff_filepair *p; | |
3365 | ||
3366 | /* Cache the renames, we found */ | |
3367 | for (side = MERGE_SIDE1; side <= MERGE_SIDE2; side++) { | |
3368 | for (i = 0; i < renames->pairs[side].nr; ++i) { | |
3369 | p = renames->pairs[side].queue[i]; | |
3370 | possibly_cache_new_pair(renames, p, side, NULL); | |
3371 | } | |
3372 | } | |
3373 | ||
3374 | /* Restart the merge with the cached renames */ | |
3375 | renames->redo_after_renames = 2; | |
3376 | trace2_region_leave("merge", "regular renames", opt->repo); | |
3377 | goto cleanup; | |
3378 | } | |
25e65b6d EN |
3379 | use_cached_pairs(opt, &renames->cached_pairs[1], &renames->pairs[1]); |
3380 | use_cached_pairs(opt, &renames->cached_pairs[2], &renames->pairs[2]); | |
557ac035 | 3381 | trace2_region_leave("merge", "regular renames", opt->repo); |
e1a124e8 | 3382 | |
557ac035 | 3383 | trace2_region_enter("merge", "directory renames", opt->repo); |
112e1112 EN |
3384 | need_dir_renames = |
3385 | !opt->priv->call_depth && | |
3386 | (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE || | |
3387 | opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT); | |
3388 | ||
3389 | if (need_dir_renames) { | |
3390 | get_provisional_directory_renames(opt, MERGE_SIDE1, &clean); | |
3391 | get_provisional_directory_renames(opt, MERGE_SIDE2, &clean); | |
3392 | handle_directory_level_conflicts(opt); | |
3393 | } | |
3394 | ||
e1a124e8 EN |
3395 | ALLOC_GROW(combined.queue, |
3396 | renames->pairs[1].nr + renames->pairs[2].nr, | |
3397 | combined.alloc); | |
3ffbe5a2 EN |
3398 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) { |
3399 | int other_side = 3 - i; | |
3400 | compute_collisions(&collisions[i], | |
3401 | &renames->dir_renames[other_side], | |
3402 | &renames->pairs[i]); | |
3403 | } | |
fa5e06d6 | 3404 | clean &= collect_renames(opt, &combined, MERGE_SIDE1, |
3ffbe5a2 | 3405 | collisions, |
fa5e06d6 EN |
3406 | &renames->dir_renames[2], |
3407 | &renames->dir_renames[1]); | |
3408 | clean &= collect_renames(opt, &combined, MERGE_SIDE2, | |
3ffbe5a2 | 3409 | collisions, |
fa5e06d6 EN |
3410 | &renames->dir_renames[1], |
3411 | &renames->dir_renames[2]); | |
3ffbe5a2 EN |
3412 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) |
3413 | free_collisions(&collisions[i]); | |
72b30910 | 3414 | STABLE_QSORT(combined.queue, combined.nr, compare_pairs); |
557ac035 | 3415 | trace2_region_leave("merge", "directory renames", opt->repo); |
e1a124e8 | 3416 | |
557ac035 | 3417 | trace2_region_enter("merge", "process renames", opt->repo); |
e1a124e8 | 3418 | clean &= process_renames(opt, &combined); |
557ac035 | 3419 | trace2_region_leave("merge", "process renames", opt->repo); |
e1a124e8 | 3420 | |
f89b4f2b EN |
3421 | goto simple_cleanup; /* collect_renames() handles some of cleanup */ |
3422 | ||
3423 | cleanup: | |
3424 | /* | |
3425 | * Free now unneeded filepairs, which would have been handled | |
3426 | * in collect_renames() normally but we skipped that code. | |
3427 | */ | |
3428 | for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) { | |
3429 | struct diff_queue_struct *side_pairs; | |
3430 | int i; | |
3431 | ||
3432 | side_pairs = &renames->pairs[s]; | |
3433 | for (i = 0; i < side_pairs->nr; ++i) { | |
3434 | struct diff_filepair *p = side_pairs->queue[i]; | |
62a15162 | 3435 | pool_diff_free_filepair(&opt->priv->pool, p); |
f89b4f2b EN |
3436 | } |
3437 | } | |
3438 | ||
3439 | simple_cleanup: | |
e1a124e8 EN |
3440 | /* Free memory for renames->pairs[] and combined */ |
3441 | for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) { | |
3442 | free(renames->pairs[s].queue); | |
3443 | DIFF_QUEUE_CLEAR(&renames->pairs[s]); | |
3444 | } | |
8d60e9d2 EN |
3445 | for (i = 0; i < combined.nr; i++) |
3446 | pool_diff_free_filepair(&opt->priv->pool, combined.queue[i]); | |
3447 | free(combined.queue); | |
231e2dd4 | 3448 | |
231e2dd4 EN |
3449 | return clean; |
3450 | } | |
3451 | ||
04af1879 EN |
3452 | /*** Function Grouping: functions related to process_entries() ***/ |
3453 | ||
5a3743da | 3454 | static int sort_dirs_next_to_their_children(const char *one, const char *two) |
8adffaa8 | 3455 | { |
5a3743da EN |
3456 | unsigned char c1, c2; |
3457 | ||
3458 | /* | |
3459 | * Here we only care that entries for directories appear adjacent | |
3460 | * to and before files underneath the directory. We can achieve | |
3461 | * that by pretending to add a trailing slash to every file and | |
3462 | * then sorting. In other words, we do not want the natural | |
3463 | * sorting of | |
3464 | * foo | |
3465 | * foo.txt | |
3466 | * foo/bar | |
3467 | * Instead, we want "foo" to sort as though it were "foo/", so that | |
3468 | * we instead get | |
3469 | * foo.txt | |
3470 | * foo | |
3471 | * foo/bar | |
3472 | * To achieve this, we basically implement our own strcmp, except that | |
3473 | * if we get to the end of either string instead of comparing NUL to | |
3474 | * another character, we compare '/' to it. | |
3475 | * | |
3476 | * If this unusual "sort as though '/' were appended" perplexes | |
3477 | * you, perhaps it will help to note that this is not the final | |
3478 | * sort. write_tree() will sort again without the trailing slash | |
3479 | * magic, but just on paths immediately under a given tree. | |
8adffaa8 | 3480 | * |
5a3743da EN |
3481 | * The reason to not use df_name_compare directly was that it was |
3482 | * just too expensive (we don't have the string lengths handy), so | |
3483 | * it was reimplemented. | |
8adffaa8 | 3484 | */ |
5a3743da | 3485 | |
8adffaa8 | 3486 | /* |
5a3743da EN |
3487 | * NOTE: This function will never be called with two equal strings, |
3488 | * because it is used to sort the keys of a strmap, and strmaps have | |
3489 | * unique keys by construction. That simplifies our c1==c2 handling | |
3490 | * below. | |
8adffaa8 | 3491 | */ |
5a3743da EN |
3492 | |
3493 | while (*one && (*one == *two)) { | |
3494 | one++; | |
3495 | two++; | |
3496 | } | |
3497 | ||
3498 | c1 = *one ? *one : '/'; | |
3499 | c2 = *two ? *two : '/'; | |
3500 | ||
3501 | if (c1 == c2) { | |
3502 | /* Getting here means one is a leading directory of the other */ | |
3503 | return (*one) ? 1 : -1; | |
3504 | } else | |
3505 | return c1 - c2; | |
8adffaa8 EN |
3506 | } |
3507 | ||
3860220b EN |
3508 | static int read_oid_strbuf(struct merge_options *opt, |
3509 | const struct object_id *oid, | |
3510 | struct strbuf *dst) | |
3511 | { | |
3512 | void *buf; | |
3513 | enum object_type type; | |
3514 | unsigned long size; | |
bc726bd0 | 3515 | buf = repo_read_object_file(the_repository, oid, &type, &size); |
3860220b EN |
3516 | if (!buf) |
3517 | return err(opt, _("cannot read object %s"), oid_to_hex(oid)); | |
3518 | if (type != OBJ_BLOB) { | |
3519 | free(buf); | |
3520 | return err(opt, _("object %s is not a blob"), oid_to_hex(oid)); | |
3521 | } | |
3522 | strbuf_attach(dst, buf, size, size + 1); | |
3523 | return 0; | |
3524 | } | |
3525 | ||
3526 | static int blob_unchanged(struct merge_options *opt, | |
3527 | const struct version_info *base, | |
3528 | const struct version_info *side, | |
3529 | const char *path) | |
3530 | { | |
3531 | struct strbuf basebuf = STRBUF_INIT; | |
3532 | struct strbuf sidebuf = STRBUF_INIT; | |
3533 | int ret = 0; /* assume changed for safety */ | |
8e978529 | 3534 | struct index_state *idx = &opt->priv->attr_index; |
3860220b EN |
3535 | |
3536 | if (!idx->initialized) | |
3537 | initialize_attr_index(opt); | |
3538 | ||
3539 | if (base->mode != side->mode) | |
3540 | return 0; | |
3541 | if (oideq(&base->oid, &side->oid)) | |
3542 | return 1; | |
3543 | ||
3544 | if (read_oid_strbuf(opt, &base->oid, &basebuf) || | |
3545 | read_oid_strbuf(opt, &side->oid, &sidebuf)) | |
3546 | goto error_return; | |
3547 | /* | |
3548 | * Note: binary | is used so that both renormalizations are | |
3549 | * performed. Comparison can be skipped if both files are | |
3550 | * unchanged since their sha1s have already been compared. | |
3551 | */ | |
3552 | if (renormalize_buffer(idx, path, basebuf.buf, basebuf.len, &basebuf) | | |
3553 | renormalize_buffer(idx, path, sidebuf.buf, sidebuf.len, &sidebuf)) | |
3554 | ret = (basebuf.len == sidebuf.len && | |
3555 | !memcmp(basebuf.buf, sidebuf.buf, basebuf.len)); | |
3556 | ||
3557 | error_return: | |
3558 | strbuf_release(&basebuf); | |
3559 | strbuf_release(&sidebuf); | |
3560 | return ret; | |
3561 | } | |
3562 | ||
a9945bba | 3563 | struct directory_versions { |
bb470f4e EN |
3564 | /* |
3565 | * versions: list of (basename -> version_info) | |
3566 | * | |
3567 | * The basenames are in reverse lexicographic order of full pathnames, | |
3568 | * as processed in process_entries(). This puts all entries within | |
3569 | * a directory together, and covers the directory itself after | |
3570 | * everything within it, allowing us to write subtrees before needing | |
3571 | * to record information for the tree itself. | |
3572 | */ | |
a9945bba | 3573 | struct string_list versions; |
bb470f4e EN |
3574 | |
3575 | /* | |
3576 | * offsets: list of (full relative path directories -> integer offsets) | |
3577 | * | |
3578 | * Since versions contains basenames from files in multiple different | |
3579 | * directories, we need to know which entries in versions correspond | |
3580 | * to which directories. Values of e.g. | |
3581 | * "" 0 | |
3582 | * src 2 | |
3583 | * src/moduleA 5 | |
3584 | * Would mean that entries 0-1 of versions are files in the toplevel | |
3585 | * directory, entries 2-4 are files under src/, and the remaining | |
3586 | * entries starting at index 5 are files under src/moduleA/. | |
3587 | */ | |
3588 | struct string_list offsets; | |
3589 | ||
3590 | /* | |
3591 | * last_directory: directory that previously processed file found in | |
3592 | * | |
3593 | * last_directory starts NULL, but records the directory in which the | |
3594 | * previous file was found within. As soon as | |
3595 | * directory(current_file) != last_directory | |
3596 | * then we need to start updating accounting in versions & offsets. | |
3597 | * Note that last_directory is always the last path in "offsets" (or | |
3598 | * NULL if "offsets" is empty) so this exists just for quick access. | |
3599 | */ | |
3600 | const char *last_directory; | |
3601 | ||
3602 | /* last_directory_len: cached computation of strlen(last_directory) */ | |
3603 | unsigned last_directory_len; | |
a9945bba EN |
3604 | }; |
3605 | ||
ee4012dc EN |
3606 | static int tree_entry_order(const void *a_, const void *b_) |
3607 | { | |
3608 | const struct string_list_item *a = a_; | |
3609 | const struct string_list_item *b = b_; | |
3610 | ||
3611 | const struct merged_info *ami = a->util; | |
3612 | const struct merged_info *bmi = b->util; | |
3613 | return base_name_compare(a->string, strlen(a->string), ami->result.mode, | |
3614 | b->string, strlen(b->string), bmi->result.mode); | |
3615 | } | |
3616 | ||
0b55d930 JS |
3617 | static int write_tree(struct object_id *result_oid, |
3618 | struct string_list *versions, | |
3619 | unsigned int offset, | |
3620 | size_t hash_size) | |
ee4012dc EN |
3621 | { |
3622 | size_t maxlen = 0, extra; | |
c1ea48a8 | 3623 | unsigned int nr; |
ee4012dc | 3624 | struct strbuf buf = STRBUF_INIT; |
0b55d930 | 3625 | int i, ret = 0; |
ee4012dc | 3626 | |
c1ea48a8 AH |
3627 | assert(offset <= versions->nr); |
3628 | nr = versions->nr - offset; | |
3629 | if (versions->nr) | |
257ae76b | 3630 | /* No need for STABLE_QSORT -- filenames must be unique */ |
c1ea48a8 | 3631 | QSORT(versions->items + offset, nr, tree_entry_order); |
ee4012dc EN |
3632 | |
3633 | /* Pre-allocate some space in buf */ | |
3634 | extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */ | |
3635 | for (i = 0; i < nr; i++) { | |
3636 | maxlen += strlen(versions->items[offset+i].string) + extra; | |
3637 | } | |
3638 | strbuf_grow(&buf, maxlen); | |
3639 | ||
3640 | /* Write each entry out to buf */ | |
3641 | for (i = 0; i < nr; i++) { | |
3642 | struct merged_info *mi = versions->items[offset+i].util; | |
3643 | struct version_info *ri = &mi->result; | |
3644 | strbuf_addf(&buf, "%o %s%c", | |
3645 | ri->mode, | |
3646 | versions->items[offset+i].string, '\0'); | |
3647 | strbuf_add(&buf, ri->oid.hash, hash_size); | |
3648 | } | |
3649 | ||
3650 | /* Write this object file out, and record in result_oid */ | |
0b55d930 JS |
3651 | if (write_object_file(buf.buf, buf.len, OBJ_TREE, result_oid)) |
3652 | ret = -1; | |
ee4012dc | 3653 | strbuf_release(&buf); |
0b55d930 | 3654 | return ret; |
ee4012dc EN |
3655 | } |
3656 | ||
a9945bba EN |
3657 | static void record_entry_for_tree(struct directory_versions *dir_metadata, |
3658 | const char *path, | |
3659 | struct merged_info *mi) | |
3660 | { | |
3661 | const char *basename; | |
3662 | ||
3663 | if (mi->is_null) | |
3664 | /* nothing to record */ | |
3665 | return; | |
3666 | ||
3667 | basename = path + mi->basename_offset; | |
3668 | assert(strchr(basename, '/') == NULL); | |
3669 | string_list_append(&dir_metadata->versions, | |
3670 | basename)->util = &mi->result; | |
3671 | } | |
3672 | ||
0b55d930 JS |
3673 | static int write_completed_directory(struct merge_options *opt, |
3674 | const char *new_directory_name, | |
3675 | struct directory_versions *info) | |
bb470f4e EN |
3676 | { |
3677 | const char *prev_dir; | |
3678 | struct merged_info *dir_info = NULL; | |
0b55d930 | 3679 | unsigned int offset, ret = 0; |
bb470f4e EN |
3680 | |
3681 | /* | |
3682 | * Some explanation of info->versions and info->offsets... | |
3683 | * | |
3684 | * process_entries() iterates over all relevant files AND | |
3685 | * directories in reverse lexicographic order, and calls this | |
3686 | * function. Thus, an example of the paths that process_entries() | |
3687 | * could operate on (along with the directories for those paths | |
3688 | * being shown) is: | |
3689 | * | |
3690 | * xtract.c "" | |
3691 | * tokens.txt "" | |
3692 | * src/moduleB/umm.c src/moduleB | |
3693 | * src/moduleB/stuff.h src/moduleB | |
3694 | * src/moduleB/baz.c src/moduleB | |
3695 | * src/moduleB src | |
3696 | * src/moduleA/foo.c src/moduleA | |
3697 | * src/moduleA/bar.c src/moduleA | |
3698 | * src/moduleA src | |
3699 | * src "" | |
3700 | * Makefile "" | |
3701 | * | |
3702 | * info->versions: | |
3703 | * | |
3704 | * always contains the unprocessed entries and their | |
3705 | * version_info information. For example, after the first five | |
3706 | * entries above, info->versions would be: | |
3707 | * | |
3708 | * xtract.c <xtract.c's version_info> | |
3709 | * token.txt <token.txt's version_info> | |
3710 | * umm.c <src/moduleB/umm.c's version_info> | |
3711 | * stuff.h <src/moduleB/stuff.h's version_info> | |
3712 | * baz.c <src/moduleB/baz.c's version_info> | |
3713 | * | |
3714 | * Once a subdirectory is completed we remove the entries in | |
3715 | * that subdirectory from info->versions, writing it as a tree | |
3716 | * (write_tree()). Thus, as soon as we get to src/moduleB, | |
3717 | * info->versions would be updated to | |
3718 | * | |
3719 | * xtract.c <xtract.c's version_info> | |
3720 | * token.txt <token.txt's version_info> | |
3721 | * moduleB <src/moduleB's version_info> | |
3722 | * | |
3723 | * info->offsets: | |
3724 | * | |
3725 | * helps us track which entries in info->versions correspond to | |
3726 | * which directories. When we are N directories deep (e.g. 4 | |
3727 | * for src/modA/submod/subdir/), we have up to N+1 unprocessed | |
3728 | * directories (+1 because of toplevel dir). Corresponding to | |
3729 | * the info->versions example above, after processing five entries | |
3730 | * info->offsets will be: | |
3731 | * | |
3732 | * "" 0 | |
3733 | * src/moduleB 2 | |
3734 | * | |
3735 | * which is used to know that xtract.c & token.txt are from the | |
3736 | * toplevel dirctory, while umm.c & stuff.h & baz.c are from the | |
3737 | * src/moduleB directory. Again, following the example above, | |
3738 | * once we need to process src/moduleB, then info->offsets is | |
3739 | * updated to | |
3740 | * | |
3741 | * "" 0 | |
3742 | * src 2 | |
3743 | * | |
3744 | * which says that moduleB (and only moduleB so far) is in the | |
3745 | * src directory. | |
3746 | * | |
3747 | * One unique thing to note about info->offsets here is that | |
3748 | * "src" was not added to info->offsets until there was a path | |
3749 | * (a file OR directory) immediately below src/ that got | |
3750 | * processed. | |
3751 | * | |
3752 | * Since process_entry() just appends new entries to info->versions, | |
3753 | * write_completed_directory() only needs to do work if the next path | |
3754 | * is in a directory that is different than the last directory found | |
3755 | * in info->offsets. | |
3756 | */ | |
3757 | ||
3758 | /* | |
3759 | * If we are working with the same directory as the last entry, there | |
3760 | * is no work to do. (See comments above the directory_name member of | |
3761 | * struct merged_info for why we can use pointer comparison instead of | |
3762 | * strcmp here.) | |
3763 | */ | |
3764 | if (new_directory_name == info->last_directory) | |
0b55d930 | 3765 | return 0; |
bb470f4e EN |
3766 | |
3767 | /* | |
3768 | * If we are just starting (last_directory is NULL), or last_directory | |
3769 | * is a prefix of the current directory, then we can just update | |
3770 | * info->offsets to record the offset where we started this directory | |
3771 | * and update last_directory to have quick access to it. | |
3772 | */ | |
3773 | if (info->last_directory == NULL || | |
3774 | !strncmp(new_directory_name, info->last_directory, | |
3775 | info->last_directory_len)) { | |
3776 | uintptr_t offset = info->versions.nr; | |
3777 | ||
3778 | info->last_directory = new_directory_name; | |
3779 | info->last_directory_len = strlen(info->last_directory); | |
3780 | /* | |
3781 | * Record the offset into info->versions where we will | |
3782 | * start recording basenames of paths found within | |
3783 | * new_directory_name. | |
3784 | */ | |
3785 | string_list_append(&info->offsets, | |
3786 | info->last_directory)->util = (void*)offset; | |
0b55d930 | 3787 | return 0; |
bb470f4e EN |
3788 | } |
3789 | ||
3790 | /* | |
3791 | * The next entry that will be processed will be within | |
3792 | * new_directory_name. Since at this point we know that | |
3793 | * new_directory_name is within a different directory than | |
3794 | * info->last_directory, we have all entries for info->last_directory | |
3795 | * in info->versions and we need to create a tree object for them. | |
3796 | */ | |
3797 | dir_info = strmap_get(&opt->priv->paths, info->last_directory); | |
3798 | assert(dir_info); | |
3799 | offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util; | |
3800 | if (offset == info->versions.nr) { | |
3801 | /* | |
3802 | * Actually, we don't need to create a tree object in this | |
3803 | * case. Whenever all files within a directory disappear | |
3804 | * during the merge (e.g. unmodified on one side and | |
3805 | * deleted on the other, or files were renamed elsewhere), | |
3806 | * then we get here and the directory itself needs to be | |
3807 | * omitted from its parent tree as well. | |
3808 | */ | |
3809 | dir_info->is_null = 1; | |
3810 | } else { | |
3811 | /* | |
3812 | * Write out the tree to the git object directory, and also | |
3813 | * record the mode and oid in dir_info->result. | |
3814 | */ | |
3815 | dir_info->is_null = 0; | |
3816 | dir_info->result.mode = S_IFDIR; | |
0b55d930 JS |
3817 | if (write_tree(&dir_info->result.oid, &info->versions, offset, |
3818 | opt->repo->hash_algo->rawsz) < 0) | |
3819 | ret = -1; | |
bb470f4e EN |
3820 | } |
3821 | ||
3822 | /* | |
3823 | * We've now used several entries from info->versions and one entry | |
3824 | * from info->offsets, so we get rid of those values. | |
3825 | */ | |
3826 | info->offsets.nr--; | |
3827 | info->versions.nr = offset; | |
3828 | ||
3829 | /* | |
3830 | * Now we've taken care of the completed directory, but we need to | |
3831 | * prepare things since future entries will be in | |
3832 | * new_directory_name. (In particular, process_entry() will be | |
3833 | * appending new entries to info->versions.) So, we need to make | |
3834 | * sure new_directory_name is the last entry in info->offsets. | |
3835 | */ | |
3836 | prev_dir = info->offsets.nr == 0 ? NULL : | |
3837 | info->offsets.items[info->offsets.nr-1].string; | |
3838 | if (new_directory_name != prev_dir) { | |
3839 | uintptr_t c = info->versions.nr; | |
3840 | string_list_append(&info->offsets, | |
3841 | new_directory_name)->util = (void*)c; | |
3842 | } | |
3843 | ||
3844 | /* And, of course, we need to update last_directory to match. */ | |
3845 | info->last_directory = new_directory_name; | |
3846 | info->last_directory_len = strlen(info->last_directory); | |
0b55d930 JS |
3847 | |
3848 | return ret; | |
bb470f4e EN |
3849 | } |
3850 | ||
6a02dd90 | 3851 | /* Per entry merge function */ |
92481d1b JS |
3852 | static int process_entry(struct merge_options *opt, |
3853 | const char *path, | |
3854 | struct conflict_info *ci, | |
3855 | struct directory_versions *dir_metadata) | |
6a02dd90 | 3856 | { |
23366d2a EN |
3857 | int df_file_index = 0; |
3858 | ||
6a02dd90 EN |
3859 | VERIFY_CI(ci); |
3860 | assert(ci->filemask >= 0 && ci->filemask <= 7); | |
3861 | /* ci->match_mask == 7 was handled in collect_merge_info_callback() */ | |
3862 | assert(ci->match_mask == 0 || ci->match_mask == 3 || | |
3863 | ci->match_mask == 5 || ci->match_mask == 6); | |
3864 | ||
a9945bba EN |
3865 | if (ci->dirmask) { |
3866 | record_entry_for_tree(dir_metadata, path, &ci->merged); | |
3867 | if (ci->filemask == 0) | |
3868 | /* nothing else to handle */ | |
92481d1b | 3869 | return 0; |
a9945bba EN |
3870 | assert(ci->df_conflict); |
3871 | } | |
3872 | ||
0ccfa4e5 EN |
3873 | if (ci->df_conflict && ci->merged.result.mode == 0) { |
3874 | int i; | |
3875 | ||
3876 | /* | |
3877 | * directory no longer in the way, but we do have a file we | |
3878 | * need to place here so we need to clean away the "directory | |
3879 | * merges to nothing" result. | |
3880 | */ | |
3881 | ci->df_conflict = 0; | |
3882 | assert(ci->filemask != 0); | |
3883 | ci->merged.clean = 0; | |
3884 | ci->merged.is_null = 0; | |
3885 | /* and we want to zero out any directory-related entries */ | |
3886 | ci->match_mask = (ci->match_mask & ~ci->dirmask); | |
3887 | ci->dirmask = 0; | |
3888 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
3889 | if (ci->filemask & (1 << i)) | |
3890 | continue; | |
3891 | ci->stages[i].mode = 0; | |
14228447 | 3892 | oidcpy(&ci->stages[i].oid, null_oid()); |
0ccfa4e5 EN |
3893 | } |
3894 | } else if (ci->df_conflict && ci->merged.result.mode != 0) { | |
23366d2a EN |
3895 | /* |
3896 | * This started out as a D/F conflict, and the entries in | |
3897 | * the competing directory were not removed by the merge as | |
3898 | * evidenced by write_completed_directory() writing a value | |
3899 | * to ci->merged.result.mode. | |
3900 | */ | |
3901 | struct conflict_info *new_ci; | |
3902 | const char *branch; | |
3903 | const char *old_path = path; | |
3904 | int i; | |
3905 | ||
3906 | assert(ci->merged.result.mode == S_IFDIR); | |
3907 | ||
3908 | /* | |
3909 | * If filemask is 1, we can just ignore the file as having | |
3910 | * been deleted on both sides. We do not want to overwrite | |
3911 | * ci->merged.result, since it stores the tree for all the | |
3912 | * files under it. | |
3913 | */ | |
3914 | if (ci->filemask == 1) { | |
3915 | ci->filemask = 0; | |
92481d1b | 3916 | return 0; |
23366d2a EN |
3917 | } |
3918 | ||
3919 | /* | |
3920 | * This file still exists on at least one side, and we want | |
3921 | * the directory to remain here, so we need to move this | |
3922 | * path to some new location. | |
3923 | */ | |
62a15162 | 3924 | new_ci = mem_pool_calloc(&opt->priv->pool, 1, sizeof(*new_ci)); |
6697ee01 | 3925 | |
23366d2a EN |
3926 | /* We don't really want new_ci->merged.result copied, but it'll |
3927 | * be overwritten below so it doesn't matter. We also don't | |
3928 | * want any directory mode/oid values copied, but we'll zero | |
3929 | * those out immediately. We do want the rest of ci copied. | |
3930 | */ | |
3931 | memcpy(new_ci, ci, sizeof(*ci)); | |
3932 | new_ci->match_mask = (new_ci->match_mask & ~new_ci->dirmask); | |
3933 | new_ci->dirmask = 0; | |
3934 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
3935 | if (new_ci->filemask & (1 << i)) | |
3936 | continue; | |
3937 | /* zero out any entries related to directories */ | |
3938 | new_ci->stages[i].mode = 0; | |
14228447 | 3939 | oidcpy(&new_ci->stages[i].oid, null_oid()); |
23366d2a EN |
3940 | } |
3941 | ||
3942 | /* | |
3943 | * Find out which side this file came from; note that we | |
3944 | * cannot just use ci->filemask, because renames could cause | |
3945 | * the filemask to go back to 7. So we use dirmask, then | |
3946 | * pick the opposite side's index. | |
3947 | */ | |
3948 | df_file_index = (ci->dirmask & (1 << 1)) ? 2 : 1; | |
3949 | branch = (df_file_index == 1) ? opt->branch1 : opt->branch2; | |
81afc794 | 3950 | path = unique_path(opt, path, branch); |
23366d2a EN |
3951 | strmap_put(&opt->priv->paths, path, new_ci); |
3952 | ||
cb260775 EN |
3953 | path_msg(opt, CONFLICT_FILE_DIRECTORY, 0, |
3954 | path, old_path, NULL, NULL, | |
23366d2a EN |
3955 | _("CONFLICT (file/directory): directory in the way " |
3956 | "of %s from %s; moving it to %s instead."), | |
3957 | old_path, branch, path); | |
3958 | ||
3959 | /* | |
3960 | * Zero out the filemask for the old ci. At this point, ci | |
3961 | * was just an entry for a directory, so we don't need to | |
3962 | * do anything more with it. | |
3963 | */ | |
3964 | ci->filemask = 0; | |
3965 | ||
3966 | /* | |
3967 | * Now note that we're working on the new entry (path was | |
3968 | * updated above. | |
3969 | */ | |
3970 | ci = new_ci; | |
6a02dd90 EN |
3971 | } |
3972 | ||
3973 | /* | |
3974 | * NOTE: Below there is a long switch-like if-elseif-elseif... block | |
3975 | * which the code goes through even for the df_conflict cases | |
23366d2a | 3976 | * above. |
6a02dd90 EN |
3977 | */ |
3978 | if (ci->match_mask) { | |
a492d533 | 3979 | ci->merged.clean = !ci->df_conflict && !ci->path_conflict; |
6a02dd90 EN |
3980 | if (ci->match_mask == 6) { |
3981 | /* stages[1] == stages[2] */ | |
3982 | ci->merged.result.mode = ci->stages[1].mode; | |
3983 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
3984 | } else { | |
3985 | /* determine the mask of the side that didn't match */ | |
3986 | unsigned int othermask = 7 & ~ci->match_mask; | |
3987 | int side = (othermask == 4) ? 2 : 1; | |
3988 | ||
3989 | ci->merged.result.mode = ci->stages[side].mode; | |
3990 | ci->merged.is_null = !ci->merged.result.mode; | |
a492d533 EN |
3991 | if (ci->merged.is_null) |
3992 | ci->merged.clean = 1; | |
6a02dd90 EN |
3993 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); |
3994 | ||
3995 | assert(othermask == 2 || othermask == 4); | |
3996 | assert(ci->merged.is_null == | |
3997 | (ci->filemask == ci->match_mask)); | |
3998 | } | |
3999 | } else if (ci->filemask >= 6 && | |
4000 | (S_IFMT & ci->stages[1].mode) != | |
4001 | (S_IFMT & ci->stages[2].mode)) { | |
4ef88fc3 EN |
4002 | /* Two different items from (file/submodule/symlink) */ |
4003 | if (opt->priv->call_depth) { | |
4004 | /* Just use the version from the merge base */ | |
4005 | ci->merged.clean = 0; | |
4006 | oidcpy(&ci->merged.result.oid, &ci->stages[0].oid); | |
4007 | ci->merged.result.mode = ci->stages[0].mode; | |
4008 | ci->merged.is_null = (ci->merged.result.mode == 0); | |
4009 | } else { | |
4010 | /* Handle by renaming one or both to separate paths. */ | |
4011 | unsigned o_mode = ci->stages[0].mode; | |
4012 | unsigned a_mode = ci->stages[1].mode; | |
4013 | unsigned b_mode = ci->stages[2].mode; | |
4014 | struct conflict_info *new_ci; | |
4015 | const char *a_path = NULL, *b_path = NULL; | |
4016 | int rename_a = 0, rename_b = 0; | |
4017 | ||
62a15162 EN |
4018 | new_ci = mem_pool_alloc(&opt->priv->pool, |
4019 | sizeof(*new_ci)); | |
4ef88fc3 EN |
4020 | |
4021 | if (S_ISREG(a_mode)) | |
4022 | rename_a = 1; | |
4023 | else if (S_ISREG(b_mode)) | |
4024 | rename_b = 1; | |
4025 | else { | |
4026 | rename_a = 1; | |
4027 | rename_b = 1; | |
4028 | } | |
4029 | ||
cb260775 EN |
4030 | if (rename_a) |
4031 | a_path = unique_path(opt, path, opt->branch1); | |
4032 | if (rename_b) | |
4033 | b_path = unique_path(opt, path, opt->branch2); | |
4034 | ||
0e59f7ad | 4035 | if (rename_a && rename_b) { |
cb260775 EN |
4036 | path_msg(opt, CONFLICT_DISTINCT_MODES, 0, |
4037 | path, a_path, b_path, NULL, | |
0e59f7ad AH |
4038 | _("CONFLICT (distinct types): %s had " |
4039 | "different types on each side; " | |
4040 | "renamed both of them so each can " | |
4041 | "be recorded somewhere."), | |
4042 | path); | |
4043 | } else { | |
cb260775 EN |
4044 | path_msg(opt, CONFLICT_DISTINCT_MODES, 0, |
4045 | path, rename_a ? a_path : b_path, | |
4046 | NULL, NULL, | |
0e59f7ad AH |
4047 | _("CONFLICT (distinct types): %s had " |
4048 | "different types on each side; " | |
4049 | "renamed one of them so each can be " | |
4050 | "recorded somewhere."), | |
4051 | path); | |
4052 | } | |
4ef88fc3 EN |
4053 | |
4054 | ci->merged.clean = 0; | |
4055 | memcpy(new_ci, ci, sizeof(*new_ci)); | |
4056 | ||
4057 | /* Put b into new_ci, removing a from stages */ | |
4058 | new_ci->merged.result.mode = ci->stages[2].mode; | |
4059 | oidcpy(&new_ci->merged.result.oid, &ci->stages[2].oid); | |
4060 | new_ci->stages[1].mode = 0; | |
14228447 | 4061 | oidcpy(&new_ci->stages[1].oid, null_oid()); |
4ef88fc3 EN |
4062 | new_ci->filemask = 5; |
4063 | if ((S_IFMT & b_mode) != (S_IFMT & o_mode)) { | |
4064 | new_ci->stages[0].mode = 0; | |
14228447 | 4065 | oidcpy(&new_ci->stages[0].oid, null_oid()); |
4ef88fc3 EN |
4066 | new_ci->filemask = 4; |
4067 | } | |
4068 | ||
4069 | /* Leave only a in ci, fixing stages. */ | |
4070 | ci->merged.result.mode = ci->stages[1].mode; | |
4071 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
4072 | ci->stages[2].mode = 0; | |
14228447 | 4073 | oidcpy(&ci->stages[2].oid, null_oid()); |
4ef88fc3 EN |
4074 | ci->filemask = 3; |
4075 | if ((S_IFMT & a_mode) != (S_IFMT & o_mode)) { | |
4076 | ci->stages[0].mode = 0; | |
14228447 | 4077 | oidcpy(&ci->stages[0].oid, null_oid()); |
4ef88fc3 EN |
4078 | ci->filemask = 2; |
4079 | } | |
4080 | ||
4081 | /* Insert entries into opt->priv_paths */ | |
4082 | assert(rename_a || rename_b); | |
cb260775 | 4083 | if (rename_a) |
4ef88fc3 | 4084 | strmap_put(&opt->priv->paths, a_path, ci); |
4ef88fc3 | 4085 | |
cb260775 | 4086 | if (!rename_b) |
4ef88fc3 EN |
4087 | b_path = path; |
4088 | strmap_put(&opt->priv->paths, b_path, new_ci); | |
4089 | ||
62a15162 | 4090 | if (rename_a && rename_b) |
4ef88fc3 | 4091 | strmap_remove(&opt->priv->paths, path, 0); |
4ef88fc3 EN |
4092 | |
4093 | /* | |
4094 | * Do special handling for b_path since process_entry() | |
4095 | * won't be called on it specially. | |
4096 | */ | |
4097 | strmap_put(&opt->priv->conflicted, b_path, new_ci); | |
4098 | record_entry_for_tree(dir_metadata, b_path, | |
4099 | &new_ci->merged); | |
4100 | ||
4101 | /* | |
4102 | * Remaining code for processing this entry should | |
4103 | * think in terms of processing a_path. | |
4104 | */ | |
4105 | if (a_path) | |
4106 | path = a_path; | |
4107 | } | |
6a02dd90 | 4108 | } else if (ci->filemask >= 6) { |
991bbdca EN |
4109 | /* Need a two-way or three-way content merge */ |
4110 | struct version_info merged_file; | |
92481d1b | 4111 | int clean_merge; |
991bbdca EN |
4112 | struct version_info *o = &ci->stages[0]; |
4113 | struct version_info *a = &ci->stages[1]; | |
4114 | struct version_info *b = &ci->stages[2]; | |
4115 | ||
4116 | clean_merge = handle_content_merge(opt, path, o, a, b, | |
4117 | ci->pathnames, | |
4118 | opt->priv->call_depth * 2, | |
4119 | &merged_file); | |
92481d1b JS |
4120 | if (clean_merge < 0) |
4121 | return -1; | |
991bbdca EN |
4122 | ci->merged.clean = clean_merge && |
4123 | !ci->df_conflict && !ci->path_conflict; | |
4124 | ci->merged.result.mode = merged_file.mode; | |
4125 | ci->merged.is_null = (merged_file.mode == 0); | |
4126 | oidcpy(&ci->merged.result.oid, &merged_file.oid); | |
4127 | if (clean_merge && ci->df_conflict) { | |
4128 | assert(df_file_index == 1 || df_file_index == 2); | |
4129 | ci->filemask = 1 << df_file_index; | |
4130 | ci->stages[df_file_index].mode = merged_file.mode; | |
4131 | oidcpy(&ci->stages[df_file_index].oid, &merged_file.oid); | |
4132 | } | |
4133 | if (!clean_merge) { | |
4134 | const char *reason = _("content"); | |
4135 | if (ci->filemask == 6) | |
4136 | reason = _("add/add"); | |
4137 | if (S_ISGITLINK(merged_file.mode)) | |
4138 | reason = _("submodule"); | |
cb260775 EN |
4139 | path_msg(opt, CONFLICT_CONTENTS, 0, |
4140 | path, NULL, NULL, NULL, | |
991bbdca EN |
4141 | _("CONFLICT (%s): Merge conflict in %s"), |
4142 | reason, path); | |
4143 | } | |
6a02dd90 EN |
4144 | } else if (ci->filemask == 3 || ci->filemask == 5) { |
4145 | /* Modify/delete */ | |
c5a6f655 EN |
4146 | const char *modify_branch, *delete_branch; |
4147 | int side = (ci->filemask == 5) ? 2 : 1; | |
4148 | int index = opt->priv->call_depth ? 0 : side; | |
4149 | ||
4150 | ci->merged.result.mode = ci->stages[index].mode; | |
4151 | oidcpy(&ci->merged.result.oid, &ci->stages[index].oid); | |
4152 | ci->merged.clean = 0; | |
4153 | ||
4154 | modify_branch = (side == 1) ? opt->branch1 : opt->branch2; | |
4155 | delete_branch = (side == 1) ? opt->branch2 : opt->branch1; | |
4156 | ||
3860220b EN |
4157 | if (opt->renormalize && |
4158 | blob_unchanged(opt, &ci->stages[0], &ci->stages[side], | |
4159 | path)) { | |
d30126c2 EN |
4160 | if (!ci->path_conflict) { |
4161 | /* | |
4162 | * Blob unchanged after renormalization, so | |
4163 | * there's no modify/delete conflict after all; | |
4164 | * we can just remove the file. | |
4165 | */ | |
4166 | ci->merged.is_null = 1; | |
4167 | ci->merged.clean = 1; | |
4168 | /* | |
4169 | * file goes away => even if there was a | |
4170 | * directory/file conflict there isn't one now. | |
4171 | */ | |
4172 | ci->df_conflict = 0; | |
4173 | } else { | |
4174 | /* rename/delete, so conflict remains */ | |
4175 | } | |
3860220b EN |
4176 | } else if (ci->path_conflict && |
4177 | oideq(&ci->stages[0].oid, &ci->stages[side].oid)) { | |
2e91ddd2 EN |
4178 | /* |
4179 | * This came from a rename/delete; no action to take, | |
4180 | * but avoid printing "modify/delete" conflict notice | |
4181 | * since the contents were not modified. | |
4182 | */ | |
4183 | } else { | |
cb260775 EN |
4184 | path_msg(opt, CONFLICT_MODIFY_DELETE, 0, |
4185 | path, NULL, NULL, NULL, | |
2e91ddd2 EN |
4186 | _("CONFLICT (modify/delete): %s deleted in %s " |
4187 | "and modified in %s. Version %s of %s left " | |
4188 | "in tree."), | |
4189 | path, delete_branch, modify_branch, | |
4190 | modify_branch, path); | |
4191 | } | |
6a02dd90 EN |
4192 | } else if (ci->filemask == 2 || ci->filemask == 4) { |
4193 | /* Added on one side */ | |
4194 | int side = (ci->filemask == 4) ? 2 : 1; | |
4195 | ci->merged.result.mode = ci->stages[side].mode; | |
4196 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
53e88a03 | 4197 | ci->merged.clean = !ci->df_conflict && !ci->path_conflict; |
6a02dd90 EN |
4198 | } else if (ci->filemask == 1) { |
4199 | /* Deleted on both sides */ | |
4200 | ci->merged.is_null = 1; | |
4201 | ci->merged.result.mode = 0; | |
14228447 | 4202 | oidcpy(&ci->merged.result.oid, null_oid()); |
a492d533 | 4203 | assert(!ci->df_conflict); |
53e88a03 | 4204 | ci->merged.clean = !ci->path_conflict; |
6a02dd90 EN |
4205 | } |
4206 | ||
4207 | /* | |
4208 | * If still conflicted, record it separately. This allows us to later | |
4209 | * iterate over just conflicted entries when updating the index instead | |
4210 | * of iterating over all entries. | |
4211 | */ | |
4212 | if (!ci->merged.clean) | |
4213 | strmap_put(&opt->priv->conflicted, path, ci); | |
ef68c3d8 EN |
4214 | |
4215 | /* Record metadata for ci->merged in dir_metadata */ | |
a9945bba | 4216 | record_entry_for_tree(dir_metadata, path, &ci->merged); |
92481d1b | 4217 | return 0; |
6a02dd90 EN |
4218 | } |
4219 | ||
2bff554b EN |
4220 | static void prefetch_for_content_merges(struct merge_options *opt, |
4221 | struct string_list *plist) | |
4222 | { | |
4223 | struct string_list_item *e; | |
4224 | struct oid_array to_fetch = OID_ARRAY_INIT; | |
4225 | ||
a5183d76 | 4226 | if (opt->repo != the_repository || !repo_has_promisor_remote(the_repository)) |
2bff554b EN |
4227 | return; |
4228 | ||
4229 | for (e = &plist->items[plist->nr-1]; e >= plist->items; --e) { | |
4230 | /* char *path = e->string; */ | |
4231 | struct conflict_info *ci = e->util; | |
4232 | int i; | |
4233 | ||
4234 | /* Ignore clean entries */ | |
4235 | if (ci->merged.clean) | |
4236 | continue; | |
4237 | ||
4238 | /* Ignore entries that don't need a content merge */ | |
4239 | if (ci->match_mask || ci->filemask < 6 || | |
4240 | !S_ISREG(ci->stages[1].mode) || | |
4241 | !S_ISREG(ci->stages[2].mode) || | |
4242 | oideq(&ci->stages[1].oid, &ci->stages[2].oid)) | |
4243 | continue; | |
4244 | ||
4245 | /* Also don't need content merge if base matches either side */ | |
4246 | if (ci->filemask == 7 && | |
4247 | S_ISREG(ci->stages[0].mode) && | |
4248 | (oideq(&ci->stages[0].oid, &ci->stages[1].oid) || | |
4249 | oideq(&ci->stages[0].oid, &ci->stages[2].oid))) | |
4250 | continue; | |
4251 | ||
4252 | for (i = 0; i < 3; i++) { | |
4253 | unsigned side_mask = (1 << i); | |
4254 | struct version_info *vi = &ci->stages[i]; | |
4255 | ||
4256 | if ((ci->filemask & side_mask) && | |
4257 | S_ISREG(vi->mode) && | |
4258 | oid_object_info_extended(opt->repo, &vi->oid, NULL, | |
4259 | OBJECT_INFO_FOR_PREFETCH)) | |
4260 | oid_array_append(&to_fetch, &vi->oid); | |
4261 | } | |
4262 | } | |
4263 | ||
4264 | promisor_remote_get_direct(opt->repo, to_fetch.oid, to_fetch.nr); | |
4265 | oid_array_clear(&to_fetch); | |
4266 | } | |
4267 | ||
0b55d930 JS |
4268 | static int process_entries(struct merge_options *opt, |
4269 | struct object_id *result_oid) | |
231e2dd4 | 4270 | { |
6a02dd90 EN |
4271 | struct hashmap_iter iter; |
4272 | struct strmap_entry *e; | |
8adffaa8 EN |
4273 | struct string_list plist = STRING_LIST_INIT_NODUP; |
4274 | struct string_list_item *entry; | |
bb470f4e EN |
4275 | struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP, |
4276 | STRING_LIST_INIT_NODUP, | |
4277 | NULL, 0 }; | |
0b55d930 | 4278 | int ret = 0; |
6a02dd90 | 4279 | |
557ac035 | 4280 | trace2_region_enter("merge", "process_entries setup", opt->repo); |
6a02dd90 EN |
4281 | if (strmap_empty(&opt->priv->paths)) { |
4282 | oidcpy(result_oid, opt->repo->hash_algo->empty_tree); | |
0b55d930 | 4283 | return 0; |
6a02dd90 EN |
4284 | } |
4285 | ||
8adffaa8 | 4286 | /* Hack to pre-allocate plist to the desired size */ |
557ac035 | 4287 | trace2_region_enter("merge", "plist grow", opt->repo); |
8adffaa8 | 4288 | ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc); |
557ac035 | 4289 | trace2_region_leave("merge", "plist grow", opt->repo); |
8adffaa8 EN |
4290 | |
4291 | /* Put every entry from paths into plist, then sort */ | |
557ac035 | 4292 | trace2_region_enter("merge", "plist copy", opt->repo); |
6a02dd90 | 4293 | strmap_for_each_entry(&opt->priv->paths, &iter, e) { |
8adffaa8 EN |
4294 | string_list_append(&plist, e->key)->util = e->value; |
4295 | } | |
557ac035 EN |
4296 | trace2_region_leave("merge", "plist copy", opt->repo); |
4297 | ||
4298 | trace2_region_enter("merge", "plist special sort", opt->repo); | |
5a3743da | 4299 | plist.cmp = sort_dirs_next_to_their_children; |
8adffaa8 | 4300 | string_list_sort(&plist); |
557ac035 EN |
4301 | trace2_region_leave("merge", "plist special sort", opt->repo); |
4302 | ||
4303 | trace2_region_leave("merge", "process_entries setup", opt->repo); | |
8adffaa8 EN |
4304 | |
4305 | /* | |
4306 | * Iterate over the items in reverse order, so we can handle paths | |
4307 | * below a directory before needing to handle the directory itself. | |
bb470f4e EN |
4308 | * |
4309 | * This allows us to write subtrees before we need to write trees, | |
4310 | * and it also enables sane handling of directory/file conflicts | |
4311 | * (because it allows us to know whether the directory is still in | |
4312 | * the way when it is time to process the file at the same path). | |
8adffaa8 | 4313 | */ |
557ac035 | 4314 | trace2_region_enter("merge", "processing", opt->repo); |
2bff554b | 4315 | prefetch_for_content_merges(opt, &plist); |
8adffaa8 EN |
4316 | for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) { |
4317 | char *path = entry->string; | |
6a02dd90 EN |
4318 | /* |
4319 | * NOTE: mi may actually be a pointer to a conflict_info, but | |
4320 | * we have to check mi->clean first to see if it's safe to | |
4321 | * reassign to such a pointer type. | |
4322 | */ | |
8adffaa8 | 4323 | struct merged_info *mi = entry->util; |
6a02dd90 | 4324 | |
0b55d930 JS |
4325 | if (write_completed_directory(opt, mi->directory_name, |
4326 | &dir_metadata) < 0) { | |
4327 | ret = -1; | |
4328 | goto cleanup; | |
4329 | } | |
a9945bba EN |
4330 | if (mi->clean) |
4331 | record_entry_for_tree(&dir_metadata, path, mi); | |
4332 | else { | |
8adffaa8 | 4333 | struct conflict_info *ci = (struct conflict_info *)mi; |
92481d1b JS |
4334 | if (process_entry(opt, path, ci, &dir_metadata) < 0) { |
4335 | ret = -1; | |
4336 | goto cleanup; | |
4337 | }; | |
8adffaa8 | 4338 | } |
6a02dd90 | 4339 | } |
557ac035 | 4340 | trace2_region_leave("merge", "processing", opt->repo); |
6a02dd90 | 4341 | |
557ac035 | 4342 | trace2_region_enter("merge", "process_entries cleanup", opt->repo); |
bb470f4e EN |
4343 | if (dir_metadata.offsets.nr != 1 || |
4344 | (uintptr_t)dir_metadata.offsets.items[0].util != 0) { | |
99d60545 ÆAB |
4345 | printf("dir_metadata.offsets.nr = %"PRIuMAX" (should be 1)\n", |
4346 | (uintmax_t)dir_metadata.offsets.nr); | |
bb470f4e EN |
4347 | printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n", |
4348 | (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util); | |
4349 | fflush(stdout); | |
4350 | BUG("dir_metadata accounting completely off; shouldn't happen"); | |
4351 | } | |
0b55d930 JS |
4352 | if (write_tree(result_oid, &dir_metadata.versions, 0, |
4353 | opt->repo->hash_algo->rawsz) < 0) | |
4354 | ret = -1; | |
4355 | cleanup: | |
8adffaa8 | 4356 | string_list_clear(&plist, 0); |
a9945bba | 4357 | string_list_clear(&dir_metadata.versions, 0); |
bb470f4e | 4358 | string_list_clear(&dir_metadata.offsets, 0); |
557ac035 | 4359 | trace2_region_leave("merge", "process_entries cleanup", opt->repo); |
0b55d930 JS |
4360 | |
4361 | return ret; | |
231e2dd4 EN |
4362 | } |
4363 | ||
04af1879 EN |
4364 | /*** Function Grouping: functions related to merge_switch_to_result() ***/ |
4365 | ||
9fefce68 EN |
4366 | static int checkout(struct merge_options *opt, |
4367 | struct tree *prev, | |
4368 | struct tree *next) | |
4369 | { | |
6681ce5c EN |
4370 | /* Switch the index/working copy from old to new */ |
4371 | int ret; | |
4372 | struct tree_desc trees[2]; | |
4373 | struct unpack_trees_options unpack_opts; | |
4374 | ||
4375 | memset(&unpack_opts, 0, sizeof(unpack_opts)); | |
4376 | unpack_opts.head_idx = -1; | |
4377 | unpack_opts.src_index = opt->repo->index; | |
4378 | unpack_opts.dst_index = opt->repo->index; | |
4379 | ||
4380 | setup_unpack_trees_porcelain(&unpack_opts, "merge"); | |
4381 | ||
4382 | /* | |
4383 | * NOTE: if this were just "git checkout" code, we would probably | |
4384 | * read or refresh the cache and check for a conflicted index, but | |
4385 | * builtin/merge.c or sequencer.c really needs to read the index | |
4386 | * and check for conflicted entries before starting merging for a | |
4387 | * good user experience (no sense waiting for merges/rebases before | |
4388 | * erroring out), so there's no reason to duplicate that work here. | |
4389 | */ | |
4390 | ||
4391 | /* 2-way merge to the new branch */ | |
4392 | unpack_opts.update = 1; | |
4393 | unpack_opts.merge = 1; | |
4394 | unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */ | |
4395 | unpack_opts.verbose_update = (opt->verbosity > 2); | |
4396 | unpack_opts.fn = twoway_merge; | |
1b5f3733 | 4397 | unpack_opts.preserve_ignored = 0; /* FIXME: !opts->overwrite_ignore */ |
6681ce5c EN |
4398 | parse_tree(prev); |
4399 | init_tree_desc(&trees[0], prev->buffer, prev->size); | |
4400 | parse_tree(next); | |
4401 | init_tree_desc(&trees[1], next->buffer, next->size); | |
4402 | ||
4403 | ret = unpack_trees(2, trees, &unpack_opts); | |
4404 | clear_unpack_trees_porcelain(&unpack_opts); | |
6681ce5c | 4405 | return ret; |
9fefce68 EN |
4406 | } |
4407 | ||
66b209b8 | 4408 | static int record_conflicted_index_entries(struct merge_options *opt) |
9fefce68 | 4409 | { |
ef2b3693 EN |
4410 | struct hashmap_iter iter; |
4411 | struct strmap_entry *e; | |
66b209b8 EN |
4412 | struct index_state *index = opt->repo->index; |
4413 | struct checkout state = CHECKOUT_INIT; | |
ef2b3693 EN |
4414 | int errs = 0; |
4415 | int original_cache_nr; | |
4416 | ||
66b209b8 | 4417 | if (strmap_empty(&opt->priv->conflicted)) |
9fefce68 EN |
4418 | return 0; |
4419 | ||
a3380639 DS |
4420 | /* |
4421 | * We are in a conflicted state. These conflicts might be inside | |
69576367 DS |
4422 | * sparse-directory entries, so check if any entries are outside |
4423 | * of the sparse-checkout cone preemptively. | |
4424 | * | |
4425 | * We set original_cache_nr below, but that might change if | |
a3380639 DS |
4426 | * index_name_pos() calls ask for paths within sparse directories. |
4427 | */ | |
69576367 DS |
4428 | strmap_for_each_entry(&opt->priv->conflicted, &iter, e) { |
4429 | if (!path_in_sparse_checkout(e->key, index)) { | |
4430 | ensure_full_index(index); | |
4431 | break; | |
4432 | } | |
4433 | } | |
a3380639 | 4434 | |
66b209b8 EN |
4435 | /* If any entries have skip_worktree set, we'll have to check 'em out */ |
4436 | state.force = 1; | |
4437 | state.quiet = 1; | |
4438 | state.refresh_cache = 1; | |
4439 | state.istate = index; | |
ef2b3693 EN |
4440 | original_cache_nr = index->cache_nr; |
4441 | ||
3584cff7 | 4442 | /* Append every entry from conflicted into index, then sort */ |
66b209b8 | 4443 | strmap_for_each_entry(&opt->priv->conflicted, &iter, e) { |
ef2b3693 EN |
4444 | const char *path = e->key; |
4445 | struct conflict_info *ci = e->value; | |
4446 | int pos; | |
4447 | struct cache_entry *ce; | |
4448 | int i; | |
4449 | ||
4450 | VERIFY_CI(ci); | |
4451 | ||
4452 | /* | |
4453 | * The index will already have a stage=0 entry for this path, | |
4454 | * because we created an as-merged-as-possible version of the | |
4455 | * file and checkout() moved the working copy and index over | |
4456 | * to that version. | |
4457 | * | |
4458 | * However, previous iterations through this loop will have | |
4459 | * added unstaged entries to the end of the cache which | |
4460 | * ignore the standard alphabetical ordering of cache | |
4461 | * entries and break invariants needed for index_name_pos() | |
4462 | * to work. However, we know the entry we want is before | |
4463 | * those appended cache entries, so do a temporary swap on | |
4464 | * cache_nr to only look through entries of interest. | |
4465 | */ | |
4466 | SWAP(index->cache_nr, original_cache_nr); | |
4467 | pos = index_name_pos(index, path, strlen(path)); | |
4468 | SWAP(index->cache_nr, original_cache_nr); | |
4469 | if (pos < 0) { | |
4470 | if (ci->filemask != 1) | |
4471 | BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path); | |
4472 | cache_tree_invalidate_path(index, path); | |
4473 | } else { | |
4474 | ce = index->cache[pos]; | |
4475 | ||
4476 | /* | |
4477 | * Clean paths with CE_SKIP_WORKTREE set will not be | |
4478 | * written to the working tree by the unpack_trees() | |
4479 | * call in checkout(). Our conflicted entries would | |
4480 | * have appeared clean to that code since we ignored | |
4481 | * the higher order stages. Thus, we need override | |
4482 | * the CE_SKIP_WORKTREE bit and manually write those | |
4483 | * files to the working disk here. | |
ef2b3693 | 4484 | */ |
ff033db7 | 4485 | if (ce_skip_worktree(ce)) |
66b209b8 | 4486 | errs |= checkout_entry(ce, &state, NULL, NULL); |
ef2b3693 EN |
4487 | |
4488 | /* | |
4489 | * Mark this cache entry for removal and instead add | |
4490 | * new stage>0 entries corresponding to the | |
4491 | * conflicts. If there are many conflicted entries, we | |
4492 | * want to avoid memmove'ing O(NM) entries by | |
4493 | * inserting the new entries one at a time. So, | |
4494 | * instead, we just add the new cache entries to the | |
4495 | * end (ignoring normal index requirements on sort | |
4496 | * order) and sort the index once we're all done. | |
4497 | */ | |
4498 | ce->ce_flags |= CE_REMOVE; | |
4499 | } | |
4500 | ||
4501 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
4502 | struct version_info *vi; | |
4503 | if (!(ci->filemask & (1ul << i))) | |
4504 | continue; | |
4505 | vi = &ci->stages[i]; | |
4506 | ce = make_cache_entry(index, vi->mode, &vi->oid, | |
4507 | path, i+1, 0); | |
4508 | add_index_entry(index, ce, ADD_CACHE_JUST_APPEND); | |
4509 | } | |
4510 | } | |
4511 | ||
4512 | /* | |
4513 | * Remove the unused cache entries (and invalidate the relevant | |
4514 | * cache-trees), then sort the index entries to get the conflicted | |
4515 | * entries we added to the end into their right locations. | |
4516 | */ | |
4517 | remove_marked_cache_entries(index, 1); | |
72b30910 EN |
4518 | /* |
4519 | * No need for STABLE_QSORT -- cmp_cache_name_compare sorts primarily | |
4520 | * on filename and secondarily on stage, and (name, stage #) are a | |
4521 | * unique tuple. | |
4522 | */ | |
ef2b3693 EN |
4523 | QSORT(index->cache, index->cache_nr, cmp_cache_name_compare); |
4524 | ||
4525 | return errs; | |
9fefce68 EN |
4526 | } |
4527 | ||
4057523a CW |
4528 | static void print_submodule_conflict_suggestion(struct string_list *csub) { |
4529 | struct string_list_item *item; | |
4530 | struct strbuf msg = STRBUF_INIT; | |
4531 | struct strbuf tmp = STRBUF_INIT; | |
4532 | struct strbuf subs = STRBUF_INIT; | |
4533 | ||
4534 | if (!csub->nr) | |
4535 | return; | |
4536 | ||
565577ed | 4537 | strbuf_add_separated_string_list(&subs, " ", csub); |
4057523a CW |
4538 | for_each_string_list_item(item, csub) { |
4539 | struct conflicted_submodule_item *util = item->util; | |
4540 | ||
565577ed EN |
4541 | /* |
4542 | * NEEDSWORK: The steps to resolve these errors deserve a more | |
4543 | * detailed explanation than what is currently printed below. | |
4544 | */ | |
4057523a | 4545 | if (util->flag == CONFLICT_SUBMODULE_NOT_INITIALIZED || |
565577ed EN |
4546 | util->flag == CONFLICT_SUBMODULE_HISTORY_NOT_AVAILABLE) |
4547 | continue; | |
a5834b77 | 4548 | |
4057523a | 4549 | /* |
a5834b77 EN |
4550 | * TRANSLATORS: This is a line of advice to resolve a merge |
4551 | * conflict in a submodule. The first argument is the submodule | |
4552 | * name, and the second argument is the abbreviated id of the | |
4553 | * commit that needs to be merged. For example: | |
4554 | * - go to submodule (mysubmodule), and either merge commit abc1234" | |
4057523a | 4555 | */ |
a5834b77 EN |
4556 | strbuf_addf(&tmp, _(" - go to submodule (%s), and either merge commit %s\n" |
4557 | " or update to an existing commit which has merged those changes\n"), | |
4558 | item->string, util->abbrev); | |
4057523a | 4559 | } |
a5834b77 EN |
4560 | |
4561 | /* | |
4562 | * TRANSLATORS: This is a detailed message for resolving submodule | |
4563 | * conflicts. The first argument is string containing one step per | |
4564 | * submodule. The second is a space-separated list of submodule names. | |
4565 | */ | |
4566 | strbuf_addf(&msg, | |
4567 | _("Recursive merging with submodules currently only supports trivial cases.\n" | |
4568 | "Please manually handle the merging of each conflicted submodule.\n" | |
4569 | "This can be accomplished with the following steps:\n" | |
4570 | "%s" | |
4571 | " - come back to superproject and run:\n\n" | |
4572 | " git add %s\n\n" | |
4573 | " to record the above merge or update\n" | |
4574 | " - resolve any other conflicts in the superproject\n" | |
4575 | " - commit the resulting index in the superproject\n"), | |
4576 | tmp.buf, subs.buf); | |
4057523a CW |
4577 | |
4578 | printf("%s", msg.buf); | |
a5834b77 | 4579 | |
4057523a CW |
4580 | strbuf_release(&subs); |
4581 | strbuf_release(&tmp); | |
4582 | strbuf_release(&msg); | |
4583 | } | |
4584 | ||
a34edae6 | 4585 | void merge_display_update_messages(struct merge_options *opt, |
de905811 | 4586 | int detailed, |
a34edae6 EN |
4587 | struct merge_result *result) |
4588 | { | |
4589 | struct merge_options_internal *opti = result->priv; | |
4590 | struct hashmap_iter iter; | |
4591 | struct strmap_entry *e; | |
4592 | struct string_list olist = STRING_LIST_INIT_NODUP; | |
a34edae6 EN |
4593 | |
4594 | if (opt->record_conflict_msgs_as_headers) | |
4595 | BUG("Either display conflict messages or record them as headers, not both"); | |
4596 | ||
4597 | trace2_region_enter("merge", "display messages", opt->repo); | |
4598 | ||
4599 | /* Hack to pre-allocate olist to the desired size */ | |
6debb752 | 4600 | ALLOC_GROW(olist.items, strmap_get_size(&opti->conflicts), |
a34edae6 EN |
4601 | olist.alloc); |
4602 | ||
4603 | /* Put every entry from output into olist, then sort */ | |
6debb752 | 4604 | strmap_for_each_entry(&opti->conflicts, &iter, e) { |
a34edae6 EN |
4605 | string_list_append(&olist, e->key)->util = e->value; |
4606 | } | |
4607 | string_list_sort(&olist); | |
4608 | ||
4609 | /* Iterate over the items, printing them */ | |
6debb752 JS |
4610 | for (int path_nr = 0; path_nr < olist.nr; ++path_nr) { |
4611 | struct string_list *conflicts = olist.items[path_nr].util; | |
de905811 EN |
4612 | for (int i = 0; i < conflicts->nr; i++) { |
4613 | struct logical_conflict_info *info = | |
4614 | conflicts->items[i].util; | |
4615 | ||
4616 | if (detailed) { | |
4617 | printf("%lu", (unsigned long)info->paths.nr); | |
4618 | putchar('\0'); | |
4619 | for (int n = 0; n < info->paths.nr; n++) { | |
4620 | fputs(info->paths.v[n], stdout); | |
4621 | putchar('\0'); | |
4622 | } | |
4623 | fputs(type_short_descriptions[info->type], | |
4624 | stdout); | |
4625 | putchar('\0'); | |
4626 | } | |
6debb752 | 4627 | puts(conflicts->items[i].string); |
de905811 EN |
4628 | if (detailed) |
4629 | putchar('\0'); | |
4630 | } | |
a34edae6 EN |
4631 | } |
4632 | string_list_clear(&olist, 0); | |
4633 | ||
4057523a CW |
4634 | print_submodule_conflict_suggestion(&opti->conflicted_submodules); |
4635 | ||
a34edae6 EN |
4636 | /* Also include needed rename limit adjustment now */ |
4637 | diff_warn_rename_limit("merge.renamelimit", | |
4638 | opti->renames.needed_limit, 0); | |
4639 | ||
4640 | trace2_region_leave("merge", "display messages", opt->repo); | |
4641 | } | |
4642 | ||
fae26ce7 EN |
4643 | void merge_get_conflicted_files(struct merge_result *result, |
4644 | struct string_list *conflicted_files) | |
4645 | { | |
4646 | struct hashmap_iter iter; | |
4647 | struct strmap_entry *e; | |
4648 | struct merge_options_internal *opti = result->priv; | |
4649 | ||
4650 | strmap_for_each_entry(&opti->conflicted, &iter, e) { | |
4651 | const char *path = e->key; | |
4652 | struct conflict_info *ci = e->value; | |
4653 | int i; | |
4654 | ||
4655 | VERIFY_CI(ci); | |
4656 | ||
4657 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
4658 | struct stage_info *si; | |
4659 | ||
4660 | if (!(ci->filemask & (1ul << i))) | |
4661 | continue; | |
4662 | ||
4663 | si = xmalloc(sizeof(*si)); | |
4664 | si->stage = i+1; | |
4665 | si->mode = ci->stages[i].mode; | |
4666 | oidcpy(&si->oid, &ci->stages[i].oid); | |
4667 | string_list_append(conflicted_files, path)->util = si; | |
4668 | } | |
4669 | } | |
4670 | /* string_list_sort() uses a stable sort, so we're good */ | |
4671 | string_list_sort(conflicted_files); | |
4672 | } | |
4673 | ||
17e5574b EN |
4674 | void merge_switch_to_result(struct merge_options *opt, |
4675 | struct tree *head, | |
4676 | struct merge_result *result, | |
4677 | int update_worktree_and_index, | |
4678 | int display_update_msgs) | |
4679 | { | |
9fefce68 EN |
4680 | assert(opt->priv == NULL); |
4681 | if (result->clean >= 0 && update_worktree_and_index) { | |
5291828d EN |
4682 | const char *filename; |
4683 | FILE *fp; | |
9fefce68 | 4684 | |
557ac035 | 4685 | trace2_region_enter("merge", "checkout", opt->repo); |
9fefce68 EN |
4686 | if (checkout(opt, head, result->tree)) { |
4687 | /* failure to function */ | |
4688 | result->clean = -1; | |
fef2b6da | 4689 | merge_finalize(opt, result); |
1250dff3 | 4690 | trace2_region_leave("merge", "checkout", opt->repo); |
9fefce68 EN |
4691 | return; |
4692 | } | |
557ac035 | 4693 | trace2_region_leave("merge", "checkout", opt->repo); |
9fefce68 | 4694 | |
557ac035 | 4695 | trace2_region_enter("merge", "record_conflicted", opt->repo); |
66b209b8 EN |
4696 | opt->priv = result->priv; |
4697 | if (record_conflicted_index_entries(opt)) { | |
9fefce68 | 4698 | /* failure to function */ |
66b209b8 | 4699 | opt->priv = NULL; |
9fefce68 | 4700 | result->clean = -1; |
fef2b6da | 4701 | merge_finalize(opt, result); |
1250dff3 JS |
4702 | trace2_region_leave("merge", "record_conflicted", |
4703 | opt->repo); | |
9fefce68 EN |
4704 | return; |
4705 | } | |
66b209b8 | 4706 | opt->priv = NULL; |
557ac035 | 4707 | trace2_region_leave("merge", "record_conflicted", opt->repo); |
5291828d EN |
4708 | |
4709 | trace2_region_enter("merge", "write_auto_merge", opt->repo); | |
4710 | filename = git_path_auto_merge(opt->repo); | |
4711 | fp = xfopen(filename, "w"); | |
4712 | fprintf(fp, "%s\n", oid_to_hex(&result->tree->object.oid)); | |
4713 | fclose(fp); | |
4714 | trace2_region_leave("merge", "write_auto_merge", opt->repo); | |
9fefce68 | 4715 | } |
a34edae6 | 4716 | if (display_update_msgs) |
de905811 | 4717 | merge_display_update_messages(opt, /* detailed */ 0, result); |
9fefce68 | 4718 | |
17e5574b EN |
4719 | merge_finalize(opt, result); |
4720 | } | |
4721 | ||
4722 | void merge_finalize(struct merge_options *opt, | |
4723 | struct merge_result *result) | |
4724 | { | |
89422d29 EN |
4725 | struct merge_options_internal *opti = result->priv; |
4726 | ||
ea305a68 EN |
4727 | if (opt->renormalize) |
4728 | git_attr_set_direction(GIT_ATTR_CHECKIN); | |
89422d29 EN |
4729 | assert(opt->priv == NULL); |
4730 | ||
43e9c4ee | 4731 | clear_or_reinit_internal_opts(opti, 0); |
89422d29 | 4732 | FREE_AND_NULL(opti); |
17e5574b EN |
4733 | } |
4734 | ||
04af1879 EN |
4735 | /*** Function Grouping: helper functions for merge_incore_*() ***/ |
4736 | ||
3639dfb3 EN |
4737 | static struct tree *shift_tree_object(struct repository *repo, |
4738 | struct tree *one, struct tree *two, | |
4739 | const char *subtree_shift) | |
4740 | { | |
4741 | struct object_id shifted; | |
4742 | ||
4743 | if (!*subtree_shift) { | |
4744 | shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0); | |
4745 | } else { | |
4746 | shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted, | |
4747 | subtree_shift); | |
4748 | } | |
4749 | if (oideq(&two->object.oid, &shifted)) | |
4750 | return two; | |
4751 | return lookup_tree(repo, &shifted); | |
4752 | } | |
4753 | ||
4296d8f1 EN |
4754 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
4755 | { | |
4756 | c->maybe_tree = t; | |
4757 | } | |
4758 | ||
4296d8f1 EN |
4759 | static struct commit *make_virtual_commit(struct repository *repo, |
4760 | struct tree *tree, | |
4761 | const char *comment) | |
4762 | { | |
4763 | struct commit *commit = alloc_commit_node(repo); | |
4764 | ||
4765 | set_merge_remote_desc(commit, comment, (struct object *)commit); | |
4766 | set_commit_tree(commit, tree); | |
4767 | commit->object.parsed = 1; | |
4768 | return commit; | |
4769 | } | |
4770 | ||
231e2dd4 EN |
4771 | static void merge_start(struct merge_options *opt, struct merge_result *result) |
4772 | { | |
f5d9fbc2 EN |
4773 | struct rename_info *renames; |
4774 | int i; | |
6697ee01 | 4775 | struct mem_pool *pool = NULL; |
f5d9fbc2 | 4776 | |
e4171b1b | 4777 | /* Sanity checks on opt */ |
557ac035 | 4778 | trace2_region_enter("merge", "sanity checks", opt->repo); |
e4171b1b EN |
4779 | assert(opt->repo); |
4780 | ||
4781 | assert(opt->branch1 && opt->branch2); | |
4782 | ||
4783 | assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE && | |
4784 | opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE); | |
4785 | assert(opt->rename_limit >= -1); | |
4786 | assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE); | |
4787 | assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1); | |
4788 | ||
4789 | assert(opt->xdl_opts >= 0); | |
4790 | assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL && | |
4791 | opt->recursive_variant <= MERGE_VARIANT_THEIRS); | |
4792 | ||
6054d1aa EN |
4793 | if (opt->msg_header_prefix) |
4794 | assert(opt->record_conflict_msgs_as_headers); | |
4795 | ||
e4171b1b EN |
4796 | /* |
4797 | * detect_renames, verbosity, buffer_output, and obuf are ignored | |
4798 | * fields that were used by "recursive" rather than "ort" -- but | |
4799 | * sanity check them anyway. | |
4800 | */ | |
4801 | assert(opt->detect_renames >= -1 && | |
4802 | opt->detect_renames <= DIFF_DETECT_COPY); | |
4803 | assert(opt->verbosity >= 0 && opt->verbosity <= 5); | |
4804 | assert(opt->buffer_output <= 2); | |
4805 | assert(opt->obuf.len == 0); | |
4806 | ||
4807 | assert(opt->priv == NULL); | |
19ceb486 EN |
4808 | if (result->_properly_initialized != 0 && |
4809 | result->_properly_initialized != RESULT_INITIALIZED) | |
4810 | BUG("struct merge_result passed to merge_incore_*recursive() must be zeroed or filled with values from a previous run"); | |
4811 | assert(!!result->priv == !!result->_properly_initialized); | |
cf8937ac EN |
4812 | if (result->priv) { |
4813 | opt->priv = result->priv; | |
4814 | result->priv = NULL; | |
4815 | /* | |
4816 | * opt->priv non-NULL means we had results from a previous | |
4817 | * run; do a few sanity checks that user didn't mess with | |
4818 | * it in an obvious fashion. | |
4819 | */ | |
4820 | assert(opt->priv->call_depth == 0); | |
4821 | assert(!opt->priv->toplevel_dir || | |
4822 | 0 == strlen(opt->priv->toplevel_dir)); | |
4823 | } | |
557ac035 | 4824 | trace2_region_leave("merge", "sanity checks", opt->repo); |
e4171b1b | 4825 | |
c8017176 EN |
4826 | /* Default to histogram diff. Actually, just hardcode it...for now. */ |
4827 | opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF); | |
4828 | ||
ea305a68 EN |
4829 | /* Handle attr direction stuff for renormalization */ |
4830 | if (opt->renormalize) | |
4831 | git_attr_set_direction(GIT_ATTR_CHECKOUT); | |
4832 | ||
e4171b1b | 4833 | /* Initialization of opt->priv, our internal merge data */ |
557ac035 | 4834 | trace2_region_enter("merge", "allocate/init", opt->repo); |
cf8937ac EN |
4835 | if (opt->priv) { |
4836 | clear_or_reinit_internal_opts(opt->priv, 1); | |
4057523a | 4837 | string_list_init_nodup(&opt->priv->conflicted_submodules); |
cf8937ac EN |
4838 | trace2_region_leave("merge", "allocate/init", opt->repo); |
4839 | return; | |
4840 | } | |
e4171b1b EN |
4841 | opt->priv = xcalloc(1, sizeof(*opt->priv)); |
4842 | ||
f5d9fbc2 EN |
4843 | /* Initialization of various renames fields */ |
4844 | renames = &opt->priv->renames; | |
62a15162 EN |
4845 | mem_pool_init(&opt->priv->pool, 0); |
4846 | pool = &opt->priv->pool; | |
f5d9fbc2 | 4847 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) { |
a49b55d5 | 4848 | strintmap_init_with_options(&renames->dirs_removed[i], |
6697ee01 | 4849 | NOT_RELEVANT, pool, 0); |
f5d9fbc2 EN |
4850 | strmap_init_with_options(&renames->dir_rename_count[i], |
4851 | NULL, 1); | |
4852 | strmap_init_with_options(&renames->dir_renames[i], | |
4853 | NULL, 0); | |
2734f2e3 EN |
4854 | /* |
4855 | * relevant_sources uses -1 for the default, because we need | |
4856 | * to be able to distinguish not-in-strintmap from valid | |
4857 | * relevant_source values from enum file_rename_relevance. | |
4858 | * In particular, possibly_cache_new_pair() expects a negative | |
4859 | * value for not-found entries. | |
4860 | */ | |
a49b55d5 | 4861 | strintmap_init_with_options(&renames->relevant_sources[i], |
2734f2e3 | 4862 | -1 /* explicitly invalid */, |
6697ee01 | 4863 | pool, 0); |
d29bd6d7 EN |
4864 | strmap_init_with_options(&renames->cached_pairs[i], |
4865 | NULL, 1); | |
4866 | strset_init_with_options(&renames->cached_irrelevant[i], | |
4867 | NULL, 1); | |
4868 | strset_init_with_options(&renames->cached_target_names[i], | |
4869 | NULL, 0); | |
f5d9fbc2 | 4870 | } |
d478f567 EN |
4871 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) { |
4872 | strintmap_init_with_options(&renames->deferred[i].possible_trivial_merges, | |
6697ee01 | 4873 | 0, pool, 0); |
d478f567 | 4874 | strset_init_with_options(&renames->deferred[i].target_dirs, |
6697ee01 | 4875 | pool, 1); |
d478f567 EN |
4876 | renames->deferred[i].trivial_merges_okay = 1; /* 1 == maybe */ |
4877 | } | |
f5d9fbc2 | 4878 | |
e4171b1b EN |
4879 | /* |
4880 | * Although we initialize opt->priv->paths with strdup_strings=0, | |
4881 | * that's just to avoid making yet another copy of an allocated | |
4882 | * string. Putting the entry into paths means we are taking | |
62a15162 | 4883 | * ownership, so we will later free it. |
e4171b1b EN |
4884 | * |
4885 | * In contrast, conflicted just has a subset of keys from paths, so | |
4886 | * we don't want to free those (it'd be a duplicate free). | |
4887 | */ | |
6697ee01 EN |
4888 | strmap_init_with_options(&opt->priv->paths, pool, 0); |
4889 | strmap_init_with_options(&opt->priv->conflicted, pool, 0); | |
c5a6f655 EN |
4890 | |
4891 | /* | |
6debb752 JS |
4892 | * keys & string_lists in conflicts will sometimes need to outlive |
4893 | * "paths", so it will have a copy of relevant keys. It's probably | |
4894 | * a small subset of the overall paths that have special output. | |
c5a6f655 | 4895 | */ |
6debb752 | 4896 | strmap_init(&opt->priv->conflicts); |
557ac035 EN |
4897 | |
4898 | trace2_region_leave("merge", "allocate/init", opt->repo); | |
231e2dd4 EN |
4899 | } |
4900 | ||
64aceb6d EN |
4901 | static void merge_check_renames_reusable(struct merge_options *opt, |
4902 | struct merge_result *result, | |
4903 | struct tree *merge_base, | |
4904 | struct tree *side1, | |
4905 | struct tree *side2) | |
4906 | { | |
4907 | struct rename_info *renames; | |
4908 | struct tree **merge_trees; | |
4909 | struct merge_options_internal *opti = result->priv; | |
4910 | ||
4911 | if (!opti) | |
4912 | return; | |
4913 | ||
4914 | renames = &opti->renames; | |
4915 | merge_trees = renames->merge_trees; | |
cbdca289 EN |
4916 | |
4917 | /* | |
4918 | * Handle case where previous merge operation did not want cache to | |
4919 | * take effect, e.g. because rename/rename(1to1) makes it invalid. | |
4920 | */ | |
4921 | if (!merge_trees[0]) { | |
4922 | assert(!merge_trees[0] && !merge_trees[1] && !merge_trees[2]); | |
4923 | renames->cached_pairs_valid_side = 0; /* neither side valid */ | |
4924 | return; | |
4925 | } | |
4926 | ||
4927 | /* | |
4928 | * Handle other cases; note that merge_trees[0..2] will only | |
4929 | * be NULL if opti is, or if all three were manually set to | |
4930 | * NULL by e.g. rename/rename(1to1) handling. | |
4931 | */ | |
64aceb6d EN |
4932 | assert(merge_trees[0] && merge_trees[1] && merge_trees[2]); |
4933 | ||
4934 | /* Check if we meet a condition for re-using cached_pairs */ | |
4935 | if (oideq(&merge_base->object.oid, &merge_trees[2]->object.oid) && | |
4936 | oideq(&side1->object.oid, &result->tree->object.oid)) | |
4937 | renames->cached_pairs_valid_side = MERGE_SIDE1; | |
4938 | else if (oideq(&merge_base->object.oid, &merge_trees[1]->object.oid) && | |
4939 | oideq(&side2->object.oid, &result->tree->object.oid)) | |
4940 | renames->cached_pairs_valid_side = MERGE_SIDE2; | |
4941 | else | |
4942 | renames->cached_pairs_valid_side = 0; /* neither side valid */ | |
4943 | } | |
4944 | ||
04af1879 EN |
4945 | /*** Function Grouping: merge_incore_*() and their internal variants ***/ |
4946 | ||
231e2dd4 EN |
4947 | /* |
4948 | * Originally from merge_trees_internal(); heavily adapted, though. | |
4949 | */ | |
4950 | static void merge_ort_nonrecursive_internal(struct merge_options *opt, | |
4951 | struct tree *merge_base, | |
4952 | struct tree *side1, | |
4953 | struct tree *side2, | |
4954 | struct merge_result *result) | |
4955 | { | |
4956 | struct object_id working_tree_oid; | |
4957 | ||
3639dfb3 EN |
4958 | if (opt->subtree_shift) { |
4959 | side2 = shift_tree_object(opt->repo, side1, side2, | |
4960 | opt->subtree_shift); | |
4961 | merge_base = shift_tree_object(opt->repo, side1, merge_base, | |
4962 | opt->subtree_shift); | |
4963 | } | |
4964 | ||
8b09a900 | 4965 | redo: |
557ac035 | 4966 | trace2_region_enter("merge", "collect_merge_info", opt->repo); |
0c0d705b EN |
4967 | if (collect_merge_info(opt, merge_base, side1, side2) != 0) { |
4968 | /* | |
4969 | * TRANSLATORS: The %s arguments are: 1) tree hash of a merge | |
4970 | * base, and 2-3) the trees for the two trees we're merging. | |
4971 | */ | |
4972 | err(opt, _("collecting merge info failed for trees %s, %s, %s"), | |
4973 | oid_to_hex(&merge_base->object.oid), | |
4974 | oid_to_hex(&side1->object.oid), | |
4975 | oid_to_hex(&side2->object.oid)); | |
4976 | result->clean = -1; | |
4977 | return; | |
4978 | } | |
557ac035 | 4979 | trace2_region_leave("merge", "collect_merge_info", opt->repo); |
0c0d705b | 4980 | |
557ac035 | 4981 | trace2_region_enter("merge", "renames", opt->repo); |
231e2dd4 EN |
4982 | result->clean = detect_and_process_renames(opt, merge_base, |
4983 | side1, side2); | |
557ac035 | 4984 | trace2_region_leave("merge", "renames", opt->repo); |
8b09a900 EN |
4985 | if (opt->priv->renames.redo_after_renames == 2) { |
4986 | trace2_region_enter("merge", "reset_maps", opt->repo); | |
4987 | clear_or_reinit_internal_opts(opt->priv, 1); | |
4988 | trace2_region_leave("merge", "reset_maps", opt->repo); | |
4989 | goto redo; | |
4990 | } | |
557ac035 EN |
4991 | |
4992 | trace2_region_enter("merge", "process_entries", opt->repo); | |
0b55d930 JS |
4993 | if (process_entries(opt, &working_tree_oid) < 0) |
4994 | result->clean = -1; | |
557ac035 | 4995 | trace2_region_leave("merge", "process_entries", opt->repo); |
231e2dd4 EN |
4996 | |
4997 | /* Set return values */ | |
2715e8a9 | 4998 | result->path_messages = &opt->priv->conflicts; |
6debb752 | 4999 | |
0b55d930 JS |
5000 | if (result->clean >= 0) { |
5001 | result->tree = parse_tree_indirect(&working_tree_oid); | |
5002 | /* existence of conflicted entries implies unclean */ | |
5003 | result->clean &= strmap_empty(&opt->priv->conflicted); | |
5004 | } | |
231e2dd4 EN |
5005 | if (!opt->priv->call_depth) { |
5006 | result->priv = opt->priv; | |
19ceb486 | 5007 | result->_properly_initialized = RESULT_INITIALIZED; |
231e2dd4 EN |
5008 | opt->priv = NULL; |
5009 | } | |
5010 | } | |
5011 | ||
8119214f EN |
5012 | /* |
5013 | * Originally from merge_recursive_internal(); somewhat adapted, though. | |
5014 | */ | |
5015 | static void merge_ort_internal(struct merge_options *opt, | |
5016 | struct commit_list *merge_bases, | |
5017 | struct commit *h1, | |
5018 | struct commit *h2, | |
5019 | struct merge_result *result) | |
5020 | { | |
a59b8dd9 | 5021 | struct commit *next; |
8119214f EN |
5022 | struct commit *merged_merge_bases; |
5023 | const char *ancestor_name; | |
5024 | struct strbuf merge_base_abbrev = STRBUF_INIT; | |
5025 | ||
5026 | if (!merge_bases) { | |
cb338c23 | 5027 | merge_bases = repo_get_merge_bases(the_repository, h1, h2); |
8119214f EN |
5028 | /* See merge-ort.h:merge_incore_recursive() declaration NOTE */ |
5029 | merge_bases = reverse_commit_list(merge_bases); | |
5030 | } | |
5031 | ||
5032 | merged_merge_bases = pop_commit(&merge_bases); | |
afe8a907 | 5033 | if (!merged_merge_bases) { |
8119214f EN |
5034 | /* if there is no common ancestor, use an empty tree */ |
5035 | struct tree *tree; | |
5036 | ||
5037 | tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree); | |
5038 | merged_merge_bases = make_virtual_commit(opt->repo, tree, | |
5039 | "ancestor"); | |
5040 | ancestor_name = "empty tree"; | |
5041 | } else if (merge_bases) { | |
5042 | ancestor_name = "merged common ancestors"; | |
5043 | } else { | |
5044 | strbuf_add_unique_abbrev(&merge_base_abbrev, | |
5045 | &merged_merge_bases->object.oid, | |
5046 | DEFAULT_ABBREV); | |
5047 | ancestor_name = merge_base_abbrev.buf; | |
5048 | } | |
5049 | ||
a59b8dd9 EN |
5050 | for (next = pop_commit(&merge_bases); next; |
5051 | next = pop_commit(&merge_bases)) { | |
8119214f EN |
5052 | const char *saved_b1, *saved_b2; |
5053 | struct commit *prev = merged_merge_bases; | |
5054 | ||
5055 | opt->priv->call_depth++; | |
5056 | /* | |
5057 | * When the merge fails, the result contains files | |
5058 | * with conflict markers. The cleanness flag is | |
5059 | * ignored (unless indicating an error), it was never | |
5060 | * actually used, as result of merge_trees has always | |
5061 | * overwritten it: the committed "conflicts" were | |
5062 | * already resolved. | |
5063 | */ | |
5064 | saved_b1 = opt->branch1; | |
5065 | saved_b2 = opt->branch2; | |
5066 | opt->branch1 = "Temporary merge branch 1"; | |
5067 | opt->branch2 = "Temporary merge branch 2"; | |
a59b8dd9 | 5068 | merge_ort_internal(opt, NULL, prev, next, result); |
8119214f EN |
5069 | if (result->clean < 0) |
5070 | return; | |
5071 | opt->branch1 = saved_b1; | |
5072 | opt->branch2 = saved_b2; | |
5073 | opt->priv->call_depth--; | |
5074 | ||
5075 | merged_merge_bases = make_virtual_commit(opt->repo, | |
5076 | result->tree, | |
5077 | "merged tree"); | |
5078 | commit_list_insert(prev, &merged_merge_bases->parents); | |
a59b8dd9 | 5079 | commit_list_insert(next, &merged_merge_bases->parents->next); |
8119214f EN |
5080 | |
5081 | clear_or_reinit_internal_opts(opt->priv, 1); | |
5082 | } | |
5083 | ||
5084 | opt->ancestor = ancestor_name; | |
5085 | merge_ort_nonrecursive_internal(opt, | |
5086 | repo_get_commit_tree(opt->repo, | |
5087 | merged_merge_bases), | |
5088 | repo_get_commit_tree(opt->repo, h1), | |
5089 | repo_get_commit_tree(opt->repo, h2), | |
5090 | result); | |
5091 | strbuf_release(&merge_base_abbrev); | |
5092 | opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */ | |
5093 | } | |
5094 | ||
17e5574b EN |
5095 | void merge_incore_nonrecursive(struct merge_options *opt, |
5096 | struct tree *merge_base, | |
5097 | struct tree *side1, | |
5098 | struct tree *side2, | |
5099 | struct merge_result *result) | |
5100 | { | |
557ac035 EN |
5101 | trace2_region_enter("merge", "incore_nonrecursive", opt->repo); |
5102 | ||
5103 | trace2_region_enter("merge", "merge_start", opt->repo); | |
231e2dd4 | 5104 | assert(opt->ancestor != NULL); |
64aceb6d | 5105 | merge_check_renames_reusable(opt, result, merge_base, side1, side2); |
231e2dd4 | 5106 | merge_start(opt, result); |
64aceb6d EN |
5107 | /* |
5108 | * Record the trees used in this merge, so if there's a next merge in | |
5109 | * a cherry-pick or rebase sequence it might be able to take advantage | |
5110 | * of the cached_pairs in that next merge. | |
5111 | */ | |
5112 | opt->priv->renames.merge_trees[0] = merge_base; | |
5113 | opt->priv->renames.merge_trees[1] = side1; | |
5114 | opt->priv->renames.merge_trees[2] = side2; | |
557ac035 EN |
5115 | trace2_region_leave("merge", "merge_start", opt->repo); |
5116 | ||
231e2dd4 | 5117 | merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result); |
557ac035 | 5118 | trace2_region_leave("merge", "incore_nonrecursive", opt->repo); |
17e5574b EN |
5119 | } |
5120 | ||
5121 | void merge_incore_recursive(struct merge_options *opt, | |
5122 | struct commit_list *merge_bases, | |
5123 | struct commit *side1, | |
5124 | struct commit *side2, | |
5125 | struct merge_result *result) | |
5126 | { | |
557ac035 EN |
5127 | trace2_region_enter("merge", "incore_recursive", opt->repo); |
5128 | ||
8119214f EN |
5129 | /* We set the ancestor label based on the merge_bases */ |
5130 | assert(opt->ancestor == NULL); | |
5131 | ||
557ac035 | 5132 | trace2_region_enter("merge", "merge_start", opt->repo); |
8119214f | 5133 | merge_start(opt, result); |
557ac035 EN |
5134 | trace2_region_leave("merge", "merge_start", opt->repo); |
5135 | ||
8119214f | 5136 | merge_ort_internal(opt, merge_bases, side1, side2, result); |
557ac035 | 5137 | trace2_region_leave("merge", "incore_recursive", opt->repo); |
17e5574b | 5138 | } |