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