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"
10 #include "tree-walk.h"
13 #include "run-command.h"
15 #include "unpack-trees.h"
16 #include "path-list.h"
17 #include "xdiff-interface.h"
19 static int subtree_merge;
21 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
23 unsigned char shifted[20];
26 * NEEDSWORK: this limits the recursion depth to hardcoded
27 * value '2' to avoid excessive overhead.
29 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
30 if (!hashcmp(two->object.sha1, shifted))
32 return lookup_tree(shifted);
36 * A virtual commit has
37 * - (const char *)commit->util set to the name, and
38 * - *(int *)commit->object.sha1 set to the virtual id.
41 static unsigned commit_list_count(const struct commit_list *l)
44 for (; l; l = l->next )
49 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
51 struct commit *commit = xcalloc(1, sizeof(struct commit));
52 static unsigned virtual_id = 1;
54 commit->util = (void*)comment;
55 *(int*)commit->object.sha1 = virtual_id++;
57 commit->object.parsed = 1;
62 * Since we use get_tree_entry(), which does not put the read object into
63 * the object pool, we cannot rely on a == b.
65 static int sha_eq(const unsigned char *a, const unsigned char *b)
69 return a && b && hashcmp(a, b) == 0;
73 * Since we want to write the index eventually, we cannot reuse the index
74 * for these (temporary) data.
81 unsigned char sha[20];
88 struct output_buffer *next;
92 static struct path_list current_file_set = {NULL, 0, 0, 1};
93 static struct path_list current_directory_set = {NULL, 0, 0, 1};
95 static int call_depth = 0;
96 static int verbosity = 2;
97 static int buffer_output = 1;
98 static int do_progress = 1;
99 static unsigned last_percent;
100 static unsigned merged_cnt;
101 static unsigned total_cnt;
102 static volatile sig_atomic_t progress_update;
103 static struct output_buffer *output_list, *output_end;
105 static int show (int v)
107 return (!call_depth && verbosity >= v) || verbosity >= 5;
110 static void output(int v, const char *fmt, ...)
114 if (buffer_output && show(v)) {
115 struct output_buffer *b = xmalloc(sizeof(*b));
116 nfvasprintf(&b->str, fmt, args);
119 output_end->next = b;
123 } else if (show(v)) {
125 for (i = call_depth; i--;)
127 vfprintf(stdout, fmt, args);
133 static void flush_output()
135 struct output_buffer *b, *n;
136 for (b = output_list; b; b = n) {
138 for (i = call_depth; i--;)
140 fputs(b->str, stdout);
150 static void output_commit_title(struct commit *commit)
154 for (i = call_depth; i--;)
157 printf("virtual %s\n", (char *)commit->util);
159 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
160 if (parse_commit(commit) != 0)
161 printf("(bad commit)\n");
165 for (s = commit->buffer; *s; s++)
166 if (*s == '\n' && s[1] == '\n') {
170 for (len = 0; s[len] && '\n' != s[len]; len++)
172 printf("%.*s\n", len, s);
177 static void progress_interval(int signum)
182 static void setup_progress_signal(void)
187 memset(&sa, 0, sizeof(sa));
188 sa.sa_handler = progress_interval;
189 sigemptyset(&sa.sa_mask);
190 sa.sa_flags = SA_RESTART;
191 sigaction(SIGALRM, &sa, NULL);
193 v.it_interval.tv_sec = 1;
194 v.it_interval.tv_usec = 0;
195 v.it_value = v.it_interval;
196 setitimer(ITIMER_REAL, &v, NULL);
199 static void display_progress()
201 unsigned percent = total_cnt ? merged_cnt * 100 / total_cnt : 0;
202 if (progress_update || percent != last_percent) {
203 fprintf(stderr, "%4u%% (%u/%u) done\r",
204 percent, merged_cnt, total_cnt);
206 last_percent = percent;
210 static struct cache_entry *make_cache_entry(unsigned int mode,
211 const unsigned char *sha1, const char *path, int stage, int refresh)
214 struct cache_entry *ce;
216 if (!verify_path(path))
220 size = cache_entry_size(len);
221 ce = xcalloc(1, size);
223 hashcpy(ce->sha1, sha1);
224 memcpy(ce->name, path, len);
225 ce->ce_flags = create_ce_flags(len, stage);
226 ce->ce_mode = create_ce_mode(mode);
229 return refresh_cache_entry(ce, 0);
234 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
235 const char *path, int stage, int refresh, int options)
237 struct cache_entry *ce;
238 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
240 return error("addinfo_cache failed for path '%s'", path);
241 return add_cache_entry(ce, options);
245 * This is a global variable which is used in a number of places but
246 * only written to in the 'merge' function.
248 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
249 * don't update the working directory.
250 * 0 => Leave unmerged entries in the cache and update
251 * the working directory.
253 static int index_only = 0;
255 static int git_merge_trees(int index_only,
261 struct object_list *trees = NULL;
262 struct unpack_trees_options opts;
264 memset(&opts, 0, sizeof(opts));
271 opts.fn = threeway_merge;
273 object_list_append(&common->object, &trees);
274 object_list_append(&head->object, &trees);
275 object_list_append(&merge->object, &trees);
277 rc = unpack_trees(trees, &opts);
278 cache_tree_free(&active_cache_tree);
282 static int unmerged_index(void)
285 for (i = 0; i < active_nr; i++) {
286 struct cache_entry *ce = active_cache[i];
293 static struct tree *git_write_tree(void)
295 struct tree *result = NULL;
297 if (unmerged_index()) {
299 output(0, "There are unmerged index entries:");
300 for (i = 0; i < active_nr; i++) {
301 struct cache_entry *ce = active_cache[i];
303 output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
308 if (!active_cache_tree)
309 active_cache_tree = cache_tree();
311 if (!cache_tree_fully_valid(active_cache_tree) &&
312 cache_tree_update(active_cache_tree,
313 active_cache, active_nr, 0, 0) < 0)
314 die("error building trees");
316 result = lookup_tree(active_cache_tree->sha1);
321 static int save_files_dirs(const unsigned char *sha1,
322 const char *base, int baselen, const char *path,
323 unsigned int mode, int stage)
325 int len = strlen(path);
326 char *newpath = xmalloc(baselen + len + 1);
327 memcpy(newpath, base, baselen);
328 memcpy(newpath + baselen, path, len);
329 newpath[baselen + len] = '\0';
332 path_list_insert(newpath, ¤t_directory_set);
334 path_list_insert(newpath, ¤t_file_set);
337 return READ_TREE_RECURSIVE;
340 static int get_files_dirs(struct tree *tree)
343 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
345 n = current_file_set.nr + current_directory_set.nr;
350 * Returns a index_entry instance which doesn't have to correspond to
351 * a real cache entry in Git's index.
353 static struct stage_data *insert_stage_data(const char *path,
354 struct tree *o, struct tree *a, struct tree *b,
355 struct path_list *entries)
357 struct path_list_item *item;
358 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
359 get_tree_entry(o->object.sha1, path,
360 e->stages[1].sha, &e->stages[1].mode);
361 get_tree_entry(a->object.sha1, path,
362 e->stages[2].sha, &e->stages[2].mode);
363 get_tree_entry(b->object.sha1, path,
364 e->stages[3].sha, &e->stages[3].mode);
365 item = path_list_insert(path, entries);
371 * Create a dictionary mapping file names to stage_data objects. The
372 * dictionary contains one entry for every path with a non-zero stage entry.
374 static struct path_list *get_unmerged(void)
376 struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
379 unmerged->strdup_paths = 1;
380 total_cnt += active_nr;
382 for (i = 0; i < active_nr; i++, merged_cnt++) {
383 struct path_list_item *item;
384 struct stage_data *e;
385 struct cache_entry *ce = active_cache[i];
391 item = path_list_lookup(ce->name, unmerged);
393 item = path_list_insert(ce->name, unmerged);
394 item->util = xcalloc(1, sizeof(struct stage_data));
397 e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
398 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
406 struct diff_filepair *pair;
407 struct stage_data *src_entry;
408 struct stage_data *dst_entry;
409 unsigned processed:1;
413 * Get information of all renames which occurred between 'o_tree' and
414 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
415 * 'b_tree') to be able to associate the correct cache entries with
416 * the rename information. 'tree' is always equal to either a_tree or b_tree.
418 static struct path_list *get_renames(struct tree *tree,
422 struct path_list *entries)
425 struct path_list *renames;
426 struct diff_options opts;
428 renames = xcalloc(1, sizeof(struct path_list));
431 opts.detect_rename = DIFF_DETECT_RENAME;
432 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
433 if (diff_setup_done(&opts) < 0)
434 die("diff setup failed");
435 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
437 for (i = 0; i < diff_queued_diff.nr; ++i) {
438 struct path_list_item *item;
440 struct diff_filepair *pair = diff_queued_diff.queue[i];
441 if (pair->status != 'R') {
442 diff_free_filepair(pair);
445 re = xmalloc(sizeof(*re));
448 item = path_list_lookup(re->pair->one->path, entries);
450 re->src_entry = insert_stage_data(re->pair->one->path,
451 o_tree, a_tree, b_tree, entries);
453 re->src_entry = item->util;
455 item = path_list_lookup(re->pair->two->path, entries);
457 re->dst_entry = insert_stage_data(re->pair->two->path,
458 o_tree, a_tree, b_tree, entries);
460 re->dst_entry = item->util;
461 item = path_list_insert(pair->one->path, renames);
464 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
465 diff_queued_diff.nr = 0;
470 static int update_stages(const char *path, struct diff_filespec *o,
471 struct diff_filespec *a, struct diff_filespec *b,
474 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
476 if (remove_file_from_cache(path))
479 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
482 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
485 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
490 static int remove_path(const char *name)
499 dirs = xmalloc(len+1);
500 memcpy(dirs, name, len);
502 while ((slash = strrchr(name, '/'))) {
505 if (rmdir(name) != 0)
512 static int remove_file(int clean, const char *path, int no_wd)
514 int update_cache = index_only || clean;
515 int update_working_directory = !index_only && !no_wd;
518 if (remove_file_from_cache(path))
521 if (update_working_directory) {
523 if (errno != ENOENT || errno != EISDIR)
530 static char *unique_path(const char *path, const char *branch)
532 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
535 char *p = newpath + strlen(path);
536 strcpy(newpath, path);
542 while (path_list_has_path(¤t_file_set, newpath) ||
543 path_list_has_path(¤t_directory_set, newpath) ||
544 lstat(newpath, &st) == 0)
545 sprintf(p, "_%d", suffix++);
547 path_list_insert(newpath, ¤t_file_set);
551 static int mkdir_p(const char *path, unsigned long mode)
553 /* path points to cache entries, so xstrdup before messing with it */
554 char *buf = xstrdup(path);
555 int result = safe_create_leading_directories(buf);
560 static void flush_buffer(int fd, const char *buf, unsigned long size)
563 long ret = write_in_full(fd, buf, size);
568 die("merge-recursive: %s", strerror(errno));
570 die("merge-recursive: disk full?");
577 static void update_file_flags(const unsigned char *sha,
587 enum object_type type;
591 buf = read_sha1_file(sha, &type, &size);
593 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
594 if (type != OBJ_BLOB)
595 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
597 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
599 if (mkdir_p(path, 0777))
600 die("failed to create path %s: %s", path, strerror(errno));
606 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
608 die("failed to open %s: %s", path, strerror(errno));
609 flush_buffer(fd, buf, size);
611 } else if (S_ISLNK(mode)) {
612 char *lnk = xmalloc(size + 1);
613 memcpy(lnk, buf, size);
620 die("do not know what to do with %06o %s '%s'",
621 mode, sha1_to_hex(sha), path);
624 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
627 static void update_file(int clean,
628 const unsigned char *sha,
632 update_file_flags(sha, mode, path, index_only || clean, !index_only);
635 /* Low level file merging, update and removal */
637 struct merge_file_info
639 unsigned char sha[20];
645 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
648 enum object_type type;
650 if (!hashcmp(sha1, null_sha1)) {
651 mm->ptr = xstrdup("");
656 mm->ptr = read_sha1_file(sha1, &type, &size);
657 if (!mm->ptr || type != OBJ_BLOB)
658 die("unable to read blob object %s", sha1_to_hex(sha1));
662 static int ll_merge(mmbuffer_t *result_buf,
663 struct diff_filespec *o,
664 struct diff_filespec *a,
665 struct diff_filespec *b,
669 mmfile_t orig, src1, src2;
674 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
675 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
677 fill_mm(o->sha1, &orig);
678 fill_mm(a->sha1, &src1);
679 fill_mm(b->sha1, &src2);
681 memset(&xpp, 0, sizeof(xpp));
682 merge_status = xdl_merge(&orig,
685 &xpp, XDL_MERGE_ZEALOUS,
695 static struct merge_file_info merge_file(struct diff_filespec *o,
696 struct diff_filespec *a, struct diff_filespec *b,
697 const char *branch1, const char *branch2)
699 struct merge_file_info result;
703 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
705 if (S_ISREG(a->mode)) {
706 result.mode = a->mode;
707 hashcpy(result.sha, a->sha1);
709 result.mode = b->mode;
710 hashcpy(result.sha, b->sha1);
713 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
716 result.mode = a->mode == o->mode ? b->mode: a->mode;
718 if (sha_eq(a->sha1, o->sha1))
719 hashcpy(result.sha, b->sha1);
720 else if (sha_eq(b->sha1, o->sha1))
721 hashcpy(result.sha, a->sha1);
722 else if (S_ISREG(a->mode)) {
723 mmbuffer_t result_buf;
726 merge_status = ll_merge(&result_buf, o, a, b,
729 if ((merge_status < 0) || !result_buf.ptr)
730 die("Failed to execute internal merge");
732 if (write_sha1_file(result_buf.ptr, result_buf.size,
733 blob_type, result.sha))
734 die("Unable to add %s to database",
737 free(result_buf.ptr);
738 result.clean = (merge_status == 0);
740 if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
741 die("cannot merge modes?");
743 hashcpy(result.sha, a->sha1);
745 if (!sha_eq(a->sha1, b->sha1))
753 static void conflict_rename_rename(struct rename *ren1,
760 const char *ren1_dst = ren1->pair->two->path;
761 const char *ren2_dst = ren2->pair->two->path;
762 const char *dst_name1 = ren1_dst;
763 const char *dst_name2 = ren2_dst;
764 if (path_list_has_path(¤t_directory_set, ren1_dst)) {
765 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
766 output(1, "%s is a directory in %s added as %s instead",
767 ren1_dst, branch2, dst_name1);
768 remove_file(0, ren1_dst, 0);
770 if (path_list_has_path(¤t_directory_set, ren2_dst)) {
771 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
772 output(1, "%s is a directory in %s added as %s instead",
773 ren2_dst, branch1, dst_name2);
774 remove_file(0, ren2_dst, 0);
777 remove_file_from_cache(dst_name1);
778 remove_file_from_cache(dst_name2);
780 * Uncomment to leave the conflicting names in the resulting tree
782 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
783 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
786 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
787 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
793 static void conflict_rename_dir(struct rename *ren1,
796 char *new_path = unique_path(ren1->pair->two->path, branch1);
797 output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
798 remove_file(0, ren1->pair->two->path, 0);
799 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
803 static void conflict_rename_rename_2(struct rename *ren1,
808 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
809 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
810 output(1, "Renamed %s to %s and %s to %s instead",
811 ren1->pair->one->path, new_path1,
812 ren2->pair->one->path, new_path2);
813 remove_file(0, ren1->pair->two->path, 0);
814 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
815 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
820 static int process_renames(struct path_list *a_renames,
821 struct path_list *b_renames,
822 const char *a_branch,
823 const char *b_branch)
825 int clean_merge = 1, i, j;
826 struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
827 const struct rename *sre;
829 for (i = 0; i < a_renames->nr; i++) {
830 sre = a_renames->items[i].util;
831 path_list_insert(sre->pair->two->path, &a_by_dst)->util
834 for (i = 0; i < b_renames->nr; i++) {
835 sre = b_renames->items[i].util;
836 path_list_insert(sre->pair->two->path, &b_by_dst)->util
840 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
843 struct path_list *renames1, *renames2, *renames2Dst;
844 struct rename *ren1 = NULL, *ren2 = NULL;
845 const char *branch1, *branch2;
846 const char *ren1_src, *ren1_dst;
848 if (i >= a_renames->nr) {
850 ren2 = b_renames->items[j++].util;
851 } else if (j >= b_renames->nr) {
853 ren1 = a_renames->items[i++].util;
855 compare = strcmp(a_renames->items[i].path,
856 b_renames->items[j].path);
858 ren1 = a_renames->items[i++].util;
860 ren2 = b_renames->items[j++].util;
863 /* TODO: refactor, so that 1/2 are not needed */
865 renames1 = a_renames;
866 renames2 = b_renames;
867 renames2Dst = &b_by_dst;
872 renames1 = b_renames;
873 renames2 = a_renames;
874 renames2Dst = &a_by_dst;
881 src = ren1->pair->one->path;
883 ren1->dst_entry->processed = 1;
884 ren1->src_entry->processed = 1;
890 ren1_src = ren1->pair->one->path;
891 ren1_dst = ren1->pair->two->path;
894 const char *ren2_src = ren2->pair->one->path;
895 const char *ren2_dst = ren2->pair->two->path;
896 /* Renamed in 1 and renamed in 2 */
897 if (strcmp(ren1_src, ren2_src) != 0)
898 die("ren1.src != ren2.src");
899 ren2->dst_entry->processed = 1;
901 if (strcmp(ren1_dst, ren2_dst) != 0) {
903 output(1, "CONFLICT (rename/rename): "
904 "Rename \"%s\"->\"%s\" in branch \"%s\" "
905 "rename \"%s\"->\"%s\" in \"%s\"%s",
906 src, ren1_dst, branch1,
907 src, ren2_dst, branch2,
908 index_only ? " (left unresolved)": "");
910 remove_file_from_cache(src);
911 update_file(0, ren1->pair->one->sha1,
912 ren1->pair->one->mode, src);
914 conflict_rename_rename(ren1, branch1, ren2, branch2);
916 struct merge_file_info mfi;
917 remove_file(1, ren1_src, 1);
918 mfi = merge_file(ren1->pair->one,
923 if (mfi.merge || !mfi.clean)
924 output(1, "Renamed %s->%s", src, ren1_dst);
927 output(2, "Auto-merged %s", ren1_dst);
930 output(1, "CONFLICT (content): merge conflict in %s",
935 update_stages(ren1_dst,
941 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
944 /* Renamed in 1, maybe changed in 2 */
945 struct path_list_item *item;
946 /* we only use sha1 and mode of these */
947 struct diff_filespec src_other, dst_other;
948 int try_merge, stage = a_renames == renames1 ? 3: 2;
950 remove_file(1, ren1_src, index_only || stage == 3);
952 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
953 src_other.mode = ren1->src_entry->stages[stage].mode;
954 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
955 dst_other.mode = ren1->dst_entry->stages[stage].mode;
959 if (path_list_has_path(¤t_directory_set, ren1_dst)) {
961 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
962 " directory %s added in %s",
963 ren1_src, ren1_dst, branch1,
965 conflict_rename_dir(ren1, branch1);
966 } else if (sha_eq(src_other.sha1, null_sha1)) {
968 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
970 ren1_src, ren1_dst, branch1,
972 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
973 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
974 const char *new_path;
977 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
979 ren1_src, ren1_dst, branch1,
981 new_path = unique_path(ren1_dst, branch2);
982 output(1, "Added as %s instead", new_path);
983 update_file(0, dst_other.sha1, dst_other.mode, new_path);
984 } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
988 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
989 "Renamed %s->%s in %s",
990 ren1_src, ren1_dst, branch1,
991 ren2->pair->one->path, ren2->pair->two->path, branch2);
992 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
997 struct diff_filespec *o, *a, *b;
998 struct merge_file_info mfi;
999 src_other.path = (char *)ren1_src;
1001 o = ren1->pair->one;
1002 if (a_renames == renames1) {
1003 a = ren1->pair->two;
1006 b = ren1->pair->two;
1009 mfi = merge_file(o, a, b,
1010 a_branch, b_branch);
1012 if (mfi.merge || !mfi.clean)
1013 output(1, "Renamed %s => %s", ren1_src, ren1_dst);
1015 output(2, "Auto-merged %s", ren1_dst);
1017 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
1022 update_stages(ren1_dst,
1025 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1029 path_list_clear(&a_by_dst, 0);
1030 path_list_clear(&b_by_dst, 0);
1035 static unsigned char *has_sha(const unsigned char *sha)
1037 return is_null_sha1(sha) ? NULL: (unsigned char *)sha;
1040 /* Per entry merge function */
1041 static int process_entry(const char *path, struct stage_data *entry,
1042 const char *branch1,
1043 const char *branch2)
1046 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1047 print_index_entry("\tpath: ", entry);
1049 int clean_merge = 1;
1050 unsigned char *o_sha = has_sha(entry->stages[1].sha);
1051 unsigned char *a_sha = has_sha(entry->stages[2].sha);
1052 unsigned char *b_sha = has_sha(entry->stages[3].sha);
1053 unsigned o_mode = entry->stages[1].mode;
1054 unsigned a_mode = entry->stages[2].mode;
1055 unsigned b_mode = entry->stages[3].mode;
1057 if (o_sha && (!a_sha || !b_sha)) {
1058 /* Case A: Deleted in one */
1059 if ((!a_sha && !b_sha) ||
1060 (sha_eq(a_sha, o_sha) && !b_sha) ||
1061 (!a_sha && sha_eq(b_sha, o_sha))) {
1062 /* Deleted in both or deleted in one and
1063 * unchanged in the other */
1065 output(2, "Removed %s", path);
1066 /* do not touch working file if it did not exist */
1067 remove_file(1, path, !a_sha);
1069 /* Deleted in one and changed in the other */
1072 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1073 "and modified in %s. Version %s of %s left in tree.",
1075 branch2, branch2, path);
1076 update_file(0, b_sha, b_mode, path);
1078 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1079 "and modified in %s. Version %s of %s left in tree.",
1081 branch1, branch1, path);
1082 update_file(0, a_sha, a_mode, path);
1086 } else if ((!o_sha && a_sha && !b_sha) ||
1087 (!o_sha && !a_sha && b_sha)) {
1088 /* Case B: Added in one. */
1089 const char *add_branch;
1090 const char *other_branch;
1092 const unsigned char *sha;
1096 add_branch = branch1;
1097 other_branch = branch2;
1100 conf = "file/directory";
1102 add_branch = branch2;
1103 other_branch = branch1;
1106 conf = "directory/file";
1108 if (path_list_has_path(¤t_directory_set, path)) {
1109 const char *new_path = unique_path(path, add_branch);
1111 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1113 conf, path, other_branch, path, new_path);
1114 remove_file(0, path, 0);
1115 update_file(0, sha, mode, new_path);
1117 output(2, "Added %s", path);
1118 update_file(1, sha, mode, path);
1120 } else if (a_sha && b_sha) {
1121 /* Case C: Added in both (check for same permissions) and */
1122 /* case D: Modified in both, but differently. */
1123 const char *reason = "content";
1124 struct merge_file_info mfi;
1125 struct diff_filespec o, a, b;
1129 o_sha = (unsigned char *)null_sha1;
1131 output(2, "Auto-merged %s", path);
1132 o.path = a.path = b.path = (char *)path;
1133 hashcpy(o.sha1, o_sha);
1135 hashcpy(a.sha1, a_sha);
1137 hashcpy(b.sha1, b_sha);
1140 mfi = merge_file(&o, &a, &b,
1144 update_file(1, mfi.sha, mfi.mode, path);
1147 output(1, "CONFLICT (%s): Merge conflict in %s",
1151 update_file(0, mfi.sha, mfi.mode, path);
1153 update_file_flags(mfi.sha, mfi.mode, path,
1154 0 /* update_cache */, 1 /* update_working_directory */);
1157 die("Fatal merge failure, shouldn't happen.");
1162 static int merge_trees(struct tree *head,
1164 struct tree *common,
1165 const char *branch1,
1166 const char *branch2,
1167 struct tree **result)
1171 if (subtree_merge) {
1172 merge = shift_tree_object(head, merge);
1173 common = shift_tree_object(head, common);
1176 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1177 output(0, "Already uptodate!");
1182 code = git_merge_trees(index_only, common, head, merge);
1185 die("merging of trees %s and %s failed",
1186 sha1_to_hex(head->object.sha1),
1187 sha1_to_hex(merge->object.sha1));
1189 if (unmerged_index()) {
1190 struct path_list *entries, *re_head, *re_merge;
1192 path_list_clear(¤t_file_set, 1);
1193 path_list_clear(¤t_directory_set, 1);
1194 get_files_dirs(head);
1195 get_files_dirs(merge);
1197 entries = get_unmerged();
1198 re_head = get_renames(head, common, head, merge, entries);
1199 re_merge = get_renames(merge, common, head, merge, entries);
1200 clean = process_renames(re_head, re_merge,
1202 total_cnt += entries->nr;
1203 for (i = 0; i < entries->nr; i++, merged_cnt++) {
1204 const char *path = entries->items[i].path;
1205 struct stage_data *e = entries->items[i].util;
1207 && !process_entry(path, e, branch1, branch2))
1213 path_list_clear(re_merge, 0);
1214 path_list_clear(re_head, 0);
1215 path_list_clear(entries, 1);
1222 *result = git_write_tree();
1227 static struct commit_list *reverse_commit_list(struct commit_list *list)
1229 struct commit_list *next = NULL, *current, *backup;
1230 for (current = list; current; current = backup) {
1231 backup = current->next;
1232 current->next = next;
1239 * Merge the commits h1 and h2, return the resulting virtual
1240 * commit object and a flag indicating the cleanness of the merge.
1242 static int merge(struct commit *h1,
1244 const char *branch1,
1245 const char *branch2,
1246 struct commit_list *ca,
1247 struct commit **result)
1249 struct commit_list *iter;
1250 struct commit *merged_common_ancestors;
1251 struct tree *mrtree;
1255 output(4, "Merging:");
1256 output_commit_title(h1);
1257 output_commit_title(h2);
1261 ca = get_merge_bases(h1, h2, 1);
1262 ca = reverse_commit_list(ca);
1266 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1267 for (iter = ca; iter; iter = iter->next)
1268 output_commit_title(iter->item);
1271 merged_common_ancestors = pop_commit(&ca);
1272 if (merged_common_ancestors == NULL) {
1273 /* if there is no common ancestor, make an empty tree */
1274 struct tree *tree = xcalloc(1, sizeof(struct tree));
1276 tree->object.parsed = 1;
1277 tree->object.type = OBJ_TREE;
1278 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1279 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1282 for (iter = ca; iter; iter = iter->next) {
1285 * When the merge fails, the result contains files
1286 * with conflict markers. The cleanness flag is
1287 * ignored, it was never actually used, as result of
1288 * merge_trees has always overwritten it: the committed
1289 * "conflicts" were already resolved.
1292 merge(merged_common_ancestors, iter->item,
1293 "Temporary merge branch 1",
1294 "Temporary merge branch 2",
1296 &merged_common_ancestors);
1299 if (!merged_common_ancestors)
1300 die("merge returned no commit");
1310 clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1311 branch1, branch2, &mrtree);
1314 *result = make_virtual_commit(mrtree, "merged tree");
1315 commit_list_insert(h1, &(*result)->parents);
1316 commit_list_insert(h2, &(*result)->parents->next);
1318 if (!call_depth && do_progress) {
1319 /* Make sure we end at 100% */
1322 merged_cnt = total_cnt;
1323 progress_update = 1;
1325 fputc('\n', stderr);
1331 static const char *better_branch_name(const char *branch)
1333 static char githead_env[8 + 40 + 1];
1336 if (strlen(branch) != 40)
1338 sprintf(githead_env, "GITHEAD_%s", branch);
1339 name = getenv(githead_env);
1340 return name ? name : branch;
1343 static struct commit *get_ref(const char *ref)
1345 unsigned char sha1[20];
1346 struct object *object;
1348 if (get_sha1(ref, sha1))
1349 die("Could not resolve ref '%s'", ref);
1350 object = deref_tag(parse_object(sha1), ref, strlen(ref));
1351 if (object->type == OBJ_TREE)
1352 return make_virtual_commit((struct tree*)object,
1353 better_branch_name(ref));
1354 if (object->type != OBJ_COMMIT)
1356 if (parse_commit((struct commit *)object))
1357 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1358 return (struct commit *)object;
1361 static int merge_config(const char *var, const char *value)
1363 if (!strcasecmp(var, "merge.verbosity")) {
1364 verbosity = git_config_int(var, value);
1367 return git_default_config(var, value);
1370 int main(int argc, char *argv[])
1372 static const char *bases[20];
1373 static unsigned bases_count = 0;
1375 const char *branch1, *branch2;
1376 struct commit *result, *h1, *h2;
1377 struct commit_list *ca = NULL;
1378 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1382 int namelen = strlen(argv[0]);
1384 !strcmp(argv[0] + namelen - 8, "-subtree"))
1388 git_config(merge_config);
1389 if (getenv("GIT_MERGE_VERBOSITY"))
1390 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1393 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1395 for (i = 1; i < argc; ++i) {
1396 if (!strcmp(argv[i], "--"))
1398 if (bases_count < sizeof(bases)/sizeof(*bases))
1399 bases[bases_count++] = argv[i];
1401 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1402 die("Not handling anything other than two heads merge.");
1403 if (verbosity >= 5) {
1408 do_progress = isatty(1);
1410 branch1 = argv[++i];
1411 branch2 = argv[++i];
1413 h1 = get_ref(branch1);
1414 h2 = get_ref(branch2);
1416 branch1 = better_branch_name(branch1);
1417 branch2 = better_branch_name(branch2);
1420 setup_progress_signal();
1422 printf("Merging %s with %s\n", branch1, branch2);
1424 index_fd = hold_locked_index(lock, 1);
1426 for (i = 0; i < bases_count; i++) {
1427 struct commit *ancestor = get_ref(bases[i]);
1428 ca = commit_list_insert(ancestor, &ca);
1430 clean = merge(h1, h2, branch1, branch2, ca, &result);
1432 if (active_cache_changed &&
1433 (write_cache(index_fd, active_cache, active_nr) ||
1434 close(index_fd) || commit_locked_index(lock)))
1435 die ("unable to write %s", get_index_file());
1437 return clean ? 0: 1;