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;
175 memset(&opts, 0, sizeof(opts));
182 opts.fn = threeway_merge;
183 opts.src_index = &the_index;
184 opts.dst_index = &the_index;
185 opts.msgs = get_porcelain_error_msgs();
187 init_tree_desc_from_tree(t+0, common);
188 init_tree_desc_from_tree(t+1, head);
189 init_tree_desc_from_tree(t+2, merge);
191 rc = unpack_trees(3, t, &opts);
192 cache_tree_free(&active_cache_tree);
196 struct tree *write_tree_from_memory(struct merge_options *o)
198 struct tree *result = NULL;
200 if (unmerged_cache()) {
202 output(o, 0, "There are unmerged index entries:");
203 for (i = 0; i < active_nr; i++) {
204 struct cache_entry *ce = active_cache[i];
206 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
211 if (!active_cache_tree)
212 active_cache_tree = cache_tree();
214 if (!cache_tree_fully_valid(active_cache_tree) &&
215 cache_tree_update(active_cache_tree,
216 active_cache, active_nr, 0, 0) < 0)
217 die("error building trees");
219 result = lookup_tree(active_cache_tree->sha1);
224 static int save_files_dirs(const unsigned char *sha1,
225 const char *base, int baselen, const char *path,
226 unsigned int mode, int stage, void *context)
228 int len = strlen(path);
229 char *newpath = xmalloc(baselen + len + 1);
230 struct merge_options *o = context;
232 memcpy(newpath, base, baselen);
233 memcpy(newpath + baselen, path, len);
234 newpath[baselen + len] = '\0';
237 string_list_insert(newpath, &o->current_directory_set);
239 string_list_insert(newpath, &o->current_file_set);
242 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
245 static int get_files_dirs(struct merge_options *o, struct tree *tree)
248 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
250 n = o->current_file_set.nr + o->current_directory_set.nr;
255 * Returns an index_entry instance which doesn't have to correspond to
256 * a real cache entry in Git's index.
258 static struct stage_data *insert_stage_data(const char *path,
259 struct tree *o, struct tree *a, struct tree *b,
260 struct string_list *entries)
262 struct string_list_item *item;
263 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
264 get_tree_entry(o->object.sha1, path,
265 e->stages[1].sha, &e->stages[1].mode);
266 get_tree_entry(a->object.sha1, path,
267 e->stages[2].sha, &e->stages[2].mode);
268 get_tree_entry(b->object.sha1, path,
269 e->stages[3].sha, &e->stages[3].mode);
270 item = string_list_insert(path, entries);
276 * Create a dictionary mapping file names to stage_data objects. The
277 * dictionary contains one entry for every path with a non-zero stage entry.
279 static struct string_list *get_unmerged(void)
281 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
284 unmerged->strdup_strings = 1;
286 for (i = 0; i < active_nr; i++) {
287 struct string_list_item *item;
288 struct stage_data *e;
289 struct cache_entry *ce = active_cache[i];
293 item = string_list_lookup(ce->name, unmerged);
295 item = string_list_insert(ce->name, unmerged);
296 item->util = xcalloc(1, sizeof(struct stage_data));
299 e->stages[ce_stage(ce)].mode = ce->ce_mode;
300 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
308 struct diff_filepair *pair;
309 struct stage_data *src_entry;
310 struct stage_data *dst_entry;
311 unsigned processed:1;
315 * Get information of all renames which occurred between 'o_tree' and
316 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
317 * 'b_tree') to be able to associate the correct cache entries with
318 * the rename information. 'tree' is always equal to either a_tree or b_tree.
320 static struct string_list *get_renames(struct merge_options *o,
325 struct string_list *entries)
328 struct string_list *renames;
329 struct diff_options opts;
331 renames = xcalloc(1, sizeof(struct string_list));
333 DIFF_OPT_SET(&opts, RECURSIVE);
334 opts.detect_rename = DIFF_DETECT_RENAME;
335 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
336 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
338 opts.warn_on_too_large_rename = 1;
339 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
340 if (diff_setup_done(&opts) < 0)
341 die("diff setup failed");
342 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
344 for (i = 0; i < diff_queued_diff.nr; ++i) {
345 struct string_list_item *item;
347 struct diff_filepair *pair = diff_queued_diff.queue[i];
348 if (pair->status != 'R') {
349 diff_free_filepair(pair);
352 re = xmalloc(sizeof(*re));
355 item = string_list_lookup(re->pair->one->path, entries);
357 re->src_entry = insert_stage_data(re->pair->one->path,
358 o_tree, a_tree, b_tree, entries);
360 re->src_entry = item->util;
362 item = string_list_lookup(re->pair->two->path, entries);
364 re->dst_entry = insert_stage_data(re->pair->two->path,
365 o_tree, a_tree, b_tree, entries);
367 re->dst_entry = item->util;
368 item = string_list_insert(pair->one->path, renames);
371 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
372 diff_queued_diff.nr = 0;
377 static int update_stages(const char *path, struct diff_filespec *o,
378 struct diff_filespec *a, struct diff_filespec *b,
381 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
383 if (remove_file_from_cache(path))
386 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
389 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
392 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
397 static int remove_file(struct merge_options *o, int clean,
398 const char *path, int no_wd)
400 int update_cache = o->call_depth || clean;
401 int update_working_directory = !o->call_depth && !no_wd;
404 if (remove_file_from_cache(path))
407 if (update_working_directory) {
408 if (remove_path(path) && errno != ENOENT)
414 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
416 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
419 char *p = newpath + strlen(path);
420 strcpy(newpath, path);
426 while (string_list_has_string(&o->current_file_set, newpath) ||
427 string_list_has_string(&o->current_directory_set, newpath) ||
428 lstat(newpath, &st) == 0)
429 sprintf(p, "_%d", suffix++);
431 string_list_insert(newpath, &o->current_file_set);
435 static void flush_buffer(int fd, const char *buf, unsigned long size)
438 long ret = write_in_full(fd, buf, size);
443 die_errno("merge-recursive");
445 die("merge-recursive: disk full?");
452 static int would_lose_untracked(const char *path)
454 int pos = cache_name_pos(path, strlen(path));
458 while (pos < active_nr &&
459 !strcmp(path, active_cache[pos]->name)) {
461 * If stage #0, it is definitely tracked.
462 * If it has stage #2 then it was tracked
463 * before this merge started. All other
464 * cases the path was not tracked.
466 switch (ce_stage(active_cache[pos])) {
473 return file_exists(path);
476 static int make_room_for_path(const char *path)
479 const char *msg = "failed to create path '%s'%s";
481 status = safe_create_leading_directories_const(path);
484 /* something else exists */
485 error(msg, path, ": perhaps a D/F conflict?");
492 * Do not unlink a file in the work tree if we are not
495 if (would_lose_untracked(path))
496 return error("refusing to lose untracked file at '%s'",
499 /* Successful unlink is good.. */
502 /* .. and so is no existing file */
505 /* .. but not some other error (who really cares what?) */
506 return error(msg, path, ": perhaps a D/F conflict?");
509 static void update_file_flags(struct merge_options *o,
510 const unsigned char *sha,
520 enum object_type type;
524 if (S_ISGITLINK(mode))
526 * We may later decide to recursively descend into
527 * the submodule directory and update its index
528 * and/or work tree, but we do not do that now.
532 buf = read_sha1_file(sha, &type, &size);
534 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
535 if (type != OBJ_BLOB)
536 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
538 struct strbuf strbuf = STRBUF_INIT;
539 if (convert_to_working_tree(path, buf, size, &strbuf)) {
542 buf = strbuf_detach(&strbuf, NULL);
546 if (make_room_for_path(path) < 0) {
551 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
557 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
559 die_errno("failed to open '%s'", path);
560 flush_buffer(fd, buf, size);
562 } else if (S_ISLNK(mode)) {
563 char *lnk = xmemdupz(buf, size);
564 safe_create_leading_directories_const(path);
566 if (symlink(lnk, path))
567 die_errno("failed to symlink '%s'", path);
570 die("do not know what to do with %06o %s '%s'",
571 mode, sha1_to_hex(sha), path);
576 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
579 static void update_file(struct merge_options *o,
581 const unsigned char *sha,
585 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
588 /* Low level file merging, update and removal */
590 struct merge_file_info
592 unsigned char sha[20];
598 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
601 enum object_type type;
603 if (!hashcmp(sha1, null_sha1)) {
604 mm->ptr = xstrdup("");
609 mm->ptr = read_sha1_file(sha1, &type, &size);
610 if (!mm->ptr || type != OBJ_BLOB)
611 die("unable to read blob object %s", sha1_to_hex(sha1));
615 static int merge_3way(struct merge_options *o,
616 mmbuffer_t *result_buf,
617 struct diff_filespec *one,
618 struct diff_filespec *a,
619 struct diff_filespec *b,
623 mmfile_t orig, src1, src2;
627 if (strcmp(a->path, b->path)) {
628 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
629 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
631 name1 = xstrdup(mkpath("%s", branch1));
632 name2 = xstrdup(mkpath("%s", branch2));
635 fill_mm(one->sha1, &orig);
636 fill_mm(a->sha1, &src1);
637 fill_mm(b->sha1, &src2);
639 merge_status = ll_merge(result_buf, a->path, &orig,
640 &src1, name1, &src2, name2,
651 static struct merge_file_info merge_file(struct merge_options *o,
652 struct diff_filespec *one,
653 struct diff_filespec *a,
654 struct diff_filespec *b,
658 struct merge_file_info result;
662 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
664 if (S_ISREG(a->mode)) {
665 result.mode = a->mode;
666 hashcpy(result.sha, a->sha1);
668 result.mode = b->mode;
669 hashcpy(result.sha, b->sha1);
672 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
678 if (a->mode == b->mode || a->mode == one->mode)
679 result.mode = b->mode;
681 result.mode = a->mode;
682 if (b->mode != one->mode) {
688 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
689 hashcpy(result.sha, b->sha1);
690 else if (sha_eq(b->sha1, one->sha1))
691 hashcpy(result.sha, a->sha1);
692 else if (S_ISREG(a->mode)) {
693 mmbuffer_t result_buf;
696 merge_status = merge_3way(o, &result_buf, one, a, b,
699 if ((merge_status < 0) || !result_buf.ptr)
700 die("Failed to execute internal merge");
702 if (write_sha1_file(result_buf.ptr, result_buf.size,
703 blob_type, result.sha))
704 die("Unable to add %s to database",
707 free(result_buf.ptr);
708 result.clean = (merge_status == 0);
709 } else if (S_ISGITLINK(a->mode)) {
711 hashcpy(result.sha, a->sha1);
712 } else if (S_ISLNK(a->mode)) {
713 hashcpy(result.sha, a->sha1);
715 if (!sha_eq(a->sha1, b->sha1))
718 die("unsupported object type in the tree");
725 static void conflict_rename_rename(struct merge_options *o,
733 const char *ren1_dst = ren1->pair->two->path;
734 const char *ren2_dst = ren2->pair->two->path;
735 const char *dst_name1 = ren1_dst;
736 const char *dst_name2 = ren2_dst;
737 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
738 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
739 output(o, 1, "%s is a directory in %s adding as %s instead",
740 ren1_dst, branch2, dst_name1);
741 remove_file(o, 0, ren1_dst, 0);
743 if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
744 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
745 output(o, 1, "%s is a directory in %s adding as %s instead",
746 ren2_dst, branch1, dst_name2);
747 remove_file(o, 0, ren2_dst, 0);
750 remove_file_from_cache(dst_name1);
751 remove_file_from_cache(dst_name2);
753 * Uncomment to leave the conflicting names in the resulting tree
755 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
756 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
759 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
760 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
766 static void conflict_rename_dir(struct merge_options *o,
770 char *new_path = unique_path(o, ren1->pair->two->path, branch1);
771 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
772 remove_file(o, 0, ren1->pair->two->path, 0);
773 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
777 static void conflict_rename_rename_2(struct merge_options *o,
783 char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
784 char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
785 output(o, 1, "Renaming %s to %s and %s to %s instead",
786 ren1->pair->one->path, new_path1,
787 ren2->pair->one->path, new_path2);
788 remove_file(o, 0, ren1->pair->two->path, 0);
789 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
790 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
795 static int process_renames(struct merge_options *o,
796 struct string_list *a_renames,
797 struct string_list *b_renames)
799 int clean_merge = 1, i, j;
800 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
801 const struct rename *sre;
803 for (i = 0; i < a_renames->nr; i++) {
804 sre = a_renames->items[i].util;
805 string_list_insert(sre->pair->two->path, &a_by_dst)->util
808 for (i = 0; i < b_renames->nr; i++) {
809 sre = b_renames->items[i].util;
810 string_list_insert(sre->pair->two->path, &b_by_dst)->util
814 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
816 struct string_list *renames1, *renames2Dst;
817 struct rename *ren1 = NULL, *ren2 = NULL;
818 const char *branch1, *branch2;
819 const char *ren1_src, *ren1_dst;
821 if (i >= a_renames->nr) {
822 ren2 = b_renames->items[j++].util;
823 } else if (j >= b_renames->nr) {
824 ren1 = a_renames->items[i++].util;
826 int compare = strcmp(a_renames->items[i].string,
827 b_renames->items[j].string);
829 ren1 = a_renames->items[i++].util;
831 ren2 = b_renames->items[j++].util;
834 /* TODO: refactor, so that 1/2 are not needed */
836 renames1 = a_renames;
837 renames2Dst = &b_by_dst;
838 branch1 = o->branch1;
839 branch2 = o->branch2;
842 renames1 = b_renames;
843 renames2Dst = &a_by_dst;
844 branch1 = o->branch2;
845 branch2 = o->branch1;
850 src = ren1->pair->one->path;
852 ren1->dst_entry->processed = 1;
853 ren1->src_entry->processed = 1;
859 ren1_src = ren1->pair->one->path;
860 ren1_dst = ren1->pair->two->path;
863 const char *ren2_src = ren2->pair->one->path;
864 const char *ren2_dst = ren2->pair->two->path;
865 /* Renamed in 1 and renamed in 2 */
866 if (strcmp(ren1_src, ren2_src) != 0)
867 die("ren1.src != ren2.src");
868 ren2->dst_entry->processed = 1;
870 if (strcmp(ren1_dst, ren2_dst) != 0) {
872 output(o, 1, "CONFLICT (rename/rename): "
873 "Rename \"%s\"->\"%s\" in branch \"%s\" "
874 "rename \"%s\"->\"%s\" in \"%s\"%s",
875 src, ren1_dst, branch1,
876 src, ren2_dst, branch2,
877 o->call_depth ? " (left unresolved)": "");
879 remove_file_from_cache(src);
880 update_file(o, 0, ren1->pair->one->sha1,
881 ren1->pair->one->mode, src);
883 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
885 struct merge_file_info mfi;
886 remove_file(o, 1, ren1_src, 1);
893 if (mfi.merge || !mfi.clean)
894 output(o, 1, "Renaming %s->%s", src, ren1_dst);
897 output(o, 2, "Auto-merging %s", ren1_dst);
900 output(o, 1, "CONFLICT (content): merge conflict in %s",
905 update_stages(ren1_dst,
911 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
914 /* Renamed in 1, maybe changed in 2 */
915 struct string_list_item *item;
916 /* we only use sha1 and mode of these */
917 struct diff_filespec src_other, dst_other;
918 int try_merge, stage = a_renames == renames1 ? 3: 2;
920 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
922 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
923 src_other.mode = ren1->src_entry->stages[stage].mode;
924 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
925 dst_other.mode = ren1->dst_entry->stages[stage].mode;
929 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
931 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
932 " directory %s added in %s",
933 ren1_src, ren1_dst, branch1,
935 conflict_rename_dir(o, ren1, branch1);
936 } else if (sha_eq(src_other.sha1, null_sha1)) {
938 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
940 ren1_src, ren1_dst, branch1,
942 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
944 update_stages(ren1_dst, NULL,
945 branch1 == o->branch1 ?
946 ren1->pair->two : NULL,
947 branch1 == o->branch1 ?
948 NULL : ren1->pair->two, 1);
949 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
950 const char *new_path;
953 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
955 ren1_src, ren1_dst, branch1,
958 struct merge_file_info mfi;
959 struct diff_filespec one, a, b;
961 one.path = a.path = b.path =
963 hashcpy(one.sha1, null_sha1);
965 hashcpy(a.sha1, ren1->pair->two->sha1);
966 a.mode = ren1->pair->two->mode;
967 hashcpy(b.sha1, dst_other.sha1);
968 b.mode = dst_other.mode;
969 mfi = merge_file(o, &one, &a, &b,
972 output(o, 1, "Adding merged %s", ren1_dst);
978 new_path = unique_path(o, ren1_dst, branch2);
979 output(o, 1, "Adding as %s instead", new_path);
980 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
982 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
986 output(o, 1, "CONFLICT (rename/rename): "
987 "Rename %s->%s in %s. "
988 "Rename %s->%s in %s",
989 ren1_src, ren1_dst, branch1,
990 ren2->pair->one->path, ren2->pair->two->path, branch2);
991 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
996 struct diff_filespec *one, *a, *b;
997 struct merge_file_info mfi;
998 src_other.path = (char *)ren1_src;
1000 one = ren1->pair->one;
1001 if (a_renames == renames1) {
1002 a = ren1->pair->two;
1005 b = ren1->pair->two;
1008 mfi = merge_file(o, one, a, b,
1009 o->branch1, o->branch2);
1012 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1013 mfi.mode == ren1->pair->two->mode)
1015 * This messaged is part of
1016 * t6022 test. If you change
1017 * it update the test too.
1019 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
1021 if (mfi.merge || !mfi.clean)
1022 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
1024 output(o, 2, "Auto-merging %s", ren1_dst);
1026 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
1031 update_stages(ren1_dst,
1034 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1039 string_list_clear(&a_by_dst, 0);
1040 string_list_clear(&b_by_dst, 0);
1045 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1047 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1050 /* Per entry merge function */
1051 static int process_entry(struct merge_options *o,
1052 const char *path, struct stage_data *entry)
1055 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1056 print_index_entry("\tpath: ", entry);
1058 int clean_merge = 1;
1059 unsigned o_mode = entry->stages[1].mode;
1060 unsigned a_mode = entry->stages[2].mode;
1061 unsigned b_mode = entry->stages[3].mode;
1062 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1063 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1064 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1066 if (o_sha && (!a_sha || !b_sha)) {
1067 /* Case A: Deleted in one */
1068 if ((!a_sha && !b_sha) ||
1069 (sha_eq(a_sha, o_sha) && !b_sha) ||
1070 (!a_sha && sha_eq(b_sha, o_sha))) {
1071 /* Deleted in both or deleted in one and
1072 * unchanged in the other */
1074 output(o, 2, "Removing %s", path);
1075 /* do not touch working file if it did not exist */
1076 remove_file(o, 1, path, !a_sha);
1078 /* Deleted in one and changed in the other */
1081 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1082 "and modified in %s. Version %s of %s left in tree.",
1084 o->branch2, o->branch2, path);
1085 update_file(o, 0, b_sha, b_mode, path);
1087 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1088 "and modified in %s. Version %s of %s left in tree.",
1090 o->branch1, o->branch1, path);
1091 update_file(o, 0, a_sha, a_mode, path);
1095 } else if ((!o_sha && a_sha && !b_sha) ||
1096 (!o_sha && !a_sha && b_sha)) {
1097 /* Case B: Added in one. */
1098 const char *add_branch;
1099 const char *other_branch;
1101 const unsigned char *sha;
1105 add_branch = o->branch1;
1106 other_branch = o->branch2;
1109 conf = "file/directory";
1111 add_branch = o->branch2;
1112 other_branch = o->branch1;
1115 conf = "directory/file";
1117 if (string_list_has_string(&o->current_directory_set, path)) {
1118 const char *new_path = unique_path(o, path, add_branch);
1120 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1122 conf, path, other_branch, path, new_path);
1123 remove_file(o, 0, path, 0);
1124 update_file(o, 0, sha, mode, new_path);
1126 output(o, 2, "Adding %s", path);
1127 update_file(o, 1, sha, mode, path);
1129 } else if (a_sha && b_sha) {
1130 /* Case C: Added in both (check for same permissions) and */
1131 /* case D: Modified in both, but differently. */
1132 const char *reason = "content";
1133 struct merge_file_info mfi;
1134 struct diff_filespec one, a, b;
1138 o_sha = (unsigned char *)null_sha1;
1140 output(o, 2, "Auto-merging %s", path);
1141 one.path = a.path = b.path = (char *)path;
1142 hashcpy(one.sha1, o_sha);
1144 hashcpy(a.sha1, a_sha);
1146 hashcpy(b.sha1, b_sha);
1149 mfi = merge_file(o, &one, &a, &b,
1150 o->branch1, o->branch2);
1152 clean_merge = mfi.clean;
1154 if (S_ISGITLINK(mfi.mode))
1155 reason = "submodule";
1156 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1159 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1160 } else if (!o_sha && !a_sha && !b_sha) {
1162 * this entry was deleted altogether. a_mode == 0 means
1163 * we had that path and want to actively remove it.
1165 remove_file(o, 1, path, !a_mode);
1167 die("Fatal merge failure, shouldn't happen.");
1172 struct unpack_trees_error_msgs get_porcelain_error_msgs(void)
1174 struct unpack_trees_error_msgs msgs = {
1175 /* would_overwrite */
1176 "Your local changes to '%s' would be overwritten by merge. Aborting.",
1177 /* not_uptodate_file */
1178 "Your local changes to '%s' would be overwritten by merge. Aborting.",
1179 /* not_uptodate_dir */
1180 "Updating '%s' would lose untracked files in it. Aborting.",
1181 /* would_lose_untracked */
1182 "Untracked working tree file '%s' would be %s by merge. Aborting",
1183 /* bind_overlap -- will not happen here */
1186 if (advice_commit_before_merge) {
1187 msgs.would_overwrite = msgs.not_uptodate_file =
1188 "Your local changes to '%s' would be overwritten by merge. Aborting.\n"
1189 "Please, commit your changes or stash them before you can merge.";
1194 int merge_trees(struct merge_options *o,
1197 struct tree *common,
1198 struct tree **result)
1202 if (o->subtree_merge) {
1203 merge = shift_tree_object(head, merge);
1204 common = shift_tree_object(head, common);
1207 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1208 output(o, 0, "Already uptodate!");
1213 code = git_merge_trees(o->call_depth, common, head, merge);
1216 if (show(o, 4) || o->call_depth)
1217 die("merging of trees %s and %s failed",
1218 sha1_to_hex(head->object.sha1),
1219 sha1_to_hex(merge->object.sha1));
1224 if (unmerged_cache()) {
1225 struct string_list *entries, *re_head, *re_merge;
1227 string_list_clear(&o->current_file_set, 1);
1228 string_list_clear(&o->current_directory_set, 1);
1229 get_files_dirs(o, head);
1230 get_files_dirs(o, merge);
1232 entries = get_unmerged();
1233 re_head = get_renames(o, head, common, head, merge, entries);
1234 re_merge = get_renames(o, merge, common, head, merge, entries);
1235 clean = process_renames(o, re_head, re_merge);
1236 for (i = 0; i < entries->nr; i++) {
1237 const char *path = entries->items[i].string;
1238 struct stage_data *e = entries->items[i].util;
1240 && !process_entry(o, path, e))
1244 string_list_clear(re_merge, 0);
1245 string_list_clear(re_head, 0);
1246 string_list_clear(entries, 1);
1253 *result = write_tree_from_memory(o);
1258 static struct commit_list *reverse_commit_list(struct commit_list *list)
1260 struct commit_list *next = NULL, *current, *backup;
1261 for (current = list; current; current = backup) {
1262 backup = current->next;
1263 current->next = next;
1270 * Merge the commits h1 and h2, return the resulting virtual
1271 * commit object and a flag indicating the cleanness of the merge.
1273 int merge_recursive(struct merge_options *o,
1276 struct commit_list *ca,
1277 struct commit **result)
1279 struct commit_list *iter;
1280 struct commit *merged_common_ancestors;
1281 struct tree *mrtree = mrtree;
1285 output(o, 4, "Merging:");
1286 output_commit_title(o, h1);
1287 output_commit_title(o, h2);
1291 ca = get_merge_bases(h1, h2, 1);
1292 ca = reverse_commit_list(ca);
1296 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1297 for (iter = ca; iter; iter = iter->next)
1298 output_commit_title(o, iter->item);
1301 merged_common_ancestors = pop_commit(&ca);
1302 if (merged_common_ancestors == NULL) {
1303 /* if there is no common ancestor, make an empty tree */
1304 struct tree *tree = xcalloc(1, sizeof(struct tree));
1306 tree->object.parsed = 1;
1307 tree->object.type = OBJ_TREE;
1308 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1309 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1312 for (iter = ca; iter; iter = iter->next) {
1313 const char *saved_b1, *saved_b2;
1316 * When the merge fails, the result contains files
1317 * with conflict markers. The cleanness flag is
1318 * ignored, it was never actually used, as result of
1319 * merge_trees has always overwritten it: the committed
1320 * "conflicts" were already resolved.
1323 saved_b1 = o->branch1;
1324 saved_b2 = o->branch2;
1325 o->branch1 = "Temporary merge branch 1";
1326 o->branch2 = "Temporary merge branch 2";
1327 merge_recursive(o, merged_common_ancestors, iter->item,
1328 NULL, &merged_common_ancestors);
1329 o->branch1 = saved_b1;
1330 o->branch2 = saved_b2;
1333 if (!merged_common_ancestors)
1334 die("merge returned no commit");
1341 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1344 if (o->call_depth) {
1345 *result = make_virtual_commit(mrtree, "merged tree");
1346 commit_list_insert(h1, &(*result)->parents);
1347 commit_list_insert(h2, &(*result)->parents->next);
1353 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1355 struct object *object;
1357 object = deref_tag(parse_object(sha1), name, strlen(name));
1360 if (object->type == OBJ_TREE)
1361 return make_virtual_commit((struct tree*)object, name);
1362 if (object->type != OBJ_COMMIT)
1364 if (parse_commit((struct commit *)object))
1366 return (struct commit *)object;
1369 int merge_recursive_generic(struct merge_options *o,
1370 const unsigned char *head,
1371 const unsigned char *merge,
1373 const unsigned char **base_list,
1374 struct commit **result)
1376 int clean, index_fd;
1377 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1378 struct commit *head_commit = get_ref(head, o->branch1);
1379 struct commit *next_commit = get_ref(merge, o->branch2);
1380 struct commit_list *ca = NULL;
1384 for (i = 0; i < num_base_list; ++i) {
1385 struct commit *base;
1386 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1387 return error("Could not parse object '%s'",
1388 sha1_to_hex(base_list[i]));
1389 commit_list_insert(base, &ca);
1393 index_fd = hold_locked_index(lock, 1);
1394 clean = merge_recursive(o, head_commit, next_commit, ca,
1396 if (active_cache_changed &&
1397 (write_cache(index_fd, active_cache, active_nr) ||
1398 commit_locked_index(lock)))
1399 return error("Unable to write index.");
1401 return clean ? 0 : 1;
1404 static int merge_recursive_config(const char *var, const char *value, void *cb)
1406 struct merge_options *o = cb;
1407 if (!strcasecmp(var, "merge.verbosity")) {
1408 o->verbosity = git_config_int(var, value);
1411 if (!strcasecmp(var, "diff.renamelimit")) {
1412 o->diff_rename_limit = git_config_int(var, value);
1415 if (!strcasecmp(var, "merge.renamelimit")) {
1416 o->merge_rename_limit = git_config_int(var, value);
1419 return git_xmerge_config(var, value, cb);
1422 void init_merge_options(struct merge_options *o)
1424 memset(o, 0, sizeof(struct merge_options));
1426 o->buffer_output = 1;
1427 o->diff_rename_limit = -1;
1428 o->merge_rename_limit = -1;
1429 git_config(merge_recursive_config, o);
1430 if (getenv("GIT_MERGE_VERBOSITY"))
1432 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1433 if (o->verbosity >= 5)
1434 o->buffer_output = 0;
1435 strbuf_init(&o->obuf, 0);
1436 memset(&o->current_file_set, 0, sizeof(struct string_list));
1437 o->current_file_set.strdup_strings = 1;
1438 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1439 o->current_directory_set.strdup_strings = 1;