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
6 * git merge [-s recursive]
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"?)
18 #include "merge-ort.h"
22 #include "cache-tree.h"
24 #include "commit-reach.h"
29 #include "object-store.h"
32 #include "submodule.h"
34 #include "unpack-trees.h"
35 #include "xdiff-interface.h"
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.
45 * See also 'filemask' and 'dirmask' in struct conflict_info; the "ith side"
46 * referred to there is one of these three sides.
54 struct merge_options_internal {
56 * paths: primary data structure in all of merge ort.
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.)
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).
90 * conflicted: a subset of keys->values from "paths"
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
100 struct strmap conflicted;
103 * paths_to_free: additional list of strings to free
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.
110 struct string_list paths_to_free;
113 * output: special messages and conflict notices for various paths
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.
119 struct strmap output;
122 * current_dir_name: temporary var used in collect_merge_info_callback()
124 * Used to set merged_info.directory_name; see documentation for that
125 * variable and the requirements placed on that field.
127 const char *current_dir_name;
129 /* call_depth: recursion level counter for merging merge bases */
133 struct version_info {
134 struct object_id oid;
139 /* if is_null, ignore result. otherwise result has oid & mode */
140 struct version_info result;
144 * clean: whether the path in question is cleanly merged.
146 * see conflict_info.merged for more details.
151 * basename_offset: offset of basename of path.
153 * perf optimization to avoid recomputing offset of final '/'
154 * character in pathname (0 if no '/' in pathname).
156 size_t basename_offset;
159 * directory_name: containing directory name.
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.
166 const char *directory_name;
169 struct conflict_info {
171 * merged: the version of the path that will be written to working tree
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.
183 struct merged_info merged;
185 /* oids & modes from each of the three trees for this path */
186 struct version_info stages[3];
188 /* pathnames for each stage; may differ due to rename detection */
189 const char *pathnames[3];
191 /* Whether this path is/was involved in a directory/file conflict */
192 unsigned df_conflict:1;
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).
199 unsigned path_conflict:1;
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.
208 * Note that these masks are related to enum merge_side, as the ith
209 * entry corresponds to side i.
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).
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.
224 unsigned match_mask:3;
227 /*** Function Grouping: various utility functions ***/
230 * For the next three macros, see warning for conflict_info.merged.
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*).
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.
240 #define INITIALIZE_CI(ci, mi) do { \
241 (ci) = (!(mi) || (mi)->clean) ? NULL : (struct conflict_info *)(mi); \
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); \
249 static void free_strmap_strings(struct strmap *map)
251 struct hashmap_iter iter;
252 struct strmap_entry *entry;
254 strmap_for_each_entry(map, &iter, entry) {
255 free((char*)entry->key);
259 static void clear_or_reinit_internal_opts(struct merge_options_internal *opti,
262 void (*strmap_func)(struct strmap *, int) =
263 reinitialize ? strmap_partial_clear : strmap_clear;
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.
272 free_strmap_strings(&opti->paths);
273 strmap_func(&opti->paths, 1);
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.
280 strmap_func(&opti->conflicted, 0);
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.
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;
294 struct hashmap_iter iter;
295 struct strmap_entry *e;
297 /* Release and free each strbuf found in output */
298 strmap_for_each_entry(&opti->output, &iter, e) {
299 struct strbuf *sb = e->value;
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.
311 strmap_clear(&opti->output, 0);
315 static int err(struct merge_options *opt, const char *err, ...)
318 struct strbuf sb = STRBUF_INIT;
320 strbuf_addstr(&sb, "error: ");
321 va_start(params, err);
322 strbuf_vaddf(&sb, err, params);
331 static void format_commit(struct strbuf *sb,
333 struct commit *commit)
335 struct merge_remote_desc *desc;
336 struct pretty_print_context ctx = {0};
337 ctx.abbrev = DEFAULT_ABBREV;
339 strbuf_addchars(sb, ' ', indent);
340 desc = merge_remote_util(commit);
342 strbuf_addf(sb, "virtual %s\n", desc->name);
346 format_commit_message(commit, "%h %s", sb, &ctx);
347 strbuf_addch(sb, '\n');
350 __attribute__((format (printf, 4, 5)))
351 static void path_msg(struct merge_options *opt,
353 int omittable_hint, /* skippable under --remerge-diff */
354 const char *fmt, ...)
357 struct strbuf *sb = strmap_get(&opt->priv->output, path);
359 sb = xmalloc(sizeof(*sb));
361 strmap_put(&opt->priv->output, path, sb);
365 strbuf_vaddf(sb, fmt, ap);
368 strbuf_addch(sb, '\n');
371 /* add a string to a strbuf, but converting "/" to "_" */
372 static void add_flattened_path(struct strbuf *out, const char *s)
375 strbuf_addstr(out, s);
376 for (; i < out->len; i++)
377 if (out->buf[i] == '/')
381 static char *unique_path(struct strmap *existing_paths,
385 struct strbuf newpath = STRBUF_INIT;
389 strbuf_addf(&newpath, "%s~", path);
390 add_flattened_path(&newpath, branch);
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++);
398 return strbuf_detach(&newpath, NULL);
401 /*** Function Grouping: functions related to collect_merge_info() ***/
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 */
414 int resolved /* boolean */)
416 /* result->util is void*, so mi is a convenience typed variable */
417 struct merged_info *mi;
419 assert(!is_null || resolved);
420 assert(!df_conflict || !resolved); /* df_conflict implies !resolved */
421 assert(resolved == (merged_version != NULL));
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;
429 mi->result.mode = merged_version->mode;
430 oidcpy(&mi->result.oid, &merged_version->oid);
431 mi->is_null = !!is_null;
434 struct conflict_info *ci;
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);
442 ci->filemask = filemask;
443 ci->dirmask = dirmask;
444 ci->df_conflict = !!df_conflict;
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().
457 strmap_put(&opt->priv->paths, fullpath, mi);
458 result->string = fullpath;
462 static int collect_merge_info_callback(int n,
464 unsigned long dirmask,
465 struct name_entry *names,
466 struct traverse_info *info)
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
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;
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));
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).
511 unsigned df_conflict = (filemask != 0) && (dirmask != 0);
513 /* n = 3 is a fundamental assumption. */
515 BUG("Called collect_merge_info_callback wrong");
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.
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);
528 /* Determine match_mask */
529 if (side1_matches_mbase)
530 match_mask = (side2_matches_mbase ? 7 : 3);
531 else if (side2_matches_mbase)
533 else if (sides_match)
537 * Get the name of the relevant filepath, which we'll pass to
538 * setup_path_info() for tracking.
543 len = traverse_path_len(info, p->pathlen);
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);
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
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);
563 * Record information about the path so we can resolve later in
566 setup_path_info(opt, &pi, dirname, info->pathlen, fullpath,
567 names, NULL, 0, df_conflict, filemask, dirmask, 0);
571 ci->match_mask = match_mask;
573 /* If dirmask, recurse into subdirectories */
575 struct traverse_info newinfo;
576 struct tree_desc t[3];
577 void *buf[3] = {NULL, NULL, NULL};
578 const char *original_dir_name;
581 ci->match_mask &= filemask;
584 newinfo.name = p->path;
585 newinfo.namelen = p->pathlen;
586 newinfo.pathlen = st_add3(newinfo.pathlen, p->pathlen, 1);
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).
597 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
598 if (i == 1 && side1_matches_mbase)
600 else if (i == 2 && side2_matches_mbase)
602 else if (i == 2 && sides_match)
605 const struct object_id *oid = NULL;
608 buf[i] = fill_tree_descriptor(opt->repo,
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;
619 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++)
629 static int collect_merge_info(struct merge_options *opt,
630 struct tree *merge_base,
635 struct tree_desc t[3];
636 struct traverse_info info;
637 const char *toplevel_dir_placeholder = "";
639 opt->priv->current_dir_name = toplevel_dir_placeholder;
640 setup_traverse_info(&info, toplevel_dir_placeholder);
641 info.fn = collect_merge_info_callback;
643 info.show_all_errors = 1;
645 parse_tree(merge_base);
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);
652 ret = traverse_trees(NULL, 3, t, &info);
657 /*** Function Grouping: functions related to threeway content merges ***/
659 static int find_first_merges(struct repository *repo,
663 struct object_array *result)
666 struct object_array merges = OBJECT_ARRAY_INIT;
667 struct commit *commit;
668 int contains_another;
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;
676 memset(result, 0, sizeof(struct object_array));
677 memset(&rev_opts, 0, sizeof(rev_opts));
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);
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);
696 reset_revision_walk();
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
702 for (i = 0; i < merges.nr; i++) {
703 struct commit *m1 = (struct commit *) merges.objects[i].item;
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;
714 if (!contains_another)
715 add_object_array(merges.objects[i].item, NULL, result);
718 object_array_clear(&merges);
722 static int merge_submodule(struct merge_options *opt,
724 const struct object_id *o,
725 const struct object_id *a,
726 const struct object_id *b,
727 struct object_id *result)
729 struct commit *commit_o, *commit_a, *commit_b;
731 struct object_array merges;
732 struct strbuf sb = STRBUF_INIT;
735 int search = !opt->priv->call_depth;
737 /* store fallback answer in result in case we fail */
738 oidcpy(result, opt->priv->call_depth ? o : a);
740 /* we can not handle deletion conflicts */
748 if (add_submodule_odb(path)) {
749 path_msg(opt, path, 0,
750 _("Failed to merge submodule %s (not checked out)"),
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)"),
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)"),
774 /* Case #1: a is contained in b or vice versa */
775 if (in_merge_bases(commit_a, commit_b)) {
777 path_msg(opt, path, 1,
778 _("Note: Fast-forwarding submodule %s to %s"),
779 path, oid_to_hex(b));
782 if (in_merge_bases(commit_b, commit_a)) {
784 path_msg(opt, path, 1,
785 _("Note: Fast-forwarding submodule %s to %s"),
786 path, oid_to_hex(a));
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.
797 /* Skip the search if makes no sense to the calling context. */
801 /* find commit which merges them */
802 parent_count = find_first_merges(opt->repo, path, commit_a, commit_b,
804 switch (parent_count) {
806 path_msg(opt, path, 0, _("Failed to merge submodule %s"), path);
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"),
816 path_msg(opt, path, 1,
817 _("If this is correct simply add it to the index "
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);
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);
835 object_array_clear(&merges);
839 static int merge_3way(struct merge_options *opt,
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)
848 mmfile_t orig, src1, src2;
849 struct ll_merge_options ll_opts = {0};
850 char *base, *name1, *name2;
853 ll_opts.renormalize = opt->renormalize;
854 ll_opts.extra_marker_size = extra_marker_size;
855 ll_opts.xdl_opts = opt->xdl_opts;
857 if (opt->priv->call_depth) {
858 ll_opts.virtual_ancestor = 1;
861 switch (opt->recursive_variant) {
862 case MERGE_VARIANT_OURS:
863 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
865 case MERGE_VARIANT_THEIRS:
866 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
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);
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]);
885 read_mmblob(&orig, o);
886 read_mmblob(&src1, a);
887 read_mmblob(&src2, b);
889 merge_status = ll_merge(result_buf, path, &orig, base,
890 &src1, name1, &src2, name2,
891 opt->repo->index, &ll_opts);
902 static int handle_content_merge(struct merge_options *opt,
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)
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.
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.
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.
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.
931 assert((S_IFMT & a->mode) == (S_IFMT & b->mode));
934 if (a->mode == b->mode || a->mode == o->mode)
935 result->mode = b->mode;
937 /* must be the 100644/100755 case */
938 assert(S_ISREG(a->mode));
939 result->mode = a->mode;
940 clean = (b->mode == o->mode);
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.
952 * Figure out if there's some kind of way we can work around
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
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);
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;
977 * If 'o' is different type, treat it as null so we do a
980 two_way = ((S_IFMT & o->mode) != (S_IFMT & a->mode));
982 merge_status = merge_3way(opt, path,
983 two_way ? &null_oid : &o->oid,
985 pathnames, extra_marker_size,
988 if ((merge_status < 0) || !result_buf.ptr)
989 ret = err(opt, _("Failed to execute internal merge"));
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"),
997 free(result_buf.ptr);
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);
1011 } else if (S_ISLNK(a->mode)) {
1012 if (opt->priv->call_depth) {
1014 result->mode = o->mode;
1015 oidcpy(&result->oid, &o->oid);
1017 switch (opt->recursive_variant) {
1018 case MERGE_VARIANT_NORMAL:
1020 oidcpy(&result->oid, &a->oid);
1022 case MERGE_VARIANT_OURS:
1023 oidcpy(&result->oid, &a->oid);
1025 case MERGE_VARIANT_THEIRS:
1026 oidcpy(&result->oid, &b->oid);
1031 BUG("unsupported object type in the tree: %06o for %s",
1037 /*** Function Grouping: functions related to detect_and_process_renames(), ***
1038 *** which are split into directory and regular rename detection sections. ***/
1040 /*** Function Grouping: functions related to directory rename detection ***/
1042 /*** Function Grouping: functions related to regular rename detection ***/
1044 static int detect_and_process_renames(struct merge_options *opt,
1045 struct tree *merge_base,
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.
1056 * TODO: Actually implement a real rename detection scheme.
1061 /*** Function Grouping: functions related to process_entries() ***/
1063 static int string_list_df_name_compare(const char *one, const char *two)
1065 int onelen = strlen(one);
1066 int twolen = strlen(two);
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.
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.
1079 int cmp = df_name_compare(one, onelen, S_IFDIR,
1080 two, twolen, S_IFDIR);
1082 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
1083 * that 'foo' comes before 'foo/bar'.
1087 return onelen - twolen;
1090 struct directory_versions {
1092 * versions: list of (basename -> version_info)
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.
1100 struct string_list versions;
1103 * offsets: list of (full relative path directories -> integer offsets)
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.
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/.
1115 struct string_list offsets;
1118 * last_directory: directory that previously processed file found in
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.
1127 const char *last_directory;
1129 /* last_directory_len: cached computation of strlen(last_directory) */
1130 unsigned last_directory_len;
1133 static int tree_entry_order(const void *a_, const void *b_)
1135 const struct string_list_item *a = a_;
1136 const struct string_list_item *b = b_;
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);
1144 static void write_tree(struct object_id *result_oid,
1145 struct string_list *versions,
1146 unsigned int offset,
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;
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.
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.
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);
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;
1172 strbuf_grow(&buf, maxlen);
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",
1180 versions->items[offset+i].string, '\0');
1181 strbuf_add(&buf, ri->oid.hash, hash_size);
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);
1189 static void record_entry_for_tree(struct directory_versions *dir_metadata,
1191 struct merged_info *mi)
1193 const char *basename;
1196 /* nothing to record */
1199 basename = path + mi->basename_offset;
1200 assert(strchr(basename, '/') == NULL);
1201 string_list_append(&dir_metadata->versions,
1202 basename)->util = &mi->result;
1205 static void write_completed_directory(struct merge_options *opt,
1206 const char *new_directory_name,
1207 struct directory_versions *info)
1209 const char *prev_dir;
1210 struct merged_info *dir_info = NULL;
1211 unsigned int offset;
1214 * Some explanation of info->versions and info->offsets...
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
1224 * src/moduleB/umm.c src/moduleB
1225 * src/moduleB/stuff.h src/moduleB
1226 * src/moduleB/baz.c src/moduleB
1228 * src/moduleA/foo.c src/moduleA
1229 * src/moduleA/bar.c src/moduleA
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:
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>
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
1251 * xtract.c <xtract.c's version_info>
1252 * token.txt <token.txt's version_info>
1253 * moduleB <src/moduleB's version_info>
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:
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
1276 * which says that moduleB (and only moduleB so far) is in the
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
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
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
1296 if (new_directory_name == info->last_directory)
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.
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;
1310 info->last_directory = new_directory_name;
1311 info->last_directory_len = strlen(info->last_directory);
1313 * Record the offset into info->versions where we will
1314 * start recording basenames of paths found within
1315 * new_directory_name.
1317 string_list_append(&info->offsets,
1318 info->last_directory)->util = (void*)offset;
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.
1329 dir_info = strmap_get(&opt->priv->paths, info->last_directory);
1331 offset = (uintptr_t)info->offsets.items[info->offsets.nr-1].util;
1332 if (offset == info->versions.nr) {
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.
1341 dir_info->is_null = 1;
1344 * Write out the tree to the git object directory, and also
1345 * record the mode and oid in dir_info->result.
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);
1354 * We've now used several entries from info->versions and one entry
1355 * from info->offsets, so we get rid of those values.
1358 info->versions.nr = offset;
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.
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;
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);
1380 /* Per entry merge function */
1381 static void process_entry(struct merge_options *opt,
1383 struct conflict_info *ci,
1384 struct directory_versions *dir_metadata)
1386 int df_file_index = 0;
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);
1395 record_entry_for_tree(dir_metadata, path, &ci->merged);
1396 if (ci->filemask == 0)
1397 /* nothing else to handle */
1399 assert(ci->df_conflict);
1402 if (ci->df_conflict && ci->merged.result.mode == 0) {
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.
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);
1417 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
1418 if (ci->filemask & (1 << i))
1420 ci->stages[i].mode = 0;
1421 oidcpy(&ci->stages[i].oid, &null_oid);
1423 } else if (ci->df_conflict && ci->merged.result.mode != 0) {
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.
1430 struct conflict_info *new_ci;
1432 const char *old_path = path;
1435 assert(ci->merged.result.mode == S_IFDIR);
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
1443 if (ci->filemask == 1) {
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.
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.
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))
1465 /* zero out any entries related to directories */
1466 new_ci->stages[i].mode = 0;
1467 oidcpy(&new_ci->stages[i].oid, &null_oid);
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.
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);
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);
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.
1494 * Now note that we're working on the new entry (path was
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
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);
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;
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);
1520 assert(othermask == 2 || othermask == 4);
1521 assert(ci->merged.is_null ==
1522 (ci->filemask == ci->match_mask));
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);
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;
1543 new_ci = xmalloc(sizeof(*new_ci));
1545 if (S_ISREG(a_mode))
1547 else if (S_ISREG(b_mode))
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."),
1559 (rename_a && rename_b) ? _("both") : _("one"));
1561 ci->merged.clean = 0;
1562 memcpy(new_ci, ci, sizeof(*new_ci));
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;
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);
1582 if ((S_IFMT & a_mode) != (S_IFMT & o_mode)) {
1583 ci->stages[0].mode = 0;
1584 oidcpy(&ci->stages[0].oid, &null_oid);
1588 /* Insert entries into opt->priv_paths */
1589 assert(rename_a || rename_b);
1591 a_path = unique_path(&opt->priv->paths,
1592 path, opt->branch1);
1593 strmap_put(&opt->priv->paths, a_path, ci);
1597 b_path = unique_path(&opt->priv->paths,
1598 path, opt->branch2);
1601 strmap_put(&opt->priv->paths, b_path, new_ci);
1603 if (rename_a && rename_b) {
1604 strmap_remove(&opt->priv->paths, path, 0);
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.
1611 string_list_append(&opt->priv->paths_to_free,
1616 * Do special handling for b_path since process_entry()
1617 * won't be called on it specially.
1619 strmap_put(&opt->priv->conflicted, b_path, new_ci);
1620 record_entry_for_tree(dir_metadata, b_path,
1624 * Remaining code for processing this entry should
1625 * think in terms of processing a_path.
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];
1638 clean_merge = handle_content_merge(opt, path, o, a, b,
1640 opt->priv->call_depth * 2,
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);
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"),
1663 } else if (ci->filemask == 3 || ci->filemask == 5) {
1665 const char *modify_branch, *delete_branch;
1666 int side = (ci->filemask == 5) ? 2 : 1;
1667 int index = opt->priv->call_depth ? 0 : side;
1669 ci->merged.result.mode = ci->stages[index].mode;
1670 oidcpy(&ci->merged.result.oid, &ci->stages[index].oid);
1671 ci->merged.clean = 0;
1673 modify_branch = (side == 1) ? opt->branch1 : opt->branch2;
1674 delete_branch = (side == 1) ? opt->branch2 : opt->branch1;
1676 path_msg(opt, path, 0,
1677 _("CONFLICT (modify/delete): %s deleted in %s "
1678 "and modified in %s. Version %s of %s left "
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;
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.
1701 if (!ci->merged.clean)
1702 strmap_put(&opt->priv->conflicted, path, ci);
1703 record_entry_for_tree(dir_metadata, path, &ci->merged);
1706 static void process_entries(struct merge_options *opt,
1707 struct object_id *result_oid)
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,
1717 if (strmap_empty(&opt->priv->paths)) {
1718 oidcpy(result_oid, opt->repo->hash_algo->empty_tree);
1722 /* Hack to pre-allocate plist to the desired size */
1723 ALLOC_GROW(plist.items, strmap_get_size(&opt->priv->paths), plist.alloc);
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;
1729 plist.cmp = string_list_df_name_compare;
1730 string_list_sort(&plist);
1733 * Iterate over the items in reverse order, so we can handle paths
1734 * below a directory before needing to handle the directory itself.
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).
1741 for (entry = &plist.items[plist.nr-1]; entry >= plist.items; --entry) {
1742 char *path = entry->string;
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.
1748 struct merged_info *mi = entry->util;
1750 write_completed_directory(opt, mi->directory_name,
1753 record_entry_for_tree(&dir_metadata, path, mi);
1755 struct conflict_info *ci = (struct conflict_info *)mi;
1756 process_entry(opt, path, ci, &dir_metadata);
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);
1767 BUG("dir_metadata accounting completely off; shouldn't happen");
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);
1776 /*** Function Grouping: functions related to merge_switch_to_result() ***/
1778 static int checkout(struct merge_options *opt,
1782 /* Switch the index/working copy from old to new */
1784 struct tree_desc trees[2];
1785 struct unpack_trees_options unpack_opts;
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;
1792 setup_unpack_trees_porcelain(&unpack_opts, "merge");
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.
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);
1815 init_tree_desc(&trees[0], prev->buffer, prev->size);
1817 init_tree_desc(&trees[1], next->buffer, next->size);
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);
1826 static int record_conflicted_index_entries(struct merge_options *opt,
1827 struct index_state *index,
1828 struct strmap *paths,
1829 struct strmap *conflicted)
1831 struct hashmap_iter iter;
1832 struct strmap_entry *e;
1834 int original_cache_nr;
1836 if (strmap_empty(conflicted))
1839 original_cache_nr = index->cache_nr;
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;
1846 struct cache_entry *ce;
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
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.
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);
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);
1873 ce = index->cache[pos];
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.
1884 * TODO: Implement this CE_SKIP_WORKTREE fixup.
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.
1897 ce->ce_flags |= CE_REMOVE;
1900 for (i = MERGE_BASE; i <= MERGE_SIDE2; i++) {
1901 struct version_info *vi;
1902 if (!(ci->filemask & (1ul << i)))
1904 vi = &ci->stages[i];
1905 ce = make_cache_entry(index, vi->mode, &vi->oid,
1907 add_index_entry(index, ce, ADD_CACHE_JUST_APPEND);
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.
1916 remove_marked_cache_entries(index, 1);
1917 QSORT(index->cache, index->cache_nr, cmp_cache_name_compare);
1922 void merge_switch_to_result(struct merge_options *opt,
1924 struct merge_result *result,
1925 int update_worktree_and_index,
1926 int display_update_msgs)
1928 assert(opt->priv == NULL);
1929 if (result->clean >= 0 && update_worktree_and_index) {
1930 struct merge_options_internal *opti = result->priv;
1932 if (checkout(opt, head, result->tree)) {
1933 /* failure to function */
1938 if (record_conflicted_index_entries(opt, opt->repo->index,
1940 &opti->conflicted)) {
1941 /* failure to function */
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;
1954 /* Hack to pre-allocate olist to the desired size */
1955 ALLOC_GROW(olist.items, strmap_get_size(&opti->output),
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;
1962 string_list_sort(&olist);
1964 /* Iterate over the items, printing them */
1965 for (i = 0; i < olist.nr; ++i) {
1966 struct strbuf *sb = olist.items[i].util;
1968 printf("%s", sb->buf);
1970 string_list_clear(&olist, 0);
1973 merge_finalize(opt, result);
1976 void merge_finalize(struct merge_options *opt,
1977 struct merge_result *result)
1979 struct merge_options_internal *opti = result->priv;
1981 assert(opt->priv == NULL);
1983 clear_or_reinit_internal_opts(opti, 0);
1984 FREE_AND_NULL(opti);
1987 /*** Function Grouping: helper functions for merge_incore_*() ***/
1989 static inline void set_commit_tree(struct commit *c, struct tree *t)
1994 static struct commit *make_virtual_commit(struct repository *repo,
1996 const char *comment)
1998 struct commit *commit = alloc_commit_node(repo);
2000 set_merge_remote_desc(commit, comment, (struct object *)commit);
2001 set_commit_tree(commit, tree);
2002 commit->object.parsed = 1;
2006 static void merge_start(struct merge_options *opt, struct merge_result *result)
2008 /* Sanity checks on opt */
2011 assert(opt->branch1 && opt->branch2);
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);
2019 assert(opt->xdl_opts >= 0);
2020 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
2021 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
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.
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);
2034 assert(opt->priv == NULL);
2036 /* Default to histogram diff. Actually, just hardcode it...for now. */
2037 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
2039 /* Initialization of opt->priv, our internal merge data */
2040 opt->priv = xcalloc(1, sizeof(*opt->priv));
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.
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).
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);
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.
2060 strmap_init(&opt->priv->output);
2063 /*** Function Grouping: merge_incore_*() and their internal variants ***/
2066 * Originally from merge_trees_internal(); heavily adapted, though.
2068 static void merge_ort_nonrecursive_internal(struct merge_options *opt,
2069 struct tree *merge_base,
2072 struct merge_result *result)
2074 struct object_id working_tree_oid;
2076 if (collect_merge_info(opt, merge_base, side1, side2) != 0) {
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.
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));
2089 result->clean = detect_and_process_renames(opt, merge_base,
2091 process_entries(opt, &working_tree_oid);
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;
2104 * Originally from merge_recursive_internal(); somewhat adapted, though.
2106 static void merge_ort_internal(struct merge_options *opt,
2107 struct commit_list *merge_bases,
2110 struct merge_result *result)
2112 struct commit_list *iter;
2113 struct commit *merged_merge_bases;
2114 const char *ancestor_name;
2115 struct strbuf merge_base_abbrev = STRBUF_INIT;
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);
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 */
2128 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
2129 merged_merge_bases = make_virtual_commit(opt->repo, tree,
2131 ancestor_name = "empty tree";
2132 } else if (merge_bases) {
2133 ancestor_name = "merged common ancestors";
2135 strbuf_add_unique_abbrev(&merge_base_abbrev,
2136 &merged_merge_bases->object.oid,
2138 ancestor_name = merge_base_abbrev.buf;
2141 for (iter = merge_bases; iter; iter = iter->next) {
2142 const char *saved_b1, *saved_b2;
2143 struct commit *prev = merged_merge_bases;
2145 opt->priv->call_depth++;
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
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)
2161 opt->branch1 = saved_b1;
2162 opt->branch2 = saved_b2;
2163 opt->priv->call_depth--;
2165 merged_merge_bases = make_virtual_commit(opt->repo,
2168 commit_list_insert(prev, &merged_merge_bases->parents);
2169 commit_list_insert(iter->item,
2170 &merged_merge_bases->parents->next);
2172 clear_or_reinit_internal_opts(opt->priv, 1);
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),
2182 strbuf_release(&merge_base_abbrev);
2183 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
2186 void merge_incore_nonrecursive(struct merge_options *opt,
2187 struct tree *merge_base,
2190 struct merge_result *result)
2192 assert(opt->ancestor != NULL);
2193 merge_start(opt, result);
2194 merge_ort_nonrecursive_internal(opt, merge_base, side1, side2, result);
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)
2203 /* We set the ancestor label based on the merge_bases */
2204 assert(opt->ancestor == NULL);
2206 merge_start(opt, result);
2207 merge_ort_internal(opt, merge_bases, side1, side2, result);