]>
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" |
f591c472 | 29 | #include "ll-merge.h" |
ee4012dc | 30 | #include "object-store.h" |
4204cd59 | 31 | #include "revision.h" |
5b59c3db | 32 | #include "strmap.h" |
c73cda76 | 33 | #include "submodule.h" |
231e2dd4 | 34 | #include "tree.h" |
6681ce5c | 35 | #include "unpack-trees.h" |
c8017176 | 36 | #include "xdiff-interface.h" |
5b59c3db | 37 | |
d2bc1994 EN |
38 | /* |
39 | * We have many arrays of size 3. Whenever we have such an array, the | |
40 | * indices refer to one of the sides of the three-way merge. This is so | |
41 | * pervasive that the constants 0, 1, and 2 are used in many places in the | |
42 | * code (especially in arithmetic operations to find the other side's index | |
43 | * or to compute a relevant mask), but sometimes these enum names are used | |
44 | * to aid code clarity. | |
45 | * | |
46 | * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side" | |
47 | * referred to there is one of these three sides. | |
48 | */ | |
49 | enum merge_side { | |
50 | MERGE_BASE = 0, | |
51 | MERGE_SIDE1 = 1, | |
52 | MERGE_SIDE2 = 2 | |
53 | }; | |
54 | ||
beb06145 EN |
55 | struct traversal_callback_data { |
56 | unsigned long mask; | |
57 | unsigned long dirmask; | |
58 | struct name_entry names[3]; | |
59 | }; | |
60 | ||
864075ec | 61 | struct rename_info { |
c09376d5 EN |
62 | /* |
63 | * All variables that are arrays of size 3 correspond to data tracked | |
64 | * for the sides in enum merge_side. Index 0 is almost always unused | |
65 | * because we often only need to track information for MERGE_SIDE1 and | |
66 | * MERGE_SIDE2 (MERGE_BASE can't have rename information since renames | |
67 | * are determined relative to what changed since the MERGE_BASE). | |
68 | */ | |
69 | ||
864075ec EN |
70 | /* |
71 | * pairs: pairing of filenames from diffcore_rename() | |
864075ec EN |
72 | */ |
73 | struct diff_queue_struct pairs[3]; | |
74 | ||
c09376d5 EN |
75 | /* |
76 | * dirs_removed: directories removed on a given side of history. | |
fb52938e EN |
77 | * |
78 | * The keys of dirs_removed[side] are the directories that were removed | |
79 | * on the given side of history. The value of the strintmap for each | |
80 | * directory is a value from enum dir_rename_relevance. | |
c09376d5 | 81 | */ |
a49b55d5 | 82 | struct strintmap dirs_removed[3]; |
c09376d5 EN |
83 | |
84 | /* | |
85 | * dir_rename_count: tracking where parts of a directory were renamed to | |
86 | * | |
87 | * When files in a directory are renamed, they may not all go to the | |
88 | * same location. Each strmap here tracks: | |
89 | * old_dir => {new_dir => int} | |
90 | * That is, dir_rename_count[side] is a strmap to a strintmap. | |
91 | */ | |
92 | struct strmap dir_rename_count[3]; | |
93 | ||
94 | /* | |
95 | * dir_renames: computed directory renames | |
96 | * | |
97 | * This is a map of old_dir => new_dir and is derived in part from | |
98 | * dir_rename_count. | |
99 | */ | |
100 | struct strmap dir_renames[3]; | |
101 | ||
32a56dfb | 102 | /* |
ec59da60 | 103 | * relevant_sources: deleted paths wanted in rename detection, and why |
32a56dfb EN |
104 | * |
105 | * relevant_sources is a set of deleted paths on each side of | |
106 | * history for which we need rename detection. If a path is deleted | |
107 | * on one side of history, we need to detect if it is part of a | |
108 | * rename if either | |
32a56dfb | 109 | * * the file is modified/deleted on the other side of history |
ec59da60 | 110 | * * we need to detect renames for an ancestor directory |
32a56dfb | 111 | * If neither of those are true, we can skip rename detection for |
ec59da60 EN |
112 | * that path. The reason is stored as a value from enum |
113 | * file_rename_relevance, as the reason can inform the algorithm in | |
114 | * diffcore_rename_extended(). | |
32a56dfb | 115 | */ |
a49b55d5 | 116 | struct strintmap relevant_sources[3]; |
32a56dfb | 117 | |
2fd9eda4 EN |
118 | /* |
119 | * dir_rename_mask: | |
120 | * 0: optimization removing unmodified potential rename source okay | |
121 | * 2 or 4: optimization okay, but must check for files added to dir | |
122 | * 7: optimization forbidden; need rename source in case of dir rename | |
123 | */ | |
124 | unsigned dir_rename_mask:3; | |
125 | ||
beb06145 EN |
126 | /* |
127 | * callback_data_*: supporting data structures for alternate traversal | |
128 | * | |
129 | * We sometimes need to be able to traverse through all the files | |
130 | * in a given tree before all immediate subdirectories within that | |
131 | * tree. Since traverse_trees() doesn't do that naturally, we have | |
132 | * a traverse_trees_wrapper() that stores any immediate | |
133 | * subdirectories while traversing files, then traverses the | |
134 | * immediate subdirectories later. These callback_data* variables | |
135 | * store the information for the subdirectories so that we can do | |
136 | * that traversal order. | |
137 | */ | |
138 | struct traversal_callback_data *callback_data; | |
139 | int callback_data_nr, callback_data_alloc; | |
140 | char *callback_data_traverse_path; | |
141 | ||
d29bd6d7 EN |
142 | /* |
143 | * cached_pairs: Caching of renames and deletions. | |
144 | * | |
145 | * These are mappings recording renames and deletions of individual | |
146 | * files (not directories). They are thus a map from an old | |
147 | * filename to either NULL (for deletions) or a new filename (for | |
148 | * renames). | |
149 | */ | |
150 | struct strmap cached_pairs[3]; | |
151 | ||
152 | /* | |
153 | * cached_target_names: just the destinations from cached_pairs | |
154 | * | |
155 | * We sometimes want a fast lookup to determine if a given filename | |
156 | * is one of the destinations in cached_pairs. cached_target_names | |
157 | * is thus duplicative information, but it provides a fast lookup. | |
158 | */ | |
159 | struct strset cached_target_names[3]; | |
160 | ||
161 | /* | |
162 | * cached_irrelevant: Caching of rename_sources that aren't relevant. | |
163 | * | |
164 | * If we try to detect a rename for a source path and succeed, it's | |
165 | * part of a rename. If we try to detect a rename for a source path | |
166 | * and fail, then it's a delete. If we do not try to detect a rename | |
167 | * for a path, then we don't know if it's a rename or a delete. If | |
168 | * merge-ort doesn't think the path is relevant, then we just won't | |
169 | * cache anything for that path. But there's a slight problem in | |
170 | * that merge-ort can think a path is RELEVANT_LOCATION, but due to | |
171 | * commit 9bd342137e ("diffcore-rename: determine which | |
172 | * relevant_sources are no longer relevant", 2021-03-13), | |
173 | * diffcore-rename can downgrade the path to RELEVANT_NO_MORE. To | |
174 | * avoid excessive calls to diffcore_rename_extended() we still need | |
175 | * to cache such paths, though we cannot record them as either | |
176 | * renames or deletes. So we cache them here as a "turned out to be | |
177 | * irrelevant *for this commit*" as they are often also irrelevant | |
178 | * for subsequent commits, though we will have to do some extra | |
179 | * checking to see whether such paths become relevant for rename | |
180 | * detection when cherry-picking/rebasing subsequent commits. | |
181 | */ | |
182 | struct strset cached_irrelevant[3]; | |
183 | ||
864075ec EN |
184 | /* |
185 | * needed_limit: value needed for inexact rename detection to run | |
186 | * | |
187 | * If the current rename limit wasn't high enough for inexact | |
188 | * rename detection to run, this records the limit needed. Otherwise, | |
189 | * this value remains 0. | |
190 | */ | |
191 | int needed_limit; | |
192 | }; | |
193 | ||
5b59c3db EN |
194 | struct merge_options_internal { |
195 | /* | |
196 | * paths: primary data structure in all of merge ort. | |
197 | * | |
198 | * The keys of paths: | |
199 | * * are full relative paths from the toplevel of the repository | |
200 | * (e.g. "drivers/firmware/raspberrypi.c"). | |
201 | * * store all relevant paths in the repo, both directories and | |
202 | * files (e.g. drivers, drivers/firmware would also be included) | |
203 | * * these keys serve to intern all the path strings, which allows | |
204 | * us to do pointer comparison on directory names instead of | |
205 | * strcmp; we just have to be careful to use the interned strings. | |
43c1dccb EN |
206 | * (Technically paths_to_free may track some strings that were |
207 | * removed from froms paths.) | |
5b59c3db EN |
208 | * |
209 | * The values of paths: | |
210 | * * either a pointer to a merged_info, or a conflict_info struct | |
211 | * * merged_info contains all relevant information for a | |
212 | * non-conflicted entry. | |
213 | * * conflict_info contains a merged_info, plus any additional | |
214 | * information about a conflict such as the higher orders stages | |
215 | * involved and the names of the paths those came from (handy | |
216 | * once renames get involved). | |
217 | * * a path may start "conflicted" (i.e. point to a conflict_info) | |
218 | * and then a later step (e.g. three-way content merge) determines | |
219 | * it can be cleanly merged, at which point it'll be marked clean | |
220 | * and the algorithm will ignore any data outside the contained | |
221 | * merged_info for that entry | |
222 | * * If an entry remains conflicted, the merged_info portion of a | |
223 | * conflict_info will later be filled with whatever version of | |
224 | * the file should be placed in the working directory (e.g. an | |
225 | * as-merged-as-possible variation that contains conflict markers). | |
226 | */ | |
227 | struct strmap paths; | |
228 | ||
229 | /* | |
230 | * conflicted: a subset of keys->values from "paths" | |
231 | * | |
232 | * conflicted is basically an optimization between process_entries() | |
233 | * and record_conflicted_index_entries(); the latter could loop over | |
234 | * ALL the entries in paths AGAIN and look for the ones that are | |
235 | * still conflicted, but since process_entries() has to loop over | |
236 | * all of them, it saves the ones it couldn't resolve in this strmap | |
237 | * so that record_conflicted_index_entries() can iterate just the | |
238 | * relevant entries. | |
239 | */ | |
240 | struct strmap conflicted; | |
241 | ||
43c1dccb EN |
242 | /* |
243 | * paths_to_free: additional list of strings to free | |
244 | * | |
245 | * If keys are removed from "paths", they are added to paths_to_free | |
246 | * to ensure they are later freed. We avoid free'ing immediately since | |
247 | * other places (e.g. conflict_info.pathnames[]) may still be | |
248 | * referencing these paths. | |
249 | */ | |
250 | struct string_list paths_to_free; | |
251 | ||
c5a6f655 EN |
252 | /* |
253 | * output: special messages and conflict notices for various paths | |
254 | * | |
255 | * This is a map of pathnames (a subset of the keys in "paths" above) | |
256 | * to strbufs. It gathers various warning/conflict/notice messages | |
257 | * for later processing. | |
258 | */ | |
259 | struct strmap output; | |
260 | ||
5b59c3db | 261 | /* |
864075ec EN |
262 | * renames: various data relating to rename detection |
263 | */ | |
264 | struct rename_info renames; | |
265 | ||
ea305a68 EN |
266 | /* |
267 | * attr_index: hacky minimal index used for renormalization | |
268 | * | |
269 | * renormalization code _requires_ an index, though it only needs to | |
270 | * find a .gitattributes file within the index. So, when | |
271 | * renormalization is important, we create a special index with just | |
272 | * that one file. | |
273 | */ | |
274 | struct index_state attr_index; | |
275 | ||
5b59c3db | 276 | /* |
05b85c6e | 277 | * current_dir_name, toplevel_dir: temporary vars |
5b59c3db | 278 | * |
05b85c6e EN |
279 | * These are used in collect_merge_info_callback(), and will set the |
280 | * various merged_info.directory_name for the various paths we get; | |
281 | * see documentation for that variable and the requirements placed on | |
282 | * that field. | |
5b59c3db EN |
283 | */ |
284 | const char *current_dir_name; | |
05b85c6e | 285 | const char *toplevel_dir; |
5b59c3db EN |
286 | |
287 | /* call_depth: recursion level counter for merging merge bases */ | |
288 | int call_depth; | |
289 | }; | |
290 | ||
291 | struct version_info { | |
292 | struct object_id oid; | |
293 | unsigned short mode; | |
294 | }; | |
295 | ||
296 | struct merged_info { | |
297 | /* if is_null, ignore result. otherwise result has oid & mode */ | |
298 | struct version_info result; | |
299 | unsigned is_null:1; | |
300 | ||
301 | /* | |
302 | * clean: whether the path in question is cleanly merged. | |
303 | * | |
304 | * see conflict_info.merged for more details. | |
305 | */ | |
306 | unsigned clean:1; | |
307 | ||
308 | /* | |
309 | * basename_offset: offset of basename of path. | |
310 | * | |
311 | * perf optimization to avoid recomputing offset of final '/' | |
312 | * character in pathname (0 if no '/' in pathname). | |
313 | */ | |
314 | size_t basename_offset; | |
315 | ||
316 | /* | |
317 | * directory_name: containing directory name. | |
318 | * | |
319 | * Note that we assume directory_name is constructed such that | |
320 | * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name, | |
321 | * i.e. string equality is equivalent to pointer equality. For this | |
322 | * to hold, we have to be careful setting directory_name. | |
323 | */ | |
324 | const char *directory_name; | |
325 | }; | |
326 | ||
327 | struct conflict_info { | |
328 | /* | |
329 | * merged: the version of the path that will be written to working tree | |
330 | * | |
331 | * WARNING: It is critical to check merged.clean and ensure it is 0 | |
332 | * before reading any conflict_info fields outside of merged. | |
333 | * Allocated merge_info structs will always have clean set to 1. | |
334 | * Allocated conflict_info structs will have merged.clean set to 0 | |
335 | * initially. The merged.clean field is how we know if it is safe | |
336 | * to access other parts of conflict_info besides merged; if a | |
337 | * conflict_info's merged.clean is changed to 1, the rest of the | |
338 | * algorithm is not allowed to look at anything outside of the | |
339 | * merged member anymore. | |
340 | */ | |
341 | struct merged_info merged; | |
342 | ||
343 | /* oids & modes from each of the three trees for this path */ | |
344 | struct version_info stages[3]; | |
345 | ||
346 | /* pathnames for each stage; may differ due to rename detection */ | |
347 | const char *pathnames[3]; | |
348 | ||
349 | /* Whether this path is/was involved in a directory/file conflict */ | |
350 | unsigned df_conflict:1; | |
351 | ||
1c7873cd EN |
352 | /* |
353 | * Whether this path is/was involved in a non-content conflict other | |
354 | * than a directory/file conflict (e.g. rename/rename, rename/delete, | |
355 | * file location based on possible directory rename). | |
356 | */ | |
357 | unsigned path_conflict:1; | |
358 | ||
5b59c3db EN |
359 | /* |
360 | * For filemask and dirmask, the ith bit corresponds to whether the | |
361 | * ith entry is a file (filemask) or a directory (dirmask). Thus, | |
362 | * filemask & dirmask is always zero, and filemask | dirmask is at | |
363 | * most 7 but can be less when a path does not appear as either a | |
364 | * file or a directory on at least one side of history. | |
365 | * | |
366 | * Note that these masks are related to enum merge_side, as the ith | |
367 | * entry corresponds to side i. | |
368 | * | |
369 | * These values come from a traverse_trees() call; more info may be | |
370 | * found looking at tree-walk.h's struct traverse_info, | |
371 | * particularly the documentation above the "fn" member (note that | |
372 | * filemask = mask & ~dirmask from that documentation). | |
373 | */ | |
374 | unsigned filemask:3; | |
375 | unsigned dirmask:3; | |
376 | ||
377 | /* | |
378 | * Optimization to track which stages match, to avoid the need to | |
379 | * recompute it in multiple steps. Either 0 or at least 2 bits are | |
380 | * set; if at least 2 bits are set, their corresponding stages match. | |
381 | */ | |
382 | unsigned match_mask:3; | |
383 | }; | |
384 | ||
04af1879 EN |
385 | /*** Function Grouping: various utility functions ***/ |
386 | ||
98bf9841 EN |
387 | /* |
388 | * For the next three macros, see warning for conflict_info.merged. | |
389 | * | |
390 | * In each of the below, mi is a struct merged_info*, and ci was defined | |
391 | * as a struct conflict_info* (but we need to verify ci isn't actually | |
392 | * pointed at a struct merged_info*). | |
393 | * | |
394 | * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise. | |
395 | * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one. | |
396 | * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first. | |
397 | */ | |
398 | #define INITIALIZE_CI(ci, mi) do { \ | |
399 | (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \ | |
400 | } while (0) | |
401 | #define VERIFY_CI(ci) assert(ci && !ci->merged.clean); | |
402 | #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \ | |
403 | (ci) = (struct conflict_info *)(mi); \ | |
404 | assert((ci) && !(mi)->clean); \ | |
405 | } while (0) | |
406 | ||
89422d29 EN |
407 | static void free_strmap_strings(struct strmap *map) |
408 | { | |
409 | struct hashmap_iter iter; | |
410 | struct strmap_entry *entry; | |
411 | ||
412 | strmap_for_each_entry(map, &iter, entry) { | |
413 | free((char*)entry->key); | |
414 | } | |
415 | } | |
416 | ||
43e9c4ee EN |
417 | static void clear_or_reinit_internal_opts(struct merge_options_internal *opti, |
418 | int reinitialize) | |
101bc5bc | 419 | { |
f5d9fbc2 EN |
420 | struct rename_info *renames = &opti->renames; |
421 | int i; | |
43e9c4ee EN |
422 | void (*strmap_func)(struct strmap *, int) = |
423 | reinitialize ? strmap_partial_clear : strmap_clear; | |
a49b55d5 EN |
424 | void (*strintmap_func)(struct strintmap *) = |
425 | reinitialize ? strintmap_partial_clear : strintmap_clear; | |
d29bd6d7 EN |
426 | void (*strset_func)(struct strset *) = |
427 | reinitialize ? strset_partial_clear : strset_clear; | |
101bc5bc EN |
428 | |
429 | /* | |
430 | * We marked opti->paths with strdup_strings = 0, so that we | |
431 | * wouldn't have to make another copy of the fullpath created by | |
432 | * make_traverse_path from setup_path_info(). But, now that we've | |
433 | * used it and have no other references to these strings, it is time | |
434 | * to deallocate them. | |
435 | */ | |
436 | free_strmap_strings(&opti->paths); | |
43e9c4ee | 437 | strmap_func(&opti->paths, 1); |
101bc5bc EN |
438 | |
439 | /* | |
440 | * All keys and values in opti->conflicted are a subset of those in | |
441 | * opti->paths. We don't want to deallocate anything twice, so we | |
442 | * don't free the keys and we pass 0 for free_values. | |
443 | */ | |
43e9c4ee | 444 | strmap_func(&opti->conflicted, 0); |
43c1dccb EN |
445 | |
446 | /* | |
447 | * opti->paths_to_free is similar to opti->paths; we created it with | |
448 | * strdup_strings = 0 to avoid making _another_ copy of the fullpath | |
449 | * but now that we've used it and have no other references to these | |
450 | * strings, it is time to deallocate them. We do so by temporarily | |
451 | * setting strdup_strings to 1. | |
452 | */ | |
453 | opti->paths_to_free.strdup_strings = 1; | |
454 | string_list_clear(&opti->paths_to_free, 0); | |
455 | opti->paths_to_free.strdup_strings = 0; | |
c5a6f655 | 456 | |
1218b3ab | 457 | if (opti->attr_index.cache_nr) /* true iff opt->renormalize */ |
ea305a68 EN |
458 | discard_index(&opti->attr_index); |
459 | ||
f5d9fbc2 EN |
460 | /* Free memory used by various renames maps */ |
461 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; ++i) { | |
a49b55d5 | 462 | strintmap_func(&renames->dirs_removed[i]); |
f5d9fbc2 | 463 | |
cd52e005 EN |
464 | partial_clear_dir_rename_count(&renames->dir_rename_count[i]); |
465 | if (!reinitialize) | |
466 | strmap_clear(&renames->dir_rename_count[i], 1); | |
f5d9fbc2 EN |
467 | |
468 | strmap_func(&renames->dir_renames[i], 0); | |
32a56dfb | 469 | |
a49b55d5 | 470 | strintmap_func(&renames->relevant_sources[i]); |
d29bd6d7 EN |
471 | strset_func(&renames->cached_target_names[i]); |
472 | strmap_func(&renames->cached_pairs[i], 1); | |
473 | strset_func(&renames->cached_irrelevant[i]); | |
f5d9fbc2 EN |
474 | } |
475 | ||
c5a6f655 EN |
476 | if (!reinitialize) { |
477 | struct hashmap_iter iter; | |
478 | struct strmap_entry *e; | |
479 | ||
480 | /* Release and free each strbuf found in output */ | |
481 | strmap_for_each_entry(&opti->output, &iter, e) { | |
482 | struct strbuf *sb = e->value; | |
483 | strbuf_release(sb); | |
484 | /* | |
485 | * While strictly speaking we don't need to free(sb) | |
486 | * here because we could pass free_values=1 when | |
487 | * calling strmap_clear() on opti->output, that would | |
488 | * require strmap_clear to do another | |
489 | * strmap_for_each_entry() loop, so we just free it | |
490 | * while we're iterating anyway. | |
491 | */ | |
492 | free(sb); | |
493 | } | |
494 | strmap_clear(&opti->output, 0); | |
495 | } | |
beb06145 | 496 | |
2fd9eda4 EN |
497 | renames->dir_rename_mask = 0; |
498 | ||
beb06145 EN |
499 | /* Clean out callback_data as well. */ |
500 | FREE_AND_NULL(renames->callback_data); | |
501 | renames->callback_data_nr = renames->callback_data_alloc = 0; | |
101bc5bc EN |
502 | } |
503 | ||
0c0d705b EN |
504 | static int err(struct merge_options *opt, const char *err, ...) |
505 | { | |
506 | va_list params; | |
507 | struct strbuf sb = STRBUF_INIT; | |
508 | ||
509 | strbuf_addstr(&sb, "error: "); | |
510 | va_start(params, err); | |
511 | strbuf_vaddf(&sb, err, params); | |
512 | va_end(params); | |
513 | ||
514 | error("%s", sb.buf); | |
515 | strbuf_release(&sb); | |
516 | ||
517 | return -1; | |
518 | } | |
519 | ||
c73cda76 EN |
520 | static void format_commit(struct strbuf *sb, |
521 | int indent, | |
522 | struct commit *commit) | |
523 | { | |
70f19c7f EN |
524 | struct merge_remote_desc *desc; |
525 | struct pretty_print_context ctx = {0}; | |
526 | ctx.abbrev = DEFAULT_ABBREV; | |
527 | ||
528 | strbuf_addchars(sb, ' ', indent); | |
529 | desc = merge_remote_util(commit); | |
530 | if (desc) { | |
531 | strbuf_addf(sb, "virtual %s\n", desc->name); | |
532 | return; | |
533 | } | |
534 | ||
535 | format_commit_message(commit, "%h %s", sb, &ctx); | |
536 | strbuf_addch(sb, '\n'); | |
c73cda76 EN |
537 | } |
538 | ||
c5a6f655 EN |
539 | __attribute__((format (printf, 4, 5))) |
540 | static void path_msg(struct merge_options *opt, | |
541 | const char *path, | |
542 | int omittable_hint, /* skippable under --remerge-diff */ | |
543 | const char *fmt, ...) | |
544 | { | |
545 | va_list ap; | |
546 | struct strbuf *sb = strmap_get(&opt->priv->output, path); | |
547 | if (!sb) { | |
548 | sb = xmalloc(sizeof(*sb)); | |
549 | strbuf_init(sb, 0); | |
550 | strmap_put(&opt->priv->output, path, sb); | |
551 | } | |
552 | ||
553 | va_start(ap, fmt); | |
554 | strbuf_vaddf(sb, fmt, ap); | |
555 | va_end(ap); | |
556 | ||
557 | strbuf_addch(sb, '\n'); | |
558 | } | |
559 | ||
5a1a1e8e EN |
560 | /* add a string to a strbuf, but converting "/" to "_" */ |
561 | static void add_flattened_path(struct strbuf *out, const char *s) | |
562 | { | |
563 | size_t i = out->len; | |
564 | strbuf_addstr(out, s); | |
565 | for (; i < out->len; i++) | |
566 | if (out->buf[i] == '/') | |
567 | out->buf[i] = '_'; | |
568 | } | |
569 | ||
23366d2a EN |
570 | static char *unique_path(struct strmap *existing_paths, |
571 | const char *path, | |
572 | const char *branch) | |
573 | { | |
5a1a1e8e EN |
574 | struct strbuf newpath = STRBUF_INIT; |
575 | int suffix = 0; | |
576 | size_t base_len; | |
577 | ||
578 | strbuf_addf(&newpath, "%s~", path); | |
579 | add_flattened_path(&newpath, branch); | |
580 | ||
581 | base_len = newpath.len; | |
582 | while (strmap_contains(existing_paths, newpath.buf)) { | |
583 | strbuf_setlen(&newpath, base_len); | |
584 | strbuf_addf(&newpath, "_%d", suffix++); | |
585 | } | |
586 | ||
587 | return strbuf_detach(&newpath, NULL); | |
23366d2a EN |
588 | } |
589 | ||
04af1879 EN |
590 | /*** Function Grouping: functions related to collect_merge_info() ***/ |
591 | ||
a68e6cea EN |
592 | static int traverse_trees_wrapper_callback(int n, |
593 | unsigned long mask, | |
594 | unsigned long dirmask, | |
595 | struct name_entry *names, | |
596 | struct traverse_info *info) | |
597 | { | |
598 | struct merge_options *opt = info->data; | |
599 | struct rename_info *renames = &opt->priv->renames; | |
2fd9eda4 | 600 | unsigned filemask = mask & ~dirmask; |
a68e6cea EN |
601 | |
602 | assert(n==3); | |
603 | ||
604 | if (!renames->callback_data_traverse_path) | |
605 | renames->callback_data_traverse_path = xstrdup(info->traverse_path); | |
606 | ||
2fd9eda4 EN |
607 | if (filemask && filemask == renames->dir_rename_mask) |
608 | renames->dir_rename_mask = 0x07; | |
609 | ||
a68e6cea EN |
610 | ALLOC_GROW(renames->callback_data, renames->callback_data_nr + 1, |
611 | renames->callback_data_alloc); | |
612 | renames->callback_data[renames->callback_data_nr].mask = mask; | |
613 | renames->callback_data[renames->callback_data_nr].dirmask = dirmask; | |
614 | COPY_ARRAY(renames->callback_data[renames->callback_data_nr].names, | |
615 | names, 3); | |
616 | renames->callback_data_nr++; | |
617 | ||
618 | return mask; | |
619 | } | |
620 | ||
621 | /* | |
622 | * Much like traverse_trees(), BUT: | |
623 | * - read all the tree entries FIRST, saving them | |
624 | * - note that the above step provides an opportunity to compute necessary | |
625 | * additional details before the "real" traversal | |
626 | * - loop through the saved entries and call the original callback on them | |
627 | */ | |
a68e6cea EN |
628 | static int traverse_trees_wrapper(struct index_state *istate, |
629 | int n, | |
630 | struct tree_desc *t, | |
631 | struct traverse_info *info) | |
632 | { | |
633 | int ret, i, old_offset; | |
634 | traverse_callback_t old_fn; | |
635 | char *old_callback_data_traverse_path; | |
636 | struct merge_options *opt = info->data; | |
637 | struct rename_info *renames = &opt->priv->renames; | |
638 | ||
2fd9eda4 EN |
639 | assert(renames->dir_rename_mask == 2 || renames->dir_rename_mask == 4); |
640 | ||
a68e6cea EN |
641 | old_callback_data_traverse_path = renames->callback_data_traverse_path; |
642 | old_fn = info->fn; | |
643 | old_offset = renames->callback_data_nr; | |
644 | ||
645 | renames->callback_data_traverse_path = NULL; | |
646 | info->fn = traverse_trees_wrapper_callback; | |
647 | ret = traverse_trees(istate, n, t, info); | |
648 | if (ret < 0) | |
649 | return ret; | |
650 | ||
651 | info->traverse_path = renames->callback_data_traverse_path; | |
652 | info->fn = old_fn; | |
653 | for (i = old_offset; i < renames->callback_data_nr; ++i) { | |
654 | info->fn(n, | |
655 | renames->callback_data[i].mask, | |
656 | renames->callback_data[i].dirmask, | |
657 | renames->callback_data[i].names, | |
658 | info); | |
659 | } | |
660 | ||
661 | renames->callback_data_nr = old_offset; | |
662 | free(renames->callback_data_traverse_path); | |
663 | renames->callback_data_traverse_path = old_callback_data_traverse_path; | |
664 | info->traverse_path = NULL; | |
665 | return 0; | |
666 | } | |
667 | ||
98bf9841 EN |
668 | static void setup_path_info(struct merge_options *opt, |
669 | struct string_list_item *result, | |
670 | const char *current_dir_name, | |
671 | int current_dir_name_len, | |
672 | char *fullpath, /* we'll take over ownership */ | |
673 | struct name_entry *names, | |
674 | struct name_entry *merged_version, | |
675 | unsigned is_null, /* boolean */ | |
676 | unsigned df_conflict, /* boolean */ | |
677 | unsigned filemask, | |
678 | unsigned dirmask, | |
679 | int resolved /* boolean */) | |
680 | { | |
681 | /* result->util is void*, so mi is a convenience typed variable */ | |
682 | struct merged_info *mi; | |
683 | ||
684 | assert(!is_null || resolved); | |
685 | assert(!df_conflict || !resolved); /* df_conflict implies !resolved */ | |
686 | assert(resolved == (merged_version != NULL)); | |
687 | ||
688 | mi = xcalloc(1, resolved ? sizeof(struct merged_info) : | |
689 | sizeof(struct conflict_info)); | |
690 | mi->directory_name = current_dir_name; | |
691 | mi->basename_offset = current_dir_name_len; | |
692 | mi->clean = !!resolved; | |
693 | if (resolved) { | |
694 | mi->result.mode = merged_version->mode; | |
695 | oidcpy(&mi->result.oid, &merged_version->oid); | |
696 | mi->is_null = !!is_null; | |
697 | } else { | |
698 | int i; | |
699 | struct conflict_info *ci; | |
700 | ||
701 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
702 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
703 | ci->pathnames[i] = fullpath; | |
704 | ci->stages[i].mode = names[i].mode; | |
705 | oidcpy(&ci->stages[i].oid, &names[i].oid); | |
706 | } | |
707 | ci->filemask = filemask; | |
708 | ci->dirmask = dirmask; | |
709 | ci->df_conflict = !!df_conflict; | |
710 | if (dirmask) | |
711 | /* | |
712 | * Assume is_null for now, but if we have entries | |
713 | * under the directory then when it is complete in | |
714 | * write_completed_directory() it'll update this. | |
715 | * Also, for D/F conflicts, we have to handle the | |
716 | * directory first, then clear this bit and process | |
717 | * the file to see how it is handled -- that occurs | |
718 | * near the top of process_entry(). | |
719 | */ | |
720 | mi->is_null = 1; | |
721 | } | |
722 | strmap_put(&opt->priv->paths, fullpath, mi); | |
723 | result->string = fullpath; | |
724 | result->util = mi; | |
725 | } | |
726 | ||
f78cf976 EN |
727 | static void add_pair(struct merge_options *opt, |
728 | struct name_entry *names, | |
729 | const char *pathname, | |
730 | unsigned side, | |
32a56dfb | 731 | unsigned is_add /* if false, is_delete */, |
2fd9eda4 EN |
732 | unsigned match_mask, |
733 | unsigned dir_rename_mask) | |
f78cf976 EN |
734 | { |
735 | struct diff_filespec *one, *two; | |
736 | struct rename_info *renames = &opt->priv->renames; | |
737 | int names_idx = is_add ? side : 0; | |
738 | ||
32a56dfb EN |
739 | if (!is_add) { |
740 | unsigned content_relevant = (match_mask == 0); | |
2fd9eda4 | 741 | unsigned location_relevant = (dir_rename_mask == 0x07); |
32a56dfb | 742 | |
ec59da60 EN |
743 | if (content_relevant || location_relevant) { |
744 | /* content_relevant trumps location_relevant */ | |
745 | strintmap_set(&renames->relevant_sources[side], pathname, | |
746 | content_relevant ? RELEVANT_CONTENT : RELEVANT_LOCATION); | |
747 | } | |
32a56dfb EN |
748 | } |
749 | ||
f78cf976 EN |
750 | one = alloc_filespec(pathname); |
751 | two = alloc_filespec(pathname); | |
752 | fill_filespec(is_add ? two : one, | |
753 | &names[names_idx].oid, 1, names[names_idx].mode); | |
754 | diff_queue(&renames->pairs[side], one, two); | |
755 | } | |
756 | ||
eb3e3e1d EN |
757 | static void collect_rename_info(struct merge_options *opt, |
758 | struct name_entry *names, | |
759 | const char *dirname, | |
760 | const char *fullname, | |
761 | unsigned filemask, | |
762 | unsigned dirmask, | |
763 | unsigned match_mask) | |
764 | { | |
765 | struct rename_info *renames = &opt->priv->renames; | |
f78cf976 | 766 | unsigned side; |
eb3e3e1d | 767 | |
2fd9eda4 EN |
768 | /* |
769 | * Update dir_rename_mask (determines ignore-rename-source validity) | |
770 | * | |
771 | * dir_rename_mask helps us keep track of when directory rename | |
772 | * detection may be relevant. Basically, whenver a directory is | |
773 | * removed on one side of history, and a file is added to that | |
774 | * directory on the other side of history, directory rename | |
775 | * detection is relevant (meaning we have to detect renames for all | |
776 | * files within that directory to deduce where the directory | |
777 | * moved). Also, whenever a directory needs directory rename | |
778 | * detection, due to the "majority rules" choice for where to move | |
779 | * it (see t6423 testcase 1f), we also need to detect renames for | |
780 | * all files within subdirectories of that directory as well. | |
781 | * | |
782 | * Here we haven't looked at files within the directory yet, we are | |
783 | * just looking at the directory itself. So, if we aren't yet in | |
784 | * a case where a parent directory needed directory rename detection | |
785 | * (i.e. dir_rename_mask != 0x07), and if the directory was removed | |
786 | * on one side of history, record the mask of the other side of | |
787 | * history in dir_rename_mask. | |
788 | */ | |
789 | if (renames->dir_rename_mask != 0x07 && | |
790 | (dirmask == 3 || dirmask == 5)) { | |
791 | /* simple sanity check */ | |
792 | assert(renames->dir_rename_mask == 0 || | |
793 | renames->dir_rename_mask == (dirmask & ~1)); | |
794 | /* update dir_rename_mask; have it record mask of new side */ | |
795 | renames->dir_rename_mask = (dirmask & ~1); | |
796 | } | |
797 | ||
eb3e3e1d EN |
798 | /* Update dirs_removed, as needed */ |
799 | if (dirmask == 1 || dirmask == 3 || dirmask == 5) { | |
800 | /* absent_mask = 0x07 - dirmask; sides = absent_mask/2 */ | |
801 | unsigned sides = (0x07 - dirmask)/2; | |
fb52938e EN |
802 | unsigned relevance = (renames->dir_rename_mask == 0x07) ? |
803 | RELEVANT_FOR_ANCESTOR : NOT_RELEVANT; | |
804 | /* | |
805 | * Record relevance of this directory. However, note that | |
806 | * when collect_merge_info_callback() recurses into this | |
807 | * directory and calls collect_rename_info() on paths | |
808 | * within that directory, if we find a path that was added | |
809 | * to this directory on the other side of history, we will | |
810 | * upgrade this value to RELEVANT_FOR_SELF; see below. | |
811 | */ | |
eb3e3e1d | 812 | if (sides & 1) |
fb52938e EN |
813 | strintmap_set(&renames->dirs_removed[1], fullname, |
814 | relevance); | |
eb3e3e1d | 815 | if (sides & 2) |
fb52938e EN |
816 | strintmap_set(&renames->dirs_removed[2], fullname, |
817 | relevance); | |
818 | } | |
819 | ||
820 | /* | |
821 | * Here's the block that potentially upgrades to RELEVANT_FOR_SELF. | |
822 | * When we run across a file added to a directory. In such a case, | |
823 | * find the directory of the file and upgrade its relevance. | |
824 | */ | |
825 | if (renames->dir_rename_mask == 0x07 && | |
826 | (filemask == 2 || filemask == 4)) { | |
827 | /* | |
828 | * Need directory rename for parent directory on other side | |
829 | * of history from added file. Thus | |
830 | * side = (~filemask & 0x06) >> 1 | |
831 | * or | |
832 | * side = 3 - (filemask/2). | |
833 | */ | |
834 | unsigned side = 3 - (filemask >> 1); | |
835 | strintmap_set(&renames->dirs_removed[side], dirname, | |
836 | RELEVANT_FOR_SELF); | |
eb3e3e1d | 837 | } |
f78cf976 EN |
838 | |
839 | if (filemask == 0 || filemask == 7) | |
840 | return; | |
841 | ||
842 | for (side = MERGE_SIDE1; side <= MERGE_SIDE2; ++side) { | |
843 | unsigned side_mask = (1 << side); | |
844 | ||
845 | /* Check for deletion on side */ | |
846 | if ((filemask & 1) && !(filemask & side_mask)) | |
32a56dfb | 847 | add_pair(opt, names, fullname, side, 0 /* delete */, |
2fd9eda4 EN |
848 | match_mask & filemask, |
849 | renames->dir_rename_mask); | |
f78cf976 EN |
850 | |
851 | /* Check for addition on side */ | |
852 | if (!(filemask & 1) && (filemask & side_mask)) | |
32a56dfb | 853 | add_pair(opt, names, fullname, side, 1 /* add */, |
2fd9eda4 EN |
854 | match_mask & filemask, |
855 | renames->dir_rename_mask); | |
f78cf976 | 856 | } |
eb3e3e1d EN |
857 | } |
858 | ||
d2bc1994 EN |
859 | static int collect_merge_info_callback(int n, |
860 | unsigned long mask, | |
861 | unsigned long dirmask, | |
862 | struct name_entry *names, | |
863 | struct traverse_info *info) | |
864 | { | |
865 | /* | |
866 | * n is 3. Always. | |
867 | * common ancestor (mbase) has mask 1, and stored in index 0 of names | |
868 | * head of side 1 (side1) has mask 2, and stored in index 1 of names | |
869 | * head of side 2 (side2) has mask 4, and stored in index 2 of names | |
870 | */ | |
871 | struct merge_options *opt = info->data; | |
872 | struct merge_options_internal *opti = opt->priv; | |
2fd9eda4 | 873 | struct rename_info *renames = &opt->priv->renames; |
98bf9841 EN |
874 | struct string_list_item pi; /* Path Info */ |
875 | struct conflict_info *ci; /* typed alias to pi.util (which is void*) */ | |
d2bc1994 EN |
876 | struct name_entry *p; |
877 | size_t len; | |
878 | char *fullpath; | |
98bf9841 | 879 | const char *dirname = opti->current_dir_name; |
2fd9eda4 | 880 | unsigned prev_dir_rename_mask = renames->dir_rename_mask; |
d2bc1994 | 881 | unsigned filemask = mask & ~dirmask; |
34e557af | 882 | unsigned match_mask = 0; /* will be updated below */ |
d2bc1994 EN |
883 | unsigned mbase_null = !(mask & 1); |
884 | unsigned side1_null = !(mask & 2); | |
885 | unsigned side2_null = !(mask & 4); | |
885f0063 EN |
886 | unsigned side1_matches_mbase = (!side1_null && !mbase_null && |
887 | names[0].mode == names[1].mode && | |
888 | oideq(&names[0].oid, &names[1].oid)); | |
889 | unsigned side2_matches_mbase = (!side2_null && !mbase_null && | |
890 | names[0].mode == names[2].mode && | |
891 | oideq(&names[0].oid, &names[2].oid)); | |
892 | unsigned sides_match = (!side1_null && !side2_null && | |
893 | names[1].mode == names[2].mode && | |
894 | oideq(&names[1].oid, &names[2].oid)); | |
d2bc1994 | 895 | |
34e557af EN |
896 | /* |
897 | * Note: When a path is a file on one side of history and a directory | |
898 | * in another, we have a directory/file conflict. In such cases, if | |
899 | * the conflict doesn't resolve from renames and deletions, then we | |
900 | * always leave directories where they are and move files out of the | |
901 | * way. Thus, while struct conflict_info has a df_conflict field to | |
902 | * track such conflicts, we ignore that field for any directories at | |
903 | * a path and only pay attention to it for files at the given path. | |
904 | * The fact that we leave directories were they are also means that | |
905 | * we do not need to worry about getting additional df_conflict | |
906 | * information propagated from parent directories down to children | |
907 | * (unlike, say traverse_trees_recursive() in unpack-trees.c, which | |
908 | * sets a newinfo.df_conflicts field specifically to propagate it). | |
909 | */ | |
910 | unsigned df_conflict = (filemask != 0) && (dirmask != 0); | |
911 | ||
d2bc1994 EN |
912 | /* n = 3 is a fundamental assumption. */ |
913 | if (n != 3) | |
914 | BUG("Called collect_merge_info_callback wrong"); | |
915 | ||
916 | /* | |
917 | * A bunch of sanity checks verifying that traverse_trees() calls | |
918 | * us the way I expect. Could just remove these at some point, | |
919 | * though maybe they are helpful to future code readers. | |
920 | */ | |
921 | assert(mbase_null == is_null_oid(&names[0].oid)); | |
922 | assert(side1_null == is_null_oid(&names[1].oid)); | |
923 | assert(side2_null == is_null_oid(&names[2].oid)); | |
924 | assert(!mbase_null || !side1_null || !side2_null); | |
925 | assert(mask > 0 && mask < 8); | |
926 | ||
34e557af EN |
927 | /* Determine match_mask */ |
928 | if (side1_matches_mbase) | |
929 | match_mask = (side2_matches_mbase ? 7 : 3); | |
930 | else if (side2_matches_mbase) | |
931 | match_mask = 5; | |
932 | else if (sides_match) | |
933 | match_mask = 6; | |
934 | ||
d2bc1994 EN |
935 | /* |
936 | * Get the name of the relevant filepath, which we'll pass to | |
937 | * setup_path_info() for tracking. | |
938 | */ | |
939 | p = names; | |
940 | while (!p->mode) | |
941 | p++; | |
942 | len = traverse_path_len(info, p->pathlen); | |
943 | ||
944 | /* +1 in both of the following lines to include the NUL byte */ | |
945 | fullpath = xmalloc(len + 1); | |
946 | make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen); | |
947 | ||
291f29ca EN |
948 | /* |
949 | * If mbase, side1, and side2 all match, we can resolve early. Even | |
950 | * if these are trees, there will be no renames or anything | |
951 | * underneath. | |
952 | */ | |
953 | if (side1_matches_mbase && side2_matches_mbase) { | |
954 | /* mbase, side1, & side2 all match; use mbase as resolution */ | |
955 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
956 | names, names+0, mbase_null, 0, | |
957 | filemask, dirmask, 1); | |
958 | return mask; | |
959 | } | |
960 | ||
eb3e3e1d EN |
961 | /* |
962 | * Gather additional information used in rename detection. | |
963 | */ | |
964 | collect_rename_info(opt, names, dirname, fullpath, | |
965 | filemask, dirmask, match_mask); | |
966 | ||
d2bc1994 | 967 | /* |
98bf9841 EN |
968 | * Record information about the path so we can resolve later in |
969 | * process_entries. | |
d2bc1994 | 970 | */ |
98bf9841 EN |
971 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, |
972 | names, NULL, 0, df_conflict, filemask, dirmask, 0); | |
973 | ||
974 | ci = pi.util; | |
975 | VERIFY_CI(ci); | |
34e557af | 976 | ci->match_mask = match_mask; |
d2bc1994 EN |
977 | |
978 | /* If dirmask, recurse into subdirectories */ | |
979 | if (dirmask) { | |
980 | struct traverse_info newinfo; | |
981 | struct tree_desc t[3]; | |
982 | void *buf[3] = {NULL, NULL, NULL}; | |
983 | const char *original_dir_name; | |
984 | int i, ret; | |
985 | ||
986 | ci->match_mask &= filemask; | |
987 | newinfo = *info; | |
988 | newinfo.prev = info; | |
989 | newinfo.name = p->path; | |
990 | newinfo.namelen = p->pathlen; | |
991 | newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1); | |
34e557af EN |
992 | /* |
993 | * If this directory we are about to recurse into cared about | |
994 | * its parent directory (the current directory) having a D/F | |
995 | * conflict, then we'd propagate the masks in this way: | |
996 | * newinfo.df_conflicts |= (mask & ~dirmask); | |
997 | * But we don't worry about propagating D/F conflicts. (See | |
998 | * comment near setting of local df_conflict variable near | |
999 | * the beginning of this function). | |
1000 | */ | |
d2bc1994 EN |
1001 | |
1002 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
885f0063 EN |
1003 | if (i == 1 && side1_matches_mbase) |
1004 | t[1] = t[0]; | |
1005 | else if (i == 2 && side2_matches_mbase) | |
1006 | t[2] = t[0]; | |
1007 | else if (i == 2 && sides_match) | |
1008 | t[2] = t[1]; | |
1009 | else { | |
1010 | const struct object_id *oid = NULL; | |
1011 | if (dirmask & 1) | |
1012 | oid = &names[i].oid; | |
1013 | buf[i] = fill_tree_descriptor(opt->repo, | |
1014 | t + i, oid); | |
1015 | } | |
d2bc1994 EN |
1016 | dirmask >>= 1; |
1017 | } | |
1018 | ||
1019 | original_dir_name = opti->current_dir_name; | |
98bf9841 | 1020 | opti->current_dir_name = pi.string; |
2fd9eda4 EN |
1021 | if (renames->dir_rename_mask == 0 || |
1022 | renames->dir_rename_mask == 0x07) | |
1023 | ret = traverse_trees(NULL, 3, t, &newinfo); | |
1024 | else | |
1025 | ret = traverse_trees_wrapper(NULL, 3, t, &newinfo); | |
d2bc1994 | 1026 | opti->current_dir_name = original_dir_name; |
2fd9eda4 | 1027 | renames->dir_rename_mask = prev_dir_rename_mask; |
d2bc1994 EN |
1028 | |
1029 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) | |
1030 | free(buf[i]); | |
1031 | ||
1032 | if (ret < 0) | |
1033 | return -1; | |
1034 | } | |
1035 | ||
1036 | return mask; | |
1037 | } | |
1038 | ||
231e2dd4 EN |
1039 | static int collect_merge_info(struct merge_options *opt, |
1040 | struct tree *merge_base, | |
1041 | struct tree *side1, | |
1042 | struct tree *side2) | |
1043 | { | |
d2bc1994 EN |
1044 | int ret; |
1045 | struct tree_desc t[3]; | |
1046 | struct traverse_info info; | |
d2bc1994 | 1047 | |
05b85c6e EN |
1048 | opt->priv->toplevel_dir = ""; |
1049 | opt->priv->current_dir_name = opt->priv->toplevel_dir; | |
1050 | setup_traverse_info(&info, opt->priv->toplevel_dir); | |
d2bc1994 EN |
1051 | info.fn = collect_merge_info_callback; |
1052 | info.data = opt; | |
1053 | info.show_all_errors = 1; | |
1054 | ||
1055 | parse_tree(merge_base); | |
1056 | parse_tree(side1); | |
1057 | parse_tree(side2); | |
1058 | init_tree_desc(t + 0, merge_base->buffer, merge_base->size); | |
1059 | init_tree_desc(t + 1, side1->buffer, side1->size); | |
1060 | init_tree_desc(t + 2, side2->buffer, side2->size); | |
1061 | ||
557ac035 | 1062 | trace2_region_enter("merge", "traverse_trees", opt->repo); |
d2bc1994 | 1063 | ret = traverse_trees(NULL, 3, t, &info); |
557ac035 | 1064 | trace2_region_leave("merge", "traverse_trees", opt->repo); |
d2bc1994 EN |
1065 | |
1066 | return ret; | |
231e2dd4 EN |
1067 | } |
1068 | ||
04af1879 EN |
1069 | /*** Function Grouping: functions related to threeway content merges ***/ |
1070 | ||
c73cda76 EN |
1071 | static int find_first_merges(struct repository *repo, |
1072 | const char *path, | |
1073 | struct commit *a, | |
1074 | struct commit *b, | |
1075 | struct object_array *result) | |
1076 | { | |
4204cd59 EN |
1077 | int i, j; |
1078 | struct object_array merges = OBJECT_ARRAY_INIT; | |
1079 | struct commit *commit; | |
1080 | int contains_another; | |
1081 | ||
1082 | char merged_revision[GIT_MAX_HEXSZ + 2]; | |
1083 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
1084 | "--all", merged_revision, NULL }; | |
1085 | struct rev_info revs; | |
1086 | struct setup_revision_opt rev_opts; | |
1087 | ||
1088 | memset(result, 0, sizeof(struct object_array)); | |
1089 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
1090 | ||
1091 | /* get all revisions that merge commit a */ | |
1092 | xsnprintf(merged_revision, sizeof(merged_revision), "^%s", | |
1093 | oid_to_hex(&a->object.oid)); | |
1094 | repo_init_revisions(repo, &revs, NULL); | |
1095 | rev_opts.submodule = path; | |
1096 | /* FIXME: can't handle linked worktrees in submodules yet */ | |
1097 | revs.single_worktree = path != NULL; | |
1098 | setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); | |
1099 | ||
1100 | /* save all revisions from the above list that contain b */ | |
1101 | if (prepare_revision_walk(&revs)) | |
1102 | die("revision walk setup failed"); | |
1103 | while ((commit = get_revision(&revs)) != NULL) { | |
1104 | struct object *o = &(commit->object); | |
1105 | if (in_merge_bases(b, commit)) | |
1106 | add_object_array(o, NULL, &merges); | |
1107 | } | |
1108 | reset_revision_walk(); | |
1109 | ||
1110 | /* Now we've got all merges that contain a and b. Prune all | |
1111 | * merges that contain another found merge and save them in | |
1112 | * result. | |
1113 | */ | |
1114 | for (i = 0; i < merges.nr; i++) { | |
1115 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
1116 | ||
1117 | contains_another = 0; | |
1118 | for (j = 0; j < merges.nr; j++) { | |
1119 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
1120 | if (i != j && in_merge_bases(m2, m1)) { | |
1121 | contains_another = 1; | |
1122 | break; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | if (!contains_another) | |
1127 | add_object_array(merges.objects[i].item, NULL, result); | |
1128 | } | |
1129 | ||
1130 | object_array_clear(&merges); | |
1131 | return result->nr; | |
c73cda76 EN |
1132 | } |
1133 | ||
62fdec17 EN |
1134 | static int merge_submodule(struct merge_options *opt, |
1135 | const char *path, | |
1136 | const struct object_id *o, | |
1137 | const struct object_id *a, | |
1138 | const struct object_id *b, | |
1139 | struct object_id *result) | |
1140 | { | |
c73cda76 EN |
1141 | struct commit *commit_o, *commit_a, *commit_b; |
1142 | int parent_count; | |
1143 | struct object_array merges; | |
1144 | struct strbuf sb = STRBUF_INIT; | |
1145 | ||
1146 | int i; | |
1147 | int search = !opt->priv->call_depth; | |
1148 | ||
1149 | /* store fallback answer in result in case we fail */ | |
1150 | oidcpy(result, opt->priv->call_depth ? o : a); | |
1151 | ||
1152 | /* we can not handle deletion conflicts */ | |
1153 | if (is_null_oid(o)) | |
1154 | return 0; | |
1155 | if (is_null_oid(a)) | |
1156 | return 0; | |
1157 | if (is_null_oid(b)) | |
1158 | return 0; | |
1159 | ||
1160 | if (add_submodule_odb(path)) { | |
1161 | path_msg(opt, path, 0, | |
1162 | _("Failed to merge submodule %s (not checked out)"), | |
1163 | path); | |
1164 | return 0; | |
1165 | } | |
1166 | ||
1167 | if (!(commit_o = lookup_commit_reference(opt->repo, o)) || | |
1168 | !(commit_a = lookup_commit_reference(opt->repo, a)) || | |
1169 | !(commit_b = lookup_commit_reference(opt->repo, b))) { | |
1170 | path_msg(opt, path, 0, | |
1171 | _("Failed to merge submodule %s (commits not present)"), | |
1172 | path); | |
1173 | return 0; | |
1174 | } | |
1175 | ||
1176 | /* check whether both changes are forward */ | |
1177 | if (!in_merge_bases(commit_o, commit_a) || | |
1178 | !in_merge_bases(commit_o, commit_b)) { | |
1179 | path_msg(opt, path, 0, | |
1180 | _("Failed to merge submodule %s " | |
1181 | "(commits don't follow merge-base)"), | |
1182 | path); | |
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | /* Case #1: a is contained in b or vice versa */ | |
1187 | if (in_merge_bases(commit_a, commit_b)) { | |
1188 | oidcpy(result, b); | |
1189 | path_msg(opt, path, 1, | |
1190 | _("Note: Fast-forwarding submodule %s to %s"), | |
1191 | path, oid_to_hex(b)); | |
1192 | return 1; | |
1193 | } | |
1194 | if (in_merge_bases(commit_b, commit_a)) { | |
1195 | oidcpy(result, a); | |
1196 | path_msg(opt, path, 1, | |
1197 | _("Note: Fast-forwarding submodule %s to %s"), | |
1198 | path, oid_to_hex(a)); | |
1199 | return 1; | |
1200 | } | |
1201 | ||
1202 | /* | |
1203 | * Case #2: There are one or more merges that contain a and b in | |
1204 | * the submodule. If there is only one, then present it as a | |
1205 | * suggestion to the user, but leave it marked unmerged so the | |
1206 | * user needs to confirm the resolution. | |
1207 | */ | |
1208 | ||
1209 | /* Skip the search if makes no sense to the calling context. */ | |
1210 | if (!search) | |
1211 | return 0; | |
1212 | ||
1213 | /* find commit which merges them */ | |
1214 | parent_count = find_first_merges(opt->repo, path, commit_a, commit_b, | |
1215 | &merges); | |
1216 | switch (parent_count) { | |
1217 | case 0: | |
1218 | path_msg(opt, path, 0, _("Failed to merge submodule %s"), path); | |
1219 | break; | |
1220 | ||
1221 | case 1: | |
1222 | format_commit(&sb, 4, | |
1223 | (struct commit *)merges.objects[0].item); | |
1224 | path_msg(opt, path, 0, | |
1225 | _("Failed to merge submodule %s, but a possible merge " | |
1226 | "resolution exists:\n%s\n"), | |
1227 | path, sb.buf); | |
1228 | path_msg(opt, path, 1, | |
1229 | _("If this is correct simply add it to the index " | |
1230 | "for example\n" | |
1231 | "by using:\n\n" | |
1232 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" | |
1233 | "which will accept this suggestion.\n"), | |
1234 | oid_to_hex(&merges.objects[0].item->oid), path); | |
1235 | strbuf_release(&sb); | |
1236 | break; | |
1237 | default: | |
1238 | for (i = 0; i < merges.nr; i++) | |
1239 | format_commit(&sb, 4, | |
1240 | (struct commit *)merges.objects[i].item); | |
1241 | path_msg(opt, path, 0, | |
1242 | _("Failed to merge submodule %s, but multiple " | |
1243 | "possible merges exist:\n%s"), path, sb.buf); | |
1244 | strbuf_release(&sb); | |
1245 | } | |
1246 | ||
1247 | object_array_clear(&merges); | |
1248 | return 0; | |
62fdec17 EN |
1249 | } |
1250 | ||
1218b3ab EN |
1251 | static void initialize_attr_index(struct merge_options *opt) |
1252 | { | |
1253 | /* | |
1254 | * The renormalize_buffer() functions require attributes, and | |
1255 | * annoyingly those can only be read from the working tree or from | |
1256 | * an index_state. merge-ort doesn't have an index_state, so we | |
1257 | * generate a fake one containing only attribute information. | |
1258 | */ | |
1259 | struct merged_info *mi; | |
1260 | struct index_state *attr_index = &opt->priv->attr_index; | |
1261 | struct cache_entry *ce; | |
1262 | ||
1263 | attr_index->initialized = 1; | |
1264 | ||
1265 | if (!opt->renormalize) | |
1266 | return; | |
1267 | ||
1268 | mi = strmap_get(&opt->priv->paths, GITATTRIBUTES_FILE); | |
1269 | if (!mi) | |
1270 | return; | |
1271 | ||
1272 | if (mi->clean) { | |
1273 | int len = strlen(GITATTRIBUTES_FILE); | |
1274 | ce = make_empty_cache_entry(attr_index, len); | |
1275 | ce->ce_mode = create_ce_mode(mi->result.mode); | |
1276 | ce->ce_flags = create_ce_flags(0); | |
1277 | ce->ce_namelen = len; | |
1278 | oidcpy(&ce->oid, &mi->result.oid); | |
1279 | memcpy(ce->name, GITATTRIBUTES_FILE, len); | |
1280 | add_index_entry(attr_index, ce, | |
1281 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
1282 | get_stream_filter(attr_index, GITATTRIBUTES_FILE, &ce->oid); | |
1283 | } else { | |
1284 | int stage, len; | |
1285 | struct conflict_info *ci; | |
1286 | ||
1287 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
1288 | for (stage = 0; stage < 3; stage++) { | |
1289 | unsigned stage_mask = (1 << stage); | |
1290 | ||
1291 | if (!(ci->filemask & stage_mask)) | |
1292 | continue; | |
1293 | len = strlen(GITATTRIBUTES_FILE); | |
1294 | ce = make_empty_cache_entry(attr_index, len); | |
1295 | ce->ce_mode = create_ce_mode(ci->stages[stage].mode); | |
1296 | ce->ce_flags = create_ce_flags(stage); | |
1297 | ce->ce_namelen = len; | |
1298 | oidcpy(&ce->oid, &ci->stages[stage].oid); | |
1299 | memcpy(ce->name, GITATTRIBUTES_FILE, len); | |
1300 | add_index_entry(attr_index, ce, | |
1301 | ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); | |
1302 | get_stream_filter(attr_index, GITATTRIBUTES_FILE, | |
1303 | &ce->oid); | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | ||
62fdec17 EN |
1308 | static int merge_3way(struct merge_options *opt, |
1309 | const char *path, | |
1310 | const struct object_id *o, | |
1311 | const struct object_id *a, | |
1312 | const struct object_id *b, | |
1313 | const char *pathnames[3], | |
1314 | const int extra_marker_size, | |
1315 | mmbuffer_t *result_buf) | |
1316 | { | |
f591c472 EN |
1317 | mmfile_t orig, src1, src2; |
1318 | struct ll_merge_options ll_opts = {0}; | |
1319 | char *base, *name1, *name2; | |
1320 | int merge_status; | |
1321 | ||
1218b3ab EN |
1322 | if (!opt->priv->attr_index.initialized) |
1323 | initialize_attr_index(opt); | |
1324 | ||
f591c472 EN |
1325 | ll_opts.renormalize = opt->renormalize; |
1326 | ll_opts.extra_marker_size = extra_marker_size; | |
1327 | ll_opts.xdl_opts = opt->xdl_opts; | |
1328 | ||
1329 | if (opt->priv->call_depth) { | |
1330 | ll_opts.virtual_ancestor = 1; | |
1331 | ll_opts.variant = 0; | |
1332 | } else { | |
1333 | switch (opt->recursive_variant) { | |
1334 | case MERGE_VARIANT_OURS: | |
1335 | ll_opts.variant = XDL_MERGE_FAVOR_OURS; | |
1336 | break; | |
1337 | case MERGE_VARIANT_THEIRS: | |
1338 | ll_opts.variant = XDL_MERGE_FAVOR_THEIRS; | |
1339 | break; | |
1340 | default: | |
1341 | ll_opts.variant = 0; | |
1342 | break; | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | assert(pathnames[0] && pathnames[1] && pathnames[2] && opt->ancestor); | |
1347 | if (pathnames[0] == pathnames[1] && pathnames[1] == pathnames[2]) { | |
1348 | base = mkpathdup("%s", opt->ancestor); | |
1349 | name1 = mkpathdup("%s", opt->branch1); | |
1350 | name2 = mkpathdup("%s", opt->branch2); | |
1351 | } else { | |
1352 | base = mkpathdup("%s:%s", opt->ancestor, pathnames[0]); | |
1353 | name1 = mkpathdup("%s:%s", opt->branch1, pathnames[1]); | |
1354 | name2 = mkpathdup("%s:%s", opt->branch2, pathnames[2]); | |
1355 | } | |
1356 | ||
1357 | read_mmblob(&orig, o); | |
1358 | read_mmblob(&src1, a); | |
1359 | read_mmblob(&src2, b); | |
1360 | ||
1361 | merge_status = ll_merge(result_buf, path, &orig, base, | |
1362 | &src1, name1, &src2, name2, | |
1218b3ab | 1363 | &opt->priv->attr_index, &ll_opts); |
f591c472 EN |
1364 | |
1365 | free(base); | |
1366 | free(name1); | |
1367 | free(name2); | |
1368 | free(orig.ptr); | |
1369 | free(src1.ptr); | |
1370 | free(src2.ptr); | |
1371 | return merge_status; | |
62fdec17 EN |
1372 | } |
1373 | ||
e2e9dc03 EN |
1374 | static int handle_content_merge(struct merge_options *opt, |
1375 | const char *path, | |
1376 | const struct version_info *o, | |
1377 | const struct version_info *a, | |
1378 | const struct version_info *b, | |
1379 | const char *pathnames[3], | |
1380 | const int extra_marker_size, | |
1381 | struct version_info *result) | |
1382 | { | |
991bbdca | 1383 | /* |
62fdec17 EN |
1384 | * path is the target location where we want to put the file, and |
1385 | * is used to determine any normalization rules in ll_merge. | |
1386 | * | |
1387 | * The normal case is that path and all entries in pathnames are | |
1388 | * identical, though renames can affect which path we got one of | |
1389 | * the three blobs to merge on various sides of history. | |
1390 | * | |
1391 | * extra_marker_size is the amount to extend conflict markers in | |
1392 | * ll_merge; this is neeed if we have content merges of content | |
1393 | * merges, which happens for example with rename/rename(2to1) and | |
1394 | * rename/add conflicts. | |
1395 | */ | |
1396 | unsigned clean = 1; | |
1397 | ||
1398 | /* | |
1399 | * handle_content_merge() needs both files to be of the same type, i.e. | |
1400 | * both files OR both submodules OR both symlinks. Conflicting types | |
1401 | * needs to be handled elsewhere. | |
991bbdca | 1402 | */ |
62fdec17 EN |
1403 | assert((S_IFMT & a->mode) == (S_IFMT & b->mode)); |
1404 | ||
1405 | /* Merge modes */ | |
1406 | if (a->mode == b->mode || a->mode == o->mode) | |
1407 | result->mode = b->mode; | |
1408 | else { | |
1409 | /* must be the 100644/100755 case */ | |
1410 | assert(S_ISREG(a->mode)); | |
1411 | result->mode = a->mode; | |
1412 | clean = (b->mode == o->mode); | |
1413 | /* | |
1414 | * FIXME: If opt->priv->call_depth && !clean, then we really | |
1415 | * should not make result->mode match either a->mode or | |
1416 | * b->mode; that causes t6036 "check conflicting mode for | |
1417 | * regular file" to fail. It would be best to use some other | |
1418 | * mode, but we'll confuse all kinds of stuff if we use one | |
1419 | * where S_ISREG(result->mode) isn't true, and if we use | |
1420 | * something like 0100666, then tree-walk.c's calls to | |
1421 | * canon_mode() will just normalize that to 100644 for us and | |
1422 | * thus not solve anything. | |
1423 | * | |
1424 | * Figure out if there's some kind of way we can work around | |
1425 | * this... | |
1426 | */ | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * Trivial oid merge. | |
1431 | * | |
1432 | * Note: While one might assume that the next four lines would | |
1433 | * be unnecessary due to the fact that match_mask is often | |
1434 | * setup and already handled, renames don't always take care | |
1435 | * of that. | |
1436 | */ | |
1437 | if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid)) | |
1438 | oidcpy(&result->oid, &b->oid); | |
1439 | else if (oideq(&b->oid, &o->oid)) | |
1440 | oidcpy(&result->oid, &a->oid); | |
1441 | ||
1442 | /* Remaining rules depend on file vs. submodule vs. symlink. */ | |
1443 | else if (S_ISREG(a->mode)) { | |
1444 | mmbuffer_t result_buf; | |
1445 | int ret = 0, merge_status; | |
1446 | int two_way; | |
1447 | ||
1448 | /* | |
1449 | * If 'o' is different type, treat it as null so we do a | |
1450 | * two-way merge. | |
1451 | */ | |
1452 | two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
1453 | ||
1454 | merge_status = merge_3way(opt, path, | |
1455 | two_way ? &null_oid : &o->oid, | |
1456 | &a->oid, &b->oid, | |
1457 | pathnames, extra_marker_size, | |
1458 | &result_buf); | |
1459 | ||
1460 | if ((merge_status < 0) || !result_buf.ptr) | |
1461 | ret = err(opt, _("Failed to execute internal merge")); | |
1462 | ||
1463 | if (!ret && | |
1464 | write_object_file(result_buf.ptr, result_buf.size, | |
1465 | blob_type, &result->oid)) | |
1466 | ret = err(opt, _("Unable to add %s to database"), | |
1467 | path); | |
1468 | ||
1469 | free(result_buf.ptr); | |
1470 | if (ret) | |
1471 | return -1; | |
1472 | clean &= (merge_status == 0); | |
1473 | path_msg(opt, path, 1, _("Auto-merging %s"), path); | |
1474 | } else if (S_ISGITLINK(a->mode)) { | |
1475 | int two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
1476 | clean = merge_submodule(opt, pathnames[0], | |
1477 | two_way ? &null_oid : &o->oid, | |
1478 | &a->oid, &b->oid, &result->oid); | |
1479 | if (opt->priv->call_depth && two_way && !clean) { | |
1480 | result->mode = o->mode; | |
1481 | oidcpy(&result->oid, &o->oid); | |
1482 | } | |
1483 | } else if (S_ISLNK(a->mode)) { | |
1484 | if (opt->priv->call_depth) { | |
1485 | clean = 0; | |
1486 | result->mode = o->mode; | |
1487 | oidcpy(&result->oid, &o->oid); | |
1488 | } else { | |
1489 | switch (opt->recursive_variant) { | |
1490 | case MERGE_VARIANT_NORMAL: | |
1491 | clean = 0; | |
1492 | oidcpy(&result->oid, &a->oid); | |
1493 | break; | |
1494 | case MERGE_VARIANT_OURS: | |
1495 | oidcpy(&result->oid, &a->oid); | |
1496 | break; | |
1497 | case MERGE_VARIANT_THEIRS: | |
1498 | oidcpy(&result->oid, &b->oid); | |
1499 | break; | |
1500 | } | |
1501 | } | |
1502 | } else | |
1503 | BUG("unsupported object type in the tree: %06o for %s", | |
1504 | a->mode, path); | |
1505 | ||
991bbdca | 1506 | return clean; |
e2e9dc03 EN |
1507 | } |
1508 | ||
04af1879 EN |
1509 | /*** Function Grouping: functions related to detect_and_process_renames(), *** |
1510 | *** which are split into directory and regular rename detection sections. ***/ | |
1511 | ||
1512 | /*** Function Grouping: functions related to directory rename detection ***/ | |
1513 | ||
fa5e06d6 EN |
1514 | struct collision_info { |
1515 | struct string_list source_files; | |
1516 | unsigned reported_already:1; | |
1517 | }; | |
1518 | ||
d9d015df EN |
1519 | /* |
1520 | * Return a new string that replaces the beginning portion (which matches | |
1521 | * rename_info->key), with rename_info->util.new_dir. In perl-speak: | |
1522 | * new_path_name = (old_path =~ s/rename_info->key/rename_info->value/); | |
1523 | * NOTE: | |
1524 | * Caller must ensure that old_path starts with rename_info->key + '/'. | |
1525 | */ | |
1526 | static char *apply_dir_rename(struct strmap_entry *rename_info, | |
1527 | const char *old_path) | |
1528 | { | |
fbcfc0cc EN |
1529 | struct strbuf new_path = STRBUF_INIT; |
1530 | const char *old_dir = rename_info->key; | |
1531 | const char *new_dir = rename_info->value; | |
1532 | int oldlen, newlen, new_dir_len; | |
1533 | ||
1534 | oldlen = strlen(old_dir); | |
1535 | if (*new_dir == '\0') | |
1536 | /* | |
1537 | * If someone renamed/merged a subdirectory into the root | |
1538 | * directory (e.g. 'some/subdir' -> ''), then we want to | |
1539 | * avoid returning | |
1540 | * '' + '/filename' | |
1541 | * as the rename; we need to make old_path + oldlen advance | |
1542 | * past the '/' character. | |
1543 | */ | |
1544 | oldlen++; | |
1545 | new_dir_len = strlen(new_dir); | |
1546 | newlen = new_dir_len + (strlen(old_path) - oldlen) + 1; | |
1547 | strbuf_grow(&new_path, newlen); | |
1548 | strbuf_add(&new_path, new_dir, new_dir_len); | |
1549 | strbuf_addstr(&new_path, &old_path[oldlen]); | |
1550 | ||
1551 | return strbuf_detach(&new_path, NULL); | |
d9d015df EN |
1552 | } |
1553 | ||
bea43365 EN |
1554 | static int path_in_way(struct strmap *paths, const char *path, unsigned side_mask) |
1555 | { | |
1556 | struct merged_info *mi = strmap_get(paths, path); | |
1557 | struct conflict_info *ci; | |
1558 | if (!mi) | |
1559 | return 0; | |
1560 | INITIALIZE_CI(ci, mi); | |
1561 | return mi->clean || (side_mask & (ci->filemask | ci->dirmask)); | |
1562 | } | |
1563 | ||
47325e85 EN |
1564 | /* |
1565 | * See if there is a directory rename for path, and if there are any file | |
1566 | * level conflicts on the given side for the renamed location. If there is | |
1567 | * a rename and there are no conflicts, return the new name. Otherwise, | |
1568 | * return NULL. | |
1569 | */ | |
1570 | static char *handle_path_level_conflicts(struct merge_options *opt, | |
1571 | const char *path, | |
1572 | unsigned side_index, | |
1573 | struct strmap_entry *rename_info, | |
1574 | struct strmap *collisions) | |
1575 | { | |
bea43365 EN |
1576 | char *new_path = NULL; |
1577 | struct collision_info *c_info; | |
1578 | int clean = 1; | |
1579 | struct strbuf collision_paths = STRBUF_INIT; | |
1580 | ||
1581 | /* | |
1582 | * entry has the mapping of old directory name to new directory name | |
1583 | * that we want to apply to path. | |
1584 | */ | |
1585 | new_path = apply_dir_rename(rename_info, path); | |
1586 | if (!new_path) | |
1587 | BUG("Failed to apply directory rename!"); | |
1588 | ||
1589 | /* | |
1590 | * The caller needs to have ensured that it has pre-populated | |
1591 | * collisions with all paths that map to new_path. Do a quick check | |
1592 | * to ensure that's the case. | |
1593 | */ | |
1594 | c_info = strmap_get(collisions, new_path); | |
1595 | if (c_info == NULL) | |
1596 | BUG("c_info is NULL"); | |
1597 | ||
1598 | /* | |
1599 | * Check for one-sided add/add/.../add conflicts, i.e. | |
1600 | * where implicit renames from the other side doing | |
1601 | * directory rename(s) can affect this side of history | |
1602 | * to put multiple paths into the same location. Warn | |
1603 | * and bail on directory renames for such paths. | |
1604 | */ | |
1605 | if (c_info->reported_already) { | |
1606 | clean = 0; | |
1607 | } else if (path_in_way(&opt->priv->paths, new_path, 1 << side_index)) { | |
1608 | c_info->reported_already = 1; | |
1609 | strbuf_add_separated_string_list(&collision_paths, ", ", | |
1610 | &c_info->source_files); | |
1611 | path_msg(opt, new_path, 0, | |
1612 | _("CONFLICT (implicit dir rename): Existing file/dir " | |
1613 | "at %s in the way of implicit directory rename(s) " | |
1614 | "putting the following path(s) there: %s."), | |
1615 | new_path, collision_paths.buf); | |
1616 | clean = 0; | |
1617 | } else if (c_info->source_files.nr > 1) { | |
1618 | c_info->reported_already = 1; | |
1619 | strbuf_add_separated_string_list(&collision_paths, ", ", | |
1620 | &c_info->source_files); | |
1621 | path_msg(opt, new_path, 0, | |
1622 | _("CONFLICT (implicit dir rename): Cannot map more " | |
1623 | "than one path to %s; implicit directory renames " | |
1624 | "tried to put these paths there: %s"), | |
1625 | new_path, collision_paths.buf); | |
1626 | clean = 0; | |
1627 | } | |
1628 | ||
1629 | /* Free memory we no longer need */ | |
1630 | strbuf_release(&collision_paths); | |
1631 | if (!clean && new_path) { | |
1632 | free(new_path); | |
1633 | return NULL; | |
1634 | } | |
1635 | ||
1636 | return new_path; | |
47325e85 EN |
1637 | } |
1638 | ||
112e1112 EN |
1639 | static void get_provisional_directory_renames(struct merge_options *opt, |
1640 | unsigned side, | |
1641 | int *clean) | |
1642 | { | |
04264d40 EN |
1643 | struct hashmap_iter iter; |
1644 | struct strmap_entry *entry; | |
1645 | struct rename_info *renames = &opt->priv->renames; | |
1646 | ||
04264d40 EN |
1647 | /* |
1648 | * Collapse | |
1649 | * dir_rename_count: old_directory -> {new_directory -> count} | |
1650 | * down to | |
1651 | * dir_renames: old_directory -> best_new_directory | |
1652 | * where best_new_directory is the one with the unique highest count. | |
1653 | */ | |
1654 | strmap_for_each_entry(&renames->dir_rename_count[side], &iter, entry) { | |
1655 | const char *source_dir = entry->key; | |
1656 | struct strintmap *counts = entry->value; | |
1657 | struct hashmap_iter count_iter; | |
1658 | struct strmap_entry *count_entry; | |
1659 | int max = 0; | |
1660 | int bad_max = 0; | |
1661 | const char *best = NULL; | |
1662 | ||
1663 | strintmap_for_each_entry(counts, &count_iter, count_entry) { | |
1664 | const char *target_dir = count_entry->key; | |
1665 | intptr_t count = (intptr_t)count_entry->value; | |
1666 | ||
1667 | if (count == max) | |
1668 | bad_max = max; | |
1669 | else if (count > max) { | |
1670 | max = count; | |
1671 | best = target_dir; | |
1672 | } | |
1673 | } | |
1674 | ||
bf238b71 EN |
1675 | if (max == 0) |
1676 | continue; | |
1677 | ||
04264d40 EN |
1678 | if (bad_max == max) { |
1679 | path_msg(opt, source_dir, 0, | |
1680 | _("CONFLICT (directory rename split): " | |
1681 | "Unclear where to rename %s to; it was " | |
1682 | "renamed to multiple other directories, with " | |
1683 | "no destination getting a majority of the " | |
1684 | "files."), | |
1685 | source_dir); | |
41376b58 | 1686 | *clean = 0; |
04264d40 EN |
1687 | } else { |
1688 | strmap_put(&renames->dir_renames[side], | |
1689 | source_dir, (void*)best); | |
1690 | } | |
1691 | } | |
112e1112 EN |
1692 | } |
1693 | ||
1694 | static void handle_directory_level_conflicts(struct merge_options *opt) | |
1695 | { | |
98d0d081 EN |
1696 | struct hashmap_iter iter; |
1697 | struct strmap_entry *entry; | |
1698 | struct string_list duplicated = STRING_LIST_INIT_NODUP; | |
1699 | struct rename_info *renames = &opt->priv->renames; | |
1700 | struct strmap *side1_dir_renames = &renames->dir_renames[MERGE_SIDE1]; | |
1701 | struct strmap *side2_dir_renames = &renames->dir_renames[MERGE_SIDE2]; | |
1702 | int i; | |
1703 | ||
1704 | strmap_for_each_entry(side1_dir_renames, &iter, entry) { | |
1705 | if (strmap_contains(side2_dir_renames, entry->key)) | |
1706 | string_list_append(&duplicated, entry->key); | |
1707 | } | |
1708 | ||
1709 | for (i = 0; i < duplicated.nr; i++) { | |
1710 | strmap_remove(side1_dir_renames, duplicated.items[i].string, 0); | |
1711 | strmap_remove(side2_dir_renames, duplicated.items[i].string, 0); | |
1712 | } | |
1713 | string_list_clear(&duplicated, 0); | |
112e1112 EN |
1714 | } |
1715 | ||
d9d015df EN |
1716 | static struct strmap_entry *check_dir_renamed(const char *path, |
1717 | struct strmap *dir_renames) | |
1718 | { | |
fbcfc0cc EN |
1719 | char *temp = xstrdup(path); |
1720 | char *end; | |
1721 | struct strmap_entry *e = NULL; | |
1722 | ||
1723 | while ((end = strrchr(temp, '/'))) { | |
1724 | *end = '\0'; | |
1725 | e = strmap_get_entry(dir_renames, temp); | |
1726 | if (e) | |
1727 | break; | |
1728 | } | |
1729 | free(temp); | |
1730 | return e; | |
d9d015df EN |
1731 | } |
1732 | ||
fa5e06d6 EN |
1733 | static void compute_collisions(struct strmap *collisions, |
1734 | struct strmap *dir_renames, | |
1735 | struct diff_queue_struct *pairs) | |
1736 | { | |
d9d015df EN |
1737 | int i; |
1738 | ||
1739 | strmap_init_with_options(collisions, NULL, 0); | |
1740 | if (strmap_empty(dir_renames)) | |
1741 | return; | |
1742 | ||
1743 | /* | |
1744 | * Multiple files can be mapped to the same path due to directory | |
1745 | * renames done by the other side of history. Since that other | |
1746 | * side of history could have merged multiple directories into one, | |
1747 | * if our side of history added the same file basename to each of | |
1748 | * those directories, then all N of them would get implicitly | |
1749 | * renamed by the directory rename detection into the same path, | |
1750 | * and we'd get an add/add/.../add conflict, and all those adds | |
1751 | * from *this* side of history. This is not representable in the | |
1752 | * index, and users aren't going to easily be able to make sense of | |
1753 | * it. So we need to provide a good warning about what's | |
1754 | * happening, and fall back to no-directory-rename detection | |
1755 | * behavior for those paths. | |
1756 | * | |
1757 | * See testcases 9e and all of section 5 from t6043 for examples. | |
1758 | */ | |
1759 | for (i = 0; i < pairs->nr; ++i) { | |
1760 | struct strmap_entry *rename_info; | |
1761 | struct collision_info *collision_info; | |
1762 | char *new_path; | |
1763 | struct diff_filepair *pair = pairs->queue[i]; | |
1764 | ||
1765 | if (pair->status != 'A' && pair->status != 'R') | |
1766 | continue; | |
1767 | rename_info = check_dir_renamed(pair->two->path, dir_renames); | |
1768 | if (!rename_info) | |
1769 | continue; | |
1770 | ||
1771 | new_path = apply_dir_rename(rename_info, pair->two->path); | |
1772 | assert(new_path); | |
1773 | collision_info = strmap_get(collisions, new_path); | |
1774 | if (collision_info) { | |
1775 | free(new_path); | |
1776 | } else { | |
1777 | collision_info = xcalloc(1, | |
1778 | sizeof(struct collision_info)); | |
1779 | string_list_init(&collision_info->source_files, 0); | |
1780 | strmap_put(collisions, new_path, collision_info); | |
1781 | } | |
1782 | string_list_insert(&collision_info->source_files, | |
1783 | pair->two->path); | |
1784 | } | |
fa5e06d6 EN |
1785 | } |
1786 | ||
1787 | static char *check_for_directory_rename(struct merge_options *opt, | |
1788 | const char *path, | |
1789 | unsigned side_index, | |
1790 | struct strmap *dir_renames, | |
1791 | struct strmap *dir_rename_exclusions, | |
1792 | struct strmap *collisions, | |
1793 | int *clean_merge) | |
1794 | { | |
47325e85 EN |
1795 | char *new_path = NULL; |
1796 | struct strmap_entry *rename_info; | |
1797 | struct strmap_entry *otherinfo = NULL; | |
1798 | const char *new_dir; | |
1799 | ||
1800 | if (strmap_empty(dir_renames)) | |
1801 | return new_path; | |
1802 | rename_info = check_dir_renamed(path, dir_renames); | |
1803 | if (!rename_info) | |
1804 | return new_path; | |
1805 | /* old_dir = rename_info->key; */ | |
1806 | new_dir = rename_info->value; | |
1807 | ||
1808 | /* | |
1809 | * This next part is a little weird. We do not want to do an | |
1810 | * implicit rename into a directory we renamed on our side, because | |
1811 | * that will result in a spurious rename/rename(1to2) conflict. An | |
1812 | * example: | |
1813 | * Base commit: dumbdir/afile, otherdir/bfile | |
1814 | * Side 1: smrtdir/afile, otherdir/bfile | |
1815 | * Side 2: dumbdir/afile, dumbdir/bfile | |
1816 | * Here, while working on Side 1, we could notice that otherdir was | |
1817 | * renamed/merged to dumbdir, and change the diff_filepair for | |
1818 | * otherdir/bfile into a rename into dumbdir/bfile. However, Side | |
1819 | * 2 will notice the rename from dumbdir to smrtdir, and do the | |
1820 | * transitive rename to move it from dumbdir/bfile to | |
1821 | * smrtdir/bfile. That gives us bfile in dumbdir vs being in | |
1822 | * smrtdir, a rename/rename(1to2) conflict. We really just want | |
1823 | * the file to end up in smrtdir. And the way to achieve that is | |
1824 | * to not let Side1 do the rename to dumbdir, since we know that is | |
1825 | * the source of one of our directory renames. | |
1826 | * | |
1827 | * That's why otherinfo and dir_rename_exclusions is here. | |
1828 | * | |
1829 | * As it turns out, this also prevents N-way transient rename | |
1830 | * confusion; See testcases 9c and 9d of t6043. | |
1831 | */ | |
1832 | otherinfo = strmap_get_entry(dir_rename_exclusions, new_dir); | |
1833 | if (otherinfo) { | |
1834 | path_msg(opt, rename_info->key, 1, | |
1835 | _("WARNING: Avoiding applying %s -> %s rename " | |
1836 | "to %s, because %s itself was renamed."), | |
1837 | rename_info->key, new_dir, path, new_dir); | |
1838 | return NULL; | |
1839 | } | |
1840 | ||
1841 | new_path = handle_path_level_conflicts(opt, path, side_index, | |
1842 | rename_info, collisions); | |
1843 | *clean_merge &= (new_path != NULL); | |
1844 | ||
1845 | return new_path; | |
fa5e06d6 EN |
1846 | } |
1847 | ||
1848 | static void apply_directory_rename_modifications(struct merge_options *opt, | |
1849 | struct diff_filepair *pair, | |
1850 | char *new_path) | |
1851 | { | |
089d82bc EN |
1852 | /* |
1853 | * The basic idea is to get the conflict_info from opt->priv->paths | |
1854 | * at old path, and insert it into new_path; basically just this: | |
1855 | * ci = strmap_get(&opt->priv->paths, old_path); | |
1856 | * strmap_remove(&opt->priv->paths, old_path, 0); | |
1857 | * strmap_put(&opt->priv->paths, new_path, ci); | |
1858 | * However, there are some factors complicating this: | |
1859 | * - opt->priv->paths may already have an entry at new_path | |
1860 | * - Each ci tracks its containing directory, so we need to | |
1861 | * update that | |
1862 | * - If another ci has the same containing directory, then | |
1863 | * the two char*'s MUST point to the same location. See the | |
1864 | * comment in struct merged_info. strcmp equality is not | |
1865 | * enough; we need pointer equality. | |
1866 | * - opt->priv->paths must hold the parent directories of any | |
1867 | * entries that are added. So, if this directory rename | |
1868 | * causes entirely new directories, we must recursively add | |
1869 | * parent directories. | |
1870 | * - For each parent directory added to opt->priv->paths, we | |
1871 | * also need to get its parent directory stored in its | |
1872 | * conflict_info->merged.directory_name with all the same | |
1873 | * requirements about pointer equality. | |
1874 | */ | |
1875 | struct string_list dirs_to_insert = STRING_LIST_INIT_NODUP; | |
1876 | struct conflict_info *ci, *new_ci; | |
1877 | struct strmap_entry *entry; | |
1878 | const char *branch_with_new_path, *branch_with_dir_rename; | |
1879 | const char *old_path = pair->two->path; | |
1880 | const char *parent_name; | |
1881 | const char *cur_path; | |
1882 | int i, len; | |
1883 | ||
1884 | entry = strmap_get_entry(&opt->priv->paths, old_path); | |
1885 | old_path = entry->key; | |
1886 | ci = entry->value; | |
1887 | VERIFY_CI(ci); | |
1888 | ||
1889 | /* Find parent directories missing from opt->priv->paths */ | |
1890 | cur_path = new_path; | |
1891 | while (1) { | |
1892 | /* Find the parent directory of cur_path */ | |
1893 | char *last_slash = strrchr(cur_path, '/'); | |
1894 | if (last_slash) { | |
1895 | parent_name = xstrndup(cur_path, last_slash - cur_path); | |
1896 | } else { | |
1897 | parent_name = opt->priv->toplevel_dir; | |
1898 | break; | |
1899 | } | |
1900 | ||
1901 | /* Look it up in opt->priv->paths */ | |
1902 | entry = strmap_get_entry(&opt->priv->paths, parent_name); | |
1903 | if (entry) { | |
1904 | free((char*)parent_name); | |
1905 | parent_name = entry->key; /* reuse known pointer */ | |
1906 | break; | |
1907 | } | |
1908 | ||
1909 | /* Record this is one of the directories we need to insert */ | |
1910 | string_list_append(&dirs_to_insert, parent_name); | |
1911 | cur_path = parent_name; | |
1912 | } | |
1913 | ||
1914 | /* Traverse dirs_to_insert and insert them into opt->priv->paths */ | |
1915 | for (i = dirs_to_insert.nr-1; i >= 0; --i) { | |
1916 | struct conflict_info *dir_ci; | |
1917 | char *cur_dir = dirs_to_insert.items[i].string; | |
1918 | ||
1919 | dir_ci = xcalloc(1, sizeof(*dir_ci)); | |
1920 | ||
1921 | dir_ci->merged.directory_name = parent_name; | |
1922 | len = strlen(parent_name); | |
1923 | /* len+1 because of trailing '/' character */ | |
1924 | dir_ci->merged.basename_offset = (len > 0 ? len+1 : len); | |
1925 | dir_ci->dirmask = ci->filemask; | |
1926 | strmap_put(&opt->priv->paths, cur_dir, dir_ci); | |
1927 | ||
1928 | parent_name = cur_dir; | |
1929 | } | |
1930 | ||
1931 | /* | |
1932 | * We are removing old_path from opt->priv->paths. old_path also will | |
1933 | * eventually need to be freed, but it may still be used by e.g. | |
1934 | * ci->pathnames. So, store it in another string-list for now. | |
1935 | */ | |
1936 | string_list_append(&opt->priv->paths_to_free, old_path); | |
1937 | ||
1938 | assert(ci->filemask == 2 || ci->filemask == 4); | |
1939 | assert(ci->dirmask == 0); | |
1940 | strmap_remove(&opt->priv->paths, old_path, 0); | |
1941 | ||
1942 | branch_with_new_path = (ci->filemask == 2) ? opt->branch1 : opt->branch2; | |
1943 | branch_with_dir_rename = (ci->filemask == 2) ? opt->branch2 : opt->branch1; | |
1944 | ||
1945 | /* Now, finally update ci and stick it into opt->priv->paths */ | |
1946 | ci->merged.directory_name = parent_name; | |
1947 | len = strlen(parent_name); | |
1948 | ci->merged.basename_offset = (len > 0 ? len+1 : len); | |
1949 | new_ci = strmap_get(&opt->priv->paths, new_path); | |
1950 | if (!new_ci) { | |
1951 | /* Place ci back into opt->priv->paths, but at new_path */ | |
1952 | strmap_put(&opt->priv->paths, new_path, ci); | |
1953 | } else { | |
1954 | int index; | |
1955 | ||
1956 | /* A few sanity checks */ | |
1957 | VERIFY_CI(new_ci); | |
1958 | assert(ci->filemask == 2 || ci->filemask == 4); | |
1959 | assert((new_ci->filemask & ci->filemask) == 0); | |
1960 | assert(!new_ci->merged.clean); | |
1961 | ||
1962 | /* Copy stuff from ci into new_ci */ | |
1963 | new_ci->filemask |= ci->filemask; | |
1964 | if (new_ci->dirmask) | |
1965 | new_ci->df_conflict = 1; | |
1966 | index = (ci->filemask >> 1); | |
1967 | new_ci->pathnames[index] = ci->pathnames[index]; | |
1968 | new_ci->stages[index].mode = ci->stages[index].mode; | |
1969 | oidcpy(&new_ci->stages[index].oid, &ci->stages[index].oid); | |
1970 | ||
1971 | free(ci); | |
1972 | ci = new_ci; | |
1973 | } | |
1974 | ||
1975 | if (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) { | |
1976 | /* Notify user of updated path */ | |
1977 | if (pair->status == 'A') | |
1978 | path_msg(opt, new_path, 1, | |
1979 | _("Path updated: %s added in %s inside a " | |
1980 | "directory that was renamed in %s; moving " | |
1981 | "it to %s."), | |
1982 | old_path, branch_with_new_path, | |
1983 | branch_with_dir_rename, new_path); | |
1984 | else | |
1985 | path_msg(opt, new_path, 1, | |
1986 | _("Path updated: %s renamed to %s in %s, " | |
1987 | "inside a directory that was renamed in %s; " | |
1988 | "moving it to %s."), | |
1989 | pair->one->path, old_path, branch_with_new_path, | |
1990 | branch_with_dir_rename, new_path); | |
1991 | } else { | |
1992 | /* | |
1993 | * opt->detect_directory_renames has the value | |
1994 | * MERGE_DIRECTORY_RENAMES_CONFLICT, so mark these as conflicts. | |
1995 | */ | |
1996 | ci->path_conflict = 1; | |
1997 | if (pair->status == 'A') | |
1998 | path_msg(opt, new_path, 0, | |
1999 | _("CONFLICT (file location): %s added in %s " | |
2000 | "inside a directory that was renamed in %s, " | |
2001 | "suggesting it should perhaps be moved to " | |
2002 | "%s."), | |
2003 | old_path, branch_with_new_path, | |
2004 | branch_with_dir_rename, new_path); | |
2005 | else | |
2006 | path_msg(opt, new_path, 0, | |
2007 | _("CONFLICT (file location): %s renamed to %s " | |
2008 | "in %s, inside a directory that was renamed " | |
2009 | "in %s, suggesting it should perhaps be " | |
2010 | "moved to %s."), | |
2011 | pair->one->path, old_path, branch_with_new_path, | |
2012 | branch_with_dir_rename, new_path); | |
2013 | } | |
2014 | ||
2015 | /* | |
2016 | * Finally, record the new location. | |
2017 | */ | |
2018 | pair->two->path = new_path; | |
fa5e06d6 EN |
2019 | } |
2020 | ||
04af1879 EN |
2021 | /*** Function Grouping: functions related to regular rename detection ***/ |
2022 | ||
e1a124e8 EN |
2023 | static int process_renames(struct merge_options *opt, |
2024 | struct diff_queue_struct *renames) | |
2025 | { | |
c2d267df EN |
2026 | int clean_merge = 1, i; |
2027 | ||
2028 | for (i = 0; i < renames->nr; ++i) { | |
2029 | const char *oldpath = NULL, *newpath; | |
2030 | struct diff_filepair *pair = renames->queue[i]; | |
2031 | struct conflict_info *oldinfo = NULL, *newinfo = NULL; | |
2032 | struct strmap_entry *old_ent, *new_ent; | |
2033 | unsigned int old_sidemask; | |
2034 | int target_index, other_source_index; | |
2035 | int source_deleted, collision, type_changed; | |
2e91ddd2 | 2036 | const char *rename_branch = NULL, *delete_branch = NULL; |
c2d267df EN |
2037 | |
2038 | old_ent = strmap_get_entry(&opt->priv->paths, pair->one->path); | |
c2d267df | 2039 | new_ent = strmap_get_entry(&opt->priv->paths, pair->two->path); |
1b6b902d EN |
2040 | if (old_ent) { |
2041 | oldpath = old_ent->key; | |
2042 | oldinfo = old_ent->value; | |
2043 | } | |
2044 | newpath = pair->two->path; | |
2045 | if (new_ent) { | |
2046 | newpath = new_ent->key; | |
2047 | newinfo = new_ent->value; | |
2048 | } | |
2049 | ||
2050 | /* | |
2051 | * If pair->one->path isn't in opt->priv->paths, that means | |
2052 | * that either directory rename detection removed that | |
2053 | * path, or a parent directory of oldpath was resolved and | |
2054 | * we don't even need the rename; in either case, we can | |
2055 | * skip it. If oldinfo->merged.clean, then the other side | |
2056 | * of history had no changes to oldpath and we don't need | |
2057 | * the rename and can skip it. | |
2058 | */ | |
2059 | if (!oldinfo || oldinfo->merged.clean) | |
2060 | continue; | |
c2d267df EN |
2061 | |
2062 | /* | |
2063 | * diff_filepairs have copies of pathnames, thus we have to | |
2064 | * use standard 'strcmp()' (negated) instead of '=='. | |
2065 | */ | |
2066 | if (i + 1 < renames->nr && | |
2067 | !strcmp(oldpath, renames->queue[i+1]->one->path)) { | |
2068 | /* Handle rename/rename(1to2) or rename/rename(1to1) */ | |
2069 | const char *pathnames[3]; | |
af1e56c4 EN |
2070 | struct version_info merged; |
2071 | struct conflict_info *base, *side1, *side2; | |
53e88a03 | 2072 | unsigned was_binary_blob = 0; |
c2d267df EN |
2073 | |
2074 | pathnames[0] = oldpath; | |
2075 | pathnames[1] = newpath; | |
2076 | pathnames[2] = renames->queue[i+1]->two->path; | |
2077 | ||
af1e56c4 EN |
2078 | base = strmap_get(&opt->priv->paths, pathnames[0]); |
2079 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
2080 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
2081 | ||
2082 | VERIFY_CI(base); | |
2083 | VERIFY_CI(side1); | |
2084 | VERIFY_CI(side2); | |
2085 | ||
c2d267df | 2086 | if (!strcmp(pathnames[1], pathnames[2])) { |
af1e56c4 EN |
2087 | /* Both sides renamed the same way */ |
2088 | assert(side1 == side2); | |
2089 | memcpy(&side1->stages[0], &base->stages[0], | |
2090 | sizeof(merged)); | |
2091 | side1->filemask |= (1 << MERGE_BASE); | |
2092 | /* Mark base as resolved by removal */ | |
2093 | base->merged.is_null = 1; | |
2094 | base->merged.clean = 1; | |
c2d267df EN |
2095 | |
2096 | /* We handled both renames, i.e. i+1 handled */ | |
2097 | i++; | |
2098 | /* Move to next rename */ | |
2099 | continue; | |
2100 | } | |
2101 | ||
2102 | /* This is a rename/rename(1to2) */ | |
53e88a03 EN |
2103 | clean_merge = handle_content_merge(opt, |
2104 | pair->one->path, | |
2105 | &base->stages[0], | |
2106 | &side1->stages[1], | |
2107 | &side2->stages[2], | |
2108 | pathnames, | |
2109 | 1 + 2 * opt->priv->call_depth, | |
2110 | &merged); | |
2111 | if (!clean_merge && | |
2112 | merged.mode == side1->stages[1].mode && | |
2113 | oideq(&merged.oid, &side1->stages[1].oid)) | |
2114 | was_binary_blob = 1; | |
2115 | memcpy(&side1->stages[1], &merged, sizeof(merged)); | |
2116 | if (was_binary_blob) { | |
2117 | /* | |
2118 | * Getting here means we were attempting to | |
2119 | * merge a binary blob. | |
2120 | * | |
2121 | * Since we can't merge binaries, | |
2122 | * handle_content_merge() just takes one | |
2123 | * side. But we don't want to copy the | |
2124 | * contents of one side to both paths. We | |
2125 | * used the contents of side1 above for | |
2126 | * side1->stages, let's use the contents of | |
2127 | * side2 for side2->stages below. | |
2128 | */ | |
2129 | oidcpy(&merged.oid, &side2->stages[2].oid); | |
2130 | merged.mode = side2->stages[2].mode; | |
2131 | } | |
2132 | memcpy(&side2->stages[2], &merged, sizeof(merged)); | |
2133 | ||
2134 | side1->path_conflict = 1; | |
2135 | side2->path_conflict = 1; | |
2136 | /* | |
2137 | * TODO: For renames we normally remove the path at the | |
2138 | * old name. It would thus seem consistent to do the | |
2139 | * same for rename/rename(1to2) cases, but we haven't | |
2140 | * done so traditionally and a number of the regression | |
2141 | * tests now encode an expectation that the file is | |
2142 | * left there at stage 1. If we ever decide to change | |
2143 | * this, add the following two lines here: | |
2144 | * base->merged.is_null = 1; | |
2145 | * base->merged.clean = 1; | |
2146 | * and remove the setting of base->path_conflict to 1. | |
2147 | */ | |
2148 | base->path_conflict = 1; | |
2149 | path_msg(opt, oldpath, 0, | |
2150 | _("CONFLICT (rename/rename): %s renamed to " | |
2151 | "%s in %s and to %s in %s."), | |
2152 | pathnames[0], | |
2153 | pathnames[1], opt->branch1, | |
2154 | pathnames[2], opt->branch2); | |
c2d267df EN |
2155 | |
2156 | i++; /* We handled both renames, i.e. i+1 handled */ | |
2157 | continue; | |
2158 | } | |
2159 | ||
2160 | VERIFY_CI(oldinfo); | |
2161 | VERIFY_CI(newinfo); | |
2162 | target_index = pair->score; /* from collect_renames() */ | |
2163 | assert(target_index == 1 || target_index == 2); | |
2164 | other_source_index = 3 - target_index; | |
2165 | old_sidemask = (1 << other_source_index); /* 2 or 4 */ | |
2166 | source_deleted = (oldinfo->filemask == 1); | |
2167 | collision = ((newinfo->filemask & old_sidemask) != 0); | |
2168 | type_changed = !source_deleted && | |
2169 | (S_ISREG(oldinfo->stages[other_source_index].mode) != | |
2170 | S_ISREG(newinfo->stages[target_index].mode)); | |
2171 | if (type_changed && collision) { | |
6fcccbd7 EN |
2172 | /* |
2173 | * special handling so later blocks can handle this... | |
2174 | * | |
2175 | * if type_changed && collision are both true, then this | |
2176 | * was really a double rename, but one side wasn't | |
2177 | * detected due to lack of break detection. I.e. | |
2178 | * something like | |
2179 | * orig: has normal file 'foo' | |
2180 | * side1: renames 'foo' to 'bar', adds 'foo' symlink | |
2181 | * side2: renames 'foo' to 'bar' | |
2182 | * In this case, the foo->bar rename on side1 won't be | |
2183 | * detected because the new symlink named 'foo' is | |
2184 | * there and we don't do break detection. But we detect | |
2185 | * this here because we don't want to merge the content | |
2186 | * of the foo symlink with the foo->bar file, so we | |
2187 | * have some logic to handle this special case. The | |
2188 | * easiest way to do that is make 'bar' on side1 not | |
2189 | * be considered a colliding file but the other part | |
2190 | * of a normal rename. If the file is very different, | |
2191 | * well we're going to get content merge conflicts | |
2192 | * anyway so it doesn't hurt. And if the colliding | |
2193 | * file also has a different type, that'll be handled | |
2194 | * by the content merge logic in process_entry() too. | |
2195 | * | |
2196 | * See also t6430, 'rename vs. rename/symlink' | |
2197 | */ | |
2198 | collision = 0; | |
c2d267df | 2199 | } |
2e91ddd2 EN |
2200 | if (source_deleted) { |
2201 | if (target_index == 1) { | |
2202 | rename_branch = opt->branch1; | |
2203 | delete_branch = opt->branch2; | |
2204 | } else { | |
2205 | rename_branch = opt->branch2; | |
2206 | delete_branch = opt->branch1; | |
2207 | } | |
2208 | } | |
c2d267df EN |
2209 | |
2210 | assert(source_deleted || oldinfo->filemask & old_sidemask); | |
2211 | ||
2212 | /* Need to check for special types of rename conflicts... */ | |
2213 | if (collision && !source_deleted) { | |
2214 | /* collision: rename/add or rename/rename(2to1) */ | |
35e47e35 EN |
2215 | const char *pathnames[3]; |
2216 | struct version_info merged; | |
2217 | ||
2218 | struct conflict_info *base, *side1, *side2; | |
2219 | unsigned clean; | |
2220 | ||
2221 | pathnames[0] = oldpath; | |
2222 | pathnames[other_source_index] = oldpath; | |
2223 | pathnames[target_index] = newpath; | |
2224 | ||
2225 | base = strmap_get(&opt->priv->paths, pathnames[0]); | |
2226 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
2227 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
2228 | ||
2229 | VERIFY_CI(base); | |
2230 | VERIFY_CI(side1); | |
2231 | VERIFY_CI(side2); | |
2232 | ||
2233 | clean = handle_content_merge(opt, pair->one->path, | |
2234 | &base->stages[0], | |
2235 | &side1->stages[1], | |
2236 | &side2->stages[2], | |
2237 | pathnames, | |
2238 | 1 + 2 * opt->priv->call_depth, | |
2239 | &merged); | |
2240 | ||
2241 | memcpy(&newinfo->stages[target_index], &merged, | |
2242 | sizeof(merged)); | |
2243 | if (!clean) { | |
2244 | path_msg(opt, newpath, 0, | |
2245 | _("CONFLICT (rename involved in " | |
2246 | "collision): rename of %s -> %s has " | |
2247 | "content conflicts AND collides " | |
2248 | "with another path; this may result " | |
2249 | "in nested conflict markers."), | |
2250 | oldpath, newpath); | |
2251 | } | |
c2d267df | 2252 | } else if (collision && source_deleted) { |
35e47e35 EN |
2253 | /* |
2254 | * rename/add/delete or rename/rename(2to1)/delete: | |
2255 | * since oldpath was deleted on the side that didn't | |
2256 | * do the rename, there's not much of a content merge | |
2257 | * we can do for the rename. oldinfo->merged.is_null | |
2258 | * was already set, so we just leave things as-is so | |
2259 | * they look like an add/add conflict. | |
2260 | */ | |
2261 | ||
2262 | newinfo->path_conflict = 1; | |
2263 | path_msg(opt, newpath, 0, | |
2264 | _("CONFLICT (rename/delete): %s renamed " | |
2265 | "to %s in %s, but deleted in %s."), | |
2266 | oldpath, newpath, rename_branch, delete_branch); | |
c2d267df | 2267 | } else { |
2e91ddd2 EN |
2268 | /* |
2269 | * a few different cases...start by copying the | |
2270 | * existing stage(s) from oldinfo over the newinfo | |
2271 | * and update the pathname(s). | |
2272 | */ | |
2273 | memcpy(&newinfo->stages[0], &oldinfo->stages[0], | |
2274 | sizeof(newinfo->stages[0])); | |
2275 | newinfo->filemask |= (1 << MERGE_BASE); | |
2276 | newinfo->pathnames[0] = oldpath; | |
c2d267df EN |
2277 | if (type_changed) { |
2278 | /* rename vs. typechange */ | |
6fcccbd7 EN |
2279 | /* Mark the original as resolved by removal */ |
2280 | memcpy(&oldinfo->stages[0].oid, &null_oid, | |
2281 | sizeof(oldinfo->stages[0].oid)); | |
2282 | oldinfo->stages[0].mode = 0; | |
2283 | oldinfo->filemask &= 0x06; | |
c2d267df EN |
2284 | } else if (source_deleted) { |
2285 | /* rename/delete */ | |
2e91ddd2 EN |
2286 | newinfo->path_conflict = 1; |
2287 | path_msg(opt, newpath, 0, | |
2288 | _("CONFLICT (rename/delete): %s renamed" | |
2289 | " to %s in %s, but deleted in %s."), | |
2290 | oldpath, newpath, | |
2291 | rename_branch, delete_branch); | |
c2d267df EN |
2292 | } else { |
2293 | /* normal rename */ | |
f1665e69 EN |
2294 | memcpy(&newinfo->stages[other_source_index], |
2295 | &oldinfo->stages[other_source_index], | |
2296 | sizeof(newinfo->stages[0])); | |
2297 | newinfo->filemask |= (1 << other_source_index); | |
2298 | newinfo->pathnames[other_source_index] = oldpath; | |
c2d267df EN |
2299 | } |
2300 | } | |
2301 | ||
2302 | if (!type_changed) { | |
2303 | /* Mark the original as resolved by removal */ | |
2304 | oldinfo->merged.is_null = 1; | |
2305 | oldinfo->merged.clean = 1; | |
2306 | } | |
2307 | ||
2308 | } | |
2309 | ||
2310 | return clean_merge; | |
e1a124e8 EN |
2311 | } |
2312 | ||
f89b4f2b EN |
2313 | static inline int possible_side_renames(struct rename_info *renames, |
2314 | unsigned side_index) | |
2315 | { | |
2316 | return renames->pairs[side_index].nr > 0 && | |
a49b55d5 | 2317 | !strintmap_empty(&renames->relevant_sources[side_index]); |
f89b4f2b EN |
2318 | } |
2319 | ||
2320 | static inline int possible_renames(struct rename_info *renames) | |
2321 | { | |
2322 | return possible_side_renames(renames, 1) || | |
2323 | possible_side_renames(renames, 2); | |
2324 | } | |
2325 | ||
f78cf976 EN |
2326 | static void resolve_diffpair_statuses(struct diff_queue_struct *q) |
2327 | { | |
2328 | /* | |
2329 | * A simplified version of diff_resolve_rename_copy(); would probably | |
2330 | * just use that function but it's static... | |
2331 | */ | |
2332 | int i; | |
2333 | struct diff_filepair *p; | |
2334 | ||
2335 | for (i = 0; i < q->nr; ++i) { | |
2336 | p = q->queue[i]; | |
2337 | p->status = 0; /* undecided */ | |
2338 | if (!DIFF_FILE_VALID(p->one)) | |
2339 | p->status = DIFF_STATUS_ADDED; | |
2340 | else if (!DIFF_FILE_VALID(p->two)) | |
2341 | p->status = DIFF_STATUS_DELETED; | |
2342 | else if (DIFF_PAIR_RENAME(p)) | |
2343 | p->status = DIFF_STATUS_RENAMED; | |
2344 | } | |
2345 | } | |
2346 | ||
e1a124e8 EN |
2347 | static int compare_pairs(const void *a_, const void *b_) |
2348 | { | |
965a7bc2 EN |
2349 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
2350 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
2351 | ||
2352 | return strcmp(a->one->path, b->one->path); | |
e1a124e8 EN |
2353 | } |
2354 | ||
2355 | /* Call diffcore_rename() to compute which files have changed on given side */ | |
2356 | static void detect_regular_renames(struct merge_options *opt, | |
e1a124e8 EN |
2357 | unsigned side_index) |
2358 | { | |
f39d05ca EN |
2359 | struct diff_options diff_opts; |
2360 | struct rename_info *renames = &opt->priv->renames; | |
2361 | ||
f89b4f2b EN |
2362 | if (!possible_side_renames(renames, side_index)) { |
2363 | /* | |
2364 | * No rename detection needed for this side, but we still need | |
2365 | * to make sure 'adds' are marked correctly in case the other | |
2366 | * side had directory renames. | |
2367 | */ | |
2368 | resolve_diffpair_statuses(&renames->pairs[side_index]); | |
2369 | return; | |
2370 | } | |
2371 | ||
f39d05ca EN |
2372 | repo_diff_setup(opt->repo, &diff_opts); |
2373 | diff_opts.flags.recursive = 1; | |
2374 | diff_opts.flags.rename_empty = 0; | |
2375 | diff_opts.detect_rename = DIFF_DETECT_RENAME; | |
2376 | diff_opts.rename_limit = opt->rename_limit; | |
2377 | if (opt->rename_limit <= 0) | |
2378 | diff_opts.rename_limit = 1000; | |
2379 | diff_opts.rename_score = opt->rename_score; | |
2380 | diff_opts.show_rename_progress = opt->show_rename_progress; | |
2381 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
2382 | diff_setup_done(&diff_opts); | |
557ac035 | 2383 | |
f78cf976 | 2384 | diff_queued_diff = renames->pairs[side_index]; |
557ac035 | 2385 | trace2_region_enter("diff", "diffcore_rename", opt->repo); |
0c4fd732 | 2386 | diffcore_rename_extended(&diff_opts, |
174791f0 | 2387 | &renames->relevant_sources[side_index], |
0c4fd732 EN |
2388 | &renames->dirs_removed[side_index], |
2389 | &renames->dir_rename_count[side_index]); | |
557ac035 | 2390 | trace2_region_leave("diff", "diffcore_rename", opt->repo); |
f78cf976 | 2391 | resolve_diffpair_statuses(&diff_queued_diff); |
f39d05ca EN |
2392 | |
2393 | if (diff_opts.needed_rename_limit > renames->needed_limit) | |
2394 | renames->needed_limit = diff_opts.needed_rename_limit; | |
2395 | ||
2396 | renames->pairs[side_index] = diff_queued_diff; | |
2397 | ||
2398 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
2399 | diff_queued_diff.nr = 0; | |
2400 | diff_queued_diff.queue = NULL; | |
2401 | diff_flush(&diff_opts); | |
e1a124e8 EN |
2402 | } |
2403 | ||
2404 | /* | |
2405 | * Get information of all renames which occurred in 'side_pairs', discarding | |
2406 | * non-renames. | |
2407 | */ | |
2408 | static int collect_renames(struct merge_options *opt, | |
2409 | struct diff_queue_struct *result, | |
fa5e06d6 EN |
2410 | unsigned side_index, |
2411 | struct strmap *dir_renames_for_side, | |
2412 | struct strmap *rename_exclusions) | |
e1a124e8 | 2413 | { |
965a7bc2 | 2414 | int i, clean = 1; |
fa5e06d6 | 2415 | struct strmap collisions; |
965a7bc2 | 2416 | struct diff_queue_struct *side_pairs; |
fa5e06d6 EN |
2417 | struct hashmap_iter iter; |
2418 | struct strmap_entry *entry; | |
965a7bc2 EN |
2419 | struct rename_info *renames = &opt->priv->renames; |
2420 | ||
2421 | side_pairs = &renames->pairs[side_index]; | |
fa5e06d6 | 2422 | compute_collisions(&collisions, dir_renames_for_side, side_pairs); |
965a7bc2 EN |
2423 | |
2424 | for (i = 0; i < side_pairs->nr; ++i) { | |
2425 | struct diff_filepair *p = side_pairs->queue[i]; | |
fa5e06d6 | 2426 | char *new_path; /* non-NULL only with directory renames */ |
965a7bc2 | 2427 | |
fa5e06d6 | 2428 | if (p->status != 'A' && p->status != 'R') { |
965a7bc2 EN |
2429 | diff_free_filepair(p); |
2430 | continue; | |
2431 | } | |
2432 | ||
fa5e06d6 EN |
2433 | new_path = check_for_directory_rename(opt, p->two->path, |
2434 | side_index, | |
2435 | dir_renames_for_side, | |
2436 | rename_exclusions, | |
2437 | &collisions, | |
2438 | &clean); | |
2439 | ||
2440 | if (p->status != 'R' && !new_path) { | |
2441 | diff_free_filepair(p); | |
2442 | continue; | |
2443 | } | |
2444 | ||
2445 | if (new_path) | |
2446 | apply_directory_rename_modifications(opt, p, new_path); | |
2447 | ||
965a7bc2 EN |
2448 | /* |
2449 | * p->score comes back from diffcore_rename_extended() with | |
2450 | * the similarity of the renamed file. The similarity is | |
2451 | * was used to determine that the two files were related | |
2452 | * and are a rename, which we have already used, but beyond | |
2453 | * that we have no use for the similarity. So p->score is | |
2454 | * now irrelevant. However, process_renames() will need to | |
2455 | * know which side of the merge this rename was associated | |
2456 | * with, so overwrite p->score with that value. | |
2457 | */ | |
2458 | p->score = side_index; | |
2459 | result->queue[result->nr++] = p; | |
2460 | } | |
2461 | ||
fa5e06d6 EN |
2462 | /* Free each value in the collisions map */ |
2463 | strmap_for_each_entry(&collisions, &iter, entry) { | |
2464 | struct collision_info *info = entry->value; | |
2465 | string_list_clear(&info->source_files, 0); | |
2466 | } | |
2467 | /* | |
2468 | * In compute_collisions(), we set collisions.strdup_strings to 0 | |
2469 | * so that we wouldn't have to make another copy of the new_path | |
2470 | * allocated by apply_dir_rename(). But now that we've used them | |
2471 | * and have no other references to these strings, it is time to | |
2472 | * deallocate them. | |
2473 | */ | |
2474 | free_strmap_strings(&collisions); | |
2475 | strmap_clear(&collisions, 1); | |
965a7bc2 | 2476 | return clean; |
e1a124e8 EN |
2477 | } |
2478 | ||
231e2dd4 EN |
2479 | static int detect_and_process_renames(struct merge_options *opt, |
2480 | struct tree *merge_base, | |
2481 | struct tree *side1, | |
2482 | struct tree *side2) | |
2483 | { | |
e1a124e8 EN |
2484 | struct diff_queue_struct combined; |
2485 | struct rename_info *renames = &opt->priv->renames; | |
112e1112 | 2486 | int need_dir_renames, s, clean = 1; |
e1a124e8 EN |
2487 | |
2488 | memset(&combined, 0, sizeof(combined)); | |
f89b4f2b EN |
2489 | if (!possible_renames(renames)) |
2490 | goto cleanup; | |
e1a124e8 | 2491 | |
557ac035 | 2492 | trace2_region_enter("merge", "regular renames", opt->repo); |
f78cf976 EN |
2493 | detect_regular_renames(opt, MERGE_SIDE1); |
2494 | detect_regular_renames(opt, MERGE_SIDE2); | |
557ac035 | 2495 | trace2_region_leave("merge", "regular renames", opt->repo); |
e1a124e8 | 2496 | |
557ac035 | 2497 | trace2_region_enter("merge", "directory renames", opt->repo); |
112e1112 EN |
2498 | need_dir_renames = |
2499 | !opt->priv->call_depth && | |
2500 | (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE || | |
2501 | opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT); | |
2502 | ||
2503 | if (need_dir_renames) { | |
2504 | get_provisional_directory_renames(opt, MERGE_SIDE1, &clean); | |
2505 | get_provisional_directory_renames(opt, MERGE_SIDE2, &clean); | |
2506 | handle_directory_level_conflicts(opt); | |
2507 | } | |
2508 | ||
e1a124e8 EN |
2509 | ALLOC_GROW(combined.queue, |
2510 | renames->pairs[1].nr + renames->pairs[2].nr, | |
2511 | combined.alloc); | |
fa5e06d6 EN |
2512 | clean &= collect_renames(opt, &combined, MERGE_SIDE1, |
2513 | &renames->dir_renames[2], | |
2514 | &renames->dir_renames[1]); | |
2515 | clean &= collect_renames(opt, &combined, MERGE_SIDE2, | |
2516 | &renames->dir_renames[1], | |
2517 | &renames->dir_renames[2]); | |
72b30910 | 2518 | STABLE_QSORT(combined.queue, combined.nr, compare_pairs); |
557ac035 | 2519 | trace2_region_leave("merge", "directory renames", opt->repo); |
e1a124e8 | 2520 | |
557ac035 | 2521 | trace2_region_enter("merge", "process renames", opt->repo); |
e1a124e8 | 2522 | clean &= process_renames(opt, &combined); |
557ac035 | 2523 | trace2_region_leave("merge", "process renames", opt->repo); |
e1a124e8 | 2524 | |
f89b4f2b EN |
2525 | goto simple_cleanup; /* collect_renames() handles some of cleanup */ |
2526 | ||
2527 | cleanup: | |
2528 | /* | |
2529 | * Free now unneeded filepairs, which would have been handled | |
2530 | * in collect_renames() normally but we skipped that code. | |
2531 | */ | |
2532 | for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) { | |
2533 | struct diff_queue_struct *side_pairs; | |
2534 | int i; | |
2535 | ||
2536 | side_pairs = &renames->pairs[s]; | |
2537 | for (i = 0; i < side_pairs->nr; ++i) { | |
2538 | struct diff_filepair *p = side_pairs->queue[i]; | |
2539 | diff_free_filepair(p); | |
2540 | } | |
2541 | } | |
2542 | ||
2543 | simple_cleanup: | |
e1a124e8 EN |
2544 | /* Free memory for renames->pairs[] and combined */ |
2545 | for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) { | |
2546 | free(renames->pairs[s].queue); | |
2547 | DIFF_QUEUE_CLEAR(&renames->pairs[s]); | |
2548 | } | |
2549 | if (combined.nr) { | |
2550 | int i; | |
2551 | for (i = 0; i < combined.nr; i++) | |
2552 | diff_free_filepair(combined.queue[i]); | |
2553 | free(combined.queue); | |
2554 | } | |
231e2dd4 | 2555 | |
231e2dd4 EN |
2556 | return clean; |
2557 | } | |
2558 | ||
04af1879 EN |
2559 | /*** Function Grouping: functions related to process_entries() ***/ |
2560 | ||
8adffaa8 EN |
2561 | static int string_list_df_name_compare(const char *one, const char *two) |
2562 | { | |
2563 | int onelen = strlen(one); | |
2564 | int twolen = strlen(two); | |
2565 | /* | |
2566 | * Here we only care that entries for D/F conflicts are | |
2567 | * adjacent, in particular with the file of the D/F conflict | |
2568 | * appearing before files below the corresponding directory. | |
2569 | * The order of the rest of the list is irrelevant for us. | |
2570 | * | |
2571 | * To achieve this, we sort with df_name_compare and provide | |
2572 | * the mode S_IFDIR so that D/F conflicts will sort correctly. | |
2573 | * We use the mode S_IFDIR for everything else for simplicity, | |
2574 | * since in other cases any changes in their order due to | |
2575 | * sorting cause no problems for us. | |
2576 | */ | |
2577 | int cmp = df_name_compare(one, onelen, S_IFDIR, | |
2578 | two, twolen, S_IFDIR); | |
2579 | /* | |
2580 | * Now that 'foo' and 'foo/bar' compare equal, we have to make sure | |
2581 | * that 'foo' comes before 'foo/bar'. | |
2582 | */ | |
2583 | if (cmp) | |
2584 | return cmp; | |
2585 | return onelen - twolen; | |
2586 | } | |
2587 | ||
3860220b EN |
2588 | static int read_oid_strbuf(struct merge_options *opt, |
2589 | const struct object_id *oid, | |
2590 | struct strbuf *dst) | |
2591 | { | |
2592 | void *buf; | |
2593 | enum object_type type; | |
2594 | unsigned long size; | |
2595 | buf = read_object_file(oid, &type, &size); | |
2596 | if (!buf) | |
2597 | return err(opt, _("cannot read object %s"), oid_to_hex(oid)); | |
2598 | if (type != OBJ_BLOB) { | |
2599 | free(buf); | |
2600 | return err(opt, _("object %s is not a blob"), oid_to_hex(oid)); | |
2601 | } | |
2602 | strbuf_attach(dst, buf, size, size + 1); | |
2603 | return 0; | |
2604 | } | |
2605 | ||
2606 | static int blob_unchanged(struct merge_options *opt, | |
2607 | const struct version_info *base, | |
2608 | const struct version_info *side, | |
2609 | const char *path) | |
2610 | { | |
2611 | struct strbuf basebuf = STRBUF_INIT; | |
2612 | struct strbuf sidebuf = STRBUF_INIT; | |
2613 | int ret = 0; /* assume changed for safety */ | |
2614 | const struct index_state *idx = &opt->priv->attr_index; | |
2615 | ||
2616 | if (!idx->initialized) | |
2617 | initialize_attr_index(opt); | |
2618 | ||
2619 | if (base->mode != side->mode) | |
2620 | return 0; | |
2621 | if (oideq(&base->oid, &side->oid)) | |
2622 | return 1; | |
2623 | ||
2624 | if (read_oid_strbuf(opt, &base->oid, &basebuf) || | |
2625 | read_oid_strbuf(opt, &side->oid, &sidebuf)) | |
2626 | goto error_return; | |
2627 | /* | |
2628 | * Note: binary | is used so that both renormalizations are | |
2629 | * performed. Comparison can be skipped if both files are | |
2630 | * unchanged since their sha1s have already been compared. | |
2631 | */ | |
2632 | if (renormalize_buffer(idx, path, basebuf.buf, basebuf.len, &basebuf) | | |
2633 | renormalize_buffer(idx, path, sidebuf.buf, sidebuf.len, &sidebuf)) | |
2634 | ret = (basebuf.len == sidebuf.len && | |
2635 | !memcmp(basebuf.buf, sidebuf.buf, basebuf.len)); | |
2636 | ||
2637 | error_return: | |
2638 | strbuf_release(&basebuf); | |
2639 | strbuf_release(&sidebuf); | |
2640 | return ret; | |
2641 | } | |
2642 | ||
a9945bba | 2643 | struct directory_versions { |
bb470f4e EN |
2644 | /* |
2645 | * versions: list of (basename -> version_info) | |
2646 | * | |
2647 | * The basenames are in reverse lexicographic order of full pathnames, | |
2648 | * as processed in process_entries(). This puts all entries within | |
2649 | * a directory together, and covers the directory itself after | |
2650 | * everything within it, allowing us to write subtrees before needing | |
2651 | * to record information for the tree itself. | |
2652 | */ | |
a9945bba | 2653 | struct string_list versions; |
bb470f4e EN |
2654 | |
2655 | /* | |
2656 | * offsets: list of (full relative path directories -> integer offsets) | |
2657 | * | |
2658 | * Since versions contains basenames from files in multiple different | |
2659 | * directories, we need to know which entries in versions correspond | |
2660 | * to which directories. Values of e.g. | |
2661 | * "" 0 | |
2662 | * src 2 | |
2663 | * src/moduleA 5 | |
2664 | * Would mean that entries 0-1 of versions are files in the toplevel | |
2665 | * directory, entries 2-4 are files under src/, and the remaining | |
2666 | * entries starting at index 5 are files under src/moduleA/. | |
2667 | */ | |
2668 | struct string_list offsets; | |
2669 | ||
2670 | /* | |
2671 | * last_directory: directory that previously processed file found in | |
2672 | * | |
2673 | * last_directory starts NULL, but records the directory in which the | |
2674 | * previous file was found within. As soon as | |
2675 | * directory(current_file) != last_directory | |
2676 | * then we need to start updating accounting in versions & offsets. | |
2677 | * Note that last_directory is always the last path in "offsets" (or | |
2678 | * NULL if "offsets" is empty) so this exists just for quick access. | |
2679 | */ | |
2680 | const char *last_directory; | |
2681 | ||
2682 | /* last_directory_len: cached computation of strlen(last_directory) */ | |
2683 | unsigned last_directory_len; | |
a9945bba EN |
2684 | }; |
2685 | ||
ee4012dc EN |
2686 | static int tree_entry_order(const void *a_, const void *b_) |
2687 | { | |
2688 | const struct string_list_item *a = a_; | |
2689 | const struct string_list_item *b = b_; | |
2690 | ||
2691 | const struct merged_info *ami = a->util; | |
2692 | const struct merged_info *bmi = b->util; | |
2693 | return base_name_compare(a->string, strlen(a->string), ami->result.mode, | |
2694 | b->string, strlen(b->string), bmi->result.mode); | |
2695 | } | |
2696 | ||
2697 | static void write_tree(struct object_id *result_oid, | |
2698 | struct string_list *versions, | |
2699 | unsigned int offset, | |
2700 | size_t hash_size) | |
2701 | { | |
2702 | size_t maxlen = 0, extra; | |
2703 | unsigned int nr = versions->nr - offset; | |
2704 | struct strbuf buf = STRBUF_INIT; | |
2705 | struct string_list relevant_entries = STRING_LIST_INIT_NODUP; | |
2706 | int i; | |
2707 | ||
2708 | /* | |
2709 | * We want to sort the last (versions->nr-offset) entries in versions. | |
2710 | * Do so by abusing the string_list API a bit: make another string_list | |
2711 | * that contains just those entries and then sort them. | |
2712 | * | |
2713 | * We won't use relevant_entries again and will let it just pop off the | |
2714 | * stack, so there won't be allocation worries or anything. | |
2715 | */ | |
2716 | relevant_entries.items = versions->items + offset; | |
2717 | relevant_entries.nr = versions->nr - offset; | |
72b30910 | 2718 | /* No need for STABLE_QSORT -- filenames must be unique */ |
ee4012dc EN |
2719 | QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order); |
2720 | ||
2721 | /* Pre-allocate some space in buf */ | |
2722 | extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */ | |
2723 | for (i = 0; i < nr; i++) { | |
2724 | maxlen += strlen(versions->items[offset+i].string) + extra; | |
2725 | } | |
2726 | strbuf_grow(&buf, maxlen); | |
2727 | ||
2728 | /* Write each entry out to buf */ | |
2729 | for (i = 0; i < nr; i++) { | |
2730 | struct merged_info *mi = versions->items[offset+i].util; | |
2731 | struct version_info *ri = &mi->result; | |
2732 | strbuf_addf(&buf, "%o %s%c", | |
2733 | ri->mode, | |
2734 | versions->items[offset+i].string, '\0'); | |
2735 | strbuf_add(&buf, ri->oid.hash, hash_size); | |
2736 | } | |
2737 | ||
2738 | /* Write this object file out, and record in result_oid */ | |
2739 | write_object_file(buf.buf, buf.len, tree_type, result_oid); | |
2740 | strbuf_release(&buf); | |
2741 | } | |
2742 | ||
a9945bba EN |
2743 | static void record_entry_for_tree(struct directory_versions *dir_metadata, |
2744 | const char *path, | |
2745 | struct merged_info *mi) | |
2746 | { | |
2747 | const char *basename; | |
2748 | ||
2749 | if (mi->is_null) | |
2750 | /* nothing to record */ | |
2751 | return; | |
2752 | ||
2753 | basename = path + mi->basename_offset; | |
2754 | assert(strchr(basename, '/') == NULL); | |
2755 | string_list_append(&dir_metadata->versions, | |
2756 | basename)->util = &mi->result; | |
2757 | } | |
2758 | ||
bb470f4e EN |
2759 | static void write_completed_directory(struct merge_options *opt, |
2760 | const char *new_directory_name, | |
2761 | struct directory_versions *info) | |
2762 | { | |
2763 | const char *prev_dir; | |
2764 | struct merged_info *dir_info = NULL; | |
2765 | unsigned int offset; | |
2766 | ||
2767 | /* | |
2768 | * Some explanation of info->versions and info->offsets... | |
2769 | * | |
2770 | * process_entries() iterates over all relevant files AND | |
2771 | * directories in reverse lexicographic order, and calls this | |
2772 | * function. Thus, an example of the paths that process_entries() | |
2773 | * could operate on (along with the directories for those paths | |
2774 | * being shown) is: | |
2775 | * | |
2776 | * xtract.c "" | |
2777 | * tokens.txt "" | |
2778 | * src/moduleB/umm.c src/moduleB | |
2779 | * src/moduleB/stuff.h src/moduleB | |
2780 | * src/moduleB/baz.c src/moduleB | |
2781 | * src/moduleB src | |
2782 | * src/moduleA/foo.c src/moduleA | |
2783 | * src/moduleA/bar.c src/moduleA | |
2784 | * src/moduleA src | |
2785 | * src "" | |
2786 | * Makefile "" | |
2787 | * | |
2788 | * info->versions: | |
2789 | * | |
2790 | * always contains the unprocessed entries and their | |
2791 | * version_info information. For example, after the first five | |
2792 | * entries above, info->versions would be: | |
2793 | * | |
2794 | * xtract.c <xtract.c's version_info> | |
2795 | * token.txt <token.txt's version_info> | |
2796 | * umm.c <src/moduleB/umm.c's version_info> | |
2797 | * stuff.h <src/moduleB/stuff.h's version_info> | |
2798 | * baz.c <src/moduleB/baz.c's version_info> | |
2799 | * | |
2800 | * Once a subdirectory is completed we remove the entries in | |
2801 | * that subdirectory from info->versions, writing it as a tree | |
2802 | * (write_tree()). Thus, as soon as we get to src/moduleB, | |
2803 | * info->versions would be updated to | |
2804 | * | |
2805 | * xtract.c <xtract.c's version_info> | |
2806 | * token.txt <token.txt's version_info> | |
2807 | * moduleB <src/moduleB's version_info> | |
2808 | * | |
2809 | * info->offsets: | |
2810 | * | |
2811 | * helps us track which entries in info->versions correspond to | |
2812 | * which directories. When we are N directories deep (e.g. 4 | |
2813 | * for src/modA/submod/subdir/), we have up to N+1 unprocessed | |
2814 | * directories (+1 because of toplevel dir). Corresponding to | |
2815 | * the info->versions example above, after processing five entries | |
2816 | * info->offsets will be: | |
2817 | * | |
2818 | * "" 0 | |
2819 | * src/moduleB 2 | |
2820 | * | |
2821 | * which is used to know that xtract.c & token.txt are from the | |
2822 | * toplevel dirctory, while umm.c & stuff.h & baz.c are from the | |
2823 | * src/moduleB directory. Again, following the example above, | |
2824 | * once we need to process src/moduleB, then info->offsets is | |
2825 | * updated to | |
2826 | * | |
2827 | * "" 0 | |
2828 | * src 2 | |
2829 | * | |
2830 | * which says that moduleB (and only moduleB so far) is in the | |
2831 | * src directory. | |
2832 | * | |
2833 | * One unique thing to note about info->offsets here is that | |
2834 | * "src" was not added to info->offsets until there was a path | |
2835 | * (a file OR directory) immediately below src/ that got | |
2836 | * processed. | |
2837 | * | |
2838 | * Since process_entry() just appends new entries to info->versions, | |
2839 | * write_completed_directory() only needs to do work if the next path | |
2840 | * is in a directory that is different than the last directory found | |
2841 | * in info->offsets. | |
2842 | */ | |
2843 | ||
2844 | /* | |
2845 | * If we are working with the same directory as the last entry, there | |
2846 | * is no work to do. (See comments above the directory_name member of | |
2847 | * struct merged_info for why we can use pointer comparison instead of | |
2848 | * strcmp here.) | |
2849 | */ | |
2850 | if (new_directory_name == info->last_directory) | |
2851 | return; | |
2852 | ||
2853 | /* | |
2854 | * If we are just starting (last_directory is NULL), or last_directory | |
2855 | * is a prefix of the current directory, then we can just update | |
2856 | * info->offsets to record the offset where we started this directory | |
2857 | * and update last_directory to have quick access to it. | |
2858 | */ | |
2859 | if (info->last_directory == NULL || | |
2860 | !strncmp(new_directory_name, info->last_directory, | |
2861 | info->last_directory_len)) { | |
2862 | uintptr_t offset = info->versions.nr; | |
2863 | ||
2864 | info->last_directory = new_directory_name; | |
2865 | info->last_directory_len = strlen(info->last_directory); | |
2866 | /* | |
2867 | * Record the offset into info->versions where we will | |
2868 | * start recording basenames of paths found within | |
2869 | * new_directory_name. | |
2870 | */ | |
2871 | string_list_append(&info->offsets, | |
2872 | info->last_directory)->util = (void*)offset; | |
2873 | return; | |
2874 | } | |
2875 | ||
2876 | /* | |
2877 | * The next entry that will be processed will be within | |
2878 | * new_directory_name. Since at this point we know that | |
2879 | * new_directory_name is within a different directory than | |
2880 | * info->last_directory, we have all entries for info->last_directory | |
2881 | * in info->versions and we need to create a tree object for them. | |
2882 | */ | |
2883 | dir_info = strmap_get(&opt->priv->paths, info->last_directory); | |
2884 | assert(dir_info); | |
2885 | offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util; | |
2886 | if (offset == info->versions.nr) { | |
2887 | /* | |
2888 | * Actually, we don't need to create a tree object in this | |
2889 | * case. Whenever all files within a directory disappear | |
2890 | * during the merge (e.g. unmodified on one side and | |
2891 | * deleted on the other, or files were renamed elsewhere), | |
2892 | * then we get here and the directory itself needs to be | |
2893 | * omitted from its parent tree as well. | |
2894 | */ | |
2895 | dir_info->is_null = 1; | |
2896 | } else { | |
2897 | /* | |
2898 | * Write out the tree to the git object directory, and also | |
2899 | * record the mode and oid in dir_info->result. | |
2900 | */ | |
2901 | dir_info->is_null = 0; | |
2902 | dir_info->result.mode = S_IFDIR; | |
2903 | write_tree(&dir_info->result.oid, &info->versions, offset, | |
2904 | opt->repo->hash_algo->rawsz); | |
2905 | } | |
2906 | ||
2907 | /* | |
2908 | * We've now used several entries from info->versions and one entry | |
2909 | * from info->offsets, so we get rid of those values. | |
2910 | */ | |
2911 | info->offsets.nr--; | |
2912 | info->versions.nr = offset; | |
2913 | ||
2914 | /* | |
2915 | * Now we've taken care of the completed directory, but we need to | |
2916 | * prepare things since future entries will be in | |
2917 | * new_directory_name. (In particular, process_entry() will be | |
2918 | * appending new entries to info->versions.) So, we need to make | |
2919 | * sure new_directory_name is the last entry in info->offsets. | |
2920 | */ | |
2921 | prev_dir = info->offsets.nr == 0 ? NULL : | |
2922 | info->offsets.items[info->offsets.nr-1].string; | |
2923 | if (new_directory_name != prev_dir) { | |
2924 | uintptr_t c = info->versions.nr; | |
2925 | string_list_append(&info->offsets, | |
2926 | new_directory_name)->util = (void*)c; | |
2927 | } | |
2928 | ||
2929 | /* And, of course, we need to update last_directory to match. */ | |
2930 | info->last_directory = new_directory_name; | |
2931 | info->last_directory_len = strlen(info->last_directory); | |
2932 | } | |
2933 | ||
6a02dd90 EN |
2934 | /* Per entry merge function */ |
2935 | static void process_entry(struct merge_options *opt, | |
2936 | const char *path, | |
a9945bba EN |
2937 | struct conflict_info *ci, |
2938 | struct directory_versions *dir_metadata) | |
6a02dd90 | 2939 | { |
23366d2a EN |
2940 | int df_file_index = 0; |
2941 | ||
6a02dd90 EN |
2942 | VERIFY_CI(ci); |
2943 | assert(ci->filemask >= 0 && ci->filemask <= 7); | |
2944 | /* ci->match_mask == 7 was handled in collect_merge_info_callback() */ | |
2945 | assert(ci->match_mask == 0 || ci->match_mask == 3 || | |
2946 | ci->match_mask == 5 || ci->match_mask == 6); | |
2947 | ||
a9945bba EN |
2948 | if (ci->dirmask) { |
2949 | record_entry_for_tree(dir_metadata, path, &ci->merged); | |
2950 | if (ci->filemask == 0) | |
2951 | /* nothing else to handle */ | |
2952 | return; | |
2953 | assert(ci->df_conflict); | |
2954 | } | |
2955 | ||
0ccfa4e5 EN |
2956 | if (ci->df_conflict && ci->merged.result.mode == 0) { |
2957 | int i; | |
2958 | ||
2959 | /* | |
2960 | * directory no longer in the way, but we do have a file we | |
2961 | * need to place here so we need to clean away the "directory | |
2962 | * merges to nothing" result. | |
2963 | */ | |
2964 | ci->df_conflict = 0; | |
2965 | assert(ci->filemask != 0); | |
2966 | ci->merged.clean = 0; | |
2967 | ci->merged.is_null = 0; | |
2968 | /* and we want to zero out any directory-related entries */ | |
2969 | ci->match_mask = (ci->match_mask & ~ci->dirmask); | |
2970 | ci->dirmask = 0; | |
2971 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
2972 | if (ci->filemask & (1 << i)) | |
2973 | continue; | |
2974 | ci->stages[i].mode = 0; | |
2975 | oidcpy(&ci->stages[i].oid, &null_oid); | |
2976 | } | |
2977 | } else if (ci->df_conflict && ci->merged.result.mode != 0) { | |
23366d2a EN |
2978 | /* |
2979 | * This started out as a D/F conflict, and the entries in | |
2980 | * the competing directory were not removed by the merge as | |
2981 | * evidenced by write_completed_directory() writing a value | |
2982 | * to ci->merged.result.mode. | |
2983 | */ | |
2984 | struct conflict_info *new_ci; | |
2985 | const char *branch; | |
2986 | const char *old_path = path; | |
2987 | int i; | |
2988 | ||
2989 | assert(ci->merged.result.mode == S_IFDIR); | |
2990 | ||
2991 | /* | |
2992 | * If filemask is 1, we can just ignore the file as having | |
2993 | * been deleted on both sides. We do not want to overwrite | |
2994 | * ci->merged.result, since it stores the tree for all the | |
2995 | * files under it. | |
2996 | */ | |
2997 | if (ci->filemask == 1) { | |
2998 | ci->filemask = 0; | |
2999 | return; | |
3000 | } | |
3001 | ||
3002 | /* | |
3003 | * This file still exists on at least one side, and we want | |
3004 | * the directory to remain here, so we need to move this | |
3005 | * path to some new location. | |
3006 | */ | |
3007 | new_ci = xcalloc(1, sizeof(*new_ci)); | |
3008 | /* We don't really want new_ci->merged.result copied, but it'll | |
3009 | * be overwritten below so it doesn't matter. We also don't | |
3010 | * want any directory mode/oid values copied, but we'll zero | |
3011 | * those out immediately. We do want the rest of ci copied. | |
3012 | */ | |
3013 | memcpy(new_ci, ci, sizeof(*ci)); | |
3014 | new_ci->match_mask = (new_ci->match_mask & ~new_ci->dirmask); | |
3015 | new_ci->dirmask = 0; | |
3016 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
3017 | if (new_ci->filemask & (1 << i)) | |
3018 | continue; | |
3019 | /* zero out any entries related to directories */ | |
3020 | new_ci->stages[i].mode = 0; | |
3021 | oidcpy(&new_ci->stages[i].oid, &null_oid); | |
3022 | } | |
3023 | ||
3024 | /* | |
3025 | * Find out which side this file came from; note that we | |
3026 | * cannot just use ci->filemask, because renames could cause | |
3027 | * the filemask to go back to 7. So we use dirmask, then | |
3028 | * pick the opposite side's index. | |
3029 | */ | |
3030 | df_file_index = (ci->dirmask & (1 << 1)) ? 2 : 1; | |
3031 | branch = (df_file_index == 1) ? opt->branch1 : opt->branch2; | |
3032 | path = unique_path(&opt->priv->paths, path, branch); | |
3033 | strmap_put(&opt->priv->paths, path, new_ci); | |
3034 | ||
3035 | path_msg(opt, path, 0, | |
3036 | _("CONFLICT (file/directory): directory in the way " | |
3037 | "of %s from %s; moving it to %s instead."), | |
3038 | old_path, branch, path); | |
3039 | ||
3040 | /* | |
3041 | * Zero out the filemask for the old ci. At this point, ci | |
3042 | * was just an entry for a directory, so we don't need to | |
3043 | * do anything more with it. | |
3044 | */ | |
3045 | ci->filemask = 0; | |
3046 | ||
3047 | /* | |
3048 | * Now note that we're working on the new entry (path was | |
3049 | * updated above. | |
3050 | */ | |
3051 | ci = new_ci; | |
6a02dd90 EN |
3052 | } |
3053 | ||
3054 | /* | |
3055 | * NOTE: Below there is a long switch-like if-elseif-elseif... block | |
3056 | * which the code goes through even for the df_conflict cases | |
23366d2a | 3057 | * above. |
6a02dd90 EN |
3058 | */ |
3059 | if (ci->match_mask) { | |
3060 | ci->merged.clean = 1; | |
3061 | if (ci->match_mask == 6) { | |
3062 | /* stages[1] == stages[2] */ | |
3063 | ci->merged.result.mode = ci->stages[1].mode; | |
3064 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
3065 | } else { | |
3066 | /* determine the mask of the side that didn't match */ | |
3067 | unsigned int othermask = 7 & ~ci->match_mask; | |
3068 | int side = (othermask == 4) ? 2 : 1; | |
3069 | ||
3070 | ci->merged.result.mode = ci->stages[side].mode; | |
3071 | ci->merged.is_null = !ci->merged.result.mode; | |
3072 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
3073 | ||
3074 | assert(othermask == 2 || othermask == 4); | |
3075 | assert(ci->merged.is_null == | |
3076 | (ci->filemask == ci->match_mask)); | |
3077 | } | |
3078 | } else if (ci->filemask >= 6 && | |
3079 | (S_IFMT & ci->stages[1].mode) != | |
3080 | (S_IFMT & ci->stages[2].mode)) { | |
4ef88fc3 EN |
3081 | /* Two different items from (file/submodule/symlink) */ |
3082 | if (opt->priv->call_depth) { | |
3083 | /* Just use the version from the merge base */ | |
3084 | ci->merged.clean = 0; | |
3085 | oidcpy(&ci->merged.result.oid, &ci->stages[0].oid); | |
3086 | ci->merged.result.mode = ci->stages[0].mode; | |
3087 | ci->merged.is_null = (ci->merged.result.mode == 0); | |
3088 | } else { | |
3089 | /* Handle by renaming one or both to separate paths. */ | |
3090 | unsigned o_mode = ci->stages[0].mode; | |
3091 | unsigned a_mode = ci->stages[1].mode; | |
3092 | unsigned b_mode = ci->stages[2].mode; | |
3093 | struct conflict_info *new_ci; | |
3094 | const char *a_path = NULL, *b_path = NULL; | |
3095 | int rename_a = 0, rename_b = 0; | |
3096 | ||
3097 | new_ci = xmalloc(sizeof(*new_ci)); | |
3098 | ||
3099 | if (S_ISREG(a_mode)) | |
3100 | rename_a = 1; | |
3101 | else if (S_ISREG(b_mode)) | |
3102 | rename_b = 1; | |
3103 | else { | |
3104 | rename_a = 1; | |
3105 | rename_b = 1; | |
3106 | } | |
3107 | ||
3108 | path_msg(opt, path, 0, | |
3109 | _("CONFLICT (distinct types): %s had different " | |
3110 | "types on each side; renamed %s of them so " | |
3111 | "each can be recorded somewhere."), | |
3112 | path, | |
3113 | (rename_a && rename_b) ? _("both") : _("one")); | |
3114 | ||
3115 | ci->merged.clean = 0; | |
3116 | memcpy(new_ci, ci, sizeof(*new_ci)); | |
3117 | ||
3118 | /* Put b into new_ci, removing a from stages */ | |
3119 | new_ci->merged.result.mode = ci->stages[2].mode; | |
3120 | oidcpy(&new_ci->merged.result.oid, &ci->stages[2].oid); | |
3121 | new_ci->stages[1].mode = 0; | |
3122 | oidcpy(&new_ci->stages[1].oid, &null_oid); | |
3123 | new_ci->filemask = 5; | |
3124 | if ((S_IFMT & b_mode) != (S_IFMT & o_mode)) { | |
3125 | new_ci->stages[0].mode = 0; | |
3126 | oidcpy(&new_ci->stages[0].oid, &null_oid); | |
3127 | new_ci->filemask = 4; | |
3128 | } | |
3129 | ||
3130 | /* Leave only a in ci, fixing stages. */ | |
3131 | ci->merged.result.mode = ci->stages[1].mode; | |
3132 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
3133 | ci->stages[2].mode = 0; | |
3134 | oidcpy(&ci->stages[2].oid, &null_oid); | |
3135 | ci->filemask = 3; | |
3136 | if ((S_IFMT & a_mode) != (S_IFMT & o_mode)) { | |
3137 | ci->stages[0].mode = 0; | |
3138 | oidcpy(&ci->stages[0].oid, &null_oid); | |
3139 | ci->filemask = 2; | |
3140 | } | |
3141 | ||
3142 | /* Insert entries into opt->priv_paths */ | |
3143 | assert(rename_a || rename_b); | |
3144 | if (rename_a) { | |
3145 | a_path = unique_path(&opt->priv->paths, | |
3146 | path, opt->branch1); | |
3147 | strmap_put(&opt->priv->paths, a_path, ci); | |
3148 | } | |
3149 | ||
3150 | if (rename_b) | |
3151 | b_path = unique_path(&opt->priv->paths, | |
3152 | path, opt->branch2); | |
3153 | else | |
3154 | b_path = path; | |
3155 | strmap_put(&opt->priv->paths, b_path, new_ci); | |
3156 | ||
3157 | if (rename_a && rename_b) { | |
3158 | strmap_remove(&opt->priv->paths, path, 0); | |
3159 | /* | |
3160 | * We removed path from opt->priv->paths. path | |
3161 | * will also eventually need to be freed, but | |
3162 | * it may still be used by e.g. ci->pathnames. | |
3163 | * So, store it in another string-list for now. | |
3164 | */ | |
3165 | string_list_append(&opt->priv->paths_to_free, | |
3166 | path); | |
3167 | } | |
3168 | ||
3169 | /* | |
3170 | * Do special handling for b_path since process_entry() | |
3171 | * won't be called on it specially. | |
3172 | */ | |
3173 | strmap_put(&opt->priv->conflicted, b_path, new_ci); | |
3174 | record_entry_for_tree(dir_metadata, b_path, | |
3175 | &new_ci->merged); | |
3176 | ||
3177 | /* | |
3178 | * Remaining code for processing this entry should | |
3179 | * think in terms of processing a_path. | |
3180 | */ | |
3181 | if (a_path) | |
3182 | path = a_path; | |
3183 | } | |
6a02dd90 | 3184 | } else if (ci->filemask >= 6) { |
991bbdca EN |
3185 | /* Need a two-way or three-way content merge */ |
3186 | struct version_info merged_file; | |
3187 | unsigned clean_merge; | |
3188 | struct version_info *o = &ci->stages[0]; | |
3189 | struct version_info *a = &ci->stages[1]; | |
3190 | struct version_info *b = &ci->stages[2]; | |
3191 | ||
3192 | clean_merge = handle_content_merge(opt, path, o, a, b, | |
3193 | ci->pathnames, | |
3194 | opt->priv->call_depth * 2, | |
3195 | &merged_file); | |
3196 | ci->merged.clean = clean_merge && | |
3197 | !ci->df_conflict && !ci->path_conflict; | |
3198 | ci->merged.result.mode = merged_file.mode; | |
3199 | ci->merged.is_null = (merged_file.mode == 0); | |
3200 | oidcpy(&ci->merged.result.oid, &merged_file.oid); | |
3201 | if (clean_merge && ci->df_conflict) { | |
3202 | assert(df_file_index == 1 || df_file_index == 2); | |
3203 | ci->filemask = 1 << df_file_index; | |
3204 | ci->stages[df_file_index].mode = merged_file.mode; | |
3205 | oidcpy(&ci->stages[df_file_index].oid, &merged_file.oid); | |
3206 | } | |
3207 | if (!clean_merge) { | |
3208 | const char *reason = _("content"); | |
3209 | if (ci->filemask == 6) | |
3210 | reason = _("add/add"); | |
3211 | if (S_ISGITLINK(merged_file.mode)) | |
3212 | reason = _("submodule"); | |
3213 | path_msg(opt, path, 0, | |
3214 | _("CONFLICT (%s): Merge conflict in %s"), | |
3215 | reason, path); | |
3216 | } | |
6a02dd90 EN |
3217 | } else if (ci->filemask == 3 || ci->filemask == 5) { |
3218 | /* Modify/delete */ | |
c5a6f655 EN |
3219 | const char *modify_branch, *delete_branch; |
3220 | int side = (ci->filemask == 5) ? 2 : 1; | |
3221 | int index = opt->priv->call_depth ? 0 : side; | |
3222 | ||
3223 | ci->merged.result.mode = ci->stages[index].mode; | |
3224 | oidcpy(&ci->merged.result.oid, &ci->stages[index].oid); | |
3225 | ci->merged.clean = 0; | |
3226 | ||
3227 | modify_branch = (side == 1) ? opt->branch1 : opt->branch2; | |
3228 | delete_branch = (side == 1) ? opt->branch2 : opt->branch1; | |
3229 | ||
3860220b EN |
3230 | if (opt->renormalize && |
3231 | blob_unchanged(opt, &ci->stages[0], &ci->stages[side], | |
3232 | path)) { | |
3233 | ci->merged.is_null = 1; | |
3234 | ci->merged.clean = 1; | |
3235 | } else if (ci->path_conflict && | |
3236 | oideq(&ci->stages[0].oid, &ci->stages[side].oid)) { | |
2e91ddd2 EN |
3237 | /* |
3238 | * This came from a rename/delete; no action to take, | |
3239 | * but avoid printing "modify/delete" conflict notice | |
3240 | * since the contents were not modified. | |
3241 | */ | |
3242 | } else { | |
3243 | path_msg(opt, path, 0, | |
3244 | _("CONFLICT (modify/delete): %s deleted in %s " | |
3245 | "and modified in %s. Version %s of %s left " | |
3246 | "in tree."), | |
3247 | path, delete_branch, modify_branch, | |
3248 | modify_branch, path); | |
3249 | } | |
6a02dd90 EN |
3250 | } else if (ci->filemask == 2 || ci->filemask == 4) { |
3251 | /* Added on one side */ | |
3252 | int side = (ci->filemask == 4) ? 2 : 1; | |
3253 | ci->merged.result.mode = ci->stages[side].mode; | |
3254 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
53e88a03 | 3255 | ci->merged.clean = !ci->df_conflict && !ci->path_conflict; |
6a02dd90 EN |
3256 | } else if (ci->filemask == 1) { |
3257 | /* Deleted on both sides */ | |
3258 | ci->merged.is_null = 1; | |
3259 | ci->merged.result.mode = 0; | |
3260 | oidcpy(&ci->merged.result.oid, &null_oid); | |
53e88a03 | 3261 | ci->merged.clean = !ci->path_conflict; |
6a02dd90 EN |
3262 | } |
3263 | ||
3264 | /* | |
3265 | * If still conflicted, record it separately. This allows us to later | |
3266 | * iterate over just conflicted entries when updating the index instead | |
3267 | * of iterating over all entries. | |
3268 | */ | |
3269 | if (!ci->merged.clean) | |
3270 | strmap_put(&opt->priv->conflicted, path, ci); | |
a9945bba | 3271 | record_entry_for_tree(dir_metadata, path, &ci->merged); |
6a02dd90 EN |
3272 | } |
3273 | ||
231e2dd4 EN |
3274 | static void process_entries(struct merge_options *opt, |
3275 | struct object_id *result_oid) | |
3276 | { | |
6a02dd90 EN |
3277 | struct hashmap_iter iter; |
3278 | struct strmap_entry *e; | |
8adffaa8 EN |
3279 | struct string_list plist = STRING_LIST_INIT_NODUP; |
3280 | struct string_list_item *entry; | |
bb470f4e EN |
3281 | struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP, |
3282 | STRING_LIST_INIT_NODUP, | |
3283 | NULL, 0 }; | |
6a02dd90 | 3284 | |
557ac035 | 3285 | trace2_region_enter("merge", "process_entries setup", opt->repo); |
6a02dd90 EN |
3286 | if (strmap_empty(&opt->priv->paths)) { |
3287 | oidcpy(result_oid, opt->repo->hash_algo->empty_tree); | |
3288 | return; | |
3289 | } | |
3290 | ||
8adffaa8 | 3291 | /* Hack to pre-allocate plist to the desired size */ |
557ac035 | 3292 | trace2_region_enter("merge", "plist grow", opt->repo); |
8adffaa8 | 3293 | ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc); |
557ac035 | 3294 | trace2_region_leave("merge", "plist grow", opt->repo); |
8adffaa8 EN |
3295 | |
3296 | /* Put every entry from paths into plist, then sort */ | |
557ac035 | 3297 | trace2_region_enter("merge", "plist copy", opt->repo); |
6a02dd90 | 3298 | strmap_for_each_entry(&opt->priv->paths, &iter, e) { |
8adffaa8 EN |
3299 | string_list_append(&plist, e->key)->util = e->value; |
3300 | } | |
557ac035 EN |
3301 | trace2_region_leave("merge", "plist copy", opt->repo); |
3302 | ||
3303 | trace2_region_enter("merge", "plist special sort", opt->repo); | |
8adffaa8 EN |
3304 | plist.cmp = string_list_df_name_compare; |
3305 | string_list_sort(&plist); | |
557ac035 EN |
3306 | trace2_region_leave("merge", "plist special sort", opt->repo); |
3307 | ||
3308 | trace2_region_leave("merge", "process_entries setup", opt->repo); | |
8adffaa8 EN |
3309 | |
3310 | /* | |
3311 | * Iterate over the items in reverse order, so we can handle paths | |
3312 | * below a directory before needing to handle the directory itself. | |
bb470f4e EN |
3313 | * |
3314 | * This allows us to write subtrees before we need to write trees, | |
3315 | * and it also enables sane handling of directory/file conflicts | |
3316 | * (because it allows us to know whether the directory is still in | |
3317 | * the way when it is time to process the file at the same path). | |
8adffaa8 | 3318 | */ |
557ac035 | 3319 | trace2_region_enter("merge", "processing", opt->repo); |
8adffaa8 EN |
3320 | for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) { |
3321 | char *path = entry->string; | |
6a02dd90 EN |
3322 | /* |
3323 | * NOTE: mi may actually be a pointer to a conflict_info, but | |
3324 | * we have to check mi->clean first to see if it's safe to | |
3325 | * reassign to such a pointer type. | |
3326 | */ | |
8adffaa8 | 3327 | struct merged_info *mi = entry->util; |
6a02dd90 | 3328 | |
bb470f4e EN |
3329 | write_completed_directory(opt, mi->directory_name, |
3330 | &dir_metadata); | |
a9945bba EN |
3331 | if (mi->clean) |
3332 | record_entry_for_tree(&dir_metadata, path, mi); | |
3333 | else { | |
8adffaa8 | 3334 | struct conflict_info *ci = (struct conflict_info *)mi; |
a9945bba | 3335 | process_entry(opt, path, ci, &dir_metadata); |
8adffaa8 | 3336 | } |
6a02dd90 | 3337 | } |
557ac035 | 3338 | trace2_region_leave("merge", "processing", opt->repo); |
6a02dd90 | 3339 | |
557ac035 | 3340 | trace2_region_enter("merge", "process_entries cleanup", opt->repo); |
bb470f4e EN |
3341 | if (dir_metadata.offsets.nr != 1 || |
3342 | (uintptr_t)dir_metadata.offsets.items[0].util != 0) { | |
3343 | printf("dir_metadata.offsets.nr = %d (should be 1)\n", | |
3344 | dir_metadata.offsets.nr); | |
3345 | printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n", | |
3346 | (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util); | |
3347 | fflush(stdout); | |
3348 | BUG("dir_metadata accounting completely off; shouldn't happen"); | |
3349 | } | |
ee4012dc EN |
3350 | write_tree(result_oid, &dir_metadata.versions, 0, |
3351 | opt->repo->hash_algo->rawsz); | |
8adffaa8 | 3352 | string_list_clear(&plist, 0); |
a9945bba | 3353 | string_list_clear(&dir_metadata.versions, 0); |
bb470f4e | 3354 | string_list_clear(&dir_metadata.offsets, 0); |
557ac035 | 3355 | trace2_region_leave("merge", "process_entries cleanup", opt->repo); |
231e2dd4 EN |
3356 | } |
3357 | ||
04af1879 EN |
3358 | /*** Function Grouping: functions related to merge_switch_to_result() ***/ |
3359 | ||
9fefce68 EN |
3360 | static int checkout(struct merge_options *opt, |
3361 | struct tree *prev, | |
3362 | struct tree *next) | |
3363 | { | |
6681ce5c EN |
3364 | /* Switch the index/working copy from old to new */ |
3365 | int ret; | |
3366 | struct tree_desc trees[2]; | |
3367 | struct unpack_trees_options unpack_opts; | |
3368 | ||
3369 | memset(&unpack_opts, 0, sizeof(unpack_opts)); | |
3370 | unpack_opts.head_idx = -1; | |
3371 | unpack_opts.src_index = opt->repo->index; | |
3372 | unpack_opts.dst_index = opt->repo->index; | |
3373 | ||
3374 | setup_unpack_trees_porcelain(&unpack_opts, "merge"); | |
3375 | ||
3376 | /* | |
3377 | * NOTE: if this were just "git checkout" code, we would probably | |
3378 | * read or refresh the cache and check for a conflicted index, but | |
3379 | * builtin/merge.c or sequencer.c really needs to read the index | |
3380 | * and check for conflicted entries before starting merging for a | |
3381 | * good user experience (no sense waiting for merges/rebases before | |
3382 | * erroring out), so there's no reason to duplicate that work here. | |
3383 | */ | |
3384 | ||
3385 | /* 2-way merge to the new branch */ | |
3386 | unpack_opts.update = 1; | |
3387 | unpack_opts.merge = 1; | |
3388 | unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */ | |
3389 | unpack_opts.verbose_update = (opt->verbosity > 2); | |
3390 | unpack_opts.fn = twoway_merge; | |
3391 | if (1/* FIXME: opts->overwrite_ignore*/) { | |
3392 | unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir)); | |
3393 | unpack_opts.dir->flags |= DIR_SHOW_IGNORED; | |
3394 | setup_standard_excludes(unpack_opts.dir); | |
3395 | } | |
3396 | parse_tree(prev); | |
3397 | init_tree_desc(&trees[0], prev->buffer, prev->size); | |
3398 | parse_tree(next); | |
3399 | init_tree_desc(&trees[1], next->buffer, next->size); | |
3400 | ||
3401 | ret = unpack_trees(2, trees, &unpack_opts); | |
3402 | clear_unpack_trees_porcelain(&unpack_opts); | |
3403 | dir_clear(unpack_opts.dir); | |
3404 | FREE_AND_NULL(unpack_opts.dir); | |
3405 | return ret; | |
9fefce68 EN |
3406 | } |
3407 | ||
66b209b8 | 3408 | static int record_conflicted_index_entries(struct merge_options *opt) |
9fefce68 | 3409 | { |
ef2b3693 EN |
3410 | struct hashmap_iter iter; |
3411 | struct strmap_entry *e; | |
66b209b8 EN |
3412 | struct index_state *index = opt->repo->index; |
3413 | struct checkout state = CHECKOUT_INIT; | |
ef2b3693 EN |
3414 | int errs = 0; |
3415 | int original_cache_nr; | |
3416 | ||
66b209b8 | 3417 | if (strmap_empty(&opt->priv->conflicted)) |
9fefce68 EN |
3418 | return 0; |
3419 | ||
66b209b8 EN |
3420 | /* If any entries have skip_worktree set, we'll have to check 'em out */ |
3421 | state.force = 1; | |
3422 | state.quiet = 1; | |
3423 | state.refresh_cache = 1; | |
3424 | state.istate = index; | |
ef2b3693 EN |
3425 | original_cache_nr = index->cache_nr; |
3426 | ||
3427 | /* Put every entry from paths into plist, then sort */ | |
66b209b8 | 3428 | strmap_for_each_entry(&opt->priv->conflicted, &iter, e) { |
ef2b3693 EN |
3429 | const char *path = e->key; |
3430 | struct conflict_info *ci = e->value; | |
3431 | int pos; | |
3432 | struct cache_entry *ce; | |
3433 | int i; | |
3434 | ||
3435 | VERIFY_CI(ci); | |
3436 | ||
3437 | /* | |
3438 | * The index will already have a stage=0 entry for this path, | |
3439 | * because we created an as-merged-as-possible version of the | |
3440 | * file and checkout() moved the working copy and index over | |
3441 | * to that version. | |
3442 | * | |
3443 | * However, previous iterations through this loop will have | |
3444 | * added unstaged entries to the end of the cache which | |
3445 | * ignore the standard alphabetical ordering of cache | |
3446 | * entries and break invariants needed for index_name_pos() | |
3447 | * to work. However, we know the entry we want is before | |
3448 | * those appended cache entries, so do a temporary swap on | |
3449 | * cache_nr to only look through entries of interest. | |
3450 | */ | |
3451 | SWAP(index->cache_nr, original_cache_nr); | |
3452 | pos = index_name_pos(index, path, strlen(path)); | |
3453 | SWAP(index->cache_nr, original_cache_nr); | |
3454 | if (pos < 0) { | |
3455 | if (ci->filemask != 1) | |
3456 | BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path); | |
3457 | cache_tree_invalidate_path(index, path); | |
3458 | } else { | |
3459 | ce = index->cache[pos]; | |
3460 | ||
3461 | /* | |
3462 | * Clean paths with CE_SKIP_WORKTREE set will not be | |
3463 | * written to the working tree by the unpack_trees() | |
3464 | * call in checkout(). Our conflicted entries would | |
3465 | * have appeared clean to that code since we ignored | |
3466 | * the higher order stages. Thus, we need override | |
3467 | * the CE_SKIP_WORKTREE bit and manually write those | |
3468 | * files to the working disk here. | |
ef2b3693 | 3469 | */ |
66b209b8 EN |
3470 | if (ce_skip_worktree(ce)) { |
3471 | struct stat st; | |
3472 | ||
3473 | if (!lstat(path, &st)) { | |
3474 | char *new_name = unique_path(&opt->priv->paths, | |
3475 | path, | |
3476 | "cruft"); | |
3477 | ||
3478 | path_msg(opt, path, 1, | |
3479 | _("Note: %s not up to date and in way of checking out conflicted version; old copy renamed to %s"), | |
3480 | path, new_name); | |
3481 | errs |= rename(path, new_name); | |
3482 | free(new_name); | |
3483 | } | |
3484 | errs |= checkout_entry(ce, &state, NULL, NULL); | |
3485 | } | |
ef2b3693 EN |
3486 | |
3487 | /* | |
3488 | * Mark this cache entry for removal and instead add | |
3489 | * new stage>0 entries corresponding to the | |
3490 | * conflicts. If there are many conflicted entries, we | |
3491 | * want to avoid memmove'ing O(NM) entries by | |
3492 | * inserting the new entries one at a time. So, | |
3493 | * instead, we just add the new cache entries to the | |
3494 | * end (ignoring normal index requirements on sort | |
3495 | * order) and sort the index once we're all done. | |
3496 | */ | |
3497 | ce->ce_flags |= CE_REMOVE; | |
3498 | } | |
3499 | ||
3500 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
3501 | struct version_info *vi; | |
3502 | if (!(ci->filemask & (1ul << i))) | |
3503 | continue; | |
3504 | vi = &ci->stages[i]; | |
3505 | ce = make_cache_entry(index, vi->mode, &vi->oid, | |
3506 | path, i+1, 0); | |
3507 | add_index_entry(index, ce, ADD_CACHE_JUST_APPEND); | |
3508 | } | |
3509 | } | |
3510 | ||
3511 | /* | |
3512 | * Remove the unused cache entries (and invalidate the relevant | |
3513 | * cache-trees), then sort the index entries to get the conflicted | |
3514 | * entries we added to the end into their right locations. | |
3515 | */ | |
3516 | remove_marked_cache_entries(index, 1); | |
72b30910 EN |
3517 | /* |
3518 | * No need for STABLE_QSORT -- cmp_cache_name_compare sorts primarily | |
3519 | * on filename and secondarily on stage, and (name, stage #) are a | |
3520 | * unique tuple. | |
3521 | */ | |
ef2b3693 EN |
3522 | QSORT(index->cache, index->cache_nr, cmp_cache_name_compare); |
3523 | ||
3524 | return errs; | |
9fefce68 EN |
3525 | } |
3526 | ||
17e5574b EN |
3527 | void merge_switch_to_result(struct merge_options *opt, |
3528 | struct tree *head, | |
3529 | struct merge_result *result, | |
3530 | int update_worktree_and_index, | |
3531 | int display_update_msgs) | |
3532 | { | |
9fefce68 EN |
3533 | assert(opt->priv == NULL); |
3534 | if (result->clean >= 0 && update_worktree_and_index) { | |
5291828d EN |
3535 | const char *filename; |
3536 | FILE *fp; | |
3537 | ||
557ac035 | 3538 | trace2_region_enter("merge", "checkout", opt->repo); |
9fefce68 EN |
3539 | if (checkout(opt, head, result->tree)) { |
3540 | /* failure to function */ | |
3541 | result->clean = -1; | |
3542 | return; | |
3543 | } | |
557ac035 | 3544 | trace2_region_leave("merge", "checkout", opt->repo); |
9fefce68 | 3545 | |
557ac035 | 3546 | trace2_region_enter("merge", "record_conflicted", opt->repo); |
66b209b8 EN |
3547 | opt->priv = result->priv; |
3548 | if (record_conflicted_index_entries(opt)) { | |
9fefce68 | 3549 | /* failure to function */ |
66b209b8 | 3550 | opt->priv = NULL; |
9fefce68 EN |
3551 | result->clean = -1; |
3552 | return; | |
3553 | } | |
66b209b8 | 3554 | opt->priv = NULL; |
557ac035 | 3555 | trace2_region_leave("merge", "record_conflicted", opt->repo); |
5291828d EN |
3556 | |
3557 | trace2_region_enter("merge", "write_auto_merge", opt->repo); | |
3558 | filename = git_path_auto_merge(opt->repo); | |
3559 | fp = xfopen(filename, "w"); | |
3560 | fprintf(fp, "%s\n", oid_to_hex(&result->tree->object.oid)); | |
3561 | fclose(fp); | |
3562 | trace2_region_leave("merge", "write_auto_merge", opt->repo); | |
9fefce68 EN |
3563 | } |
3564 | ||
3565 | if (display_update_msgs) { | |
c5a6f655 EN |
3566 | struct merge_options_internal *opti = result->priv; |
3567 | struct hashmap_iter iter; | |
3568 | struct strmap_entry *e; | |
3569 | struct string_list olist = STRING_LIST_INIT_NODUP; | |
3570 | int i; | |
3571 | ||
557ac035 EN |
3572 | trace2_region_enter("merge", "display messages", opt->repo); |
3573 | ||
c5a6f655 EN |
3574 | /* Hack to pre-allocate olist to the desired size */ |
3575 | ALLOC_GROW(olist.items, strmap_get_size(&opti->output), | |
3576 | olist.alloc); | |
3577 | ||
3578 | /* Put every entry from output into olist, then sort */ | |
3579 | strmap_for_each_entry(&opti->output, &iter, e) { | |
3580 | string_list_append(&olist, e->key)->util = e->value; | |
3581 | } | |
3582 | string_list_sort(&olist); | |
3583 | ||
3584 | /* Iterate over the items, printing them */ | |
3585 | for (i = 0; i < olist.nr; ++i) { | |
3586 | struct strbuf *sb = olist.items[i].util; | |
3587 | ||
3588 | printf("%s", sb->buf); | |
3589 | } | |
3590 | string_list_clear(&olist, 0); | |
f39d05ca EN |
3591 | |
3592 | /* Also include needed rename limit adjustment now */ | |
3593 | diff_warn_rename_limit("merge.renamelimit", | |
3594 | opti->renames.needed_limit, 0); | |
557ac035 EN |
3595 | |
3596 | trace2_region_leave("merge", "display messages", opt->repo); | |
9fefce68 EN |
3597 | } |
3598 | ||
17e5574b EN |
3599 | merge_finalize(opt, result); |
3600 | } | |
3601 | ||
3602 | void merge_finalize(struct merge_options *opt, | |
3603 | struct merge_result *result) | |
3604 | { | |
89422d29 EN |
3605 | struct merge_options_internal *opti = result->priv; |
3606 | ||
ea305a68 EN |
3607 | if (opt->renormalize) |
3608 | git_attr_set_direction(GIT_ATTR_CHECKIN); | |
89422d29 EN |
3609 | assert(opt->priv == NULL); |
3610 | ||
43e9c4ee | 3611 | clear_or_reinit_internal_opts(opti, 0); |
89422d29 | 3612 | FREE_AND_NULL(opti); |
17e5574b EN |
3613 | } |
3614 | ||
04af1879 EN |
3615 | /*** Function Grouping: helper functions for merge_incore_*() ***/ |
3616 | ||
3639dfb3 EN |
3617 | static struct tree *shift_tree_object(struct repository *repo, |
3618 | struct tree *one, struct tree *two, | |
3619 | const char *subtree_shift) | |
3620 | { | |
3621 | struct object_id shifted; | |
3622 | ||
3623 | if (!*subtree_shift) { | |
3624 | shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0); | |
3625 | } else { | |
3626 | shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted, | |
3627 | subtree_shift); | |
3628 | } | |
3629 | if (oideq(&two->object.oid, &shifted)) | |
3630 | return two; | |
3631 | return lookup_tree(repo, &shifted); | |
3632 | } | |
3633 | ||
4296d8f1 EN |
3634 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
3635 | { | |
3636 | c->maybe_tree = t; | |
3637 | } | |
3638 | ||
4296d8f1 EN |
3639 | static struct commit *make_virtual_commit(struct repository *repo, |
3640 | struct tree *tree, | |
3641 | const char *comment) | |
3642 | { | |
3643 | struct commit *commit = alloc_commit_node(repo); | |
3644 | ||
3645 | set_merge_remote_desc(commit, comment, (struct object *)commit); | |
3646 | set_commit_tree(commit, tree); | |
3647 | commit->object.parsed = 1; | |
3648 | return commit; | |
3649 | } | |
3650 | ||
231e2dd4 EN |
3651 | static void merge_start(struct merge_options *opt, struct merge_result *result) |
3652 | { | |
f5d9fbc2 EN |
3653 | struct rename_info *renames; |
3654 | int i; | |
3655 | ||
e4171b1b | 3656 | /* Sanity checks on opt */ |
557ac035 | 3657 | trace2_region_enter("merge", "sanity checks", opt->repo); |
e4171b1b EN |
3658 | assert(opt->repo); |
3659 | ||
3660 | assert(opt->branch1 && opt->branch2); | |
3661 | ||
3662 | assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE && | |
3663 | opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE); | |
3664 | assert(opt->rename_limit >= -1); | |
3665 | assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE); | |
3666 | assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1); | |
3667 | ||
3668 | assert(opt->xdl_opts >= 0); | |
3669 | assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL && | |
3670 | opt->recursive_variant <= MERGE_VARIANT_THEIRS); | |
3671 | ||
3672 | /* | |
3673 | * detect_renames, verbosity, buffer_output, and obuf are ignored | |
3674 | * fields that were used by "recursive" rather than "ort" -- but | |
3675 | * sanity check them anyway. | |
3676 | */ | |
3677 | assert(opt->detect_renames >= -1 && | |
3678 | opt->detect_renames <= DIFF_DETECT_COPY); | |
3679 | assert(opt->verbosity >= 0 && opt->verbosity <= 5); | |
3680 | assert(opt->buffer_output <= 2); | |
3681 | assert(opt->obuf.len == 0); | |
3682 | ||
3683 | assert(opt->priv == NULL); | |
cf8937ac EN |
3684 | if (result->priv) { |
3685 | opt->priv = result->priv; | |
3686 | result->priv = NULL; | |
3687 | /* | |
3688 | * opt->priv non-NULL means we had results from a previous | |
3689 | * run; do a few sanity checks that user didn't mess with | |
3690 | * it in an obvious fashion. | |
3691 | */ | |
3692 | assert(opt->priv->call_depth == 0); | |
3693 | assert(!opt->priv->toplevel_dir || | |
3694 | 0 == strlen(opt->priv->toplevel_dir)); | |
3695 | } | |
557ac035 | 3696 | trace2_region_leave("merge", "sanity checks", opt->repo); |
e4171b1b | 3697 | |
c8017176 EN |
3698 | /* Default to histogram diff. Actually, just hardcode it...for now. */ |
3699 | opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF); | |
3700 | ||
ea305a68 EN |
3701 | /* Handle attr direction stuff for renormalization */ |
3702 | if (opt->renormalize) | |
3703 | git_attr_set_direction(GIT_ATTR_CHECKOUT); | |
3704 | ||
e4171b1b | 3705 | /* Initialization of opt->priv, our internal merge data */ |
557ac035 | 3706 | trace2_region_enter("merge", "allocate/init", opt->repo); |
cf8937ac EN |
3707 | if (opt->priv) { |
3708 | clear_or_reinit_internal_opts(opt->priv, 1); | |
3709 | trace2_region_leave("merge", "allocate/init", opt->repo); | |
3710 | return; | |
3711 | } | |
e4171b1b EN |
3712 | opt->priv = xcalloc(1, sizeof(*opt->priv)); |
3713 | ||
f5d9fbc2 EN |
3714 | /* Initialization of various renames fields */ |
3715 | renames = &opt->priv->renames; | |
3716 | for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) { | |
a49b55d5 | 3717 | strintmap_init_with_options(&renames->dirs_removed[i], |
fb52938e | 3718 | NOT_RELEVANT, NULL, 0); |
f5d9fbc2 EN |
3719 | strmap_init_with_options(&renames->dir_rename_count[i], |
3720 | NULL, 1); | |
3721 | strmap_init_with_options(&renames->dir_renames[i], | |
3722 | NULL, 0); | |
a49b55d5 EN |
3723 | strintmap_init_with_options(&renames->relevant_sources[i], |
3724 | 0, NULL, 0); | |
d29bd6d7 EN |
3725 | strmap_init_with_options(&renames->cached_pairs[i], |
3726 | NULL, 1); | |
3727 | strset_init_with_options(&renames->cached_irrelevant[i], | |
3728 | NULL, 1); | |
3729 | strset_init_with_options(&renames->cached_target_names[i], | |
3730 | NULL, 0); | |
f5d9fbc2 EN |
3731 | } |
3732 | ||
e4171b1b EN |
3733 | /* |
3734 | * Although we initialize opt->priv->paths with strdup_strings=0, | |
3735 | * that's just to avoid making yet another copy of an allocated | |
3736 | * string. Putting the entry into paths means we are taking | |
43c1dccb | 3737 | * ownership, so we will later free it. paths_to_free is similar. |
e4171b1b EN |
3738 | * |
3739 | * In contrast, conflicted just has a subset of keys from paths, so | |
3740 | * we don't want to free those (it'd be a duplicate free). | |
3741 | */ | |
3742 | strmap_init_with_options(&opt->priv->paths, NULL, 0); | |
3743 | strmap_init_with_options(&opt->priv->conflicted, NULL, 0); | |
43c1dccb | 3744 | string_list_init(&opt->priv->paths_to_free, 0); |
c5a6f655 EN |
3745 | |
3746 | /* | |
3747 | * keys & strbufs in output will sometimes need to outlive "paths", | |
3748 | * so it will have a copy of relevant keys. It's probably a small | |
3749 | * subset of the overall paths that have special output. | |
3750 | */ | |
3751 | strmap_init(&opt->priv->output); | |
557ac035 EN |
3752 | |
3753 | trace2_region_leave("merge", "allocate/init", opt->repo); | |
231e2dd4 EN |
3754 | } |
3755 | ||
04af1879 EN |
3756 | /*** Function Grouping: merge_incore_*() and their internal variants ***/ |
3757 | ||
231e2dd4 EN |
3758 | /* |
3759 | * Originally from merge_trees_internal(); heavily adapted, though. | |
3760 | */ | |
3761 | static void merge_ort_nonrecursive_internal(struct merge_options *opt, | |
3762 | struct tree *merge_base, | |
3763 | struct tree *side1, | |
3764 | struct tree *side2, | |
3765 | struct merge_result *result) | |
3766 | { | |
3767 | struct object_id working_tree_oid; | |
3768 | ||
3639dfb3 EN |
3769 | if (opt->subtree_shift) { |
3770 | side2 = shift_tree_object(opt->repo, side1, side2, | |
3771 | opt->subtree_shift); | |
3772 | merge_base = shift_tree_object(opt->repo, side1, merge_base, | |
3773 | opt->subtree_shift); | |
3774 | } | |
3775 | ||
557ac035 | 3776 | trace2_region_enter("merge", "collect_merge_info", opt->repo); |
0c0d705b EN |
3777 | if (collect_merge_info(opt, merge_base, side1, side2) != 0) { |
3778 | /* | |
3779 | * TRANSLATORS: The %s arguments are: 1) tree hash of a merge | |
3780 | * base, and 2-3) the trees for the two trees we're merging. | |
3781 | */ | |
3782 | err(opt, _("collecting merge info failed for trees %s, %s, %s"), | |
3783 | oid_to_hex(&merge_base->object.oid), | |
3784 | oid_to_hex(&side1->object.oid), | |
3785 | oid_to_hex(&side2->object.oid)); | |
3786 | result->clean = -1; | |
3787 | return; | |
3788 | } | |
557ac035 | 3789 | trace2_region_leave("merge", "collect_merge_info", opt->repo); |
0c0d705b | 3790 | |
557ac035 | 3791 | trace2_region_enter("merge", "renames", opt->repo); |
231e2dd4 EN |
3792 | result->clean = detect_and_process_renames(opt, merge_base, |
3793 | side1, side2); | |
557ac035 EN |
3794 | trace2_region_leave("merge", "renames", opt->repo); |
3795 | ||
3796 | trace2_region_enter("merge", "process_entries", opt->repo); | |
231e2dd4 | 3797 | process_entries(opt, &working_tree_oid); |
557ac035 | 3798 | trace2_region_leave("merge", "process_entries", opt->repo); |
231e2dd4 EN |
3799 | |
3800 | /* Set return values */ | |
3801 | result->tree = parse_tree_indirect(&working_tree_oid); | |
3802 | /* existence of conflicted entries implies unclean */ | |
3803 | result->clean &= strmap_empty(&opt->priv->conflicted); | |
3804 | if (!opt->priv->call_depth) { | |
3805 | result->priv = opt->priv; | |
3806 | opt->priv = NULL; | |
3807 | } | |
3808 | } | |
3809 | ||
8119214f EN |
3810 | /* |
3811 | * Originally from merge_recursive_internal(); somewhat adapted, though. | |
3812 | */ | |
3813 | static void merge_ort_internal(struct merge_options *opt, | |
3814 | struct commit_list *merge_bases, | |
3815 | struct commit *h1, | |
3816 | struct commit *h2, | |
3817 | struct merge_result *result) | |
3818 | { | |
3819 | struct commit_list *iter; | |
3820 | struct commit *merged_merge_bases; | |
3821 | const char *ancestor_name; | |
3822 | struct strbuf merge_base_abbrev = STRBUF_INIT; | |
3823 | ||
3824 | if (!merge_bases) { | |
3825 | merge_bases = get_merge_bases(h1, h2); | |
3826 | /* See merge-ort.h:merge_incore_recursive() declaration NOTE */ | |
3827 | merge_bases = reverse_commit_list(merge_bases); | |
3828 | } | |
3829 | ||
3830 | merged_merge_bases = pop_commit(&merge_bases); | |
3831 | if (merged_merge_bases == NULL) { | |
3832 | /* if there is no common ancestor, use an empty tree */ | |
3833 | struct tree *tree; | |
3834 | ||
3835 | tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree); | |
3836 | merged_merge_bases = make_virtual_commit(opt->repo, tree, | |
3837 | "ancestor"); | |
3838 | ancestor_name = "empty tree"; | |
3839 | } else if (merge_bases) { | |
3840 | ancestor_name = "merged common ancestors"; | |
3841 | } else { | |
3842 | strbuf_add_unique_abbrev(&merge_base_abbrev, | |
3843 | &merged_merge_bases->object.oid, | |
3844 | DEFAULT_ABBREV); | |
3845 | ancestor_name = merge_base_abbrev.buf; | |
3846 | } | |
3847 | ||
3848 | for (iter = merge_bases; iter; iter = iter->next) { | |
3849 | const char *saved_b1, *saved_b2; | |
3850 | struct commit *prev = merged_merge_bases; | |
3851 | ||
3852 | opt->priv->call_depth++; | |
3853 | /* | |
3854 | * When the merge fails, the result contains files | |
3855 | * with conflict markers. The cleanness flag is | |
3856 | * ignored (unless indicating an error), it was never | |
3857 | * actually used, as result of merge_trees has always | |
3858 | * overwritten it: the committed "conflicts" were | |
3859 | * already resolved. | |
3860 | */ | |
3861 | saved_b1 = opt->branch1; | |
3862 | saved_b2 = opt->branch2; | |
3863 | opt->branch1 = "Temporary merge branch 1"; | |
3864 | opt->branch2 = "Temporary merge branch 2"; | |
3865 | merge_ort_internal(opt, NULL, prev, iter->item, result); | |
3866 | if (result->clean < 0) | |
3867 | return; | |
3868 | opt->branch1 = saved_b1; | |
3869 | opt->branch2 = saved_b2; | |
3870 | opt->priv->call_depth--; | |
3871 | ||
3872 | merged_merge_bases = make_virtual_commit(opt->repo, | |
3873 | result->tree, | |
3874 | "merged tree"); | |
3875 | commit_list_insert(prev, &merged_merge_bases->parents); | |
3876 | commit_list_insert(iter->item, | |
3877 | &merged_merge_bases->parents->next); | |
3878 | ||
3879 | clear_or_reinit_internal_opts(opt->priv, 1); | |
3880 | } | |
3881 | ||
3882 | opt->ancestor = ancestor_name; | |
3883 | merge_ort_nonrecursive_internal(opt, | |
3884 | repo_get_commit_tree(opt->repo, | |
3885 | merged_merge_bases), | |
3886 | repo_get_commit_tree(opt->repo, h1), | |
3887 | repo_get_commit_tree(opt->repo, h2), | |
3888 | result); | |
3889 | strbuf_release(&merge_base_abbrev); | |
3890 | opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */ | |
3891 | } | |
3892 | ||
17e5574b EN |
3893 | void merge_incore_nonrecursive(struct merge_options *opt, |
3894 | struct tree *merge_base, | |
3895 | struct tree *side1, | |
3896 | struct tree *side2, | |
3897 | struct merge_result *result) | |
3898 | { | |
557ac035 EN |
3899 | trace2_region_enter("merge", "incore_nonrecursive", opt->repo); |
3900 | ||
3901 | trace2_region_enter("merge", "merge_start", opt->repo); | |
231e2dd4 EN |
3902 | assert(opt->ancestor != NULL); |
3903 | merge_start(opt, result); | |
557ac035 EN |
3904 | trace2_region_leave("merge", "merge_start", opt->repo); |
3905 | ||
231e2dd4 | 3906 | merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result); |
557ac035 | 3907 | trace2_region_leave("merge", "incore_nonrecursive", opt->repo); |
17e5574b EN |
3908 | } |
3909 | ||
3910 | void merge_incore_recursive(struct merge_options *opt, | |
3911 | struct commit_list *merge_bases, | |
3912 | struct commit *side1, | |
3913 | struct commit *side2, | |
3914 | struct merge_result *result) | |
3915 | { | |
557ac035 EN |
3916 | trace2_region_enter("merge", "incore_recursive", opt->repo); |
3917 | ||
8119214f EN |
3918 | /* We set the ancestor label based on the merge_bases */ |
3919 | assert(opt->ancestor == NULL); | |
3920 | ||
557ac035 | 3921 | trace2_region_enter("merge", "merge_start", opt->repo); |
8119214f | 3922 | merge_start(opt, result); |
557ac035 EN |
3923 | trace2_region_leave("merge", "merge_start", opt->repo); |
3924 | ||
8119214f | 3925 | merge_ort_internal(opt, merge_bases, side1, side2, result); |
557ac035 | 3926 | trace2_region_leave("merge", "incore_recursive", opt->repo); |
17e5574b | 3927 | } |