Merge branch 'master' into next
[git/mjg.git] / merge-recursive.c
blob6051ee8516b453c5d1c03a18a6734ee959f4b2a0
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"
23 #include "submodule.h"
25 static const char rename_limit_advice[] =
26 "inexact rename detection was skipped because there were too many\n"
27 " files. You may want to set your merge.renamelimit variable to at least\n"
28 " %d and retry this merge.";
30 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
31 const char *subtree_shift)
33 unsigned char shifted[20];
35 if (!*subtree_shift) {
36 shift_tree(one->object.sha1, two->object.sha1, shifted, 0);
37 } else {
38 shift_tree_by(one->object.sha1, two->object.sha1, shifted,
39 subtree_shift);
41 if (!hashcmp(two->object.sha1, shifted))
42 return two;
43 return lookup_tree(shifted);
47 * A virtual commit has (const char *)commit->util set to the name.
50 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
52 struct commit *commit = xcalloc(1, sizeof(struct commit));
53 commit->tree = tree;
54 commit->util = (void*)comment;
55 /* avoid warnings */
56 commit->object.parsed = 1;
57 return commit;
61 * Since we use get_tree_entry(), which does not put the read object into
62 * the object pool, we cannot rely on a == b.
64 static int sha_eq(const unsigned char *a, const unsigned char *b)
66 if (!a && !b)
67 return 2;
68 return a && b && hashcmp(a, b) == 0;
71 enum rename_type {
72 RENAME_NORMAL = 0,
73 RENAME_DELETE,
74 RENAME_ONE_FILE_TO_TWO
77 struct rename_df_conflict_info {
78 enum rename_type rename_type;
79 struct diff_filepair *pair1;
80 struct diff_filepair *pair2;
81 const char *branch1;
82 const char *branch2;
83 struct stage_data *dst_entry1;
84 struct stage_data *dst_entry2;
88 * Since we want to write the index eventually, we cannot reuse the index
89 * for these (temporary) data.
91 struct stage_data
93 struct
95 unsigned mode;
96 unsigned char sha[20];
97 } stages[4];
98 struct rename_df_conflict_info *rename_df_conflict_info;
99 unsigned processed:1;
102 static inline void setup_rename_df_conflict_info(enum rename_type rename_type,
103 struct diff_filepair *pair1,
104 struct diff_filepair *pair2,
105 const char *branch1,
106 const char *branch2,
107 struct stage_data *dst_entry1,
108 struct stage_data *dst_entry2)
110 struct rename_df_conflict_info *ci = xcalloc(1, sizeof(struct rename_df_conflict_info));
111 ci->rename_type = rename_type;
112 ci->pair1 = pair1;
113 ci->branch1 = branch1;
114 ci->branch2 = branch2;
116 ci->dst_entry1 = dst_entry1;
117 dst_entry1->rename_df_conflict_info = ci;
118 dst_entry1->processed = 0;
120 assert(!pair2 == !dst_entry2);
121 if (dst_entry2) {
122 ci->dst_entry2 = dst_entry2;
123 ci->pair2 = pair2;
124 dst_entry2->rename_df_conflict_info = ci;
125 dst_entry2->processed = 0;
129 static int show(struct merge_options *o, int v)
131 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
134 static void flush_output(struct merge_options *o)
136 if (o->obuf.len) {
137 fputs(o->obuf.buf, stdout);
138 strbuf_reset(&o->obuf);
142 __attribute__((format (printf, 3, 4)))
143 static void output(struct merge_options *o, int v, const char *fmt, ...)
145 va_list ap;
147 if (!show(o, v))
148 return;
150 strbuf_grow(&o->obuf, o->call_depth * 2 + 2);
151 memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2);
152 strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
154 va_start(ap, fmt);
155 strbuf_vaddf(&o->obuf, fmt, ap);
156 va_end(ap);
158 strbuf_add(&o->obuf, "\n", 1);
159 if (!o->buffer_output)
160 flush_output(o);
163 static void output_commit_title(struct merge_options *o, struct commit *commit)
165 int i;
166 flush_output(o);
167 for (i = o->call_depth; i--;)
168 fputs(" ", stdout);
169 if (commit->util)
170 printf("virtual %s\n", (char *)commit->util);
171 else {
172 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
173 if (parse_commit(commit) != 0)
174 printf("(bad commit)\n");
175 else {
176 const char *title;
177 int len = find_commit_subject(commit->buffer, &title);
178 if (len)
179 printf("%.*s\n", len, title);
184 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
185 const char *path, int stage, int refresh, int options)
187 struct cache_entry *ce;
188 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
189 if (!ce)
190 return error("addinfo_cache failed for path '%s'", path);
191 return add_cache_entry(ce, options);
194 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
196 parse_tree(tree);
197 init_tree_desc(desc, tree->buffer, tree->size);
200 static int git_merge_trees(int index_only,
201 struct tree *common,
202 struct tree *head,
203 struct tree *merge)
205 int rc;
206 struct tree_desc t[3];
207 struct unpack_trees_options opts;
209 memset(&opts, 0, sizeof(opts));
210 if (index_only)
211 opts.index_only = 1;
212 else
213 opts.update = 1;
214 opts.merge = 1;
215 opts.head_idx = 2;
216 opts.fn = threeway_merge;
217 opts.src_index = &the_index;
218 opts.dst_index = &the_index;
219 setup_unpack_trees_porcelain(&opts, "merge");
221 init_tree_desc_from_tree(t+0, common);
222 init_tree_desc_from_tree(t+1, head);
223 init_tree_desc_from_tree(t+2, merge);
225 rc = unpack_trees(3, t, &opts);
226 cache_tree_free(&active_cache_tree);
227 return rc;
230 struct tree *write_tree_from_memory(struct merge_options *o)
232 struct tree *result = NULL;
234 if (unmerged_cache()) {
235 int i;
236 fprintf(stderr, "BUG: There are unmerged index entries:\n");
237 for (i = 0; i < active_nr; i++) {
238 struct cache_entry *ce = active_cache[i];
239 if (ce_stage(ce))
240 fprintf(stderr, "BUG: %d %.*s", ce_stage(ce),
241 (int)ce_namelen(ce), ce->name);
243 die("Bug in merge-recursive.c");
246 if (!active_cache_tree)
247 active_cache_tree = cache_tree();
249 if (!cache_tree_fully_valid(active_cache_tree) &&
250 cache_tree_update(active_cache_tree,
251 active_cache, active_nr, 0, 0) < 0)
252 die("error building trees");
254 result = lookup_tree(active_cache_tree->sha1);
256 return result;
259 static int save_files_dirs(const unsigned char *sha1,
260 const char *base, int baselen, const char *path,
261 unsigned int mode, int stage, void *context)
263 int len = strlen(path);
264 char *newpath = xmalloc(baselen + len + 1);
265 struct merge_options *o = context;
267 memcpy(newpath, base, baselen);
268 memcpy(newpath + baselen, path, len);
269 newpath[baselen + len] = '\0';
271 if (S_ISDIR(mode))
272 string_list_insert(&o->current_directory_set, newpath);
273 else
274 string_list_insert(&o->current_file_set, newpath);
275 free(newpath);
277 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
280 static int get_files_dirs(struct merge_options *o, struct tree *tree)
282 int n;
283 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
284 return 0;
285 n = o->current_file_set.nr + o->current_directory_set.nr;
286 return n;
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 string_list *entries)
297 struct string_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 = string_list_insert(entries, path);
306 item->util = e;
307 return e;
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 string_list *get_unmerged(void)
316 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
317 int i;
319 unmerged->strdup_strings = 1;
321 for (i = 0; i < active_nr; i++) {
322 struct string_list_item *item;
323 struct stage_data *e;
324 struct cache_entry *ce = active_cache[i];
325 if (!ce_stage(ce))
326 continue;
328 item = string_list_lookup(unmerged, ce->name);
329 if (!item) {
330 item = string_list_insert(unmerged, ce->name);
331 item->util = xcalloc(1, sizeof(struct stage_data));
333 e = item->util;
334 e->stages[ce_stage(ce)].mode = ce->ce_mode;
335 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
338 return unmerged;
341 static void make_room_for_directories_of_df_conflicts(struct merge_options *o,
342 struct string_list *entries)
344 /* If there are D/F conflicts, and the paths currently exist
345 * in the working copy as a file, we want to remove them to
346 * make room for the corresponding directory. Such paths will
347 * later be processed in process_df_entry() at the end. If
348 * the corresponding directory ends up being removed by the
349 * merge, then the file will be reinstated at that time
350 * (albeit with a different timestamp!); otherwise, if the
351 * file is not supposed to be removed by the merge, the
352 * contents of the file will be placed in another unique
353 * filename.
355 * NOTE: This function relies on the fact that entries for a
356 * D/F conflict will appear adjacent in the index, with the
357 * entries for the file appearing before entries for paths
358 * below the corresponding directory.
360 const char *last_file = NULL;
361 int last_len = 0;
362 struct stage_data *last_e;
363 int i;
365 for (i = 0; i < entries->nr; i++) {
366 const char *path = entries->items[i].string;
367 int len = strlen(path);
368 struct stage_data *e = entries->items[i].util;
371 * Check if last_file & path correspond to a D/F conflict;
372 * i.e. whether path is last_file+'/'+<something>.
373 * If so, remove last_file to make room for path and friends.
375 if (last_file &&
376 len > last_len &&
377 memcmp(path, last_file, last_len) == 0 &&
378 path[last_len] == '/') {
379 output(o, 3, "Removing %s to make room for subdirectory; may re-add later.", last_file);
380 unlink(last_file);
384 * Determine whether path could exist as a file in the
385 * working directory as a possible D/F conflict. This
386 * will only occur when it exists in stage 2 as a
387 * file.
389 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
390 last_file = path;
391 last_len = len;
392 last_e = e;
393 } else {
394 last_file = NULL;
399 struct rename
401 struct diff_filepair *pair;
402 struct stage_data *src_entry;
403 struct stage_data *dst_entry;
404 unsigned processed:1;
408 * Get information of all renames which occurred between 'o_tree' and
409 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
410 * 'b_tree') to be able to associate the correct cache entries with
411 * the rename information. 'tree' is always equal to either a_tree or b_tree.
413 static struct string_list *get_renames(struct merge_options *o,
414 struct tree *tree,
415 struct tree *o_tree,
416 struct tree *a_tree,
417 struct tree *b_tree,
418 struct string_list *entries)
420 int i;
421 struct string_list *renames;
422 struct diff_options opts;
424 renames = xcalloc(1, sizeof(struct string_list));
425 diff_setup(&opts);
426 DIFF_OPT_SET(&opts, RECURSIVE);
427 opts.detect_rename = DIFF_DETECT_RENAME;
428 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
429 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
430 1000;
431 opts.rename_score = o->rename_score;
432 opts.show_rename_progress = o->show_rename_progress;
433 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
434 if (diff_setup_done(&opts) < 0)
435 die("diff setup failed");
436 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
437 diffcore_std(&opts);
438 if (opts.needed_rename_limit > o->needed_rename_limit)
439 o->needed_rename_limit = opts.needed_rename_limit;
440 for (i = 0; i < diff_queued_diff.nr; ++i) {
441 struct string_list_item *item;
442 struct rename *re;
443 struct diff_filepair *pair = diff_queued_diff.queue[i];
444 if (pair->status != 'R') {
445 diff_free_filepair(pair);
446 continue;
448 re = xmalloc(sizeof(*re));
449 re->processed = 0;
450 re->pair = pair;
451 item = string_list_lookup(entries, re->pair->one->path);
452 if (!item)
453 re->src_entry = insert_stage_data(re->pair->one->path,
454 o_tree, a_tree, b_tree, entries);
455 else
456 re->src_entry = item->util;
458 item = string_list_lookup(entries, re->pair->two->path);
459 if (!item)
460 re->dst_entry = insert_stage_data(re->pair->two->path,
461 o_tree, a_tree, b_tree, entries);
462 else
463 re->dst_entry = item->util;
464 item = string_list_insert(renames, pair->one->path);
465 item->util = re;
467 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
468 diff_queued_diff.nr = 0;
469 diff_flush(&opts);
470 return renames;
473 static int update_stages_options(const char *path, struct diff_filespec *o,
474 struct diff_filespec *a, struct diff_filespec *b,
475 int clear, int options)
477 if (clear)
478 if (remove_file_from_cache(path))
479 return -1;
480 if (o)
481 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
482 return -1;
483 if (a)
484 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
485 return -1;
486 if (b)
487 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
488 return -1;
489 return 0;
492 static int update_stages(const char *path, struct diff_filespec *o,
493 struct diff_filespec *a, struct diff_filespec *b,
494 int clear)
496 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
497 return update_stages_options(path, o, a, b, clear, options);
500 static int update_stages_and_entry(const char *path,
501 struct stage_data *entry,
502 struct diff_filespec *o,
503 struct diff_filespec *a,
504 struct diff_filespec *b,
505 int clear)
507 int options;
509 entry->processed = 0;
510 entry->stages[1].mode = o->mode;
511 entry->stages[2].mode = a->mode;
512 entry->stages[3].mode = b->mode;
513 hashcpy(entry->stages[1].sha, o->sha1);
514 hashcpy(entry->stages[2].sha, a->sha1);
515 hashcpy(entry->stages[3].sha, b->sha1);
516 options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
517 return update_stages_options(path, o, a, b, clear, options);
520 static int remove_file(struct merge_options *o, int clean,
521 const char *path, int no_wd)
523 int update_cache = o->call_depth || clean;
524 int update_working_directory = !o->call_depth && !no_wd;
526 if (update_cache) {
527 if (remove_file_from_cache(path))
528 return -1;
530 if (update_working_directory) {
531 if (remove_path(path))
532 return -1;
534 return 0;
537 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
539 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
540 int suffix = 0;
541 struct stat st;
542 char *p = newpath + strlen(path);
543 strcpy(newpath, path);
544 *(p++) = '~';
545 strcpy(p, branch);
546 for (; *p; ++p)
547 if ('/' == *p)
548 *p = '_';
549 while (string_list_has_string(&o->current_file_set, newpath) ||
550 string_list_has_string(&o->current_directory_set, newpath) ||
551 lstat(newpath, &st) == 0)
552 sprintf(p, "_%d", suffix++);
554 string_list_insert(&o->current_file_set, newpath);
555 return newpath;
558 static void flush_buffer(int fd, const char *buf, unsigned long size)
560 while (size > 0) {
561 long ret = write_in_full(fd, buf, size);
562 if (ret < 0) {
563 /* Ignore epipe */
564 if (errno == EPIPE)
565 break;
566 die_errno("merge-recursive");
567 } else if (!ret) {
568 die("merge-recursive: disk full?");
570 size -= ret;
571 buf += ret;
575 static int would_lose_untracked(const char *path)
577 int pos = cache_name_pos(path, strlen(path));
579 if (pos < 0)
580 pos = -1 - pos;
581 while (pos < active_nr &&
582 !strcmp(path, active_cache[pos]->name)) {
584 * If stage #0, it is definitely tracked.
585 * If it has stage #2 then it was tracked
586 * before this merge started. All other
587 * cases the path was not tracked.
589 switch (ce_stage(active_cache[pos])) {
590 case 0:
591 case 2:
592 return 0;
594 pos++;
596 return file_exists(path);
599 static int make_room_for_path(const char *path)
601 int status;
602 const char *msg = "failed to create path '%s'%s";
604 status = safe_create_leading_directories_const(path);
605 if (status) {
606 if (status == -3) {
607 /* something else exists */
608 error(msg, path, ": perhaps a D/F conflict?");
609 return -1;
611 die(msg, path, "");
615 * Do not unlink a file in the work tree if we are not
616 * tracking it.
618 if (would_lose_untracked(path))
619 return error("refusing to lose untracked file at '%s'",
620 path);
622 /* Successful unlink is good.. */
623 if (!unlink(path))
624 return 0;
625 /* .. and so is no existing file */
626 if (errno == ENOENT)
627 return 0;
628 /* .. but not some other error (who really cares what?) */
629 return error(msg, path, ": perhaps a D/F conflict?");
632 static void update_file_flags(struct merge_options *o,
633 const unsigned char *sha,
634 unsigned mode,
635 const char *path,
636 int update_cache,
637 int update_wd)
639 if (o->call_depth)
640 update_wd = 0;
642 if (update_wd) {
643 enum object_type type;
644 void *buf;
645 unsigned long size;
647 if (S_ISGITLINK(mode)) {
649 * We may later decide to recursively descend into
650 * the submodule directory and update its index
651 * and/or work tree, but we do not do that now.
653 update_wd = 0;
654 goto update_index;
657 buf = read_sha1_file(sha, &type, &size);
658 if (!buf)
659 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
660 if (type != OBJ_BLOB)
661 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
662 if (S_ISREG(mode)) {
663 struct strbuf strbuf = STRBUF_INIT;
664 if (convert_to_working_tree(path, buf, size, &strbuf)) {
665 free(buf);
666 size = strbuf.len;
667 buf = strbuf_detach(&strbuf, NULL);
671 if (make_room_for_path(path) < 0) {
672 update_wd = 0;
673 free(buf);
674 goto update_index;
676 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
677 int fd;
678 if (mode & 0100)
679 mode = 0777;
680 else
681 mode = 0666;
682 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
683 if (fd < 0)
684 die_errno("failed to open '%s'", path);
685 flush_buffer(fd, buf, size);
686 close(fd);
687 } else if (S_ISLNK(mode)) {
688 char *lnk = xmemdupz(buf, size);
689 safe_create_leading_directories_const(path);
690 unlink(path);
691 if (symlink(lnk, path))
692 die_errno("failed to symlink '%s'", path);
693 free(lnk);
694 } else
695 die("do not know what to do with %06o %s '%s'",
696 mode, sha1_to_hex(sha), path);
697 free(buf);
699 update_index:
700 if (update_cache)
701 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
704 static void update_file(struct merge_options *o,
705 int clean,
706 const unsigned char *sha,
707 unsigned mode,
708 const char *path)
710 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
713 /* Low level file merging, update and removal */
715 struct merge_file_info
717 unsigned char sha[20];
718 unsigned mode;
719 unsigned clean:1,
720 merge:1;
723 static int merge_3way(struct merge_options *o,
724 mmbuffer_t *result_buf,
725 struct diff_filespec *one,
726 struct diff_filespec *a,
727 struct diff_filespec *b,
728 const char *branch1,
729 const char *branch2)
731 mmfile_t orig, src1, src2;
732 struct ll_merge_options ll_opts = {0};
733 char *base_name, *name1, *name2;
734 int merge_status;
736 ll_opts.renormalize = o->renormalize;
737 ll_opts.xdl_opts = o->xdl_opts;
739 if (o->call_depth) {
740 ll_opts.virtual_ancestor = 1;
741 ll_opts.variant = 0;
742 } else {
743 switch (o->recursive_variant) {
744 case MERGE_RECURSIVE_OURS:
745 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
746 break;
747 case MERGE_RECURSIVE_THEIRS:
748 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
749 break;
750 default:
751 ll_opts.variant = 0;
752 break;
756 if (strcmp(a->path, b->path) ||
757 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
758 base_name = o->ancestor == NULL ? NULL :
759 xstrdup(mkpath("%s:%s", o->ancestor, one->path));
760 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
761 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
762 } else {
763 base_name = o->ancestor == NULL ? NULL :
764 xstrdup(mkpath("%s", o->ancestor));
765 name1 = xstrdup(mkpath("%s", branch1));
766 name2 = xstrdup(mkpath("%s", branch2));
769 read_mmblob(&orig, one->sha1);
770 read_mmblob(&src1, a->sha1);
771 read_mmblob(&src2, b->sha1);
773 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
774 &src1, name1, &src2, name2, &ll_opts);
776 free(name1);
777 free(name2);
778 free(orig.ptr);
779 free(src1.ptr);
780 free(src2.ptr);
781 return merge_status;
784 static struct merge_file_info merge_file(struct merge_options *o,
785 struct diff_filespec *one,
786 struct diff_filespec *a,
787 struct diff_filespec *b,
788 const char *branch1,
789 const char *branch2)
791 struct merge_file_info result;
792 result.merge = 0;
793 result.clean = 1;
795 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
796 result.clean = 0;
797 if (S_ISREG(a->mode)) {
798 result.mode = a->mode;
799 hashcpy(result.sha, a->sha1);
800 } else {
801 result.mode = b->mode;
802 hashcpy(result.sha, b->sha1);
804 } else {
805 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
806 result.merge = 1;
809 * Merge modes
811 if (a->mode == b->mode || a->mode == one->mode)
812 result.mode = b->mode;
813 else {
814 result.mode = a->mode;
815 if (b->mode != one->mode) {
816 result.clean = 0;
817 result.merge = 1;
821 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
822 hashcpy(result.sha, b->sha1);
823 else if (sha_eq(b->sha1, one->sha1))
824 hashcpy(result.sha, a->sha1);
825 else if (S_ISREG(a->mode)) {
826 mmbuffer_t result_buf;
827 int merge_status;
829 merge_status = merge_3way(o, &result_buf, one, a, b,
830 branch1, branch2);
832 if ((merge_status < 0) || !result_buf.ptr)
833 die("Failed to execute internal merge");
835 if (write_sha1_file(result_buf.ptr, result_buf.size,
836 blob_type, result.sha))
837 die("Unable to add %s to database",
838 a->path);
840 free(result_buf.ptr);
841 result.clean = (merge_status == 0);
842 } else if (S_ISGITLINK(a->mode)) {
843 result.clean = merge_submodule(result.sha, one->path, one->sha1,
844 a->sha1, b->sha1);
845 } else if (S_ISLNK(a->mode)) {
846 hashcpy(result.sha, a->sha1);
848 if (!sha_eq(a->sha1, b->sha1))
849 result.clean = 0;
850 } else {
851 die("unsupported object type in the tree");
855 return result;
858 static void conflict_rename_delete(struct merge_options *o,
859 struct diff_filepair *pair,
860 const char *rename_branch,
861 const char *other_branch)
863 char *dest_name = pair->two->path;
864 int df_conflict = 0;
865 struct stat st;
867 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
868 "and deleted in %s",
869 pair->one->path, pair->two->path, rename_branch,
870 other_branch);
871 if (!o->call_depth)
872 update_stages(dest_name, NULL,
873 rename_branch == o->branch1 ? pair->two : NULL,
874 rename_branch == o->branch1 ? NULL : pair->two,
876 if (lstat(dest_name, &st) == 0 && S_ISDIR(st.st_mode)) {
877 dest_name = unique_path(o, dest_name, rename_branch);
878 df_conflict = 1;
880 update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
881 if (df_conflict)
882 free(dest_name);
885 static void conflict_rename_rename_1to2(struct merge_options *o,
886 struct diff_filepair *pair1,
887 const char *branch1,
888 struct diff_filepair *pair2,
889 const char *branch2)
891 /* One file was renamed in both branches, but to different names. */
892 char *del[2];
893 int delp = 0;
894 const char *ren1_dst = pair1->two->path;
895 const char *ren2_dst = pair2->two->path;
896 const char *dst_name1 = ren1_dst;
897 const char *dst_name2 = ren2_dst;
898 struct stat st;
899 if (lstat(ren1_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
900 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
901 output(o, 1, "%s is a directory in %s adding as %s instead",
902 ren1_dst, branch2, dst_name1);
904 if (lstat(ren2_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
905 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
906 output(o, 1, "%s is a directory in %s adding as %s instead",
907 ren2_dst, branch1, dst_name2);
909 if (o->call_depth) {
910 remove_file_from_cache(dst_name1);
911 remove_file_from_cache(dst_name2);
913 * Uncomment to leave the conflicting names in the resulting tree
915 * update_file(o, 0, pair1->two->sha1, pair1->two->mode, dst_name1);
916 * update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
918 } else {
919 update_stages(ren1_dst, NULL, pair1->two, NULL, 1);
920 update_stages(ren2_dst, NULL, NULL, pair2->two, 1);
922 update_file(o, 0, pair1->two->sha1, pair1->two->mode, dst_name1);
923 update_file(o, 0, pair2->two->sha1, pair2->two->mode, dst_name2);
925 while (delp--)
926 free(del[delp]);
929 static void conflict_rename_rename_2to1(struct merge_options *o,
930 struct rename *ren1,
931 const char *branch1,
932 struct rename *ren2,
933 const char *branch2)
935 /* Two files were renamed to the same thing. */
936 char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
937 char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
938 output(o, 1, "Renaming %s to %s and %s to %s instead",
939 ren1->pair->one->path, new_path1,
940 ren2->pair->one->path, new_path2);
941 remove_file(o, 0, ren1->pair->two->path, 0);
942 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
943 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
944 free(new_path2);
945 free(new_path1);
948 static int process_renames(struct merge_options *o,
949 struct string_list *a_renames,
950 struct string_list *b_renames)
952 int clean_merge = 1, i, j;
953 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
954 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
955 const struct rename *sre;
957 for (i = 0; i < a_renames->nr; i++) {
958 sre = a_renames->items[i].util;
959 string_list_insert(&a_by_dst, sre->pair->two->path)->util
960 = sre->dst_entry;
962 for (i = 0; i < b_renames->nr; i++) {
963 sre = b_renames->items[i].util;
964 string_list_insert(&b_by_dst, sre->pair->two->path)->util
965 = sre->dst_entry;
968 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
969 char *src;
970 struct string_list *renames1, *renames2Dst;
971 struct rename *ren1 = NULL, *ren2 = NULL;
972 const char *branch1, *branch2;
973 const char *ren1_src, *ren1_dst;
975 if (i >= a_renames->nr) {
976 ren2 = b_renames->items[j++].util;
977 } else if (j >= b_renames->nr) {
978 ren1 = a_renames->items[i++].util;
979 } else {
980 int compare = strcmp(a_renames->items[i].string,
981 b_renames->items[j].string);
982 if (compare <= 0)
983 ren1 = a_renames->items[i++].util;
984 if (compare >= 0)
985 ren2 = b_renames->items[j++].util;
988 /* TODO: refactor, so that 1/2 are not needed */
989 if (ren1) {
990 renames1 = a_renames;
991 renames2Dst = &b_by_dst;
992 branch1 = o->branch1;
993 branch2 = o->branch2;
994 } else {
995 struct rename *tmp;
996 renames1 = b_renames;
997 renames2Dst = &a_by_dst;
998 branch1 = o->branch2;
999 branch2 = o->branch1;
1000 tmp = ren2;
1001 ren2 = ren1;
1002 ren1 = tmp;
1004 src = ren1->pair->one->path;
1006 ren1->dst_entry->processed = 1;
1007 ren1->src_entry->processed = 1;
1009 if (ren1->processed)
1010 continue;
1011 ren1->processed = 1;
1013 ren1_src = ren1->pair->one->path;
1014 ren1_dst = ren1->pair->two->path;
1016 if (ren2) {
1017 const char *ren2_src = ren2->pair->one->path;
1018 const char *ren2_dst = ren2->pair->two->path;
1019 /* Renamed in 1 and renamed in 2 */
1020 if (strcmp(ren1_src, ren2_src) != 0)
1021 die("ren1.src != ren2.src");
1022 ren2->dst_entry->processed = 1;
1023 ren2->processed = 1;
1024 if (strcmp(ren1_dst, ren2_dst) != 0) {
1025 setup_rename_df_conflict_info(RENAME_ONE_FILE_TO_TWO,
1026 ren1->pair,
1027 ren2->pair,
1028 branch1,
1029 branch2,
1030 ren1->dst_entry,
1031 ren2->dst_entry);
1032 } else {
1033 remove_file(o, 1, ren1_src, 1);
1034 update_stages_and_entry(ren1_dst,
1035 ren1->dst_entry,
1036 ren1->pair->one,
1037 ren1->pair->two,
1038 ren2->pair->two,
1039 1 /* clear */);
1041 } else {
1042 /* Renamed in 1, maybe changed in 2 */
1043 struct string_list_item *item;
1044 /* we only use sha1 and mode of these */
1045 struct diff_filespec src_other, dst_other;
1046 int try_merge;
1049 * unpack_trees loads entries from common-commit
1050 * into stage 1, from head-commit into stage 2, and
1051 * from merge-commit into stage 3. We keep track
1052 * of which side corresponds to the rename.
1054 int renamed_stage = a_renames == renames1 ? 2 : 3;
1055 int other_stage = a_renames == renames1 ? 3 : 2;
1057 remove_file(o, 1, ren1_src, o->call_depth || renamed_stage == 2);
1059 hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
1060 src_other.mode = ren1->src_entry->stages[other_stage].mode;
1061 hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
1062 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
1063 try_merge = 0;
1065 if (sha_eq(src_other.sha1, null_sha1)) {
1066 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
1067 ren1->dst_entry->processed = 0;
1068 setup_rename_df_conflict_info(RENAME_DELETE,
1069 ren1->pair,
1070 NULL,
1071 branch1,
1072 branch2,
1073 ren1->dst_entry,
1074 NULL);
1075 } else {
1076 clean_merge = 0;
1077 conflict_rename_delete(o, ren1->pair, branch1, branch2);
1079 } else if ((dst_other.mode == ren1->pair->two->mode) &&
1080 sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
1081 /* Added file on the other side
1082 identical to the file being
1083 renamed: clean merge */
1084 update_file(o, 1, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
1085 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
1086 const char *new_path;
1087 clean_merge = 0;
1088 try_merge = 1;
1089 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
1090 "%s added in %s",
1091 ren1_src, ren1_dst, branch1,
1092 ren1_dst, branch2);
1093 if (o->call_depth) {
1094 struct merge_file_info mfi;
1095 struct diff_filespec one, a, b;
1097 one.path = a.path = b.path =
1098 (char *)ren1_dst;
1099 hashcpy(one.sha1, null_sha1);
1100 one.mode = 0;
1101 hashcpy(a.sha1, ren1->pair->two->sha1);
1102 a.mode = ren1->pair->two->mode;
1103 hashcpy(b.sha1, dst_other.sha1);
1104 b.mode = dst_other.mode;
1105 mfi = merge_file(o, &one, &a, &b,
1106 branch1,
1107 branch2);
1108 output(o, 1, "Adding merged %s", ren1_dst);
1109 update_file(o, 0,
1110 mfi.sha,
1111 mfi.mode,
1112 ren1_dst);
1113 try_merge = 0;
1114 } else {
1115 new_path = unique_path(o, ren1_dst, branch2);
1116 output(o, 1, "Adding as %s instead", new_path);
1117 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
1119 } else if ((item = string_list_lookup(renames2Dst, ren1_dst))) {
1120 ren2 = item->util;
1121 clean_merge = 0;
1122 ren2->processed = 1;
1123 output(o, 1, "CONFLICT (rename/rename): "
1124 "Rename %s->%s in %s. "
1125 "Rename %s->%s in %s",
1126 ren1_src, ren1_dst, branch1,
1127 ren2->pair->one->path, ren2->pair->two->path, branch2);
1128 conflict_rename_rename_2to1(o, ren1, branch1, ren2, branch2);
1129 } else
1130 try_merge = 1;
1132 if (try_merge) {
1133 struct diff_filespec *one, *a, *b;
1134 src_other.path = (char *)ren1_src;
1136 one = ren1->pair->one;
1137 if (a_renames == renames1) {
1138 a = ren1->pair->two;
1139 b = &src_other;
1140 } else {
1141 b = ren1->pair->two;
1142 a = &src_other;
1144 update_stages_and_entry(ren1_dst, ren1->dst_entry, one, a, b, 1);
1145 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
1146 setup_rename_df_conflict_info(RENAME_NORMAL,
1147 ren1->pair,
1148 NULL,
1149 branch1,
1150 NULL,
1151 ren1->dst_entry,
1152 NULL);
1157 string_list_clear(&a_by_dst, 0);
1158 string_list_clear(&b_by_dst, 0);
1160 return clean_merge;
1163 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1165 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1168 static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
1170 void *buf;
1171 enum object_type type;
1172 unsigned long size;
1173 buf = read_sha1_file(sha1, &type, &size);
1174 if (!buf)
1175 return error("cannot read object %s", sha1_to_hex(sha1));
1176 if (type != OBJ_BLOB) {
1177 free(buf);
1178 return error("object %s is not a blob", sha1_to_hex(sha1));
1180 strbuf_attach(dst, buf, size, size + 1);
1181 return 0;
1184 static int blob_unchanged(const unsigned char *o_sha,
1185 const unsigned char *a_sha,
1186 int renormalize, const char *path)
1188 struct strbuf o = STRBUF_INIT;
1189 struct strbuf a = STRBUF_INIT;
1190 int ret = 0; /* assume changed for safety */
1192 if (sha_eq(o_sha, a_sha))
1193 return 1;
1194 if (!renormalize)
1195 return 0;
1197 assert(o_sha && a_sha);
1198 if (read_sha1_strbuf(o_sha, &o) || read_sha1_strbuf(a_sha, &a))
1199 goto error_return;
1201 * Note: binary | is used so that both renormalizations are
1202 * performed. Comparison can be skipped if both files are
1203 * unchanged since their sha1s have already been compared.
1205 if (renormalize_buffer(path, o.buf, o.len, &o) |
1206 renormalize_buffer(path, a.buf, o.len, &a))
1207 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1209 error_return:
1210 strbuf_release(&o);
1211 strbuf_release(&a);
1212 return ret;
1215 static void handle_delete_modify(struct merge_options *o,
1216 const char *path,
1217 const char *new_path,
1218 unsigned char *a_sha, int a_mode,
1219 unsigned char *b_sha, int b_mode)
1221 if (!a_sha) {
1222 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1223 "and modified in %s. Version %s of %s left in tree%s%s.",
1224 path, o->branch1,
1225 o->branch2, o->branch2, path,
1226 path == new_path ? "" : " at ",
1227 path == new_path ? "" : new_path);
1228 update_file(o, 0, b_sha, b_mode, new_path);
1229 } else {
1230 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1231 "and modified in %s. Version %s of %s left in tree%s%s.",
1232 path, o->branch2,
1233 o->branch1, o->branch1, path,
1234 path == new_path ? "" : " at ",
1235 path == new_path ? "" : new_path);
1236 update_file(o, 0, a_sha, a_mode, new_path);
1240 static int merge_content(struct merge_options *o,
1241 const char *path,
1242 unsigned char *o_sha, int o_mode,
1243 unsigned char *a_sha, int a_mode,
1244 unsigned char *b_sha, int b_mode,
1245 const char *df_rename_conflict_branch)
1247 const char *reason = "content";
1248 struct merge_file_info mfi;
1249 struct diff_filespec one, a, b;
1250 struct stat st;
1251 unsigned df_conflict_remains = 0;
1253 if (!o_sha) {
1254 reason = "add/add";
1255 o_sha = (unsigned char *)null_sha1;
1257 one.path = a.path = b.path = (char *)path;
1258 hashcpy(one.sha1, o_sha);
1259 one.mode = o_mode;
1260 hashcpy(a.sha1, a_sha);
1261 a.mode = a_mode;
1262 hashcpy(b.sha1, b_sha);
1263 b.mode = b_mode;
1265 mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
1266 if (df_rename_conflict_branch &&
1267 lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
1268 df_conflict_remains = 1;
1271 if (mfi.clean && !df_conflict_remains &&
1272 sha_eq(mfi.sha, a_sha) && mfi.mode == a.mode &&
1273 lstat(path, &st) == 0) {
1274 output(o, 3, "Skipped %s (merged same as existing)", path);
1275 add_cacheinfo(mfi.mode, mfi.sha, path,
1276 0 /*stage*/, 1 /*refresh*/, 0 /*options*/);
1277 return mfi.clean;
1278 } else
1279 output(o, 2, "Auto-merging %s", path);
1281 if (!mfi.clean) {
1282 if (S_ISGITLINK(mfi.mode))
1283 reason = "submodule";
1284 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1285 reason, path);
1288 if (df_conflict_remains) {
1289 const char *new_path;
1290 update_file_flags(o, mfi.sha, mfi.mode, path,
1291 o->call_depth || mfi.clean, 0);
1292 new_path = unique_path(o, path, df_rename_conflict_branch);
1293 mfi.clean = 0;
1294 output(o, 1, "Adding as %s instead", new_path);
1295 update_file_flags(o, mfi.sha, mfi.mode, new_path, 0, 1);
1296 } else {
1297 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1299 return mfi.clean;
1303 /* Per entry merge function */
1304 static int process_entry(struct merge_options *o,
1305 const char *path, struct stage_data *entry)
1308 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1309 print_index_entry("\tpath: ", entry);
1311 int clean_merge = 1;
1312 int normalize = o->renormalize;
1313 unsigned o_mode = entry->stages[1].mode;
1314 unsigned a_mode = entry->stages[2].mode;
1315 unsigned b_mode = entry->stages[3].mode;
1316 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1317 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1318 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1320 if (entry->rename_df_conflict_info)
1321 return 1; /* Such cases are handled elsewhere. */
1323 entry->processed = 1;
1324 if (o_sha && (!a_sha || !b_sha)) {
1325 /* Case A: Deleted in one */
1326 if ((!a_sha && !b_sha) ||
1327 (!b_sha && blob_unchanged(o_sha, a_sha, normalize, path)) ||
1328 (!a_sha && blob_unchanged(o_sha, b_sha, normalize, path))) {
1329 /* Deleted in both or deleted in one and
1330 * unchanged in the other */
1331 if (a_sha)
1332 output(o, 2, "Removing %s", path);
1333 /* do not touch working file if it did not exist */
1334 remove_file(o, 1, path, !a_sha);
1335 } else if (string_list_has_string(&o->current_directory_set,
1336 path)) {
1337 entry->processed = 0;
1338 return 1; /* Assume clean until processed */
1339 } else {
1340 /* Deleted in one and changed in the other */
1341 clean_merge = 0;
1342 handle_delete_modify(o, path, path,
1343 a_sha, a_mode, b_sha, b_mode);
1346 } else if ((!o_sha && a_sha && !b_sha) ||
1347 (!o_sha && !a_sha && b_sha)) {
1348 /* Case B: Added in one. */
1349 unsigned mode;
1350 const unsigned char *sha;
1352 if (a_sha) {
1353 mode = a_mode;
1354 sha = a_sha;
1355 } else {
1356 mode = b_mode;
1357 sha = b_sha;
1359 if (string_list_has_string(&o->current_directory_set, path)) {
1360 /* Handle D->F conflicts after all subfiles */
1361 entry->processed = 0;
1362 return 1; /* Assume clean until processed */
1363 } else {
1364 output(o, 2, "Adding %s", path);
1365 update_file(o, 1, sha, mode, path);
1367 } else if (a_sha && b_sha) {
1368 /* Case C: Added in both (check for same permissions) and */
1369 /* case D: Modified in both, but differently. */
1370 clean_merge = merge_content(o, path,
1371 o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
1372 NULL);
1373 } else if (!o_sha && !a_sha && !b_sha) {
1375 * this entry was deleted altogether. a_mode == 0 means
1376 * we had that path and want to actively remove it.
1378 remove_file(o, 1, path, !a_mode);
1379 } else
1380 die("Fatal merge failure, shouldn't happen.");
1382 return clean_merge;
1386 * Per entry merge function for D/F (and/or rename) conflicts. In the
1387 * cases we can cleanly resolve D/F conflicts, process_entry() can
1388 * clean out all the files below the directory for us. All D/F
1389 * conflict cases must be handled here at the end to make sure any
1390 * directories that can be cleaned out, are.
1392 * Some rename conflicts may also be handled here that don't necessarily
1393 * involve D/F conflicts, since the code to handle them is generic enough
1394 * to handle those rename conflicts with or without D/F conflicts also
1395 * being involved.
1397 static int process_df_entry(struct merge_options *o,
1398 const char *path, struct stage_data *entry)
1400 int clean_merge = 1;
1401 unsigned o_mode = entry->stages[1].mode;
1402 unsigned a_mode = entry->stages[2].mode;
1403 unsigned b_mode = entry->stages[3].mode;
1404 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1405 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1406 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1407 struct stat st;
1409 entry->processed = 1;
1410 if (entry->rename_df_conflict_info) {
1411 struct rename_df_conflict_info *conflict_info = entry->rename_df_conflict_info;
1412 char *src;
1413 switch (conflict_info->rename_type) {
1414 case RENAME_NORMAL:
1415 clean_merge = merge_content(o, path,
1416 o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
1417 conflict_info->branch1);
1418 break;
1419 case RENAME_DELETE:
1420 clean_merge = 0;
1421 conflict_rename_delete(o, conflict_info->pair1,
1422 conflict_info->branch1,
1423 conflict_info->branch2);
1424 break;
1425 case RENAME_ONE_FILE_TO_TWO:
1426 src = conflict_info->pair1->one->path;
1427 clean_merge = 0;
1428 output(o, 1, "CONFLICT (rename/rename): "
1429 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1430 "rename \"%s\"->\"%s\" in \"%s\"%s",
1431 src, conflict_info->pair1->two->path, conflict_info->branch1,
1432 src, conflict_info->pair2->two->path, conflict_info->branch2,
1433 o->call_depth ? " (left unresolved)" : "");
1434 if (o->call_depth) {
1435 remove_file_from_cache(src);
1436 update_file(o, 0, conflict_info->pair1->one->sha1,
1437 conflict_info->pair1->one->mode, src);
1439 conflict_rename_rename_1to2(o, conflict_info->pair1,
1440 conflict_info->branch1,
1441 conflict_info->pair2,
1442 conflict_info->branch2);
1443 conflict_info->dst_entry2->processed = 1;
1444 break;
1445 default:
1446 entry->processed = 0;
1447 break;
1449 } else if (o_sha && (!a_sha || !b_sha)) {
1450 /* Modify/delete; deleted side may have put a directory in the way */
1451 const char *new_path = path;
1452 if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode))
1453 new_path = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
1454 clean_merge = 0;
1455 handle_delete_modify(o, path, new_path,
1456 a_sha, a_mode, b_sha, b_mode);
1457 } else if (!o_sha && !!a_sha != !!b_sha) {
1458 /* directory -> (directory, file) */
1459 const char *add_branch;
1460 const char *other_branch;
1461 unsigned mode;
1462 const unsigned char *sha;
1463 const char *conf;
1465 if (a_sha) {
1466 add_branch = o->branch1;
1467 other_branch = o->branch2;
1468 mode = a_mode;
1469 sha = a_sha;
1470 conf = "file/directory";
1471 } else {
1472 add_branch = o->branch2;
1473 other_branch = o->branch1;
1474 mode = b_mode;
1475 sha = b_sha;
1476 conf = "directory/file";
1478 if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
1479 const char *new_path = unique_path(o, path, add_branch);
1480 clean_merge = 0;
1481 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1482 "Adding %s as %s",
1483 conf, path, other_branch, path, new_path);
1484 update_file(o, 0, sha, mode, new_path);
1485 } else {
1486 output(o, 2, "Adding %s", path);
1487 update_file(o, 1, sha, mode, path);
1489 } else {
1490 entry->processed = 0;
1491 return 1; /* not handled; assume clean until processed */
1494 return clean_merge;
1497 int merge_trees(struct merge_options *o,
1498 struct tree *head,
1499 struct tree *merge,
1500 struct tree *common,
1501 struct tree **result)
1503 int code, clean;
1505 if (o->subtree_shift) {
1506 merge = shift_tree_object(head, merge, o->subtree_shift);
1507 common = shift_tree_object(head, common, o->subtree_shift);
1510 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1511 output(o, 0, "Already up-to-date!");
1512 *result = head;
1513 return 1;
1516 code = git_merge_trees(o->call_depth, common, head, merge);
1518 if (code != 0) {
1519 if (show(o, 4) || o->call_depth)
1520 die("merging of trees %s and %s failed",
1521 sha1_to_hex(head->object.sha1),
1522 sha1_to_hex(merge->object.sha1));
1523 else
1524 exit(128);
1527 if (unmerged_cache()) {
1528 struct string_list *entries, *re_head, *re_merge;
1529 int i;
1530 string_list_clear(&o->current_file_set, 1);
1531 string_list_clear(&o->current_directory_set, 1);
1532 get_files_dirs(o, head);
1533 get_files_dirs(o, merge);
1535 entries = get_unmerged();
1536 make_room_for_directories_of_df_conflicts(o, entries);
1537 re_head = get_renames(o, head, common, head, merge, entries);
1538 re_merge = get_renames(o, merge, common, head, merge, entries);
1539 clean = process_renames(o, re_head, re_merge);
1540 for (i = 0; i < entries->nr; i++) {
1541 const char *path = entries->items[i].string;
1542 struct stage_data *e = entries->items[i].util;
1543 if (!e->processed
1544 && !process_entry(o, path, e))
1545 clean = 0;
1547 for (i = 0; i < entries->nr; i++) {
1548 const char *path = entries->items[i].string;
1549 struct stage_data *e = entries->items[i].util;
1550 if (!e->processed
1551 && !process_df_entry(o, path, e))
1552 clean = 0;
1554 for (i = 0; i < entries->nr; i++) {
1555 struct stage_data *e = entries->items[i].util;
1556 if (!e->processed)
1557 die("Unprocessed path??? %s",
1558 entries->items[i].string);
1561 string_list_clear(re_merge, 0);
1562 string_list_clear(re_head, 0);
1563 string_list_clear(entries, 1);
1566 else
1567 clean = 1;
1569 if (o->call_depth)
1570 *result = write_tree_from_memory(o);
1572 return clean;
1575 static struct commit_list *reverse_commit_list(struct commit_list *list)
1577 struct commit_list *next = NULL, *current, *backup;
1578 for (current = list; current; current = backup) {
1579 backup = current->next;
1580 current->next = next;
1581 next = current;
1583 return next;
1587 * Merge the commits h1 and h2, return the resulting virtual
1588 * commit object and a flag indicating the cleanness of the merge.
1590 int merge_recursive(struct merge_options *o,
1591 struct commit *h1,
1592 struct commit *h2,
1593 struct commit_list *ca,
1594 struct commit **result)
1596 struct commit_list *iter;
1597 struct commit *merged_common_ancestors;
1598 struct tree *mrtree = mrtree;
1599 int clean;
1601 if (show(o, 4)) {
1602 output(o, 4, "Merging:");
1603 output_commit_title(o, h1);
1604 output_commit_title(o, h2);
1607 if (!ca) {
1608 ca = get_merge_bases(h1, h2, 1);
1609 ca = reverse_commit_list(ca);
1612 if (show(o, 5)) {
1613 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1614 for (iter = ca; iter; iter = iter->next)
1615 output_commit_title(o, iter->item);
1618 merged_common_ancestors = pop_commit(&ca);
1619 if (merged_common_ancestors == NULL) {
1620 /* if there is no common ancestor, make an empty tree */
1621 struct tree *tree = xcalloc(1, sizeof(struct tree));
1623 tree->object.parsed = 1;
1624 tree->object.type = OBJ_TREE;
1625 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1626 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1629 for (iter = ca; iter; iter = iter->next) {
1630 const char *saved_b1, *saved_b2;
1631 o->call_depth++;
1633 * When the merge fails, the result contains files
1634 * with conflict markers. The cleanness flag is
1635 * ignored, it was never actually used, as result of
1636 * merge_trees has always overwritten it: the committed
1637 * "conflicts" were already resolved.
1639 discard_cache();
1640 saved_b1 = o->branch1;
1641 saved_b2 = o->branch2;
1642 o->branch1 = "Temporary merge branch 1";
1643 o->branch2 = "Temporary merge branch 2";
1644 merge_recursive(o, merged_common_ancestors, iter->item,
1645 NULL, &merged_common_ancestors);
1646 o->branch1 = saved_b1;
1647 o->branch2 = saved_b2;
1648 o->call_depth--;
1650 if (!merged_common_ancestors)
1651 die("merge returned no commit");
1654 discard_cache();
1655 if (!o->call_depth)
1656 read_cache();
1658 o->ancestor = "merged common ancestors";
1659 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1660 &mrtree);
1662 if (o->call_depth) {
1663 *result = make_virtual_commit(mrtree, "merged tree");
1664 commit_list_insert(h1, &(*result)->parents);
1665 commit_list_insert(h2, &(*result)->parents->next);
1667 flush_output(o);
1668 if (o->needed_rename_limit)
1669 warning(rename_limit_advice, o->needed_rename_limit);
1670 return clean;
1673 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1675 struct object *object;
1677 object = deref_tag(parse_object(sha1), name, strlen(name));
1678 if (!object)
1679 return NULL;
1680 if (object->type == OBJ_TREE)
1681 return make_virtual_commit((struct tree*)object, name);
1682 if (object->type != OBJ_COMMIT)
1683 return NULL;
1684 if (parse_commit((struct commit *)object))
1685 return NULL;
1686 return (struct commit *)object;
1689 int merge_recursive_generic(struct merge_options *o,
1690 const unsigned char *head,
1691 const unsigned char *merge,
1692 int num_base_list,
1693 const unsigned char **base_list,
1694 struct commit **result)
1696 int clean, index_fd;
1697 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1698 struct commit *head_commit = get_ref(head, o->branch1);
1699 struct commit *next_commit = get_ref(merge, o->branch2);
1700 struct commit_list *ca = NULL;
1702 if (base_list) {
1703 int i;
1704 for (i = 0; i < num_base_list; ++i) {
1705 struct commit *base;
1706 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1707 return error("Could not parse object '%s'",
1708 sha1_to_hex(base_list[i]));
1709 commit_list_insert(base, &ca);
1713 index_fd = hold_locked_index(lock, 1);
1714 clean = merge_recursive(o, head_commit, next_commit, ca,
1715 result);
1716 if (active_cache_changed &&
1717 (write_cache(index_fd, active_cache, active_nr) ||
1718 commit_locked_index(lock)))
1719 return error("Unable to write index.");
1721 return clean ? 0 : 1;
1724 static int merge_recursive_config(const char *var, const char *value, void *cb)
1726 struct merge_options *o = cb;
1727 if (!strcasecmp(var, "merge.verbosity")) {
1728 o->verbosity = git_config_int(var, value);
1729 return 0;
1731 if (!strcasecmp(var, "diff.renamelimit")) {
1732 o->diff_rename_limit = git_config_int(var, value);
1733 return 0;
1735 if (!strcasecmp(var, "merge.renamelimit")) {
1736 o->merge_rename_limit = git_config_int(var, value);
1737 return 0;
1739 return git_xmerge_config(var, value, cb);
1742 void init_merge_options(struct merge_options *o)
1744 memset(o, 0, sizeof(struct merge_options));
1745 o->verbosity = 2;
1746 o->buffer_output = 1;
1747 o->diff_rename_limit = -1;
1748 o->merge_rename_limit = -1;
1749 o->renormalize = 0;
1750 git_config(merge_recursive_config, o);
1751 if (getenv("GIT_MERGE_VERBOSITY"))
1752 o->verbosity =
1753 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1754 if (o->verbosity >= 5)
1755 o->buffer_output = 0;
1756 strbuf_init(&o->obuf, 0);
1757 memset(&o->current_file_set, 0, sizeof(struct string_list));
1758 o->current_file_set.strdup_strings = 1;
1759 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1760 o->current_directory_set.strdup_strings = 1;
1763 int parse_merge_opt(struct merge_options *o, const char *s)
1765 if (!s || !*s)
1766 return -1;
1767 if (!strcmp(s, "ours"))
1768 o->recursive_variant = MERGE_RECURSIVE_OURS;
1769 else if (!strcmp(s, "theirs"))
1770 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
1771 else if (!strcmp(s, "subtree"))
1772 o->subtree_shift = "";
1773 else if (!prefixcmp(s, "subtree="))
1774 o->subtree_shift = s + strlen("subtree=");
1775 else if (!strcmp(s, "patience"))
1776 o->xdl_opts |= XDF_PATIENCE_DIFF;
1777 else if (!strcmp(s, "ignore-space-change"))
1778 o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1779 else if (!strcmp(s, "ignore-all-space"))
1780 o->xdl_opts |= XDF_IGNORE_WHITESPACE;
1781 else if (!strcmp(s, "ignore-space-at-eol"))
1782 o->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
1783 else if (!strcmp(s, "renormalize"))
1784 o->renormalize = 1;
1785 else if (!strcmp(s, "no-renormalize"))
1786 o->renormalize = 0;
1787 else if (!prefixcmp(s, "rename-threshold=")) {
1788 const char *score = s + strlen("rename-threshold=");
1789 if ((o->rename_score = parse_rename_score(&score)) == -1 || *score != 0)
1790 return -1;
1792 else
1793 return -1;
1794 return 0;