2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
8 #include "cache-tree.h"
12 #include "tree-walk.h"
16 #include "unpack-trees.h"
17 #include "string-list.h"
18 #include "xdiff-interface.h"
21 #include "merge-recursive.h"
24 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
26 unsigned char shifted[20];
29 * NEEDSWORK: this limits the recursion depth to hardcoded
30 * value '2' to avoid excessive overhead.
32 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
33 if (!hashcmp(two->object.sha1, shifted))
35 return lookup_tree(shifted);
39 * A virtual commit has (const char *)commit->util set to the name.
42 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
44 struct commit *commit = xcalloc(1, sizeof(struct commit));
46 commit->util = (void*)comment;
48 commit->object.parsed = 1;
53 * Since we use get_tree_entry(), which does not put the read object into
54 * the object pool, we cannot rely on a == b.
56 static int sha_eq(const unsigned char *a, const unsigned char *b)
60 return a && b && hashcmp(a, b) == 0;
64 * Since we want to write the index eventually, we cannot reuse the index
65 * for these (temporary) data.
72 unsigned char sha[20];
77 static int show(struct merge_options *o, int v)
79 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
82 static void flush_output(struct merge_options *o)
85 fputs(o->obuf.buf, stdout);
86 strbuf_reset(&o->obuf);
90 static void output(struct merge_options *o, int v, const char *fmt, ...)
98 strbuf_grow(&o->obuf, o->call_depth * 2 + 2);
99 memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2);
100 strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
103 len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
108 if (len >= strbuf_avail(&o->obuf)) {
109 strbuf_grow(&o->obuf, len + 2);
111 len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
113 if (len >= strbuf_avail(&o->obuf)) {
114 die("this should not happen, your snprintf is broken");
117 strbuf_setlen(&o->obuf, o->obuf.len + len);
118 strbuf_add(&o->obuf, "\n", 1);
119 if (!o->buffer_output)
123 static void output_commit_title(struct merge_options *o, struct commit *commit)
127 for (i = o->call_depth; i--;)
130 printf("virtual %s\n", (char *)commit->util);
132 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
133 if (parse_commit(commit) != 0)
134 printf("(bad commit)\n");
138 for (s = commit->buffer; *s; s++)
139 if (*s == '\n' && s[1] == '\n') {
143 for (len = 0; s[len] && '\n' != s[len]; len++)
145 printf("%.*s\n", len, s);
150 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
151 const char *path, int stage, int refresh, int options)
153 struct cache_entry *ce;
154 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
156 return error("addinfo_cache failed for path '%s'", path);
157 return add_cache_entry(ce, options);
160 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
163 init_tree_desc(desc, tree->buffer, tree->size);
166 static int git_merge_trees(int index_only,
172 struct tree_desc t[3];
173 struct unpack_trees_options opts;
174 struct unpack_trees_error_msgs msgs = {
175 /* would_overwrite */
176 "Your local changes to '%s' would be overwritten by merge. Aborting.",
177 /* not_uptodate_file */
178 "Your local changes to '%s' would be overwritten by merge. Aborting.",
179 /* not_uptodate_dir */
180 "Updating '%s' would lose untracked files in it. Aborting.",
181 /* would_lose_untracked */
182 "Untracked working tree file '%s' would be %s by merge. Aborting",
183 /* bind_overlap -- will not happen here */
186 if (advice_commit_before_merge) {
187 msgs.would_overwrite = msgs.not_uptodate_file =
188 "Your local changes to '%s' would be overwritten by merge. Aborting.\n"
189 "Please, commit your changes or stash them before you can merge.";
192 memset(&opts, 0, sizeof(opts));
199 opts.fn = threeway_merge;
200 opts.src_index = &the_index;
201 opts.dst_index = &the_index;
204 init_tree_desc_from_tree(t+0, common);
205 init_tree_desc_from_tree(t+1, head);
206 init_tree_desc_from_tree(t+2, merge);
208 rc = unpack_trees(3, t, &opts);
209 cache_tree_free(&active_cache_tree);
213 struct tree *write_tree_from_memory(struct merge_options *o)
215 struct tree *result = NULL;
217 if (unmerged_cache()) {
219 output(o, 0, "There are unmerged index entries:");
220 for (i = 0; i < active_nr; i++) {
221 struct cache_entry *ce = active_cache[i];
223 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
228 if (!active_cache_tree)
229 active_cache_tree = cache_tree();
231 if (!cache_tree_fully_valid(active_cache_tree) &&
232 cache_tree_update(active_cache_tree,
233 active_cache, active_nr, 0, 0) < 0)
234 die("error building trees");
236 result = lookup_tree(active_cache_tree->sha1);
241 static int save_files_dirs(const unsigned char *sha1,
242 const char *base, int baselen, const char *path,
243 unsigned int mode, int stage, void *context)
245 int len = strlen(path);
246 char *newpath = xmalloc(baselen + len + 1);
247 struct merge_options *o = context;
249 memcpy(newpath, base, baselen);
250 memcpy(newpath + baselen, path, len);
251 newpath[baselen + len] = '\0';
254 string_list_insert(newpath, &o->current_directory_set);
256 string_list_insert(newpath, &o->current_file_set);
259 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
262 static int get_files_dirs(struct merge_options *o, struct tree *tree)
265 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
267 n = o->current_file_set.nr + o->current_directory_set.nr;
272 * Returns an index_entry instance which doesn't have to correspond to
273 * a real cache entry in Git's index.
275 static struct stage_data *insert_stage_data(const char *path,
276 struct tree *o, struct tree *a, struct tree *b,
277 struct string_list *entries)
279 struct string_list_item *item;
280 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
281 get_tree_entry(o->object.sha1, path,
282 e->stages[1].sha, &e->stages[1].mode);
283 get_tree_entry(a->object.sha1, path,
284 e->stages[2].sha, &e->stages[2].mode);
285 get_tree_entry(b->object.sha1, path,
286 e->stages[3].sha, &e->stages[3].mode);
287 item = string_list_insert(path, entries);
293 * Create a dictionary mapping file names to stage_data objects. The
294 * dictionary contains one entry for every path with a non-zero stage entry.
296 static struct string_list *get_unmerged(void)
298 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
301 unmerged->strdup_strings = 1;
303 for (i = 0; i < active_nr; i++) {
304 struct string_list_item *item;
305 struct stage_data *e;
306 struct cache_entry *ce = active_cache[i];
310 item = string_list_lookup(ce->name, unmerged);
312 item = string_list_insert(ce->name, unmerged);
313 item->util = xcalloc(1, sizeof(struct stage_data));
316 e->stages[ce_stage(ce)].mode = ce->ce_mode;
317 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
325 struct diff_filepair *pair;
326 struct stage_data *src_entry;
327 struct stage_data *dst_entry;
328 unsigned processed:1;
332 * Get information of all renames which occurred between 'o_tree' and
333 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
334 * 'b_tree') to be able to associate the correct cache entries with
335 * the rename information. 'tree' is always equal to either a_tree or b_tree.
337 static struct string_list *get_renames(struct merge_options *o,
342 struct string_list *entries)
345 struct string_list *renames;
346 struct diff_options opts;
348 renames = xcalloc(1, sizeof(struct string_list));
350 DIFF_OPT_SET(&opts, RECURSIVE);
351 opts.detect_rename = DIFF_DETECT_RENAME;
352 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
353 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
355 opts.warn_on_too_large_rename = 1;
356 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
357 if (diff_setup_done(&opts) < 0)
358 die("diff setup failed");
359 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
361 for (i = 0; i < diff_queued_diff.nr; ++i) {
362 struct string_list_item *item;
364 struct diff_filepair *pair = diff_queued_diff.queue[i];
365 if (pair->status != 'R') {
366 diff_free_filepair(pair);
369 re = xmalloc(sizeof(*re));
372 item = string_list_lookup(re->pair->one->path, entries);
374 re->src_entry = insert_stage_data(re->pair->one->path,
375 o_tree, a_tree, b_tree, entries);
377 re->src_entry = item->util;
379 item = string_list_lookup(re->pair->two->path, entries);
381 re->dst_entry = insert_stage_data(re->pair->two->path,
382 o_tree, a_tree, b_tree, entries);
384 re->dst_entry = item->util;
385 item = string_list_insert(pair->one->path, renames);
388 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
389 diff_queued_diff.nr = 0;
394 static int update_stages(const char *path, struct diff_filespec *o,
395 struct diff_filespec *a, struct diff_filespec *b,
398 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
400 if (remove_file_from_cache(path))
403 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
406 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
409 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
414 static int remove_file(struct merge_options *o, int clean,
415 const char *path, int no_wd)
417 int update_cache = o->call_depth || clean;
418 int update_working_directory = !o->call_depth && !no_wd;
421 if (remove_file_from_cache(path))
424 if (update_working_directory) {
425 if (remove_path(path) && errno != ENOENT)
431 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
433 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
436 char *p = newpath + strlen(path);
437 strcpy(newpath, path);
443 while (string_list_has_string(&o->current_file_set, newpath) ||
444 string_list_has_string(&o->current_directory_set, newpath) ||
445 lstat(newpath, &st) == 0)
446 sprintf(p, "_%d", suffix++);
448 string_list_insert(newpath, &o->current_file_set);
452 static void flush_buffer(int fd, const char *buf, unsigned long size)
455 long ret = write_in_full(fd, buf, size);
460 die_errno("merge-recursive");
462 die("merge-recursive: disk full?");
469 static int would_lose_untracked(const char *path)
471 int pos = cache_name_pos(path, strlen(path));
475 while (pos < active_nr &&
476 !strcmp(path, active_cache[pos]->name)) {
478 * If stage #0, it is definitely tracked.
479 * If it has stage #2 then it was tracked
480 * before this merge started. All other
481 * cases the path was not tracked.
483 switch (ce_stage(active_cache[pos])) {
490 return file_exists(path);
493 static int make_room_for_path(const char *path)
496 const char *msg = "failed to create path '%s'%s";
498 status = safe_create_leading_directories_const(path);
501 /* something else exists */
502 error(msg, path, ": perhaps a D/F conflict?");
509 * Do not unlink a file in the work tree if we are not
512 if (would_lose_untracked(path))
513 return error("refusing to lose untracked file at '%s'",
516 /* Successful unlink is good.. */
519 /* .. and so is no existing file */
522 /* .. but not some other error (who really cares what?) */
523 return error(msg, path, ": perhaps a D/F conflict?");
526 static void update_file_flags(struct merge_options *o,
527 const unsigned char *sha,
537 enum object_type type;
541 if (S_ISGITLINK(mode))
543 * We may later decide to recursively descend into
544 * the submodule directory and update its index
545 * and/or work tree, but we do not do that now.
549 buf = read_sha1_file(sha, &type, &size);
551 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
552 if (type != OBJ_BLOB)
553 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
555 struct strbuf strbuf = STRBUF_INIT;
556 if (convert_to_working_tree(path, buf, size, &strbuf)) {
559 buf = strbuf_detach(&strbuf, NULL);
563 if (make_room_for_path(path) < 0) {
568 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
574 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
576 die_errno("failed to open '%s'", path);
577 flush_buffer(fd, buf, size);
579 } else if (S_ISLNK(mode)) {
580 char *lnk = xmemdupz(buf, size);
581 safe_create_leading_directories_const(path);
583 if (symlink(lnk, path))
584 die_errno("failed to symlink '%s'", path);
587 die("do not know what to do with %06o %s '%s'",
588 mode, sha1_to_hex(sha), path);
593 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
596 static void update_file(struct merge_options *o,
598 const unsigned char *sha,
602 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
605 /* Low level file merging, update and removal */
607 struct merge_file_info
609 unsigned char sha[20];
615 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
618 enum object_type type;
620 if (!hashcmp(sha1, null_sha1)) {
621 mm->ptr = xstrdup("");
626 mm->ptr = read_sha1_file(sha1, &type, &size);
627 if (!mm->ptr || type != OBJ_BLOB)
628 die("unable to read blob object %s", sha1_to_hex(sha1));
632 static int merge_3way(struct merge_options *o,
633 mmbuffer_t *result_buf,
634 struct diff_filespec *one,
635 struct diff_filespec *a,
636 struct diff_filespec *b,
640 mmfile_t orig, src1, src2;
644 if (strcmp(a->path, b->path)) {
645 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
646 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
648 name1 = xstrdup(mkpath("%s", branch1));
649 name2 = xstrdup(mkpath("%s", branch2));
652 fill_mm(one->sha1, &orig);
653 fill_mm(a->sha1, &src1);
654 fill_mm(b->sha1, &src2);
656 merge_status = ll_merge(result_buf, a->path, &orig,
657 &src1, name1, &src2, name2,
668 static struct merge_file_info merge_file(struct merge_options *o,
669 struct diff_filespec *one,
670 struct diff_filespec *a,
671 struct diff_filespec *b,
675 struct merge_file_info result;
679 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
681 if (S_ISREG(a->mode)) {
682 result.mode = a->mode;
683 hashcpy(result.sha, a->sha1);
685 result.mode = b->mode;
686 hashcpy(result.sha, b->sha1);
689 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
695 if (a->mode == b->mode || a->mode == one->mode)
696 result.mode = b->mode;
698 result.mode = a->mode;
699 if (b->mode != one->mode) {
705 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
706 hashcpy(result.sha, b->sha1);
707 else if (sha_eq(b->sha1, one->sha1))
708 hashcpy(result.sha, a->sha1);
709 else if (S_ISREG(a->mode)) {
710 mmbuffer_t result_buf;
713 merge_status = merge_3way(o, &result_buf, one, a, b,
716 if ((merge_status < 0) || !result_buf.ptr)
717 die("Failed to execute internal merge");
719 if (write_sha1_file(result_buf.ptr, result_buf.size,
720 blob_type, result.sha))
721 die("Unable to add %s to database",
724 free(result_buf.ptr);
725 result.clean = (merge_status == 0);
726 } else if (S_ISGITLINK(a->mode)) {
728 hashcpy(result.sha, a->sha1);
729 } else if (S_ISLNK(a->mode)) {
730 hashcpy(result.sha, a->sha1);
732 if (!sha_eq(a->sha1, b->sha1))
735 die("unsupported object type in the tree");
742 static void conflict_rename_rename(struct merge_options *o,
750 const char *ren1_dst = ren1->pair->two->path;
751 const char *ren2_dst = ren2->pair->two->path;
752 const char *dst_name1 = ren1_dst;
753 const char *dst_name2 = ren2_dst;
754 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
755 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
756 output(o, 1, "%s is a directory in %s adding as %s instead",
757 ren1_dst, branch2, dst_name1);
758 remove_file(o, 0, ren1_dst, 0);
760 if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
761 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
762 output(o, 1, "%s is a directory in %s adding as %s instead",
763 ren2_dst, branch1, dst_name2);
764 remove_file(o, 0, ren2_dst, 0);
767 remove_file_from_cache(dst_name1);
768 remove_file_from_cache(dst_name2);
770 * Uncomment to leave the conflicting names in the resulting tree
772 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
773 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
776 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
777 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
783 static void conflict_rename_dir(struct merge_options *o,
787 char *new_path = unique_path(o, ren1->pair->two->path, branch1);
788 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
789 remove_file(o, 0, ren1->pair->two->path, 0);
790 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
794 static void conflict_rename_rename_2(struct merge_options *o,
800 char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
801 char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
802 output(o, 1, "Renaming %s to %s and %s to %s instead",
803 ren1->pair->one->path, new_path1,
804 ren2->pair->one->path, new_path2);
805 remove_file(o, 0, ren1->pair->two->path, 0);
806 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
807 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
812 static int process_renames(struct merge_options *o,
813 struct string_list *a_renames,
814 struct string_list *b_renames)
816 int clean_merge = 1, i, j;
817 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
818 const struct rename *sre;
820 for (i = 0; i < a_renames->nr; i++) {
821 sre = a_renames->items[i].util;
822 string_list_insert(sre->pair->two->path, &a_by_dst)->util
825 for (i = 0; i < b_renames->nr; i++) {
826 sre = b_renames->items[i].util;
827 string_list_insert(sre->pair->two->path, &b_by_dst)->util
831 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
833 struct string_list *renames1, *renames2Dst;
834 struct rename *ren1 = NULL, *ren2 = NULL;
835 const char *branch1, *branch2;
836 const char *ren1_src, *ren1_dst;
838 if (i >= a_renames->nr) {
839 ren2 = b_renames->items[j++].util;
840 } else if (j >= b_renames->nr) {
841 ren1 = a_renames->items[i++].util;
843 int compare = strcmp(a_renames->items[i].string,
844 b_renames->items[j].string);
846 ren1 = a_renames->items[i++].util;
848 ren2 = b_renames->items[j++].util;
851 /* TODO: refactor, so that 1/2 are not needed */
853 renames1 = a_renames;
854 renames2Dst = &b_by_dst;
855 branch1 = o->branch1;
856 branch2 = o->branch2;
859 renames1 = b_renames;
860 renames2Dst = &a_by_dst;
861 branch1 = o->branch2;
862 branch2 = o->branch1;
867 src = ren1->pair->one->path;
869 ren1->dst_entry->processed = 1;
870 ren1->src_entry->processed = 1;
876 ren1_src = ren1->pair->one->path;
877 ren1_dst = ren1->pair->two->path;
880 const char *ren2_src = ren2->pair->one->path;
881 const char *ren2_dst = ren2->pair->two->path;
882 /* Renamed in 1 and renamed in 2 */
883 if (strcmp(ren1_src, ren2_src) != 0)
884 die("ren1.src != ren2.src");
885 ren2->dst_entry->processed = 1;
887 if (strcmp(ren1_dst, ren2_dst) != 0) {
889 output(o, 1, "CONFLICT (rename/rename): "
890 "Rename \"%s\"->\"%s\" in branch \"%s\" "
891 "rename \"%s\"->\"%s\" in \"%s\"%s",
892 src, ren1_dst, branch1,
893 src, ren2_dst, branch2,
894 o->call_depth ? " (left unresolved)": "");
896 remove_file_from_cache(src);
897 update_file(o, 0, ren1->pair->one->sha1,
898 ren1->pair->one->mode, src);
900 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
902 struct merge_file_info mfi;
903 remove_file(o, 1, ren1_src, 1);
910 if (mfi.merge || !mfi.clean)
911 output(o, 1, "Renaming %s->%s", src, ren1_dst);
914 output(o, 2, "Auto-merging %s", ren1_dst);
917 output(o, 1, "CONFLICT (content): merge conflict in %s",
922 update_stages(ren1_dst,
928 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
931 /* Renamed in 1, maybe changed in 2 */
932 struct string_list_item *item;
933 /* we only use sha1 and mode of these */
934 struct diff_filespec src_other, dst_other;
935 int try_merge, stage = a_renames == renames1 ? 3: 2;
937 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
939 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
940 src_other.mode = ren1->src_entry->stages[stage].mode;
941 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
942 dst_other.mode = ren1->dst_entry->stages[stage].mode;
946 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
948 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
949 " directory %s added in %s",
950 ren1_src, ren1_dst, branch1,
952 conflict_rename_dir(o, ren1, branch1);
953 } else if (sha_eq(src_other.sha1, null_sha1)) {
955 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
957 ren1_src, ren1_dst, branch1,
959 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
961 update_stages(ren1_dst, NULL,
962 branch1 == o->branch1 ?
963 ren1->pair->two : NULL,
964 branch1 == o->branch1 ?
965 NULL : ren1->pair->two, 1);
966 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
967 const char *new_path;
970 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
972 ren1_src, ren1_dst, branch1,
975 struct merge_file_info mfi;
976 struct diff_filespec one, a, b;
978 one.path = a.path = b.path =
980 hashcpy(one.sha1, null_sha1);
982 hashcpy(a.sha1, ren1->pair->two->sha1);
983 a.mode = ren1->pair->two->mode;
984 hashcpy(b.sha1, dst_other.sha1);
985 b.mode = dst_other.mode;
986 mfi = merge_file(o, &one, &a, &b,
989 output(o, 1, "Adding merged %s", ren1_dst);
995 new_path = unique_path(o, ren1_dst, branch2);
996 output(o, 1, "Adding as %s instead", new_path);
997 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
999 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
1002 ren2->processed = 1;
1003 output(o, 1, "CONFLICT (rename/rename): "
1004 "Rename %s->%s in %s. "
1005 "Rename %s->%s in %s",
1006 ren1_src, ren1_dst, branch1,
1007 ren2->pair->one->path, ren2->pair->two->path, branch2);
1008 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
1013 struct diff_filespec *one, *a, *b;
1014 struct merge_file_info mfi;
1015 src_other.path = (char *)ren1_src;
1017 one = ren1->pair->one;
1018 if (a_renames == renames1) {
1019 a = ren1->pair->two;
1022 b = ren1->pair->two;
1025 mfi = merge_file(o, one, a, b,
1026 o->branch1, o->branch2);
1029 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1030 mfi.mode == ren1->pair->two->mode)
1032 * This messaged is part of
1033 * t6022 test. If you change
1034 * it update the test too.
1036 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
1038 if (mfi.merge || !mfi.clean)
1039 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
1041 output(o, 2, "Auto-merging %s", ren1_dst);
1043 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
1048 update_stages(ren1_dst,
1051 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1056 string_list_clear(&a_by_dst, 0);
1057 string_list_clear(&b_by_dst, 0);
1062 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1064 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1067 /* Per entry merge function */
1068 static int process_entry(struct merge_options *o,
1069 const char *path, struct stage_data *entry)
1072 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1073 print_index_entry("\tpath: ", entry);
1075 int clean_merge = 1;
1076 unsigned o_mode = entry->stages[1].mode;
1077 unsigned a_mode = entry->stages[2].mode;
1078 unsigned b_mode = entry->stages[3].mode;
1079 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1080 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1081 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1083 if (o_sha && (!a_sha || !b_sha)) {
1084 /* Case A: Deleted in one */
1085 if ((!a_sha && !b_sha) ||
1086 (sha_eq(a_sha, o_sha) && !b_sha) ||
1087 (!a_sha && sha_eq(b_sha, o_sha))) {
1088 /* Deleted in both or deleted in one and
1089 * unchanged in the other */
1091 output(o, 2, "Removing %s", path);
1092 /* do not touch working file if it did not exist */
1093 remove_file(o, 1, path, !a_sha);
1095 /* Deleted in one and changed in the other */
1098 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1099 "and modified in %s. Version %s of %s left in tree.",
1101 o->branch2, o->branch2, path);
1102 update_file(o, 0, b_sha, b_mode, path);
1104 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1105 "and modified in %s. Version %s of %s left in tree.",
1107 o->branch1, o->branch1, path);
1108 update_file(o, 0, a_sha, a_mode, path);
1112 } else if ((!o_sha && a_sha && !b_sha) ||
1113 (!o_sha && !a_sha && b_sha)) {
1114 /* Case B: Added in one. */
1115 const char *add_branch;
1116 const char *other_branch;
1118 const unsigned char *sha;
1122 add_branch = o->branch1;
1123 other_branch = o->branch2;
1126 conf = "file/directory";
1128 add_branch = o->branch2;
1129 other_branch = o->branch1;
1132 conf = "directory/file";
1134 if (string_list_has_string(&o->current_directory_set, path)) {
1135 const char *new_path = unique_path(o, path, add_branch);
1137 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1139 conf, path, other_branch, path, new_path);
1140 remove_file(o, 0, path, 0);
1141 update_file(o, 0, sha, mode, new_path);
1143 output(o, 2, "Adding %s", path);
1144 update_file(o, 1, sha, mode, path);
1146 } else if (a_sha && b_sha) {
1147 /* Case C: Added in both (check for same permissions) and */
1148 /* case D: Modified in both, but differently. */
1149 const char *reason = "content";
1150 struct merge_file_info mfi;
1151 struct diff_filespec one, a, b;
1155 o_sha = (unsigned char *)null_sha1;
1157 output(o, 2, "Auto-merging %s", path);
1158 one.path = a.path = b.path = (char *)path;
1159 hashcpy(one.sha1, o_sha);
1161 hashcpy(a.sha1, a_sha);
1163 hashcpy(b.sha1, b_sha);
1166 mfi = merge_file(o, &one, &a, &b,
1167 o->branch1, o->branch2);
1169 clean_merge = mfi.clean;
1171 if (S_ISGITLINK(mfi.mode))
1172 reason = "submodule";
1173 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1176 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1177 } else if (!o_sha && !a_sha && !b_sha) {
1179 * this entry was deleted altogether. a_mode == 0 means
1180 * we had that path and want to actively remove it.
1182 remove_file(o, 1, path, !a_mode);
1184 die("Fatal merge failure, shouldn't happen.");
1189 int merge_trees(struct merge_options *o,
1192 struct tree *common,
1193 struct tree **result)
1197 if (o->subtree_merge) {
1198 merge = shift_tree_object(head, merge);
1199 common = shift_tree_object(head, common);
1202 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1203 output(o, 0, "Already uptodate!");
1208 code = git_merge_trees(o->call_depth, common, head, merge);
1211 if (show(o, 4) || o->call_depth)
1212 die("merging of trees %s and %s failed",
1213 sha1_to_hex(head->object.sha1),
1214 sha1_to_hex(merge->object.sha1));
1219 if (unmerged_cache()) {
1220 struct string_list *entries, *re_head, *re_merge;
1222 string_list_clear(&o->current_file_set, 1);
1223 string_list_clear(&o->current_directory_set, 1);
1224 get_files_dirs(o, head);
1225 get_files_dirs(o, merge);
1227 entries = get_unmerged();
1228 re_head = get_renames(o, head, common, head, merge, entries);
1229 re_merge = get_renames(o, merge, common, head, merge, entries);
1230 clean = process_renames(o, re_head, re_merge);
1231 for (i = 0; i < entries->nr; i++) {
1232 const char *path = entries->items[i].string;
1233 struct stage_data *e = entries->items[i].util;
1235 && !process_entry(o, path, e))
1239 string_list_clear(re_merge, 0);
1240 string_list_clear(re_head, 0);
1241 string_list_clear(entries, 1);
1248 *result = write_tree_from_memory(o);
1253 static struct commit_list *reverse_commit_list(struct commit_list *list)
1255 struct commit_list *next = NULL, *current, *backup;
1256 for (current = list; current; current = backup) {
1257 backup = current->next;
1258 current->next = next;
1265 * Merge the commits h1 and h2, return the resulting virtual
1266 * commit object and a flag indicating the cleanness of the merge.
1268 int merge_recursive(struct merge_options *o,
1271 struct commit_list *ca,
1272 struct commit **result)
1274 struct commit_list *iter;
1275 struct commit *merged_common_ancestors;
1276 struct tree *mrtree = mrtree;
1280 output(o, 4, "Merging:");
1281 output_commit_title(o, h1);
1282 output_commit_title(o, h2);
1286 ca = get_merge_bases(h1, h2, 1);
1287 ca = reverse_commit_list(ca);
1291 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1292 for (iter = ca; iter; iter = iter->next)
1293 output_commit_title(o, iter->item);
1296 merged_common_ancestors = pop_commit(&ca);
1297 if (merged_common_ancestors == NULL) {
1298 /* if there is no common ancestor, make an empty tree */
1299 struct tree *tree = xcalloc(1, sizeof(struct tree));
1301 tree->object.parsed = 1;
1302 tree->object.type = OBJ_TREE;
1303 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1304 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1307 for (iter = ca; iter; iter = iter->next) {
1308 const char *saved_b1, *saved_b2;
1311 * When the merge fails, the result contains files
1312 * with conflict markers. The cleanness flag is
1313 * ignored, it was never actually used, as result of
1314 * merge_trees has always overwritten it: the committed
1315 * "conflicts" were already resolved.
1318 saved_b1 = o->branch1;
1319 saved_b2 = o->branch2;
1320 o->branch1 = "Temporary merge branch 1";
1321 o->branch2 = "Temporary merge branch 2";
1322 merge_recursive(o, merged_common_ancestors, iter->item,
1323 NULL, &merged_common_ancestors);
1324 o->branch1 = saved_b1;
1325 o->branch2 = saved_b2;
1328 if (!merged_common_ancestors)
1329 die("merge returned no commit");
1336 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1339 if (o->call_depth) {
1340 *result = make_virtual_commit(mrtree, "merged tree");
1341 commit_list_insert(h1, &(*result)->parents);
1342 commit_list_insert(h2, &(*result)->parents->next);
1348 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1350 struct object *object;
1352 object = deref_tag(parse_object(sha1), name, strlen(name));
1355 if (object->type == OBJ_TREE)
1356 return make_virtual_commit((struct tree*)object, name);
1357 if (object->type != OBJ_COMMIT)
1359 if (parse_commit((struct commit *)object))
1361 return (struct commit *)object;
1364 int merge_recursive_generic(struct merge_options *o,
1365 const unsigned char *head,
1366 const unsigned char *merge,
1368 const unsigned char **base_list,
1369 struct commit **result)
1371 int clean, index_fd;
1372 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1373 struct commit *head_commit = get_ref(head, o->branch1);
1374 struct commit *next_commit = get_ref(merge, o->branch2);
1375 struct commit_list *ca = NULL;
1379 for (i = 0; i < num_base_list; ++i) {
1380 struct commit *base;
1381 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1382 return error("Could not parse object '%s'",
1383 sha1_to_hex(base_list[i]));
1384 commit_list_insert(base, &ca);
1388 index_fd = hold_locked_index(lock, 1);
1389 clean = merge_recursive(o, head_commit, next_commit, ca,
1391 if (active_cache_changed &&
1392 (write_cache(index_fd, active_cache, active_nr) ||
1393 commit_locked_index(lock)))
1394 return error("Unable to write index.");
1396 return clean ? 0 : 1;
1399 static int merge_recursive_config(const char *var, const char *value, void *cb)
1401 struct merge_options *o = cb;
1402 if (!strcasecmp(var, "merge.verbosity")) {
1403 o->verbosity = git_config_int(var, value);
1406 if (!strcasecmp(var, "diff.renamelimit")) {
1407 o->diff_rename_limit = git_config_int(var, value);
1410 if (!strcasecmp(var, "merge.renamelimit")) {
1411 o->merge_rename_limit = git_config_int(var, value);
1414 return git_xmerge_config(var, value, cb);
1417 void init_merge_options(struct merge_options *o)
1419 memset(o, 0, sizeof(struct merge_options));
1421 o->buffer_output = 1;
1422 o->diff_rename_limit = -1;
1423 o->merge_rename_limit = -1;
1424 git_config(merge_recursive_config, o);
1425 if (getenv("GIT_MERGE_VERBOSITY"))
1427 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1428 if (o->verbosity >= 5)
1429 o->buffer_output = 0;
1430 strbuf_init(&o->obuf, 0);
1431 memset(&o->current_file_set, 0, sizeof(struct string_list));
1432 o->current_file_set.strdup_strings = 1;
1433 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1434 o->current_directory_set.strdup_strings = 1;