approxidate_careful() reports errorneous date string
[git/mjg.git] / merge-recursive.c
blobcd3628c4d9b1831c73e752b3b49c9d88b9e23098
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;
175 memset(&opts, 0, sizeof(opts));
176 if (index_only)
177 opts.index_only = 1;
178 else
179 opts.update = 1;
180 opts.merge = 1;
181 opts.head_idx = 2;
182 opts.fn = threeway_merge;
183 opts.src_index = &the_index;
184 opts.dst_index = &the_index;
185 opts.msgs = get_porcelain_error_msgs();
187 init_tree_desc_from_tree(t+0, common);
188 init_tree_desc_from_tree(t+1, head);
189 init_tree_desc_from_tree(t+2, merge);
191 rc = unpack_trees(3, t, &opts);
192 cache_tree_free(&active_cache_tree);
193 return rc;
196 struct tree *write_tree_from_memory(struct merge_options *o)
198 struct tree *result = NULL;
200 if (unmerged_cache()) {
201 int i;
202 output(o, 0, "There are unmerged index entries:");
203 for (i = 0; i < active_nr; i++) {
204 struct cache_entry *ce = active_cache[i];
205 if (ce_stage(ce))
206 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
208 return NULL;
211 if (!active_cache_tree)
212 active_cache_tree = cache_tree();
214 if (!cache_tree_fully_valid(active_cache_tree) &&
215 cache_tree_update(active_cache_tree,
216 active_cache, active_nr, 0, 0) < 0)
217 die("error building trees");
219 result = lookup_tree(active_cache_tree->sha1);
221 return result;
224 static int save_files_dirs(const unsigned char *sha1,
225 const char *base, int baselen, const char *path,
226 unsigned int mode, int stage, void *context)
228 int len = strlen(path);
229 char *newpath = xmalloc(baselen + len + 1);
230 struct merge_options *o = context;
232 memcpy(newpath, base, baselen);
233 memcpy(newpath + baselen, path, len);
234 newpath[baselen + len] = '\0';
236 if (S_ISDIR(mode))
237 string_list_insert(newpath, &o->current_directory_set);
238 else
239 string_list_insert(newpath, &o->current_file_set);
240 free(newpath);
242 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
245 static int get_files_dirs(struct merge_options *o, struct tree *tree)
247 int n;
248 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
249 return 0;
250 n = o->current_file_set.nr + o->current_directory_set.nr;
251 return n;
255 * Returns an index_entry instance which doesn't have to correspond to
256 * a real cache entry in Git's index.
258 static struct stage_data *insert_stage_data(const char *path,
259 struct tree *o, struct tree *a, struct tree *b,
260 struct string_list *entries)
262 struct string_list_item *item;
263 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
264 get_tree_entry(o->object.sha1, path,
265 e->stages[1].sha, &e->stages[1].mode);
266 get_tree_entry(a->object.sha1, path,
267 e->stages[2].sha, &e->stages[2].mode);
268 get_tree_entry(b->object.sha1, path,
269 e->stages[3].sha, &e->stages[3].mode);
270 item = string_list_insert(path, entries);
271 item->util = e;
272 return e;
276 * Create a dictionary mapping file names to stage_data objects. The
277 * dictionary contains one entry for every path with a non-zero stage entry.
279 static struct string_list *get_unmerged(void)
281 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
282 int i;
284 unmerged->strdup_strings = 1;
286 for (i = 0; i < active_nr; i++) {
287 struct string_list_item *item;
288 struct stage_data *e;
289 struct cache_entry *ce = active_cache[i];
290 if (!ce_stage(ce))
291 continue;
293 item = string_list_lookup(ce->name, unmerged);
294 if (!item) {
295 item = string_list_insert(ce->name, unmerged);
296 item->util = xcalloc(1, sizeof(struct stage_data));
298 e = item->util;
299 e->stages[ce_stage(ce)].mode = ce->ce_mode;
300 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
303 return unmerged;
306 struct rename
308 struct diff_filepair *pair;
309 struct stage_data *src_entry;
310 struct stage_data *dst_entry;
311 unsigned processed:1;
315 * Get information of all renames which occurred between 'o_tree' and
316 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
317 * 'b_tree') to be able to associate the correct cache entries with
318 * the rename information. 'tree' is always equal to either a_tree or b_tree.
320 static struct string_list *get_renames(struct merge_options *o,
321 struct tree *tree,
322 struct tree *o_tree,
323 struct tree *a_tree,
324 struct tree *b_tree,
325 struct string_list *entries)
327 int i;
328 struct string_list *renames;
329 struct diff_options opts;
331 renames = xcalloc(1, sizeof(struct string_list));
332 diff_setup(&opts);
333 DIFF_OPT_SET(&opts, RECURSIVE);
334 opts.detect_rename = DIFF_DETECT_RENAME;
335 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
336 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
337 500;
338 opts.warn_on_too_large_rename = 1;
339 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
340 if (diff_setup_done(&opts) < 0)
341 die("diff setup failed");
342 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
343 diffcore_std(&opts);
344 for (i = 0; i < diff_queued_diff.nr; ++i) {
345 struct string_list_item *item;
346 struct rename *re;
347 struct diff_filepair *pair = diff_queued_diff.queue[i];
348 if (pair->status != 'R') {
349 diff_free_filepair(pair);
350 continue;
352 re = xmalloc(sizeof(*re));
353 re->processed = 0;
354 re->pair = pair;
355 item = string_list_lookup(re->pair->one->path, entries);
356 if (!item)
357 re->src_entry = insert_stage_data(re->pair->one->path,
358 o_tree, a_tree, b_tree, entries);
359 else
360 re->src_entry = item->util;
362 item = string_list_lookup(re->pair->two->path, entries);
363 if (!item)
364 re->dst_entry = insert_stage_data(re->pair->two->path,
365 o_tree, a_tree, b_tree, entries);
366 else
367 re->dst_entry = item->util;
368 item = string_list_insert(pair->one->path, renames);
369 item->util = re;
371 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
372 diff_queued_diff.nr = 0;
373 diff_flush(&opts);
374 return renames;
377 static int update_stages(const char *path, struct diff_filespec *o,
378 struct diff_filespec *a, struct diff_filespec *b,
379 int clear)
381 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
382 if (clear)
383 if (remove_file_from_cache(path))
384 return -1;
385 if (o)
386 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
387 return -1;
388 if (a)
389 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
390 return -1;
391 if (b)
392 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
393 return -1;
394 return 0;
397 static int remove_file(struct merge_options *o, int clean,
398 const char *path, int no_wd)
400 int update_cache = o->call_depth || clean;
401 int update_working_directory = !o->call_depth && !no_wd;
403 if (update_cache) {
404 if (remove_file_from_cache(path))
405 return -1;
407 if (update_working_directory) {
408 if (remove_path(path) && errno != ENOENT)
409 return -1;
411 return 0;
414 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
416 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
417 int suffix = 0;
418 struct stat st;
419 char *p = newpath + strlen(path);
420 strcpy(newpath, path);
421 *(p++) = '~';
422 strcpy(p, branch);
423 for (; *p; ++p)
424 if ('/' == *p)
425 *p = '_';
426 while (string_list_has_string(&o->current_file_set, newpath) ||
427 string_list_has_string(&o->current_directory_set, newpath) ||
428 lstat(newpath, &st) == 0)
429 sprintf(p, "_%d", suffix++);
431 string_list_insert(newpath, &o->current_file_set);
432 return newpath;
435 static void flush_buffer(int fd, const char *buf, unsigned long size)
437 while (size > 0) {
438 long ret = write_in_full(fd, buf, size);
439 if (ret < 0) {
440 /* Ignore epipe */
441 if (errno == EPIPE)
442 break;
443 die_errno("merge-recursive");
444 } else if (!ret) {
445 die("merge-recursive: disk full?");
447 size -= ret;
448 buf += ret;
452 static int would_lose_untracked(const char *path)
454 int pos = cache_name_pos(path, strlen(path));
456 if (pos < 0)
457 pos = -1 - pos;
458 while (pos < active_nr &&
459 !strcmp(path, active_cache[pos]->name)) {
461 * If stage #0, it is definitely tracked.
462 * If it has stage #2 then it was tracked
463 * before this merge started. All other
464 * cases the path was not tracked.
466 switch (ce_stage(active_cache[pos])) {
467 case 0:
468 case 2:
469 return 0;
471 pos++;
473 return file_exists(path);
476 static int make_room_for_path(const char *path)
478 int status;
479 const char *msg = "failed to create path '%s'%s";
481 status = safe_create_leading_directories_const(path);
482 if (status) {
483 if (status == -3) {
484 /* something else exists */
485 error(msg, path, ": perhaps a D/F conflict?");
486 return -1;
488 die(msg, path, "");
492 * Do not unlink a file in the work tree if we are not
493 * tracking it.
495 if (would_lose_untracked(path))
496 return error("refusing to lose untracked file at '%s'",
497 path);
499 /* Successful unlink is good.. */
500 if (!unlink(path))
501 return 0;
502 /* .. and so is no existing file */
503 if (errno == ENOENT)
504 return 0;
505 /* .. but not some other error (who really cares what?) */
506 return error(msg, path, ": perhaps a D/F conflict?");
509 static void update_file_flags(struct merge_options *o,
510 const unsigned char *sha,
511 unsigned mode,
512 const char *path,
513 int update_cache,
514 int update_wd)
516 if (o->call_depth)
517 update_wd = 0;
519 if (update_wd) {
520 enum object_type type;
521 void *buf;
522 unsigned long size;
524 if (S_ISGITLINK(mode))
526 * We may later decide to recursively descend into
527 * the submodule directory and update its index
528 * and/or work tree, but we do not do that now.
530 goto update_index;
532 buf = read_sha1_file(sha, &type, &size);
533 if (!buf)
534 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
535 if (type != OBJ_BLOB)
536 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
537 if (S_ISREG(mode)) {
538 struct strbuf strbuf = STRBUF_INIT;
539 if (convert_to_working_tree(path, buf, size, &strbuf)) {
540 free(buf);
541 size = strbuf.len;
542 buf = strbuf_detach(&strbuf, NULL);
546 if (make_room_for_path(path) < 0) {
547 update_wd = 0;
548 free(buf);
549 goto update_index;
551 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
552 int fd;
553 if (mode & 0100)
554 mode = 0777;
555 else
556 mode = 0666;
557 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
558 if (fd < 0)
559 die_errno("failed to open '%s'", path);
560 flush_buffer(fd, buf, size);
561 close(fd);
562 } else if (S_ISLNK(mode)) {
563 char *lnk = xmemdupz(buf, size);
564 safe_create_leading_directories_const(path);
565 unlink(path);
566 if (symlink(lnk, path))
567 die_errno("failed to symlink '%s'", path);
568 free(lnk);
569 } else
570 die("do not know what to do with %06o %s '%s'",
571 mode, sha1_to_hex(sha), path);
572 free(buf);
574 update_index:
575 if (update_cache)
576 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
579 static void update_file(struct merge_options *o,
580 int clean,
581 const unsigned char *sha,
582 unsigned mode,
583 const char *path)
585 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
588 /* Low level file merging, update and removal */
590 struct merge_file_info
592 unsigned char sha[20];
593 unsigned mode;
594 unsigned clean:1,
595 merge:1;
598 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
600 unsigned long size;
601 enum object_type type;
603 if (!hashcmp(sha1, null_sha1)) {
604 mm->ptr = xstrdup("");
605 mm->size = 0;
606 return;
609 mm->ptr = read_sha1_file(sha1, &type, &size);
610 if (!mm->ptr || type != OBJ_BLOB)
611 die("unable to read blob object %s", sha1_to_hex(sha1));
612 mm->size = size;
615 static int merge_3way(struct merge_options *o,
616 mmbuffer_t *result_buf,
617 struct diff_filespec *one,
618 struct diff_filespec *a,
619 struct diff_filespec *b,
620 const char *branch1,
621 const char *branch2)
623 mmfile_t orig, src1, src2;
624 char *name1, *name2;
625 int merge_status;
627 if (strcmp(a->path, b->path)) {
628 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
629 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
630 } else {
631 name1 = xstrdup(mkpath("%s", branch1));
632 name2 = xstrdup(mkpath("%s", branch2));
635 fill_mm(one->sha1, &orig);
636 fill_mm(a->sha1, &src1);
637 fill_mm(b->sha1, &src2);
639 merge_status = ll_merge(result_buf, a->path, &orig,
640 &src1, name1, &src2, name2,
641 o->call_depth);
643 free(name1);
644 free(name2);
645 free(orig.ptr);
646 free(src1.ptr);
647 free(src2.ptr);
648 return merge_status;
651 static struct merge_file_info merge_file(struct merge_options *o,
652 struct diff_filespec *one,
653 struct diff_filespec *a,
654 struct diff_filespec *b,
655 const char *branch1,
656 const char *branch2)
658 struct merge_file_info result;
659 result.merge = 0;
660 result.clean = 1;
662 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
663 result.clean = 0;
664 if (S_ISREG(a->mode)) {
665 result.mode = a->mode;
666 hashcpy(result.sha, a->sha1);
667 } else {
668 result.mode = b->mode;
669 hashcpy(result.sha, b->sha1);
671 } else {
672 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
673 result.merge = 1;
676 * Merge modes
678 if (a->mode == b->mode || a->mode == one->mode)
679 result.mode = b->mode;
680 else {
681 result.mode = a->mode;
682 if (b->mode != one->mode) {
683 result.clean = 0;
684 result.merge = 1;
688 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
689 hashcpy(result.sha, b->sha1);
690 else if (sha_eq(b->sha1, one->sha1))
691 hashcpy(result.sha, a->sha1);
692 else if (S_ISREG(a->mode)) {
693 mmbuffer_t result_buf;
694 int merge_status;
696 merge_status = merge_3way(o, &result_buf, one, a, b,
697 branch1, branch2);
699 if ((merge_status < 0) || !result_buf.ptr)
700 die("Failed to execute internal merge");
702 if (write_sha1_file(result_buf.ptr, result_buf.size,
703 blob_type, result.sha))
704 die("Unable to add %s to database",
705 a->path);
707 free(result_buf.ptr);
708 result.clean = (merge_status == 0);
709 } else if (S_ISGITLINK(a->mode)) {
710 result.clean = 0;
711 hashcpy(result.sha, a->sha1);
712 } else if (S_ISLNK(a->mode)) {
713 hashcpy(result.sha, a->sha1);
715 if (!sha_eq(a->sha1, b->sha1))
716 result.clean = 0;
717 } else {
718 die("unsupported object type in the tree");
722 return result;
725 static void conflict_rename_rename(struct merge_options *o,
726 struct rename *ren1,
727 const char *branch1,
728 struct rename *ren2,
729 const char *branch2)
731 char *del[2];
732 int delp = 0;
733 const char *ren1_dst = ren1->pair->two->path;
734 const char *ren2_dst = ren2->pair->two->path;
735 const char *dst_name1 = ren1_dst;
736 const char *dst_name2 = ren2_dst;
737 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
738 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
739 output(o, 1, "%s is a directory in %s adding as %s instead",
740 ren1_dst, branch2, dst_name1);
741 remove_file(o, 0, ren1_dst, 0);
743 if (string_list_has_string(&o->current_directory_set, ren2_dst)) {
744 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
745 output(o, 1, "%s is a directory in %s adding as %s instead",
746 ren2_dst, branch1, dst_name2);
747 remove_file(o, 0, ren2_dst, 0);
749 if (o->call_depth) {
750 remove_file_from_cache(dst_name1);
751 remove_file_from_cache(dst_name2);
753 * Uncomment to leave the conflicting names in the resulting tree
755 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
756 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
758 } else {
759 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
760 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
762 while (delp--)
763 free(del[delp]);
766 static void conflict_rename_dir(struct merge_options *o,
767 struct rename *ren1,
768 const char *branch1)
770 char *new_path = unique_path(o, ren1->pair->two->path, branch1);
771 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
772 remove_file(o, 0, ren1->pair->two->path, 0);
773 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
774 free(new_path);
777 static void conflict_rename_rename_2(struct merge_options *o,
778 struct rename *ren1,
779 const char *branch1,
780 struct rename *ren2,
781 const char *branch2)
783 char *new_path1 = unique_path(o, ren1->pair->two->path, branch1);
784 char *new_path2 = unique_path(o, ren2->pair->two->path, branch2);
785 output(o, 1, "Renaming %s to %s and %s to %s instead",
786 ren1->pair->one->path, new_path1,
787 ren2->pair->one->path, new_path2);
788 remove_file(o, 0, ren1->pair->two->path, 0);
789 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
790 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
791 free(new_path2);
792 free(new_path1);
795 static int process_renames(struct merge_options *o,
796 struct string_list *a_renames,
797 struct string_list *b_renames)
799 int clean_merge = 1, i, j;
800 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
801 const struct rename *sre;
803 for (i = 0; i < a_renames->nr; i++) {
804 sre = a_renames->items[i].util;
805 string_list_insert(sre->pair->two->path, &a_by_dst)->util
806 = sre->dst_entry;
808 for (i = 0; i < b_renames->nr; i++) {
809 sre = b_renames->items[i].util;
810 string_list_insert(sre->pair->two->path, &b_by_dst)->util
811 = sre->dst_entry;
814 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
815 char *src;
816 struct string_list *renames1, *renames2Dst;
817 struct rename *ren1 = NULL, *ren2 = NULL;
818 const char *branch1, *branch2;
819 const char *ren1_src, *ren1_dst;
821 if (i >= a_renames->nr) {
822 ren2 = b_renames->items[j++].util;
823 } else if (j >= b_renames->nr) {
824 ren1 = a_renames->items[i++].util;
825 } else {
826 int compare = strcmp(a_renames->items[i].string,
827 b_renames->items[j].string);
828 if (compare <= 0)
829 ren1 = a_renames->items[i++].util;
830 if (compare >= 0)
831 ren2 = b_renames->items[j++].util;
834 /* TODO: refactor, so that 1/2 are not needed */
835 if (ren1) {
836 renames1 = a_renames;
837 renames2Dst = &b_by_dst;
838 branch1 = o->branch1;
839 branch2 = o->branch2;
840 } else {
841 struct rename *tmp;
842 renames1 = b_renames;
843 renames2Dst = &a_by_dst;
844 branch1 = o->branch2;
845 branch2 = o->branch1;
846 tmp = ren2;
847 ren2 = ren1;
848 ren1 = tmp;
850 src = ren1->pair->one->path;
852 ren1->dst_entry->processed = 1;
853 ren1->src_entry->processed = 1;
855 if (ren1->processed)
856 continue;
857 ren1->processed = 1;
859 ren1_src = ren1->pair->one->path;
860 ren1_dst = ren1->pair->two->path;
862 if (ren2) {
863 const char *ren2_src = ren2->pair->one->path;
864 const char *ren2_dst = ren2->pair->two->path;
865 /* Renamed in 1 and renamed in 2 */
866 if (strcmp(ren1_src, ren2_src) != 0)
867 die("ren1.src != ren2.src");
868 ren2->dst_entry->processed = 1;
869 ren2->processed = 1;
870 if (strcmp(ren1_dst, ren2_dst) != 0) {
871 clean_merge = 0;
872 output(o, 1, "CONFLICT (rename/rename): "
873 "Rename \"%s\"->\"%s\" in branch \"%s\" "
874 "rename \"%s\"->\"%s\" in \"%s\"%s",
875 src, ren1_dst, branch1,
876 src, ren2_dst, branch2,
877 o->call_depth ? " (left unresolved)": "");
878 if (o->call_depth) {
879 remove_file_from_cache(src);
880 update_file(o, 0, ren1->pair->one->sha1,
881 ren1->pair->one->mode, src);
883 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
884 } else {
885 struct merge_file_info mfi;
886 remove_file(o, 1, ren1_src, 1);
887 mfi = merge_file(o,
888 ren1->pair->one,
889 ren1->pair->two,
890 ren2->pair->two,
891 branch1,
892 branch2);
893 if (mfi.merge || !mfi.clean)
894 output(o, 1, "Renaming %s->%s", src, ren1_dst);
896 if (mfi.merge)
897 output(o, 2, "Auto-merging %s", ren1_dst);
899 if (!mfi.clean) {
900 output(o, 1, "CONFLICT (content): merge conflict in %s",
901 ren1_dst);
902 clean_merge = 0;
904 if (!o->call_depth)
905 update_stages(ren1_dst,
906 ren1->pair->one,
907 ren1->pair->two,
908 ren2->pair->two,
909 1 /* clear */);
911 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
913 } else {
914 /* Renamed in 1, maybe changed in 2 */
915 struct string_list_item *item;
916 /* we only use sha1 and mode of these */
917 struct diff_filespec src_other, dst_other;
918 int try_merge, stage = a_renames == renames1 ? 3: 2;
920 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
922 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
923 src_other.mode = ren1->src_entry->stages[stage].mode;
924 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
925 dst_other.mode = ren1->dst_entry->stages[stage].mode;
927 try_merge = 0;
929 if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
930 clean_merge = 0;
931 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
932 " directory %s added in %s",
933 ren1_src, ren1_dst, branch1,
934 ren1_dst, branch2);
935 conflict_rename_dir(o, ren1, branch1);
936 } else if (sha_eq(src_other.sha1, null_sha1)) {
937 clean_merge = 0;
938 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
939 "and deleted in %s",
940 ren1_src, ren1_dst, branch1,
941 branch2);
942 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
943 if (!o->call_depth)
944 update_stages(ren1_dst, NULL,
945 branch1 == o->branch1 ?
946 ren1->pair->two : NULL,
947 branch1 == o->branch1 ?
948 NULL : ren1->pair->two, 1);
949 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
950 const char *new_path;
951 clean_merge = 0;
952 try_merge = 1;
953 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
954 "%s added in %s",
955 ren1_src, ren1_dst, branch1,
956 ren1_dst, branch2);
957 if (o->call_depth) {
958 struct merge_file_info mfi;
959 struct diff_filespec one, a, b;
961 one.path = a.path = b.path =
962 (char *)ren1_dst;
963 hashcpy(one.sha1, null_sha1);
964 one.mode = 0;
965 hashcpy(a.sha1, ren1->pair->two->sha1);
966 a.mode = ren1->pair->two->mode;
967 hashcpy(b.sha1, dst_other.sha1);
968 b.mode = dst_other.mode;
969 mfi = merge_file(o, &one, &a, &b,
970 branch1,
971 branch2);
972 output(o, 1, "Adding merged %s", ren1_dst);
973 update_file(o, 0,
974 mfi.sha,
975 mfi.mode,
976 ren1_dst);
977 } else {
978 new_path = unique_path(o, ren1_dst, branch2);
979 output(o, 1, "Adding as %s instead", new_path);
980 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
982 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
983 ren2 = item->util;
984 clean_merge = 0;
985 ren2->processed = 1;
986 output(o, 1, "CONFLICT (rename/rename): "
987 "Rename %s->%s in %s. "
988 "Rename %s->%s in %s",
989 ren1_src, ren1_dst, branch1,
990 ren2->pair->one->path, ren2->pair->two->path, branch2);
991 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
992 } else
993 try_merge = 1;
995 if (try_merge) {
996 struct diff_filespec *one, *a, *b;
997 struct merge_file_info mfi;
998 src_other.path = (char *)ren1_src;
1000 one = ren1->pair->one;
1001 if (a_renames == renames1) {
1002 a = ren1->pair->two;
1003 b = &src_other;
1004 } else {
1005 b = ren1->pair->two;
1006 a = &src_other;
1008 mfi = merge_file(o, one, a, b,
1009 o->branch1, o->branch2);
1011 if (mfi.clean &&
1012 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1013 mfi.mode == ren1->pair->two->mode)
1015 * This messaged is part of
1016 * t6022 test. If you change
1017 * it update the test too.
1019 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
1020 else {
1021 if (mfi.merge || !mfi.clean)
1022 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
1023 if (mfi.merge)
1024 output(o, 2, "Auto-merging %s", ren1_dst);
1025 if (!mfi.clean) {
1026 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
1027 ren1_dst);
1028 clean_merge = 0;
1030 if (!o->call_depth)
1031 update_stages(ren1_dst,
1032 one, a, b, 1);
1034 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1039 string_list_clear(&a_by_dst, 0);
1040 string_list_clear(&b_by_dst, 0);
1042 return clean_merge;
1045 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1047 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1050 /* Per entry merge function */
1051 static int process_entry(struct merge_options *o,
1052 const char *path, struct stage_data *entry)
1055 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1056 print_index_entry("\tpath: ", entry);
1058 int clean_merge = 1;
1059 unsigned o_mode = entry->stages[1].mode;
1060 unsigned a_mode = entry->stages[2].mode;
1061 unsigned b_mode = entry->stages[3].mode;
1062 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1063 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1064 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1066 if (o_sha && (!a_sha || !b_sha)) {
1067 /* Case A: Deleted in one */
1068 if ((!a_sha && !b_sha) ||
1069 (sha_eq(a_sha, o_sha) && !b_sha) ||
1070 (!a_sha && sha_eq(b_sha, o_sha))) {
1071 /* Deleted in both or deleted in one and
1072 * unchanged in the other */
1073 if (a_sha)
1074 output(o, 2, "Removing %s", path);
1075 /* do not touch working file if it did not exist */
1076 remove_file(o, 1, path, !a_sha);
1077 } else {
1078 /* Deleted in one and changed in the other */
1079 clean_merge = 0;
1080 if (!a_sha) {
1081 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1082 "and modified in %s. Version %s of %s left in tree.",
1083 path, o->branch1,
1084 o->branch2, o->branch2, path);
1085 update_file(o, 0, b_sha, b_mode, path);
1086 } else {
1087 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1088 "and modified in %s. Version %s of %s left in tree.",
1089 path, o->branch2,
1090 o->branch1, o->branch1, path);
1091 update_file(o, 0, a_sha, a_mode, path);
1095 } else if ((!o_sha && a_sha && !b_sha) ||
1096 (!o_sha && !a_sha && b_sha)) {
1097 /* Case B: Added in one. */
1098 const char *add_branch;
1099 const char *other_branch;
1100 unsigned mode;
1101 const unsigned char *sha;
1102 const char *conf;
1104 if (a_sha) {
1105 add_branch = o->branch1;
1106 other_branch = o->branch2;
1107 mode = a_mode;
1108 sha = a_sha;
1109 conf = "file/directory";
1110 } else {
1111 add_branch = o->branch2;
1112 other_branch = o->branch1;
1113 mode = b_mode;
1114 sha = b_sha;
1115 conf = "directory/file";
1117 if (string_list_has_string(&o->current_directory_set, path)) {
1118 const char *new_path = unique_path(o, path, add_branch);
1119 clean_merge = 0;
1120 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1121 "Adding %s as %s",
1122 conf, path, other_branch, path, new_path);
1123 remove_file(o, 0, path, 0);
1124 update_file(o, 0, sha, mode, new_path);
1125 } else {
1126 output(o, 2, "Adding %s", path);
1127 update_file(o, 1, sha, mode, path);
1129 } else if (a_sha && b_sha) {
1130 /* Case C: Added in both (check for same permissions) and */
1131 /* case D: Modified in both, but differently. */
1132 const char *reason = "content";
1133 struct merge_file_info mfi;
1134 struct diff_filespec one, a, b;
1136 if (!o_sha) {
1137 reason = "add/add";
1138 o_sha = (unsigned char *)null_sha1;
1140 output(o, 2, "Auto-merging %s", path);
1141 one.path = a.path = b.path = (char *)path;
1142 hashcpy(one.sha1, o_sha);
1143 one.mode = o_mode;
1144 hashcpy(a.sha1, a_sha);
1145 a.mode = a_mode;
1146 hashcpy(b.sha1, b_sha);
1147 b.mode = b_mode;
1149 mfi = merge_file(o, &one, &a, &b,
1150 o->branch1, o->branch2);
1152 clean_merge = mfi.clean;
1153 if (!mfi.clean) {
1154 if (S_ISGITLINK(mfi.mode))
1155 reason = "submodule";
1156 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1157 reason, path);
1159 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1160 } else if (!o_sha && !a_sha && !b_sha) {
1162 * this entry was deleted altogether. a_mode == 0 means
1163 * we had that path and want to actively remove it.
1165 remove_file(o, 1, path, !a_mode);
1166 } else
1167 die("Fatal merge failure, shouldn't happen.");
1169 return clean_merge;
1172 struct unpack_trees_error_msgs get_porcelain_error_msgs(void)
1174 struct unpack_trees_error_msgs msgs = {
1175 /* would_overwrite */
1176 "Your local changes to '%s' would be overwritten by merge. Aborting.",
1177 /* not_uptodate_file */
1178 "Your local changes to '%s' would be overwritten by merge. Aborting.",
1179 /* not_uptodate_dir */
1180 "Updating '%s' would lose untracked files in it. Aborting.",
1181 /* would_lose_untracked */
1182 "Untracked working tree file '%s' would be %s by merge. Aborting",
1183 /* bind_overlap -- will not happen here */
1184 NULL,
1186 if (advice_commit_before_merge) {
1187 msgs.would_overwrite = msgs.not_uptodate_file =
1188 "Your local changes to '%s' would be overwritten by merge. Aborting.\n"
1189 "Please, commit your changes or stash them before you can merge.";
1191 return msgs;
1194 int merge_trees(struct merge_options *o,
1195 struct tree *head,
1196 struct tree *merge,
1197 struct tree *common,
1198 struct tree **result)
1200 int code, clean;
1202 if (o->subtree_merge) {
1203 merge = shift_tree_object(head, merge);
1204 common = shift_tree_object(head, common);
1207 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1208 output(o, 0, "Already uptodate!");
1209 *result = head;
1210 return 1;
1213 code = git_merge_trees(o->call_depth, common, head, merge);
1215 if (code != 0) {
1216 if (show(o, 4) || o->call_depth)
1217 die("merging of trees %s and %s failed",
1218 sha1_to_hex(head->object.sha1),
1219 sha1_to_hex(merge->object.sha1));
1220 else
1221 exit(128);
1224 if (unmerged_cache()) {
1225 struct string_list *entries, *re_head, *re_merge;
1226 int i;
1227 string_list_clear(&o->current_file_set, 1);
1228 string_list_clear(&o->current_directory_set, 1);
1229 get_files_dirs(o, head);
1230 get_files_dirs(o, merge);
1232 entries = get_unmerged();
1233 re_head = get_renames(o, head, common, head, merge, entries);
1234 re_merge = get_renames(o, merge, common, head, merge, entries);
1235 clean = process_renames(o, re_head, re_merge);
1236 for (i = 0; i < entries->nr; i++) {
1237 const char *path = entries->items[i].string;
1238 struct stage_data *e = entries->items[i].util;
1239 if (!e->processed
1240 && !process_entry(o, path, e))
1241 clean = 0;
1244 string_list_clear(re_merge, 0);
1245 string_list_clear(re_head, 0);
1246 string_list_clear(entries, 1);
1249 else
1250 clean = 1;
1252 if (o->call_depth)
1253 *result = write_tree_from_memory(o);
1255 return clean;
1258 static struct commit_list *reverse_commit_list(struct commit_list *list)
1260 struct commit_list *next = NULL, *current, *backup;
1261 for (current = list; current; current = backup) {
1262 backup = current->next;
1263 current->next = next;
1264 next = current;
1266 return next;
1270 * Merge the commits h1 and h2, return the resulting virtual
1271 * commit object and a flag indicating the cleanness of the merge.
1273 int merge_recursive(struct merge_options *o,
1274 struct commit *h1,
1275 struct commit *h2,
1276 struct commit_list *ca,
1277 struct commit **result)
1279 struct commit_list *iter;
1280 struct commit *merged_common_ancestors;
1281 struct tree *mrtree = mrtree;
1282 int clean;
1284 if (show(o, 4)) {
1285 output(o, 4, "Merging:");
1286 output_commit_title(o, h1);
1287 output_commit_title(o, h2);
1290 if (!ca) {
1291 ca = get_merge_bases(h1, h2, 1);
1292 ca = reverse_commit_list(ca);
1295 if (show(o, 5)) {
1296 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1297 for (iter = ca; iter; iter = iter->next)
1298 output_commit_title(o, iter->item);
1301 merged_common_ancestors = pop_commit(&ca);
1302 if (merged_common_ancestors == NULL) {
1303 /* if there is no common ancestor, make an empty tree */
1304 struct tree *tree = xcalloc(1, sizeof(struct tree));
1306 tree->object.parsed = 1;
1307 tree->object.type = OBJ_TREE;
1308 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1309 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1312 for (iter = ca; iter; iter = iter->next) {
1313 const char *saved_b1, *saved_b2;
1314 o->call_depth++;
1316 * When the merge fails, the result contains files
1317 * with conflict markers. The cleanness flag is
1318 * ignored, it was never actually used, as result of
1319 * merge_trees has always overwritten it: the committed
1320 * "conflicts" were already resolved.
1322 discard_cache();
1323 saved_b1 = o->branch1;
1324 saved_b2 = o->branch2;
1325 o->branch1 = "Temporary merge branch 1";
1326 o->branch2 = "Temporary merge branch 2";
1327 merge_recursive(o, merged_common_ancestors, iter->item,
1328 NULL, &merged_common_ancestors);
1329 o->branch1 = saved_b1;
1330 o->branch2 = saved_b2;
1331 o->call_depth--;
1333 if (!merged_common_ancestors)
1334 die("merge returned no commit");
1337 discard_cache();
1338 if (!o->call_depth)
1339 read_cache();
1341 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1342 &mrtree);
1344 if (o->call_depth) {
1345 *result = make_virtual_commit(mrtree, "merged tree");
1346 commit_list_insert(h1, &(*result)->parents);
1347 commit_list_insert(h2, &(*result)->parents->next);
1349 flush_output(o);
1350 return clean;
1353 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1355 struct object *object;
1357 object = deref_tag(parse_object(sha1), name, strlen(name));
1358 if (!object)
1359 return NULL;
1360 if (object->type == OBJ_TREE)
1361 return make_virtual_commit((struct tree*)object, name);
1362 if (object->type != OBJ_COMMIT)
1363 return NULL;
1364 if (parse_commit((struct commit *)object))
1365 return NULL;
1366 return (struct commit *)object;
1369 int merge_recursive_generic(struct merge_options *o,
1370 const unsigned char *head,
1371 const unsigned char *merge,
1372 int num_base_list,
1373 const unsigned char **base_list,
1374 struct commit **result)
1376 int clean, index_fd;
1377 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1378 struct commit *head_commit = get_ref(head, o->branch1);
1379 struct commit *next_commit = get_ref(merge, o->branch2);
1380 struct commit_list *ca = NULL;
1382 if (base_list) {
1383 int i;
1384 for (i = 0; i < num_base_list; ++i) {
1385 struct commit *base;
1386 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1387 return error("Could not parse object '%s'",
1388 sha1_to_hex(base_list[i]));
1389 commit_list_insert(base, &ca);
1393 index_fd = hold_locked_index(lock, 1);
1394 clean = merge_recursive(o, head_commit, next_commit, ca,
1395 result);
1396 if (active_cache_changed &&
1397 (write_cache(index_fd, active_cache, active_nr) ||
1398 commit_locked_index(lock)))
1399 return error("Unable to write index.");
1401 return clean ? 0 : 1;
1404 static int merge_recursive_config(const char *var, const char *value, void *cb)
1406 struct merge_options *o = cb;
1407 if (!strcasecmp(var, "merge.verbosity")) {
1408 o->verbosity = git_config_int(var, value);
1409 return 0;
1411 if (!strcasecmp(var, "diff.renamelimit")) {
1412 o->diff_rename_limit = git_config_int(var, value);
1413 return 0;
1415 if (!strcasecmp(var, "merge.renamelimit")) {
1416 o->merge_rename_limit = git_config_int(var, value);
1417 return 0;
1419 return git_xmerge_config(var, value, cb);
1422 void init_merge_options(struct merge_options *o)
1424 memset(o, 0, sizeof(struct merge_options));
1425 o->verbosity = 2;
1426 o->buffer_output = 1;
1427 o->diff_rename_limit = -1;
1428 o->merge_rename_limit = -1;
1429 git_config(merge_recursive_config, o);
1430 if (getenv("GIT_MERGE_VERBOSITY"))
1431 o->verbosity =
1432 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1433 if (o->verbosity >= 5)
1434 o->buffer_output = 0;
1435 strbuf_init(&o->obuf, 0);
1436 memset(&o->current_file_set, 0, sizeof(struct string_list));
1437 o->current_file_set.strdup_strings = 1;
1438 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1439 o->current_directory_set.strdup_strings = 1;