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