2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
7 #include "cache-tree.h"
11 #include "tree-walk.h"
15 #include "unpack-trees.h"
16 #include "string-list.h"
17 #include "xdiff-interface.h"
19 #include "interpolate.h"
21 #include "merge-recursive.h"
23 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
25 unsigned char shifted[20];
28 * NEEDSWORK: this limits the recursion depth to hardcoded
29 * value '2' to avoid excessive overhead.
31 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
32 if (!hashcmp(two->object.sha1, shifted))
34 return lookup_tree(shifted);
38 * A virtual commit has
39 * - (const char *)commit->util set to the name, and
40 * - *(int *)commit->object.sha1 set to the virtual id.
43 struct commit *make_virtual_commit(struct tree *tree, const char *comment)
45 struct commit *commit = xcalloc(1, sizeof(struct commit));
46 static unsigned virtual_id = 1;
48 commit->util = (void*)comment;
49 *(int*)commit->object.sha1 = virtual_id++;
51 commit->object.parsed = 1;
56 * Since we use get_tree_entry(), which does not put the read object into
57 * the object pool, we cannot rely on a == b.
59 static int sha_eq(const unsigned char *a, const unsigned char *b)
63 return a && b && hashcmp(a, b) == 0;
67 * Since we want to write the index eventually, we cannot reuse the index
68 * for these (temporary) data.
75 unsigned char sha[20];
80 static struct string_list current_file_set = {NULL, 0, 0, 1};
81 static struct string_list current_directory_set = {NULL, 0, 0, 1};
83 static int call_depth = 0;
84 static struct strbuf obuf = STRBUF_INIT;
86 static int show(struct merge_options *o, int v)
88 return (!call_depth && o->verbosity >= v) || o->verbosity >= 5;
91 static void flush_output(void)
94 fputs(obuf.buf, stdout);
99 static void output(struct merge_options *o, int v, const char *fmt, ...)
107 strbuf_grow(&obuf, call_depth * 2 + 2);
108 memset(obuf.buf + obuf.len, ' ', call_depth * 2);
109 strbuf_setlen(&obuf, obuf.len + call_depth * 2);
112 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
117 if (len >= strbuf_avail(&obuf)) {
118 strbuf_grow(&obuf, len + 2);
120 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
122 if (len >= strbuf_avail(&obuf)) {
123 die("this should not happen, your snprintf is broken");
126 strbuf_setlen(&obuf, obuf.len + len);
127 strbuf_add(&obuf, "\n", 1);
128 if (!o->buffer_output)
132 static void output_commit_title(struct commit *commit)
136 for (i = call_depth; i--;)
139 printf("virtual %s\n", (char *)commit->util);
141 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
142 if (parse_commit(commit) != 0)
143 printf("(bad commit)\n");
147 for (s = commit->buffer; *s; s++)
148 if (*s == '\n' && s[1] == '\n') {
152 for (len = 0; s[len] && '\n' != s[len]; len++)
154 printf("%.*s\n", len, s);
159 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
160 const char *path, int stage, int refresh, int options)
162 struct cache_entry *ce;
163 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
165 return error("addinfo_cache failed for path '%s'", path);
166 return add_cache_entry(ce, options);
170 * This is a global variable which is used in a number of places but
171 * only written to in the 'merge' function.
173 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
174 * don't update the working directory.
175 * 0 => Leave unmerged entries in the cache and update
176 * the working directory.
178 static int index_only = 0;
180 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
183 init_tree_desc(desc, tree->buffer, tree->size);
186 static int git_merge_trees(int index_only,
192 struct tree_desc t[3];
193 struct unpack_trees_options opts;
195 memset(&opts, 0, sizeof(opts));
202 opts.fn = threeway_merge;
203 opts.src_index = &the_index;
204 opts.dst_index = &the_index;
206 init_tree_desc_from_tree(t+0, common);
207 init_tree_desc_from_tree(t+1, head);
208 init_tree_desc_from_tree(t+2, merge);
210 rc = unpack_trees(3, t, &opts);
211 cache_tree_free(&active_cache_tree);
215 struct tree *write_tree_from_memory(struct merge_options *o)
217 struct tree *result = NULL;
219 if (unmerged_cache()) {
221 output(o, 0, "There are unmerged index entries:");
222 for (i = 0; i < active_nr; i++) {
223 struct cache_entry *ce = active_cache[i];
225 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
230 if (!active_cache_tree)
231 active_cache_tree = cache_tree();
233 if (!cache_tree_fully_valid(active_cache_tree) &&
234 cache_tree_update(active_cache_tree,
235 active_cache, active_nr, 0, 0) < 0)
236 die("error building trees");
238 result = lookup_tree(active_cache_tree->sha1);
243 static int save_files_dirs(const unsigned char *sha1,
244 const char *base, int baselen, const char *path,
245 unsigned int mode, int stage, void *context)
247 int len = strlen(path);
248 char *newpath = xmalloc(baselen + len + 1);
249 memcpy(newpath, base, baselen);
250 memcpy(newpath + baselen, path, len);
251 newpath[baselen + len] = '\0';
254 string_list_insert(newpath, ¤t_directory_set);
256 string_list_insert(newpath, ¤t_file_set);
259 return READ_TREE_RECURSIVE;
262 static int get_files_dirs(struct tree *tree)
265 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL))
267 n = current_file_set.nr + 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_path(const char *name)
422 dirs = xstrdup(name);
423 while ((slash = strrchr(name, '/'))) {
425 if (rmdir(name) != 0)
432 static int remove_file(int clean, const char *path, int no_wd)
434 int update_cache = index_only || clean;
435 int update_working_directory = !index_only && !no_wd;
438 if (remove_file_from_cache(path))
441 if (update_working_directory) {
443 if (errno != ENOENT || errno != EISDIR)
450 static char *unique_path(const char *path, const char *branch)
452 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
455 char *p = newpath + strlen(path);
456 strcpy(newpath, path);
462 while (string_list_has_string(¤t_file_set, newpath) ||
463 string_list_has_string(¤t_directory_set, newpath) ||
464 lstat(newpath, &st) == 0)
465 sprintf(p, "_%d", suffix++);
467 string_list_insert(newpath, ¤t_file_set);
471 static void flush_buffer(int fd, const char *buf, unsigned long size)
474 long ret = write_in_full(fd, buf, size);
479 die("merge-recursive: %s", strerror(errno));
481 die("merge-recursive: disk full?");
488 static int make_room_for_path(const char *path)
491 const char *msg = "failed to create path '%s'%s";
493 status = safe_create_leading_directories_const(path);
496 /* something else exists */
497 error(msg, path, ": perhaps a D/F conflict?");
503 /* Successful unlink is good.. */
506 /* .. and so is no existing file */
509 /* .. but not some other error (who really cares what?) */
510 return error(msg, path, ": perhaps a D/F conflict?");
513 static void update_file_flags(const unsigned char *sha,
523 enum object_type type;
527 if (S_ISGITLINK(mode))
528 die("cannot read object %s '%s': It is a submodule!",
529 sha1_to_hex(sha), path);
531 buf = read_sha1_file(sha, &type, &size);
533 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
534 if (type != OBJ_BLOB)
535 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
537 struct strbuf strbuf;
538 strbuf_init(&strbuf, 0);
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("failed to open %s: %s", path, strerror(errno));
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);
569 die("do not know what to do with %06o %s '%s'",
570 mode, sha1_to_hex(sha), path);
575 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
578 static void update_file(int clean,
579 const unsigned char *sha,
583 update_file_flags(sha, mode, path, index_only || clean, !index_only);
586 /* Low level file merging, update and removal */
588 struct merge_file_info
590 unsigned char sha[20];
596 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
599 enum object_type type;
601 if (!hashcmp(sha1, null_sha1)) {
602 mm->ptr = xstrdup("");
607 mm->ptr = read_sha1_file(sha1, &type, &size);
608 if (!mm->ptr || type != OBJ_BLOB)
609 die("unable to read blob object %s", sha1_to_hex(sha1));
613 static int merge_3way(mmbuffer_t *result_buf,
614 struct diff_filespec *o,
615 struct diff_filespec *a,
616 struct diff_filespec *b,
620 mmfile_t orig, src1, src2;
624 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
625 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
627 fill_mm(o->sha1, &orig);
628 fill_mm(a->sha1, &src1);
629 fill_mm(b->sha1, &src2);
631 merge_status = ll_merge(result_buf, a->path, &orig,
632 &src1, name1, &src2, name2,
643 static struct merge_file_info merge_file(struct diff_filespec *o,
644 struct diff_filespec *a, struct diff_filespec *b,
645 const char *branch1, const char *branch2)
647 struct merge_file_info result;
651 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
653 if (S_ISREG(a->mode)) {
654 result.mode = a->mode;
655 hashcpy(result.sha, a->sha1);
657 result.mode = b->mode;
658 hashcpy(result.sha, b->sha1);
661 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
667 if (a->mode == b->mode || a->mode == o->mode)
668 result.mode = b->mode;
670 result.mode = a->mode;
671 if (b->mode != o->mode) {
677 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, o->sha1))
678 hashcpy(result.sha, b->sha1);
679 else if (sha_eq(b->sha1, o->sha1))
680 hashcpy(result.sha, a->sha1);
681 else if (S_ISREG(a->mode)) {
682 mmbuffer_t result_buf;
685 merge_status = merge_3way(&result_buf, o, a, b,
688 if ((merge_status < 0) || !result_buf.ptr)
689 die("Failed to execute internal merge");
691 if (write_sha1_file(result_buf.ptr, result_buf.size,
692 blob_type, result.sha))
693 die("Unable to add %s to database",
696 free(result_buf.ptr);
697 result.clean = (merge_status == 0);
698 } else if (S_ISGITLINK(a->mode)) {
700 hashcpy(result.sha, a->sha1);
701 } else if (S_ISLNK(a->mode)) {
702 hashcpy(result.sha, a->sha1);
704 if (!sha_eq(a->sha1, b->sha1))
707 die("unsupported object type in the tree");
714 static void conflict_rename_rename(struct merge_options *o,
722 const char *ren1_dst = ren1->pair->two->path;
723 const char *ren2_dst = ren2->pair->two->path;
724 const char *dst_name1 = ren1_dst;
725 const char *dst_name2 = ren2_dst;
726 if (string_list_has_string(¤t_directory_set, ren1_dst)) {
727 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
728 output(o, 1, "%s is a directory in %s adding as %s instead",
729 ren1_dst, branch2, dst_name1);
730 remove_file(0, ren1_dst, 0);
732 if (string_list_has_string(¤t_directory_set, ren2_dst)) {
733 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
734 output(o, 1, "%s is a directory in %s adding as %s instead",
735 ren2_dst, branch1, dst_name2);
736 remove_file(0, ren2_dst, 0);
739 remove_file_from_cache(dst_name1);
740 remove_file_from_cache(dst_name2);
742 * Uncomment to leave the conflicting names in the resulting tree
744 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
745 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
748 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
749 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
755 static void conflict_rename_dir(struct merge_options *o,
759 char *new_path = unique_path(ren1->pair->two->path, branch1);
760 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
761 remove_file(0, ren1->pair->two->path, 0);
762 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
766 static void conflict_rename_rename_2(struct merge_options *o,
772 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
773 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
774 output(o, 1, "Renaming %s to %s and %s to %s instead",
775 ren1->pair->one->path, new_path1,
776 ren2->pair->one->path, new_path2);
777 remove_file(0, ren1->pair->two->path, 0);
778 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
779 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
784 static int process_renames(struct merge_options *o,
785 struct string_list *a_renames,
786 struct string_list *b_renames)
788 int clean_merge = 1, i, j;
789 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
790 const struct rename *sre;
792 for (i = 0; i < a_renames->nr; i++) {
793 sre = a_renames->items[i].util;
794 string_list_insert(sre->pair->two->path, &a_by_dst)->util
797 for (i = 0; i < b_renames->nr; i++) {
798 sre = b_renames->items[i].util;
799 string_list_insert(sre->pair->two->path, &b_by_dst)->util
803 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
806 struct string_list *renames1, *renames2, *renames2Dst;
807 struct rename *ren1 = NULL, *ren2 = NULL;
808 const char *branch1, *branch2;
809 const char *ren1_src, *ren1_dst;
811 if (i >= a_renames->nr) {
813 ren2 = b_renames->items[j++].util;
814 } else if (j >= b_renames->nr) {
816 ren1 = a_renames->items[i++].util;
818 compare = strcmp(a_renames->items[i].string,
819 b_renames->items[j].string);
821 ren1 = a_renames->items[i++].util;
823 ren2 = b_renames->items[j++].util;
826 /* TODO: refactor, so that 1/2 are not needed */
828 renames1 = a_renames;
829 renames2 = b_renames;
830 renames2Dst = &b_by_dst;
831 branch1 = o->branch1;
832 branch2 = o->branch2;
835 renames1 = b_renames;
836 renames2 = a_renames;
837 renames2Dst = &a_by_dst;
838 branch1 = o->branch2;
839 branch2 = o->branch1;
844 src = ren1->pair->one->path;
846 ren1->dst_entry->processed = 1;
847 ren1->src_entry->processed = 1;
853 ren1_src = ren1->pair->one->path;
854 ren1_dst = ren1->pair->two->path;
857 const char *ren2_src = ren2->pair->one->path;
858 const char *ren2_dst = ren2->pair->two->path;
859 /* Renamed in 1 and renamed in 2 */
860 if (strcmp(ren1_src, ren2_src) != 0)
861 die("ren1.src != ren2.src");
862 ren2->dst_entry->processed = 1;
864 if (strcmp(ren1_dst, ren2_dst) != 0) {
866 output(o, 1, "CONFLICT (rename/rename): "
867 "Rename \"%s\"->\"%s\" in branch \"%s\" "
868 "rename \"%s\"->\"%s\" in \"%s\"%s",
869 src, ren1_dst, branch1,
870 src, ren2_dst, branch2,
871 index_only ? " (left unresolved)": "");
873 remove_file_from_cache(src);
874 update_file(0, ren1->pair->one->sha1,
875 ren1->pair->one->mode, src);
877 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
879 struct merge_file_info mfi;
880 remove_file(1, ren1_src, 1);
881 mfi = merge_file(ren1->pair->one,
886 if (mfi.merge || !mfi.clean)
887 output(o, 1, "Renaming %s->%s", src, ren1_dst);
890 output(o, 2, "Auto-merging %s", ren1_dst);
893 output(o, 1, "CONFLICT (content): merge conflict in %s",
898 update_stages(ren1_dst,
904 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
907 /* Renamed in 1, maybe changed in 2 */
908 struct string_list_item *item;
909 /* we only use sha1 and mode of these */
910 struct diff_filespec src_other, dst_other;
911 int try_merge, stage = a_renames == renames1 ? 3: 2;
913 remove_file(1, ren1_src, index_only || stage == 3);
915 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
916 src_other.mode = ren1->src_entry->stages[stage].mode;
917 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
918 dst_other.mode = ren1->dst_entry->stages[stage].mode;
922 if (string_list_has_string(¤t_directory_set, ren1_dst)) {
924 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
925 " directory %s added in %s",
926 ren1_src, ren1_dst, branch1,
928 conflict_rename_dir(o, ren1, branch1);
929 } else if (sha_eq(src_other.sha1, null_sha1)) {
931 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
933 ren1_src, ren1_dst, branch1,
935 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
936 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
937 const char *new_path;
940 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
942 ren1_src, ren1_dst, branch1,
944 new_path = unique_path(ren1_dst, branch2);
945 output(o, 1, "Adding as %s instead", new_path);
946 update_file(0, dst_other.sha1, dst_other.mode, new_path);
947 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
951 output(o, 1, "CONFLICT (rename/rename): "
952 "Rename %s->%s in %s. "
953 "Rename %s->%s in %s",
954 ren1_src, ren1_dst, branch1,
955 ren2->pair->one->path, ren2->pair->two->path, branch2);
956 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
961 struct diff_filespec *one, *a, *b;
962 struct merge_file_info mfi;
963 src_other.path = (char *)ren1_src;
965 one = ren1->pair->one;
966 if (a_renames == renames1) {
973 mfi = merge_file(one, a, b,
974 o->branch1, o->branch2);
977 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
978 mfi.mode == ren1->pair->two->mode)
980 * This messaged is part of
981 * t6022 test. If you change
982 * it update the test too.
984 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
986 if (mfi.merge || !mfi.clean)
987 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
989 output(o, 2, "Auto-merging %s", ren1_dst);
991 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
996 update_stages(ren1_dst,
999 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1004 string_list_clear(&a_by_dst, 0);
1005 string_list_clear(&b_by_dst, 0);
1010 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1012 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1015 /* Per entry merge function */
1016 static int process_entry(struct merge_options *o,
1017 const char *path, struct stage_data *entry)
1020 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1021 print_index_entry("\tpath: ", entry);
1023 int clean_merge = 1;
1024 unsigned o_mode = entry->stages[1].mode;
1025 unsigned a_mode = entry->stages[2].mode;
1026 unsigned b_mode = entry->stages[3].mode;
1027 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1028 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1029 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1031 if (o_sha && (!a_sha || !b_sha)) {
1032 /* Case A: Deleted in one */
1033 if ((!a_sha && !b_sha) ||
1034 (sha_eq(a_sha, o_sha) && !b_sha) ||
1035 (!a_sha && sha_eq(b_sha, o_sha))) {
1036 /* Deleted in both or deleted in one and
1037 * unchanged in the other */
1039 output(o, 2, "Removing %s", path);
1040 /* do not touch working file if it did not exist */
1041 remove_file(1, path, !a_sha);
1043 /* Deleted in one and changed in the other */
1046 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1047 "and modified in %s. Version %s of %s left in tree.",
1049 o->branch2, o->branch2, path);
1050 update_file(0, b_sha, b_mode, path);
1052 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1053 "and modified in %s. Version %s of %s left in tree.",
1055 o->branch1, o->branch1, path);
1056 update_file(0, a_sha, a_mode, path);
1060 } else if ((!o_sha && a_sha && !b_sha) ||
1061 (!o_sha && !a_sha && b_sha)) {
1062 /* Case B: Added in one. */
1063 const char *add_branch;
1064 const char *other_branch;
1066 const unsigned char *sha;
1070 add_branch = o->branch1;
1071 other_branch = o->branch2;
1074 conf = "file/directory";
1076 add_branch = o->branch2;
1077 other_branch = o->branch1;
1080 conf = "directory/file";
1082 if (string_list_has_string(¤t_directory_set, path)) {
1083 const char *new_path = unique_path(path, add_branch);
1085 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1087 conf, path, other_branch, path, new_path);
1088 remove_file(0, path, 0);
1089 update_file(0, sha, mode, new_path);
1091 output(o, 2, "Adding %s", path);
1092 update_file(1, sha, mode, path);
1094 } else if (a_sha && b_sha) {
1095 /* Case C: Added in both (check for same permissions) and */
1096 /* case D: Modified in both, but differently. */
1097 const char *reason = "content";
1098 struct merge_file_info mfi;
1099 struct diff_filespec one, a, b;
1103 o_sha = (unsigned char *)null_sha1;
1105 output(o, 2, "Auto-merging %s", path);
1106 one.path = a.path = b.path = (char *)path;
1107 hashcpy(one.sha1, o_sha);
1109 hashcpy(a.sha1, a_sha);
1111 hashcpy(b.sha1, b_sha);
1114 mfi = merge_file(&one, &a, &b,
1115 o->branch1, o->branch2);
1117 clean_merge = mfi.clean;
1119 update_file(1, mfi.sha, mfi.mode, path);
1120 else if (S_ISGITLINK(mfi.mode))
1121 output(o, 1, "CONFLICT (submodule): Merge conflict in %s "
1122 "- needs %s", path, sha1_to_hex(b.sha1));
1124 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1128 update_file(0, mfi.sha, mfi.mode, path);
1130 update_file_flags(mfi.sha, mfi.mode, path,
1131 0 /* update_cache */, 1 /* update_working_directory */);
1133 } else if (!o_sha && !a_sha && !b_sha) {
1135 * this entry was deleted altogether. a_mode == 0 means
1136 * we had that path and want to actively remove it.
1138 remove_file(1, path, !a_mode);
1140 die("Fatal merge failure, shouldn't happen.");
1145 int merge_trees(struct merge_options *o,
1148 struct tree *common,
1149 struct tree **result)
1153 if (o->subtree_merge) {
1154 merge = shift_tree_object(head, merge);
1155 common = shift_tree_object(head, common);
1158 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1159 output(o, 0, "Already uptodate!");
1164 code = git_merge_trees(index_only, common, head, merge);
1167 die("merging of trees %s and %s failed",
1168 sha1_to_hex(head->object.sha1),
1169 sha1_to_hex(merge->object.sha1));
1171 if (unmerged_cache()) {
1172 struct string_list *entries, *re_head, *re_merge;
1174 string_list_clear(¤t_file_set, 1);
1175 string_list_clear(¤t_directory_set, 1);
1176 get_files_dirs(head);
1177 get_files_dirs(merge);
1179 entries = get_unmerged();
1180 re_head = get_renames(o, head, common, head, merge, entries);
1181 re_merge = get_renames(o, merge, common, head, merge, entries);
1182 clean = process_renames(o, re_head, re_merge);
1183 for (i = 0; i < entries->nr; i++) {
1184 const char *path = entries->items[i].string;
1185 struct stage_data *e = entries->items[i].util;
1187 && !process_entry(o, path, e))
1191 string_list_clear(re_merge, 0);
1192 string_list_clear(re_head, 0);
1193 string_list_clear(entries, 1);
1200 *result = write_tree_from_memory(o);
1205 static struct commit_list *reverse_commit_list(struct commit_list *list)
1207 struct commit_list *next = NULL, *current, *backup;
1208 for (current = list; current; current = backup) {
1209 backup = current->next;
1210 current->next = next;
1217 * Merge the commits h1 and h2, return the resulting virtual
1218 * commit object and a flag indicating the cleanness of the merge.
1220 int merge_recursive(struct merge_options *o,
1223 struct commit_list *ca,
1224 struct commit **result)
1226 struct commit_list *iter;
1227 struct commit *merged_common_ancestors;
1228 struct tree *mrtree = mrtree;
1232 output(o, 4, "Merging:");
1233 output_commit_title(h1);
1234 output_commit_title(h2);
1238 ca = get_merge_bases(h1, h2, 1);
1239 ca = reverse_commit_list(ca);
1243 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1244 for (iter = ca; iter; iter = iter->next)
1245 output_commit_title(iter->item);
1248 merged_common_ancestors = pop_commit(&ca);
1249 if (merged_common_ancestors == NULL) {
1250 /* if there is no common ancestor, make an empty tree */
1251 struct tree *tree = xcalloc(1, sizeof(struct tree));
1253 tree->object.parsed = 1;
1254 tree->object.type = OBJ_TREE;
1255 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1256 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1259 for (iter = ca; iter; iter = iter->next) {
1260 const char *saved_b1, *saved_b2;
1263 * When the merge fails, the result contains files
1264 * with conflict markers. The cleanness flag is
1265 * ignored, it was never actually used, as result of
1266 * merge_trees has always overwritten it: the committed
1267 * "conflicts" were already resolved.
1270 saved_b1 = o->branch1;
1271 saved_b2 = o->branch2;
1272 o->branch1 = "Temporary merge branch 1";
1273 o->branch2 = "Temporary merge branch 2";
1274 merge_recursive(o, merged_common_ancestors, iter->item,
1275 NULL, &merged_common_ancestors);
1276 o->branch1 = saved_b1;
1277 o->branch2 = saved_b2;
1280 if (!merged_common_ancestors)
1281 die("merge returned no commit");
1291 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1295 *result = make_virtual_commit(mrtree, "merged tree");
1296 commit_list_insert(h1, &(*result)->parents);
1297 commit_list_insert(h2, &(*result)->parents->next);
1303 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1305 struct object *object;
1307 object = deref_tag(parse_object(sha1), name, strlen(name));
1310 if (object->type == OBJ_TREE)
1311 return make_virtual_commit((struct tree*)object, name);
1312 if (object->type != OBJ_COMMIT)
1314 if (parse_commit((struct commit *)object))
1316 return (struct commit *)object;
1319 int merge_recursive_generic(struct merge_options *o,
1320 const unsigned char *head,
1321 const unsigned char *merge,
1323 const unsigned char **base_list,
1324 struct commit **result)
1326 int clean, index_fd;
1327 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1328 struct commit *head_commit = get_ref(head, o->branch1);
1329 struct commit *next_commit = get_ref(merge, o->branch2);
1330 struct commit_list *ca = NULL;
1334 for (i = 0; i < num_base_list; ++i) {
1335 struct commit *base;
1336 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1337 return error("Could not parse object '%s'",
1338 sha1_to_hex(base_list[i]));
1339 commit_list_insert(base, &ca);
1343 index_fd = hold_locked_index(lock, 1);
1344 clean = merge_recursive(o, head_commit, next_commit, ca,
1346 if (active_cache_changed &&
1347 (write_cache(index_fd, active_cache, active_nr) ||
1348 commit_locked_index(lock)))
1349 return error("Unable to write index.");
1351 return clean ? 0 : 1;
1354 static int merge_recursive_config(const char *var, const char *value, void *cb)
1356 struct merge_options *o = cb;
1357 if (!strcasecmp(var, "merge.verbosity")) {
1358 o->verbosity = git_config_int(var, value);
1361 if (!strcasecmp(var, "diff.renamelimit")) {
1362 o->diff_rename_limit = git_config_int(var, value);
1365 if (!strcasecmp(var, "merge.renamelimit")) {
1366 o->merge_rename_limit = git_config_int(var, value);
1369 return git_default_config(var, value, cb);
1372 void init_merge_options(struct merge_options *o)
1374 memset(o, 0, sizeof(struct merge_options));
1376 o->buffer_output = 1;
1377 o->diff_rename_limit = -1;
1378 o->merge_rename_limit = -1;
1379 git_config(merge_recursive_config, o);
1380 if (getenv("GIT_MERGE_VERBOSITY"))
1382 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1383 if (o->verbosity >= 5)
1384 o->buffer_output = 0;