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