]>
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 EN |
54 | struct rename_info { |
55 | /* | |
56 | * pairs: pairing of filenames from diffcore_rename() | |
57 | * | |
58 | * Index 1 and 2 correspond to sides 1 & 2 as used in | |
59 | * conflict_info.stages. Index 0 unused. | |
60 | */ | |
61 | struct diff_queue_struct pairs[3]; | |
62 | ||
63 | /* | |
64 | * needed_limit: value needed for inexact rename detection to run | |
65 | * | |
66 | * If the current rename limit wasn't high enough for inexact | |
67 | * rename detection to run, this records the limit needed. Otherwise, | |
68 | * this value remains 0. | |
69 | */ | |
70 | int needed_limit; | |
71 | }; | |
72 | ||
5b59c3db EN |
73 | struct merge_options_internal { |
74 | /* | |
75 | * paths: primary data structure in all of merge ort. | |
76 | * | |
77 | * The keys of paths: | |
78 | * * are full relative paths from the toplevel of the repository | |
79 | * (e.g. "drivers/firmware/raspberrypi.c"). | |
80 | * * store all relevant paths in the repo, both directories and | |
81 | * files (e.g. drivers, drivers/firmware would also be included) | |
82 | * * these keys serve to intern all the path strings, which allows | |
83 | * us to do pointer comparison on directory names instead of | |
84 | * strcmp; we just have to be careful to use the interned strings. | |
43c1dccb EN |
85 | * (Technically paths_to_free may track some strings that were |
86 | * removed from froms paths.) | |
5b59c3db EN |
87 | * |
88 | * The values of paths: | |
89 | * * either a pointer to a merged_info, or a conflict_info struct | |
90 | * * merged_info contains all relevant information for a | |
91 | * non-conflicted entry. | |
92 | * * conflict_info contains a merged_info, plus any additional | |
93 | * information about a conflict such as the higher orders stages | |
94 | * involved and the names of the paths those came from (handy | |
95 | * once renames get involved). | |
96 | * * a path may start "conflicted" (i.e. point to a conflict_info) | |
97 | * and then a later step (e.g. three-way content merge) determines | |
98 | * it can be cleanly merged, at which point it'll be marked clean | |
99 | * and the algorithm will ignore any data outside the contained | |
100 | * merged_info for that entry | |
101 | * * If an entry remains conflicted, the merged_info portion of a | |
102 | * conflict_info will later be filled with whatever version of | |
103 | * the file should be placed in the working directory (e.g. an | |
104 | * as-merged-as-possible variation that contains conflict markers). | |
105 | */ | |
106 | struct strmap paths; | |
107 | ||
108 | /* | |
109 | * conflicted: a subset of keys->values from "paths" | |
110 | * | |
111 | * conflicted is basically an optimization between process_entries() | |
112 | * and record_conflicted_index_entries(); the latter could loop over | |
113 | * ALL the entries in paths AGAIN and look for the ones that are | |
114 | * still conflicted, but since process_entries() has to loop over | |
115 | * all of them, it saves the ones it couldn't resolve in this strmap | |
116 | * so that record_conflicted_index_entries() can iterate just the | |
117 | * relevant entries. | |
118 | */ | |
119 | struct strmap conflicted; | |
120 | ||
43c1dccb EN |
121 | /* |
122 | * paths_to_free: additional list of strings to free | |
123 | * | |
124 | * If keys are removed from "paths", they are added to paths_to_free | |
125 | * to ensure they are later freed. We avoid free'ing immediately since | |
126 | * other places (e.g. conflict_info.pathnames[]) may still be | |
127 | * referencing these paths. | |
128 | */ | |
129 | struct string_list paths_to_free; | |
130 | ||
c5a6f655 EN |
131 | /* |
132 | * output: special messages and conflict notices for various paths | |
133 | * | |
134 | * This is a map of pathnames (a subset of the keys in "paths" above) | |
135 | * to strbufs. It gathers various warning/conflict/notice messages | |
136 | * for later processing. | |
137 | */ | |
138 | struct strmap output; | |
139 | ||
864075ec EN |
140 | /* |
141 | * renames: various data relating to rename detection | |
142 | */ | |
143 | struct rename_info renames; | |
144 | ||
5b59c3db EN |
145 | /* |
146 | * current_dir_name: temporary var used in collect_merge_info_callback() | |
147 | * | |
148 | * Used to set merged_info.directory_name; see documentation for that | |
149 | * variable and the requirements placed on that field. | |
150 | */ | |
151 | const char *current_dir_name; | |
152 | ||
153 | /* call_depth: recursion level counter for merging merge bases */ | |
154 | int call_depth; | |
155 | }; | |
156 | ||
157 | struct version_info { | |
158 | struct object_id oid; | |
159 | unsigned short mode; | |
160 | }; | |
161 | ||
162 | struct merged_info { | |
163 | /* if is_null, ignore result. otherwise result has oid & mode */ | |
164 | struct version_info result; | |
165 | unsigned is_null:1; | |
166 | ||
167 | /* | |
168 | * clean: whether the path in question is cleanly merged. | |
169 | * | |
170 | * see conflict_info.merged for more details. | |
171 | */ | |
172 | unsigned clean:1; | |
173 | ||
174 | /* | |
175 | * basename_offset: offset of basename of path. | |
176 | * | |
177 | * perf optimization to avoid recomputing offset of final '/' | |
178 | * character in pathname (0 if no '/' in pathname). | |
179 | */ | |
180 | size_t basename_offset; | |
181 | ||
182 | /* | |
183 | * directory_name: containing directory name. | |
184 | * | |
185 | * Note that we assume directory_name is constructed such that | |
186 | * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name, | |
187 | * i.e. string equality is equivalent to pointer equality. For this | |
188 | * to hold, we have to be careful setting directory_name. | |
189 | */ | |
190 | const char *directory_name; | |
191 | }; | |
192 | ||
193 | struct conflict_info { | |
194 | /* | |
195 | * merged: the version of the path that will be written to working tree | |
196 | * | |
197 | * WARNING: It is critical to check merged.clean and ensure it is 0 | |
198 | * before reading any conflict_info fields outside of merged. | |
199 | * Allocated merge_info structs will always have clean set to 1. | |
200 | * Allocated conflict_info structs will have merged.clean set to 0 | |
201 | * initially. The merged.clean field is how we know if it is safe | |
202 | * to access other parts of conflict_info besides merged; if a | |
203 | * conflict_info's merged.clean is changed to 1, the rest of the | |
204 | * algorithm is not allowed to look at anything outside of the | |
205 | * merged member anymore. | |
206 | */ | |
207 | struct merged_info merged; | |
208 | ||
209 | /* oids & modes from each of the three trees for this path */ | |
210 | struct version_info stages[3]; | |
211 | ||
212 | /* pathnames for each stage; may differ due to rename detection */ | |
213 | const char *pathnames[3]; | |
214 | ||
215 | /* Whether this path is/was involved in a directory/file conflict */ | |
216 | unsigned df_conflict:1; | |
217 | ||
1c7873cd EN |
218 | /* |
219 | * Whether this path is/was involved in a non-content conflict other | |
220 | * than a directory/file conflict (e.g. rename/rename, rename/delete, | |
221 | * file location based on possible directory rename). | |
222 | */ | |
223 | unsigned path_conflict:1; | |
224 | ||
5b59c3db EN |
225 | /* |
226 | * For filemask and dirmask, the ith bit corresponds to whether the | |
227 | * ith entry is a file (filemask) or a directory (dirmask). Thus, | |
228 | * filemask & dirmask is always zero, and filemask | dirmask is at | |
229 | * most 7 but can be less when a path does not appear as either a | |
230 | * file or a directory on at least one side of history. | |
231 | * | |
232 | * Note that these masks are related to enum merge_side, as the ith | |
233 | * entry corresponds to side i. | |
234 | * | |
235 | * These values come from a traverse_trees() call; more info may be | |
236 | * found looking at tree-walk.h's struct traverse_info, | |
237 | * particularly the documentation above the "fn" member (note that | |
238 | * filemask = mask & ~dirmask from that documentation). | |
239 | */ | |
240 | unsigned filemask:3; | |
241 | unsigned dirmask:3; | |
242 | ||
243 | /* | |
244 | * Optimization to track which stages match, to avoid the need to | |
245 | * recompute it in multiple steps. Either 0 or at least 2 bits are | |
246 | * set; if at least 2 bits are set, their corresponding stages match. | |
247 | */ | |
248 | unsigned match_mask:3; | |
249 | }; | |
250 | ||
04af1879 EN |
251 | /*** Function Grouping: various utility functions ***/ |
252 | ||
98bf9841 EN |
253 | /* |
254 | * For the next three macros, see warning for conflict_info.merged. | |
255 | * | |
256 | * In each of the below, mi is a struct merged_info*, and ci was defined | |
257 | * as a struct conflict_info* (but we need to verify ci isn't actually | |
258 | * pointed at a struct merged_info*). | |
259 | * | |
260 | * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise. | |
261 | * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one. | |
262 | * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first. | |
263 | */ | |
264 | #define INITIALIZE_CI(ci, mi) do { \ | |
265 | (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \ | |
266 | } while (0) | |
267 | #define VERIFY_CI(ci) assert(ci && !ci->merged.clean); | |
268 | #define ASSIGN_AND_VERIFY_CI(ci, mi) do { \ | |
269 | (ci) = (struct conflict_info *)(mi); \ | |
270 | assert((ci) && !(mi)->clean); \ | |
271 | } while (0) | |
272 | ||
89422d29 EN |
273 | static void free_strmap_strings(struct strmap *map) |
274 | { | |
275 | struct hashmap_iter iter; | |
276 | struct strmap_entry *entry; | |
277 | ||
278 | strmap_for_each_entry(map, &iter, entry) { | |
279 | free((char*)entry->key); | |
280 | } | |
281 | } | |
282 | ||
43e9c4ee EN |
283 | static void clear_or_reinit_internal_opts(struct merge_options_internal *opti, |
284 | int reinitialize) | |
101bc5bc | 285 | { |
43e9c4ee EN |
286 | void (*strmap_func)(struct strmap *, int) = |
287 | reinitialize ? strmap_partial_clear : strmap_clear; | |
101bc5bc EN |
288 | |
289 | /* | |
290 | * We marked opti->paths with strdup_strings = 0, so that we | |
291 | * wouldn't have to make another copy of the fullpath created by | |
292 | * make_traverse_path from setup_path_info(). But, now that we've | |
293 | * used it and have no other references to these strings, it is time | |
294 | * to deallocate them. | |
295 | */ | |
296 | free_strmap_strings(&opti->paths); | |
43e9c4ee | 297 | strmap_func(&opti->paths, 1); |
101bc5bc EN |
298 | |
299 | /* | |
300 | * All keys and values in opti->conflicted are a subset of those in | |
301 | * opti->paths. We don't want to deallocate anything twice, so we | |
302 | * don't free the keys and we pass 0 for free_values. | |
303 | */ | |
43e9c4ee | 304 | strmap_func(&opti->conflicted, 0); |
43c1dccb EN |
305 | |
306 | /* | |
307 | * opti->paths_to_free is similar to opti->paths; we created it with | |
308 | * strdup_strings = 0 to avoid making _another_ copy of the fullpath | |
309 | * but now that we've used it and have no other references to these | |
310 | * strings, it is time to deallocate them. We do so by temporarily | |
311 | * setting strdup_strings to 1. | |
312 | */ | |
313 | opti->paths_to_free.strdup_strings = 1; | |
314 | string_list_clear(&opti->paths_to_free, 0); | |
315 | opti->paths_to_free.strdup_strings = 0; | |
c5a6f655 EN |
316 | |
317 | if (!reinitialize) { | |
318 | struct hashmap_iter iter; | |
319 | struct strmap_entry *e; | |
320 | ||
321 | /* Release and free each strbuf found in output */ | |
322 | strmap_for_each_entry(&opti->output, &iter, e) { | |
323 | struct strbuf *sb = e->value; | |
324 | strbuf_release(sb); | |
325 | /* | |
326 | * While strictly speaking we don't need to free(sb) | |
327 | * here because we could pass free_values=1 when | |
328 | * calling strmap_clear() on opti->output, that would | |
329 | * require strmap_clear to do another | |
330 | * strmap_for_each_entry() loop, so we just free it | |
331 | * while we're iterating anyway. | |
332 | */ | |
333 | free(sb); | |
334 | } | |
335 | strmap_clear(&opti->output, 0); | |
336 | } | |
101bc5bc EN |
337 | } |
338 | ||
0c0d705b EN |
339 | static int err(struct merge_options *opt, const char *err, ...) |
340 | { | |
341 | va_list params; | |
342 | struct strbuf sb = STRBUF_INIT; | |
343 | ||
344 | strbuf_addstr(&sb, "error: "); | |
345 | va_start(params, err); | |
346 | strbuf_vaddf(&sb, err, params); | |
347 | va_end(params); | |
348 | ||
349 | error("%s", sb.buf); | |
350 | strbuf_release(&sb); | |
351 | ||
352 | return -1; | |
353 | } | |
354 | ||
c73cda76 EN |
355 | static void format_commit(struct strbuf *sb, |
356 | int indent, | |
357 | struct commit *commit) | |
358 | { | |
70f19c7f EN |
359 | struct merge_remote_desc *desc; |
360 | struct pretty_print_context ctx = {0}; | |
361 | ctx.abbrev = DEFAULT_ABBREV; | |
362 | ||
363 | strbuf_addchars(sb, ' ', indent); | |
364 | desc = merge_remote_util(commit); | |
365 | if (desc) { | |
366 | strbuf_addf(sb, "virtual %s\n", desc->name); | |
367 | return; | |
368 | } | |
369 | ||
370 | format_commit_message(commit, "%h %s", sb, &ctx); | |
371 | strbuf_addch(sb, '\n'); | |
c73cda76 EN |
372 | } |
373 | ||
c5a6f655 EN |
374 | __attribute__((format (printf, 4, 5))) |
375 | static void path_msg(struct merge_options *opt, | |
376 | const char *path, | |
377 | int omittable_hint, /* skippable under --remerge-diff */ | |
378 | const char *fmt, ...) | |
379 | { | |
380 | va_list ap; | |
381 | struct strbuf *sb = strmap_get(&opt->priv->output, path); | |
382 | if (!sb) { | |
383 | sb = xmalloc(sizeof(*sb)); | |
384 | strbuf_init(sb, 0); | |
385 | strmap_put(&opt->priv->output, path, sb); | |
386 | } | |
387 | ||
388 | va_start(ap, fmt); | |
389 | strbuf_vaddf(sb, fmt, ap); | |
390 | va_end(ap); | |
391 | ||
392 | strbuf_addch(sb, '\n'); | |
393 | } | |
394 | ||
5a1a1e8e EN |
395 | /* add a string to a strbuf, but converting "/" to "_" */ |
396 | static void add_flattened_path(struct strbuf *out, const char *s) | |
397 | { | |
398 | size_t i = out->len; | |
399 | strbuf_addstr(out, s); | |
400 | for (; i < out->len; i++) | |
401 | if (out->buf[i] == '/') | |
402 | out->buf[i] = '_'; | |
403 | } | |
404 | ||
23366d2a EN |
405 | static char *unique_path(struct strmap *existing_paths, |
406 | const char *path, | |
407 | const char *branch) | |
408 | { | |
5a1a1e8e EN |
409 | struct strbuf newpath = STRBUF_INIT; |
410 | int suffix = 0; | |
411 | size_t base_len; | |
412 | ||
413 | strbuf_addf(&newpath, "%s~", path); | |
414 | add_flattened_path(&newpath, branch); | |
415 | ||
416 | base_len = newpath.len; | |
417 | while (strmap_contains(existing_paths, newpath.buf)) { | |
418 | strbuf_setlen(&newpath, base_len); | |
419 | strbuf_addf(&newpath, "_%d", suffix++); | |
420 | } | |
421 | ||
422 | return strbuf_detach(&newpath, NULL); | |
23366d2a EN |
423 | } |
424 | ||
04af1879 EN |
425 | /*** Function Grouping: functions related to collect_merge_info() ***/ |
426 | ||
98bf9841 EN |
427 | static void setup_path_info(struct merge_options *opt, |
428 | struct string_list_item *result, | |
429 | const char *current_dir_name, | |
430 | int current_dir_name_len, | |
431 | char *fullpath, /* we'll take over ownership */ | |
432 | struct name_entry *names, | |
433 | struct name_entry *merged_version, | |
434 | unsigned is_null, /* boolean */ | |
435 | unsigned df_conflict, /* boolean */ | |
436 | unsigned filemask, | |
437 | unsigned dirmask, | |
438 | int resolved /* boolean */) | |
439 | { | |
440 | /* result->util is void*, so mi is a convenience typed variable */ | |
441 | struct merged_info *mi; | |
442 | ||
443 | assert(!is_null || resolved); | |
444 | assert(!df_conflict || !resolved); /* df_conflict implies !resolved */ | |
445 | assert(resolved == (merged_version != NULL)); | |
446 | ||
447 | mi = xcalloc(1, resolved ? sizeof(struct merged_info) : | |
448 | sizeof(struct conflict_info)); | |
449 | mi->directory_name = current_dir_name; | |
450 | mi->basename_offset = current_dir_name_len; | |
451 | mi->clean = !!resolved; | |
452 | if (resolved) { | |
453 | mi->result.mode = merged_version->mode; | |
454 | oidcpy(&mi->result.oid, &merged_version->oid); | |
455 | mi->is_null = !!is_null; | |
456 | } else { | |
457 | int i; | |
458 | struct conflict_info *ci; | |
459 | ||
460 | ASSIGN_AND_VERIFY_CI(ci, mi); | |
461 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
462 | ci->pathnames[i] = fullpath; | |
463 | ci->stages[i].mode = names[i].mode; | |
464 | oidcpy(&ci->stages[i].oid, &names[i].oid); | |
465 | } | |
466 | ci->filemask = filemask; | |
467 | ci->dirmask = dirmask; | |
468 | ci->df_conflict = !!df_conflict; | |
469 | if (dirmask) | |
470 | /* | |
471 | * Assume is_null for now, but if we have entries | |
472 | * under the directory then when it is complete in | |
473 | * write_completed_directory() it'll update this. | |
474 | * Also, for D/F conflicts, we have to handle the | |
475 | * directory first, then clear this bit and process | |
476 | * the file to see how it is handled -- that occurs | |
477 | * near the top of process_entry(). | |
478 | */ | |
479 | mi->is_null = 1; | |
480 | } | |
481 | strmap_put(&opt->priv->paths, fullpath, mi); | |
482 | result->string = fullpath; | |
483 | result->util = mi; | |
484 | } | |
485 | ||
d2bc1994 EN |
486 | static int collect_merge_info_callback(int n, |
487 | unsigned long mask, | |
488 | unsigned long dirmask, | |
489 | struct name_entry *names, | |
490 | struct traverse_info *info) | |
491 | { | |
492 | /* | |
493 | * n is 3. Always. | |
494 | * common ancestor (mbase) has mask 1, and stored in index 0 of names | |
495 | * head of side 1 (side1) has mask 2, and stored in index 1 of names | |
496 | * head of side 2 (side2) has mask 4, and stored in index 2 of names | |
497 | */ | |
498 | struct merge_options *opt = info->data; | |
499 | struct merge_options_internal *opti = opt->priv; | |
98bf9841 EN |
500 | struct string_list_item pi; /* Path Info */ |
501 | struct conflict_info *ci; /* typed alias to pi.util (which is void*) */ | |
d2bc1994 EN |
502 | struct name_entry *p; |
503 | size_t len; | |
504 | char *fullpath; | |
98bf9841 | 505 | const char *dirname = opti->current_dir_name; |
d2bc1994 | 506 | unsigned filemask = mask & ~dirmask; |
34e557af | 507 | unsigned match_mask = 0; /* will be updated below */ |
d2bc1994 EN |
508 | unsigned mbase_null = !(mask & 1); |
509 | unsigned side1_null = !(mask & 2); | |
510 | unsigned side2_null = !(mask & 4); | |
885f0063 EN |
511 | unsigned side1_matches_mbase = (!side1_null && !mbase_null && |
512 | names[0].mode == names[1].mode && | |
513 | oideq(&names[0].oid, &names[1].oid)); | |
514 | unsigned side2_matches_mbase = (!side2_null && !mbase_null && | |
515 | names[0].mode == names[2].mode && | |
516 | oideq(&names[0].oid, &names[2].oid)); | |
517 | unsigned sides_match = (!side1_null && !side2_null && | |
518 | names[1].mode == names[2].mode && | |
519 | oideq(&names[1].oid, &names[2].oid)); | |
d2bc1994 | 520 | |
34e557af EN |
521 | /* |
522 | * Note: When a path is a file on one side of history and a directory | |
523 | * in another, we have a directory/file conflict. In such cases, if | |
524 | * the conflict doesn't resolve from renames and deletions, then we | |
525 | * always leave directories where they are and move files out of the | |
526 | * way. Thus, while struct conflict_info has a df_conflict field to | |
527 | * track such conflicts, we ignore that field for any directories at | |
528 | * a path and only pay attention to it for files at the given path. | |
529 | * The fact that we leave directories were they are also means that | |
530 | * we do not need to worry about getting additional df_conflict | |
531 | * information propagated from parent directories down to children | |
532 | * (unlike, say traverse_trees_recursive() in unpack-trees.c, which | |
533 | * sets a newinfo.df_conflicts field specifically to propagate it). | |
534 | */ | |
535 | unsigned df_conflict = (filemask != 0) && (dirmask != 0); | |
536 | ||
d2bc1994 EN |
537 | /* n = 3 is a fundamental assumption. */ |
538 | if (n != 3) | |
539 | BUG("Called collect_merge_info_callback wrong"); | |
540 | ||
541 | /* | |
542 | * A bunch of sanity checks verifying that traverse_trees() calls | |
543 | * us the way I expect. Could just remove these at some point, | |
544 | * though maybe they are helpful to future code readers. | |
545 | */ | |
546 | assert(mbase_null == is_null_oid(&names[0].oid)); | |
547 | assert(side1_null == is_null_oid(&names[1].oid)); | |
548 | assert(side2_null == is_null_oid(&names[2].oid)); | |
549 | assert(!mbase_null || !side1_null || !side2_null); | |
550 | assert(mask > 0 && mask < 8); | |
551 | ||
34e557af EN |
552 | /* Determine match_mask */ |
553 | if (side1_matches_mbase) | |
554 | match_mask = (side2_matches_mbase ? 7 : 3); | |
555 | else if (side2_matches_mbase) | |
556 | match_mask = 5; | |
557 | else if (sides_match) | |
558 | match_mask = 6; | |
559 | ||
d2bc1994 EN |
560 | /* |
561 | * Get the name of the relevant filepath, which we'll pass to | |
562 | * setup_path_info() for tracking. | |
563 | */ | |
564 | p = names; | |
565 | while (!p->mode) | |
566 | p++; | |
567 | len = traverse_path_len(info, p->pathlen); | |
568 | ||
569 | /* +1 in both of the following lines to include the NUL byte */ | |
570 | fullpath = xmalloc(len + 1); | |
571 | make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen); | |
572 | ||
291f29ca EN |
573 | /* |
574 | * If mbase, side1, and side2 all match, we can resolve early. Even | |
575 | * if these are trees, there will be no renames or anything | |
576 | * underneath. | |
577 | */ | |
578 | if (side1_matches_mbase && side2_matches_mbase) { | |
579 | /* mbase, side1, & side2 all match; use mbase as resolution */ | |
580 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, | |
581 | names, names+0, mbase_null, 0, | |
582 | filemask, dirmask, 1); | |
583 | return mask; | |
584 | } | |
585 | ||
d2bc1994 | 586 | /* |
98bf9841 EN |
587 | * Record information about the path so we can resolve later in |
588 | * process_entries. | |
d2bc1994 | 589 | */ |
98bf9841 EN |
590 | setup_path_info(opt, &pi, dirname, info->pathlen, fullpath, |
591 | names, NULL, 0, df_conflict, filemask, dirmask, 0); | |
592 | ||
593 | ci = pi.util; | |
594 | VERIFY_CI(ci); | |
34e557af | 595 | ci->match_mask = match_mask; |
d2bc1994 EN |
596 | |
597 | /* If dirmask, recurse into subdirectories */ | |
598 | if (dirmask) { | |
599 | struct traverse_info newinfo; | |
600 | struct tree_desc t[3]; | |
601 | void *buf[3] = {NULL, NULL, NULL}; | |
602 | const char *original_dir_name; | |
603 | int i, ret; | |
604 | ||
605 | ci->match_mask &= filemask; | |
606 | newinfo = *info; | |
607 | newinfo.prev = info; | |
608 | newinfo.name = p->path; | |
609 | newinfo.namelen = p->pathlen; | |
610 | newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1); | |
34e557af EN |
611 | /* |
612 | * If this directory we are about to recurse into cared about | |
613 | * its parent directory (the current directory) having a D/F | |
614 | * conflict, then we'd propagate the masks in this way: | |
615 | * newinfo.df_conflicts |= (mask & ~dirmask); | |
616 | * But we don't worry about propagating D/F conflicts. (See | |
617 | * comment near setting of local df_conflict variable near | |
618 | * the beginning of this function). | |
619 | */ | |
d2bc1994 EN |
620 | |
621 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
885f0063 EN |
622 | if (i == 1 && side1_matches_mbase) |
623 | t[1] = t[0]; | |
624 | else if (i == 2 && side2_matches_mbase) | |
625 | t[2] = t[0]; | |
626 | else if (i == 2 && sides_match) | |
627 | t[2] = t[1]; | |
628 | else { | |
629 | const struct object_id *oid = NULL; | |
630 | if (dirmask & 1) | |
631 | oid = &names[i].oid; | |
632 | buf[i] = fill_tree_descriptor(opt->repo, | |
633 | t + i, oid); | |
634 | } | |
d2bc1994 EN |
635 | dirmask >>= 1; |
636 | } | |
637 | ||
638 | original_dir_name = opti->current_dir_name; | |
98bf9841 | 639 | opti->current_dir_name = pi.string; |
d2bc1994 EN |
640 | ret = traverse_trees(NULL, 3, t, &newinfo); |
641 | opti->current_dir_name = original_dir_name; | |
642 | ||
643 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) | |
644 | free(buf[i]); | |
645 | ||
646 | if (ret < 0) | |
647 | return -1; | |
648 | } | |
649 | ||
650 | return mask; | |
651 | } | |
652 | ||
231e2dd4 EN |
653 | static int collect_merge_info(struct merge_options *opt, |
654 | struct tree *merge_base, | |
655 | struct tree *side1, | |
656 | struct tree *side2) | |
657 | { | |
d2bc1994 EN |
658 | int ret; |
659 | struct tree_desc t[3]; | |
660 | struct traverse_info info; | |
661 | const char *toplevel_dir_placeholder = ""; | |
662 | ||
663 | opt->priv->current_dir_name = toplevel_dir_placeholder; | |
664 | setup_traverse_info(&info, toplevel_dir_placeholder); | |
665 | info.fn = collect_merge_info_callback; | |
666 | info.data = opt; | |
667 | info.show_all_errors = 1; | |
668 | ||
669 | parse_tree(merge_base); | |
670 | parse_tree(side1); | |
671 | parse_tree(side2); | |
672 | init_tree_desc(t + 0, merge_base->buffer, merge_base->size); | |
673 | init_tree_desc(t + 1, side1->buffer, side1->size); | |
674 | init_tree_desc(t + 2, side2->buffer, side2->size); | |
675 | ||
676 | ret = traverse_trees(NULL, 3, t, &info); | |
677 | ||
678 | return ret; | |
231e2dd4 EN |
679 | } |
680 | ||
04af1879 EN |
681 | /*** Function Grouping: functions related to threeway content merges ***/ |
682 | ||
c73cda76 EN |
683 | static int find_first_merges(struct repository *repo, |
684 | const char *path, | |
685 | struct commit *a, | |
686 | struct commit *b, | |
687 | struct object_array *result) | |
688 | { | |
4204cd59 EN |
689 | int i, j; |
690 | struct object_array merges = OBJECT_ARRAY_INIT; | |
691 | struct commit *commit; | |
692 | int contains_another; | |
693 | ||
694 | char merged_revision[GIT_MAX_HEXSZ + 2]; | |
695 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
696 | "--all", merged_revision, NULL }; | |
697 | struct rev_info revs; | |
698 | struct setup_revision_opt rev_opts; | |
699 | ||
700 | memset(result, 0, sizeof(struct object_array)); | |
701 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
702 | ||
703 | /* get all revisions that merge commit a */ | |
704 | xsnprintf(merged_revision, sizeof(merged_revision), "^%s", | |
705 | oid_to_hex(&a->object.oid)); | |
706 | repo_init_revisions(repo, &revs, NULL); | |
707 | rev_opts.submodule = path; | |
708 | /* FIXME: can't handle linked worktrees in submodules yet */ | |
709 | revs.single_worktree = path != NULL; | |
710 | setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); | |
711 | ||
712 | /* save all revisions from the above list that contain b */ | |
713 | if (prepare_revision_walk(&revs)) | |
714 | die("revision walk setup failed"); | |
715 | while ((commit = get_revision(&revs)) != NULL) { | |
716 | struct object *o = &(commit->object); | |
717 | if (in_merge_bases(b, commit)) | |
718 | add_object_array(o, NULL, &merges); | |
719 | } | |
720 | reset_revision_walk(); | |
721 | ||
722 | /* Now we've got all merges that contain a and b. Prune all | |
723 | * merges that contain another found merge and save them in | |
724 | * result. | |
725 | */ | |
726 | for (i = 0; i < merges.nr; i++) { | |
727 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
728 | ||
729 | contains_another = 0; | |
730 | for (j = 0; j < merges.nr; j++) { | |
731 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
732 | if (i != j && in_merge_bases(m2, m1)) { | |
733 | contains_another = 1; | |
734 | break; | |
735 | } | |
736 | } | |
737 | ||
738 | if (!contains_another) | |
739 | add_object_array(merges.objects[i].item, NULL, result); | |
740 | } | |
741 | ||
742 | object_array_clear(&merges); | |
743 | return result->nr; | |
c73cda76 EN |
744 | } |
745 | ||
62fdec17 EN |
746 | static int merge_submodule(struct merge_options *opt, |
747 | const char *path, | |
748 | const struct object_id *o, | |
749 | const struct object_id *a, | |
750 | const struct object_id *b, | |
751 | struct object_id *result) | |
752 | { | |
c73cda76 EN |
753 | struct commit *commit_o, *commit_a, *commit_b; |
754 | int parent_count; | |
755 | struct object_array merges; | |
756 | struct strbuf sb = STRBUF_INIT; | |
757 | ||
758 | int i; | |
759 | int search = !opt->priv->call_depth; | |
760 | ||
761 | /* store fallback answer in result in case we fail */ | |
762 | oidcpy(result, opt->priv->call_depth ? o : a); | |
763 | ||
764 | /* we can not handle deletion conflicts */ | |
765 | if (is_null_oid(o)) | |
766 | return 0; | |
767 | if (is_null_oid(a)) | |
768 | return 0; | |
769 | if (is_null_oid(b)) | |
770 | return 0; | |
771 | ||
772 | if (add_submodule_odb(path)) { | |
773 | path_msg(opt, path, 0, | |
774 | _("Failed to merge submodule %s (not checked out)"), | |
775 | path); | |
776 | return 0; | |
777 | } | |
778 | ||
779 | if (!(commit_o = lookup_commit_reference(opt->repo, o)) || | |
780 | !(commit_a = lookup_commit_reference(opt->repo, a)) || | |
781 | !(commit_b = lookup_commit_reference(opt->repo, b))) { | |
782 | path_msg(opt, path, 0, | |
783 | _("Failed to merge submodule %s (commits not present)"), | |
784 | path); | |
785 | return 0; | |
786 | } | |
787 | ||
788 | /* check whether both changes are forward */ | |
789 | if (!in_merge_bases(commit_o, commit_a) || | |
790 | !in_merge_bases(commit_o, commit_b)) { | |
791 | path_msg(opt, path, 0, | |
792 | _("Failed to merge submodule %s " | |
793 | "(commits don't follow merge-base)"), | |
794 | path); | |
795 | return 0; | |
796 | } | |
797 | ||
798 | /* Case #1: a is contained in b or vice versa */ | |
799 | if (in_merge_bases(commit_a, commit_b)) { | |
800 | oidcpy(result, b); | |
801 | path_msg(opt, path, 1, | |
802 | _("Note: Fast-forwarding submodule %s to %s"), | |
803 | path, oid_to_hex(b)); | |
804 | return 1; | |
805 | } | |
806 | if (in_merge_bases(commit_b, commit_a)) { | |
807 | oidcpy(result, a); | |
808 | path_msg(opt, path, 1, | |
809 | _("Note: Fast-forwarding submodule %s to %s"), | |
810 | path, oid_to_hex(a)); | |
811 | return 1; | |
812 | } | |
813 | ||
814 | /* | |
815 | * Case #2: There are one or more merges that contain a and b in | |
816 | * the submodule. If there is only one, then present it as a | |
817 | * suggestion to the user, but leave it marked unmerged so the | |
818 | * user needs to confirm the resolution. | |
819 | */ | |
820 | ||
821 | /* Skip the search if makes no sense to the calling context. */ | |
822 | if (!search) | |
823 | return 0; | |
824 | ||
825 | /* find commit which merges them */ | |
826 | parent_count = find_first_merges(opt->repo, path, commit_a, commit_b, | |
827 | &merges); | |
828 | switch (parent_count) { | |
829 | case 0: | |
830 | path_msg(opt, path, 0, _("Failed to merge submodule %s"), path); | |
831 | break; | |
832 | ||
833 | case 1: | |
834 | format_commit(&sb, 4, | |
835 | (struct commit *)merges.objects[0].item); | |
836 | path_msg(opt, path, 0, | |
837 | _("Failed to merge submodule %s, but a possible merge " | |
838 | "resolution exists:\n%s\n"), | |
839 | path, sb.buf); | |
840 | path_msg(opt, path, 1, | |
841 | _("If this is correct simply add it to the index " | |
842 | "for example\n" | |
843 | "by using:\n\n" | |
844 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" | |
845 | "which will accept this suggestion.\n"), | |
846 | oid_to_hex(&merges.objects[0].item->oid), path); | |
847 | strbuf_release(&sb); | |
848 | break; | |
849 | default: | |
850 | for (i = 0; i < merges.nr; i++) | |
851 | format_commit(&sb, 4, | |
852 | (struct commit *)merges.objects[i].item); | |
853 | path_msg(opt, path, 0, | |
854 | _("Failed to merge submodule %s, but multiple " | |
855 | "possible merges exist:\n%s"), path, sb.buf); | |
856 | strbuf_release(&sb); | |
857 | } | |
858 | ||
859 | object_array_clear(&merges); | |
860 | return 0; | |
62fdec17 EN |
861 | } |
862 | ||
863 | static int merge_3way(struct merge_options *opt, | |
864 | const char *path, | |
865 | const struct object_id *o, | |
866 | const struct object_id *a, | |
867 | const struct object_id *b, | |
868 | const char *pathnames[3], | |
869 | const int extra_marker_size, | |
870 | mmbuffer_t *result_buf) | |
871 | { | |
f591c472 EN |
872 | mmfile_t orig, src1, src2; |
873 | struct ll_merge_options ll_opts = {0}; | |
874 | char *base, *name1, *name2; | |
875 | int merge_status; | |
876 | ||
877 | ll_opts.renormalize = opt->renormalize; | |
878 | ll_opts.extra_marker_size = extra_marker_size; | |
879 | ll_opts.xdl_opts = opt->xdl_opts; | |
880 | ||
881 | if (opt->priv->call_depth) { | |
882 | ll_opts.virtual_ancestor = 1; | |
883 | ll_opts.variant = 0; | |
884 | } else { | |
885 | switch (opt->recursive_variant) { | |
886 | case MERGE_VARIANT_OURS: | |
887 | ll_opts.variant = XDL_MERGE_FAVOR_OURS; | |
888 | break; | |
889 | case MERGE_VARIANT_THEIRS: | |
890 | ll_opts.variant = XDL_MERGE_FAVOR_THEIRS; | |
891 | break; | |
892 | default: | |
893 | ll_opts.variant = 0; | |
894 | break; | |
895 | } | |
896 | } | |
897 | ||
898 | assert(pathnames[0] && pathnames[1] && pathnames[2] && opt->ancestor); | |
899 | if (pathnames[0] == pathnames[1] && pathnames[1] == pathnames[2]) { | |
900 | base = mkpathdup("%s", opt->ancestor); | |
901 | name1 = mkpathdup("%s", opt->branch1); | |
902 | name2 = mkpathdup("%s", opt->branch2); | |
903 | } else { | |
904 | base = mkpathdup("%s:%s", opt->ancestor, pathnames[0]); | |
905 | name1 = mkpathdup("%s:%s", opt->branch1, pathnames[1]); | |
906 | name2 = mkpathdup("%s:%s", opt->branch2, pathnames[2]); | |
907 | } | |
908 | ||
909 | read_mmblob(&orig, o); | |
910 | read_mmblob(&src1, a); | |
911 | read_mmblob(&src2, b); | |
912 | ||
913 | merge_status = ll_merge(result_buf, path, &orig, base, | |
914 | &src1, name1, &src2, name2, | |
915 | opt->repo->index, &ll_opts); | |
916 | ||
917 | free(base); | |
918 | free(name1); | |
919 | free(name2); | |
920 | free(orig.ptr); | |
921 | free(src1.ptr); | |
922 | free(src2.ptr); | |
923 | return merge_status; | |
62fdec17 EN |
924 | } |
925 | ||
e2e9dc03 EN |
926 | static int handle_content_merge(struct merge_options *opt, |
927 | const char *path, | |
928 | const struct version_info *o, | |
929 | const struct version_info *a, | |
930 | const struct version_info *b, | |
931 | const char *pathnames[3], | |
932 | const int extra_marker_size, | |
933 | struct version_info *result) | |
934 | { | |
991bbdca | 935 | /* |
62fdec17 EN |
936 | * path is the target location where we want to put the file, and |
937 | * is used to determine any normalization rules in ll_merge. | |
938 | * | |
939 | * The normal case is that path and all entries in pathnames are | |
940 | * identical, though renames can affect which path we got one of | |
941 | * the three blobs to merge on various sides of history. | |
942 | * | |
943 | * extra_marker_size is the amount to extend conflict markers in | |
944 | * ll_merge; this is neeed if we have content merges of content | |
945 | * merges, which happens for example with rename/rename(2to1) and | |
946 | * rename/add conflicts. | |
947 | */ | |
948 | unsigned clean = 1; | |
949 | ||
950 | /* | |
951 | * handle_content_merge() needs both files to be of the same type, i.e. | |
952 | * both files OR both submodules OR both symlinks. Conflicting types | |
953 | * needs to be handled elsewhere. | |
991bbdca | 954 | */ |
62fdec17 EN |
955 | assert((S_IFMT & a->mode) == (S_IFMT & b->mode)); |
956 | ||
957 | /* Merge modes */ | |
958 | if (a->mode == b->mode || a->mode == o->mode) | |
959 | result->mode = b->mode; | |
960 | else { | |
961 | /* must be the 100644/100755 case */ | |
962 | assert(S_ISREG(a->mode)); | |
963 | result->mode = a->mode; | |
964 | clean = (b->mode == o->mode); | |
965 | /* | |
966 | * FIXME: If opt->priv->call_depth && !clean, then we really | |
967 | * should not make result->mode match either a->mode or | |
968 | * b->mode; that causes t6036 "check conflicting mode for | |
969 | * regular file" to fail. It would be best to use some other | |
970 | * mode, but we'll confuse all kinds of stuff if we use one | |
971 | * where S_ISREG(result->mode) isn't true, and if we use | |
972 | * something like 0100666, then tree-walk.c's calls to | |
973 | * canon_mode() will just normalize that to 100644 for us and | |
974 | * thus not solve anything. | |
975 | * | |
976 | * Figure out if there's some kind of way we can work around | |
977 | * this... | |
978 | */ | |
979 | } | |
980 | ||
981 | /* | |
982 | * Trivial oid merge. | |
983 | * | |
984 | * Note: While one might assume that the next four lines would | |
985 | * be unnecessary due to the fact that match_mask is often | |
986 | * setup and already handled, renames don't always take care | |
987 | * of that. | |
988 | */ | |
989 | if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid)) | |
990 | oidcpy(&result->oid, &b->oid); | |
991 | else if (oideq(&b->oid, &o->oid)) | |
992 | oidcpy(&result->oid, &a->oid); | |
993 | ||
994 | /* Remaining rules depend on file vs. submodule vs. symlink. */ | |
995 | else if (S_ISREG(a->mode)) { | |
996 | mmbuffer_t result_buf; | |
997 | int ret = 0, merge_status; | |
998 | int two_way; | |
999 | ||
1000 | /* | |
1001 | * If 'o' is different type, treat it as null so we do a | |
1002 | * two-way merge. | |
1003 | */ | |
1004 | two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
1005 | ||
1006 | merge_status = merge_3way(opt, path, | |
1007 | two_way ? &null_oid : &o->oid, | |
1008 | &a->oid, &b->oid, | |
1009 | pathnames, extra_marker_size, | |
1010 | &result_buf); | |
1011 | ||
1012 | if ((merge_status < 0) || !result_buf.ptr) | |
1013 | ret = err(opt, _("Failed to execute internal merge")); | |
1014 | ||
1015 | if (!ret && | |
1016 | write_object_file(result_buf.ptr, result_buf.size, | |
1017 | blob_type, &result->oid)) | |
1018 | ret = err(opt, _("Unable to add %s to database"), | |
1019 | path); | |
1020 | ||
1021 | free(result_buf.ptr); | |
1022 | if (ret) | |
1023 | return -1; | |
1024 | clean &= (merge_status == 0); | |
1025 | path_msg(opt, path, 1, _("Auto-merging %s"), path); | |
1026 | } else if (S_ISGITLINK(a->mode)) { | |
1027 | int two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode)); | |
1028 | clean = merge_submodule(opt, pathnames[0], | |
1029 | two_way ? &null_oid : &o->oid, | |
1030 | &a->oid, &b->oid, &result->oid); | |
1031 | if (opt->priv->call_depth && two_way && !clean) { | |
1032 | result->mode = o->mode; | |
1033 | oidcpy(&result->oid, &o->oid); | |
1034 | } | |
1035 | } else if (S_ISLNK(a->mode)) { | |
1036 | if (opt->priv->call_depth) { | |
1037 | clean = 0; | |
1038 | result->mode = o->mode; | |
1039 | oidcpy(&result->oid, &o->oid); | |
1040 | } else { | |
1041 | switch (opt->recursive_variant) { | |
1042 | case MERGE_VARIANT_NORMAL: | |
1043 | clean = 0; | |
1044 | oidcpy(&result->oid, &a->oid); | |
1045 | break; | |
1046 | case MERGE_VARIANT_OURS: | |
1047 | oidcpy(&result->oid, &a->oid); | |
1048 | break; | |
1049 | case MERGE_VARIANT_THEIRS: | |
1050 | oidcpy(&result->oid, &b->oid); | |
1051 | break; | |
1052 | } | |
1053 | } | |
1054 | } else | |
1055 | BUG("unsupported object type in the tree: %06o for %s", | |
1056 | a->mode, path); | |
1057 | ||
991bbdca | 1058 | return clean; |
e2e9dc03 EN |
1059 | } |
1060 | ||
04af1879 EN |
1061 | /*** Function Grouping: functions related to detect_and_process_renames(), *** |
1062 | *** which are split into directory and regular rename detection sections. ***/ | |
1063 | ||
1064 | /*** Function Grouping: functions related to directory rename detection ***/ | |
1065 | ||
1066 | /*** Function Grouping: functions related to regular rename detection ***/ | |
1067 | ||
e1a124e8 EN |
1068 | static int process_renames(struct merge_options *opt, |
1069 | struct diff_queue_struct *renames) | |
1070 | { | |
c2d267df EN |
1071 | int clean_merge = 1, i; |
1072 | ||
1073 | for (i = 0; i < renames->nr; ++i) { | |
1074 | const char *oldpath = NULL, *newpath; | |
1075 | struct diff_filepair *pair = renames->queue[i]; | |
1076 | struct conflict_info *oldinfo = NULL, *newinfo = NULL; | |
1077 | struct strmap_entry *old_ent, *new_ent; | |
1078 | unsigned int old_sidemask; | |
1079 | int target_index, other_source_index; | |
1080 | int source_deleted, collision, type_changed; | |
2e91ddd2 | 1081 | const char *rename_branch = NULL, *delete_branch = NULL; |
c2d267df EN |
1082 | |
1083 | old_ent = strmap_get_entry(&opt->priv->paths, pair->one->path); | |
1084 | oldpath = old_ent->key; | |
1085 | oldinfo = old_ent->value; | |
1086 | ||
1087 | new_ent = strmap_get_entry(&opt->priv->paths, pair->two->path); | |
1088 | newpath = new_ent->key; | |
1089 | newinfo = new_ent->value; | |
1090 | ||
1091 | /* | |
1092 | * diff_filepairs have copies of pathnames, thus we have to | |
1093 | * use standard 'strcmp()' (negated) instead of '=='. | |
1094 | */ | |
1095 | if (i + 1 < renames->nr && | |
1096 | !strcmp(oldpath, renames->queue[i+1]->one->path)) { | |
1097 | /* Handle rename/rename(1to2) or rename/rename(1to1) */ | |
1098 | const char *pathnames[3]; | |
af1e56c4 EN |
1099 | struct version_info merged; |
1100 | struct conflict_info *base, *side1, *side2; | |
53e88a03 | 1101 | unsigned was_binary_blob = 0; |
c2d267df EN |
1102 | |
1103 | pathnames[0] = oldpath; | |
1104 | pathnames[1] = newpath; | |
1105 | pathnames[2] = renames->queue[i+1]->two->path; | |
1106 | ||
af1e56c4 EN |
1107 | base = strmap_get(&opt->priv->paths, pathnames[0]); |
1108 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
1109 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
1110 | ||
1111 | VERIFY_CI(base); | |
1112 | VERIFY_CI(side1); | |
1113 | VERIFY_CI(side2); | |
1114 | ||
c2d267df | 1115 | if (!strcmp(pathnames[1], pathnames[2])) { |
af1e56c4 EN |
1116 | /* Both sides renamed the same way */ |
1117 | assert(side1 == side2); | |
1118 | memcpy(&side1->stages[0], &base->stages[0], | |
1119 | sizeof(merged)); | |
1120 | side1->filemask |= (1 << MERGE_BASE); | |
1121 | /* Mark base as resolved by removal */ | |
1122 | base->merged.is_null = 1; | |
1123 | base->merged.clean = 1; | |
c2d267df EN |
1124 | |
1125 | /* We handled both renames, i.e. i+1 handled */ | |
1126 | i++; | |
1127 | /* Move to next rename */ | |
1128 | continue; | |
1129 | } | |
1130 | ||
1131 | /* This is a rename/rename(1to2) */ | |
53e88a03 EN |
1132 | clean_merge = handle_content_merge(opt, |
1133 | pair->one->path, | |
1134 | &base->stages[0], | |
1135 | &side1->stages[1], | |
1136 | &side2->stages[2], | |
1137 | pathnames, | |
1138 | 1 + 2 * opt->priv->call_depth, | |
1139 | &merged); | |
1140 | if (!clean_merge && | |
1141 | merged.mode == side1->stages[1].mode && | |
1142 | oideq(&merged.oid, &side1->stages[1].oid)) | |
1143 | was_binary_blob = 1; | |
1144 | memcpy(&side1->stages[1], &merged, sizeof(merged)); | |
1145 | if (was_binary_blob) { | |
1146 | /* | |
1147 | * Getting here means we were attempting to | |
1148 | * merge a binary blob. | |
1149 | * | |
1150 | * Since we can't merge binaries, | |
1151 | * handle_content_merge() just takes one | |
1152 | * side. But we don't want to copy the | |
1153 | * contents of one side to both paths. We | |
1154 | * used the contents of side1 above for | |
1155 | * side1->stages, let's use the contents of | |
1156 | * side2 for side2->stages below. | |
1157 | */ | |
1158 | oidcpy(&merged.oid, &side2->stages[2].oid); | |
1159 | merged.mode = side2->stages[2].mode; | |
1160 | } | |
1161 | memcpy(&side2->stages[2], &merged, sizeof(merged)); | |
1162 | ||
1163 | side1->path_conflict = 1; | |
1164 | side2->path_conflict = 1; | |
1165 | /* | |
1166 | * TODO: For renames we normally remove the path at the | |
1167 | * old name. It would thus seem consistent to do the | |
1168 | * same for rename/rename(1to2) cases, but we haven't | |
1169 | * done so traditionally and a number of the regression | |
1170 | * tests now encode an expectation that the file is | |
1171 | * left there at stage 1. If we ever decide to change | |
1172 | * this, add the following two lines here: | |
1173 | * base->merged.is_null = 1; | |
1174 | * base->merged.clean = 1; | |
1175 | * and remove the setting of base->path_conflict to 1. | |
1176 | */ | |
1177 | base->path_conflict = 1; | |
1178 | path_msg(opt, oldpath, 0, | |
1179 | _("CONFLICT (rename/rename): %s renamed to " | |
1180 | "%s in %s and to %s in %s."), | |
1181 | pathnames[0], | |
1182 | pathnames[1], opt->branch1, | |
1183 | pathnames[2], opt->branch2); | |
c2d267df EN |
1184 | |
1185 | i++; /* We handled both renames, i.e. i+1 handled */ | |
1186 | continue; | |
1187 | } | |
1188 | ||
1189 | VERIFY_CI(oldinfo); | |
1190 | VERIFY_CI(newinfo); | |
1191 | target_index = pair->score; /* from collect_renames() */ | |
1192 | assert(target_index == 1 || target_index == 2); | |
1193 | other_source_index = 3 - target_index; | |
1194 | old_sidemask = (1 << other_source_index); /* 2 or 4 */ | |
1195 | source_deleted = (oldinfo->filemask == 1); | |
1196 | collision = ((newinfo->filemask & old_sidemask) != 0); | |
1197 | type_changed = !source_deleted && | |
1198 | (S_ISREG(oldinfo->stages[other_source_index].mode) != | |
1199 | S_ISREG(newinfo->stages[target_index].mode)); | |
1200 | if (type_changed && collision) { | |
6fcccbd7 EN |
1201 | /* |
1202 | * special handling so later blocks can handle this... | |
1203 | * | |
1204 | * if type_changed && collision are both true, then this | |
1205 | * was really a double rename, but one side wasn't | |
1206 | * detected due to lack of break detection. I.e. | |
1207 | * something like | |
1208 | * orig: has normal file 'foo' | |
1209 | * side1: renames 'foo' to 'bar', adds 'foo' symlink | |
1210 | * side2: renames 'foo' to 'bar' | |
1211 | * In this case, the foo->bar rename on side1 won't be | |
1212 | * detected because the new symlink named 'foo' is | |
1213 | * there and we don't do break detection. But we detect | |
1214 | * this here because we don't want to merge the content | |
1215 | * of the foo symlink with the foo->bar file, so we | |
1216 | * have some logic to handle this special case. The | |
1217 | * easiest way to do that is make 'bar' on side1 not | |
1218 | * be considered a colliding file but the other part | |
1219 | * of a normal rename. If the file is very different, | |
1220 | * well we're going to get content merge conflicts | |
1221 | * anyway so it doesn't hurt. And if the colliding | |
1222 | * file also has a different type, that'll be handled | |
1223 | * by the content merge logic in process_entry() too. | |
1224 | * | |
1225 | * See also t6430, 'rename vs. rename/symlink' | |
1226 | */ | |
1227 | collision = 0; | |
c2d267df | 1228 | } |
2e91ddd2 EN |
1229 | if (source_deleted) { |
1230 | if (target_index == 1) { | |
1231 | rename_branch = opt->branch1; | |
1232 | delete_branch = opt->branch2; | |
1233 | } else { | |
1234 | rename_branch = opt->branch2; | |
1235 | delete_branch = opt->branch1; | |
1236 | } | |
1237 | } | |
c2d267df EN |
1238 | |
1239 | assert(source_deleted || oldinfo->filemask & old_sidemask); | |
1240 | ||
1241 | /* Need to check for special types of rename conflicts... */ | |
1242 | if (collision && !source_deleted) { | |
1243 | /* collision: rename/add or rename/rename(2to1) */ | |
35e47e35 EN |
1244 | const char *pathnames[3]; |
1245 | struct version_info merged; | |
1246 | ||
1247 | struct conflict_info *base, *side1, *side2; | |
1248 | unsigned clean; | |
1249 | ||
1250 | pathnames[0] = oldpath; | |
1251 | pathnames[other_source_index] = oldpath; | |
1252 | pathnames[target_index] = newpath; | |
1253 | ||
1254 | base = strmap_get(&opt->priv->paths, pathnames[0]); | |
1255 | side1 = strmap_get(&opt->priv->paths, pathnames[1]); | |
1256 | side2 = strmap_get(&opt->priv->paths, pathnames[2]); | |
1257 | ||
1258 | VERIFY_CI(base); | |
1259 | VERIFY_CI(side1); | |
1260 | VERIFY_CI(side2); | |
1261 | ||
1262 | clean = handle_content_merge(opt, pair->one->path, | |
1263 | &base->stages[0], | |
1264 | &side1->stages[1], | |
1265 | &side2->stages[2], | |
1266 | pathnames, | |
1267 | 1 + 2 * opt->priv->call_depth, | |
1268 | &merged); | |
1269 | ||
1270 | memcpy(&newinfo->stages[target_index], &merged, | |
1271 | sizeof(merged)); | |
1272 | if (!clean) { | |
1273 | path_msg(opt, newpath, 0, | |
1274 | _("CONFLICT (rename involved in " | |
1275 | "collision): rename of %s -> %s has " | |
1276 | "content conflicts AND collides " | |
1277 | "with another path; this may result " | |
1278 | "in nested conflict markers."), | |
1279 | oldpath, newpath); | |
1280 | } | |
c2d267df | 1281 | } else if (collision && source_deleted) { |
35e47e35 EN |
1282 | /* |
1283 | * rename/add/delete or rename/rename(2to1)/delete: | |
1284 | * since oldpath was deleted on the side that didn't | |
1285 | * do the rename, there's not much of a content merge | |
1286 | * we can do for the rename. oldinfo->merged.is_null | |
1287 | * was already set, so we just leave things as-is so | |
1288 | * they look like an add/add conflict. | |
1289 | */ | |
1290 | ||
1291 | newinfo->path_conflict = 1; | |
1292 | path_msg(opt, newpath, 0, | |
1293 | _("CONFLICT (rename/delete): %s renamed " | |
1294 | "to %s in %s, but deleted in %s."), | |
1295 | oldpath, newpath, rename_branch, delete_branch); | |
c2d267df | 1296 | } else { |
2e91ddd2 EN |
1297 | /* |
1298 | * a few different cases...start by copying the | |
1299 | * existing stage(s) from oldinfo over the newinfo | |
1300 | * and update the pathname(s). | |
1301 | */ | |
1302 | memcpy(&newinfo->stages[0], &oldinfo->stages[0], | |
1303 | sizeof(newinfo->stages[0])); | |
1304 | newinfo->filemask |= (1 << MERGE_BASE); | |
1305 | newinfo->pathnames[0] = oldpath; | |
c2d267df EN |
1306 | if (type_changed) { |
1307 | /* rename vs. typechange */ | |
6fcccbd7 EN |
1308 | /* Mark the original as resolved by removal */ |
1309 | memcpy(&oldinfo->stages[0].oid, &null_oid, | |
1310 | sizeof(oldinfo->stages[0].oid)); | |
1311 | oldinfo->stages[0].mode = 0; | |
1312 | oldinfo->filemask &= 0x06; | |
c2d267df EN |
1313 | } else if (source_deleted) { |
1314 | /* rename/delete */ | |
2e91ddd2 EN |
1315 | newinfo->path_conflict = 1; |
1316 | path_msg(opt, newpath, 0, | |
1317 | _("CONFLICT (rename/delete): %s renamed" | |
1318 | " to %s in %s, but deleted in %s."), | |
1319 | oldpath, newpath, | |
1320 | rename_branch, delete_branch); | |
c2d267df EN |
1321 | } else { |
1322 | /* normal rename */ | |
f1665e69 EN |
1323 | memcpy(&newinfo->stages[other_source_index], |
1324 | &oldinfo->stages[other_source_index], | |
1325 | sizeof(newinfo->stages[0])); | |
1326 | newinfo->filemask |= (1 << other_source_index); | |
1327 | newinfo->pathnames[other_source_index] = oldpath; | |
c2d267df EN |
1328 | } |
1329 | } | |
1330 | ||
1331 | if (!type_changed) { | |
1332 | /* Mark the original as resolved by removal */ | |
1333 | oldinfo->merged.is_null = 1; | |
1334 | oldinfo->merged.clean = 1; | |
1335 | } | |
1336 | ||
1337 | } | |
1338 | ||
1339 | return clean_merge; | |
e1a124e8 EN |
1340 | } |
1341 | ||
1342 | static int compare_pairs(const void *a_, const void *b_) | |
1343 | { | |
965a7bc2 EN |
1344 | const struct diff_filepair *a = *((const struct diff_filepair **)a_); |
1345 | const struct diff_filepair *b = *((const struct diff_filepair **)b_); | |
1346 | ||
1347 | return strcmp(a->one->path, b->one->path); | |
e1a124e8 EN |
1348 | } |
1349 | ||
1350 | /* Call diffcore_rename() to compute which files have changed on given side */ | |
1351 | static void detect_regular_renames(struct merge_options *opt, | |
1352 | struct tree *merge_base, | |
1353 | struct tree *side, | |
1354 | unsigned side_index) | |
1355 | { | |
f39d05ca EN |
1356 | struct diff_options diff_opts; |
1357 | struct rename_info *renames = &opt->priv->renames; | |
1358 | ||
1359 | repo_diff_setup(opt->repo, &diff_opts); | |
1360 | diff_opts.flags.recursive = 1; | |
1361 | diff_opts.flags.rename_empty = 0; | |
1362 | diff_opts.detect_rename = DIFF_DETECT_RENAME; | |
1363 | diff_opts.rename_limit = opt->rename_limit; | |
1364 | if (opt->rename_limit <= 0) | |
1365 | diff_opts.rename_limit = 1000; | |
1366 | diff_opts.rename_score = opt->rename_score; | |
1367 | diff_opts.show_rename_progress = opt->show_rename_progress; | |
1368 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
1369 | diff_setup_done(&diff_opts); | |
1370 | diff_tree_oid(&merge_base->object.oid, &side->object.oid, "", | |
1371 | &diff_opts); | |
1372 | diffcore_std(&diff_opts); | |
1373 | ||
1374 | if (diff_opts.needed_rename_limit > renames->needed_limit) | |
1375 | renames->needed_limit = diff_opts.needed_rename_limit; | |
1376 | ||
1377 | renames->pairs[side_index] = diff_queued_diff; | |
1378 | ||
1379 | diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
1380 | diff_queued_diff.nr = 0; | |
1381 | diff_queued_diff.queue = NULL; | |
1382 | diff_flush(&diff_opts); | |
e1a124e8 EN |
1383 | } |
1384 | ||
1385 | /* | |
1386 | * Get information of all renames which occurred in 'side_pairs', discarding | |
1387 | * non-renames. | |
1388 | */ | |
1389 | static int collect_renames(struct merge_options *opt, | |
1390 | struct diff_queue_struct *result, | |
1391 | unsigned side_index) | |
1392 | { | |
965a7bc2 EN |
1393 | int i, clean = 1; |
1394 | struct diff_queue_struct *side_pairs; | |
1395 | struct rename_info *renames = &opt->priv->renames; | |
1396 | ||
1397 | side_pairs = &renames->pairs[side_index]; | |
1398 | ||
1399 | for (i = 0; i < side_pairs->nr; ++i) { | |
1400 | struct diff_filepair *p = side_pairs->queue[i]; | |
1401 | ||
1402 | if (p->status != 'R') { | |
1403 | diff_free_filepair(p); | |
1404 | continue; | |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * p->score comes back from diffcore_rename_extended() with | |
1409 | * the similarity of the renamed file. The similarity is | |
1410 | * was used to determine that the two files were related | |
1411 | * and are a rename, which we have already used, but beyond | |
1412 | * that we have no use for the similarity. So p->score is | |
1413 | * now irrelevant. However, process_renames() will need to | |
1414 | * know which side of the merge this rename was associated | |
1415 | * with, so overwrite p->score with that value. | |
1416 | */ | |
1417 | p->score = side_index; | |
1418 | result->queue[result->nr++] = p; | |
1419 | } | |
1420 | ||
1421 | return clean; | |
e1a124e8 EN |
1422 | } |
1423 | ||
231e2dd4 EN |
1424 | static int detect_and_process_renames(struct merge_options *opt, |
1425 | struct tree *merge_base, | |
1426 | struct tree *side1, | |
1427 | struct tree *side2) | |
1428 | { | |
e1a124e8 EN |
1429 | struct diff_queue_struct combined; |
1430 | struct rename_info *renames = &opt->priv->renames; | |
1431 | int s, clean = 1; | |
1432 | ||
1433 | memset(&combined, 0, sizeof(combined)); | |
1434 | ||
1435 | detect_regular_renames(opt, merge_base, side1, MERGE_SIDE1); | |
1436 | detect_regular_renames(opt, merge_base, side2, MERGE_SIDE2); | |
1437 | ||
1438 | ALLOC_GROW(combined.queue, | |
1439 | renames->pairs[1].nr + renames->pairs[2].nr, | |
1440 | combined.alloc); | |
1441 | clean &= collect_renames(opt, &combined, MERGE_SIDE1); | |
1442 | clean &= collect_renames(opt, &combined, MERGE_SIDE2); | |
1443 | QSORT(combined.queue, combined.nr, compare_pairs); | |
1444 | ||
1445 | clean &= process_renames(opt, &combined); | |
1446 | ||
1447 | /* Free memory for renames->pairs[] and combined */ | |
1448 | for (s = MERGE_SIDE1; s <= MERGE_SIDE2; s++) { | |
1449 | free(renames->pairs[s].queue); | |
1450 | DIFF_QUEUE_CLEAR(&renames->pairs[s]); | |
1451 | } | |
1452 | if (combined.nr) { | |
1453 | int i; | |
1454 | for (i = 0; i < combined.nr; i++) | |
1455 | diff_free_filepair(combined.queue[i]); | |
1456 | free(combined.queue); | |
1457 | } | |
231e2dd4 | 1458 | |
231e2dd4 EN |
1459 | return clean; |
1460 | } | |
1461 | ||
04af1879 EN |
1462 | /*** Function Grouping: functions related to process_entries() ***/ |
1463 | ||
8adffaa8 EN |
1464 | static int string_list_df_name_compare(const char *one, const char *two) |
1465 | { | |
1466 | int onelen = strlen(one); | |
1467 | int twolen = strlen(two); | |
1468 | /* | |
1469 | * Here we only care that entries for D/F conflicts are | |
1470 | * adjacent, in particular with the file of the D/F conflict | |
1471 | * appearing before files below the corresponding directory. | |
1472 | * The order of the rest of the list is irrelevant for us. | |
1473 | * | |
1474 | * To achieve this, we sort with df_name_compare and provide | |
1475 | * the mode S_IFDIR so that D/F conflicts will sort correctly. | |
1476 | * We use the mode S_IFDIR for everything else for simplicity, | |
1477 | * since in other cases any changes in their order due to | |
1478 | * sorting cause no problems for us. | |
1479 | */ | |
1480 | int cmp = df_name_compare(one, onelen, S_IFDIR, | |
1481 | two, twolen, S_IFDIR); | |
1482 | /* | |
1483 | * Now that 'foo' and 'foo/bar' compare equal, we have to make sure | |
1484 | * that 'foo' comes before 'foo/bar'. | |
1485 | */ | |
1486 | if (cmp) | |
1487 | return cmp; | |
1488 | return onelen - twolen; | |
1489 | } | |
1490 | ||
a9945bba | 1491 | struct directory_versions { |
bb470f4e EN |
1492 | /* |
1493 | * versions: list of (basename -> version_info) | |
1494 | * | |
1495 | * The basenames are in reverse lexicographic order of full pathnames, | |
1496 | * as processed in process_entries(). This puts all entries within | |
1497 | * a directory together, and covers the directory itself after | |
1498 | * everything within it, allowing us to write subtrees before needing | |
1499 | * to record information for the tree itself. | |
1500 | */ | |
a9945bba | 1501 | struct string_list versions; |
bb470f4e EN |
1502 | |
1503 | /* | |
1504 | * offsets: list of (full relative path directories -> integer offsets) | |
1505 | * | |
1506 | * Since versions contains basenames from files in multiple different | |
1507 | * directories, we need to know which entries in versions correspond | |
1508 | * to which directories. Values of e.g. | |
1509 | * "" 0 | |
1510 | * src 2 | |
1511 | * src/moduleA 5 | |
1512 | * Would mean that entries 0-1 of versions are files in the toplevel | |
1513 | * directory, entries 2-4 are files under src/, and the remaining | |
1514 | * entries starting at index 5 are files under src/moduleA/. | |
1515 | */ | |
1516 | struct string_list offsets; | |
1517 | ||
1518 | /* | |
1519 | * last_directory: directory that previously processed file found in | |
1520 | * | |
1521 | * last_directory starts NULL, but records the directory in which the | |
1522 | * previous file was found within. As soon as | |
1523 | * directory(current_file) != last_directory | |
1524 | * then we need to start updating accounting in versions & offsets. | |
1525 | * Note that last_directory is always the last path in "offsets" (or | |
1526 | * NULL if "offsets" is empty) so this exists just for quick access. | |
1527 | */ | |
1528 | const char *last_directory; | |
1529 | ||
1530 | /* last_directory_len: cached computation of strlen(last_directory) */ | |
1531 | unsigned last_directory_len; | |
a9945bba EN |
1532 | }; |
1533 | ||
ee4012dc EN |
1534 | static int tree_entry_order(const void *a_, const void *b_) |
1535 | { | |
1536 | const struct string_list_item *a = a_; | |
1537 | const struct string_list_item *b = b_; | |
1538 | ||
1539 | const struct merged_info *ami = a->util; | |
1540 | const struct merged_info *bmi = b->util; | |
1541 | return base_name_compare(a->string, strlen(a->string), ami->result.mode, | |
1542 | b->string, strlen(b->string), bmi->result.mode); | |
1543 | } | |
1544 | ||
1545 | static void write_tree(struct object_id *result_oid, | |
1546 | struct string_list *versions, | |
1547 | unsigned int offset, | |
1548 | size_t hash_size) | |
1549 | { | |
1550 | size_t maxlen = 0, extra; | |
1551 | unsigned int nr = versions->nr - offset; | |
1552 | struct strbuf buf = STRBUF_INIT; | |
1553 | struct string_list relevant_entries = STRING_LIST_INIT_NODUP; | |
1554 | int i; | |
1555 | ||
1556 | /* | |
1557 | * We want to sort the last (versions->nr-offset) entries in versions. | |
1558 | * Do so by abusing the string_list API a bit: make another string_list | |
1559 | * that contains just those entries and then sort them. | |
1560 | * | |
1561 | * We won't use relevant_entries again and will let it just pop off the | |
1562 | * stack, so there won't be allocation worries or anything. | |
1563 | */ | |
1564 | relevant_entries.items = versions->items + offset; | |
1565 | relevant_entries.nr = versions->nr - offset; | |
1566 | QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order); | |
1567 | ||
1568 | /* Pre-allocate some space in buf */ | |
1569 | extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */ | |
1570 | for (i = 0; i < nr; i++) { | |
1571 | maxlen += strlen(versions->items[offset+i].string) + extra; | |
1572 | } | |
1573 | strbuf_grow(&buf, maxlen); | |
1574 | ||
1575 | /* Write each entry out to buf */ | |
1576 | for (i = 0; i < nr; i++) { | |
1577 | struct merged_info *mi = versions->items[offset+i].util; | |
1578 | struct version_info *ri = &mi->result; | |
1579 | strbuf_addf(&buf, "%o %s%c", | |
1580 | ri->mode, | |
1581 | versions->items[offset+i].string, '\0'); | |
1582 | strbuf_add(&buf, ri->oid.hash, hash_size); | |
1583 | } | |
1584 | ||
1585 | /* Write this object file out, and record in result_oid */ | |
1586 | write_object_file(buf.buf, buf.len, tree_type, result_oid); | |
1587 | strbuf_release(&buf); | |
1588 | } | |
1589 | ||
a9945bba EN |
1590 | static void record_entry_for_tree(struct directory_versions *dir_metadata, |
1591 | const char *path, | |
1592 | struct merged_info *mi) | |
1593 | { | |
1594 | const char *basename; | |
1595 | ||
1596 | if (mi->is_null) | |
1597 | /* nothing to record */ | |
1598 | return; | |
1599 | ||
1600 | basename = path + mi->basename_offset; | |
1601 | assert(strchr(basename, '/') == NULL); | |
1602 | string_list_append(&dir_metadata->versions, | |
1603 | basename)->util = &mi->result; | |
1604 | } | |
1605 | ||
bb470f4e EN |
1606 | static void write_completed_directory(struct merge_options *opt, |
1607 | const char *new_directory_name, | |
1608 | struct directory_versions *info) | |
1609 | { | |
1610 | const char *prev_dir; | |
1611 | struct merged_info *dir_info = NULL; | |
1612 | unsigned int offset; | |
1613 | ||
1614 | /* | |
1615 | * Some explanation of info->versions and info->offsets... | |
1616 | * | |
1617 | * process_entries() iterates over all relevant files AND | |
1618 | * directories in reverse lexicographic order, and calls this | |
1619 | * function. Thus, an example of the paths that process_entries() | |
1620 | * could operate on (along with the directories for those paths | |
1621 | * being shown) is: | |
1622 | * | |
1623 | * xtract.c "" | |
1624 | * tokens.txt "" | |
1625 | * src/moduleB/umm.c src/moduleB | |
1626 | * src/moduleB/stuff.h src/moduleB | |
1627 | * src/moduleB/baz.c src/moduleB | |
1628 | * src/moduleB src | |
1629 | * src/moduleA/foo.c src/moduleA | |
1630 | * src/moduleA/bar.c src/moduleA | |
1631 | * src/moduleA src | |
1632 | * src "" | |
1633 | * Makefile "" | |
1634 | * | |
1635 | * info->versions: | |
1636 | * | |
1637 | * always contains the unprocessed entries and their | |
1638 | * version_info information. For example, after the first five | |
1639 | * entries above, info->versions would be: | |
1640 | * | |
1641 | * xtract.c <xtract.c's version_info> | |
1642 | * token.txt <token.txt's version_info> | |
1643 | * umm.c <src/moduleB/umm.c's version_info> | |
1644 | * stuff.h <src/moduleB/stuff.h's version_info> | |
1645 | * baz.c <src/moduleB/baz.c's version_info> | |
1646 | * | |
1647 | * Once a subdirectory is completed we remove the entries in | |
1648 | * that subdirectory from info->versions, writing it as a tree | |
1649 | * (write_tree()). Thus, as soon as we get to src/moduleB, | |
1650 | * info->versions would be updated to | |
1651 | * | |
1652 | * xtract.c <xtract.c's version_info> | |
1653 | * token.txt <token.txt's version_info> | |
1654 | * moduleB <src/moduleB's version_info> | |
1655 | * | |
1656 | * info->offsets: | |
1657 | * | |
1658 | * helps us track which entries in info->versions correspond to | |
1659 | * which directories. When we are N directories deep (e.g. 4 | |
1660 | * for src/modA/submod/subdir/), we have up to N+1 unprocessed | |
1661 | * directories (+1 because of toplevel dir). Corresponding to | |
1662 | * the info->versions example above, after processing five entries | |
1663 | * info->offsets will be: | |
1664 | * | |
1665 | * "" 0 | |
1666 | * src/moduleB 2 | |
1667 | * | |
1668 | * which is used to know that xtract.c & token.txt are from the | |
1669 | * toplevel dirctory, while umm.c & stuff.h & baz.c are from the | |
1670 | * src/moduleB directory. Again, following the example above, | |
1671 | * once we need to process src/moduleB, then info->offsets is | |
1672 | * updated to | |
1673 | * | |
1674 | * "" 0 | |
1675 | * src 2 | |
1676 | * | |
1677 | * which says that moduleB (and only moduleB so far) is in the | |
1678 | * src directory. | |
1679 | * | |
1680 | * One unique thing to note about info->offsets here is that | |
1681 | * "src" was not added to info->offsets until there was a path | |
1682 | * (a file OR directory) immediately below src/ that got | |
1683 | * processed. | |
1684 | * | |
1685 | * Since process_entry() just appends new entries to info->versions, | |
1686 | * write_completed_directory() only needs to do work if the next path | |
1687 | * is in a directory that is different than the last directory found | |
1688 | * in info->offsets. | |
1689 | */ | |
1690 | ||
1691 | /* | |
1692 | * If we are working with the same directory as the last entry, there | |
1693 | * is no work to do. (See comments above the directory_name member of | |
1694 | * struct merged_info for why we can use pointer comparison instead of | |
1695 | * strcmp here.) | |
1696 | */ | |
1697 | if (new_directory_name == info->last_directory) | |
1698 | return; | |
1699 | ||
1700 | /* | |
1701 | * If we are just starting (last_directory is NULL), or last_directory | |
1702 | * is a prefix of the current directory, then we can just update | |
1703 | * info->offsets to record the offset where we started this directory | |
1704 | * and update last_directory to have quick access to it. | |
1705 | */ | |
1706 | if (info->last_directory == NULL || | |
1707 | !strncmp(new_directory_name, info->last_directory, | |
1708 | info->last_directory_len)) { | |
1709 | uintptr_t offset = info->versions.nr; | |
1710 | ||
1711 | info->last_directory = new_directory_name; | |
1712 | info->last_directory_len = strlen(info->last_directory); | |
1713 | /* | |
1714 | * Record the offset into info->versions where we will | |
1715 | * start recording basenames of paths found within | |
1716 | * new_directory_name. | |
1717 | */ | |
1718 | string_list_append(&info->offsets, | |
1719 | info->last_directory)->util = (void*)offset; | |
1720 | return; | |
1721 | } | |
1722 | ||
1723 | /* | |
1724 | * The next entry that will be processed will be within | |
1725 | * new_directory_name. Since at this point we know that | |
1726 | * new_directory_name is within a different directory than | |
1727 | * info->last_directory, we have all entries for info->last_directory | |
1728 | * in info->versions and we need to create a tree object for them. | |
1729 | */ | |
1730 | dir_info = strmap_get(&opt->priv->paths, info->last_directory); | |
1731 | assert(dir_info); | |
1732 | offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util; | |
1733 | if (offset == info->versions.nr) { | |
1734 | /* | |
1735 | * Actually, we don't need to create a tree object in this | |
1736 | * case. Whenever all files within a directory disappear | |
1737 | * during the merge (e.g. unmodified on one side and | |
1738 | * deleted on the other, or files were renamed elsewhere), | |
1739 | * then we get here and the directory itself needs to be | |
1740 | * omitted from its parent tree as well. | |
1741 | */ | |
1742 | dir_info->is_null = 1; | |
1743 | } else { | |
1744 | /* | |
1745 | * Write out the tree to the git object directory, and also | |
1746 | * record the mode and oid in dir_info->result. | |
1747 | */ | |
1748 | dir_info->is_null = 0; | |
1749 | dir_info->result.mode = S_IFDIR; | |
1750 | write_tree(&dir_info->result.oid, &info->versions, offset, | |
1751 | opt->repo->hash_algo->rawsz); | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * We've now used several entries from info->versions and one entry | |
1756 | * from info->offsets, so we get rid of those values. | |
1757 | */ | |
1758 | info->offsets.nr--; | |
1759 | info->versions.nr = offset; | |
1760 | ||
1761 | /* | |
1762 | * Now we've taken care of the completed directory, but we need to | |
1763 | * prepare things since future entries will be in | |
1764 | * new_directory_name. (In particular, process_entry() will be | |
1765 | * appending new entries to info->versions.) So, we need to make | |
1766 | * sure new_directory_name is the last entry in info->offsets. | |
1767 | */ | |
1768 | prev_dir = info->offsets.nr == 0 ? NULL : | |
1769 | info->offsets.items[info->offsets.nr-1].string; | |
1770 | if (new_directory_name != prev_dir) { | |
1771 | uintptr_t c = info->versions.nr; | |
1772 | string_list_append(&info->offsets, | |
1773 | new_directory_name)->util = (void*)c; | |
1774 | } | |
1775 | ||
1776 | /* And, of course, we need to update last_directory to match. */ | |
1777 | info->last_directory = new_directory_name; | |
1778 | info->last_directory_len = strlen(info->last_directory); | |
1779 | } | |
1780 | ||
6a02dd90 EN |
1781 | /* Per entry merge function */ |
1782 | static void process_entry(struct merge_options *opt, | |
1783 | const char *path, | |
a9945bba EN |
1784 | struct conflict_info *ci, |
1785 | struct directory_versions *dir_metadata) | |
6a02dd90 | 1786 | { |
23366d2a EN |
1787 | int df_file_index = 0; |
1788 | ||
6a02dd90 EN |
1789 | VERIFY_CI(ci); |
1790 | assert(ci->filemask >= 0 && ci->filemask <= 7); | |
1791 | /* ci->match_mask == 7 was handled in collect_merge_info_callback() */ | |
1792 | assert(ci->match_mask == 0 || ci->match_mask == 3 || | |
1793 | ci->match_mask == 5 || ci->match_mask == 6); | |
1794 | ||
a9945bba EN |
1795 | if (ci->dirmask) { |
1796 | record_entry_for_tree(dir_metadata, path, &ci->merged); | |
1797 | if (ci->filemask == 0) | |
1798 | /* nothing else to handle */ | |
1799 | return; | |
1800 | assert(ci->df_conflict); | |
1801 | } | |
1802 | ||
0ccfa4e5 EN |
1803 | if (ci->df_conflict && ci->merged.result.mode == 0) { |
1804 | int i; | |
1805 | ||
1806 | /* | |
1807 | * directory no longer in the way, but we do have a file we | |
1808 | * need to place here so we need to clean away the "directory | |
1809 | * merges to nothing" result. | |
1810 | */ | |
1811 | ci->df_conflict = 0; | |
1812 | assert(ci->filemask != 0); | |
1813 | ci->merged.clean = 0; | |
1814 | ci->merged.is_null = 0; | |
1815 | /* and we want to zero out any directory-related entries */ | |
1816 | ci->match_mask = (ci->match_mask & ~ci->dirmask); | |
1817 | ci->dirmask = 0; | |
1818 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
1819 | if (ci->filemask & (1 << i)) | |
1820 | continue; | |
1821 | ci->stages[i].mode = 0; | |
1822 | oidcpy(&ci->stages[i].oid, &null_oid); | |
1823 | } | |
1824 | } else if (ci->df_conflict && ci->merged.result.mode != 0) { | |
23366d2a EN |
1825 | /* |
1826 | * This started out as a D/F conflict, and the entries in | |
1827 | * the competing directory were not removed by the merge as | |
1828 | * evidenced by write_completed_directory() writing a value | |
1829 | * to ci->merged.result.mode. | |
1830 | */ | |
1831 | struct conflict_info *new_ci; | |
1832 | const char *branch; | |
1833 | const char *old_path = path; | |
1834 | int i; | |
1835 | ||
1836 | assert(ci->merged.result.mode == S_IFDIR); | |
1837 | ||
1838 | /* | |
1839 | * If filemask is 1, we can just ignore the file as having | |
1840 | * been deleted on both sides. We do not want to overwrite | |
1841 | * ci->merged.result, since it stores the tree for all the | |
1842 | * files under it. | |
1843 | */ | |
1844 | if (ci->filemask == 1) { | |
1845 | ci->filemask = 0; | |
1846 | return; | |
1847 | } | |
1848 | ||
1849 | /* | |
1850 | * This file still exists on at least one side, and we want | |
1851 | * the directory to remain here, so we need to move this | |
1852 | * path to some new location. | |
1853 | */ | |
1854 | new_ci = xcalloc(1, sizeof(*new_ci)); | |
1855 | /* We don't really want new_ci->merged.result copied, but it'll | |
1856 | * be overwritten below so it doesn't matter. We also don't | |
1857 | * want any directory mode/oid values copied, but we'll zero | |
1858 | * those out immediately. We do want the rest of ci copied. | |
1859 | */ | |
1860 | memcpy(new_ci, ci, sizeof(*ci)); | |
1861 | new_ci->match_mask = (new_ci->match_mask & ~new_ci->dirmask); | |
1862 | new_ci->dirmask = 0; | |
1863 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
1864 | if (new_ci->filemask & (1 << i)) | |
1865 | continue; | |
1866 | /* zero out any entries related to directories */ | |
1867 | new_ci->stages[i].mode = 0; | |
1868 | oidcpy(&new_ci->stages[i].oid, &null_oid); | |
1869 | } | |
1870 | ||
1871 | /* | |
1872 | * Find out which side this file came from; note that we | |
1873 | * cannot just use ci->filemask, because renames could cause | |
1874 | * the filemask to go back to 7. So we use dirmask, then | |
1875 | * pick the opposite side's index. | |
1876 | */ | |
1877 | df_file_index = (ci->dirmask & (1 << 1)) ? 2 : 1; | |
1878 | branch = (df_file_index == 1) ? opt->branch1 : opt->branch2; | |
1879 | path = unique_path(&opt->priv->paths, path, branch); | |
1880 | strmap_put(&opt->priv->paths, path, new_ci); | |
1881 | ||
1882 | path_msg(opt, path, 0, | |
1883 | _("CONFLICT (file/directory): directory in the way " | |
1884 | "of %s from %s; moving it to %s instead."), | |
1885 | old_path, branch, path); | |
1886 | ||
1887 | /* | |
1888 | * Zero out the filemask for the old ci. At this point, ci | |
1889 | * was just an entry for a directory, so we don't need to | |
1890 | * do anything more with it. | |
1891 | */ | |
1892 | ci->filemask = 0; | |
1893 | ||
1894 | /* | |
1895 | * Now note that we're working on the new entry (path was | |
1896 | * updated above. | |
1897 | */ | |
1898 | ci = new_ci; | |
6a02dd90 EN |
1899 | } |
1900 | ||
1901 | /* | |
1902 | * NOTE: Below there is a long switch-like if-elseif-elseif... block | |
1903 | * which the code goes through even for the df_conflict cases | |
23366d2a | 1904 | * above. |
6a02dd90 EN |
1905 | */ |
1906 | if (ci->match_mask) { | |
1907 | ci->merged.clean = 1; | |
1908 | if (ci->match_mask == 6) { | |
1909 | /* stages[1] == stages[2] */ | |
1910 | ci->merged.result.mode = ci->stages[1].mode; | |
1911 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
1912 | } else { | |
1913 | /* determine the mask of the side that didn't match */ | |
1914 | unsigned int othermask = 7 & ~ci->match_mask; | |
1915 | int side = (othermask == 4) ? 2 : 1; | |
1916 | ||
1917 | ci->merged.result.mode = ci->stages[side].mode; | |
1918 | ci->merged.is_null = !ci->merged.result.mode; | |
1919 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
1920 | ||
1921 | assert(othermask == 2 || othermask == 4); | |
1922 | assert(ci->merged.is_null == | |
1923 | (ci->filemask == ci->match_mask)); | |
1924 | } | |
1925 | } else if (ci->filemask >= 6 && | |
1926 | (S_IFMT & ci->stages[1].mode) != | |
1927 | (S_IFMT & ci->stages[2].mode)) { | |
4ef88fc3 EN |
1928 | /* Two different items from (file/submodule/symlink) */ |
1929 | if (opt->priv->call_depth) { | |
1930 | /* Just use the version from the merge base */ | |
1931 | ci->merged.clean = 0; | |
1932 | oidcpy(&ci->merged.result.oid, &ci->stages[0].oid); | |
1933 | ci->merged.result.mode = ci->stages[0].mode; | |
1934 | ci->merged.is_null = (ci->merged.result.mode == 0); | |
1935 | } else { | |
1936 | /* Handle by renaming one or both to separate paths. */ | |
1937 | unsigned o_mode = ci->stages[0].mode; | |
1938 | unsigned a_mode = ci->stages[1].mode; | |
1939 | unsigned b_mode = ci->stages[2].mode; | |
1940 | struct conflict_info *new_ci; | |
1941 | const char *a_path = NULL, *b_path = NULL; | |
1942 | int rename_a = 0, rename_b = 0; | |
1943 | ||
1944 | new_ci = xmalloc(sizeof(*new_ci)); | |
1945 | ||
1946 | if (S_ISREG(a_mode)) | |
1947 | rename_a = 1; | |
1948 | else if (S_ISREG(b_mode)) | |
1949 | rename_b = 1; | |
1950 | else { | |
1951 | rename_a = 1; | |
1952 | rename_b = 1; | |
1953 | } | |
1954 | ||
1955 | path_msg(opt, path, 0, | |
1956 | _("CONFLICT (distinct types): %s had different " | |
1957 | "types on each side; renamed %s of them so " | |
1958 | "each can be recorded somewhere."), | |
1959 | path, | |
1960 | (rename_a && rename_b) ? _("both") : _("one")); | |
1961 | ||
1962 | ci->merged.clean = 0; | |
1963 | memcpy(new_ci, ci, sizeof(*new_ci)); | |
1964 | ||
1965 | /* Put b into new_ci, removing a from stages */ | |
1966 | new_ci->merged.result.mode = ci->stages[2].mode; | |
1967 | oidcpy(&new_ci->merged.result.oid, &ci->stages[2].oid); | |
1968 | new_ci->stages[1].mode = 0; | |
1969 | oidcpy(&new_ci->stages[1].oid, &null_oid); | |
1970 | new_ci->filemask = 5; | |
1971 | if ((S_IFMT & b_mode) != (S_IFMT & o_mode)) { | |
1972 | new_ci->stages[0].mode = 0; | |
1973 | oidcpy(&new_ci->stages[0].oid, &null_oid); | |
1974 | new_ci->filemask = 4; | |
1975 | } | |
1976 | ||
1977 | /* Leave only a in ci, fixing stages. */ | |
1978 | ci->merged.result.mode = ci->stages[1].mode; | |
1979 | oidcpy(&ci->merged.result.oid, &ci->stages[1].oid); | |
1980 | ci->stages[2].mode = 0; | |
1981 | oidcpy(&ci->stages[2].oid, &null_oid); | |
1982 | ci->filemask = 3; | |
1983 | if ((S_IFMT & a_mode) != (S_IFMT & o_mode)) { | |
1984 | ci->stages[0].mode = 0; | |
1985 | oidcpy(&ci->stages[0].oid, &null_oid); | |
1986 | ci->filemask = 2; | |
1987 | } | |
1988 | ||
1989 | /* Insert entries into opt->priv_paths */ | |
1990 | assert(rename_a || rename_b); | |
1991 | if (rename_a) { | |
1992 | a_path = unique_path(&opt->priv->paths, | |
1993 | path, opt->branch1); | |
1994 | strmap_put(&opt->priv->paths, a_path, ci); | |
1995 | } | |
1996 | ||
1997 | if (rename_b) | |
1998 | b_path = unique_path(&opt->priv->paths, | |
1999 | path, opt->branch2); | |
2000 | else | |
2001 | b_path = path; | |
2002 | strmap_put(&opt->priv->paths, b_path, new_ci); | |
2003 | ||
2004 | if (rename_a && rename_b) { | |
2005 | strmap_remove(&opt->priv->paths, path, 0); | |
2006 | /* | |
2007 | * We removed path from opt->priv->paths. path | |
2008 | * will also eventually need to be freed, but | |
2009 | * it may still be used by e.g. ci->pathnames. | |
2010 | * So, store it in another string-list for now. | |
2011 | */ | |
2012 | string_list_append(&opt->priv->paths_to_free, | |
2013 | path); | |
2014 | } | |
2015 | ||
2016 | /* | |
2017 | * Do special handling for b_path since process_entry() | |
2018 | * won't be called on it specially. | |
2019 | */ | |
2020 | strmap_put(&opt->priv->conflicted, b_path, new_ci); | |
2021 | record_entry_for_tree(dir_metadata, b_path, | |
2022 | &new_ci->merged); | |
2023 | ||
2024 | /* | |
2025 | * Remaining code for processing this entry should | |
2026 | * think in terms of processing a_path. | |
2027 | */ | |
2028 | if (a_path) | |
2029 | path = a_path; | |
2030 | } | |
6a02dd90 | 2031 | } else if (ci->filemask >= 6) { |
991bbdca EN |
2032 | /* Need a two-way or three-way content merge */ |
2033 | struct version_info merged_file; | |
2034 | unsigned clean_merge; | |
2035 | struct version_info *o = &ci->stages[0]; | |
2036 | struct version_info *a = &ci->stages[1]; | |
2037 | struct version_info *b = &ci->stages[2]; | |
2038 | ||
2039 | clean_merge = handle_content_merge(opt, path, o, a, b, | |
2040 | ci->pathnames, | |
2041 | opt->priv->call_depth * 2, | |
2042 | &merged_file); | |
2043 | ci->merged.clean = clean_merge && | |
2044 | !ci->df_conflict && !ci->path_conflict; | |
2045 | ci->merged.result.mode = merged_file.mode; | |
2046 | ci->merged.is_null = (merged_file.mode == 0); | |
2047 | oidcpy(&ci->merged.result.oid, &merged_file.oid); | |
2048 | if (clean_merge && ci->df_conflict) { | |
2049 | assert(df_file_index == 1 || df_file_index == 2); | |
2050 | ci->filemask = 1 << df_file_index; | |
2051 | ci->stages[df_file_index].mode = merged_file.mode; | |
2052 | oidcpy(&ci->stages[df_file_index].oid, &merged_file.oid); | |
2053 | } | |
2054 | if (!clean_merge) { | |
2055 | const char *reason = _("content"); | |
2056 | if (ci->filemask == 6) | |
2057 | reason = _("add/add"); | |
2058 | if (S_ISGITLINK(merged_file.mode)) | |
2059 | reason = _("submodule"); | |
2060 | path_msg(opt, path, 0, | |
2061 | _("CONFLICT (%s): Merge conflict in %s"), | |
2062 | reason, path); | |
2063 | } | |
6a02dd90 EN |
2064 | } else if (ci->filemask == 3 || ci->filemask == 5) { |
2065 | /* Modify/delete */ | |
c5a6f655 EN |
2066 | const char *modify_branch, *delete_branch; |
2067 | int side = (ci->filemask == 5) ? 2 : 1; | |
2068 | int index = opt->priv->call_depth ? 0 : side; | |
2069 | ||
2070 | ci->merged.result.mode = ci->stages[index].mode; | |
2071 | oidcpy(&ci->merged.result.oid, &ci->stages[index].oid); | |
2072 | ci->merged.clean = 0; | |
2073 | ||
2074 | modify_branch = (side == 1) ? opt->branch1 : opt->branch2; | |
2075 | delete_branch = (side == 1) ? opt->branch2 : opt->branch1; | |
2076 | ||
2e91ddd2 EN |
2077 | if (ci->path_conflict && |
2078 | oideq(&ci->stages[0].oid, &ci->stages[side].oid)) { | |
2079 | /* | |
2080 | * This came from a rename/delete; no action to take, | |
2081 | * but avoid printing "modify/delete" conflict notice | |
2082 | * since the contents were not modified. | |
2083 | */ | |
2084 | } else { | |
2085 | path_msg(opt, path, 0, | |
2086 | _("CONFLICT (modify/delete): %s deleted in %s " | |
2087 | "and modified in %s. Version %s of %s left " | |
2088 | "in tree."), | |
2089 | path, delete_branch, modify_branch, | |
2090 | modify_branch, path); | |
2091 | } | |
6a02dd90 EN |
2092 | } else if (ci->filemask == 2 || ci->filemask == 4) { |
2093 | /* Added on one side */ | |
2094 | int side = (ci->filemask == 4) ? 2 : 1; | |
2095 | ci->merged.result.mode = ci->stages[side].mode; | |
2096 | oidcpy(&ci->merged.result.oid, &ci->stages[side].oid); | |
53e88a03 | 2097 | ci->merged.clean = !ci->df_conflict && !ci->path_conflict; |
6a02dd90 EN |
2098 | } else if (ci->filemask == 1) { |
2099 | /* Deleted on both sides */ | |
2100 | ci->merged.is_null = 1; | |
2101 | ci->merged.result.mode = 0; | |
2102 | oidcpy(&ci->merged.result.oid, &null_oid); | |
53e88a03 | 2103 | ci->merged.clean = !ci->path_conflict; |
6a02dd90 EN |
2104 | } |
2105 | ||
2106 | /* | |
2107 | * If still conflicted, record it separately. This allows us to later | |
2108 | * iterate over just conflicted entries when updating the index instead | |
2109 | * of iterating over all entries. | |
2110 | */ | |
2111 | if (!ci->merged.clean) | |
2112 | strmap_put(&opt->priv->conflicted, path, ci); | |
a9945bba | 2113 | record_entry_for_tree(dir_metadata, path, &ci->merged); |
6a02dd90 EN |
2114 | } |
2115 | ||
231e2dd4 EN |
2116 | static void process_entries(struct merge_options *opt, |
2117 | struct object_id *result_oid) | |
2118 | { | |
6a02dd90 EN |
2119 | struct hashmap_iter iter; |
2120 | struct strmap_entry *e; | |
8adffaa8 EN |
2121 | struct string_list plist = STRING_LIST_INIT_NODUP; |
2122 | struct string_list_item *entry; | |
bb470f4e EN |
2123 | struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP, |
2124 | STRING_LIST_INIT_NODUP, | |
2125 | NULL, 0 }; | |
6a02dd90 EN |
2126 | |
2127 | if (strmap_empty(&opt->priv->paths)) { | |
2128 | oidcpy(result_oid, opt->repo->hash_algo->empty_tree); | |
2129 | return; | |
2130 | } | |
2131 | ||
8adffaa8 EN |
2132 | /* Hack to pre-allocate plist to the desired size */ |
2133 | ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc); | |
2134 | ||
2135 | /* Put every entry from paths into plist, then sort */ | |
6a02dd90 | 2136 | strmap_for_each_entry(&opt->priv->paths, &iter, e) { |
8adffaa8 EN |
2137 | string_list_append(&plist, e->key)->util = e->value; |
2138 | } | |
2139 | plist.cmp = string_list_df_name_compare; | |
2140 | string_list_sort(&plist); | |
2141 | ||
2142 | /* | |
2143 | * Iterate over the items in reverse order, so we can handle paths | |
2144 | * below a directory before needing to handle the directory itself. | |
bb470f4e EN |
2145 | * |
2146 | * This allows us to write subtrees before we need to write trees, | |
2147 | * and it also enables sane handling of directory/file conflicts | |
2148 | * (because it allows us to know whether the directory is still in | |
2149 | * the way when it is time to process the file at the same path). | |
8adffaa8 EN |
2150 | */ |
2151 | for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) { | |
2152 | char *path = entry->string; | |
6a02dd90 EN |
2153 | /* |
2154 | * NOTE: mi may actually be a pointer to a conflict_info, but | |
2155 | * we have to check mi->clean first to see if it's safe to | |
2156 | * reassign to such a pointer type. | |
2157 | */ | |
8adffaa8 | 2158 | struct merged_info *mi = entry->util; |
6a02dd90 | 2159 | |
bb470f4e EN |
2160 | write_completed_directory(opt, mi->directory_name, |
2161 | &dir_metadata); | |
a9945bba EN |
2162 | if (mi->clean) |
2163 | record_entry_for_tree(&dir_metadata, path, mi); | |
2164 | else { | |
8adffaa8 | 2165 | struct conflict_info *ci = (struct conflict_info *)mi; |
a9945bba | 2166 | process_entry(opt, path, ci, &dir_metadata); |
8adffaa8 | 2167 | } |
6a02dd90 EN |
2168 | } |
2169 | ||
bb470f4e EN |
2170 | if (dir_metadata.offsets.nr != 1 || |
2171 | (uintptr_t)dir_metadata.offsets.items[0].util != 0) { | |
2172 | printf("dir_metadata.offsets.nr = %d (should be 1)\n", | |
2173 | dir_metadata.offsets.nr); | |
2174 | printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n", | |
2175 | (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util); | |
2176 | fflush(stdout); | |
2177 | BUG("dir_metadata accounting completely off; shouldn't happen"); | |
2178 | } | |
ee4012dc EN |
2179 | write_tree(result_oid, &dir_metadata.versions, 0, |
2180 | opt->repo->hash_algo->rawsz); | |
8adffaa8 | 2181 | string_list_clear(&plist, 0); |
a9945bba | 2182 | string_list_clear(&dir_metadata.versions, 0); |
bb470f4e | 2183 | string_list_clear(&dir_metadata.offsets, 0); |
231e2dd4 EN |
2184 | } |
2185 | ||
04af1879 EN |
2186 | /*** Function Grouping: functions related to merge_switch_to_result() ***/ |
2187 | ||
9fefce68 EN |
2188 | static int checkout(struct merge_options *opt, |
2189 | struct tree *prev, | |
2190 | struct tree *next) | |
2191 | { | |
6681ce5c EN |
2192 | /* Switch the index/working copy from old to new */ |
2193 | int ret; | |
2194 | struct tree_desc trees[2]; | |
2195 | struct unpack_trees_options unpack_opts; | |
2196 | ||
2197 | memset(&unpack_opts, 0, sizeof(unpack_opts)); | |
2198 | unpack_opts.head_idx = -1; | |
2199 | unpack_opts.src_index = opt->repo->index; | |
2200 | unpack_opts.dst_index = opt->repo->index; | |
2201 | ||
2202 | setup_unpack_trees_porcelain(&unpack_opts, "merge"); | |
2203 | ||
2204 | /* | |
2205 | * NOTE: if this were just "git checkout" code, we would probably | |
2206 | * read or refresh the cache and check for a conflicted index, but | |
2207 | * builtin/merge.c or sequencer.c really needs to read the index | |
2208 | * and check for conflicted entries before starting merging for a | |
2209 | * good user experience (no sense waiting for merges/rebases before | |
2210 | * erroring out), so there's no reason to duplicate that work here. | |
2211 | */ | |
2212 | ||
2213 | /* 2-way merge to the new branch */ | |
2214 | unpack_opts.update = 1; | |
2215 | unpack_opts.merge = 1; | |
2216 | unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */ | |
2217 | unpack_opts.verbose_update = (opt->verbosity > 2); | |
2218 | unpack_opts.fn = twoway_merge; | |
2219 | if (1/* FIXME: opts->overwrite_ignore*/) { | |
2220 | unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir)); | |
2221 | unpack_opts.dir->flags |= DIR_SHOW_IGNORED; | |
2222 | setup_standard_excludes(unpack_opts.dir); | |
2223 | } | |
2224 | parse_tree(prev); | |
2225 | init_tree_desc(&trees[0], prev->buffer, prev->size); | |
2226 | parse_tree(next); | |
2227 | init_tree_desc(&trees[1], next->buffer, next->size); | |
2228 | ||
2229 | ret = unpack_trees(2, trees, &unpack_opts); | |
2230 | clear_unpack_trees_porcelain(&unpack_opts); | |
2231 | dir_clear(unpack_opts.dir); | |
2232 | FREE_AND_NULL(unpack_opts.dir); | |
2233 | return ret; | |
9fefce68 EN |
2234 | } |
2235 | ||
2236 | static int record_conflicted_index_entries(struct merge_options *opt, | |
2237 | struct index_state *index, | |
2238 | struct strmap *paths, | |
2239 | struct strmap *conflicted) | |
2240 | { | |
ef2b3693 EN |
2241 | struct hashmap_iter iter; |
2242 | struct strmap_entry *e; | |
2243 | int errs = 0; | |
2244 | int original_cache_nr; | |
2245 | ||
9fefce68 EN |
2246 | if (strmap_empty(conflicted)) |
2247 | return 0; | |
2248 | ||
ef2b3693 EN |
2249 | original_cache_nr = index->cache_nr; |
2250 | ||
2251 | /* Put every entry from paths into plist, then sort */ | |
2252 | strmap_for_each_entry(conflicted, &iter, e) { | |
2253 | const char *path = e->key; | |
2254 | struct conflict_info *ci = e->value; | |
2255 | int pos; | |
2256 | struct cache_entry *ce; | |
2257 | int i; | |
2258 | ||
2259 | VERIFY_CI(ci); | |
2260 | ||
2261 | /* | |
2262 | * The index will already have a stage=0 entry for this path, | |
2263 | * because we created an as-merged-as-possible version of the | |
2264 | * file and checkout() moved the working copy and index over | |
2265 | * to that version. | |
2266 | * | |
2267 | * However, previous iterations through this loop will have | |
2268 | * added unstaged entries to the end of the cache which | |
2269 | * ignore the standard alphabetical ordering of cache | |
2270 | * entries and break invariants needed for index_name_pos() | |
2271 | * to work. However, we know the entry we want is before | |
2272 | * those appended cache entries, so do a temporary swap on | |
2273 | * cache_nr to only look through entries of interest. | |
2274 | */ | |
2275 | SWAP(index->cache_nr, original_cache_nr); | |
2276 | pos = index_name_pos(index, path, strlen(path)); | |
2277 | SWAP(index->cache_nr, original_cache_nr); | |
2278 | if (pos < 0) { | |
2279 | if (ci->filemask != 1) | |
2280 | BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path); | |
2281 | cache_tree_invalidate_path(index, path); | |
2282 | } else { | |
2283 | ce = index->cache[pos]; | |
2284 | ||
2285 | /* | |
2286 | * Clean paths with CE_SKIP_WORKTREE set will not be | |
2287 | * written to the working tree by the unpack_trees() | |
2288 | * call in checkout(). Our conflicted entries would | |
2289 | * have appeared clean to that code since we ignored | |
2290 | * the higher order stages. Thus, we need override | |
2291 | * the CE_SKIP_WORKTREE bit and manually write those | |
2292 | * files to the working disk here. | |
2293 | * | |
2294 | * TODO: Implement this CE_SKIP_WORKTREE fixup. | |
2295 | */ | |
2296 | ||
2297 | /* | |
2298 | * Mark this cache entry for removal and instead add | |
2299 | * new stage>0 entries corresponding to the | |
2300 | * conflicts. If there are many conflicted entries, we | |
2301 | * want to avoid memmove'ing O(NM) entries by | |
2302 | * inserting the new entries one at a time. So, | |
2303 | * instead, we just add the new cache entries to the | |
2304 | * end (ignoring normal index requirements on sort | |
2305 | * order) and sort the index once we're all done. | |
2306 | */ | |
2307 | ce->ce_flags |= CE_REMOVE; | |
2308 | } | |
2309 | ||
2310 | for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) { | |
2311 | struct version_info *vi; | |
2312 | if (!(ci->filemask & (1ul << i))) | |
2313 | continue; | |
2314 | vi = &ci->stages[i]; | |
2315 | ce = make_cache_entry(index, vi->mode, &vi->oid, | |
2316 | path, i+1, 0); | |
2317 | add_index_entry(index, ce, ADD_CACHE_JUST_APPEND); | |
2318 | } | |
2319 | } | |
2320 | ||
2321 | /* | |
2322 | * Remove the unused cache entries (and invalidate the relevant | |
2323 | * cache-trees), then sort the index entries to get the conflicted | |
2324 | * entries we added to the end into their right locations. | |
2325 | */ | |
2326 | remove_marked_cache_entries(index, 1); | |
2327 | QSORT(index->cache, index->cache_nr, cmp_cache_name_compare); | |
2328 | ||
2329 | return errs; | |
9fefce68 EN |
2330 | } |
2331 | ||
17e5574b EN |
2332 | void merge_switch_to_result(struct merge_options *opt, |
2333 | struct tree *head, | |
2334 | struct merge_result *result, | |
2335 | int update_worktree_and_index, | |
2336 | int display_update_msgs) | |
2337 | { | |
9fefce68 EN |
2338 | assert(opt->priv == NULL); |
2339 | if (result->clean >= 0 && update_worktree_and_index) { | |
2340 | struct merge_options_internal *opti = result->priv; | |
2341 | ||
2342 | if (checkout(opt, head, result->tree)) { | |
2343 | /* failure to function */ | |
2344 | result->clean = -1; | |
2345 | return; | |
2346 | } | |
2347 | ||
2348 | if (record_conflicted_index_entries(opt, opt->repo->index, | |
2349 | &opti->paths, | |
2350 | &opti->conflicted)) { | |
2351 | /* failure to function */ | |
2352 | result->clean = -1; | |
2353 | return; | |
2354 | } | |
2355 | } | |
2356 | ||
2357 | if (display_update_msgs) { | |
c5a6f655 EN |
2358 | struct merge_options_internal *opti = result->priv; |
2359 | struct hashmap_iter iter; | |
2360 | struct strmap_entry *e; | |
2361 | struct string_list olist = STRING_LIST_INIT_NODUP; | |
2362 | int i; | |
2363 | ||
2364 | /* Hack to pre-allocate olist to the desired size */ | |
2365 | ALLOC_GROW(olist.items, strmap_get_size(&opti->output), | |
2366 | olist.alloc); | |
2367 | ||
2368 | /* Put every entry from output into olist, then sort */ | |
2369 | strmap_for_each_entry(&opti->output, &iter, e) { | |
2370 | string_list_append(&olist, e->key)->util = e->value; | |
2371 | } | |
2372 | string_list_sort(&olist); | |
2373 | ||
2374 | /* Iterate over the items, printing them */ | |
2375 | for (i = 0; i < olist.nr; ++i) { | |
2376 | struct strbuf *sb = olist.items[i].util; | |
2377 | ||
2378 | printf("%s", sb->buf); | |
2379 | } | |
2380 | string_list_clear(&olist, 0); | |
f39d05ca EN |
2381 | |
2382 | /* Also include needed rename limit adjustment now */ | |
2383 | diff_warn_rename_limit("merge.renamelimit", | |
2384 | opti->renames.needed_limit, 0); | |
9fefce68 EN |
2385 | } |
2386 | ||
17e5574b EN |
2387 | merge_finalize(opt, result); |
2388 | } | |
2389 | ||
2390 | void merge_finalize(struct merge_options *opt, | |
2391 | struct merge_result *result) | |
2392 | { | |
89422d29 EN |
2393 | struct merge_options_internal *opti = result->priv; |
2394 | ||
2395 | assert(opt->priv == NULL); | |
2396 | ||
43e9c4ee | 2397 | clear_or_reinit_internal_opts(opti, 0); |
89422d29 | 2398 | FREE_AND_NULL(opti); |
17e5574b EN |
2399 | } |
2400 | ||
04af1879 EN |
2401 | /*** Function Grouping: helper functions for merge_incore_*() ***/ |
2402 | ||
4296d8f1 EN |
2403 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
2404 | { | |
2405 | c->maybe_tree = t; | |
2406 | } | |
2407 | ||
4296d8f1 EN |
2408 | static struct commit *make_virtual_commit(struct repository *repo, |
2409 | struct tree *tree, | |
2410 | const char *comment) | |
2411 | { | |
2412 | struct commit *commit = alloc_commit_node(repo); | |
2413 | ||
2414 | set_merge_remote_desc(commit, comment, (struct object *)commit); | |
2415 | set_commit_tree(commit, tree); | |
2416 | commit->object.parsed = 1; | |
2417 | return commit; | |
2418 | } | |
2419 | ||
231e2dd4 EN |
2420 | static void merge_start(struct merge_options *opt, struct merge_result *result) |
2421 | { | |
e4171b1b EN |
2422 | /* Sanity checks on opt */ |
2423 | assert(opt->repo); | |
2424 | ||
2425 | assert(opt->branch1 && opt->branch2); | |
2426 | ||
2427 | assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE && | |
2428 | opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE); | |
2429 | assert(opt->rename_limit >= -1); | |
2430 | assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE); | |
2431 | assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1); | |
2432 | ||
2433 | assert(opt->xdl_opts >= 0); | |
2434 | assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL && | |
2435 | opt->recursive_variant <= MERGE_VARIANT_THEIRS); | |
2436 | ||
2437 | /* | |
2438 | * detect_renames, verbosity, buffer_output, and obuf are ignored | |
2439 | * fields that were used by "recursive" rather than "ort" -- but | |
2440 | * sanity check them anyway. | |
2441 | */ | |
2442 | assert(opt->detect_renames >= -1 && | |
2443 | opt->detect_renames <= DIFF_DETECT_COPY); | |
2444 | assert(opt->verbosity >= 0 && opt->verbosity <= 5); | |
2445 | assert(opt->buffer_output <= 2); | |
2446 | assert(opt->obuf.len == 0); | |
2447 | ||
2448 | assert(opt->priv == NULL); | |
2449 | ||
c8017176 EN |
2450 | /* Default to histogram diff. Actually, just hardcode it...for now. */ |
2451 | opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF); | |
2452 | ||
e4171b1b EN |
2453 | /* Initialization of opt->priv, our internal merge data */ |
2454 | opt->priv = xcalloc(1, sizeof(*opt->priv)); | |
2455 | ||
2456 | /* | |
2457 | * Although we initialize opt->priv->paths with strdup_strings=0, | |
2458 | * that's just to avoid making yet another copy of an allocated | |
2459 | * string. Putting the entry into paths means we are taking | |
43c1dccb | 2460 | * ownership, so we will later free it. paths_to_free is similar. |
e4171b1b EN |
2461 | * |
2462 | * In contrast, conflicted just has a subset of keys from paths, so | |
2463 | * we don't want to free those (it'd be a duplicate free). | |
2464 | */ | |
2465 | strmap_init_with_options(&opt->priv->paths, NULL, 0); | |
2466 | strmap_init_with_options(&opt->priv->conflicted, NULL, 0); | |
43c1dccb | 2467 | string_list_init(&opt->priv->paths_to_free, 0); |
c5a6f655 EN |
2468 | |
2469 | /* | |
2470 | * keys & strbufs in output will sometimes need to outlive "paths", | |
2471 | * so it will have a copy of relevant keys. It's probably a small | |
2472 | * subset of the overall paths that have special output. | |
2473 | */ | |
2474 | strmap_init(&opt->priv->output); | |
231e2dd4 EN |
2475 | } |
2476 | ||
04af1879 EN |
2477 | /*** Function Grouping: merge_incore_*() and their internal variants ***/ |
2478 | ||
231e2dd4 EN |
2479 | /* |
2480 | * Originally from merge_trees_internal(); heavily adapted, though. | |
2481 | */ | |
2482 | static void merge_ort_nonrecursive_internal(struct merge_options *opt, | |
2483 | struct tree *merge_base, | |
2484 | struct tree *side1, | |
2485 | struct tree *side2, | |
2486 | struct merge_result *result) | |
2487 | { | |
2488 | struct object_id working_tree_oid; | |
2489 | ||
0c0d705b EN |
2490 | if (collect_merge_info(opt, merge_base, side1, side2) != 0) { |
2491 | /* | |
2492 | * TRANSLATORS: The %s arguments are: 1) tree hash of a merge | |
2493 | * base, and 2-3) the trees for the two trees we're merging. | |
2494 | */ | |
2495 | err(opt, _("collecting merge info failed for trees %s, %s, %s"), | |
2496 | oid_to_hex(&merge_base->object.oid), | |
2497 | oid_to_hex(&side1->object.oid), | |
2498 | oid_to_hex(&side2->object.oid)); | |
2499 | result->clean = -1; | |
2500 | return; | |
2501 | } | |
2502 | ||
231e2dd4 EN |
2503 | result->clean = detect_and_process_renames(opt, merge_base, |
2504 | side1, side2); | |
2505 | process_entries(opt, &working_tree_oid); | |
2506 | ||
2507 | /* Set return values */ | |
2508 | result->tree = parse_tree_indirect(&working_tree_oid); | |
2509 | /* existence of conflicted entries implies unclean */ | |
2510 | result->clean &= strmap_empty(&opt->priv->conflicted); | |
2511 | if (!opt->priv->call_depth) { | |
2512 | result->priv = opt->priv; | |
2513 | opt->priv = NULL; | |
2514 | } | |
2515 | } | |
2516 | ||
8119214f EN |
2517 | /* |
2518 | * Originally from merge_recursive_internal(); somewhat adapted, though. | |
2519 | */ | |
2520 | static void merge_ort_internal(struct merge_options *opt, | |
2521 | struct commit_list *merge_bases, | |
2522 | struct commit *h1, | |
2523 | struct commit *h2, | |
2524 | struct merge_result *result) | |
2525 | { | |
2526 | struct commit_list *iter; | |
2527 | struct commit *merged_merge_bases; | |
2528 | const char *ancestor_name; | |
2529 | struct strbuf merge_base_abbrev = STRBUF_INIT; | |
2530 | ||
2531 | if (!merge_bases) { | |
2532 | merge_bases = get_merge_bases(h1, h2); | |
2533 | /* See merge-ort.h:merge_incore_recursive() declaration NOTE */ | |
2534 | merge_bases = reverse_commit_list(merge_bases); | |
2535 | } | |
2536 | ||
2537 | merged_merge_bases = pop_commit(&merge_bases); | |
2538 | if (merged_merge_bases == NULL) { | |
2539 | /* if there is no common ancestor, use an empty tree */ | |
2540 | struct tree *tree; | |
2541 | ||
2542 | tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree); | |
2543 | merged_merge_bases = make_virtual_commit(opt->repo, tree, | |
2544 | "ancestor"); | |
2545 | ancestor_name = "empty tree"; | |
2546 | } else if (merge_bases) { | |
2547 | ancestor_name = "merged common ancestors"; | |
2548 | } else { | |
2549 | strbuf_add_unique_abbrev(&merge_base_abbrev, | |
2550 | &merged_merge_bases->object.oid, | |
2551 | DEFAULT_ABBREV); | |
2552 | ancestor_name = merge_base_abbrev.buf; | |
2553 | } | |
2554 | ||
2555 | for (iter = merge_bases; iter; iter = iter->next) { | |
2556 | const char *saved_b1, *saved_b2; | |
2557 | struct commit *prev = merged_merge_bases; | |
2558 | ||
2559 | opt->priv->call_depth++; | |
2560 | /* | |
2561 | * When the merge fails, the result contains files | |
2562 | * with conflict markers. The cleanness flag is | |
2563 | * ignored (unless indicating an error), it was never | |
2564 | * actually used, as result of merge_trees has always | |
2565 | * overwritten it: the committed "conflicts" were | |
2566 | * already resolved. | |
2567 | */ | |
2568 | saved_b1 = opt->branch1; | |
2569 | saved_b2 = opt->branch2; | |
2570 | opt->branch1 = "Temporary merge branch 1"; | |
2571 | opt->branch2 = "Temporary merge branch 2"; | |
2572 | merge_ort_internal(opt, NULL, prev, iter->item, result); | |
2573 | if (result->clean < 0) | |
2574 | return; | |
2575 | opt->branch1 = saved_b1; | |
2576 | opt->branch2 = saved_b2; | |
2577 | opt->priv->call_depth--; | |
2578 | ||
2579 | merged_merge_bases = make_virtual_commit(opt->repo, | |
2580 | result->tree, | |
2581 | "merged tree"); | |
2582 | commit_list_insert(prev, &merged_merge_bases->parents); | |
2583 | commit_list_insert(iter->item, | |
2584 | &merged_merge_bases->parents->next); | |
2585 | ||
2586 | clear_or_reinit_internal_opts(opt->priv, 1); | |
2587 | } | |
2588 | ||
2589 | opt->ancestor = ancestor_name; | |
2590 | merge_ort_nonrecursive_internal(opt, | |
2591 | repo_get_commit_tree(opt->repo, | |
2592 | merged_merge_bases), | |
2593 | repo_get_commit_tree(opt->repo, h1), | |
2594 | repo_get_commit_tree(opt->repo, h2), | |
2595 | result); | |
2596 | strbuf_release(&merge_base_abbrev); | |
2597 | opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */ | |
2598 | } | |
2599 | ||
17e5574b EN |
2600 | void merge_incore_nonrecursive(struct merge_options *opt, |
2601 | struct tree *merge_base, | |
2602 | struct tree *side1, | |
2603 | struct tree *side2, | |
2604 | struct merge_result *result) | |
2605 | { | |
231e2dd4 EN |
2606 | assert(opt->ancestor != NULL); |
2607 | merge_start(opt, result); | |
2608 | merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result); | |
17e5574b EN |
2609 | } |
2610 | ||
2611 | void merge_incore_recursive(struct merge_options *opt, | |
2612 | struct commit_list *merge_bases, | |
2613 | struct commit *side1, | |
2614 | struct commit *side2, | |
2615 | struct merge_result *result) | |
2616 | { | |
8119214f EN |
2617 | /* We set the ancestor label based on the merge_bases */ |
2618 | assert(opt->ancestor == NULL); | |
2619 | ||
2620 | merge_start(opt, result); | |
2621 | merge_ort_internal(opt, merge_bases, side1, side2, result); | |
17e5574b | 2622 | } |