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 struct path_hashmap_entry {
28 struct hashmap_entry e;
29 char path[FLEX_ARRAY];
32 static int path_hashmap_cmp(const void *cmp_data,
34 const void *entry_or_key,
37 const struct path_hashmap_entry *a = entry;
38 const struct path_hashmap_entry *b = entry_or_key;
39 const char *key = keydata;
42 return strcasecmp(a->path, key ? key : b->path);
44 return strcmp(a->path, key ? key : b->path);
47 static unsigned int path_hash(const char *path)
49 return ignore_case ? strihash(path) : strhash(path);
52 static void flush_output(struct merge_options *o)
54 if (o->buffer_output < 2 && o->obuf.len) {
55 fputs(o->obuf.buf, stdout);
56 strbuf_reset(&o->obuf);
60 static int err(struct merge_options *o, const char *err, ...)
64 if (o->buffer_output < 2)
67 strbuf_complete(&o->obuf, '\n');
68 strbuf_addstr(&o->obuf, "error: ");
70 va_start(params, err);
71 strbuf_vaddf(&o->obuf, err, params);
73 if (o->buffer_output > 1)
74 strbuf_addch(&o->obuf, '\n');
76 error("%s", o->obuf.buf);
77 strbuf_reset(&o->obuf);
83 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
84 const char *subtree_shift)
86 struct object_id shifted;
88 if (!*subtree_shift) {
89 shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
91 shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
94 if (!oidcmp(&two->object.oid, &shifted))
96 return lookup_tree(&shifted);
99 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
101 struct commit *commit = alloc_commit_node();
103 set_merge_remote_desc(commit, comment, (struct object *)commit);
105 commit->object.parsed = 1;
110 * Since we use get_tree_entry(), which does not put the read object into
111 * the object pool, we cannot rely on a == b.
113 static int oid_eq(const struct object_id *a, const struct object_id *b)
117 return a && b && oidcmp(a, b) == 0;
123 RENAME_ONE_FILE_TO_ONE,
124 RENAME_ONE_FILE_TO_TWO,
125 RENAME_TWO_FILES_TO_ONE
128 struct rename_conflict_info {
129 enum rename_type rename_type;
130 struct diff_filepair *pair1;
131 struct diff_filepair *pair2;
134 struct stage_data *dst_entry1;
135 struct stage_data *dst_entry2;
136 struct diff_filespec ren1_other;
137 struct diff_filespec ren2_other;
141 * Since we want to write the index eventually, we cannot reuse the index
142 * for these (temporary) data.
147 struct object_id oid;
149 struct rename_conflict_info *rename_conflict_info;
150 unsigned processed:1;
153 static inline void setup_rename_conflict_info(enum rename_type rename_type,
154 struct diff_filepair *pair1,
155 struct diff_filepair *pair2,
158 struct stage_data *dst_entry1,
159 struct stage_data *dst_entry2,
160 struct merge_options *o,
161 struct stage_data *src_entry1,
162 struct stage_data *src_entry2)
164 struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
165 ci->rename_type = rename_type;
167 ci->branch1 = branch1;
168 ci->branch2 = branch2;
170 ci->dst_entry1 = dst_entry1;
171 dst_entry1->rename_conflict_info = ci;
172 dst_entry1->processed = 0;
174 assert(!pair2 == !dst_entry2);
176 ci->dst_entry2 = dst_entry2;
178 dst_entry2->rename_conflict_info = ci;
181 if (rename_type == RENAME_TWO_FILES_TO_ONE) {
183 * For each rename, there could have been
184 * modifications on the side of history where that
185 * file was not renamed.
187 int ostage1 = o->branch1 == branch1 ? 3 : 2;
188 int ostage2 = ostage1 ^ 1;
190 ci->ren1_other.path = pair1->one->path;
191 oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
192 ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
194 ci->ren2_other.path = pair2->one->path;
195 oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
196 ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
200 static int show(struct merge_options *o, int v)
202 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
205 __attribute__((format (printf, 3, 4)))
206 static void output(struct merge_options *o, int v, const char *fmt, ...)
213 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
216 strbuf_vaddf(&o->obuf, fmt, ap);
219 strbuf_addch(&o->obuf, '\n');
220 if (!o->buffer_output)
224 static void output_commit_title(struct merge_options *o, struct commit *commit)
226 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
228 strbuf_addf(&o->obuf, "virtual %s\n",
229 merge_remote_util(commit)->name);
231 strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
233 strbuf_addch(&o->obuf, ' ');
234 if (parse_commit(commit) != 0)
235 strbuf_addstr(&o->obuf, _("(bad commit)\n"));
238 const char *msg = get_commit_buffer(commit, NULL);
239 int len = find_commit_subject(msg, &title);
241 strbuf_addf(&o->obuf, "%.*s\n", len, title);
242 unuse_commit_buffer(commit, msg);
248 static int add_cacheinfo(struct merge_options *o,
249 unsigned int mode, const struct object_id *oid,
250 const char *path, int stage, int refresh, int options)
252 struct cache_entry *ce;
255 ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
257 return err(o, _("addinfo_cache failed for path '%s'"), path);
259 ret = add_cache_entry(ce, options);
261 struct cache_entry *nce;
263 nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
265 return err(o, _("addinfo_cache failed for path '%s'"), path);
267 ret = add_cache_entry(nce, options);
272 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
275 init_tree_desc(desc, tree->buffer, tree->size);
278 static int git_merge_trees(int index_only,
284 struct tree_desc t[3];
285 struct unpack_trees_options opts;
287 memset(&opts, 0, sizeof(opts));
294 opts.fn = threeway_merge;
295 opts.src_index = &the_index;
296 opts.dst_index = &the_index;
297 setup_unpack_trees_porcelain(&opts, "merge");
299 init_tree_desc_from_tree(t+0, common);
300 init_tree_desc_from_tree(t+1, head);
301 init_tree_desc_from_tree(t+2, merge);
303 rc = unpack_trees(3, t, &opts);
304 cache_tree_free(&active_cache_tree);
308 struct tree *write_tree_from_memory(struct merge_options *o)
310 struct tree *result = NULL;
312 if (unmerged_cache()) {
314 fprintf(stderr, "BUG: There are unmerged index entries:\n");
315 for (i = 0; i < active_nr; i++) {
316 const struct cache_entry *ce = active_cache[i];
318 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
319 (int)ce_namelen(ce), ce->name);
321 die("BUG: unmerged index entries in merge-recursive.c");
324 if (!active_cache_tree)
325 active_cache_tree = cache_tree();
327 if (!cache_tree_fully_valid(active_cache_tree) &&
328 cache_tree_update(&the_index, 0) < 0) {
329 err(o, _("error building trees"));
333 result = lookup_tree(&active_cache_tree->oid);
338 static int save_files_dirs(const unsigned char *sha1,
339 struct strbuf *base, const char *path,
340 unsigned int mode, int stage, void *context)
342 struct path_hashmap_entry *entry;
343 int baselen = base->len;
344 struct merge_options *o = context;
346 strbuf_addstr(base, path);
348 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
349 hashmap_entry_init(entry, path_hash(entry->path));
350 hashmap_add(&o->current_file_dir_set, entry);
352 strbuf_setlen(base, baselen);
353 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
356 static void get_files_dirs(struct merge_options *o, struct tree *tree)
358 struct pathspec match_all;
359 memset(&match_all, 0, sizeof(match_all));
360 read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o);
364 * Returns an index_entry instance which doesn't have to correspond to
365 * a real cache entry in Git's index.
367 static struct stage_data *insert_stage_data(const char *path,
368 struct tree *o, struct tree *a, struct tree *b,
369 struct string_list *entries)
371 struct string_list_item *item;
372 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
373 get_tree_entry(o->object.oid.hash, path,
374 e->stages[1].oid.hash, &e->stages[1].mode);
375 get_tree_entry(a->object.oid.hash, path,
376 e->stages[2].oid.hash, &e->stages[2].mode);
377 get_tree_entry(b->object.oid.hash, path,
378 e->stages[3].oid.hash, &e->stages[3].mode);
379 item = string_list_insert(entries, path);
385 * Create a dictionary mapping file names to stage_data objects. The
386 * dictionary contains one entry for every path with a non-zero stage entry.
388 static struct string_list *get_unmerged(void)
390 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
393 unmerged->strdup_strings = 1;
395 for (i = 0; i < active_nr; i++) {
396 struct string_list_item *item;
397 struct stage_data *e;
398 const struct cache_entry *ce = active_cache[i];
402 item = string_list_lookup(unmerged, ce->name);
404 item = string_list_insert(unmerged, ce->name);
405 item->util = xcalloc(1, sizeof(struct stage_data));
408 e->stages[ce_stage(ce)].mode = ce->ce_mode;
409 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
415 static int string_list_df_name_compare(const char *one, const char *two)
417 int onelen = strlen(one);
418 int twolen = strlen(two);
420 * Here we only care that entries for D/F conflicts are
421 * adjacent, in particular with the file of the D/F conflict
422 * appearing before files below the corresponding directory.
423 * The order of the rest of the list is irrelevant for us.
425 * To achieve this, we sort with df_name_compare and provide
426 * the mode S_IFDIR so that D/F conflicts will sort correctly.
427 * We use the mode S_IFDIR for everything else for simplicity,
428 * since in other cases any changes in their order due to
429 * sorting cause no problems for us.
431 int cmp = df_name_compare(one, onelen, S_IFDIR,
432 two, twolen, S_IFDIR);
434 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
435 * that 'foo' comes before 'foo/bar'.
439 return onelen - twolen;
442 static void record_df_conflict_files(struct merge_options *o,
443 struct string_list *entries)
445 /* If there is a D/F conflict and the file for such a conflict
446 * currently exist in the working tree, we want to allow it to be
447 * removed to make room for the corresponding directory if needed.
448 * The files underneath the directories of such D/F conflicts will
449 * be processed before the corresponding file involved in the D/F
450 * conflict. If the D/F directory ends up being removed by the
451 * merge, then we won't have to touch the D/F file. If the D/F
452 * directory needs to be written to the working copy, then the D/F
453 * file will simply be removed (in make_room_for_path()) to make
454 * room for the necessary paths. Note that if both the directory
455 * and the file need to be present, then the D/F file will be
456 * reinstated with a new unique name at the time it is processed.
458 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
459 const char *last_file = NULL;
464 * If we're merging merge-bases, we don't want to bother with
465 * any working directory changes.
470 /* Ensure D/F conflicts are adjacent in the entries list. */
471 for (i = 0; i < entries->nr; i++) {
472 struct string_list_item *next = &entries->items[i];
473 string_list_append(&df_sorted_entries, next->string)->util =
476 df_sorted_entries.cmp = string_list_df_name_compare;
477 string_list_sort(&df_sorted_entries);
479 string_list_clear(&o->df_conflict_file_set, 1);
480 for (i = 0; i < df_sorted_entries.nr; i++) {
481 const char *path = df_sorted_entries.items[i].string;
482 int len = strlen(path);
483 struct stage_data *e = df_sorted_entries.items[i].util;
486 * Check if last_file & path correspond to a D/F conflict;
487 * i.e. whether path is last_file+'/'+<something>.
488 * If so, record that it's okay to remove last_file to make
489 * room for path and friends if needed.
493 memcmp(path, last_file, last_len) == 0 &&
494 path[last_len] == '/') {
495 string_list_insert(&o->df_conflict_file_set, last_file);
499 * Determine whether path could exist as a file in the
500 * working directory as a possible D/F conflict. This
501 * will only occur when it exists in stage 2 as a
504 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
511 string_list_clear(&df_sorted_entries, 0);
515 struct diff_filepair *pair;
516 struct stage_data *src_entry;
517 struct stage_data *dst_entry;
518 unsigned processed:1;
522 * Get information of all renames which occurred between 'o_tree' and
523 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
524 * 'b_tree') to be able to associate the correct cache entries with
525 * the rename information. 'tree' is always equal to either a_tree or b_tree.
527 static struct string_list *get_renames(struct merge_options *o,
532 struct string_list *entries)
535 struct string_list *renames;
536 struct diff_options opts;
538 renames = xcalloc(1, sizeof(struct string_list));
539 if (!o->detect_rename)
543 opts.flags.recursive = 1;
544 opts.flags.rename_empty = 0;
545 opts.detect_rename = DIFF_DETECT_RENAME;
546 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
547 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
549 opts.rename_score = o->rename_score;
550 opts.show_rename_progress = o->show_rename_progress;
551 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
552 diff_setup_done(&opts);
553 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
555 if (opts.needed_rename_limit > o->needed_rename_limit)
556 o->needed_rename_limit = opts.needed_rename_limit;
557 for (i = 0; i < diff_queued_diff.nr; ++i) {
558 struct string_list_item *item;
560 struct diff_filepair *pair = diff_queued_diff.queue[i];
561 if (pair->status != 'R') {
562 diff_free_filepair(pair);
565 re = xmalloc(sizeof(*re));
568 item = string_list_lookup(entries, re->pair->one->path);
570 re->src_entry = insert_stage_data(re->pair->one->path,
571 o_tree, a_tree, b_tree, entries);
573 re->src_entry = item->util;
575 item = string_list_lookup(entries, re->pair->two->path);
577 re->dst_entry = insert_stage_data(re->pair->two->path,
578 o_tree, a_tree, b_tree, entries);
580 re->dst_entry = item->util;
581 item = string_list_insert(renames, pair->one->path);
584 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
585 diff_queued_diff.nr = 0;
590 static int update_stages(struct merge_options *opt, const char *path,
591 const struct diff_filespec *o,
592 const struct diff_filespec *a,
593 const struct diff_filespec *b)
597 * NOTE: It is usually a bad idea to call update_stages on a path
598 * before calling update_file on that same path, since it can
599 * sometimes lead to spurious "refusing to lose untracked file..."
600 * messages from update_file (via make_room_for path via
601 * would_lose_untracked). Instead, reverse the order of the calls
602 * (executing update_file first and then update_stages).
605 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
607 if (remove_file_from_cache(path))
610 if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
613 if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
616 if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
621 static void update_entry(struct stage_data *entry,
622 struct diff_filespec *o,
623 struct diff_filespec *a,
624 struct diff_filespec *b)
626 entry->processed = 0;
627 entry->stages[1].mode = o->mode;
628 entry->stages[2].mode = a->mode;
629 entry->stages[3].mode = b->mode;
630 oidcpy(&entry->stages[1].oid, &o->oid);
631 oidcpy(&entry->stages[2].oid, &a->oid);
632 oidcpy(&entry->stages[3].oid, &b->oid);
635 static int remove_file(struct merge_options *o, int clean,
636 const char *path, int no_wd)
638 int update_cache = o->call_depth || clean;
639 int update_working_directory = !o->call_depth && !no_wd;
642 if (remove_file_from_cache(path))
645 if (update_working_directory) {
647 struct cache_entry *ce;
648 ce = cache_file_exists(path, strlen(path), ignore_case);
649 if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
652 if (remove_path(path))
658 /* add a string to a strbuf, but converting "/" to "_" */
659 static void add_flattened_path(struct strbuf *out, const char *s)
662 strbuf_addstr(out, s);
663 for (; i < out->len; i++)
664 if (out->buf[i] == '/')
668 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
670 struct path_hashmap_entry *entry;
671 struct strbuf newpath = STRBUF_INIT;
675 strbuf_addf(&newpath, "%s~", path);
676 add_flattened_path(&newpath, branch);
678 base_len = newpath.len;
679 while (hashmap_get_from_hash(&o->current_file_dir_set,
680 path_hash(newpath.buf), newpath.buf) ||
681 (!o->call_depth && file_exists(newpath.buf))) {
682 strbuf_setlen(&newpath, base_len);
683 strbuf_addf(&newpath, "_%d", suffix++);
686 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
687 hashmap_entry_init(entry, path_hash(entry->path));
688 hashmap_add(&o->current_file_dir_set, entry);
689 return strbuf_detach(&newpath, NULL);
693 * Check whether a directory in the index is in the way of an incoming
694 * file. Return 1 if so. If check_working_copy is non-zero, also
695 * check the working directory. If empty_ok is non-zero, also return
696 * 0 in the case where the working-tree dir exists but is empty.
698 static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
701 struct strbuf dirpath = STRBUF_INIT;
704 strbuf_addstr(&dirpath, path);
705 strbuf_addch(&dirpath, '/');
707 pos = cache_name_pos(dirpath.buf, dirpath.len);
711 if (pos < active_nr &&
712 !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
713 strbuf_release(&dirpath);
717 strbuf_release(&dirpath);
718 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
719 !(empty_ok && is_empty_dir(path));
722 static int was_tracked(const char *path)
724 int pos = cache_name_pos(path, strlen(path));
727 /* we have been tracking this path */
731 * Look for an unmerged entry for the path,
732 * specifically stage #2, which would indicate
733 * that "our" side before the merge started
734 * had the path tracked (and resulted in a conflict).
737 pos < active_nr && !strcmp(path, active_cache[pos]->name);
739 if (ce_stage(active_cache[pos]) == 2)
744 static int would_lose_untracked(const char *path)
746 return !was_tracked(path) && file_exists(path);
749 static int make_room_for_path(struct merge_options *o, const char *path)
752 const char *msg = _("failed to create path '%s'%s");
754 /* Unlink any D/F conflict files that are in the way */
755 for (i = 0; i < o->df_conflict_file_set.nr; i++) {
756 const char *df_path = o->df_conflict_file_set.items[i].string;
757 size_t pathlen = strlen(path);
758 size_t df_pathlen = strlen(df_path);
759 if (df_pathlen < pathlen &&
760 path[df_pathlen] == '/' &&
761 strncmp(path, df_path, df_pathlen) == 0) {
763 _("Removing %s to make room for subdirectory\n"),
766 unsorted_string_list_delete_item(&o->df_conflict_file_set,
772 /* Make sure leading directories are created */
773 status = safe_create_leading_directories_const(path);
775 if (status == SCLD_EXISTS)
776 /* something else exists */
777 return err(o, msg, path, _(": perhaps a D/F conflict?"));
778 return err(o, msg, path, "");
782 * Do not unlink a file in the work tree if we are not
785 if (would_lose_untracked(path))
786 return err(o, _("refusing to lose untracked file at '%s'"),
789 /* Successful unlink is good.. */
792 /* .. and so is no existing file */
795 /* .. but not some other error (who really cares what?) */
796 return err(o, msg, path, _(": perhaps a D/F conflict?"));
799 static int update_file_flags(struct merge_options *o,
800 const struct object_id *oid,
812 enum object_type type;
816 if (S_ISGITLINK(mode)) {
818 * We may later decide to recursively descend into
819 * the submodule directory and update its index
820 * and/or work tree, but we do not do that now.
826 buf = read_sha1_file(oid->hash, &type, &size);
828 return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
829 if (type != OBJ_BLOB) {
830 ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
834 struct strbuf strbuf = STRBUF_INIT;
835 if (convert_to_working_tree(path, buf, size, &strbuf)) {
838 buf = strbuf_detach(&strbuf, NULL);
842 if (make_room_for_path(o, path) < 0) {
846 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
852 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
854 ret = err(o, _("failed to open '%s': %s"),
855 path, strerror(errno));
858 write_in_full(fd, buf, size);
860 } else if (S_ISLNK(mode)) {
861 char *lnk = xmemdupz(buf, size);
862 safe_create_leading_directories_const(path);
864 if (symlink(lnk, path))
865 ret = err(o, _("failed to symlink '%s': %s"),
866 path, strerror(errno));
870 _("do not know what to do with %06o %s '%s'"),
871 mode, oid_to_hex(oid), path);
876 if (!ret && update_cache)
877 add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
881 static int update_file(struct merge_options *o,
883 const struct object_id *oid,
887 return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
890 /* Low level file merging, update and removal */
892 struct merge_file_info {
893 struct object_id oid;
899 static int merge_3way(struct merge_options *o,
900 mmbuffer_t *result_buf,
901 const struct diff_filespec *one,
902 const struct diff_filespec *a,
903 const struct diff_filespec *b,
907 mmfile_t orig, src1, src2;
908 struct ll_merge_options ll_opts = {0};
909 char *base_name, *name1, *name2;
912 ll_opts.renormalize = o->renormalize;
913 ll_opts.xdl_opts = o->xdl_opts;
916 ll_opts.virtual_ancestor = 1;
919 switch (o->recursive_variant) {
920 case MERGE_RECURSIVE_OURS:
921 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
923 case MERGE_RECURSIVE_THEIRS:
924 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
932 if (strcmp(a->path, b->path) ||
933 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
934 base_name = o->ancestor == NULL ? NULL :
935 mkpathdup("%s:%s", o->ancestor, one->path);
936 name1 = mkpathdup("%s:%s", branch1, a->path);
937 name2 = mkpathdup("%s:%s", branch2, b->path);
939 base_name = o->ancestor == NULL ? NULL :
940 mkpathdup("%s", o->ancestor);
941 name1 = mkpathdup("%s", branch1);
942 name2 = mkpathdup("%s", branch2);
945 read_mmblob(&orig, &one->oid);
946 read_mmblob(&src1, &a->oid);
947 read_mmblob(&src2, &b->oid);
949 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
950 &src1, name1, &src2, name2, &ll_opts);
961 static int merge_file_1(struct merge_options *o,
962 const struct diff_filespec *one,
963 const struct diff_filespec *a,
964 const struct diff_filespec *b,
967 struct merge_file_info *result)
972 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
974 if (S_ISREG(a->mode)) {
975 result->mode = a->mode;
976 oidcpy(&result->oid, &a->oid);
978 result->mode = b->mode;
979 oidcpy(&result->oid, &b->oid);
982 if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
988 if (a->mode == b->mode || a->mode == one->mode)
989 result->mode = b->mode;
991 result->mode = a->mode;
992 if (b->mode != one->mode) {
998 if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
999 oidcpy(&result->oid, &b->oid);
1000 else if (oid_eq(&b->oid, &one->oid))
1001 oidcpy(&result->oid, &a->oid);
1002 else if (S_ISREG(a->mode)) {
1003 mmbuffer_t result_buf;
1004 int ret = 0, merge_status;
1006 merge_status = merge_3way(o, &result_buf, one, a, b,
1009 if ((merge_status < 0) || !result_buf.ptr)
1010 ret = err(o, _("Failed to execute internal merge"));
1012 if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
1013 blob_type, result->oid.hash))
1014 ret = err(o, _("Unable to add %s to database"),
1017 free(result_buf.ptr);
1020 result->clean = (merge_status == 0);
1021 } else if (S_ISGITLINK(a->mode)) {
1022 result->clean = merge_submodule(&result->oid,
1028 } else if (S_ISLNK(a->mode)) {
1029 oidcpy(&result->oid, &a->oid);
1031 if (!oid_eq(&a->oid, &b->oid))
1034 die("BUG: unsupported object type in the tree");
1040 static int merge_file_special_markers(struct merge_options *o,
1041 const struct diff_filespec *one,
1042 const struct diff_filespec *a,
1043 const struct diff_filespec *b,
1044 const char *branch1,
1045 const char *filename1,
1046 const char *branch2,
1047 const char *filename2,
1048 struct merge_file_info *mfi)
1055 side1 = xstrfmt("%s:%s", branch1, filename1);
1057 side2 = xstrfmt("%s:%s", branch2, filename2);
1059 ret = merge_file_1(o, one, a, b,
1060 side1 ? side1 : branch1,
1061 side2 ? side2 : branch2, mfi);
1067 static int merge_file_one(struct merge_options *o,
1069 const struct object_id *o_oid, int o_mode,
1070 const struct object_id *a_oid, int a_mode,
1071 const struct object_id *b_oid, int b_mode,
1072 const char *branch1,
1073 const char *branch2,
1074 struct merge_file_info *mfi)
1076 struct diff_filespec one, a, b;
1078 one.path = a.path = b.path = (char *)path;
1079 oidcpy(&one.oid, o_oid);
1081 oidcpy(&a.oid, a_oid);
1083 oidcpy(&b.oid, b_oid);
1085 return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
1088 static int handle_change_delete(struct merge_options *o,
1089 const char *path, const char *old_path,
1090 const struct object_id *o_oid, int o_mode,
1091 const struct object_id *changed_oid,
1093 const char *change_branch,
1094 const char *delete_branch,
1095 const char *change, const char *change_past)
1097 char *alt_path = NULL;
1098 const char *update_path = path;
1101 if (dir_in_way(path, !o->call_depth, 0)) {
1102 update_path = alt_path = unique_path(o, path, change_branch);
1105 if (o->call_depth) {
1107 * We cannot arbitrarily accept either a_sha or b_sha as
1108 * correct; since there is no true "middle point" between
1109 * them, simply reuse the base version for virtual merge base.
1111 ret = remove_file_from_cache(path);
1113 ret = update_file(o, 0, o_oid, o_mode, update_path);
1117 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1118 "and %s in %s. Version %s of %s left in tree."),
1119 change, path, delete_branch, change_past,
1120 change_branch, change_branch, path);
1122 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1123 "and %s to %s in %s. Version %s of %s left in tree."),
1124 change, old_path, delete_branch, change_past, path,
1125 change_branch, change_branch, path);
1129 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1130 "and %s in %s. Version %s of %s left in tree at %s."),
1131 change, path, delete_branch, change_past,
1132 change_branch, change_branch, path, alt_path);
1134 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1135 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1136 change, old_path, delete_branch, change_past, path,
1137 change_branch, change_branch, path, alt_path);
1141 * No need to call update_file() on path when change_branch ==
1142 * o->branch1 && !alt_path, since that would needlessly touch
1143 * path. We could call update_file_flags() with update_cache=0
1144 * and update_wd=0, but that's a no-op.
1146 if (change_branch != o->branch1 || alt_path)
1147 ret = update_file(o, 0, changed_oid, changed_mode, update_path);
1154 static int conflict_rename_delete(struct merge_options *o,
1155 struct diff_filepair *pair,
1156 const char *rename_branch,
1157 const char *delete_branch)
1159 const struct diff_filespec *orig = pair->one;
1160 const struct diff_filespec *dest = pair->two;
1162 if (handle_change_delete(o,
1163 o->call_depth ? orig->path : dest->path,
1164 o->call_depth ? NULL : orig->path,
1165 &orig->oid, orig->mode,
1166 &dest->oid, dest->mode,
1167 rename_branch, delete_branch,
1168 _("rename"), _("renamed")))
1172 return remove_file_from_cache(dest->path);
1174 return update_stages(o, dest->path, NULL,
1175 rename_branch == o->branch1 ? dest : NULL,
1176 rename_branch == o->branch1 ? NULL : dest);
1179 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
1180 struct stage_data *entry,
1183 struct object_id *oid = &entry->stages[stage].oid;
1184 unsigned mode = entry->stages[stage].mode;
1185 if (mode == 0 || is_null_oid(oid))
1187 oidcpy(&target->oid, oid);
1188 target->mode = mode;
1192 static int handle_file(struct merge_options *o,
1193 struct diff_filespec *rename,
1195 struct rename_conflict_info *ci)
1197 char *dst_name = rename->path;
1198 struct stage_data *dst_entry;
1199 const char *cur_branch, *other_branch;
1200 struct diff_filespec other;
1201 struct diff_filespec *add;
1205 dst_entry = ci->dst_entry1;
1206 cur_branch = ci->branch1;
1207 other_branch = ci->branch2;
1209 dst_entry = ci->dst_entry2;
1210 cur_branch = ci->branch2;
1211 other_branch = ci->branch1;
1214 add = filespec_from_entry(&other, dst_entry, stage ^ 1);
1216 char *add_name = unique_path(o, rename->path, other_branch);
1217 if (update_file(o, 0, &add->oid, add->mode, add_name))
1220 remove_file(o, 0, rename->path, 0);
1221 dst_name = unique_path(o, rename->path, cur_branch);
1223 if (dir_in_way(rename->path, !o->call_depth, 0)) {
1224 dst_name = unique_path(o, rename->path, cur_branch);
1225 output(o, 1, _("%s is a directory in %s adding as %s instead"),
1226 rename->path, other_branch, dst_name);
1229 if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
1230 ; /* fall through, do allow dst_name to be released */
1231 else if (stage == 2)
1232 ret = update_stages(o, rename->path, NULL, rename, add);
1234 ret = update_stages(o, rename->path, NULL, add, rename);
1236 if (dst_name != rename->path)
1242 static int conflict_rename_rename_1to2(struct merge_options *o,
1243 struct rename_conflict_info *ci)
1245 /* One file was renamed in both branches, but to different names. */
1246 struct diff_filespec *one = ci->pair1->one;
1247 struct diff_filespec *a = ci->pair1->two;
1248 struct diff_filespec *b = ci->pair2->two;
1250 output(o, 1, _("CONFLICT (rename/rename): "
1251 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1252 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1253 one->path, a->path, ci->branch1,
1254 one->path, b->path, ci->branch2,
1255 o->call_depth ? _(" (left unresolved)") : "");
1256 if (o->call_depth) {
1257 struct merge_file_info mfi;
1258 struct diff_filespec other;
1259 struct diff_filespec *add;
1260 if (merge_file_one(o, one->path,
1261 &one->oid, one->mode,
1264 ci->branch1, ci->branch2, &mfi))
1268 * FIXME: For rename/add-source conflicts (if we could detect
1269 * such), this is wrong. We should instead find a unique
1270 * pathname and then either rename the add-source file to that
1271 * unique path, or use that unique path instead of src here.
1273 if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
1277 * Above, we put the merged content at the merge-base's
1278 * path. Now we usually need to delete both a->path and
1279 * b->path. However, the rename on each side of the merge
1280 * could also be involved in a rename/add conflict. In
1281 * such cases, we should keep the added file around,
1282 * resolving the conflict at that path in its favor.
1284 add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
1286 if (update_file(o, 0, &add->oid, add->mode, a->path))
1290 remove_file_from_cache(a->path);
1291 add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
1293 if (update_file(o, 0, &add->oid, add->mode, b->path))
1297 remove_file_from_cache(b->path);
1298 } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
1304 static int conflict_rename_rename_2to1(struct merge_options *o,
1305 struct rename_conflict_info *ci)
1307 /* Two files, a & b, were renamed to the same thing, c. */
1308 struct diff_filespec *a = ci->pair1->one;
1309 struct diff_filespec *b = ci->pair2->one;
1310 struct diff_filespec *c1 = ci->pair1->two;
1311 struct diff_filespec *c2 = ci->pair2->two;
1312 char *path = c1->path; /* == c2->path */
1313 struct merge_file_info mfi_c1;
1314 struct merge_file_info mfi_c2;
1317 output(o, 1, _("CONFLICT (rename/rename): "
1318 "Rename %s->%s in %s. "
1319 "Rename %s->%s in %s"),
1320 a->path, c1->path, ci->branch1,
1321 b->path, c2->path, ci->branch2);
1323 remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
1324 remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
1326 if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
1327 o->branch1, c1->path,
1328 o->branch2, ci->ren1_other.path, &mfi_c1) ||
1329 merge_file_special_markers(o, b, &ci->ren2_other, c2,
1330 o->branch1, ci->ren2_other.path,
1331 o->branch2, c2->path, &mfi_c2))
1334 if (o->call_depth) {
1336 * If mfi_c1.clean && mfi_c2.clean, then it might make
1337 * sense to do a two-way merge of those results. But, I
1338 * think in all cases, it makes sense to have the virtual
1339 * merge base just undo the renames; they can be detected
1340 * again later for the non-recursive merge.
1342 remove_file(o, 0, path, 0);
1343 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
1345 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1348 char *new_path1 = unique_path(o, path, ci->branch1);
1349 char *new_path2 = unique_path(o, path, ci->branch2);
1350 output(o, 1, _("Renaming %s to %s and %s to %s instead"),
1351 a->path, new_path1, b->path, new_path2);
1352 remove_file(o, 0, path, 0);
1353 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
1355 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1364 static int process_renames(struct merge_options *o,
1365 struct string_list *a_renames,
1366 struct string_list *b_renames)
1368 int clean_merge = 1, i, j;
1369 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
1370 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
1371 const struct rename *sre;
1373 for (i = 0; i < a_renames->nr; i++) {
1374 sre = a_renames->items[i].util;
1375 string_list_insert(&a_by_dst, sre->pair->two->path)->util
1378 for (i = 0; i < b_renames->nr; i++) {
1379 sre = b_renames->items[i].util;
1380 string_list_insert(&b_by_dst, sre->pair->two->path)->util
1384 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
1385 struct string_list *renames1, *renames2Dst;
1386 struct rename *ren1 = NULL, *ren2 = NULL;
1387 const char *branch1, *branch2;
1388 const char *ren1_src, *ren1_dst;
1389 struct string_list_item *lookup;
1391 if (i >= a_renames->nr) {
1392 ren2 = b_renames->items[j++].util;
1393 } else if (j >= b_renames->nr) {
1394 ren1 = a_renames->items[i++].util;
1396 int compare = strcmp(a_renames->items[i].string,
1397 b_renames->items[j].string);
1399 ren1 = a_renames->items[i++].util;
1401 ren2 = b_renames->items[j++].util;
1404 /* TODO: refactor, so that 1/2 are not needed */
1406 renames1 = a_renames;
1407 renames2Dst = &b_by_dst;
1408 branch1 = o->branch1;
1409 branch2 = o->branch2;
1411 renames1 = b_renames;
1412 renames2Dst = &a_by_dst;
1413 branch1 = o->branch2;
1414 branch2 = o->branch1;
1418 if (ren1->processed)
1420 ren1->processed = 1;
1421 ren1->dst_entry->processed = 1;
1422 /* BUG: We should only mark src_entry as processed if we
1423 * are not dealing with a rename + add-source case.
1425 ren1->src_entry->processed = 1;
1427 ren1_src = ren1->pair->one->path;
1428 ren1_dst = ren1->pair->two->path;
1431 /* One file renamed on both sides */
1432 const char *ren2_src = ren2->pair->one->path;
1433 const char *ren2_dst = ren2->pair->two->path;
1434 enum rename_type rename_type;
1435 if (strcmp(ren1_src, ren2_src) != 0)
1436 die("BUG: ren1_src != ren2_src");
1437 ren2->dst_entry->processed = 1;
1438 ren2->processed = 1;
1439 if (strcmp(ren1_dst, ren2_dst) != 0) {
1440 rename_type = RENAME_ONE_FILE_TO_TWO;
1443 rename_type = RENAME_ONE_FILE_TO_ONE;
1444 /* BUG: We should only remove ren1_src in
1445 * the base stage (think of rename +
1446 * add-source cases).
1448 remove_file(o, 1, ren1_src, 1);
1449 update_entry(ren1->dst_entry,
1454 setup_rename_conflict_info(rename_type,
1464 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
1465 /* Two different files renamed to the same thing */
1467 ren2 = lookup->util;
1468 ren2_dst = ren2->pair->two->path;
1469 if (strcmp(ren1_dst, ren2_dst) != 0)
1470 die("BUG: ren1_dst != ren2_dst");
1473 ren2->processed = 1;
1475 * BUG: We should only mark src_entry as processed
1476 * if we are not dealing with a rename + add-source
1479 ren2->src_entry->processed = 1;
1481 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
1493 /* Renamed in 1, maybe changed in 2 */
1494 /* we only use sha1 and mode of these */
1495 struct diff_filespec src_other, dst_other;
1499 * unpack_trees loads entries from common-commit
1500 * into stage 1, from head-commit into stage 2, and
1501 * from merge-commit into stage 3. We keep track
1502 * of which side corresponds to the rename.
1504 int renamed_stage = a_renames == renames1 ? 2 : 3;
1505 int other_stage = a_renames == renames1 ? 3 : 2;
1507 /* BUG: We should only remove ren1_src in the base
1508 * stage and in other_stage (think of rename +
1511 remove_file(o, 1, ren1_src,
1512 renamed_stage == 2 || !was_tracked(ren1_src));
1514 oidcpy(&src_other.oid,
1515 &ren1->src_entry->stages[other_stage].oid);
1516 src_other.mode = ren1->src_entry->stages[other_stage].mode;
1517 oidcpy(&dst_other.oid,
1518 &ren1->dst_entry->stages[other_stage].oid);
1519 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
1522 if (oid_eq(&src_other.oid, &null_oid)) {
1523 setup_rename_conflict_info(RENAME_DELETE,
1533 } else if ((dst_other.mode == ren1->pair->two->mode) &&
1534 oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
1536 * Added file on the other side identical to
1537 * the file being renamed: clean merge.
1538 * Also, there is no need to overwrite the
1539 * file already in the working copy, so call
1540 * update_file_flags() instead of
1543 if (update_file_flags(o,
1544 &ren1->pair->two->oid,
1545 ren1->pair->two->mode,
1547 1, /* update_cache */
1550 } else if (!oid_eq(&dst_other.oid, &null_oid)) {
1553 output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1555 ren1_src, ren1_dst, branch1,
1557 if (o->call_depth) {
1558 struct merge_file_info mfi;
1559 if (merge_file_one(o, ren1_dst, &null_oid, 0,
1560 &ren1->pair->two->oid,
1561 ren1->pair->two->mode,
1564 branch1, branch2, &mfi)) {
1566 goto cleanup_and_return;
1568 output(o, 1, _("Adding merged %s"), ren1_dst);
1569 if (update_file(o, 0, &mfi.oid,
1570 mfi.mode, ren1_dst))
1574 char *new_path = unique_path(o, ren1_dst, branch2);
1575 output(o, 1, _("Adding as %s instead"), new_path);
1576 if (update_file(o, 0, &dst_other.oid,
1577 dst_other.mode, new_path))
1584 if (clean_merge < 0)
1585 goto cleanup_and_return;
1587 struct diff_filespec *one, *a, *b;
1588 src_other.path = (char *)ren1_src;
1590 one = ren1->pair->one;
1591 if (a_renames == renames1) {
1592 a = ren1->pair->two;
1595 b = ren1->pair->two;
1598 update_entry(ren1->dst_entry, one, a, b);
1599 setup_rename_conflict_info(RENAME_NORMAL,
1613 string_list_clear(&a_by_dst, 0);
1614 string_list_clear(&b_by_dst, 0);
1619 static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
1621 return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
1624 static int read_oid_strbuf(struct merge_options *o,
1625 const struct object_id *oid, struct strbuf *dst)
1628 enum object_type type;
1630 buf = read_sha1_file(oid->hash, &type, &size);
1632 return err(o, _("cannot read object %s"), oid_to_hex(oid));
1633 if (type != OBJ_BLOB) {
1635 return err(o, _("object %s is not a blob"), oid_to_hex(oid));
1637 strbuf_attach(dst, buf, size, size + 1);
1641 static int blob_unchanged(struct merge_options *opt,
1642 const struct object_id *o_oid,
1644 const struct object_id *a_oid,
1646 int renormalize, const char *path)
1648 struct strbuf o = STRBUF_INIT;
1649 struct strbuf a = STRBUF_INIT;
1650 int ret = 0; /* assume changed for safety */
1652 if (a_mode != o_mode)
1654 if (oid_eq(o_oid, a_oid))
1659 assert(o_oid && a_oid);
1660 if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
1663 * Note: binary | is used so that both renormalizations are
1664 * performed. Comparison can be skipped if both files are
1665 * unchanged since their sha1s have already been compared.
1667 if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
1668 renormalize_buffer(&the_index, path, a.buf, a.len, &a))
1669 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1677 static int handle_modify_delete(struct merge_options *o,
1679 struct object_id *o_oid, int o_mode,
1680 struct object_id *a_oid, int a_mode,
1681 struct object_id *b_oid, int b_mode)
1683 const char *modify_branch, *delete_branch;
1684 struct object_id *changed_oid;
1688 modify_branch = o->branch1;
1689 delete_branch = o->branch2;
1690 changed_oid = a_oid;
1691 changed_mode = a_mode;
1693 modify_branch = o->branch2;
1694 delete_branch = o->branch1;
1695 changed_oid = b_oid;
1696 changed_mode = b_mode;
1699 return handle_change_delete(o,
1702 changed_oid, changed_mode,
1703 modify_branch, delete_branch,
1704 _("modify"), _("modified"));
1707 static int merge_content(struct merge_options *o,
1709 struct object_id *o_oid, int o_mode,
1710 struct object_id *a_oid, int a_mode,
1711 struct object_id *b_oid, int b_mode,
1712 struct rename_conflict_info *rename_conflict_info)
1714 const char *reason = _("content");
1715 const char *path1 = NULL, *path2 = NULL;
1716 struct merge_file_info mfi;
1717 struct diff_filespec one, a, b;
1718 unsigned df_conflict_remains = 0;
1721 reason = _("add/add");
1722 o_oid = (struct object_id *)&null_oid;
1724 one.path = a.path = b.path = (char *)path;
1725 oidcpy(&one.oid, o_oid);
1727 oidcpy(&a.oid, a_oid);
1729 oidcpy(&b.oid, b_oid);
1732 if (rename_conflict_info) {
1733 struct diff_filepair *pair1 = rename_conflict_info->pair1;
1735 path1 = (o->branch1 == rename_conflict_info->branch1) ?
1736 pair1->two->path : pair1->one->path;
1737 /* If rename_conflict_info->pair2 != NULL, we are in
1738 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1741 path2 = (rename_conflict_info->pair2 ||
1742 o->branch2 == rename_conflict_info->branch1) ?
1743 pair1->two->path : pair1->one->path;
1745 if (dir_in_way(path, !o->call_depth,
1746 S_ISGITLINK(pair1->two->mode)))
1747 df_conflict_remains = 1;
1749 if (merge_file_special_markers(o, &one, &a, &b,
1751 o->branch2, path2, &mfi))
1754 if (mfi.clean && !df_conflict_remains &&
1755 oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
1756 int path_renamed_outside_HEAD;
1757 output(o, 3, _("Skipped %s (merged same as existing)"), path);
1759 * The content merge resulted in the same file contents we
1760 * already had. We can return early if those file contents
1761 * are recorded at the correct path (which may not be true
1762 * if the merge involves a rename).
1764 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
1765 if (!path_renamed_outside_HEAD) {
1766 add_cacheinfo(o, mfi.mode, &mfi.oid, path,
1767 0, (!o->call_depth), 0);
1771 output(o, 2, _("Auto-merging %s"), path);
1774 if (S_ISGITLINK(mfi.mode))
1775 reason = _("submodule");
1776 output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
1778 if (rename_conflict_info && !df_conflict_remains)
1779 if (update_stages(o, path, &one, &a, &b))
1783 if (df_conflict_remains) {
1785 if (o->call_depth) {
1786 remove_file_from_cache(path);
1789 if (update_stages(o, path, &one, &a, &b))
1792 int file_from_stage2 = was_tracked(path);
1793 struct diff_filespec merged;
1794 oidcpy(&merged.oid, &mfi.oid);
1795 merged.mode = mfi.mode;
1797 if (update_stages(o, path, NULL,
1798 file_from_stage2 ? &merged : NULL,
1799 file_from_stage2 ? NULL : &merged))
1804 new_path = unique_path(o, path, rename_conflict_info->branch1);
1805 output(o, 1, _("Adding as %s instead"), new_path);
1806 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
1812 } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
1817 /* Per entry merge function */
1818 static int process_entry(struct merge_options *o,
1819 const char *path, struct stage_data *entry)
1821 int clean_merge = 1;
1822 int normalize = o->renormalize;
1823 unsigned o_mode = entry->stages[1].mode;
1824 unsigned a_mode = entry->stages[2].mode;
1825 unsigned b_mode = entry->stages[3].mode;
1826 struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
1827 struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
1828 struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
1830 entry->processed = 1;
1831 if (entry->rename_conflict_info) {
1832 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
1833 switch (conflict_info->rename_type) {
1835 case RENAME_ONE_FILE_TO_ONE:
1836 clean_merge = merge_content(o, path,
1837 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1842 if (conflict_rename_delete(o,
1843 conflict_info->pair1,
1844 conflict_info->branch1,
1845 conflict_info->branch2))
1848 case RENAME_ONE_FILE_TO_TWO:
1850 if (conflict_rename_rename_1to2(o, conflict_info))
1853 case RENAME_TWO_FILES_TO_ONE:
1855 if (conflict_rename_rename_2to1(o, conflict_info))
1859 entry->processed = 0;
1862 } else if (o_oid && (!a_oid || !b_oid)) {
1863 /* Case A: Deleted in one */
1864 if ((!a_oid && !b_oid) ||
1865 (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
1866 (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
1867 /* Deleted in both or deleted in one and
1868 * unchanged in the other */
1870 output(o, 2, _("Removing %s"), path);
1871 /* do not touch working file if it did not exist */
1872 remove_file(o, 1, path, !a_oid);
1874 /* Modify/delete; deleted side may have put a directory in the way */
1876 if (handle_modify_delete(o, path, o_oid, o_mode,
1877 a_oid, a_mode, b_oid, b_mode))
1880 } else if ((!o_oid && a_oid && !b_oid) ||
1881 (!o_oid && !a_oid && b_oid)) {
1882 /* Case B: Added in one. */
1883 /* [nothing|directory] -> ([nothing|directory], file) */
1885 const char *add_branch;
1886 const char *other_branch;
1888 const struct object_id *oid;
1892 add_branch = o->branch1;
1893 other_branch = o->branch2;
1896 conf = _("file/directory");
1898 add_branch = o->branch2;
1899 other_branch = o->branch1;
1902 conf = _("directory/file");
1904 if (dir_in_way(path,
1905 !o->call_depth && !S_ISGITLINK(a_mode),
1907 char *new_path = unique_path(o, path, add_branch);
1909 output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1911 conf, path, other_branch, path, new_path);
1912 if (update_file(o, 0, oid, mode, new_path))
1914 else if (o->call_depth)
1915 remove_file_from_cache(path);
1918 output(o, 2, _("Adding %s"), path);
1919 /* do not overwrite file if already present */
1920 if (update_file_flags(o, oid, mode, path, 1, !a_oid))
1923 } else if (a_oid && b_oid) {
1924 /* Case C: Added in both (check for same permissions) and */
1925 /* case D: Modified in both, but differently. */
1926 clean_merge = merge_content(o, path,
1927 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1929 } else if (!o_oid && !a_oid && !b_oid) {
1931 * this entry was deleted altogether. a_mode == 0 means
1932 * we had that path and want to actively remove it.
1934 remove_file(o, 1, path, !a_mode);
1936 die("BUG: fatal merge failure, shouldn't happen.");
1941 int merge_trees(struct merge_options *o,
1944 struct tree *common,
1945 struct tree **result)
1949 if (o->subtree_shift) {
1950 merge = shift_tree_object(head, merge, o->subtree_shift);
1951 common = shift_tree_object(head, common, o->subtree_shift);
1954 if (oid_eq(&common->object.oid, &merge->object.oid)) {
1955 struct strbuf sb = STRBUF_INIT;
1957 if (!o->call_depth && index_has_changes(&sb)) {
1958 err(o, _("Dirty index: cannot merge (dirty: %s)"),
1962 output(o, 0, _("Already up to date!"));
1967 code = git_merge_trees(o->call_depth, common, head, merge);
1970 if (show(o, 4) || o->call_depth)
1971 err(o, _("merging of trees %s and %s failed"),
1972 oid_to_hex(&head->object.oid),
1973 oid_to_hex(&merge->object.oid));
1977 if (unmerged_cache()) {
1978 struct string_list *entries, *re_head, *re_merge;
1981 * Only need the hashmap while processing entries, so
1982 * initialize it here and free it when we are done running
1983 * through the entries. Keeping it in the merge_options as
1984 * opposed to decaring a local hashmap is for convenience
1985 * so that we don't have to pass it to around.
1987 hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512);
1988 get_files_dirs(o, head);
1989 get_files_dirs(o, merge);
1991 entries = get_unmerged();
1992 record_df_conflict_files(o, entries);
1993 re_head = get_renames(o, head, common, head, merge, entries);
1994 re_merge = get_renames(o, merge, common, head, merge, entries);
1995 clean = process_renames(o, re_head, re_merge);
1998 for (i = entries->nr-1; 0 <= i; i--) {
1999 const char *path = entries->items[i].string;
2000 struct stage_data *e = entries->items[i].util;
2001 if (!e->processed) {
2002 int ret = process_entry(o, path, e);
2011 for (i = 0; i < entries->nr; i++) {
2012 struct stage_data *e = entries->items[i].util;
2014 die("BUG: unprocessed path??? %s",
2015 entries->items[i].string);
2019 string_list_clear(re_merge, 0);
2020 string_list_clear(re_head, 0);
2021 string_list_clear(entries, 1);
2023 hashmap_free(&o->current_file_dir_set, 1);
2035 if (o->call_depth && !(*result = write_tree_from_memory(o)))
2041 static struct commit_list *reverse_commit_list(struct commit_list *list)
2043 struct commit_list *next = NULL, *current, *backup;
2044 for (current = list; current; current = backup) {
2045 backup = current->next;
2046 current->next = next;
2053 * Merge the commits h1 and h2, return the resulting virtual
2054 * commit object and a flag indicating the cleanness of the merge.
2056 int merge_recursive(struct merge_options *o,
2059 struct commit_list *ca,
2060 struct commit **result)
2062 struct commit_list *iter;
2063 struct commit *merged_common_ancestors;
2064 struct tree *mrtree = mrtree;
2068 output(o, 4, _("Merging:"));
2069 output_commit_title(o, h1);
2070 output_commit_title(o, h2);
2074 ca = get_merge_bases(h1, h2);
2075 ca = reverse_commit_list(ca);
2079 unsigned cnt = commit_list_count(ca);
2081 output(o, 5, Q_("found %u common ancestor:",
2082 "found %u common ancestors:", cnt), cnt);
2083 for (iter = ca; iter; iter = iter->next)
2084 output_commit_title(o, iter->item);
2087 merged_common_ancestors = pop_commit(&ca);
2088 if (merged_common_ancestors == NULL) {
2089 /* if there is no common ancestor, use an empty tree */
2092 tree = lookup_tree(the_hash_algo->empty_tree);
2093 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
2096 for (iter = ca; iter; iter = iter->next) {
2097 const char *saved_b1, *saved_b2;
2100 * When the merge fails, the result contains files
2101 * with conflict markers. The cleanness flag is
2102 * ignored (unless indicating an error), it was never
2103 * actually used, as result of merge_trees has always
2104 * overwritten it: the committed "conflicts" were
2108 saved_b1 = o->branch1;
2109 saved_b2 = o->branch2;
2110 o->branch1 = "Temporary merge branch 1";
2111 o->branch2 = "Temporary merge branch 2";
2112 if (merge_recursive(o, merged_common_ancestors, iter->item,
2113 NULL, &merged_common_ancestors) < 0)
2115 o->branch1 = saved_b1;
2116 o->branch2 = saved_b2;
2119 if (!merged_common_ancestors)
2120 return err(o, _("merge returned no commit"));
2127 o->ancestor = "merged common ancestors";
2128 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
2135 if (o->call_depth) {
2136 *result = make_virtual_commit(mrtree, "merged tree");
2137 commit_list_insert(h1, &(*result)->parents);
2138 commit_list_insert(h2, &(*result)->parents->next);
2141 if (!o->call_depth && o->buffer_output < 2)
2142 strbuf_release(&o->obuf);
2144 diff_warn_rename_limit("merge.renamelimit",
2145 o->needed_rename_limit, 0);
2149 static struct commit *get_ref(const struct object_id *oid, const char *name)
2151 struct object *object;
2153 object = deref_tag(parse_object(oid), name, strlen(name));
2156 if (object->type == OBJ_TREE)
2157 return make_virtual_commit((struct tree*)object, name);
2158 if (object->type != OBJ_COMMIT)
2160 if (parse_commit((struct commit *)object))
2162 return (struct commit *)object;
2165 int merge_recursive_generic(struct merge_options *o,
2166 const struct object_id *head,
2167 const struct object_id *merge,
2169 const struct object_id **base_list,
2170 struct commit **result)
2173 struct lock_file lock = LOCK_INIT;
2174 struct commit *head_commit = get_ref(head, o->branch1);
2175 struct commit *next_commit = get_ref(merge, o->branch2);
2176 struct commit_list *ca = NULL;
2180 for (i = 0; i < num_base_list; ++i) {
2181 struct commit *base;
2182 if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
2183 return err(o, _("Could not parse object '%s'"),
2184 oid_to_hex(base_list[i]));
2185 commit_list_insert(base, &ca);
2189 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
2190 clean = merge_recursive(o, head_commit, next_commit, ca,
2195 if (active_cache_changed &&
2196 write_locked_index(&the_index, &lock, COMMIT_LOCK))
2197 return err(o, _("Unable to write index."));
2199 return clean ? 0 : 1;
2202 static void merge_recursive_config(struct merge_options *o)
2204 git_config_get_int("merge.verbosity", &o->verbosity);
2205 git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
2206 git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
2207 git_config(git_xmerge_config, NULL);
2210 void init_merge_options(struct merge_options *o)
2212 const char *merge_verbosity;
2213 memset(o, 0, sizeof(struct merge_options));
2215 o->buffer_output = 1;
2216 o->diff_rename_limit = -1;
2217 o->merge_rename_limit = -1;
2219 o->detect_rename = 1;
2220 merge_recursive_config(o);
2221 merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
2222 if (merge_verbosity)
2223 o->verbosity = strtol(merge_verbosity, NULL, 10);
2224 if (o->verbosity >= 5)
2225 o->buffer_output = 0;
2226 strbuf_init(&o->obuf, 0);
2227 string_list_init(&o->df_conflict_file_set, 1);
2230 int parse_merge_opt(struct merge_options *o, const char *s)
2236 if (!strcmp(s, "ours"))
2237 o->recursive_variant = MERGE_RECURSIVE_OURS;
2238 else if (!strcmp(s, "theirs"))
2239 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
2240 else if (!strcmp(s, "subtree"))
2241 o->subtree_shift = "";
2242 else if (skip_prefix(s, "subtree=", &arg))
2243 o->subtree_shift = arg;
2244 else if (!strcmp(s, "patience"))
2245 o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
2246 else if (!strcmp(s, "histogram"))
2247 o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
2248 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
2249 long value = parse_algorithm_value(arg);
2252 /* clear out previous settings */
2253 DIFF_XDL_CLR(o, NEED_MINIMAL);
2254 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
2255 o->xdl_opts |= value;
2257 else if (!strcmp(s, "ignore-space-change"))
2258 DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);
2259 else if (!strcmp(s, "ignore-all-space"))
2260 DIFF_XDL_SET(o, IGNORE_WHITESPACE);
2261 else if (!strcmp(s, "ignore-space-at-eol"))
2262 DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);
2263 else if (!strcmp(s, "ignore-cr-at-eol"))
2264 DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);
2265 else if (!strcmp(s, "renormalize"))
2267 else if (!strcmp(s, "no-renormalize"))
2269 else if (!strcmp(s, "no-renames"))
2270 o->detect_rename = 0;
2271 else if (!strcmp(s, "find-renames")) {
2272 o->detect_rename = 1;
2273 o->rename_score = 0;
2275 else if (skip_prefix(s, "find-renames=", &arg) ||
2276 skip_prefix(s, "rename-threshold=", &arg)) {
2277 if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
2279 o->detect_rename = 1;