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