Merge branch 'ab/gettext-charset-comment-fix'
[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 "object-store.h"
29 #include "strmap.h"
30 #include "tree.h"
31 #include "unpack-trees.h"
32 #include "xdiff-interface.h"
33
34 /*
35  * We have many arrays of size 3.  Whenever we have such an array, the
36  * indices refer to one of the sides of the three-way merge.  This is so
37  * pervasive that the constants 0, 1, and 2 are used in many places in the
38  * code (especially in arithmetic operations to find the other side's index
39  * or to compute a relevant mask), but sometimes these enum names are used
40  * to aid code clarity.
41  *
42  * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
43  * referred to there is one of these three sides.
44  */
45 enum merge_side {
46         MERGE_BASE = 0,
47         MERGE_SIDE1 = 1,
48         MERGE_SIDE2 = 2
49 };
50
51 struct merge_options_internal {
52         /*
53          * paths: primary data structure in all of merge ort.
54          *
55          * The keys of paths:
56          *   * are full relative paths from the toplevel of the repository
57          *     (e.g. "drivers/firmware/raspberrypi.c").
58          *   * store all relevant paths in the repo, both directories and
59          *     files (e.g. drivers, drivers/firmware would also be included)
60          *   * these keys serve to intern all the path strings, which allows
61          *     us to do pointer comparison on directory names instead of
62          *     strcmp; we just have to be careful to use the interned strings.
63          *     (Technically paths_to_free may track some strings that were
64          *      removed from froms paths.)
65          *
66          * The values of paths:
67          *   * either a pointer to a merged_info, or a conflict_info struct
68          *   * merged_info contains all relevant information for a
69          *     non-conflicted entry.
70          *   * conflict_info contains a merged_info, plus any additional
71          *     information about a conflict such as the higher orders stages
72          *     involved and the names of the paths those came from (handy
73          *     once renames get involved).
74          *   * a path may start "conflicted" (i.e. point to a conflict_info)
75          *     and then a later step (e.g. three-way content merge) determines
76          *     it can be cleanly merged, at which point it'll be marked clean
77          *     and the algorithm will ignore any data outside the contained
78          *     merged_info for that entry
79          *   * If an entry remains conflicted, the merged_info portion of a
80          *     conflict_info will later be filled with whatever version of
81          *     the file should be placed in the working directory (e.g. an
82          *     as-merged-as-possible variation that contains conflict markers).
83          */
84         struct strmap paths;
85
86         /*
87          * conflicted: a subset of keys->values from "paths"
88          *
89          * conflicted is basically an optimization between process_entries()
90          * and record_conflicted_index_entries(); the latter could loop over
91          * ALL the entries in paths AGAIN and look for the ones that are
92          * still conflicted, but since process_entries() has to loop over
93          * all of them, it saves the ones it couldn't resolve in this strmap
94          * so that record_conflicted_index_entries() can iterate just the
95          * relevant entries.
96          */
97         struct strmap conflicted;
98
99         /*
100          * paths_to_free: additional list of strings to free
101          *
102          * If keys are removed from "paths", they are added to paths_to_free
103          * to ensure they are later freed.  We avoid free'ing immediately since
104          * other places (e.g. conflict_info.pathnames[]) may still be
105          * referencing these paths.
106          */
107         struct string_list paths_to_free;
108
109         /*
110          * output: special messages and conflict notices for various paths
111          *
112          * This is a map of pathnames (a subset of the keys in "paths" above)
113          * to strbufs.  It gathers various warning/conflict/notice messages
114          * for later processing.
115          */
116         struct strmap output;
117
118         /*
119          * current_dir_name: temporary var used in collect_merge_info_callback()
120          *
121          * Used to set merged_info.directory_name; see documentation for that
122          * variable and the requirements placed on that field.
123          */
124         const char *current_dir_name;
125
126         /* call_depth: recursion level counter for merging merge bases */
127         int call_depth;
128 };
129
130 struct version_info {
131         struct object_id oid;
132         unsigned short mode;
133 };
134
135 struct merged_info {
136         /* if is_null, ignore result.  otherwise result has oid & mode */
137         struct version_info result;
138         unsigned is_null:1;
139
140         /*
141          * clean: whether the path in question is cleanly merged.
142          *
143          * see conflict_info.merged for more details.
144          */
145         unsigned clean:1;
146
147         /*
148          * basename_offset: offset of basename of path.
149          *
150          * perf optimization to avoid recomputing offset of final '/'
151          * character in pathname (0 if no '/' in pathname).
152          */
153         size_t basename_offset;
154
155          /*
156           * directory_name: containing directory name.
157           *
158           * Note that we assume directory_name is constructed such that
159           *    strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
160           * i.e. string equality is equivalent to pointer equality.  For this
161           * to hold, we have to be careful setting directory_name.
162           */
163         const char *directory_name;
164 };
165
166 struct conflict_info {
167         /*
168          * merged: the version of the path that will be written to working tree
169          *
170          * WARNING: It is critical to check merged.clean and ensure it is 0
171          * before reading any conflict_info fields outside of merged.
172          * Allocated merge_info structs will always have clean set to 1.
173          * Allocated conflict_info structs will have merged.clean set to 0
174          * initially.  The merged.clean field is how we know if it is safe
175          * to access other parts of conflict_info besides merged; if a
176          * conflict_info's merged.clean is changed to 1, the rest of the
177          * algorithm is not allowed to look at anything outside of the
178          * merged member anymore.
179          */
180         struct merged_info merged;
181
182         /* oids & modes from each of the three trees for this path */
183         struct version_info stages[3];
184
185         /* pathnames for each stage; may differ due to rename detection */
186         const char *pathnames[3];
187
188         /* Whether this path is/was involved in a directory/file conflict */
189         unsigned df_conflict:1;
190
191         /*
192          * Whether this path is/was involved in a non-content conflict other
193          * than a directory/file conflict (e.g. rename/rename, rename/delete,
194          * file location based on possible directory rename).
195          */
196         unsigned path_conflict:1;
197
198         /*
199          * For filemask and dirmask, the ith bit corresponds to whether the
200          * ith entry is a file (filemask) or a directory (dirmask).  Thus,
201          * filemask & dirmask is always zero, and filemask | dirmask is at
202          * most 7 but can be less when a path does not appear as either a
203          * file or a directory on at least one side of history.
204          *
205          * Note that these masks are related to enum merge_side, as the ith
206          * entry corresponds to side i.
207          *
208          * These values come from a traverse_trees() call; more info may be
209          * found looking at tree-walk.h's struct traverse_info,
210          * particularly the documentation above the "fn" member (note that
211          * filemask = mask & ~dirmask from that documentation).
212          */
213         unsigned filemask:3;
214         unsigned dirmask:3;
215
216         /*
217          * Optimization to track which stages match, to avoid the need to
218          * recompute it in multiple steps. Either 0 or at least 2 bits are
219          * set; if at least 2 bits are set, their corresponding stages match.
220          */
221         unsigned match_mask:3;
222 };
223
224 /*** Function Grouping: various utility functions ***/
225
226 /*
227  * For the next three macros, see warning for conflict_info.merged.
228  *
229  * In each of the below, mi is a struct merged_info*, and ci was defined
230  * as a struct conflict_info* (but we need to verify ci isn't actually
231  * pointed at a struct merged_info*).
232  *
233  * INITIALIZE_CI: Assign ci to mi but only if it's safe; set to NULL otherwise.
234  * VERIFY_CI: Ensure that something we assigned to a conflict_info* is one.
235  * ASSIGN_AND_VERIFY_CI: Similar to VERIFY_CI but do assignment first.
236  */
237 #define INITIALIZE_CI(ci, mi) do {                                           \
238         (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
239 } while (0)
240 #define VERIFY_CI(ci) assert(ci && !ci->merged.clean);
241 #define ASSIGN_AND_VERIFY_CI(ci, mi) do {    \
242         (ci) = (struct conflict_info *)(mi);  \
243         assert((ci) && !(mi)->clean);        \
244 } while (0)
245
246 static void free_strmap_strings(struct strmap *map)
247 {
248         struct hashmap_iter iter;
249         struct strmap_entry *entry;
250
251         strmap_for_each_entry(map, &iter, entry) {
252                 free((char*)entry->key);
253         }
254 }
255
256 static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
257                                           int reinitialize)
258 {
259         void (*strmap_func)(struct strmap *, int) =
260                 reinitialize ? strmap_partial_clear : strmap_clear;
261
262         /*
263          * We marked opti->paths with strdup_strings = 0, so that we
264          * wouldn't have to make another copy of the fullpath created by
265          * make_traverse_path from setup_path_info().  But, now that we've
266          * used it and have no other references to these strings, it is time
267          * to deallocate them.
268          */
269         free_strmap_strings(&opti->paths);
270         strmap_func(&opti->paths, 1);
271
272         /*
273          * All keys and values in opti->conflicted are a subset of those in
274          * opti->paths.  We don't want to deallocate anything twice, so we
275          * don't free the keys and we pass 0 for free_values.
276          */
277         strmap_func(&opti->conflicted, 0);
278
279         /*
280          * opti->paths_to_free is similar to opti->paths; we created it with
281          * strdup_strings = 0 to avoid making _another_ copy of the fullpath
282          * but now that we've used it and have no other references to these
283          * strings, it is time to deallocate them.  We do so by temporarily
284          * setting strdup_strings to 1.
285          */
286         opti->paths_to_free.strdup_strings = 1;
287         string_list_clear(&opti->paths_to_free, 0);
288         opti->paths_to_free.strdup_strings = 0;
289
290         if (!reinitialize) {
291                 struct hashmap_iter iter;
292                 struct strmap_entry *e;
293
294                 /* Release and free each strbuf found in output */
295                 strmap_for_each_entry(&opti->output, &iter, e) {
296                         struct strbuf *sb = e->value;
297                         strbuf_release(sb);
298                         /*
299                          * While strictly speaking we don't need to free(sb)
300                          * here because we could pass free_values=1 when
301                          * calling strmap_clear() on opti->output, that would
302                          * require strmap_clear to do another
303                          * strmap_for_each_entry() loop, so we just free it
304                          * while we're iterating anyway.
305                          */
306                         free(sb);
307                 }
308                 strmap_clear(&opti->output, 0);
309         }
310 }
311
312 static int err(struct merge_options *opt, const char *err, ...)
313 {
314         va_list params;
315         struct strbuf sb = STRBUF_INIT;
316
317         strbuf_addstr(&sb, "error: ");
318         va_start(params, err);
319         strbuf_vaddf(&sb, err, params);
320         va_end(params);
321
322         error("%s", sb.buf);
323         strbuf_release(&sb);
324
325         return -1;
326 }
327
328 __attribute__((format (printf, 4, 5)))
329 static void path_msg(struct merge_options *opt,
330                      const char *path,
331                      int omittable_hint, /* skippable under --remerge-diff */
332                      const char *fmt, ...)
333 {
334         va_list ap;
335         struct strbuf *sb = strmap_get(&opt->priv->output, path);
336         if (!sb) {
337                 sb = xmalloc(sizeof(*sb));
338                 strbuf_init(sb, 0);
339                 strmap_put(&opt->priv->output, path, sb);
340         }
341
342         va_start(ap, fmt);
343         strbuf_vaddf(sb, fmt, ap);
344         va_end(ap);
345
346         strbuf_addch(sb, '\n');
347 }
348
349 /*** Function Grouping: functions related to collect_merge_info() ***/
350
351 static void setup_path_info(struct merge_options *opt,
352                             struct string_list_item *result,
353                             const char *current_dir_name,
354                             int current_dir_name_len,
355                             char *fullpath, /* we'll take over ownership */
356                             struct name_entry *names,
357                             struct name_entry *merged_version,
358                             unsigned is_null,     /* boolean */
359                             unsigned df_conflict, /* boolean */
360                             unsigned filemask,
361                             unsigned dirmask,
362                             int resolved          /* boolean */)
363 {
364         /* result->util is void*, so mi is a convenience typed variable */
365         struct merged_info *mi;
366
367         assert(!is_null || resolved);
368         assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
369         assert(resolved == (merged_version != NULL));
370
371         mi = xcalloc(1, resolved ? sizeof(struct merged_info) :
372                                    sizeof(struct conflict_info));
373         mi->directory_name = current_dir_name;
374         mi->basename_offset = current_dir_name_len;
375         mi->clean = !!resolved;
376         if (resolved) {
377                 mi->result.mode = merged_version->mode;
378                 oidcpy(&mi->result.oid, &merged_version->oid);
379                 mi->is_null = !!is_null;
380         } else {
381                 int i;
382                 struct conflict_info *ci;
383
384                 ASSIGN_AND_VERIFY_CI(ci, mi);
385                 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
386                         ci->pathnames[i] = fullpath;
387                         ci->stages[i].mode = names[i].mode;
388                         oidcpy(&ci->stages[i].oid, &names[i].oid);
389                 }
390                 ci->filemask = filemask;
391                 ci->dirmask = dirmask;
392                 ci->df_conflict = !!df_conflict;
393                 if (dirmask)
394                         /*
395                          * Assume is_null for now, but if we have entries
396                          * under the directory then when it is complete in
397                          * write_completed_directory() it'll update this.
398                          * Also, for D/F conflicts, we have to handle the
399                          * directory first, then clear this bit and process
400                          * the file to see how it is handled -- that occurs
401                          * near the top of process_entry().
402                          */
403                         mi->is_null = 1;
404         }
405         strmap_put(&opt->priv->paths, fullpath, mi);
406         result->string = fullpath;
407         result->util = mi;
408 }
409
410 static int collect_merge_info_callback(int n,
411                                        unsigned long mask,
412                                        unsigned long dirmask,
413                                        struct name_entry *names,
414                                        struct traverse_info *info)
415 {
416         /*
417          * n is 3.  Always.
418          * common ancestor (mbase) has mask 1, and stored in index 0 of names
419          * head of side 1  (side1) has mask 2, and stored in index 1 of names
420          * head of side 2  (side2) has mask 4, and stored in index 2 of names
421          */
422         struct merge_options *opt = info->data;
423         struct merge_options_internal *opti = opt->priv;
424         struct string_list_item pi;  /* Path Info */
425         struct conflict_info *ci; /* typed alias to pi.util (which is void*) */
426         struct name_entry *p;
427         size_t len;
428         char *fullpath;
429         const char *dirname = opti->current_dir_name;
430         unsigned filemask = mask & ~dirmask;
431         unsigned match_mask = 0; /* will be updated below */
432         unsigned mbase_null = !(mask & 1);
433         unsigned side1_null = !(mask & 2);
434         unsigned side2_null = !(mask & 4);
435         unsigned side1_matches_mbase = (!side1_null && !mbase_null &&
436                                         names[0].mode == names[1].mode &&
437                                         oideq(&names[0].oid, &names[1].oid));
438         unsigned side2_matches_mbase = (!side2_null && !mbase_null &&
439                                         names[0].mode == names[2].mode &&
440                                         oideq(&names[0].oid, &names[2].oid));
441         unsigned sides_match = (!side1_null && !side2_null &&
442                                 names[1].mode == names[2].mode &&
443                                 oideq(&names[1].oid, &names[2].oid));
444
445         /*
446          * Note: When a path is a file on one side of history and a directory
447          * in another, we have a directory/file conflict.  In such cases, if
448          * the conflict doesn't resolve from renames and deletions, then we
449          * always leave directories where they are and move files out of the
450          * way.  Thus, while struct conflict_info has a df_conflict field to
451          * track such conflicts, we ignore that field for any directories at
452          * a path and only pay attention to it for files at the given path.
453          * The fact that we leave directories were they are also means that
454          * we do not need to worry about getting additional df_conflict
455          * information propagated from parent directories down to children
456          * (unlike, say traverse_trees_recursive() in unpack-trees.c, which
457          * sets a newinfo.df_conflicts field specifically to propagate it).
458          */
459         unsigned df_conflict = (filemask != 0) && (dirmask != 0);
460
461         /* n = 3 is a fundamental assumption. */
462         if (n != 3)
463                 BUG("Called collect_merge_info_callback wrong");
464
465         /*
466          * A bunch of sanity checks verifying that traverse_trees() calls
467          * us the way I expect.  Could just remove these at some point,
468          * though maybe they are helpful to future code readers.
469          */
470         assert(mbase_null == is_null_oid(&names[0].oid));
471         assert(side1_null == is_null_oid(&names[1].oid));
472         assert(side2_null == is_null_oid(&names[2].oid));
473         assert(!mbase_null || !side1_null || !side2_null);
474         assert(mask > 0 && mask < 8);
475
476         /* Determine match_mask */
477         if (side1_matches_mbase)
478                 match_mask = (side2_matches_mbase ? 7 : 3);
479         else if (side2_matches_mbase)
480                 match_mask = 5;
481         else if (sides_match)
482                 match_mask = 6;
483
484         /*
485          * Get the name of the relevant filepath, which we'll pass to
486          * setup_path_info() for tracking.
487          */
488         p = names;
489         while (!p->mode)
490                 p++;
491         len = traverse_path_len(info, p->pathlen);
492
493         /* +1 in both of the following lines to include the NUL byte */
494         fullpath = xmalloc(len + 1);
495         make_traverse_path(fullpath, len + 1, info, p->path, p->pathlen);
496
497         /*
498          * If mbase, side1, and side2 all match, we can resolve early.  Even
499          * if these are trees, there will be no renames or anything
500          * underneath.
501          */
502         if (side1_matches_mbase && side2_matches_mbase) {
503                 /* mbase, side1, & side2 all match; use mbase as resolution */
504                 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
505                                 names, names+0, mbase_null, 0,
506                                 filemask, dirmask, 1);
507                 return mask;
508         }
509
510         /*
511          * Record information about the path so we can resolve later in
512          * process_entries.
513          */
514         setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
515                         names, NULL, 0, df_conflict, filemask, dirmask, 0);
516
517         ci = pi.util;
518         VERIFY_CI(ci);
519         ci->match_mask = match_mask;
520
521         /* If dirmask, recurse into subdirectories */
522         if (dirmask) {
523                 struct traverse_info newinfo;
524                 struct tree_desc t[3];
525                 void *buf[3] = {NULL, NULL, NULL};
526                 const char *original_dir_name;
527                 int i, ret;
528
529                 ci->match_mask &= filemask;
530                 newinfo = *info;
531                 newinfo.prev = info;
532                 newinfo.name = p->path;
533                 newinfo.namelen = p->pathlen;
534                 newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1);
535                 /*
536                  * If this directory we are about to recurse into cared about
537                  * its parent directory (the current directory) having a D/F
538                  * conflict, then we'd propagate the masks in this way:
539                  *    newinfo.df_conflicts |= (mask & ~dirmask);
540                  * But we don't worry about propagating D/F conflicts.  (See
541                  * comment near setting of local df_conflict variable near
542                  * the beginning of this function).
543                  */
544
545                 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
546                         if (i == 1 && side1_matches_mbase)
547                                 t[1] = t[0];
548                         else if (i == 2 && side2_matches_mbase)
549                                 t[2] = t[0];
550                         else if (i == 2 && sides_match)
551                                 t[2] = t[1];
552                         else {
553                                 const struct object_id *oid = NULL;
554                                 if (dirmask & 1)
555                                         oid = &names[i].oid;
556                                 buf[i] = fill_tree_descriptor(opt->repo,
557                                                               t + i, oid);
558                         }
559                         dirmask >>= 1;
560                 }
561
562                 original_dir_name = opti->current_dir_name;
563                 opti->current_dir_name = pi.string;
564                 ret = traverse_trees(NULL, 3, t, &newinfo);
565                 opti->current_dir_name = original_dir_name;
566
567                 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++)
568                         free(buf[i]);
569
570                 if (ret < 0)
571                         return -1;
572         }
573
574         return mask;
575 }
576
577 static int collect_merge_info(struct merge_options *opt,
578                               struct tree *merge_base,
579                               struct tree *side1,
580                               struct tree *side2)
581 {
582         int ret;
583         struct tree_desc t[3];
584         struct traverse_info info;
585         const char *toplevel_dir_placeholder = "";
586
587         opt->priv->current_dir_name = toplevel_dir_placeholder;
588         setup_traverse_info(&info, toplevel_dir_placeholder);
589         info.fn = collect_merge_info_callback;
590         info.data = opt;
591         info.show_all_errors = 1;
592
593         parse_tree(merge_base);
594         parse_tree(side1);
595         parse_tree(side2);
596         init_tree_desc(t + 0, merge_base->buffer, merge_base->size);
597         init_tree_desc(t + 1, side1->buffer, side1->size);
598         init_tree_desc(t + 2, side2->buffer, side2->size);
599
600         ret = traverse_trees(NULL, 3, t, &info);
601
602         return ret;
603 }
604
605 /*** Function Grouping: functions related to threeway content merges ***/
606
607 static int handle_content_merge(struct merge_options *opt,
608                                 const char *path,
609                                 const struct version_info *o,
610                                 const struct version_info *a,
611                                 const struct version_info *b,
612                                 const char *pathnames[3],
613                                 const int extra_marker_size,
614                                 struct version_info *result)
615 {
616         die("Not yet implemented");
617 }
618
619 /*** Function Grouping: functions related to detect_and_process_renames(), ***
620  *** which are split into directory and regular rename detection sections. ***/
621
622 /*** Function Grouping: functions related to directory rename detection ***/
623
624 /*** Function Grouping: functions related to regular rename detection ***/
625
626 static int detect_and_process_renames(struct merge_options *opt,
627                                       struct tree *merge_base,
628                                       struct tree *side1,
629                                       struct tree *side2)
630 {
631         int clean = 1;
632
633         /*
634          * Rename detection works by detecting file similarity.  Here we use
635          * a really easy-to-implement scheme: files are similar IFF they have
636          * the same filename.  Therefore, by this scheme, there are no renames.
637          *
638          * TODO: Actually implement a real rename detection scheme.
639          */
640         return clean;
641 }
642
643 /*** Function Grouping: functions related to process_entries() ***/
644
645 static int string_list_df_name_compare(const char *one, const char *two)
646 {
647         int onelen = strlen(one);
648         int twolen = strlen(two);
649         /*
650          * Here we only care that entries for D/F conflicts are
651          * adjacent, in particular with the file of the D/F conflict
652          * appearing before files below the corresponding directory.
653          * The order of the rest of the list is irrelevant for us.
654          *
655          * To achieve this, we sort with df_name_compare and provide
656          * the mode S_IFDIR so that D/F conflicts will sort correctly.
657          * We use the mode S_IFDIR for everything else for simplicity,
658          * since in other cases any changes in their order due to
659          * sorting cause no problems for us.
660          */
661         int cmp = df_name_compare(one, onelen, S_IFDIR,
662                                   two, twolen, S_IFDIR);
663         /*
664          * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
665          * that 'foo' comes before 'foo/bar'.
666          */
667         if (cmp)
668                 return cmp;
669         return onelen - twolen;
670 }
671
672 struct directory_versions {
673         /*
674          * versions: list of (basename -> version_info)
675          *
676          * The basenames are in reverse lexicographic order of full pathnames,
677          * as processed in process_entries().  This puts all entries within
678          * a directory together, and covers the directory itself after
679          * everything within it, allowing us to write subtrees before needing
680          * to record information for the tree itself.
681          */
682         struct string_list versions;
683
684         /*
685          * offsets: list of (full relative path directories -> integer offsets)
686          *
687          * Since versions contains basenames from files in multiple different
688          * directories, we need to know which entries in versions correspond
689          * to which directories.  Values of e.g.
690          *     ""             0
691          *     src            2
692          *     src/moduleA    5
693          * Would mean that entries 0-1 of versions are files in the toplevel
694          * directory, entries 2-4 are files under src/, and the remaining
695          * entries starting at index 5 are files under src/moduleA/.
696          */
697         struct string_list offsets;
698
699         /*
700          * last_directory: directory that previously processed file found in
701          *
702          * last_directory starts NULL, but records the directory in which the
703          * previous file was found within.  As soon as
704          *    directory(current_file) != last_directory
705          * then we need to start updating accounting in versions & offsets.
706          * Note that last_directory is always the last path in "offsets" (or
707          * NULL if "offsets" is empty) so this exists just for quick access.
708          */
709         const char *last_directory;
710
711         /* last_directory_len: cached computation of strlen(last_directory) */
712         unsigned last_directory_len;
713 };
714
715 static int tree_entry_order(const void *a_, const void *b_)
716 {
717         const struct string_list_item *a = a_;
718         const struct string_list_item *b = b_;
719
720         const struct merged_info *ami = a->util;
721         const struct merged_info *bmi = b->util;
722         return base_name_compare(a->string, strlen(a->string), ami->result.mode,
723                                  b->string, strlen(b->string), bmi->result.mode);
724 }
725
726 static void write_tree(struct object_id *result_oid,
727                        struct string_list *versions,
728                        unsigned int offset,
729                        size_t hash_size)
730 {
731         size_t maxlen = 0, extra;
732         unsigned int nr = versions->nr - offset;
733         struct strbuf buf = STRBUF_INIT;
734         struct string_list relevant_entries = STRING_LIST_INIT_NODUP;
735         int i;
736
737         /*
738          * We want to sort the last (versions->nr-offset) entries in versions.
739          * Do so by abusing the string_list API a bit: make another string_list
740          * that contains just those entries and then sort them.
741          *
742          * We won't use relevant_entries again and will let it just pop off the
743          * stack, so there won't be allocation worries or anything.
744          */
745         relevant_entries.items = versions->items + offset;
746         relevant_entries.nr = versions->nr - offset;
747         QSORT(relevant_entries.items, relevant_entries.nr, tree_entry_order);
748
749         /* Pre-allocate some space in buf */
750         extra = hash_size + 8; /* 8: 6 for mode, 1 for space, 1 for NUL char */
751         for (i = 0; i < nr; i++) {
752                 maxlen += strlen(versions->items[offset+i].string) + extra;
753         }
754         strbuf_grow(&buf, maxlen);
755
756         /* Write each entry out to buf */
757         for (i = 0; i < nr; i++) {
758                 struct merged_info *mi = versions->items[offset+i].util;
759                 struct version_info *ri = &mi->result;
760                 strbuf_addf(&buf, "%o %s%c",
761                             ri->mode,
762                             versions->items[offset+i].string, '\0');
763                 strbuf_add(&buf, ri->oid.hash, hash_size);
764         }
765
766         /* Write this object file out, and record in result_oid */
767         write_object_file(buf.buf, buf.len, tree_type, result_oid);
768         strbuf_release(&buf);
769 }
770
771 static void record_entry_for_tree(struct directory_versions *dir_metadata,
772                                   const char *path,
773                                   struct merged_info *mi)
774 {
775         const char *basename;
776
777         if (mi->is_null)
778                 /* nothing to record */
779                 return;
780
781         basename = path + mi->basename_offset;
782         assert(strchr(basename, '/') == NULL);
783         string_list_append(&dir_metadata->versions,
784                            basename)->util = &mi->result;
785 }
786
787 static void write_completed_directory(struct merge_options *opt,
788                                       const char *new_directory_name,
789                                       struct directory_versions *info)
790 {
791         const char *prev_dir;
792         struct merged_info *dir_info = NULL;
793         unsigned int offset;
794
795         /*
796          * Some explanation of info->versions and info->offsets...
797          *
798          * process_entries() iterates over all relevant files AND
799          * directories in reverse lexicographic order, and calls this
800          * function.  Thus, an example of the paths that process_entries()
801          * could operate on (along with the directories for those paths
802          * being shown) is:
803          *
804          *     xtract.c             ""
805          *     tokens.txt           ""
806          *     src/moduleB/umm.c    src/moduleB
807          *     src/moduleB/stuff.h  src/moduleB
808          *     src/moduleB/baz.c    src/moduleB
809          *     src/moduleB          src
810          *     src/moduleA/foo.c    src/moduleA
811          *     src/moduleA/bar.c    src/moduleA
812          *     src/moduleA          src
813          *     src                  ""
814          *     Makefile             ""
815          *
816          * info->versions:
817          *
818          *     always contains the unprocessed entries and their
819          *     version_info information.  For example, after the first five
820          *     entries above, info->versions would be:
821          *
822          *         xtract.c     <xtract.c's version_info>
823          *         token.txt    <token.txt's version_info>
824          *         umm.c        <src/moduleB/umm.c's version_info>
825          *         stuff.h      <src/moduleB/stuff.h's version_info>
826          *         baz.c        <src/moduleB/baz.c's version_info>
827          *
828          *     Once a subdirectory is completed we remove the entries in
829          *     that subdirectory from info->versions, writing it as a tree
830          *     (write_tree()).  Thus, as soon as we get to src/moduleB,
831          *     info->versions would be updated to
832          *
833          *         xtract.c     <xtract.c's version_info>
834          *         token.txt    <token.txt's version_info>
835          *         moduleB      <src/moduleB's version_info>
836          *
837          * info->offsets:
838          *
839          *     helps us track which entries in info->versions correspond to
840          *     which directories.  When we are N directories deep (e.g. 4
841          *     for src/modA/submod/subdir/), we have up to N+1 unprocessed
842          *     directories (+1 because of toplevel dir).  Corresponding to
843          *     the info->versions example above, after processing five entries
844          *     info->offsets will be:
845          *
846          *         ""           0
847          *         src/moduleB  2
848          *
849          *     which is used to know that xtract.c & token.txt are from the
850          *     toplevel dirctory, while umm.c & stuff.h & baz.c are from the
851          *     src/moduleB directory.  Again, following the example above,
852          *     once we need to process src/moduleB, then info->offsets is
853          *     updated to
854          *
855          *         ""           0
856          *         src          2
857          *
858          *     which says that moduleB (and only moduleB so far) is in the
859          *     src directory.
860          *
861          *     One unique thing to note about info->offsets here is that
862          *     "src" was not added to info->offsets until there was a path
863          *     (a file OR directory) immediately below src/ that got
864          *     processed.
865          *
866          * Since process_entry() just appends new entries to info->versions,
867          * write_completed_directory() only needs to do work if the next path
868          * is in a directory that is different than the last directory found
869          * in info->offsets.
870          */
871
872         /*
873          * If we are working with the same directory as the last entry, there
874          * is no work to do.  (See comments above the directory_name member of
875          * struct merged_info for why we can use pointer comparison instead of
876          * strcmp here.)
877          */
878         if (new_directory_name == info->last_directory)
879                 return;
880
881         /*
882          * If we are just starting (last_directory is NULL), or last_directory
883          * is a prefix of the current directory, then we can just update
884          * info->offsets to record the offset where we started this directory
885          * and update last_directory to have quick access to it.
886          */
887         if (info->last_directory == NULL ||
888             !strncmp(new_directory_name, info->last_directory,
889                      info->last_directory_len)) {
890                 uintptr_t offset = info->versions.nr;
891
892                 info->last_directory = new_directory_name;
893                 info->last_directory_len = strlen(info->last_directory);
894                 /*
895                  * Record the offset into info->versions where we will
896                  * start recording basenames of paths found within
897                  * new_directory_name.
898                  */
899                 string_list_append(&info->offsets,
900                                    info->last_directory)->util = (void*)offset;
901                 return;
902         }
903
904         /*
905          * The next entry that will be processed will be within
906          * new_directory_name.  Since at this point we know that
907          * new_directory_name is within a different directory than
908          * info->last_directory, we have all entries for info->last_directory
909          * in info->versions and we need to create a tree object for them.
910          */
911         dir_info = strmap_get(&opt->priv->paths, info->last_directory);
912         assert(dir_info);
913         offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util;
914         if (offset == info->versions.nr) {
915                 /*
916                  * Actually, we don't need to create a tree object in this
917                  * case.  Whenever all files within a directory disappear
918                  * during the merge (e.g. unmodified on one side and
919                  * deleted on the other, or files were renamed elsewhere),
920                  * then we get here and the directory itself needs to be
921                  * omitted from its parent tree as well.
922                  */
923                 dir_info->is_null = 1;
924         } else {
925                 /*
926                  * Write out the tree to the git object directory, and also
927                  * record the mode and oid in dir_info->result.
928                  */
929                 dir_info->is_null = 0;
930                 dir_info->result.mode = S_IFDIR;
931                 write_tree(&dir_info->result.oid, &info->versions, offset,
932                            opt->repo->hash_algo->rawsz);
933         }
934
935         /*
936          * We've now used several entries from info->versions and one entry
937          * from info->offsets, so we get rid of those values.
938          */
939         info->offsets.nr--;
940         info->versions.nr = offset;
941
942         /*
943          * Now we've taken care of the completed directory, but we need to
944          * prepare things since future entries will be in
945          * new_directory_name.  (In particular, process_entry() will be
946          * appending new entries to info->versions.)  So, we need to make
947          * sure new_directory_name is the last entry in info->offsets.
948          */
949         prev_dir = info->offsets.nr == 0 ? NULL :
950                    info->offsets.items[info->offsets.nr-1].string;
951         if (new_directory_name != prev_dir) {
952                 uintptr_t c = info->versions.nr;
953                 string_list_append(&info->offsets,
954                                    new_directory_name)->util = (void*)c;
955         }
956
957         /* And, of course, we need to update last_directory to match. */
958         info->last_directory = new_directory_name;
959         info->last_directory_len = strlen(info->last_directory);
960 }
961
962 /* Per entry merge function */
963 static void process_entry(struct merge_options *opt,
964                           const char *path,
965                           struct conflict_info *ci,
966                           struct directory_versions *dir_metadata)
967 {
968         VERIFY_CI(ci);
969         assert(ci->filemask >= 0 && ci->filemask <= 7);
970         /* ci->match_mask == 7 was handled in collect_merge_info_callback() */
971         assert(ci->match_mask == 0 || ci->match_mask == 3 ||
972                ci->match_mask == 5 || ci->match_mask == 6);
973
974         if (ci->dirmask) {
975                 record_entry_for_tree(dir_metadata, path, &ci->merged);
976                 if (ci->filemask == 0)
977                         /* nothing else to handle */
978                         return;
979                 assert(ci->df_conflict);
980         }
981
982         if (ci->df_conflict) {
983                 die("Not yet implemented.");
984         }
985
986         /*
987          * NOTE: Below there is a long switch-like if-elseif-elseif... block
988          *       which the code goes through even for the df_conflict cases
989          *       above.  Well, it will once we don't die-not-implemented above.
990          */
991         if (ci->match_mask) {
992                 ci->merged.clean = 1;
993                 if (ci->match_mask == 6) {
994                         /* stages[1] == stages[2] */
995                         ci->merged.result.mode = ci->stages[1].mode;
996                         oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
997                 } else {
998                         /* determine the mask of the side that didn't match */
999                         unsigned int othermask = 7 & ~ci->match_mask;
1000                         int side = (othermask == 4) ? 2 : 1;
1001
1002                         ci->merged.result.mode = ci->stages[side].mode;
1003                         ci->merged.is_null = !ci->merged.result.mode;
1004                         oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1005
1006                         assert(othermask == 2 || othermask == 4);
1007                         assert(ci->merged.is_null ==
1008                                (ci->filemask == ci->match_mask));
1009                 }
1010         } else if (ci->filemask >= 6 &&
1011                    (S_IFMT & ci->stages[1].mode) !=
1012                    (S_IFMT & ci->stages[2].mode)) {
1013                 /*
1014                  * Two different items from (file/submodule/symlink)
1015                  */
1016                 die("Not yet implemented.");
1017         } else if (ci->filemask >= 6) {
1018                 /*
1019                  * TODO: Needs a two-way or three-way content merge, but we're
1020                  * just being lazy and copying the version from HEAD and
1021                  * leaving it as conflicted.
1022                  */
1023                 ci->merged.clean = 0;
1024                 ci->merged.result.mode = ci->stages[1].mode;
1025                 oidcpy(&ci->merged.result.oid, &ci->stages[1].oid);
1026                 /* When we fix above, we'll call handle_content_merge() */
1027                 (void)handle_content_merge;
1028         } else if (ci->filemask == 3 || ci->filemask == 5) {
1029                 /* Modify/delete */
1030                 const char *modify_branch, *delete_branch;
1031                 int side = (ci->filemask == 5) ? 2 : 1;
1032                 int index = opt->priv->call_depth ? 0 : side;
1033
1034                 ci->merged.result.mode = ci->stages[index].mode;
1035                 oidcpy(&ci->merged.result.oid, &ci->stages[index].oid);
1036                 ci->merged.clean = 0;
1037
1038                 modify_branch = (side == 1) ? opt->branch1 : opt->branch2;
1039                 delete_branch = (side == 1) ? opt->branch2 : opt->branch1;
1040
1041                 path_msg(opt, path, 0,
1042                          _("CONFLICT (modify/delete): %s deleted in %s "
1043                            "and modified in %s.  Version %s of %s left "
1044                            "in tree."),
1045                          path, delete_branch, modify_branch,
1046                          modify_branch, path);
1047         } else if (ci->filemask == 2 || ci->filemask == 4) {
1048                 /* Added on one side */
1049                 int side = (ci->filemask == 4) ? 2 : 1;
1050                 ci->merged.result.mode = ci->stages[side].mode;
1051                 oidcpy(&ci->merged.result.oid, &ci->stages[side].oid);
1052                 ci->merged.clean = !ci->df_conflict;
1053         } else if (ci->filemask == 1) {
1054                 /* Deleted on both sides */
1055                 ci->merged.is_null = 1;
1056                 ci->merged.result.mode = 0;
1057                 oidcpy(&ci->merged.result.oid, &null_oid);
1058                 ci->merged.clean = 1;
1059         }
1060
1061         /*
1062          * If still conflicted, record it separately.  This allows us to later
1063          * iterate over just conflicted entries when updating the index instead
1064          * of iterating over all entries.
1065          */
1066         if (!ci->merged.clean)
1067                 strmap_put(&opt->priv->conflicted, path, ci);
1068         record_entry_for_tree(dir_metadata, path, &ci->merged);
1069 }
1070
1071 static void process_entries(struct merge_options *opt,
1072                             struct object_id *result_oid)
1073 {
1074         struct hashmap_iter iter;
1075         struct strmap_entry *e;
1076         struct string_list plist = STRING_LIST_INIT_NODUP;
1077         struct string_list_item *entry;
1078         struct directory_versions dir_metadata = { STRING_LIST_INIT_NODUP,
1079                                                    STRING_LIST_INIT_NODUP,
1080                                                    NULL, 0 };
1081
1082         if (strmap_empty(&opt->priv->paths)) {
1083                 oidcpy(result_oid, opt->repo->hash_algo->empty_tree);
1084                 return;
1085         }
1086
1087         /* Hack to pre-allocate plist to the desired size */
1088         ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc);
1089
1090         /* Put every entry from paths into plist, then sort */
1091         strmap_for_each_entry(&opt->priv->paths, &iter, e) {
1092                 string_list_append(&plist, e->key)->util = e->value;
1093         }
1094         plist.cmp = string_list_df_name_compare;
1095         string_list_sort(&plist);
1096
1097         /*
1098          * Iterate over the items in reverse order, so we can handle paths
1099          * below a directory before needing to handle the directory itself.
1100          *
1101          * This allows us to write subtrees before we need to write trees,
1102          * and it also enables sane handling of directory/file conflicts
1103          * (because it allows us to know whether the directory is still in
1104          * the way when it is time to process the file at the same path).
1105          */
1106         for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) {
1107                 char *path = entry->string;
1108                 /*
1109                  * NOTE: mi may actually be a pointer to a conflict_info, but
1110                  * we have to check mi->clean first to see if it's safe to
1111                  * reassign to such a pointer type.
1112                  */
1113                 struct merged_info *mi = entry->util;
1114
1115                 write_completed_directory(opt, mi->directory_name,
1116                                           &dir_metadata);
1117                 if (mi->clean)
1118                         record_entry_for_tree(&dir_metadata, path, mi);
1119                 else {
1120                         struct conflict_info *ci = (struct conflict_info *)mi;
1121                         process_entry(opt, path, ci, &dir_metadata);
1122                 }
1123         }
1124
1125         if (dir_metadata.offsets.nr != 1 ||
1126             (uintptr_t)dir_metadata.offsets.items[0].util != 0) {
1127                 printf("dir_metadata.offsets.nr = %d (should be 1)\n",
1128                        dir_metadata.offsets.nr);
1129                 printf("dir_metadata.offsets.items[0].util = %u (should be 0)\n",
1130                        (unsigned)(uintptr_t)dir_metadata.offsets.items[0].util);
1131                 fflush(stdout);
1132                 BUG("dir_metadata accounting completely off; shouldn't happen");
1133         }
1134         write_tree(result_oid, &dir_metadata.versions, 0,
1135                    opt->repo->hash_algo->rawsz);
1136         string_list_clear(&plist, 0);
1137         string_list_clear(&dir_metadata.versions, 0);
1138         string_list_clear(&dir_metadata.offsets, 0);
1139 }
1140
1141 /*** Function Grouping: functions related to merge_switch_to_result() ***/
1142
1143 static int checkout(struct merge_options *opt,
1144                     struct tree *prev,
1145                     struct tree *next)
1146 {
1147         /* Switch the index/working copy from old to new */
1148         int ret;
1149         struct tree_desc trees[2];
1150         struct unpack_trees_options unpack_opts;
1151
1152         memset(&unpack_opts, 0, sizeof(unpack_opts));
1153         unpack_opts.head_idx = -1;
1154         unpack_opts.src_index = opt->repo->index;
1155         unpack_opts.dst_index = opt->repo->index;
1156
1157         setup_unpack_trees_porcelain(&unpack_opts, "merge");
1158
1159         /*
1160          * NOTE: if this were just "git checkout" code, we would probably
1161          * read or refresh the cache and check for a conflicted index, but
1162          * builtin/merge.c or sequencer.c really needs to read the index
1163          * and check for conflicted entries before starting merging for a
1164          * good user experience (no sense waiting for merges/rebases before
1165          * erroring out), so there's no reason to duplicate that work here.
1166          */
1167
1168         /* 2-way merge to the new branch */
1169         unpack_opts.update = 1;
1170         unpack_opts.merge = 1;
1171         unpack_opts.quiet = 0; /* FIXME: sequencer might want quiet? */
1172         unpack_opts.verbose_update = (opt->verbosity > 2);
1173         unpack_opts.fn = twoway_merge;
1174         if (1/* FIXME: opts->overwrite_ignore*/) {
1175                 unpack_opts.dir = xcalloc(1, sizeof(*unpack_opts.dir));
1176                 unpack_opts.dir->flags |= DIR_SHOW_IGNORED;
1177                 setup_standard_excludes(unpack_opts.dir);
1178         }
1179         parse_tree(prev);
1180         init_tree_desc(&trees[0], prev->buffer, prev->size);
1181         parse_tree(next);
1182         init_tree_desc(&trees[1], next->buffer, next->size);
1183
1184         ret = unpack_trees(2, trees, &unpack_opts);
1185         clear_unpack_trees_porcelain(&unpack_opts);
1186         dir_clear(unpack_opts.dir);
1187         FREE_AND_NULL(unpack_opts.dir);
1188         return ret;
1189 }
1190
1191 static int record_conflicted_index_entries(struct merge_options *opt,
1192                                            struct index_state *index,
1193                                            struct strmap *paths,
1194                                            struct strmap *conflicted)
1195 {
1196         struct hashmap_iter iter;
1197         struct strmap_entry *e;
1198         int errs = 0;
1199         int original_cache_nr;
1200
1201         if (strmap_empty(conflicted))
1202                 return 0;
1203
1204         original_cache_nr = index->cache_nr;
1205
1206         /* Put every entry from paths into plist, then sort */
1207         strmap_for_each_entry(conflicted, &iter, e) {
1208                 const char *path = e->key;
1209                 struct conflict_info *ci = e->value;
1210                 int pos;
1211                 struct cache_entry *ce;
1212                 int i;
1213
1214                 VERIFY_CI(ci);
1215
1216                 /*
1217                  * The index will already have a stage=0 entry for this path,
1218                  * because we created an as-merged-as-possible version of the
1219                  * file and checkout() moved the working copy and index over
1220                  * to that version.
1221                  *
1222                  * However, previous iterations through this loop will have
1223                  * added unstaged entries to the end of the cache which
1224                  * ignore the standard alphabetical ordering of cache
1225                  * entries and break invariants needed for index_name_pos()
1226                  * to work.  However, we know the entry we want is before
1227                  * those appended cache entries, so do a temporary swap on
1228                  * cache_nr to only look through entries of interest.
1229                  */
1230                 SWAP(index->cache_nr, original_cache_nr);
1231                 pos = index_name_pos(index, path, strlen(path));
1232                 SWAP(index->cache_nr, original_cache_nr);
1233                 if (pos < 0) {
1234                         if (ci->filemask != 1)
1235                                 BUG("Conflicted %s but nothing in basic working tree or index; this shouldn't happen", path);
1236                         cache_tree_invalidate_path(index, path);
1237                 } else {
1238                         ce = index->cache[pos];
1239
1240                         /*
1241                          * Clean paths with CE_SKIP_WORKTREE set will not be
1242                          * written to the working tree by the unpack_trees()
1243                          * call in checkout().  Our conflicted entries would
1244                          * have appeared clean to that code since we ignored
1245                          * the higher order stages.  Thus, we need override
1246                          * the CE_SKIP_WORKTREE bit and manually write those
1247                          * files to the working disk here.
1248                          *
1249                          * TODO: Implement this CE_SKIP_WORKTREE fixup.
1250                          */
1251
1252                         /*
1253                          * Mark this cache entry for removal and instead add
1254                          * new stage>0 entries corresponding to the
1255                          * conflicts.  If there are many conflicted entries, we
1256                          * want to avoid memmove'ing O(NM) entries by
1257                          * inserting the new entries one at a time.  So,
1258                          * instead, we just add the new cache entries to the
1259                          * end (ignoring normal index requirements on sort
1260                          * order) and sort the index once we're all done.
1261                          */
1262                         ce->ce_flags |= CE_REMOVE;
1263                 }
1264
1265                 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
1266                         struct version_info *vi;
1267                         if (!(ci->filemask & (1ul << i)))
1268                                 continue;
1269                         vi = &ci->stages[i];
1270                         ce = make_cache_entry(index, vi->mode, &vi->oid,
1271                                               path, i+1, 0);
1272                         add_index_entry(index, ce, ADD_CACHE_JUST_APPEND);
1273                 }
1274         }
1275
1276         /*
1277          * Remove the unused cache entries (and invalidate the relevant
1278          * cache-trees), then sort the index entries to get the conflicted
1279          * entries we added to the end into their right locations.
1280          */
1281         remove_marked_cache_entries(index, 1);
1282         QSORT(index->cache, index->cache_nr, cmp_cache_name_compare);
1283
1284         return errs;
1285 }
1286
1287 void merge_switch_to_result(struct merge_options *opt,
1288                             struct tree *head,
1289                             struct merge_result *result,
1290                             int update_worktree_and_index,
1291                             int display_update_msgs)
1292 {
1293         assert(opt->priv == NULL);
1294         if (result->clean >= 0 && update_worktree_and_index) {
1295                 struct merge_options_internal *opti = result->priv;
1296
1297                 if (checkout(opt, head, result->tree)) {
1298                         /* failure to function */
1299                         result->clean = -1;
1300                         return;
1301                 }
1302
1303                 if (record_conflicted_index_entries(opt, opt->repo->index,
1304                                                     &opti->paths,
1305                                                     &opti->conflicted)) {
1306                         /* failure to function */
1307                         result->clean = -1;
1308                         return;
1309                 }
1310         }
1311
1312         if (display_update_msgs) {
1313                 struct merge_options_internal *opti = result->priv;
1314                 struct hashmap_iter iter;
1315                 struct strmap_entry *e;
1316                 struct string_list olist = STRING_LIST_INIT_NODUP;
1317                 int i;
1318
1319                 /* Hack to pre-allocate olist to the desired size */
1320                 ALLOC_GROW(olist.items, strmap_get_size(&opti->output),
1321                            olist.alloc);
1322
1323                 /* Put every entry from output into olist, then sort */
1324                 strmap_for_each_entry(&opti->output, &iter, e) {
1325                         string_list_append(&olist, e->key)->util = e->value;
1326                 }
1327                 string_list_sort(&olist);
1328
1329                 /* Iterate over the items, printing them */
1330                 for (i = 0; i < olist.nr; ++i) {
1331                         struct strbuf *sb = olist.items[i].util;
1332
1333                         printf("%s", sb->buf);
1334                 }
1335                 string_list_clear(&olist, 0);
1336         }
1337
1338         merge_finalize(opt, result);
1339 }
1340
1341 void merge_finalize(struct merge_options *opt,
1342                     struct merge_result *result)
1343 {
1344         struct merge_options_internal *opti = result->priv;
1345
1346         assert(opt->priv == NULL);
1347
1348         clear_or_reinit_internal_opts(opti, 0);
1349         FREE_AND_NULL(opti);
1350 }
1351
1352 /*** Function Grouping: helper functions for merge_incore_*() ***/
1353
1354 static inline void set_commit_tree(struct commit *c, struct tree *t)
1355 {
1356         c->maybe_tree = t;
1357 }
1358
1359 static struct commit *make_virtual_commit(struct repository *repo,
1360                                           struct tree *tree,
1361                                           const char *comment)
1362 {
1363         struct commit *commit = alloc_commit_node(repo);
1364
1365         set_merge_remote_desc(commit, comment, (struct object *)commit);
1366         set_commit_tree(commit, tree);
1367         commit->object.parsed = 1;
1368         return commit;
1369 }
1370
1371 static void merge_start(struct merge_options *opt, struct merge_result *result)
1372 {
1373         /* Sanity checks on opt */
1374         assert(opt->repo);
1375
1376         assert(opt->branch1 && opt->branch2);
1377
1378         assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
1379                opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
1380         assert(opt->rename_limit >= -1);
1381         assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
1382         assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
1383
1384         assert(opt->xdl_opts >= 0);
1385         assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
1386                opt->recursive_variant <= MERGE_VARIANT_THEIRS);
1387
1388         /*
1389          * detect_renames, verbosity, buffer_output, and obuf are ignored
1390          * fields that were used by "recursive" rather than "ort" -- but
1391          * sanity check them anyway.
1392          */
1393         assert(opt->detect_renames >= -1 &&
1394                opt->detect_renames <= DIFF_DETECT_COPY);
1395         assert(opt->verbosity >= 0 && opt->verbosity <= 5);
1396         assert(opt->buffer_output <= 2);
1397         assert(opt->obuf.len == 0);
1398
1399         assert(opt->priv == NULL);
1400
1401         /* Default to histogram diff.  Actually, just hardcode it...for now. */
1402         opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
1403
1404         /* Initialization of opt->priv, our internal merge data */
1405         opt->priv = xcalloc(1, sizeof(*opt->priv));
1406
1407         /*
1408          * Although we initialize opt->priv->paths with strdup_strings=0,
1409          * that's just to avoid making yet another copy of an allocated
1410          * string.  Putting the entry into paths means we are taking
1411          * ownership, so we will later free it.  paths_to_free is similar.
1412          *
1413          * In contrast, conflicted just has a subset of keys from paths, so
1414          * we don't want to free those (it'd be a duplicate free).
1415          */
1416         strmap_init_with_options(&opt->priv->paths, NULL, 0);
1417         strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
1418         string_list_init(&opt->priv->paths_to_free, 0);
1419
1420         /*
1421          * keys & strbufs in output will sometimes need to outlive "paths",
1422          * so it will have a copy of relevant keys.  It's probably a small
1423          * subset of the overall paths that have special output.
1424          */
1425         strmap_init(&opt->priv->output);
1426 }
1427
1428 /*** Function Grouping: merge_incore_*() and their internal variants ***/
1429
1430 /*
1431  * Originally from merge_trees_internal(); heavily adapted, though.
1432  */
1433 static void merge_ort_nonrecursive_internal(struct merge_options *opt,
1434                                             struct tree *merge_base,
1435                                             struct tree *side1,
1436                                             struct tree *side2,
1437                                             struct merge_result *result)
1438 {
1439         struct object_id working_tree_oid;
1440
1441         if (collect_merge_info(opt, merge_base, side1, side2) != 0) {
1442                 /*
1443                  * TRANSLATORS: The %s arguments are: 1) tree hash of a merge
1444                  * base, and 2-3) the trees for the two trees we're merging.
1445                  */
1446                 err(opt, _("collecting merge info failed for trees %s, %s, %s"),
1447                     oid_to_hex(&merge_base->object.oid),
1448                     oid_to_hex(&side1->object.oid),
1449                     oid_to_hex(&side2->object.oid));
1450                 result->clean = -1;
1451                 return;
1452         }
1453
1454         result->clean = detect_and_process_renames(opt, merge_base,
1455                                                    side1, side2);
1456         process_entries(opt, &working_tree_oid);
1457
1458         /* Set return values */
1459         result->tree = parse_tree_indirect(&working_tree_oid);
1460         /* existence of conflicted entries implies unclean */
1461         result->clean &= strmap_empty(&opt->priv->conflicted);
1462         if (!opt->priv->call_depth) {
1463                 result->priv = opt->priv;
1464                 opt->priv = NULL;
1465         }
1466 }
1467
1468 /*
1469  * Originally from merge_recursive_internal(); somewhat adapted, though.
1470  */
1471 static void merge_ort_internal(struct merge_options *opt,
1472                                struct commit_list *merge_bases,
1473                                struct commit *h1,
1474                                struct commit *h2,
1475                                struct merge_result *result)
1476 {
1477         struct commit_list *iter;
1478         struct commit *merged_merge_bases;
1479         const char *ancestor_name;
1480         struct strbuf merge_base_abbrev = STRBUF_INIT;
1481
1482         if (!merge_bases) {
1483                 merge_bases = get_merge_bases(h1, h2);
1484                 /* See merge-ort.h:merge_incore_recursive() declaration NOTE */
1485                 merge_bases = reverse_commit_list(merge_bases);
1486         }
1487
1488         merged_merge_bases = pop_commit(&merge_bases);
1489         if (merged_merge_bases == NULL) {
1490                 /* if there is no common ancestor, use an empty tree */
1491                 struct tree *tree;
1492
1493                 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
1494                 merged_merge_bases = make_virtual_commit(opt->repo, tree,
1495                                                          "ancestor");
1496                 ancestor_name = "empty tree";
1497         } else if (merge_bases) {
1498                 ancestor_name = "merged common ancestors";
1499         } else {
1500                 strbuf_add_unique_abbrev(&merge_base_abbrev,
1501                                          &merged_merge_bases->object.oid,
1502                                          DEFAULT_ABBREV);
1503                 ancestor_name = merge_base_abbrev.buf;
1504         }
1505
1506         for (iter = merge_bases; iter; iter = iter->next) {
1507                 const char *saved_b1, *saved_b2;
1508                 struct commit *prev = merged_merge_bases;
1509
1510                 opt->priv->call_depth++;
1511                 /*
1512                  * When the merge fails, the result contains files
1513                  * with conflict markers. The cleanness flag is
1514                  * ignored (unless indicating an error), it was never
1515                  * actually used, as result of merge_trees has always
1516                  * overwritten it: the committed "conflicts" were
1517                  * already resolved.
1518                  */
1519                 saved_b1 = opt->branch1;
1520                 saved_b2 = opt->branch2;
1521                 opt->branch1 = "Temporary merge branch 1";
1522                 opt->branch2 = "Temporary merge branch 2";
1523                 merge_ort_internal(opt, NULL, prev, iter->item, result);
1524                 if (result->clean < 0)
1525                         return;
1526                 opt->branch1 = saved_b1;
1527                 opt->branch2 = saved_b2;
1528                 opt->priv->call_depth--;
1529
1530                 merged_merge_bases = make_virtual_commit(opt->repo,
1531                                                          result->tree,
1532                                                          "merged tree");
1533                 commit_list_insert(prev, &merged_merge_bases->parents);
1534                 commit_list_insert(iter->item,
1535                                    &merged_merge_bases->parents->next);
1536
1537                 clear_or_reinit_internal_opts(opt->priv, 1);
1538         }
1539
1540         opt->ancestor = ancestor_name;
1541         merge_ort_nonrecursive_internal(opt,
1542                                         repo_get_commit_tree(opt->repo,
1543                                                              merged_merge_bases),
1544                                         repo_get_commit_tree(opt->repo, h1),
1545                                         repo_get_commit_tree(opt->repo, h2),
1546                                         result);
1547         strbuf_release(&merge_base_abbrev);
1548         opt->ancestor = NULL;  /* avoid accidental re-use of opt->ancestor */
1549 }
1550
1551 void merge_incore_nonrecursive(struct merge_options *opt,
1552                                struct tree *merge_base,
1553                                struct tree *side1,
1554                                struct tree *side2,
1555                                struct merge_result *result)
1556 {
1557         assert(opt->ancestor != NULL);
1558         merge_start(opt, result);
1559         merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
1560 }
1561
1562 void merge_incore_recursive(struct merge_options *opt,
1563                             struct commit_list *merge_bases,
1564                             struct commit *side1,
1565                             struct commit *side2,
1566                             struct merge_result *result)
1567 {
1568         /* We set the ancestor label based on the merge_bases */
1569         assert(opt->ancestor == NULL);
1570
1571         merge_start(opt, result);
1572         merge_ort_internal(opt, merge_bases, side1, side2, result);
1573 }