2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
10 #include "cache-tree.h"
14 #include "tree-walk.h"
18 #include "unpack-trees.h"
19 #include "string-list.h"
20 #include "xdiff-interface.h"
23 #include "merge-recursive.h"
25 #include "submodule.h"
27 static void flush_output(struct merge_options *o)
29 if (o->buffer_output < 2 && o->obuf.len) {
30 fputs(o->obuf.buf, stdout);
31 strbuf_reset(&o->obuf);
35 static int err(struct merge_options *o, const char *err, ...)
39 if (o->buffer_output < 2)
42 strbuf_complete(&o->obuf, '\n');
43 strbuf_addstr(&o->obuf, "error: ");
45 va_start(params, err);
46 strbuf_vaddf(&o->obuf, err, params);
48 if (o->buffer_output > 1)
49 strbuf_addch(&o->obuf, '\n');
51 error("%s", o->obuf.buf);
52 strbuf_reset(&o->obuf);
58 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
59 const char *subtree_shift)
61 struct object_id shifted;
63 if (!*subtree_shift) {
64 shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
66 shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
69 if (!oidcmp(&two->object.oid, &shifted))
71 return lookup_tree(&shifted);
74 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
76 struct commit *commit = alloc_commit_node();
78 set_merge_remote_desc(commit, comment, (struct object *)commit);
80 commit->object.parsed = 1;
85 * Since we use get_tree_entry(), which does not put the read object into
86 * the object pool, we cannot rely on a == b.
88 static int oid_eq(const struct object_id *a, const struct object_id *b)
92 return a && b && oidcmp(a, b) == 0;
98 RENAME_ONE_FILE_TO_ONE,
99 RENAME_ONE_FILE_TO_TWO,
100 RENAME_TWO_FILES_TO_ONE
103 struct rename_conflict_info {
104 enum rename_type rename_type;
105 struct diff_filepair *pair1;
106 struct diff_filepair *pair2;
109 struct stage_data *dst_entry1;
110 struct stage_data *dst_entry2;
111 struct diff_filespec ren1_other;
112 struct diff_filespec ren2_other;
116 * Since we want to write the index eventually, we cannot reuse the index
117 * for these (temporary) data.
122 struct object_id oid;
124 struct rename_conflict_info *rename_conflict_info;
125 unsigned processed:1;
128 static inline void setup_rename_conflict_info(enum rename_type rename_type,
129 struct diff_filepair *pair1,
130 struct diff_filepair *pair2,
133 struct stage_data *dst_entry1,
134 struct stage_data *dst_entry2,
135 struct merge_options *o,
136 struct stage_data *src_entry1,
137 struct stage_data *src_entry2)
139 struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
140 ci->rename_type = rename_type;
142 ci->branch1 = branch1;
143 ci->branch2 = branch2;
145 ci->dst_entry1 = dst_entry1;
146 dst_entry1->rename_conflict_info = ci;
147 dst_entry1->processed = 0;
149 assert(!pair2 == !dst_entry2);
151 ci->dst_entry2 = dst_entry2;
153 dst_entry2->rename_conflict_info = ci;
156 if (rename_type == RENAME_TWO_FILES_TO_ONE) {
158 * For each rename, there could have been
159 * modifications on the side of history where that
160 * file was not renamed.
162 int ostage1 = o->branch1 == branch1 ? 3 : 2;
163 int ostage2 = ostage1 ^ 1;
165 ci->ren1_other.path = pair1->one->path;
166 oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
167 ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
169 ci->ren2_other.path = pair2->one->path;
170 oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
171 ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
175 static int show(struct merge_options *o, int v)
177 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
180 __attribute__((format (printf, 3, 4)))
181 static void output(struct merge_options *o, int v, const char *fmt, ...)
188 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
191 strbuf_vaddf(&o->obuf, fmt, ap);
194 strbuf_addch(&o->obuf, '\n');
195 if (!o->buffer_output)
199 static void output_commit_title(struct merge_options *o, struct commit *commit)
201 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
203 strbuf_addf(&o->obuf, "virtual %s\n",
204 merge_remote_util(commit)->name);
206 strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
208 strbuf_addch(&o->obuf, ' ');
209 if (parse_commit(commit) != 0)
210 strbuf_addstr(&o->obuf, _("(bad commit)\n"));
213 const char *msg = get_commit_buffer(commit, NULL);
214 int len = find_commit_subject(msg, &title);
216 strbuf_addf(&o->obuf, "%.*s\n", len, title);
217 unuse_commit_buffer(commit, msg);
223 static int add_cacheinfo(struct merge_options *o,
224 unsigned int mode, const struct object_id *oid,
225 const char *path, int stage, int refresh, int options)
227 struct cache_entry *ce;
230 ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
232 return err(o, _("addinfo_cache failed for path '%s'"), path);
234 ret = add_cache_entry(ce, options);
236 struct cache_entry *nce;
238 nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
240 return err(o, _("addinfo_cache failed for path '%s'"), path);
242 ret = add_cache_entry(nce, options);
247 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
250 init_tree_desc(desc, tree->buffer, tree->size);
253 static int git_merge_trees(int index_only,
259 struct tree_desc t[3];
260 struct unpack_trees_options opts;
262 memset(&opts, 0, sizeof(opts));
269 opts.fn = threeway_merge;
270 opts.src_index = &the_index;
271 opts.dst_index = &the_index;
272 setup_unpack_trees_porcelain(&opts, "merge");
274 init_tree_desc_from_tree(t+0, common);
275 init_tree_desc_from_tree(t+1, head);
276 init_tree_desc_from_tree(t+2, merge);
278 rc = unpack_trees(3, t, &opts);
279 cache_tree_free(&active_cache_tree);
283 struct tree *write_tree_from_memory(struct merge_options *o)
285 struct tree *result = NULL;
287 if (unmerged_cache()) {
289 fprintf(stderr, "BUG: There are unmerged index entries:\n");
290 for (i = 0; i < active_nr; i++) {
291 const struct cache_entry *ce = active_cache[i];
293 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
294 (int)ce_namelen(ce), ce->name);
296 die("BUG: unmerged index entries in merge-recursive.c");
299 if (!active_cache_tree)
300 active_cache_tree = cache_tree();
302 if (!cache_tree_fully_valid(active_cache_tree) &&
303 cache_tree_update(&the_index, 0) < 0) {
304 err(o, _("error building trees"));
308 result = lookup_tree(&active_cache_tree->oid);
313 static int save_files_dirs(const unsigned char *sha1,
314 struct strbuf *base, const char *path,
315 unsigned int mode, int stage, void *context)
317 int baselen = base->len;
318 struct merge_options *o = context;
320 strbuf_addstr(base, path);
323 string_list_insert(&o->current_directory_set, base->buf);
325 string_list_insert(&o->current_file_set, base->buf);
327 strbuf_setlen(base, baselen);
328 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
331 static int get_files_dirs(struct merge_options *o, struct tree *tree)
334 struct pathspec match_all;
335 memset(&match_all, 0, sizeof(match_all));
336 if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
338 n = o->current_file_set.nr + o->current_directory_set.nr;
343 * Returns an index_entry instance which doesn't have to correspond to
344 * a real cache entry in Git's index.
346 static struct stage_data *insert_stage_data(const char *path,
347 struct tree *o, struct tree *a, struct tree *b,
348 struct string_list *entries)
350 struct string_list_item *item;
351 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
352 get_tree_entry(o->object.oid.hash, path,
353 e->stages[1].oid.hash, &e->stages[1].mode);
354 get_tree_entry(a->object.oid.hash, path,
355 e->stages[2].oid.hash, &e->stages[2].mode);
356 get_tree_entry(b->object.oid.hash, path,
357 e->stages[3].oid.hash, &e->stages[3].mode);
358 item = string_list_insert(entries, path);
364 * Create a dictionary mapping file names to stage_data objects. The
365 * dictionary contains one entry for every path with a non-zero stage entry.
367 static struct string_list *get_unmerged(void)
369 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
372 unmerged->strdup_strings = 1;
374 for (i = 0; i < active_nr; i++) {
375 struct string_list_item *item;
376 struct stage_data *e;
377 const struct cache_entry *ce = active_cache[i];
381 item = string_list_lookup(unmerged, ce->name);
383 item = string_list_insert(unmerged, ce->name);
384 item->util = xcalloc(1, sizeof(struct stage_data));
387 e->stages[ce_stage(ce)].mode = ce->ce_mode;
388 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
394 static int string_list_df_name_compare(const char *one, const char *two)
396 int onelen = strlen(one);
397 int twolen = strlen(two);
399 * Here we only care that entries for D/F conflicts are
400 * adjacent, in particular with the file of the D/F conflict
401 * appearing before files below the corresponding directory.
402 * The order of the rest of the list is irrelevant for us.
404 * To achieve this, we sort with df_name_compare and provide
405 * the mode S_IFDIR so that D/F conflicts will sort correctly.
406 * We use the mode S_IFDIR for everything else for simplicity,
407 * since in other cases any changes in their order due to
408 * sorting cause no problems for us.
410 int cmp = df_name_compare(one, onelen, S_IFDIR,
411 two, twolen, S_IFDIR);
413 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
414 * that 'foo' comes before 'foo/bar'.
418 return onelen - twolen;
421 static void record_df_conflict_files(struct merge_options *o,
422 struct string_list *entries)
424 /* If there is a D/F conflict and the file for such a conflict
425 * currently exist in the working tree, we want to allow it to be
426 * removed to make room for the corresponding directory if needed.
427 * The files underneath the directories of such D/F conflicts will
428 * be processed before the corresponding file involved in the D/F
429 * conflict. If the D/F directory ends up being removed by the
430 * merge, then we won't have to touch the D/F file. If the D/F
431 * directory needs to be written to the working copy, then the D/F
432 * file will simply be removed (in make_room_for_path()) to make
433 * room for the necessary paths. Note that if both the directory
434 * and the file need to be present, then the D/F file will be
435 * reinstated with a new unique name at the time it is processed.
437 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
438 const char *last_file = NULL;
443 * If we're merging merge-bases, we don't want to bother with
444 * any working directory changes.
449 /* Ensure D/F conflicts are adjacent in the entries list. */
450 for (i = 0; i < entries->nr; i++) {
451 struct string_list_item *next = &entries->items[i];
452 string_list_append(&df_sorted_entries, next->string)->util =
455 df_sorted_entries.cmp = string_list_df_name_compare;
456 string_list_sort(&df_sorted_entries);
458 string_list_clear(&o->df_conflict_file_set, 1);
459 for (i = 0; i < df_sorted_entries.nr; i++) {
460 const char *path = df_sorted_entries.items[i].string;
461 int len = strlen(path);
462 struct stage_data *e = df_sorted_entries.items[i].util;
465 * Check if last_file & path correspond to a D/F conflict;
466 * i.e. whether path is last_file+'/'+<something>.
467 * If so, record that it's okay to remove last_file to make
468 * room for path and friends if needed.
472 memcmp(path, last_file, last_len) == 0 &&
473 path[last_len] == '/') {
474 string_list_insert(&o->df_conflict_file_set, last_file);
478 * Determine whether path could exist as a file in the
479 * working directory as a possible D/F conflict. This
480 * will only occur when it exists in stage 2 as a
483 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
490 string_list_clear(&df_sorted_entries, 0);
494 struct diff_filepair *pair;
495 struct stage_data *src_entry;
496 struct stage_data *dst_entry;
497 unsigned processed:1;
501 * Get information of all renames which occurred between 'o_tree' and
502 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
503 * 'b_tree') to be able to associate the correct cache entries with
504 * the rename information. 'tree' is always equal to either a_tree or b_tree.
506 static struct string_list *get_renames(struct merge_options *o,
511 struct string_list *entries)
514 struct string_list *renames;
515 struct diff_options opts;
517 renames = xcalloc(1, sizeof(struct string_list));
518 if (!o->detect_rename)
522 DIFF_OPT_SET(&opts, RECURSIVE);
523 DIFF_OPT_CLR(&opts, RENAME_EMPTY);
524 opts.detect_rename = DIFF_DETECT_RENAME;
525 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
526 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
528 opts.rename_score = o->rename_score;
529 opts.show_rename_progress = o->show_rename_progress;
530 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
531 diff_setup_done(&opts);
532 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
534 if (opts.needed_rename_limit > o->needed_rename_limit)
535 o->needed_rename_limit = opts.needed_rename_limit;
536 for (i = 0; i < diff_queued_diff.nr; ++i) {
537 struct string_list_item *item;
539 struct diff_filepair *pair = diff_queued_diff.queue[i];
540 if (pair->status != 'R') {
541 diff_free_filepair(pair);
544 re = xmalloc(sizeof(*re));
547 item = string_list_lookup(entries, re->pair->one->path);
549 re->src_entry = insert_stage_data(re->pair->one->path,
550 o_tree, a_tree, b_tree, entries);
552 re->src_entry = item->util;
554 item = string_list_lookup(entries, re->pair->two->path);
556 re->dst_entry = insert_stage_data(re->pair->two->path,
557 o_tree, a_tree, b_tree, entries);
559 re->dst_entry = item->util;
560 item = string_list_insert(renames, pair->one->path);
563 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
564 diff_queued_diff.nr = 0;
569 static int update_stages(struct merge_options *opt, const char *path,
570 const struct diff_filespec *o,
571 const struct diff_filespec *a,
572 const struct diff_filespec *b)
576 * NOTE: It is usually a bad idea to call update_stages on a path
577 * before calling update_file on that same path, since it can
578 * sometimes lead to spurious "refusing to lose untracked file..."
579 * messages from update_file (via make_room_for path via
580 * would_lose_untracked). Instead, reverse the order of the calls
581 * (executing update_file first and then update_stages).
584 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
586 if (remove_file_from_cache(path))
589 if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
592 if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
595 if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
600 static void update_entry(struct stage_data *entry,
601 struct diff_filespec *o,
602 struct diff_filespec *a,
603 struct diff_filespec *b)
605 entry->processed = 0;
606 entry->stages[1].mode = o->mode;
607 entry->stages[2].mode = a->mode;
608 entry->stages[3].mode = b->mode;
609 oidcpy(&entry->stages[1].oid, &o->oid);
610 oidcpy(&entry->stages[2].oid, &a->oid);
611 oidcpy(&entry->stages[3].oid, &b->oid);
614 static int remove_file(struct merge_options *o, int clean,
615 const char *path, int no_wd)
617 int update_cache = o->call_depth || clean;
618 int update_working_directory = !o->call_depth && !no_wd;
621 if (remove_file_from_cache(path))
624 if (update_working_directory) {
626 struct cache_entry *ce;
627 ce = cache_file_exists(path, strlen(path), ignore_case);
628 if (ce && ce_stage(ce) == 0)
631 if (remove_path(path))
637 /* add a string to a strbuf, but converting "/" to "_" */
638 static void add_flattened_path(struct strbuf *out, const char *s)
641 strbuf_addstr(out, s);
642 for (; i < out->len; i++)
643 if (out->buf[i] == '/')
647 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
649 struct strbuf newpath = STRBUF_INIT;
653 strbuf_addf(&newpath, "%s~", path);
654 add_flattened_path(&newpath, branch);
656 base_len = newpath.len;
657 while (string_list_has_string(&o->current_file_set, newpath.buf) ||
658 string_list_has_string(&o->current_directory_set, newpath.buf) ||
659 (!o->call_depth && file_exists(newpath.buf))) {
660 strbuf_setlen(&newpath, base_len);
661 strbuf_addf(&newpath, "_%d", suffix++);
664 string_list_insert(&o->current_file_set, newpath.buf);
665 return strbuf_detach(&newpath, NULL);
669 * Check whether a directory in the index is in the way of an incoming
670 * file. Return 1 if so. If check_working_copy is non-zero, also
671 * check the working directory. If empty_ok is non-zero, also return
672 * 0 in the case where the working-tree dir exists but is empty.
674 static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
677 struct strbuf dirpath = STRBUF_INIT;
680 strbuf_addstr(&dirpath, path);
681 strbuf_addch(&dirpath, '/');
683 pos = cache_name_pos(dirpath.buf, dirpath.len);
687 if (pos < active_nr &&
688 !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
689 strbuf_release(&dirpath);
693 strbuf_release(&dirpath);
694 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
695 !(empty_ok && is_empty_dir(path));
698 static int was_tracked(const char *path)
700 int pos = cache_name_pos(path, strlen(path));
703 /* we have been tracking this path */
707 * Look for an unmerged entry for the path,
708 * specifically stage #2, which would indicate
709 * that "our" side before the merge started
710 * had the path tracked (and resulted in a conflict).
713 pos < active_nr && !strcmp(path, active_cache[pos]->name);
715 if (ce_stage(active_cache[pos]) == 2)
720 static int would_lose_untracked(const char *path)
722 return !was_tracked(path) && file_exists(path);
725 static int make_room_for_path(struct merge_options *o, const char *path)
728 const char *msg = _("failed to create path '%s'%s");
730 /* Unlink any D/F conflict files that are in the way */
731 for (i = 0; i < o->df_conflict_file_set.nr; i++) {
732 const char *df_path = o->df_conflict_file_set.items[i].string;
733 size_t pathlen = strlen(path);
734 size_t df_pathlen = strlen(df_path);
735 if (df_pathlen < pathlen &&
736 path[df_pathlen] == '/' &&
737 strncmp(path, df_path, df_pathlen) == 0) {
739 _("Removing %s to make room for subdirectory\n"),
742 unsorted_string_list_delete_item(&o->df_conflict_file_set,
748 /* Make sure leading directories are created */
749 status = safe_create_leading_directories_const(path);
751 if (status == SCLD_EXISTS)
752 /* something else exists */
753 return err(o, msg, path, _(": perhaps a D/F conflict?"));
754 return err(o, msg, path, "");
758 * Do not unlink a file in the work tree if we are not
761 if (would_lose_untracked(path))
762 return err(o, _("refusing to lose untracked file at '%s'"),
765 /* Successful unlink is good.. */
768 /* .. and so is no existing file */
771 /* .. but not some other error (who really cares what?) */
772 return err(o, msg, path, _(": perhaps a D/F conflict?"));
775 static int update_file_flags(struct merge_options *o,
776 const struct object_id *oid,
788 enum object_type type;
792 if (S_ISGITLINK(mode)) {
794 * We may later decide to recursively descend into
795 * the submodule directory and update its index
796 * and/or work tree, but we do not do that now.
802 buf = read_sha1_file(oid->hash, &type, &size);
804 return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
805 if (type != OBJ_BLOB) {
806 ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
810 struct strbuf strbuf = STRBUF_INIT;
811 if (convert_to_working_tree(path, buf, size, &strbuf)) {
814 buf = strbuf_detach(&strbuf, NULL);
818 if (make_room_for_path(o, path) < 0) {
822 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
828 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
830 ret = err(o, _("failed to open '%s': %s"),
831 path, strerror(errno));
834 write_in_full(fd, buf, size);
836 } else if (S_ISLNK(mode)) {
837 char *lnk = xmemdupz(buf, size);
838 safe_create_leading_directories_const(path);
840 if (symlink(lnk, path))
841 ret = err(o, _("failed to symlink '%s': %s"),
842 path, strerror(errno));
846 _("do not know what to do with %06o %s '%s'"),
847 mode, oid_to_hex(oid), path);
852 if (!ret && update_cache)
853 add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
857 static int update_file(struct merge_options *o,
859 const struct object_id *oid,
863 return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
866 /* Low level file merging, update and removal */
868 struct merge_file_info {
869 struct object_id oid;
875 static int merge_3way(struct merge_options *o,
876 mmbuffer_t *result_buf,
877 const struct diff_filespec *one,
878 const struct diff_filespec *a,
879 const struct diff_filespec *b,
883 mmfile_t orig, src1, src2;
884 struct ll_merge_options ll_opts = {0};
885 char *base_name, *name1, *name2;
888 ll_opts.renormalize = o->renormalize;
889 ll_opts.xdl_opts = o->xdl_opts;
892 ll_opts.virtual_ancestor = 1;
895 switch (o->recursive_variant) {
896 case MERGE_RECURSIVE_OURS:
897 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
899 case MERGE_RECURSIVE_THEIRS:
900 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
908 if (strcmp(a->path, b->path) ||
909 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
910 base_name = o->ancestor == NULL ? NULL :
911 mkpathdup("%s:%s", o->ancestor, one->path);
912 name1 = mkpathdup("%s:%s", branch1, a->path);
913 name2 = mkpathdup("%s:%s", branch2, b->path);
915 base_name = o->ancestor == NULL ? NULL :
916 mkpathdup("%s", o->ancestor);
917 name1 = mkpathdup("%s", branch1);
918 name2 = mkpathdup("%s", branch2);
921 read_mmblob(&orig, &one->oid);
922 read_mmblob(&src1, &a->oid);
923 read_mmblob(&src2, &b->oid);
925 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
926 &src1, name1, &src2, name2, &ll_opts);
937 static int merge_file_1(struct merge_options *o,
938 const struct diff_filespec *one,
939 const struct diff_filespec *a,
940 const struct diff_filespec *b,
943 struct merge_file_info *result)
948 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
950 if (S_ISREG(a->mode)) {
951 result->mode = a->mode;
952 oidcpy(&result->oid, &a->oid);
954 result->mode = b->mode;
955 oidcpy(&result->oid, &b->oid);
958 if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
964 if (a->mode == b->mode || a->mode == one->mode)
965 result->mode = b->mode;
967 result->mode = a->mode;
968 if (b->mode != one->mode) {
974 if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
975 oidcpy(&result->oid, &b->oid);
976 else if (oid_eq(&b->oid, &one->oid))
977 oidcpy(&result->oid, &a->oid);
978 else if (S_ISREG(a->mode)) {
979 mmbuffer_t result_buf;
980 int ret = 0, merge_status;
982 merge_status = merge_3way(o, &result_buf, one, a, b,
985 if ((merge_status < 0) || !result_buf.ptr)
986 ret = err(o, _("Failed to execute internal merge"));
988 if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
989 blob_type, result->oid.hash))
990 ret = err(o, _("Unable to add %s to database"),
993 free(result_buf.ptr);
996 result->clean = (merge_status == 0);
997 } else if (S_ISGITLINK(a->mode)) {
998 result->clean = merge_submodule(&result->oid,
1004 } else if (S_ISLNK(a->mode)) {
1005 switch (o->recursive_variant) {
1006 case MERGE_RECURSIVE_NORMAL:
1007 oidcpy(&result->oid, &a->oid);
1008 if (!oid_eq(&a->oid, &b->oid))
1011 case MERGE_RECURSIVE_OURS:
1012 oidcpy(&result->oid, &a->oid);
1014 case MERGE_RECURSIVE_THEIRS:
1015 oidcpy(&result->oid, &b->oid);
1019 die("BUG: unsupported object type in the tree");
1025 static int merge_file_special_markers(struct merge_options *o,
1026 const struct diff_filespec *one,
1027 const struct diff_filespec *a,
1028 const struct diff_filespec *b,
1029 const char *branch1,
1030 const char *filename1,
1031 const char *branch2,
1032 const char *filename2,
1033 struct merge_file_info *mfi)
1040 side1 = xstrfmt("%s:%s", branch1, filename1);
1042 side2 = xstrfmt("%s:%s", branch2, filename2);
1044 ret = merge_file_1(o, one, a, b,
1045 side1 ? side1 : branch1,
1046 side2 ? side2 : branch2, mfi);
1052 static int merge_file_one(struct merge_options *o,
1054 const struct object_id *o_oid, int o_mode,
1055 const struct object_id *a_oid, int a_mode,
1056 const struct object_id *b_oid, int b_mode,
1057 const char *branch1,
1058 const char *branch2,
1059 struct merge_file_info *mfi)
1061 struct diff_filespec one, a, b;
1063 one.path = a.path = b.path = (char *)path;
1064 oidcpy(&one.oid, o_oid);
1066 oidcpy(&a.oid, a_oid);
1068 oidcpy(&b.oid, b_oid);
1070 return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
1073 static int handle_change_delete(struct merge_options *o,
1074 const char *path, const char *old_path,
1075 const struct object_id *o_oid, int o_mode,
1076 const struct object_id *changed_oid,
1078 const char *change_branch,
1079 const char *delete_branch,
1080 const char *change, const char *change_past)
1082 char *alt_path = NULL;
1083 const char *update_path = path;
1086 if (dir_in_way(path, !o->call_depth, 0)) {
1087 update_path = alt_path = unique_path(o, path, change_branch);
1090 if (o->call_depth) {
1092 * We cannot arbitrarily accept either a_sha or b_sha as
1093 * correct; since there is no true "middle point" between
1094 * them, simply reuse the base version for virtual merge base.
1096 ret = remove_file_from_cache(path);
1098 ret = update_file(o, 0, o_oid, o_mode, update_path);
1102 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1103 "and %s in %s. Version %s of %s left in tree."),
1104 change, path, delete_branch, change_past,
1105 change_branch, change_branch, path);
1107 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1108 "and %s to %s in %s. Version %s of %s left in tree."),
1109 change, old_path, delete_branch, change_past, path,
1110 change_branch, change_branch, path);
1114 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1115 "and %s in %s. Version %s of %s left in tree at %s."),
1116 change, path, delete_branch, change_past,
1117 change_branch, change_branch, path, alt_path);
1119 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1120 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1121 change, old_path, delete_branch, change_past, path,
1122 change_branch, change_branch, path, alt_path);
1126 * No need to call update_file() on path when change_branch ==
1127 * o->branch1 && !alt_path, since that would needlessly touch
1128 * path. We could call update_file_flags() with update_cache=0
1129 * and update_wd=0, but that's a no-op.
1131 if (change_branch != o->branch1 || alt_path)
1132 ret = update_file(o, 0, changed_oid, changed_mode, update_path);
1139 static int conflict_rename_delete(struct merge_options *o,
1140 struct diff_filepair *pair,
1141 const char *rename_branch,
1142 const char *delete_branch)
1144 const struct diff_filespec *orig = pair->one;
1145 const struct diff_filespec *dest = pair->two;
1147 if (handle_change_delete(o,
1148 o->call_depth ? orig->path : dest->path,
1149 o->call_depth ? NULL : orig->path,
1150 &orig->oid, orig->mode,
1151 &dest->oid, dest->mode,
1152 rename_branch, delete_branch,
1153 _("rename"), _("renamed")))
1157 return remove_file_from_cache(dest->path);
1159 return update_stages(o, dest->path, NULL,
1160 rename_branch == o->branch1 ? dest : NULL,
1161 rename_branch == o->branch1 ? NULL : dest);
1164 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
1165 struct stage_data *entry,
1168 struct object_id *oid = &entry->stages[stage].oid;
1169 unsigned mode = entry->stages[stage].mode;
1170 if (mode == 0 || is_null_oid(oid))
1172 oidcpy(&target->oid, oid);
1173 target->mode = mode;
1177 static int handle_file(struct merge_options *o,
1178 struct diff_filespec *rename,
1180 struct rename_conflict_info *ci)
1182 char *dst_name = rename->path;
1183 struct stage_data *dst_entry;
1184 const char *cur_branch, *other_branch;
1185 struct diff_filespec other;
1186 struct diff_filespec *add;
1190 dst_entry = ci->dst_entry1;
1191 cur_branch = ci->branch1;
1192 other_branch = ci->branch2;
1194 dst_entry = ci->dst_entry2;
1195 cur_branch = ci->branch2;
1196 other_branch = ci->branch1;
1199 add = filespec_from_entry(&other, dst_entry, stage ^ 1);
1201 char *add_name = unique_path(o, rename->path, other_branch);
1202 if (update_file(o, 0, &add->oid, add->mode, add_name))
1205 remove_file(o, 0, rename->path, 0);
1206 dst_name = unique_path(o, rename->path, cur_branch);
1208 if (dir_in_way(rename->path, !o->call_depth, 0)) {
1209 dst_name = unique_path(o, rename->path, cur_branch);
1210 output(o, 1, _("%s is a directory in %s adding as %s instead"),
1211 rename->path, other_branch, dst_name);
1214 if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
1215 ; /* fall through, do allow dst_name to be released */
1216 else if (stage == 2)
1217 ret = update_stages(o, rename->path, NULL, rename, add);
1219 ret = update_stages(o, rename->path, NULL, add, rename);
1221 if (dst_name != rename->path)
1227 static int conflict_rename_rename_1to2(struct merge_options *o,
1228 struct rename_conflict_info *ci)
1230 /* One file was renamed in both branches, but to different names. */
1231 struct diff_filespec *one = ci->pair1->one;
1232 struct diff_filespec *a = ci->pair1->two;
1233 struct diff_filespec *b = ci->pair2->two;
1235 output(o, 1, _("CONFLICT (rename/rename): "
1236 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1237 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1238 one->path, a->path, ci->branch1,
1239 one->path, b->path, ci->branch2,
1240 o->call_depth ? _(" (left unresolved)") : "");
1241 if (o->call_depth) {
1242 struct merge_file_info mfi;
1243 struct diff_filespec other;
1244 struct diff_filespec *add;
1245 if (merge_file_one(o, one->path,
1246 &one->oid, one->mode,
1249 ci->branch1, ci->branch2, &mfi))
1253 * FIXME: For rename/add-source conflicts (if we could detect
1254 * such), this is wrong. We should instead find a unique
1255 * pathname and then either rename the add-source file to that
1256 * unique path, or use that unique path instead of src here.
1258 if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
1262 * Above, we put the merged content at the merge-base's
1263 * path. Now we usually need to delete both a->path and
1264 * b->path. However, the rename on each side of the merge
1265 * could also be involved in a rename/add conflict. In
1266 * such cases, we should keep the added file around,
1267 * resolving the conflict at that path in its favor.
1269 add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
1271 if (update_file(o, 0, &add->oid, add->mode, a->path))
1275 remove_file_from_cache(a->path);
1276 add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
1278 if (update_file(o, 0, &add->oid, add->mode, b->path))
1282 remove_file_from_cache(b->path);
1283 } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
1289 static int conflict_rename_rename_2to1(struct merge_options *o,
1290 struct rename_conflict_info *ci)
1292 /* Two files, a & b, were renamed to the same thing, c. */
1293 struct diff_filespec *a = ci->pair1->one;
1294 struct diff_filespec *b = ci->pair2->one;
1295 struct diff_filespec *c1 = ci->pair1->two;
1296 struct diff_filespec *c2 = ci->pair2->two;
1297 char *path = c1->path; /* == c2->path */
1298 struct merge_file_info mfi_c1;
1299 struct merge_file_info mfi_c2;
1302 output(o, 1, _("CONFLICT (rename/rename): "
1303 "Rename %s->%s in %s. "
1304 "Rename %s->%s in %s"),
1305 a->path, c1->path, ci->branch1,
1306 b->path, c2->path, ci->branch2);
1308 remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
1309 remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
1311 if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
1312 o->branch1, c1->path,
1313 o->branch2, ci->ren1_other.path, &mfi_c1) ||
1314 merge_file_special_markers(o, b, &ci->ren2_other, c2,
1315 o->branch1, ci->ren2_other.path,
1316 o->branch2, c2->path, &mfi_c2))
1319 if (o->call_depth) {
1321 * If mfi_c1.clean && mfi_c2.clean, then it might make
1322 * sense to do a two-way merge of those results. But, I
1323 * think in all cases, it makes sense to have the virtual
1324 * merge base just undo the renames; they can be detected
1325 * again later for the non-recursive merge.
1327 remove_file(o, 0, path, 0);
1328 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
1330 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1333 char *new_path1 = unique_path(o, path, ci->branch1);
1334 char *new_path2 = unique_path(o, path, ci->branch2);
1335 output(o, 1, _("Renaming %s to %s and %s to %s instead"),
1336 a->path, new_path1, b->path, new_path2);
1337 remove_file(o, 0, path, 0);
1338 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
1340 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1349 static int process_renames(struct merge_options *o,
1350 struct string_list *a_renames,
1351 struct string_list *b_renames)
1353 int clean_merge = 1, i, j;
1354 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
1355 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
1356 const struct rename *sre;
1358 for (i = 0; i < a_renames->nr; i++) {
1359 sre = a_renames->items[i].util;
1360 string_list_insert(&a_by_dst, sre->pair->two->path)->util
1363 for (i = 0; i < b_renames->nr; i++) {
1364 sre = b_renames->items[i].util;
1365 string_list_insert(&b_by_dst, sre->pair->two->path)->util
1369 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
1370 struct string_list *renames1, *renames2Dst;
1371 struct rename *ren1 = NULL, *ren2 = NULL;
1372 const char *branch1, *branch2;
1373 const char *ren1_src, *ren1_dst;
1374 struct string_list_item *lookup;
1376 if (i >= a_renames->nr) {
1377 ren2 = b_renames->items[j++].util;
1378 } else if (j >= b_renames->nr) {
1379 ren1 = a_renames->items[i++].util;
1381 int compare = strcmp(a_renames->items[i].string,
1382 b_renames->items[j].string);
1384 ren1 = a_renames->items[i++].util;
1386 ren2 = b_renames->items[j++].util;
1389 /* TODO: refactor, so that 1/2 are not needed */
1391 renames1 = a_renames;
1392 renames2Dst = &b_by_dst;
1393 branch1 = o->branch1;
1394 branch2 = o->branch2;
1396 renames1 = b_renames;
1397 renames2Dst = &a_by_dst;
1398 branch1 = o->branch2;
1399 branch2 = o->branch1;
1403 if (ren1->processed)
1405 ren1->processed = 1;
1406 ren1->dst_entry->processed = 1;
1407 /* BUG: We should only mark src_entry as processed if we
1408 * are not dealing with a rename + add-source case.
1410 ren1->src_entry->processed = 1;
1412 ren1_src = ren1->pair->one->path;
1413 ren1_dst = ren1->pair->two->path;
1416 /* One file renamed on both sides */
1417 const char *ren2_src = ren2->pair->one->path;
1418 const char *ren2_dst = ren2->pair->two->path;
1419 enum rename_type rename_type;
1420 if (strcmp(ren1_src, ren2_src) != 0)
1421 die("BUG: ren1_src != ren2_src");
1422 ren2->dst_entry->processed = 1;
1423 ren2->processed = 1;
1424 if (strcmp(ren1_dst, ren2_dst) != 0) {
1425 rename_type = RENAME_ONE_FILE_TO_TWO;
1428 rename_type = RENAME_ONE_FILE_TO_ONE;
1429 /* BUG: We should only remove ren1_src in
1430 * the base stage (think of rename +
1431 * add-source cases).
1433 remove_file(o, 1, ren1_src, 1);
1434 update_entry(ren1->dst_entry,
1439 setup_rename_conflict_info(rename_type,
1449 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
1450 /* Two different files renamed to the same thing */
1452 ren2 = lookup->util;
1453 ren2_dst = ren2->pair->two->path;
1454 if (strcmp(ren1_dst, ren2_dst) != 0)
1455 die("BUG: ren1_dst != ren2_dst");
1458 ren2->processed = 1;
1460 * BUG: We should only mark src_entry as processed
1461 * if we are not dealing with a rename + add-source
1464 ren2->src_entry->processed = 1;
1466 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
1478 /* Renamed in 1, maybe changed in 2 */
1479 /* we only use sha1 and mode of these */
1480 struct diff_filespec src_other, dst_other;
1484 * unpack_trees loads entries from common-commit
1485 * into stage 1, from head-commit into stage 2, and
1486 * from merge-commit into stage 3. We keep track
1487 * of which side corresponds to the rename.
1489 int renamed_stage = a_renames == renames1 ? 2 : 3;
1490 int other_stage = a_renames == renames1 ? 3 : 2;
1492 /* BUG: We should only remove ren1_src in the base
1493 * stage and in other_stage (think of rename +
1496 remove_file(o, 1, ren1_src,
1497 renamed_stage == 2 || !was_tracked(ren1_src));
1499 oidcpy(&src_other.oid,
1500 &ren1->src_entry->stages[other_stage].oid);
1501 src_other.mode = ren1->src_entry->stages[other_stage].mode;
1502 oidcpy(&dst_other.oid,
1503 &ren1->dst_entry->stages[other_stage].oid);
1504 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
1507 if (oid_eq(&src_other.oid, &null_oid)) {
1508 setup_rename_conflict_info(RENAME_DELETE,
1518 } else if ((dst_other.mode == ren1->pair->two->mode) &&
1519 oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
1521 * Added file on the other side identical to
1522 * the file being renamed: clean merge.
1523 * Also, there is no need to overwrite the
1524 * file already in the working copy, so call
1525 * update_file_flags() instead of
1528 if (update_file_flags(o,
1529 &ren1->pair->two->oid,
1530 ren1->pair->two->mode,
1532 1, /* update_cache */
1535 } else if (!oid_eq(&dst_other.oid, &null_oid)) {
1538 output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1540 ren1_src, ren1_dst, branch1,
1542 if (o->call_depth) {
1543 struct merge_file_info mfi;
1544 if (merge_file_one(o, ren1_dst, &null_oid, 0,
1545 &ren1->pair->two->oid,
1546 ren1->pair->two->mode,
1549 branch1, branch2, &mfi)) {
1551 goto cleanup_and_return;
1553 output(o, 1, _("Adding merged %s"), ren1_dst);
1554 if (update_file(o, 0, &mfi.oid,
1555 mfi.mode, ren1_dst))
1559 char *new_path = unique_path(o, ren1_dst, branch2);
1560 output(o, 1, _("Adding as %s instead"), new_path);
1561 if (update_file(o, 0, &dst_other.oid,
1562 dst_other.mode, new_path))
1569 if (clean_merge < 0)
1570 goto cleanup_and_return;
1572 struct diff_filespec *one, *a, *b;
1573 src_other.path = (char *)ren1_src;
1575 one = ren1->pair->one;
1576 if (a_renames == renames1) {
1577 a = ren1->pair->two;
1580 b = ren1->pair->two;
1583 update_entry(ren1->dst_entry, one, a, b);
1584 setup_rename_conflict_info(RENAME_NORMAL,
1598 string_list_clear(&a_by_dst, 0);
1599 string_list_clear(&b_by_dst, 0);
1604 static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
1606 return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
1609 static int read_oid_strbuf(struct merge_options *o,
1610 const struct object_id *oid, struct strbuf *dst)
1613 enum object_type type;
1615 buf = read_sha1_file(oid->hash, &type, &size);
1617 return err(o, _("cannot read object %s"), oid_to_hex(oid));
1618 if (type != OBJ_BLOB) {
1620 return err(o, _("object %s is not a blob"), oid_to_hex(oid));
1622 strbuf_attach(dst, buf, size, size + 1);
1626 static int blob_unchanged(struct merge_options *opt,
1627 const struct object_id *o_oid,
1629 const struct object_id *a_oid,
1631 int renormalize, const char *path)
1633 struct strbuf o = STRBUF_INIT;
1634 struct strbuf a = STRBUF_INIT;
1635 int ret = 0; /* assume changed for safety */
1637 if (a_mode != o_mode)
1639 if (oid_eq(o_oid, a_oid))
1644 assert(o_oid && a_oid);
1645 if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
1648 * Note: binary | is used so that both renormalizations are
1649 * performed. Comparison can be skipped if both files are
1650 * unchanged since their sha1s have already been compared.
1652 if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
1653 renormalize_buffer(&the_index, path, a.buf, a.len, &a))
1654 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1662 static int handle_modify_delete(struct merge_options *o,
1664 struct object_id *o_oid, int o_mode,
1665 struct object_id *a_oid, int a_mode,
1666 struct object_id *b_oid, int b_mode)
1668 const char *modify_branch, *delete_branch;
1669 struct object_id *changed_oid;
1673 modify_branch = o->branch1;
1674 delete_branch = o->branch2;
1675 changed_oid = a_oid;
1676 changed_mode = a_mode;
1678 modify_branch = o->branch2;
1679 delete_branch = o->branch1;
1680 changed_oid = b_oid;
1681 changed_mode = b_mode;
1684 return handle_change_delete(o,
1687 changed_oid, changed_mode,
1688 modify_branch, delete_branch,
1689 _("modify"), _("modified"));
1692 static int merge_content(struct merge_options *o,
1694 struct object_id *o_oid, int o_mode,
1695 struct object_id *a_oid, int a_mode,
1696 struct object_id *b_oid, int b_mode,
1697 struct rename_conflict_info *rename_conflict_info)
1699 const char *reason = _("content");
1700 const char *path1 = NULL, *path2 = NULL;
1701 struct merge_file_info mfi;
1702 struct diff_filespec one, a, b;
1703 unsigned df_conflict_remains = 0;
1706 reason = _("add/add");
1707 o_oid = (struct object_id *)&null_oid;
1709 one.path = a.path = b.path = (char *)path;
1710 oidcpy(&one.oid, o_oid);
1712 oidcpy(&a.oid, a_oid);
1714 oidcpy(&b.oid, b_oid);
1717 if (rename_conflict_info) {
1718 struct diff_filepair *pair1 = rename_conflict_info->pair1;
1720 path1 = (o->branch1 == rename_conflict_info->branch1) ?
1721 pair1->two->path : pair1->one->path;
1722 /* If rename_conflict_info->pair2 != NULL, we are in
1723 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1726 path2 = (rename_conflict_info->pair2 ||
1727 o->branch2 == rename_conflict_info->branch1) ?
1728 pair1->two->path : pair1->one->path;
1730 if (dir_in_way(path, !o->call_depth,
1731 S_ISGITLINK(pair1->two->mode)))
1732 df_conflict_remains = 1;
1734 if (merge_file_special_markers(o, &one, &a, &b,
1736 o->branch2, path2, &mfi))
1739 if (mfi.clean && !df_conflict_remains &&
1740 oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
1741 int path_renamed_outside_HEAD;
1742 output(o, 3, _("Skipped %s (merged same as existing)"), path);
1744 * The content merge resulted in the same file contents we
1745 * already had. We can return early if those file contents
1746 * are recorded at the correct path (which may not be true
1747 * if the merge involves a rename).
1749 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
1750 if (!path_renamed_outside_HEAD) {
1751 add_cacheinfo(o, mfi.mode, &mfi.oid, path,
1752 0, (!o->call_depth), 0);
1756 output(o, 2, _("Auto-merging %s"), path);
1759 if (S_ISGITLINK(mfi.mode))
1760 reason = _("submodule");
1761 output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
1763 if (rename_conflict_info && !df_conflict_remains)
1764 if (update_stages(o, path, &one, &a, &b))
1768 if (df_conflict_remains) {
1770 if (o->call_depth) {
1771 remove_file_from_cache(path);
1774 if (update_stages(o, path, &one, &a, &b))
1777 int file_from_stage2 = was_tracked(path);
1778 struct diff_filespec merged;
1779 oidcpy(&merged.oid, &mfi.oid);
1780 merged.mode = mfi.mode;
1782 if (update_stages(o, path, NULL,
1783 file_from_stage2 ? &merged : NULL,
1784 file_from_stage2 ? NULL : &merged))
1789 new_path = unique_path(o, path, rename_conflict_info->branch1);
1790 output(o, 1, _("Adding as %s instead"), new_path);
1791 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
1797 } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
1802 /* Per entry merge function */
1803 static int process_entry(struct merge_options *o,
1804 const char *path, struct stage_data *entry)
1806 int clean_merge = 1;
1807 int normalize = o->renormalize;
1808 unsigned o_mode = entry->stages[1].mode;
1809 unsigned a_mode = entry->stages[2].mode;
1810 unsigned b_mode = entry->stages[3].mode;
1811 struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
1812 struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
1813 struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
1815 entry->processed = 1;
1816 if (entry->rename_conflict_info) {
1817 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
1818 switch (conflict_info->rename_type) {
1820 case RENAME_ONE_FILE_TO_ONE:
1821 clean_merge = merge_content(o, path,
1822 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1827 if (conflict_rename_delete(o,
1828 conflict_info->pair1,
1829 conflict_info->branch1,
1830 conflict_info->branch2))
1833 case RENAME_ONE_FILE_TO_TWO:
1835 if (conflict_rename_rename_1to2(o, conflict_info))
1838 case RENAME_TWO_FILES_TO_ONE:
1840 if (conflict_rename_rename_2to1(o, conflict_info))
1844 entry->processed = 0;
1847 } else if (o_oid && (!a_oid || !b_oid)) {
1848 /* Case A: Deleted in one */
1849 if ((!a_oid && !b_oid) ||
1850 (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
1851 (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
1852 /* Deleted in both or deleted in one and
1853 * unchanged in the other */
1855 output(o, 2, _("Removing %s"), path);
1856 /* do not touch working file if it did not exist */
1857 remove_file(o, 1, path, !a_oid);
1859 /* Modify/delete; deleted side may have put a directory in the way */
1861 if (handle_modify_delete(o, path, o_oid, o_mode,
1862 a_oid, a_mode, b_oid, b_mode))
1865 } else if ((!o_oid && a_oid && !b_oid) ||
1866 (!o_oid && !a_oid && b_oid)) {
1867 /* Case B: Added in one. */
1868 /* [nothing|directory] -> ([nothing|directory], file) */
1870 const char *add_branch;
1871 const char *other_branch;
1873 const struct object_id *oid;
1877 add_branch = o->branch1;
1878 other_branch = o->branch2;
1881 conf = _("file/directory");
1883 add_branch = o->branch2;
1884 other_branch = o->branch1;
1887 conf = _("directory/file");
1889 if (dir_in_way(path, !o->call_depth,
1890 S_ISGITLINK(a_mode))) {
1891 char *new_path = unique_path(o, path, add_branch);
1893 output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1895 conf, path, other_branch, path, new_path);
1896 if (update_file(o, 0, oid, mode, new_path))
1898 else if (o->call_depth)
1899 remove_file_from_cache(path);
1902 output(o, 2, _("Adding %s"), path);
1903 /* do not overwrite file if already present */
1904 if (update_file_flags(o, oid, mode, path, 1, !a_oid))
1907 } else if (a_oid && b_oid) {
1908 /* Case C: Added in both (check for same permissions) and */
1909 /* case D: Modified in both, but differently. */
1910 clean_merge = merge_content(o, path,
1911 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1913 } else if (!o_oid && !a_oid && !b_oid) {
1915 * this entry was deleted altogether. a_mode == 0 means
1916 * we had that path and want to actively remove it.
1918 remove_file(o, 1, path, !a_mode);
1920 die("BUG: fatal merge failure, shouldn't happen.");
1925 int merge_trees(struct merge_options *o,
1928 struct tree *common,
1929 struct tree **result)
1933 if (o->subtree_shift) {
1934 merge = shift_tree_object(head, merge, o->subtree_shift);
1935 common = shift_tree_object(head, common, o->subtree_shift);
1938 if (oid_eq(&common->object.oid, &merge->object.oid)) {
1939 output(o, 0, _("Already up-to-date!"));
1944 code = git_merge_trees(o->call_depth, common, head, merge);
1947 if (show(o, 4) || o->call_depth)
1948 err(o, _("merging of trees %s and %s failed"),
1949 oid_to_hex(&head->object.oid),
1950 oid_to_hex(&merge->object.oid));
1954 if (unmerged_cache()) {
1955 struct string_list *entries, *re_head, *re_merge;
1957 string_list_clear(&o->current_file_set, 1);
1958 string_list_clear(&o->current_directory_set, 1);
1959 get_files_dirs(o, head);
1960 get_files_dirs(o, merge);
1962 entries = get_unmerged();
1963 record_df_conflict_files(o, entries);
1964 re_head = get_renames(o, head, common, head, merge, entries);
1965 re_merge = get_renames(o, merge, common, head, merge, entries);
1966 clean = process_renames(o, re_head, re_merge);
1969 for (i = entries->nr-1; 0 <= i; i--) {
1970 const char *path = entries->items[i].string;
1971 struct stage_data *e = entries->items[i].util;
1972 if (!e->processed) {
1973 int ret = process_entry(o, path, e);
1980 for (i = 0; i < entries->nr; i++) {
1981 struct stage_data *e = entries->items[i].util;
1983 die("BUG: unprocessed path??? %s",
1984 entries->items[i].string);
1987 string_list_clear(re_merge, 0);
1988 string_list_clear(re_head, 0);
1989 string_list_clear(entries, 1);
1998 if (o->call_depth && !(*result = write_tree_from_memory(o)))
2004 static struct commit_list *reverse_commit_list(struct commit_list *list)
2006 struct commit_list *next = NULL, *current, *backup;
2007 for (current = list; current; current = backup) {
2008 backup = current->next;
2009 current->next = next;
2016 * Merge the commits h1 and h2, return the resulting virtual
2017 * commit object and a flag indicating the cleanness of the merge.
2019 int merge_recursive(struct merge_options *o,
2022 struct commit_list *ca,
2023 struct commit **result)
2025 struct commit_list *iter;
2026 struct commit *merged_common_ancestors;
2027 struct tree *mrtree = mrtree;
2031 output(o, 4, _("Merging:"));
2032 output_commit_title(o, h1);
2033 output_commit_title(o, h2);
2037 ca = get_merge_bases(h1, h2);
2038 ca = reverse_commit_list(ca);
2042 unsigned cnt = commit_list_count(ca);
2044 output(o, 5, Q_("found %u common ancestor:",
2045 "found %u common ancestors:", cnt), cnt);
2046 for (iter = ca; iter; iter = iter->next)
2047 output_commit_title(o, iter->item);
2050 merged_common_ancestors = pop_commit(&ca);
2051 if (merged_common_ancestors == NULL) {
2052 /* if there is no common ancestor, use an empty tree */
2055 tree = lookup_tree(&empty_tree_oid);
2056 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
2059 for (iter = ca; iter; iter = iter->next) {
2060 const char *saved_b1, *saved_b2;
2063 * When the merge fails, the result contains files
2064 * with conflict markers. The cleanness flag is
2065 * ignored (unless indicating an error), it was never
2066 * actually used, as result of merge_trees has always
2067 * overwritten it: the committed "conflicts" were
2071 saved_b1 = o->branch1;
2072 saved_b2 = o->branch2;
2073 o->branch1 = "Temporary merge branch 1";
2074 o->branch2 = "Temporary merge branch 2";
2075 if (merge_recursive(o, merged_common_ancestors, iter->item,
2076 NULL, &merged_common_ancestors) < 0)
2078 o->branch1 = saved_b1;
2079 o->branch2 = saved_b2;
2082 if (!merged_common_ancestors)
2083 return err(o, _("merge returned no commit"));
2090 o->ancestor = "merged common ancestors";
2091 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
2098 if (o->call_depth) {
2099 *result = make_virtual_commit(mrtree, "merged tree");
2100 commit_list_insert(h1, &(*result)->parents);
2101 commit_list_insert(h2, &(*result)->parents->next);
2104 if (!o->call_depth && o->buffer_output < 2)
2105 strbuf_release(&o->obuf);
2107 diff_warn_rename_limit("merge.renamelimit",
2108 o->needed_rename_limit, 0);
2112 static struct commit *get_ref(const struct object_id *oid, const char *name)
2114 struct object *object;
2116 object = deref_tag(parse_object(oid), name, strlen(name));
2119 if (object->type == OBJ_TREE)
2120 return make_virtual_commit((struct tree*)object, name);
2121 if (object->type != OBJ_COMMIT)
2123 if (parse_commit((struct commit *)object))
2125 return (struct commit *)object;
2128 int merge_recursive_generic(struct merge_options *o,
2129 const struct object_id *head,
2130 const struct object_id *merge,
2132 const struct object_id **base_list,
2133 struct commit **result)
2136 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
2137 struct commit *head_commit = get_ref(head, o->branch1);
2138 struct commit *next_commit = get_ref(merge, o->branch2);
2139 struct commit_list *ca = NULL;
2143 for (i = 0; i < num_base_list; ++i) {
2144 struct commit *base;
2145 if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
2146 return err(o, _("Could not parse object '%s'"),
2147 oid_to_hex(base_list[i]));
2148 commit_list_insert(base, &ca);
2152 hold_locked_index(lock, LOCK_DIE_ON_ERROR);
2153 clean = merge_recursive(o, head_commit, next_commit, ca,
2158 if (active_cache_changed &&
2159 write_locked_index(&the_index, lock, COMMIT_LOCK))
2160 return err(o, _("Unable to write index."));
2162 return clean ? 0 : 1;
2165 static void merge_recursive_config(struct merge_options *o)
2167 git_config_get_int("merge.verbosity", &o->verbosity);
2168 git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
2169 git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
2170 git_config(git_xmerge_config, NULL);
2173 void init_merge_options(struct merge_options *o)
2175 memset(o, 0, sizeof(struct merge_options));
2177 o->buffer_output = 1;
2178 o->diff_rename_limit = -1;
2179 o->merge_rename_limit = -1;
2181 o->detect_rename = 1;
2182 merge_recursive_config(o);
2183 if (getenv("GIT_MERGE_VERBOSITY"))
2185 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
2186 if (o->verbosity >= 5)
2187 o->buffer_output = 0;
2188 strbuf_init(&o->obuf, 0);
2189 string_list_init(&o->current_file_set, 1);
2190 string_list_init(&o->current_directory_set, 1);
2191 string_list_init(&o->df_conflict_file_set, 1);
2194 int parse_merge_opt(struct merge_options *o, const char *s)
2200 if (!strcmp(s, "ours"))
2201 o->recursive_variant = MERGE_RECURSIVE_OURS;
2202 else if (!strcmp(s, "theirs"))
2203 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
2204 else if (!strcmp(s, "subtree"))
2205 o->subtree_shift = "";
2206 else if (skip_prefix(s, "subtree=", &arg))
2207 o->subtree_shift = arg;
2208 else if (!strcmp(s, "patience"))
2209 o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
2210 else if (!strcmp(s, "histogram"))
2211 o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
2212 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
2213 long value = parse_algorithm_value(arg);
2216 /* clear out previous settings */
2217 DIFF_XDL_CLR(o, NEED_MINIMAL);
2218 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
2219 o->xdl_opts |= value;
2221 else if (!strcmp(s, "ignore-space-change"))
2222 DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);
2223 else if (!strcmp(s, "ignore-all-space"))
2224 DIFF_XDL_SET(o, IGNORE_WHITESPACE);
2225 else if (!strcmp(s, "ignore-space-at-eol"))
2226 DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);
2227 else if (!strcmp(s, "renormalize"))
2229 else if (!strcmp(s, "no-renormalize"))
2231 else if (!strcmp(s, "no-renames"))
2232 o->detect_rename = 0;
2233 else if (!strcmp(s, "find-renames")) {
2234 o->detect_rename = 1;
2235 o->rename_score = 0;
2237 else if (skip_prefix(s, "find-renames=", &arg) ||
2238 skip_prefix(s, "rename-threshold=", &arg)) {
2239 if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
2241 o->detect_rename = 1;