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"
14 #include "unpack-trees.h"
15 #include "path-list.h"
16 #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];
86 static struct path_list current_file_set = {NULL, 0, 0, 1};
87 static struct path_list current_directory_set = {NULL, 0, 0, 1};
89 static int call_depth = 0;
90 static int verbosity = 2;
91 static int rename_limit = -1;
92 static int buffer_output = 1;
93 static struct strbuf obuf = STRBUF_INIT;
95 static int show(int v)
97 return (!call_depth && verbosity >= v) || verbosity >= 5;
100 static void flush_output(void)
103 fputs(obuf.buf, stdout);
108 static void output(int v, const char *fmt, ...)
116 strbuf_grow(&obuf, call_depth * 2 + 2);
117 memset(obuf.buf + obuf.len, ' ', call_depth * 2);
118 strbuf_setlen(&obuf, obuf.len + call_depth * 2);
121 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
126 if (len >= strbuf_avail(&obuf)) {
127 strbuf_grow(&obuf, len + 2);
129 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
131 if (len >= strbuf_avail(&obuf)) {
132 die("this should not happen, your snprintf is broken");
135 strbuf_setlen(&obuf, obuf.len + len);
136 strbuf_add(&obuf, "\n", 1);
141 static void output_commit_title(struct commit *commit)
145 for (i = call_depth; i--;)
148 printf("virtual %s\n", (char *)commit->util);
150 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
151 if (parse_commit(commit) != 0)
152 printf("(bad commit)\n");
156 for (s = commit->buffer; *s; s++)
157 if (*s == '\n' && s[1] == '\n') {
161 for (len = 0; s[len] && '\n' != s[len]; len++)
163 printf("%.*s\n", len, s);
168 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
169 const char *path, int stage, int refresh, int options)
171 struct cache_entry *ce;
172 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
174 return error("addinfo_cache failed for path '%s'", path);
175 return add_cache_entry(ce, options);
179 * This is a global variable which is used in a number of places but
180 * only written to in the 'merge' function.
182 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
183 * don't update the working directory.
184 * 0 => Leave unmerged entries in the cache and update
185 * the working directory.
187 static int index_only = 0;
189 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
192 init_tree_desc(desc, tree->buffer, tree->size);
195 static int git_merge_trees(int index_only,
201 struct tree_desc t[3];
202 struct unpack_trees_options opts;
204 memset(&opts, 0, sizeof(opts));
211 opts.fn = threeway_merge;
213 init_tree_desc_from_tree(t+0, common);
214 init_tree_desc_from_tree(t+1, head);
215 init_tree_desc_from_tree(t+2, merge);
217 rc = unpack_trees(3, t, &opts);
218 cache_tree_free(&active_cache_tree);
222 static int unmerged_index(void)
225 for (i = 0; i < active_nr; i++) {
226 struct cache_entry *ce = active_cache[i];
233 static struct tree *git_write_tree(void)
235 struct tree *result = NULL;
237 if (unmerged_index()) {
239 output(0, "There are unmerged index entries:");
240 for (i = 0; i < active_nr; i++) {
241 struct cache_entry *ce = active_cache[i];
243 output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
248 if (!active_cache_tree)
249 active_cache_tree = cache_tree();
251 if (!cache_tree_fully_valid(active_cache_tree) &&
252 cache_tree_update(active_cache_tree,
253 active_cache, active_nr, 0, 0) < 0)
254 die("error building trees");
256 result = lookup_tree(active_cache_tree->sha1);
261 static int save_files_dirs(const unsigned char *sha1,
262 const char *base, int baselen, const char *path,
263 unsigned int mode, int stage)
265 int len = strlen(path);
266 char *newpath = xmalloc(baselen + len + 1);
267 memcpy(newpath, base, baselen);
268 memcpy(newpath + baselen, path, len);
269 newpath[baselen + len] = '\0';
272 path_list_insert(newpath, ¤t_directory_set);
274 path_list_insert(newpath, ¤t_file_set);
277 return READ_TREE_RECURSIVE;
280 static int get_files_dirs(struct tree *tree)
283 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
285 n = current_file_set.nr + current_directory_set.nr;
290 * Returns an index_entry instance which doesn't have to correspond to
291 * a real cache entry in Git's index.
293 static struct stage_data *insert_stage_data(const char *path,
294 struct tree *o, struct tree *a, struct tree *b,
295 struct path_list *entries)
297 struct path_list_item *item;
298 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
299 get_tree_entry(o->object.sha1, path,
300 e->stages[1].sha, &e->stages[1].mode);
301 get_tree_entry(a->object.sha1, path,
302 e->stages[2].sha, &e->stages[2].mode);
303 get_tree_entry(b->object.sha1, path,
304 e->stages[3].sha, &e->stages[3].mode);
305 item = path_list_insert(path, entries);
311 * Create a dictionary mapping file names to stage_data objects. The
312 * dictionary contains one entry for every path with a non-zero stage entry.
314 static struct path_list *get_unmerged(void)
316 struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
319 unmerged->strdup_paths = 1;
321 for (i = 0; i < active_nr; i++) {
322 struct path_list_item *item;
323 struct stage_data *e;
324 struct cache_entry *ce = active_cache[i];
328 item = path_list_lookup(ce->name, unmerged);
330 item = path_list_insert(ce->name, unmerged);
331 item->util = xcalloc(1, sizeof(struct stage_data));
334 e->stages[ce_stage(ce)].mode = ce->ce_mode;
335 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
343 struct diff_filepair *pair;
344 struct stage_data *src_entry;
345 struct stage_data *dst_entry;
346 unsigned processed:1;
350 * Get information of all renames which occurred between 'o_tree' and
351 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
352 * 'b_tree') to be able to associate the correct cache entries with
353 * the rename information. 'tree' is always equal to either a_tree or b_tree.
355 static struct path_list *get_renames(struct tree *tree,
359 struct path_list *entries)
362 struct path_list *renames;
363 struct diff_options opts;
365 renames = xcalloc(1, sizeof(struct path_list));
367 DIFF_OPT_SET(&opts, RECURSIVE);
368 opts.detect_rename = DIFF_DETECT_RENAME;
369 opts.rename_limit = rename_limit;
370 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
371 if (diff_setup_done(&opts) < 0)
372 die("diff setup failed");
373 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
375 for (i = 0; i < diff_queued_diff.nr; ++i) {
376 struct path_list_item *item;
378 struct diff_filepair *pair = diff_queued_diff.queue[i];
379 if (pair->status != 'R') {
380 diff_free_filepair(pair);
383 re = xmalloc(sizeof(*re));
386 item = path_list_lookup(re->pair->one->path, entries);
388 re->src_entry = insert_stage_data(re->pair->one->path,
389 o_tree, a_tree, b_tree, entries);
391 re->src_entry = item->util;
393 item = path_list_lookup(re->pair->two->path, entries);
395 re->dst_entry = insert_stage_data(re->pair->two->path,
396 o_tree, a_tree, b_tree, entries);
398 re->dst_entry = item->util;
399 item = path_list_insert(pair->one->path, renames);
402 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
403 diff_queued_diff.nr = 0;
408 static int update_stages(const char *path, struct diff_filespec *o,
409 struct diff_filespec *a, struct diff_filespec *b,
412 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
414 if (remove_file_from_cache(path))
417 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
420 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
423 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
428 static int remove_path(const char *name)
436 dirs = xstrdup(name);
437 while ((slash = strrchr(name, '/'))) {
439 if (rmdir(name) != 0)
446 static int remove_file(int clean, const char *path, int no_wd)
448 int update_cache = index_only || clean;
449 int update_working_directory = !index_only && !no_wd;
452 if (remove_file_from_cache(path))
455 if (update_working_directory) {
457 if (errno != ENOENT || errno != EISDIR)
464 static char *unique_path(const char *path, const char *branch)
466 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
469 char *p = newpath + strlen(path);
470 strcpy(newpath, path);
476 while (path_list_has_path(¤t_file_set, newpath) ||
477 path_list_has_path(¤t_directory_set, newpath) ||
478 lstat(newpath, &st) == 0)
479 sprintf(p, "_%d", suffix++);
481 path_list_insert(newpath, ¤t_file_set);
485 static int mkdir_p(const char *path, unsigned long mode)
487 /* path points to cache entries, so xstrdup before messing with it */
488 char *buf = xstrdup(path);
489 int result = safe_create_leading_directories(buf);
494 static void flush_buffer(int fd, const char *buf, unsigned long size)
497 long ret = write_in_full(fd, buf, size);
502 die("merge-recursive: %s", strerror(errno));
504 die("merge-recursive: disk full?");
511 static int make_room_for_path(const char *path)
514 const char *msg = "failed to create path '%s'%s";
516 status = mkdir_p(path, 0777);
519 /* something else exists */
520 error(msg, path, ": perhaps a D/F conflict?");
526 /* Successful unlink is good.. */
529 /* .. and so is no existing file */
532 /* .. but not some other error (who really cares what?) */
533 return error(msg, path, ": perhaps a D/F conflict?");
536 static void update_file_flags(const unsigned char *sha,
546 enum object_type type;
550 if (S_ISGITLINK(mode))
551 die("cannot read object %s '%s': It is a submodule!",
552 sha1_to_hex(sha), path);
554 buf = read_sha1_file(sha, &type, &size);
556 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
557 if (type != OBJ_BLOB)
558 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
560 if (make_room_for_path(path) < 0) {
564 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
570 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
572 die("failed to open %s: %s", path, strerror(errno));
573 flush_buffer(fd, buf, size);
575 } else if (S_ISLNK(mode)) {
576 char *lnk = xmemdupz(buf, size);
582 die("do not know what to do with %06o %s '%s'",
583 mode, sha1_to_hex(sha), path);
587 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
590 static void update_file(int clean,
591 const unsigned char *sha,
595 update_file_flags(sha, mode, path, index_only || clean, !index_only);
598 /* Low level file merging, update and removal */
600 struct merge_file_info
602 unsigned char sha[20];
608 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
611 enum object_type type;
613 if (!hashcmp(sha1, null_sha1)) {
614 mm->ptr = xstrdup("");
619 mm->ptr = read_sha1_file(sha1, &type, &size);
620 if (!mm->ptr || type != OBJ_BLOB)
621 die("unable to read blob object %s", sha1_to_hex(sha1));
625 static int merge_3way(mmbuffer_t *result_buf,
626 struct diff_filespec *o,
627 struct diff_filespec *a,
628 struct diff_filespec *b,
632 mmfile_t orig, src1, src2;
636 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
637 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
639 fill_mm(o->sha1, &orig);
640 fill_mm(a->sha1, &src1);
641 fill_mm(b->sha1, &src2);
643 merge_status = ll_merge(result_buf, a->path, &orig,
644 &src1, name1, &src2, name2,
655 static struct merge_file_info merge_file(struct diff_filespec *o,
656 struct diff_filespec *a, struct diff_filespec *b,
657 const char *branch1, const char *branch2)
659 struct merge_file_info result;
663 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
665 if (S_ISREG(a->mode)) {
666 result.mode = a->mode;
667 hashcpy(result.sha, a->sha1);
669 result.mode = b->mode;
670 hashcpy(result.sha, b->sha1);
673 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
676 result.mode = a->mode == o->mode ? b->mode: a->mode;
678 if (sha_eq(a->sha1, o->sha1))
679 hashcpy(result.sha, b->sha1);
680 else if (sha_eq(b->sha1, o->sha1))
681 hashcpy(result.sha, a->sha1);
682 else if (S_ISREG(a->mode)) {
683 mmbuffer_t result_buf;
686 merge_status = merge_3way(&result_buf, o, a, b,
689 if ((merge_status < 0) || !result_buf.ptr)
690 die("Failed to execute internal merge");
692 if (write_sha1_file(result_buf.ptr, result_buf.size,
693 blob_type, result.sha))
694 die("Unable to add %s to database",
697 free(result_buf.ptr);
698 result.clean = (merge_status == 0);
699 } else if (S_ISGITLINK(a->mode)) {
701 hashcpy(result.sha, a->sha1);
702 } else if (S_ISLNK(a->mode)) {
703 hashcpy(result.sha, a->sha1);
705 if (!sha_eq(a->sha1, b->sha1))
708 die("unsupported object type in the tree");
715 static void conflict_rename_rename(struct rename *ren1,
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 (path_list_has_path(¤t_directory_set, ren1_dst)) {
727 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
728 output(1, "%s is a directory in %s added as %s instead",
729 ren1_dst, branch2, dst_name1);
730 remove_file(0, ren1_dst, 0);
732 if (path_list_has_path(¤t_directory_set, ren2_dst)) {
733 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
734 output(1, "%s is a directory in %s added 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 rename *ren1,
758 char *new_path = unique_path(ren1->pair->two->path, branch1);
759 output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
760 remove_file(0, ren1->pair->two->path, 0);
761 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
765 static void conflict_rename_rename_2(struct rename *ren1,
770 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
771 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
772 output(1, "Renamed %s to %s and %s to %s instead",
773 ren1->pair->one->path, new_path1,
774 ren2->pair->one->path, new_path2);
775 remove_file(0, ren1->pair->two->path, 0);
776 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
777 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
782 static int process_renames(struct path_list *a_renames,
783 struct path_list *b_renames,
784 const char *a_branch,
785 const char *b_branch)
787 int clean_merge = 1, i, j;
788 struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
789 const struct rename *sre;
791 for (i = 0; i < a_renames->nr; i++) {
792 sre = a_renames->items[i].util;
793 path_list_insert(sre->pair->two->path, &a_by_dst)->util
796 for (i = 0; i < b_renames->nr; i++) {
797 sre = b_renames->items[i].util;
798 path_list_insert(sre->pair->two->path, &b_by_dst)->util
802 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
805 struct path_list *renames1, *renames2, *renames2Dst;
806 struct rename *ren1 = NULL, *ren2 = NULL;
807 const char *branch1, *branch2;
808 const char *ren1_src, *ren1_dst;
810 if (i >= a_renames->nr) {
812 ren2 = b_renames->items[j++].util;
813 } else if (j >= b_renames->nr) {
815 ren1 = a_renames->items[i++].util;
817 compare = strcmp(a_renames->items[i].path,
818 b_renames->items[j].path);
820 ren1 = a_renames->items[i++].util;
822 ren2 = b_renames->items[j++].util;
825 /* TODO: refactor, so that 1/2 are not needed */
827 renames1 = a_renames;
828 renames2 = b_renames;
829 renames2Dst = &b_by_dst;
834 renames1 = b_renames;
835 renames2 = a_renames;
836 renames2Dst = &a_by_dst;
843 src = ren1->pair->one->path;
845 ren1->dst_entry->processed = 1;
846 ren1->src_entry->processed = 1;
852 ren1_src = ren1->pair->one->path;
853 ren1_dst = ren1->pair->two->path;
856 const char *ren2_src = ren2->pair->one->path;
857 const char *ren2_dst = ren2->pair->two->path;
858 /* Renamed in 1 and renamed in 2 */
859 if (strcmp(ren1_src, ren2_src) != 0)
860 die("ren1.src != ren2.src");
861 ren2->dst_entry->processed = 1;
863 if (strcmp(ren1_dst, ren2_dst) != 0) {
865 output(1, "CONFLICT (rename/rename): "
866 "Rename \"%s\"->\"%s\" in branch \"%s\" "
867 "rename \"%s\"->\"%s\" in \"%s\"%s",
868 src, ren1_dst, branch1,
869 src, ren2_dst, branch2,
870 index_only ? " (left unresolved)": "");
872 remove_file_from_cache(src);
873 update_file(0, ren1->pair->one->sha1,
874 ren1->pair->one->mode, src);
876 conflict_rename_rename(ren1, branch1, ren2, branch2);
878 struct merge_file_info mfi;
879 remove_file(1, ren1_src, 1);
880 mfi = merge_file(ren1->pair->one,
885 if (mfi.merge || !mfi.clean)
886 output(1, "Renamed %s->%s", src, ren1_dst);
889 output(2, "Auto-merged %s", ren1_dst);
892 output(1, "CONFLICT (content): merge conflict in %s",
897 update_stages(ren1_dst,
903 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
906 /* Renamed in 1, maybe changed in 2 */
907 struct path_list_item *item;
908 /* we only use sha1 and mode of these */
909 struct diff_filespec src_other, dst_other;
910 int try_merge, stage = a_renames == renames1 ? 3: 2;
912 remove_file(1, ren1_src, index_only || stage == 3);
914 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
915 src_other.mode = ren1->src_entry->stages[stage].mode;
916 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
917 dst_other.mode = ren1->dst_entry->stages[stage].mode;
921 if (path_list_has_path(¤t_directory_set, ren1_dst)) {
923 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
924 " directory %s added in %s",
925 ren1_src, ren1_dst, branch1,
927 conflict_rename_dir(ren1, branch1);
928 } else if (sha_eq(src_other.sha1, null_sha1)) {
930 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
932 ren1_src, ren1_dst, branch1,
934 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
935 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
936 const char *new_path;
939 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
941 ren1_src, ren1_dst, branch1,
943 new_path = unique_path(ren1_dst, branch2);
944 output(1, "Added as %s instead", new_path);
945 update_file(0, dst_other.sha1, dst_other.mode, new_path);
946 } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
950 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
951 "Renamed %s->%s in %s",
952 ren1_src, ren1_dst, branch1,
953 ren2->pair->one->path, ren2->pair->two->path, branch2);
954 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
959 struct diff_filespec *o, *a, *b;
960 struct merge_file_info mfi;
961 src_other.path = (char *)ren1_src;
964 if (a_renames == renames1) {
971 mfi = merge_file(o, a, b,
975 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
976 mfi.mode == ren1->pair->two->mode)
978 * This messaged is part of
979 * t6022 test. If you change
980 * it update the test too.
982 output(3, "Skipped %s (merged same as existing)", ren1_dst);
984 if (mfi.merge || !mfi.clean)
985 output(1, "Renamed %s => %s", ren1_src, ren1_dst);
987 output(2, "Auto-merged %s", ren1_dst);
989 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
994 update_stages(ren1_dst,
997 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1002 path_list_clear(&a_by_dst, 0);
1003 path_list_clear(&b_by_dst, 0);
1008 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1010 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1013 /* Per entry merge function */
1014 static int process_entry(const char *path, struct stage_data *entry,
1015 const char *branch1,
1016 const char *branch2)
1019 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1020 print_index_entry("\tpath: ", entry);
1022 int clean_merge = 1;
1023 unsigned o_mode = entry->stages[1].mode;
1024 unsigned a_mode = entry->stages[2].mode;
1025 unsigned b_mode = entry->stages[3].mode;
1026 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1027 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1028 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1030 if (o_sha && (!a_sha || !b_sha)) {
1031 /* Case A: Deleted in one */
1032 if ((!a_sha && !b_sha) ||
1033 (sha_eq(a_sha, o_sha) && !b_sha) ||
1034 (!a_sha && sha_eq(b_sha, o_sha))) {
1035 /* Deleted in both or deleted in one and
1036 * unchanged in the other */
1038 output(2, "Removed %s", path);
1039 /* do not touch working file if it did not exist */
1040 remove_file(1, path, !a_sha);
1042 /* Deleted in one and changed in the other */
1045 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1046 "and modified in %s. Version %s of %s left in tree.",
1048 branch2, branch2, path);
1049 update_file(0, b_sha, b_mode, path);
1051 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1052 "and modified in %s. Version %s of %s left in tree.",
1054 branch1, branch1, path);
1055 update_file(0, a_sha, a_mode, path);
1059 } else if ((!o_sha && a_sha && !b_sha) ||
1060 (!o_sha && !a_sha && b_sha)) {
1061 /* Case B: Added in one. */
1062 const char *add_branch;
1063 const char *other_branch;
1065 const unsigned char *sha;
1069 add_branch = branch1;
1070 other_branch = branch2;
1073 conf = "file/directory";
1075 add_branch = branch2;
1076 other_branch = branch1;
1079 conf = "directory/file";
1081 if (path_list_has_path(¤t_directory_set, path)) {
1082 const char *new_path = unique_path(path, add_branch);
1084 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1086 conf, path, other_branch, path, new_path);
1087 remove_file(0, path, 0);
1088 update_file(0, sha, mode, new_path);
1090 output(2, "Added %s", path);
1091 update_file(1, sha, mode, path);
1093 } else if (a_sha && b_sha) {
1094 /* Case C: Added in both (check for same permissions) and */
1095 /* case D: Modified in both, but differently. */
1096 const char *reason = "content";
1097 struct merge_file_info mfi;
1098 struct diff_filespec o, a, b;
1102 o_sha = (unsigned char *)null_sha1;
1104 output(2, "Auto-merged %s", path);
1105 o.path = a.path = b.path = (char *)path;
1106 hashcpy(o.sha1, o_sha);
1108 hashcpy(a.sha1, a_sha);
1110 hashcpy(b.sha1, b_sha);
1113 mfi = merge_file(&o, &a, &b,
1116 clean_merge = mfi.clean;
1118 update_file(1, mfi.sha, mfi.mode, path);
1119 else if (S_ISGITLINK(mfi.mode))
1120 output(1, "CONFLICT (submodule): Merge conflict in %s "
1121 "- needs %s", path, sha1_to_hex(b.sha1));
1123 output(1, "CONFLICT (%s): Merge conflict in %s",
1127 update_file(0, mfi.sha, mfi.mode, path);
1129 update_file_flags(mfi.sha, mfi.mode, path,
1130 0 /* update_cache */, 1 /* update_working_directory */);
1132 } else if (!o_sha && !a_sha && !b_sha) {
1134 * this entry was deleted altogether. a_mode == 0 means
1135 * we had that path and want to actively remove it.
1137 remove_file(1, path, !a_mode);
1139 die("Fatal merge failure, shouldn't happen.");
1144 static int merge_trees(struct tree *head,
1146 struct tree *common,
1147 const char *branch1,
1148 const char *branch2,
1149 struct tree **result)
1153 if (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(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_index()) {
1172 struct path_list *entries, *re_head, *re_merge;
1174 path_list_clear(¤t_file_set, 1);
1175 path_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(head, common, head, merge, entries);
1181 re_merge = get_renames(merge, common, head, merge, entries);
1182 clean = process_renames(re_head, re_merge,
1184 for (i = 0; i < entries->nr; i++) {
1185 const char *path = entries->items[i].path;
1186 struct stage_data *e = entries->items[i].util;
1188 && !process_entry(path, e, branch1, branch2))
1192 path_list_clear(re_merge, 0);
1193 path_list_clear(re_head, 0);
1194 path_list_clear(entries, 1);
1201 *result = git_write_tree();
1206 static struct commit_list *reverse_commit_list(struct commit_list *list)
1208 struct commit_list *next = NULL, *current, *backup;
1209 for (current = list; current; current = backup) {
1210 backup = current->next;
1211 current->next = next;
1218 * Merge the commits h1 and h2, return the resulting virtual
1219 * commit object and a flag indicating the cleanness of the merge.
1221 static int merge(struct commit *h1,
1223 const char *branch1,
1224 const char *branch2,
1225 struct commit_list *ca,
1226 struct commit **result)
1228 struct commit_list *iter;
1229 struct commit *merged_common_ancestors;
1230 struct tree *mrtree = mrtree;
1234 output(4, "Merging:");
1235 output_commit_title(h1);
1236 output_commit_title(h2);
1240 ca = get_merge_bases(h1, h2, 1);
1241 ca = reverse_commit_list(ca);
1245 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1246 for (iter = ca; iter; iter = iter->next)
1247 output_commit_title(iter->item);
1250 merged_common_ancestors = pop_commit(&ca);
1251 if (merged_common_ancestors == NULL) {
1252 /* if there is no common ancestor, make an empty tree */
1253 struct tree *tree = xcalloc(1, sizeof(struct tree));
1255 tree->object.parsed = 1;
1256 tree->object.type = OBJ_TREE;
1257 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1258 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1261 for (iter = ca; iter; iter = iter->next) {
1264 * When the merge fails, the result contains files
1265 * with conflict markers. The cleanness flag is
1266 * ignored, it was never actually used, as result of
1267 * merge_trees has always overwritten it: the committed
1268 * "conflicts" were already resolved.
1271 merge(merged_common_ancestors, iter->item,
1272 "Temporary merge branch 1",
1273 "Temporary merge branch 2",
1275 &merged_common_ancestors);
1278 if (!merged_common_ancestors)
1279 die("merge returned no commit");
1289 clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1290 branch1, branch2, &mrtree);
1293 *result = make_virtual_commit(mrtree, "merged tree");
1294 commit_list_insert(h1, &(*result)->parents);
1295 commit_list_insert(h2, &(*result)->parents->next);
1301 static const char *better_branch_name(const char *branch)
1303 static char githead_env[8 + 40 + 1];
1306 if (strlen(branch) != 40)
1308 sprintf(githead_env, "GITHEAD_%s", branch);
1309 name = getenv(githead_env);
1310 return name ? name : branch;
1313 static struct commit *get_ref(const char *ref)
1315 unsigned char sha1[20];
1316 struct object *object;
1318 if (get_sha1(ref, sha1))
1319 die("Could not resolve ref '%s'", ref);
1320 object = deref_tag(parse_object(sha1), ref, strlen(ref));
1323 if (object->type == OBJ_TREE)
1324 return make_virtual_commit((struct tree*)object,
1325 better_branch_name(ref));
1326 if (object->type != OBJ_COMMIT)
1328 if (parse_commit((struct commit *)object))
1329 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1330 return (struct commit *)object;
1333 static int merge_config(const char *var, const char *value)
1335 if (!strcasecmp(var, "merge.verbosity")) {
1336 verbosity = git_config_int(var, value);
1339 if (!strcasecmp(var, "diff.renamelimit")) {
1340 rename_limit = git_config_int(var, value);
1343 return git_default_config(var, value);
1346 int main(int argc, char *argv[])
1348 static const char *bases[20];
1349 static unsigned bases_count = 0;
1351 const char *branch1, *branch2;
1352 struct commit *result, *h1, *h2;
1353 struct commit_list *ca = NULL;
1354 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1358 int namelen = strlen(argv[0]);
1360 !strcmp(argv[0] + namelen - 8, "-subtree"))
1364 git_config(merge_config);
1365 if (getenv("GIT_MERGE_VERBOSITY"))
1366 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1369 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1371 for (i = 1; i < argc; ++i) {
1372 if (!strcmp(argv[i], "--"))
1374 if (bases_count < sizeof(bases)/sizeof(*bases))
1375 bases[bases_count++] = argv[i];
1377 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1378 die("Not handling anything other than two heads merge.");
1382 branch1 = argv[++i];
1383 branch2 = argv[++i];
1385 h1 = get_ref(branch1);
1386 h2 = get_ref(branch2);
1388 branch1 = better_branch_name(branch1);
1389 branch2 = better_branch_name(branch2);
1392 printf("Merging %s with %s\n", branch1, branch2);
1394 index_fd = hold_locked_index(lock, 1);
1396 for (i = 0; i < bases_count; i++) {
1397 struct commit *ancestor = get_ref(bases[i]);
1398 ca = commit_list_insert(ancestor, &ca);
1400 clean = merge(h1, h2, branch1, branch2, ca, &result);
1402 if (active_cache_changed &&
1403 (write_cache(index_fd, active_cache, active_nr) ||
1404 commit_locked_index(lock)))
1405 die ("unable to write %s", get_index_file());
1407 return clean ? 0: 1;