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