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 struct strbuf obuf = STRBUF_INIT;
85 static int show(struct merge_options *o, int v)
87 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
90 static void flush_output(void)
93 fputs(obuf.buf, stdout);
98 static void output(struct merge_options *o, int v, const char *fmt, ...)
106 strbuf_grow(&obuf, o->call_depth * 2 + 2);
107 memset(obuf.buf + obuf.len, ' ', o->call_depth * 2);
108 strbuf_setlen(&obuf, obuf.len + o->call_depth * 2);
111 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
116 if (len >= strbuf_avail(&obuf)) {
117 strbuf_grow(&obuf, len + 2);
119 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
121 if (len >= strbuf_avail(&obuf)) {
122 die("this should not happen, your snprintf is broken");
125 strbuf_setlen(&obuf, obuf.len + len);
126 strbuf_add(&obuf, "\n", 1);
127 if (!o->buffer_output)
131 static void output_commit_title(struct merge_options *o, struct commit *commit)
135 for (i = o->call_depth; i--;)
138 printf("virtual %s\n", (char *)commit->util);
140 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
141 if (parse_commit(commit) != 0)
142 printf("(bad commit)\n");
146 for (s = commit->buffer; *s; s++)
147 if (*s == '\n' && s[1] == '\n') {
151 for (len = 0; s[len] && '\n' != s[len]; len++)
153 printf("%.*s\n", len, s);
158 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
159 const char *path, int stage, int refresh, int options)
161 struct cache_entry *ce;
162 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
164 return error("addinfo_cache failed for path '%s'", path);
165 return add_cache_entry(ce, options);
168 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
171 init_tree_desc(desc, tree->buffer, tree->size);
174 static int git_merge_trees(int index_only,
180 struct tree_desc t[3];
181 struct unpack_trees_options opts;
183 memset(&opts, 0, sizeof(opts));
190 opts.fn = threeway_merge;
191 opts.src_index = &the_index;
192 opts.dst_index = &the_index;
194 init_tree_desc_from_tree(t+0, common);
195 init_tree_desc_from_tree(t+1, head);
196 init_tree_desc_from_tree(t+2, merge);
198 rc = unpack_trees(3, t, &opts);
199 cache_tree_free(&active_cache_tree);
203 struct tree *write_tree_from_memory(struct merge_options *o)
205 struct tree *result = NULL;
207 if (unmerged_cache()) {
209 output(o, 0, "There are unmerged index entries:");
210 for (i = 0; i < active_nr; i++) {
211 struct cache_entry *ce = active_cache[i];
213 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
218 if (!active_cache_tree)
219 active_cache_tree = cache_tree();
221 if (!cache_tree_fully_valid(active_cache_tree) &&
222 cache_tree_update(active_cache_tree,
223 active_cache, active_nr, 0, 0) < 0)
224 die("error building trees");
226 result = lookup_tree(active_cache_tree->sha1);
231 static int save_files_dirs(const unsigned char *sha1,
232 const char *base, int baselen, const char *path,
233 unsigned int mode, int stage, void *context)
235 int len = strlen(path);
236 char *newpath = xmalloc(baselen + len + 1);
237 memcpy(newpath, base, baselen);
238 memcpy(newpath + baselen, path, len);
239 newpath[baselen + len] = '\0';
242 string_list_insert(newpath, ¤t_directory_set);
244 string_list_insert(newpath, ¤t_file_set);
247 return READ_TREE_RECURSIVE;
250 static int get_files_dirs(struct tree *tree)
253 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL))
255 n = current_file_set.nr + current_directory_set.nr;
260 * Returns an index_entry instance which doesn't have to correspond to
261 * a real cache entry in Git's index.
263 static struct stage_data *insert_stage_data(const char *path,
264 struct tree *o, struct tree *a, struct tree *b,
265 struct string_list *entries)
267 struct string_list_item *item;
268 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
269 get_tree_entry(o->object.sha1, path,
270 e->stages[1].sha, &e->stages[1].mode);
271 get_tree_entry(a->object.sha1, path,
272 e->stages[2].sha, &e->stages[2].mode);
273 get_tree_entry(b->object.sha1, path,
274 e->stages[3].sha, &e->stages[3].mode);
275 item = string_list_insert(path, entries);
281 * Create a dictionary mapping file names to stage_data objects. The
282 * dictionary contains one entry for every path with a non-zero stage entry.
284 static struct string_list *get_unmerged(void)
286 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
289 unmerged->strdup_strings = 1;
291 for (i = 0; i < active_nr; i++) {
292 struct string_list_item *item;
293 struct stage_data *e;
294 struct cache_entry *ce = active_cache[i];
298 item = string_list_lookup(ce->name, unmerged);
300 item = string_list_insert(ce->name, unmerged);
301 item->util = xcalloc(1, sizeof(struct stage_data));
304 e->stages[ce_stage(ce)].mode = ce->ce_mode;
305 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
313 struct diff_filepair *pair;
314 struct stage_data *src_entry;
315 struct stage_data *dst_entry;
316 unsigned processed:1;
320 * Get information of all renames which occurred between 'o_tree' and
321 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
322 * 'b_tree') to be able to associate the correct cache entries with
323 * the rename information. 'tree' is always equal to either a_tree or b_tree.
325 static struct string_list *get_renames(struct merge_options *o,
330 struct string_list *entries)
333 struct string_list *renames;
334 struct diff_options opts;
336 renames = xcalloc(1, sizeof(struct string_list));
338 DIFF_OPT_SET(&opts, RECURSIVE);
339 opts.detect_rename = DIFF_DETECT_RENAME;
340 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
341 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
343 opts.warn_on_too_large_rename = 1;
344 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
345 if (diff_setup_done(&opts) < 0)
346 die("diff setup failed");
347 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
349 for (i = 0; i < diff_queued_diff.nr; ++i) {
350 struct string_list_item *item;
352 struct diff_filepair *pair = diff_queued_diff.queue[i];
353 if (pair->status != 'R') {
354 diff_free_filepair(pair);
357 re = xmalloc(sizeof(*re));
360 item = string_list_lookup(re->pair->one->path, entries);
362 re->src_entry = insert_stage_data(re->pair->one->path,
363 o_tree, a_tree, b_tree, entries);
365 re->src_entry = item->util;
367 item = string_list_lookup(re->pair->two->path, entries);
369 re->dst_entry = insert_stage_data(re->pair->two->path,
370 o_tree, a_tree, b_tree, entries);
372 re->dst_entry = item->util;
373 item = string_list_insert(pair->one->path, renames);
376 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
377 diff_queued_diff.nr = 0;
382 static int update_stages(const char *path, struct diff_filespec *o,
383 struct diff_filespec *a, struct diff_filespec *b,
386 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
388 if (remove_file_from_cache(path))
391 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
394 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
397 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
402 static int remove_path(const char *name)
410 dirs = xstrdup(name);
411 while ((slash = strrchr(name, '/'))) {
413 if (rmdir(name) != 0)
420 static int remove_file(struct merge_options *o, int clean,
421 const char *path, int no_wd)
423 int update_cache = o->call_depth || clean;
424 int update_working_directory = !o->call_depth && !no_wd;
427 if (remove_file_from_cache(path))
430 if (update_working_directory) {
432 if (errno != ENOENT || errno != EISDIR)
439 static char *unique_path(const char *path, const char *branch)
441 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
444 char *p = newpath + strlen(path);
445 strcpy(newpath, path);
451 while (string_list_has_string(¤t_file_set, newpath) ||
452 string_list_has_string(¤t_directory_set, newpath) ||
453 lstat(newpath, &st) == 0)
454 sprintf(p, "_%d", suffix++);
456 string_list_insert(newpath, ¤t_file_set);
460 static void flush_buffer(int fd, const char *buf, unsigned long size)
463 long ret = write_in_full(fd, buf, size);
468 die("merge-recursive: %s", strerror(errno));
470 die("merge-recursive: disk full?");
477 static int make_room_for_path(const char *path)
480 const char *msg = "failed to create path '%s'%s";
482 status = safe_create_leading_directories_const(path);
485 /* something else exists */
486 error(msg, path, ": perhaps a D/F conflict?");
492 /* Successful unlink is good.. */
495 /* .. and so is no existing file */
498 /* .. but not some other error (who really cares what?) */
499 return error(msg, path, ": perhaps a D/F conflict?");
502 static void update_file_flags(struct merge_options *o,
503 const unsigned char *sha,
513 enum object_type type;
517 if (S_ISGITLINK(mode))
518 die("cannot read object %s '%s': It is a submodule!",
519 sha1_to_hex(sha), path);
521 buf = read_sha1_file(sha, &type, &size);
523 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
524 if (type != OBJ_BLOB)
525 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
527 struct strbuf strbuf;
528 strbuf_init(&strbuf, 0);
529 if (convert_to_working_tree(path, buf, size, &strbuf)) {
532 buf = strbuf_detach(&strbuf, NULL);
536 if (make_room_for_path(path) < 0) {
541 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
547 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
549 die("failed to open %s: %s", path, strerror(errno));
550 flush_buffer(fd, buf, size);
552 } else if (S_ISLNK(mode)) {
553 char *lnk = xmemdupz(buf, size);
554 safe_create_leading_directories_const(path);
559 die("do not know what to do with %06o %s '%s'",
560 mode, sha1_to_hex(sha), path);
565 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
568 static void update_file(struct merge_options *o,
570 const unsigned char *sha,
574 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
577 /* Low level file merging, update and removal */
579 struct merge_file_info
581 unsigned char sha[20];
587 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
590 enum object_type type;
592 if (!hashcmp(sha1, null_sha1)) {
593 mm->ptr = xstrdup("");
598 mm->ptr = read_sha1_file(sha1, &type, &size);
599 if (!mm->ptr || type != OBJ_BLOB)
600 die("unable to read blob object %s", sha1_to_hex(sha1));
604 static int merge_3way(struct merge_options *o,
605 mmbuffer_t *result_buf,
606 struct diff_filespec *one,
607 struct diff_filespec *a,
608 struct diff_filespec *b,
612 mmfile_t orig, src1, src2;
616 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
617 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
619 fill_mm(one->sha1, &orig);
620 fill_mm(a->sha1, &src1);
621 fill_mm(b->sha1, &src2);
623 merge_status = ll_merge(result_buf, a->path, &orig,
624 &src1, name1, &src2, name2,
635 static struct merge_file_info merge_file(struct merge_options *o,
636 struct diff_filespec *one,
637 struct diff_filespec *a,
638 struct diff_filespec *b,
642 struct merge_file_info result;
646 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
648 if (S_ISREG(a->mode)) {
649 result.mode = a->mode;
650 hashcpy(result.sha, a->sha1);
652 result.mode = b->mode;
653 hashcpy(result.sha, b->sha1);
656 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
662 if (a->mode == b->mode || a->mode == one->mode)
663 result.mode = b->mode;
665 result.mode = a->mode;
666 if (b->mode != one->mode) {
672 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
673 hashcpy(result.sha, b->sha1);
674 else if (sha_eq(b->sha1, one->sha1))
675 hashcpy(result.sha, a->sha1);
676 else if (S_ISREG(a->mode)) {
677 mmbuffer_t result_buf;
680 merge_status = merge_3way(o, &result_buf, one, a, b,
683 if ((merge_status < 0) || !result_buf.ptr)
684 die("Failed to execute internal merge");
686 if (write_sha1_file(result_buf.ptr, result_buf.size,
687 blob_type, result.sha))
688 die("Unable to add %s to database",
691 free(result_buf.ptr);
692 result.clean = (merge_status == 0);
693 } else if (S_ISGITLINK(a->mode)) {
695 hashcpy(result.sha, a->sha1);
696 } else if (S_ISLNK(a->mode)) {
697 hashcpy(result.sha, a->sha1);
699 if (!sha_eq(a->sha1, b->sha1))
702 die("unsupported object type in the tree");
709 static void conflict_rename_rename(struct merge_options *o,
717 const char *ren1_dst = ren1->pair->two->path;
718 const char *ren2_dst = ren2->pair->two->path;
719 const char *dst_name1 = ren1_dst;
720 const char *dst_name2 = ren2_dst;
721 if (string_list_has_string(¤t_directory_set, ren1_dst)) {
722 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
723 output(o, 1, "%s is a directory in %s adding as %s instead",
724 ren1_dst, branch2, dst_name1);
725 remove_file(o, 0, ren1_dst, 0);
727 if (string_list_has_string(¤t_directory_set, ren2_dst)) {
728 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
729 output(o, 1, "%s is a directory in %s adding as %s instead",
730 ren2_dst, branch1, dst_name2);
731 remove_file(o, 0, ren2_dst, 0);
734 remove_file_from_cache(dst_name1);
735 remove_file_from_cache(dst_name2);
737 * Uncomment to leave the conflicting names in the resulting tree
739 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
740 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
743 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
744 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
750 static void conflict_rename_dir(struct merge_options *o,
754 char *new_path = unique_path(ren1->pair->two->path, branch1);
755 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
756 remove_file(o, 0, ren1->pair->two->path, 0);
757 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
761 static void conflict_rename_rename_2(struct merge_options *o,
767 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
768 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
769 output(o, 1, "Renaming %s to %s and %s to %s instead",
770 ren1->pair->one->path, new_path1,
771 ren2->pair->one->path, new_path2);
772 remove_file(o, 0, ren1->pair->two->path, 0);
773 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
774 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
779 static int process_renames(struct merge_options *o,
780 struct string_list *a_renames,
781 struct string_list *b_renames)
783 int clean_merge = 1, i, j;
784 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
785 const struct rename *sre;
787 for (i = 0; i < a_renames->nr; i++) {
788 sre = a_renames->items[i].util;
789 string_list_insert(sre->pair->two->path, &a_by_dst)->util
792 for (i = 0; i < b_renames->nr; i++) {
793 sre = b_renames->items[i].util;
794 string_list_insert(sre->pair->two->path, &b_by_dst)->util
798 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
801 struct string_list *renames1, *renames2, *renames2Dst;
802 struct rename *ren1 = NULL, *ren2 = NULL;
803 const char *branch1, *branch2;
804 const char *ren1_src, *ren1_dst;
806 if (i >= a_renames->nr) {
808 ren2 = b_renames->items[j++].util;
809 } else if (j >= b_renames->nr) {
811 ren1 = a_renames->items[i++].util;
813 compare = strcmp(a_renames->items[i].string,
814 b_renames->items[j].string);
816 ren1 = a_renames->items[i++].util;
818 ren2 = b_renames->items[j++].util;
821 /* TODO: refactor, so that 1/2 are not needed */
823 renames1 = a_renames;
824 renames2 = b_renames;
825 renames2Dst = &b_by_dst;
826 branch1 = o->branch1;
827 branch2 = o->branch2;
830 renames1 = b_renames;
831 renames2 = a_renames;
832 renames2Dst = &a_by_dst;
833 branch1 = o->branch2;
834 branch2 = o->branch1;
839 src = ren1->pair->one->path;
841 ren1->dst_entry->processed = 1;
842 ren1->src_entry->processed = 1;
848 ren1_src = ren1->pair->one->path;
849 ren1_dst = ren1->pair->two->path;
852 const char *ren2_src = ren2->pair->one->path;
853 const char *ren2_dst = ren2->pair->two->path;
854 /* Renamed in 1 and renamed in 2 */
855 if (strcmp(ren1_src, ren2_src) != 0)
856 die("ren1.src != ren2.src");
857 ren2->dst_entry->processed = 1;
859 if (strcmp(ren1_dst, ren2_dst) != 0) {
861 output(o, 1, "CONFLICT (rename/rename): "
862 "Rename \"%s\"->\"%s\" in branch \"%s\" "
863 "rename \"%s\"->\"%s\" in \"%s\"%s",
864 src, ren1_dst, branch1,
865 src, ren2_dst, branch2,
866 o->call_depth ? " (left unresolved)": "");
868 remove_file_from_cache(src);
869 update_file(o, 0, ren1->pair->one->sha1,
870 ren1->pair->one->mode, src);
872 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
874 struct merge_file_info mfi;
875 remove_file(o, 1, ren1_src, 1);
882 if (mfi.merge || !mfi.clean)
883 output(o, 1, "Renaming %s->%s", src, ren1_dst);
886 output(o, 2, "Auto-merging %s", ren1_dst);
889 output(o, 1, "CONFLICT (content): merge conflict in %s",
894 update_stages(ren1_dst,
900 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
903 /* Renamed in 1, maybe changed in 2 */
904 struct string_list_item *item;
905 /* we only use sha1 and mode of these */
906 struct diff_filespec src_other, dst_other;
907 int try_merge, stage = a_renames == renames1 ? 3: 2;
909 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
911 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
912 src_other.mode = ren1->src_entry->stages[stage].mode;
913 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
914 dst_other.mode = ren1->dst_entry->stages[stage].mode;
918 if (string_list_has_string(¤t_directory_set, ren1_dst)) {
920 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
921 " directory %s added in %s",
922 ren1_src, ren1_dst, branch1,
924 conflict_rename_dir(o, ren1, branch1);
925 } else if (sha_eq(src_other.sha1, null_sha1)) {
927 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
929 ren1_src, ren1_dst, branch1,
931 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
932 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
933 const char *new_path;
936 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
938 ren1_src, ren1_dst, branch1,
940 new_path = unique_path(ren1_dst, branch2);
941 output(o, 1, "Adding as %s instead", new_path);
942 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
943 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
947 output(o, 1, "CONFLICT (rename/rename): "
948 "Rename %s->%s in %s. "
949 "Rename %s->%s in %s",
950 ren1_src, ren1_dst, branch1,
951 ren2->pair->one->path, ren2->pair->two->path, branch2);
952 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
957 struct diff_filespec *one, *a, *b;
958 struct merge_file_info mfi;
959 src_other.path = (char *)ren1_src;
961 one = ren1->pair->one;
962 if (a_renames == renames1) {
969 mfi = merge_file(o, one, a, b,
970 o->branch1, o->branch2);
973 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
974 mfi.mode == ren1->pair->two->mode)
976 * This messaged is part of
977 * t6022 test. If you change
978 * it update the test too.
980 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
982 if (mfi.merge || !mfi.clean)
983 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
985 output(o, 2, "Auto-merging %s", ren1_dst);
987 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
992 update_stages(ren1_dst,
995 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1000 string_list_clear(&a_by_dst, 0);
1001 string_list_clear(&b_by_dst, 0);
1006 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1008 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1011 /* Per entry merge function */
1012 static int process_entry(struct merge_options *o,
1013 const char *path, struct stage_data *entry)
1016 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1017 print_index_entry("\tpath: ", entry);
1019 int clean_merge = 1;
1020 unsigned o_mode = entry->stages[1].mode;
1021 unsigned a_mode = entry->stages[2].mode;
1022 unsigned b_mode = entry->stages[3].mode;
1023 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1024 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1025 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1027 if (o_sha && (!a_sha || !b_sha)) {
1028 /* Case A: Deleted in one */
1029 if ((!a_sha && !b_sha) ||
1030 (sha_eq(a_sha, o_sha) && !b_sha) ||
1031 (!a_sha && sha_eq(b_sha, o_sha))) {
1032 /* Deleted in both or deleted in one and
1033 * unchanged in the other */
1035 output(o, 2, "Removing %s", path);
1036 /* do not touch working file if it did not exist */
1037 remove_file(o, 1, path, !a_sha);
1039 /* Deleted in one and changed in the other */
1042 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1043 "and modified in %s. Version %s of %s left in tree.",
1045 o->branch2, o->branch2, path);
1046 update_file(o, 0, b_sha, b_mode, path);
1048 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1049 "and modified in %s. Version %s of %s left in tree.",
1051 o->branch1, o->branch1, path);
1052 update_file(o, 0, a_sha, a_mode, path);
1056 } else if ((!o_sha && a_sha && !b_sha) ||
1057 (!o_sha && !a_sha && b_sha)) {
1058 /* Case B: Added in one. */
1059 const char *add_branch;
1060 const char *other_branch;
1062 const unsigned char *sha;
1066 add_branch = o->branch1;
1067 other_branch = o->branch2;
1070 conf = "file/directory";
1072 add_branch = o->branch2;
1073 other_branch = o->branch1;
1076 conf = "directory/file";
1078 if (string_list_has_string(¤t_directory_set, path)) {
1079 const char *new_path = unique_path(path, add_branch);
1081 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1083 conf, path, other_branch, path, new_path);
1084 remove_file(o, 0, path, 0);
1085 update_file(o, 0, sha, mode, new_path);
1087 output(o, 2, "Adding %s", path);
1088 update_file(o, 1, sha, mode, path);
1090 } else if (a_sha && b_sha) {
1091 /* Case C: Added in both (check for same permissions) and */
1092 /* case D: Modified in both, but differently. */
1093 const char *reason = "content";
1094 struct merge_file_info mfi;
1095 struct diff_filespec one, a, b;
1099 o_sha = (unsigned char *)null_sha1;
1101 output(o, 2, "Auto-merging %s", path);
1102 one.path = a.path = b.path = (char *)path;
1103 hashcpy(one.sha1, o_sha);
1105 hashcpy(a.sha1, a_sha);
1107 hashcpy(b.sha1, b_sha);
1110 mfi = merge_file(o, &one, &a, &b,
1111 o->branch1, o->branch2);
1113 clean_merge = mfi.clean;
1115 update_file(o, 1, mfi.sha, mfi.mode, path);
1116 else if (S_ISGITLINK(mfi.mode))
1117 output(o, 1, "CONFLICT (submodule): Merge conflict in %s "
1118 "- needs %s", path, sha1_to_hex(b.sha1));
1120 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1124 update_file(o, 0, mfi.sha, mfi.mode, path);
1126 update_file_flags(o, mfi.sha, mfi.mode, path,
1127 0 /* update_cache */, 1 /* update_working_directory */);
1129 } else if (!o_sha && !a_sha && !b_sha) {
1131 * this entry was deleted altogether. a_mode == 0 means
1132 * we had that path and want to actively remove it.
1134 remove_file(o, 1, path, !a_mode);
1136 die("Fatal merge failure, shouldn't happen.");
1141 int merge_trees(struct merge_options *o,
1144 struct tree *common,
1145 struct tree **result)
1149 if (o->subtree_merge) {
1150 merge = shift_tree_object(head, merge);
1151 common = shift_tree_object(head, common);
1154 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1155 output(o, 0, "Already uptodate!");
1160 code = git_merge_trees(o->call_depth, common, head, merge);
1163 die("merging of trees %s and %s failed",
1164 sha1_to_hex(head->object.sha1),
1165 sha1_to_hex(merge->object.sha1));
1167 if (unmerged_cache()) {
1168 struct string_list *entries, *re_head, *re_merge;
1170 string_list_clear(¤t_file_set, 1);
1171 string_list_clear(¤t_directory_set, 1);
1172 get_files_dirs(head);
1173 get_files_dirs(merge);
1175 entries = get_unmerged();
1176 re_head = get_renames(o, head, common, head, merge, entries);
1177 re_merge = get_renames(o, merge, common, head, merge, entries);
1178 clean = process_renames(o, re_head, re_merge);
1179 for (i = 0; i < entries->nr; i++) {
1180 const char *path = entries->items[i].string;
1181 struct stage_data *e = entries->items[i].util;
1183 && !process_entry(o, path, e))
1187 string_list_clear(re_merge, 0);
1188 string_list_clear(re_head, 0);
1189 string_list_clear(entries, 1);
1196 *result = write_tree_from_memory(o);
1201 static struct commit_list *reverse_commit_list(struct commit_list *list)
1203 struct commit_list *next = NULL, *current, *backup;
1204 for (current = list; current; current = backup) {
1205 backup = current->next;
1206 current->next = next;
1213 * Merge the commits h1 and h2, return the resulting virtual
1214 * commit object and a flag indicating the cleanness of the merge.
1216 int merge_recursive(struct merge_options *o,
1219 struct commit_list *ca,
1220 struct commit **result)
1222 struct commit_list *iter;
1223 struct commit *merged_common_ancestors;
1224 struct tree *mrtree = mrtree;
1228 output(o, 4, "Merging:");
1229 output_commit_title(o, h1);
1230 output_commit_title(o, h2);
1234 ca = get_merge_bases(h1, h2, 1);
1235 ca = reverse_commit_list(ca);
1239 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1240 for (iter = ca; iter; iter = iter->next)
1241 output_commit_title(o, iter->item);
1244 merged_common_ancestors = pop_commit(&ca);
1245 if (merged_common_ancestors == NULL) {
1246 /* if there is no common ancestor, make an empty tree */
1247 struct tree *tree = xcalloc(1, sizeof(struct tree));
1249 tree->object.parsed = 1;
1250 tree->object.type = OBJ_TREE;
1251 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1252 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1255 for (iter = ca; iter; iter = iter->next) {
1256 const char *saved_b1, *saved_b2;
1259 * When the merge fails, the result contains files
1260 * with conflict markers. The cleanness flag is
1261 * ignored, it was never actually used, as result of
1262 * merge_trees has always overwritten it: the committed
1263 * "conflicts" were already resolved.
1266 saved_b1 = o->branch1;
1267 saved_b2 = o->branch2;
1268 o->branch1 = "Temporary merge branch 1";
1269 o->branch2 = "Temporary merge branch 2";
1270 merge_recursive(o, merged_common_ancestors, iter->item,
1271 NULL, &merged_common_ancestors);
1272 o->branch1 = saved_b1;
1273 o->branch2 = saved_b2;
1276 if (!merged_common_ancestors)
1277 die("merge returned no commit");
1284 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1287 if (o->call_depth) {
1288 *result = make_virtual_commit(mrtree, "merged tree");
1289 commit_list_insert(h1, &(*result)->parents);
1290 commit_list_insert(h2, &(*result)->parents->next);
1296 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1298 struct object *object;
1300 object = deref_tag(parse_object(sha1), name, strlen(name));
1303 if (object->type == OBJ_TREE)
1304 return make_virtual_commit((struct tree*)object, name);
1305 if (object->type != OBJ_COMMIT)
1307 if (parse_commit((struct commit *)object))
1309 return (struct commit *)object;
1312 int merge_recursive_generic(struct merge_options *o,
1313 const unsigned char *head,
1314 const unsigned char *merge,
1316 const unsigned char **base_list,
1317 struct commit **result)
1319 int clean, index_fd;
1320 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1321 struct commit *head_commit = get_ref(head, o->branch1);
1322 struct commit *next_commit = get_ref(merge, o->branch2);
1323 struct commit_list *ca = NULL;
1327 for (i = 0; i < num_base_list; ++i) {
1328 struct commit *base;
1329 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1330 return error("Could not parse object '%s'",
1331 sha1_to_hex(base_list[i]));
1332 commit_list_insert(base, &ca);
1336 index_fd = hold_locked_index(lock, 1);
1337 clean = merge_recursive(o, head_commit, next_commit, ca,
1339 if (active_cache_changed &&
1340 (write_cache(index_fd, active_cache, active_nr) ||
1341 commit_locked_index(lock)))
1342 return error("Unable to write index.");
1344 return clean ? 0 : 1;
1347 static int merge_recursive_config(const char *var, const char *value, void *cb)
1349 struct merge_options *o = cb;
1350 if (!strcasecmp(var, "merge.verbosity")) {
1351 o->verbosity = git_config_int(var, value);
1354 if (!strcasecmp(var, "diff.renamelimit")) {
1355 o->diff_rename_limit = git_config_int(var, value);
1358 if (!strcasecmp(var, "merge.renamelimit")) {
1359 o->merge_rename_limit = git_config_int(var, value);
1362 return git_default_config(var, value, cb);
1365 void init_merge_options(struct merge_options *o)
1367 memset(o, 0, sizeof(struct merge_options));
1369 o->buffer_output = 1;
1370 o->diff_rename_limit = -1;
1371 o->merge_rename_limit = -1;
1372 git_config(merge_recursive_config, o);
1373 if (getenv("GIT_MERGE_VERBOSITY"))
1375 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1376 if (o->verbosity >= 5)
1377 o->buffer_output = 0;