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