Merge branch 'ap/maint-merge-strategy-list-fix' into maint
[git/git-svn.git] / merge-recursive.c
blob1870448d98d56fea31af56953dc33f6529d1d380
1 /*
2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
3 * Fredrik Kuivinen.
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
5 */
6 #include "advice.h"
7 #include "cache.h"
8 #include "cache-tree.h"
9 #include "commit.h"
10 #include "blob.h"
11 #include "builtin.h"
12 #include "tree-walk.h"
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "tag.h"
16 #include "unpack-trees.h"
17 #include "string-list.h"
18 #include "xdiff-interface.h"
19 #include "ll-merge.h"
20 #include "attr.h"
21 #include "merge-recursive.h"
22 #include "dir.h"
24 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
26 unsigned char shifted[20];
29 * NEEDSWORK: this limits the recursion depth to hardcoded
30 * value '2' to avoid excessive overhead.
32 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
33 if (!hashcmp(two->object.sha1, shifted))
34 return two;
35 return lookup_tree(shifted);
39 * A virtual commit has (const char *)commit->util set to the name.
42 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
44 struct commit *commit = xcalloc(1, sizeof(struct commit));
45 commit->tree = tree;
46 commit->util = (void*)comment;
47 /* avoid warnings */
48 commit->object.parsed = 1;
49 return commit;
53 * Since we use get_tree_entry(), which does not put the read object into
54 * the object pool, we cannot rely on a == b.
56 static int sha_eq(const unsigned char *a, const unsigned char *b)
58 if (!a && !b)
59 return 2;
60 return a && b && hashcmp(a, b) == 0;
64 * Since we want to write the index eventually, we cannot reuse the index
65 * for these (temporary) data.
67 struct stage_data
69 struct
71 unsigned mode;
72 unsigned char sha[20];
73 } stages[4];
74 unsigned processed:1;
77 static int show(struct merge_options *o, int v)
79 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
82 static void flush_output(struct merge_options *o)
84 if (o->obuf.len) {
85 fputs(o->obuf.buf, stdout);
86 strbuf_reset(&o->obuf);
90 static void output(struct merge_options *o, int v, const char *fmt, ...)
92 int len;
93 va_list ap;
95 if (!show(o, v))
96 return;
98 strbuf_grow(&o->obuf, o->call_depth * 2 + 2);
99 memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2);
100 strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
102 va_start(ap, fmt);
103 len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
104 va_end(ap);
106 if (len < 0)
107 len = 0;
108 if (len >= strbuf_avail(&o->obuf)) {
109 strbuf_grow(&o->obuf, len + 2);
110 va_start(ap, fmt);
111 len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
112 va_end(ap);
113 if (len >= strbuf_avail(&o->obuf)) {
114 die("this should not happen, your snprintf is broken");
117 strbuf_setlen(&o->obuf, o->obuf.len + len);
118 strbuf_add(&o->obuf, "\n", 1);
119 if (!o->buffer_output)
120 flush_output(o);
123 static void output_commit_title(struct merge_options *o, struct commit *commit)
125 int i;
126 flush_output(o);
127 for (i = o->call_depth; i--;)
128 fputs(" ", stdout);
129 if (commit->util)
130 printf("virtual %s\n", (char *)commit->util);
131 else {
132 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
133 if (parse_commit(commit) != 0)
134 printf("(bad commit)\n");
135 else {
136 const char *s;
137 int len;
138 for (s = commit->buffer; *s; s++)
139 if (*s == '\n' && s[1] == '\n') {
140 s += 2;
141 break;
143 for (len = 0; s[len] && '\n' != s[len]; len++)
144 ; /* do nothing */
145 printf("%.*s\n", len, s);
150 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
151 const char *path, int stage, int refresh, int options)
153 struct cache_entry *ce;
154 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
155 if (!ce)
156 return error("addinfo_cache failed for path '%s'", path);
157 return add_cache_entry(ce, options);
160 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
162 parse_tree(tree);
163 init_tree_desc(desc, tree->buffer, tree->size);
166 static int git_merge_trees(int index_only,
167 struct tree *common,
168 struct tree *head,
169 struct tree *merge)
171 int rc;
172 struct tree_desc t[3];
173 struct unpack_trees_options opts;
174 struct unpack_trees_error_msgs msgs = {
175 /* would_overwrite */
176 "Your local changes to '%s' would be overwritten by merge. Aborting.",
177 /* not_uptodate_file */
178 "Your local changes to '%s' would be overwritten by merge. Aborting.",
179 /* not_uptodate_dir */
180 "Updating '%s' would lose untracked files in it. Aborting.",
181 /* would_lose_untracked */
182 "Untracked working tree file '%s' would be %s by merge. Aborting",
183 /* bind_overlap -- will not happen here */
184 NULL,
186 if (advice_commit_before_merge) {
187 msgs.would_overwrite = msgs.not_uptodate_file =
188 "Your local changes to '%s' would be overwritten by merge. Aborting.\n"
189 "Please, commit your changes or stash them before you can merge.";
192 memset(&opts, 0, sizeof(opts));
193 if (index_only)
194 opts.index_only = 1;
195 else
196 opts.update = 1;
197 opts.merge = 1;
198 opts.head_idx = 2;
199 opts.fn = threeway_merge;
200 opts.src_index = &the_index;
201 opts.dst_index = &the_index;
202 opts.msgs = msgs;
204 init_tree_desc_from_tree(t+0, common);
205 init_tree_desc_from_tree(t+1, head);
206 init_tree_desc_from_tree(t+2, merge);
208 rc = unpack_trees(3, t, &opts);
209 cache_tree_free(&active_cache_tree);
210 return rc;
213 struct tree *write_tree_from_memory(struct merge_options *o)
215 struct tree *result = NULL;
217 if (unmerged_cache()) {
218 int i;
219 output(o, 0, "There are unmerged index entries:");
220 for (i = 0; i < active_nr; i++) {
221 struct cache_entry *ce = active_cache[i];
222 if (ce_stage(ce))
223 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
225 return NULL;
228 if (!active_cache_tree)
229 active_cache_tree = cache_tree();
231 if (!cache_tree_fully_valid(active_cache_tree) &&
232 cache_tree_update(active_cache_tree,
233 active_cache, active_nr, 0, 0) < 0)
234 die("error building trees");
236 result = lookup_tree(active_cache_tree->sha1);
238 return result;
241 static int save_files_dirs(const unsigned char *sha1,
242 const char *base, int baselen, const char *path,
243 unsigned int mode, int stage, void *context)
245 int len = strlen(path);
246 char *newpath = xmalloc(baselen + len + 1);
247 struct merge_options *o = context;
249 memcpy(newpath, base, baselen);
250 memcpy(newpath + baselen, path, len);
251 newpath[baselen + len] = '\0';
253 if (S_ISDIR(mode))
254 string_list_insert(newpath, &o->current_directory_set);
255 else
256 string_list_insert(newpath, &o->current_file_set);
257 free(newpath);
259 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
262 static int get_files_dirs(struct merge_options *o, struct tree *tree)
264 int n;
265 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
266 return 0;
267 n = o->current_file_set.nr + o->current_directory_set.nr;
268 return n;
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);
288 item->util = e;
289 return e;
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));
299 int i;
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];
307 if (!ce_stage(ce))
308 continue;
310 item = string_list_lookup(ce->name, unmerged);
311 if (!item) {
312 item = string_list_insert(ce->name, unmerged);
313 item->util = xcalloc(1, sizeof(struct stage_data));
315 e = item->util;
316 e->stages[ce_stage(ce)].mode = ce->ce_mode;
317 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
320 return unmerged;
323 struct rename
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,
338 struct tree *tree,
339 struct tree *o_tree,
340 struct tree *a_tree,
341 struct tree *b_tree,
342 struct string_list *entries)
344 int i;
345 struct string_list *renames;
346 struct diff_options opts;
348 renames = xcalloc(1, sizeof(struct string_list));
349 diff_setup(&opts);
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 :
354 500;
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);
360 diffcore_std(&opts);
361 for (i = 0; i < diff_queued_diff.nr; ++i) {
362 struct string_list_item *item;
363 struct rename *re;
364 struct diff_filepair *pair = diff_queued_diff.queue[i];
365 if (pair->status != 'R') {
366 diff_free_filepair(pair);
367 continue;
369 re = xmalloc(sizeof(*re));
370 re->processed = 0;
371 re->pair = pair;
372 item = string_list_lookup(re->pair->one->path, entries);
373 if (!item)
374 re->src_entry = insert_stage_data(re->pair->one->path,
375 o_tree, a_tree, b_tree, entries);
376 else
377 re->src_entry = item->util;
379 item = string_list_lookup(re->pair->two->path, entries);
380 if (!item)
381 re->dst_entry = insert_stage_data(re->pair->two->path,
382 o_tree, a_tree, b_tree, entries);
383 else
384 re->dst_entry = item->util;
385 item = string_list_insert(pair->one->path, renames);
386 item->util = re;
388 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
389 diff_queued_diff.nr = 0;
390 diff_flush(&opts);
391 return renames;
394 static int update_stages(const char *path, struct diff_filespec *o,
395 struct diff_filespec *a, struct diff_filespec *b,
396 int clear)
398 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
399 if (clear)
400 if (remove_file_from_cache(path))
401 return -1;
402 if (o)
403 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
404 return -1;
405 if (a)
406 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
407 return -1;
408 if (b)
409 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
410 return -1;
411 return 0;
414 static int remove_file(struct merge_options *o, int clean,
415 const char *path, int no_wd)
417 int update_cache = o->call_depth || clean;
418 int update_working_directory = !o->call_depth && !no_wd;
420 if (update_cache) {
421 if (remove_file_from_cache(path))
422 return -1;
424 if (update_working_directory) {
425 if (remove_path(path) && errno != ENOENT)
426 return -1;
428 return 0;
431 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
433 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
434 int suffix = 0;
435 struct stat st;
436 char *p = newpath + strlen(path);
437 strcpy(newpath, path);
438 *(p++) = '~';
439 strcpy(p, branch);
440 for (; *p; ++p)
441 if ('/' == *p)
442 *p = '_';
443 while (string_list_has_string(&o->current_file_set, newpath) ||
444 string_list_has_string(&o->current_directory_set, newpath) ||
445 lstat(newpath, &st) == 0)
446 sprintf(p, "_%d", suffix++);
448 string_list_insert(newpath, &o->current_file_set);
449 return newpath;
452 static void flush_buffer(int fd, const char *buf, unsigned long size)
454 while (size > 0) {
455 long ret = write_in_full(fd, buf, size);
456 if (ret < 0) {
457 /* Ignore epipe */
458 if (errno == EPIPE)
459 break;
460 die_errno("merge-recursive");
461 } else if (!ret) {
462 die("merge-recursive: disk full?");
464 size -= ret;
465 buf += ret;
469 static int would_lose_untracked(const char *path)
471 int pos = cache_name_pos(path, strlen(path));
473 if (pos < 0)
474 pos = -1 - pos;
475 while (pos < active_nr &&
476 !strcmp(path, active_cache[pos]->name)) {
478 * If stage #0, it is definitely tracked.
479 * If it has stage #2 then it was tracked
480 * before this merge started. All other
481 * cases the path was not tracked.
483 switch (ce_stage(active_cache[pos])) {
484 case 0:
485 case 2:
486 return 0;
488 pos++;
490 return file_exists(path);
493 static int make_room_for_path(const char *path)
495 int status;
496 const char *msg = "failed to create path '%s'%s";
498 status = safe_create_leading_directories_const(path);
499 if (status) {
500 if (status == -3) {
501 /* something else exists */
502 error(msg, path, ": perhaps a D/F conflict?");
503 return -1;
505 die(msg, path, "");
509 * Do not unlink a file in the work tree if we are not
510 * tracking it.
512 if (would_lose_untracked(path))
513 return error("refusing to lose untracked file at '%s'",
514 path);
516 /* Successful unlink is good.. */
517 if (!unlink(path))
518 return 0;
519 /* .. and so is no existing file */
520 if (errno == ENOENT)
521 return 0;
522 /* .. but not some other error (who really cares what?) */
523 return error(msg, path, ": perhaps a D/F conflict?");
526 static void update_file_flags(struct merge_options *o,
527 const unsigned char *sha,
528 unsigned mode,
529 const char *path,
530 int update_cache,
531 int update_wd)
533 if (o->call_depth)
534 update_wd = 0;
536 if (update_wd) {
537 enum object_type type;
538 void *buf;
539 unsigned long size;
541 if (S_ISGITLINK(mode))
543 * We may later decide to recursively descend into
544 * the submodule directory and update its index
545 * and/or work tree, but we do not do that now.
547 goto update_index;
549 buf = read_sha1_file(sha, &type, &size);
550 if (!buf)
551 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
552 if (type != OBJ_BLOB)
553 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
554 if (S_ISREG(mode)) {
555 struct strbuf strbuf = STRBUF_INIT;
556 if (convert_to_working_tree(path, buf, size, &strbuf)) {
557 free(buf);
558 size = strbuf.len;
559 buf = strbuf_detach(&strbuf, NULL);
563 if (make_room_for_path(path) < 0) {
564 update_wd = 0;
565 free(buf);
566 goto update_index;
568 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
569 int fd;
570 if (mode & 0100)
571 mode = 0777;
572 else
573 mode = 0666;
574 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
575 if (fd < 0)
576 die_errno("failed to open '%s'", path);
577 flush_buffer(fd, buf, size);
578 close(fd);
579 } else if (S_ISLNK(mode)) {
580 char *lnk = xmemdupz(buf, size);
581 safe_create_leading_directories_const(path);
582 unlink(path);
583 if (symlink(lnk, path))
584 die_errno("failed to symlink '%s'", path);
585 free(lnk);
586 } else
587 die("do not know what to do with %06o %s '%s'",
588 mode, sha1_to_hex(sha), path);
589 free(buf);
591 update_index:
592 if (update_cache)
593 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
596 static void update_file(struct merge_options *o,
597 int clean,
598 const unsigned char *sha,
599 unsigned mode,
600 const char *path)
602 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
605 /* Low level file merging, update and removal */
607 struct merge_file_info
609 unsigned char sha[20];
610 unsigned mode;
611 unsigned clean:1,
612 merge:1;
615 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
617 unsigned long size;
618 enum object_type type;
620 if (!hashcmp(sha1, null_sha1)) {
621 mm->ptr = xstrdup("");
622 mm->size = 0;
623 return;
626 mm->ptr = read_sha1_file(sha1, &type, &size);
627 if (!mm->ptr || type != OBJ_BLOB)
628 die("unable to read blob object %s", sha1_to_hex(sha1));
629 mm->size = size;
632 static int merge_3way(struct merge_options *o,
633 mmbuffer_t *result_buf,
634 struct diff_filespec *one,
635 struct diff_filespec *a,
636 struct diff_filespec *b,
637 const char *branch1,
638 const char *branch2)
640 mmfile_t orig, src1, src2;
641 char *name1, *name2;
642 int merge_status;
644 if (strcmp(a->path, b->path)) {
645 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
646 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
647 } else {
648 name1 = xstrdup(mkpath("%s", branch1));
649 name2 = xstrdup(mkpath("%s", branch2));
652 fill_mm(one->sha1, &orig);
653 fill_mm(a->sha1, &src1);
654 fill_mm(b->sha1, &src2);
656 merge_status = ll_merge(result_buf, a->path, &orig,
657 &src1, name1, &src2, name2,
658 o->call_depth);
660 free(name1);
661 free(name2);
662 free(orig.ptr);
663 free(src1.ptr);
664 free(src2.ptr);
665 return merge_status;
668 static struct merge_file_info merge_file(struct merge_options *o,
669 struct diff_filespec *one,
670 struct diff_filespec *a,
671 struct diff_filespec *b,
672 const char *branch1,
673 const char *branch2)
675 struct merge_file_info result;
676 result.merge = 0;
677 result.clean = 1;
679 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
680 result.clean = 0;
681 if (S_ISREG(a->mode)) {
682 result.mode = a->mode;
683 hashcpy(result.sha, a->sha1);
684 } else {
685 result.mode = b->mode;
686 hashcpy(result.sha, b->sha1);
688 } else {
689 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
690 result.merge = 1;
693 * Merge modes
695 if (a->mode == b->mode || a->mode == one->mode)
696 result.mode = b->mode;
697 else {
698 result.mode = a->mode;
699 if (b->mode != one->mode) {
700 result.clean = 0;
701 result.merge = 1;
705 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
706 hashcpy(result.sha, b->sha1);
707 else if (sha_eq(b->sha1, one->sha1))
708 hashcpy(result.sha, a->sha1);
709 else if (S_ISREG(a->mode)) {
710 mmbuffer_t result_buf;
711 int merge_status;
713 merge_status = merge_3way(o, &result_buf, one, a, b,
714 branch1, branch2);
716 if ((merge_status < 0) || !result_buf.ptr)
717 die("Failed to execute internal merge");
719 if (write_sha1_file(result_buf.ptr, result_buf.size,
720 blob_type, result.sha))
721 die("Unable to add %s to database",
722 a->path);
724 free(result_buf.ptr);
725 result.clean = (merge_status == 0);
726 } else if (S_ISGITLINK(a->mode)) {
727 result.clean = 0;
728 hashcpy(result.sha, a->sha1);
729 } else if (S_ISLNK(a->mode)) {
730 hashcpy(result.sha, a->sha1);
732 if (!sha_eq(a->sha1, b->sha1))
733 result.clean = 0;
734 } else {
735 die("unsupported object type in the tree");
739 return result;
742 static void conflict_rename_rename(struct merge_options *o,
743 struct rename *ren1,
744 const char *branch1,
745 struct rename *ren2,
746 const char *branch2)
748 char *del[2];
749 int delp = 0;
750 const char *ren1_dst = ren1->pair->two->path;
751 const char *ren2_dst = ren2->pair->two->path;
752 const char *dst_name1 = ren1_dst;
753 const char *dst_name2 = ren2_dst;
754 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
755 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
756 output(o, 1, "%s is a directory in %s adding as %s instead",
757 ren1_dst, branch2, dst_name1);
758 remove_file(o, 0, ren1_dst, 0);
760 if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
761 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
762 output(o, 1, "%s is a directory in %s adding as %s instead",
763 ren2_dst, branch1, dst_name2);
764 remove_file(o, 0, ren2_dst, 0);
766 if (o->call_depth) {
767 remove_file_from_cache(dst_name1);
768 remove_file_from_cache(dst_name2);
770 * Uncomment to leave the conflicting names in the resulting tree
772 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
773 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
775 } else {
776 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
777 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
779 while (delp--)
780 free(del[delp]);
783 static void conflict_rename_dir(struct merge_options *o,
784 struct rename *ren1,
785 const char *branch1)
787 char *new_path = unique_path(o, ren1->pair->two->path, branch1);
788 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
789 remove_file(o, 0, ren1->pair->two->path, 0);
790 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
791 free(new_path);
794 static void conflict_rename_rename_2(struct merge_options *o,
795 struct rename *ren1,
796 const char *branch1,
797 struct rename *ren2,
798 const char *branch2)
800 char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
801 char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
802 output(o, 1, "Renaming %s to %s and %s to %s instead",
803 ren1->pair->one->path, new_path1,
804 ren2->pair->one->path, new_path2);
805 remove_file(o, 0, ren1->pair->two->path, 0);
806 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
807 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
808 free(new_path2);
809 free(new_path1);
812 static int process_renames(struct merge_options *o,
813 struct string_list *a_renames,
814 struct string_list *b_renames)
816 int clean_merge = 1, i, j;
817 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
818 const struct rename *sre;
820 for (i = 0; i < a_renames->nr; i++) {
821 sre = a_renames->items[i].util;
822 string_list_insert(sre->pair->two->path, &a_by_dst)->util
823 = sre->dst_entry;
825 for (i = 0; i < b_renames->nr; i++) {
826 sre = b_renames->items[i].util;
827 string_list_insert(sre->pair->two->path, &b_by_dst)->util
828 = sre->dst_entry;
831 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
832 char *src;
833 struct string_list *renames1, *renames2Dst;
834 struct rename *ren1 = NULL, *ren2 = NULL;
835 const char *branch1, *branch2;
836 const char *ren1_src, *ren1_dst;
838 if (i >= a_renames->nr) {
839 ren2 = b_renames->items[j++].util;
840 } else if (j >= b_renames->nr) {
841 ren1 = a_renames->items[i++].util;
842 } else {
843 int compare = strcmp(a_renames->items[i].string,
844 b_renames->items[j].string);
845 if (compare <= 0)
846 ren1 = a_renames->items[i++].util;
847 if (compare >= 0)
848 ren2 = b_renames->items[j++].util;
851 /* TODO: refactor, so that 1/2 are not needed */
852 if (ren1) {
853 renames1 = a_renames;
854 renames2Dst = &b_by_dst;
855 branch1 = o->branch1;
856 branch2 = o->branch2;
857 } else {
858 struct rename *tmp;
859 renames1 = b_renames;
860 renames2Dst = &a_by_dst;
861 branch1 = o->branch2;
862 branch2 = o->branch1;
863 tmp = ren2;
864 ren2 = ren1;
865 ren1 = tmp;
867 src = ren1->pair->one->path;
869 ren1->dst_entry->processed = 1;
870 ren1->src_entry->processed = 1;
872 if (ren1->processed)
873 continue;
874 ren1->processed = 1;
876 ren1_src = ren1->pair->one->path;
877 ren1_dst = ren1->pair->two->path;
879 if (ren2) {
880 const char *ren2_src = ren2->pair->one->path;
881 const char *ren2_dst = ren2->pair->two->path;
882 /* Renamed in 1 and renamed in 2 */
883 if (strcmp(ren1_src, ren2_src) != 0)
884 die("ren1.src != ren2.src");
885 ren2->dst_entry->processed = 1;
886 ren2->processed = 1;
887 if (strcmp(ren1_dst, ren2_dst) != 0) {
888 clean_merge = 0;
889 output(o, 1, "CONFLICT (rename/rename): "
890 "Rename \"%s\"->\"%s\" in branch \"%s\" "
891 "rename \"%s\"->\"%s\" in \"%s\"%s",
892 src, ren1_dst, branch1,
893 src, ren2_dst, branch2,
894 o->call_depth ? " (left unresolved)": "");
895 if (o->call_depth) {
896 remove_file_from_cache(src);
897 update_file(o, 0, ren1->pair->one->sha1,
898 ren1->pair->one->mode, src);
900 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
901 } else {
902 struct merge_file_info mfi;
903 remove_file(o, 1, ren1_src, 1);
904 mfi = merge_file(o,
905 ren1->pair->one,
906 ren1->pair->two,
907 ren2->pair->two,
908 branch1,
909 branch2);
910 if (mfi.merge || !mfi.clean)
911 output(o, 1, "Renaming %s->%s", src, ren1_dst);
913 if (mfi.merge)
914 output(o, 2, "Auto-merging %s", ren1_dst);
916 if (!mfi.clean) {
917 output(o, 1, "CONFLICT (content): merge conflict in %s",
918 ren1_dst);
919 clean_merge = 0;
921 if (!o->call_depth)
922 update_stages(ren1_dst,
923 ren1->pair->one,
924 ren1->pair->two,
925 ren2->pair->two,
926 1 /* clear */);
928 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
930 } else {
931 /* Renamed in 1, maybe changed in 2 */
932 struct string_list_item *item;
933 /* we only use sha1 and mode of these */
934 struct diff_filespec src_other, dst_other;
935 int try_merge, stage = a_renames == renames1 ? 3: 2;
937 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
939 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
940 src_other.mode = ren1->src_entry->stages[stage].mode;
941 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
942 dst_other.mode = ren1->dst_entry->stages[stage].mode;
944 try_merge = 0;
946 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
947 clean_merge = 0;
948 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
949 " directory %s added in %s",
950 ren1_src, ren1_dst, branch1,
951 ren1_dst, branch2);
952 conflict_rename_dir(o, ren1, branch1);
953 } else if (sha_eq(src_other.sha1, null_sha1)) {
954 clean_merge = 0;
955 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
956 "and deleted in %s",
957 ren1_src, ren1_dst, branch1,
958 branch2);
959 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
960 if (!o->call_depth)
961 update_stages(ren1_dst, NULL,
962 branch1 == o->branch1 ?
963 ren1->pair->two : NULL,
964 branch1 == o->branch1 ?
965 NULL : ren1->pair->two, 1);
966 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
967 const char *new_path;
968 clean_merge = 0;
969 try_merge = 1;
970 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
971 "%s added in %s",
972 ren1_src, ren1_dst, branch1,
973 ren1_dst, branch2);
974 if (o->call_depth) {
975 struct merge_file_info mfi;
976 struct diff_filespec one, a, b;
978 one.path = a.path = b.path =
979 (char *)ren1_dst;
980 hashcpy(one.sha1, null_sha1);
981 one.mode = 0;
982 hashcpy(a.sha1, ren1->pair->two->sha1);
983 a.mode = ren1->pair->two->mode;
984 hashcpy(b.sha1, dst_other.sha1);
985 b.mode = dst_other.mode;
986 mfi = merge_file(o, &one, &a, &b,
987 branch1,
988 branch2);
989 output(o, 1, "Adding merged %s", ren1_dst);
990 update_file(o, 0,
991 mfi.sha,
992 mfi.mode,
993 ren1_dst);
994 } else {
995 new_path = unique_path(o, ren1_dst, branch2);
996 output(o, 1, "Adding as %s instead", new_path);
997 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
999 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
1000 ren2 = item->util;
1001 clean_merge = 0;
1002 ren2->processed = 1;
1003 output(o, 1, "CONFLICT (rename/rename): "
1004 "Rename %s->%s in %s. "
1005 "Rename %s->%s in %s",
1006 ren1_src, ren1_dst, branch1,
1007 ren2->pair->one->path, ren2->pair->two->path, branch2);
1008 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
1009 } else
1010 try_merge = 1;
1012 if (try_merge) {
1013 struct diff_filespec *one, *a, *b;
1014 struct merge_file_info mfi;
1015 src_other.path = (char *)ren1_src;
1017 one = ren1->pair->one;
1018 if (a_renames == renames1) {
1019 a = ren1->pair->two;
1020 b = &src_other;
1021 } else {
1022 b = ren1->pair->two;
1023 a = &src_other;
1025 mfi = merge_file(o, one, a, b,
1026 o->branch1, o->branch2);
1028 if (mfi.clean &&
1029 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1030 mfi.mode == ren1->pair->two->mode)
1032 * This messaged is part of
1033 * t6022 test. If you change
1034 * it update the test too.
1036 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
1037 else {
1038 if (mfi.merge || !mfi.clean)
1039 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
1040 if (mfi.merge)
1041 output(o, 2, "Auto-merging %s", ren1_dst);
1042 if (!mfi.clean) {
1043 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
1044 ren1_dst);
1045 clean_merge = 0;
1047 if (!o->call_depth)
1048 update_stages(ren1_dst,
1049 one, a, b, 1);
1051 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1056 string_list_clear(&a_by_dst, 0);
1057 string_list_clear(&b_by_dst, 0);
1059 return clean_merge;
1062 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1064 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1067 /* Per entry merge function */
1068 static int process_entry(struct merge_options *o,
1069 const char *path, struct stage_data *entry)
1072 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1073 print_index_entry("\tpath: ", entry);
1075 int clean_merge = 1;
1076 unsigned o_mode = entry->stages[1].mode;
1077 unsigned a_mode = entry->stages[2].mode;
1078 unsigned b_mode = entry->stages[3].mode;
1079 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1080 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1081 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1083 if (o_sha && (!a_sha || !b_sha)) {
1084 /* Case A: Deleted in one */
1085 if ((!a_sha && !b_sha) ||
1086 (sha_eq(a_sha, o_sha) && !b_sha) ||
1087 (!a_sha && sha_eq(b_sha, o_sha))) {
1088 /* Deleted in both or deleted in one and
1089 * unchanged in the other */
1090 if (a_sha)
1091 output(o, 2, "Removing %s", path);
1092 /* do not touch working file if it did not exist */
1093 remove_file(o, 1, path, !a_sha);
1094 } else {
1095 /* Deleted in one and changed in the other */
1096 clean_merge = 0;
1097 if (!a_sha) {
1098 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1099 "and modified in %s. Version %s of %s left in tree.",
1100 path, o->branch1,
1101 o->branch2, o->branch2, path);
1102 update_file(o, 0, b_sha, b_mode, path);
1103 } else {
1104 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1105 "and modified in %s. Version %s of %s left in tree.",
1106 path, o->branch2,
1107 o->branch1, o->branch1, path);
1108 update_file(o, 0, a_sha, a_mode, path);
1112 } else if ((!o_sha && a_sha && !b_sha) ||
1113 (!o_sha && !a_sha && b_sha)) {
1114 /* Case B: Added in one. */
1115 const char *add_branch;
1116 const char *other_branch;
1117 unsigned mode;
1118 const unsigned char *sha;
1119 const char *conf;
1121 if (a_sha) {
1122 add_branch = o->branch1;
1123 other_branch = o->branch2;
1124 mode = a_mode;
1125 sha = a_sha;
1126 conf = "file/directory";
1127 } else {
1128 add_branch = o->branch2;
1129 other_branch = o->branch1;
1130 mode = b_mode;
1131 sha = b_sha;
1132 conf = "directory/file";
1134 if (string_list_has_string(&o->current_directory_set, path)) {
1135 const char *new_path = unique_path(o, path, add_branch);
1136 clean_merge = 0;
1137 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1138 "Adding %s as %s",
1139 conf, path, other_branch, path, new_path);
1140 remove_file(o, 0, path, 0);
1141 update_file(o, 0, sha, mode, new_path);
1142 } else {
1143 output(o, 2, "Adding %s", path);
1144 update_file(o, 1, sha, mode, path);
1146 } else if (a_sha && b_sha) {
1147 /* Case C: Added in both (check for same permissions) and */
1148 /* case D: Modified in both, but differently. */
1149 const char *reason = "content";
1150 struct merge_file_info mfi;
1151 struct diff_filespec one, a, b;
1153 if (!o_sha) {
1154 reason = "add/add";
1155 o_sha = (unsigned char *)null_sha1;
1157 output(o, 2, "Auto-merging %s", path);
1158 one.path = a.path = b.path = (char *)path;
1159 hashcpy(one.sha1, o_sha);
1160 one.mode = o_mode;
1161 hashcpy(a.sha1, a_sha);
1162 a.mode = a_mode;
1163 hashcpy(b.sha1, b_sha);
1164 b.mode = b_mode;
1166 mfi = merge_file(o, &one, &a, &b,
1167 o->branch1, o->branch2);
1169 clean_merge = mfi.clean;
1170 if (!mfi.clean) {
1171 if (S_ISGITLINK(mfi.mode))
1172 reason = "submodule";
1173 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1174 reason, path);
1176 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1177 } else if (!o_sha && !a_sha && !b_sha) {
1179 * this entry was deleted altogether. a_mode == 0 means
1180 * we had that path and want to actively remove it.
1182 remove_file(o, 1, path, !a_mode);
1183 } else
1184 die("Fatal merge failure, shouldn't happen.");
1186 return clean_merge;
1189 int merge_trees(struct merge_options *o,
1190 struct tree *head,
1191 struct tree *merge,
1192 struct tree *common,
1193 struct tree **result)
1195 int code, clean;
1197 if (o->subtree_merge) {
1198 merge = shift_tree_object(head, merge);
1199 common = shift_tree_object(head, common);
1202 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1203 output(o, 0, "Already uptodate!");
1204 *result = head;
1205 return 1;
1208 code = git_merge_trees(o->call_depth, common, head, merge);
1210 if (code != 0) {
1211 if (show(o, 4) || o->call_depth)
1212 die("merging of trees %s and %s failed",
1213 sha1_to_hex(head->object.sha1),
1214 sha1_to_hex(merge->object.sha1));
1215 else
1216 exit(128);
1219 if (unmerged_cache()) {
1220 struct string_list *entries, *re_head, *re_merge;
1221 int i;
1222 string_list_clear(&o->current_file_set, 1);
1223 string_list_clear(&o->current_directory_set, 1);
1224 get_files_dirs(o, head);
1225 get_files_dirs(o, merge);
1227 entries = get_unmerged();
1228 re_head = get_renames(o, head, common, head, merge, entries);
1229 re_merge = get_renames(o, merge, common, head, merge, entries);
1230 clean = process_renames(o, re_head, re_merge);
1231 for (i = 0; i < entries->nr; i++) {
1232 const char *path = entries->items[i].string;
1233 struct stage_data *e = entries->items[i].util;
1234 if (!e->processed
1235 && !process_entry(o, path, e))
1236 clean = 0;
1239 string_list_clear(re_merge, 0);
1240 string_list_clear(re_head, 0);
1241 string_list_clear(entries, 1);
1244 else
1245 clean = 1;
1247 if (o->call_depth)
1248 *result = write_tree_from_memory(o);
1250 return clean;
1253 static struct commit_list *reverse_commit_list(struct commit_list *list)
1255 struct commit_list *next = NULL, *current, *backup;
1256 for (current = list; current; current = backup) {
1257 backup = current->next;
1258 current->next = next;
1259 next = current;
1261 return next;
1265 * Merge the commits h1 and h2, return the resulting virtual
1266 * commit object and a flag indicating the cleanness of the merge.
1268 int merge_recursive(struct merge_options *o,
1269 struct commit *h1,
1270 struct commit *h2,
1271 struct commit_list *ca,
1272 struct commit **result)
1274 struct commit_list *iter;
1275 struct commit *merged_common_ancestors;
1276 struct tree *mrtree = mrtree;
1277 int clean;
1279 if (show(o, 4)) {
1280 output(o, 4, "Merging:");
1281 output_commit_title(o, h1);
1282 output_commit_title(o, h2);
1285 if (!ca) {
1286 ca = get_merge_bases(h1, h2, 1);
1287 ca = reverse_commit_list(ca);
1290 if (show(o, 5)) {
1291 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1292 for (iter = ca; iter; iter = iter->next)
1293 output_commit_title(o, iter->item);
1296 merged_common_ancestors = pop_commit(&ca);
1297 if (merged_common_ancestors == NULL) {
1298 /* if there is no common ancestor, make an empty tree */
1299 struct tree *tree = xcalloc(1, sizeof(struct tree));
1301 tree->object.parsed = 1;
1302 tree->object.type = OBJ_TREE;
1303 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1304 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1307 for (iter = ca; iter; iter = iter->next) {
1308 const char *saved_b1, *saved_b2;
1309 o->call_depth++;
1311 * When the merge fails, the result contains files
1312 * with conflict markers. The cleanness flag is
1313 * ignored, it was never actually used, as result of
1314 * merge_trees has always overwritten it: the committed
1315 * "conflicts" were already resolved.
1317 discard_cache();
1318 saved_b1 = o->branch1;
1319 saved_b2 = o->branch2;
1320 o->branch1 = "Temporary merge branch 1";
1321 o->branch2 = "Temporary merge branch 2";
1322 merge_recursive(o, merged_common_ancestors, iter->item,
1323 NULL, &merged_common_ancestors);
1324 o->branch1 = saved_b1;
1325 o->branch2 = saved_b2;
1326 o->call_depth--;
1328 if (!merged_common_ancestors)
1329 die("merge returned no commit");
1332 discard_cache();
1333 if (!o->call_depth)
1334 read_cache();
1336 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1337 &mrtree);
1339 if (o->call_depth) {
1340 *result = make_virtual_commit(mrtree, "merged tree");
1341 commit_list_insert(h1, &(*result)->parents);
1342 commit_list_insert(h2, &(*result)->parents->next);
1344 flush_output(o);
1345 return clean;
1348 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1350 struct object *object;
1352 object = deref_tag(parse_object(sha1), name, strlen(name));
1353 if (!object)
1354 return NULL;
1355 if (object->type == OBJ_TREE)
1356 return make_virtual_commit((struct tree*)object, name);
1357 if (object->type != OBJ_COMMIT)
1358 return NULL;
1359 if (parse_commit((struct commit *)object))
1360 return NULL;
1361 return (struct commit *)object;
1364 int merge_recursive_generic(struct merge_options *o,
1365 const unsigned char *head,
1366 const unsigned char *merge,
1367 int num_base_list,
1368 const unsigned char **base_list,
1369 struct commit **result)
1371 int clean, index_fd;
1372 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1373 struct commit *head_commit = get_ref(head, o->branch1);
1374 struct commit *next_commit = get_ref(merge, o->branch2);
1375 struct commit_list *ca = NULL;
1377 if (base_list) {
1378 int i;
1379 for (i = 0; i < num_base_list; ++i) {
1380 struct commit *base;
1381 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1382 return error("Could not parse object '%s'",
1383 sha1_to_hex(base_list[i]));
1384 commit_list_insert(base, &ca);
1388 index_fd = hold_locked_index(lock, 1);
1389 clean = merge_recursive(o, head_commit, next_commit, ca,
1390 result);
1391 if (active_cache_changed &&
1392 (write_cache(index_fd, active_cache, active_nr) ||
1393 commit_locked_index(lock)))
1394 return error("Unable to write index.");
1396 return clean ? 0 : 1;
1399 static int merge_recursive_config(const char *var, const char *value, void *cb)
1401 struct merge_options *o = cb;
1402 if (!strcasecmp(var, "merge.verbosity")) {
1403 o->verbosity = git_config_int(var, value);
1404 return 0;
1406 if (!strcasecmp(var, "diff.renamelimit")) {
1407 o->diff_rename_limit = git_config_int(var, value);
1408 return 0;
1410 if (!strcasecmp(var, "merge.renamelimit")) {
1411 o->merge_rename_limit = git_config_int(var, value);
1412 return 0;
1414 return git_xmerge_config(var, value, cb);
1417 void init_merge_options(struct merge_options *o)
1419 memset(o, 0, sizeof(struct merge_options));
1420 o->verbosity = 2;
1421 o->buffer_output = 1;
1422 o->diff_rename_limit = -1;
1423 o->merge_rename_limit = -1;
1424 git_config(merge_recursive_config, o);
1425 if (getenv("GIT_MERGE_VERBOSITY"))
1426 o->verbosity =
1427 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1428 if (o->verbosity >= 5)
1429 o->buffer_output = 0;
1430 strbuf_init(&o->obuf, 0);
1431 memset(&o->current_file_set, 0, sizeof(struct string_list));
1432 o->current_file_set.strdup_strings = 1;
1433 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1434 o->current_directory_set.strdup_strings = 1;