merge-recursive: get rid of the index_only global variable
[git/kirr.git] / merge-recursive.c
blobc426589d1172ddc6b693e117f9b175ad0e861425
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 "cache.h"
7 #include "cache-tree.h"
8 #include "commit.h"
9 #include "blob.h"
10 #include "builtin.h"
11 #include "tree-walk.h"
12 #include "diff.h"
13 #include "diffcore.h"
14 #include "tag.h"
15 #include "unpack-trees.h"
16 #include "string-list.h"
17 #include "xdiff-interface.h"
18 #include "ll-merge.h"
19 #include "interpolate.h"
20 #include "attr.h"
21 #include "merge-recursive.h"
23 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
25 unsigned char shifted[20];
28 * NEEDSWORK: this limits the recursion depth to hardcoded
29 * value '2' to avoid excessive overhead.
31 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
32 if (!hashcmp(two->object.sha1, shifted))
33 return two;
34 return lookup_tree(shifted);
38 * A virtual commit has
39 * - (const char *)commit->util set to the name, and
40 * - *(int *)commit->object.sha1 set to the virtual id.
43 struct commit *make_virtual_commit(struct tree *tree, const char *comment)
45 struct commit *commit = xcalloc(1, sizeof(struct commit));
46 static unsigned virtual_id = 1;
47 commit->tree = tree;
48 commit->util = (void*)comment;
49 *(int*)commit->object.sha1 = virtual_id++;
50 /* avoid warnings */
51 commit->object.parsed = 1;
52 return commit;
56 * Since we use get_tree_entry(), which does not put the read object into
57 * the object pool, we cannot rely on a == b.
59 static int sha_eq(const unsigned char *a, const unsigned char *b)
61 if (!a && !b)
62 return 2;
63 return a && b && hashcmp(a, b) == 0;
67 * Since we want to write the index eventually, we cannot reuse the index
68 * for these (temporary) data.
70 struct stage_data
72 struct
74 unsigned mode;
75 unsigned char sha[20];
76 } stages[4];
77 unsigned processed:1;
80 static struct string_list current_file_set = {NULL, 0, 0, 1};
81 static struct string_list current_directory_set = {NULL, 0, 0, 1};
83 static struct strbuf obuf = STRBUF_INIT;
85 static int show(struct merge_options *o, int v)
87 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
90 static void flush_output(void)
92 if (obuf.len) {
93 fputs(obuf.buf, stdout);
94 strbuf_reset(&obuf);
98 static void output(struct merge_options *o, int v, const char *fmt, ...)
100 int len;
101 va_list ap;
103 if (!show(o, v))
104 return;
106 strbuf_grow(&obuf, o->call_depth * 2 + 2);
107 memset(obuf.buf + obuf.len, ' ', o->call_depth * 2);
108 strbuf_setlen(&obuf, obuf.len + o->call_depth * 2);
110 va_start(ap, fmt);
111 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
112 va_end(ap);
114 if (len < 0)
115 len = 0;
116 if (len >= strbuf_avail(&obuf)) {
117 strbuf_grow(&obuf, len + 2);
118 va_start(ap, fmt);
119 len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
120 va_end(ap);
121 if (len >= strbuf_avail(&obuf)) {
122 die("this should not happen, your snprintf is broken");
125 strbuf_setlen(&obuf, obuf.len + len);
126 strbuf_add(&obuf, "\n", 1);
127 if (!o->buffer_output)
128 flush_output();
131 static void output_commit_title(struct merge_options *o, struct commit *commit)
133 int i;
134 flush_output();
135 for (i = o->call_depth; i--;)
136 fputs(" ", stdout);
137 if (commit->util)
138 printf("virtual %s\n", (char *)commit->util);
139 else {
140 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
141 if (parse_commit(commit) != 0)
142 printf("(bad commit)\n");
143 else {
144 const char *s;
145 int len;
146 for (s = commit->buffer; *s; s++)
147 if (*s == '\n' && s[1] == '\n') {
148 s += 2;
149 break;
151 for (len = 0; s[len] && '\n' != s[len]; len++)
152 ; /* do nothing */
153 printf("%.*s\n", len, s);
158 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
159 const char *path, int stage, int refresh, int options)
161 struct cache_entry *ce;
162 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
163 if (!ce)
164 return error("addinfo_cache failed for path '%s'", path);
165 return add_cache_entry(ce, options);
168 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
170 parse_tree(tree);
171 init_tree_desc(desc, tree->buffer, tree->size);
174 static int git_merge_trees(int index_only,
175 struct tree *common,
176 struct tree *head,
177 struct tree *merge)
179 int rc;
180 struct tree_desc t[3];
181 struct unpack_trees_options opts;
183 memset(&opts, 0, sizeof(opts));
184 if (index_only)
185 opts.index_only = 1;
186 else
187 opts.update = 1;
188 opts.merge = 1;
189 opts.head_idx = 2;
190 opts.fn = threeway_merge;
191 opts.src_index = &the_index;
192 opts.dst_index = &the_index;
194 init_tree_desc_from_tree(t+0, common);
195 init_tree_desc_from_tree(t+1, head);
196 init_tree_desc_from_tree(t+2, merge);
198 rc = unpack_trees(3, t, &opts);
199 cache_tree_free(&active_cache_tree);
200 return rc;
203 struct tree *write_tree_from_memory(struct merge_options *o)
205 struct tree *result = NULL;
207 if (unmerged_cache()) {
208 int i;
209 output(o, 0, "There are unmerged index entries:");
210 for (i = 0; i < active_nr; i++) {
211 struct cache_entry *ce = active_cache[i];
212 if (ce_stage(ce))
213 output(o, 0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
215 return NULL;
218 if (!active_cache_tree)
219 active_cache_tree = cache_tree();
221 if (!cache_tree_fully_valid(active_cache_tree) &&
222 cache_tree_update(active_cache_tree,
223 active_cache, active_nr, 0, 0) < 0)
224 die("error building trees");
226 result = lookup_tree(active_cache_tree->sha1);
228 return result;
231 static int save_files_dirs(const unsigned char *sha1,
232 const char *base, int baselen, const char *path,
233 unsigned int mode, int stage, void *context)
235 int len = strlen(path);
236 char *newpath = xmalloc(baselen + len + 1);
237 memcpy(newpath, base, baselen);
238 memcpy(newpath + baselen, path, len);
239 newpath[baselen + len] = '\0';
241 if (S_ISDIR(mode))
242 string_list_insert(newpath, &current_directory_set);
243 else
244 string_list_insert(newpath, &current_file_set);
245 free(newpath);
247 return READ_TREE_RECURSIVE;
250 static int get_files_dirs(struct tree *tree)
252 int n;
253 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, NULL))
254 return 0;
255 n = current_file_set.nr + current_directory_set.nr;
256 return n;
260 * Returns an index_entry instance which doesn't have to correspond to
261 * a real cache entry in Git's index.
263 static struct stage_data *insert_stage_data(const char *path,
264 struct tree *o, struct tree *a, struct tree *b,
265 struct string_list *entries)
267 struct string_list_item *item;
268 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
269 get_tree_entry(o->object.sha1, path,
270 e->stages[1].sha, &e->stages[1].mode);
271 get_tree_entry(a->object.sha1, path,
272 e->stages[2].sha, &e->stages[2].mode);
273 get_tree_entry(b->object.sha1, path,
274 e->stages[3].sha, &e->stages[3].mode);
275 item = string_list_insert(path, entries);
276 item->util = e;
277 return e;
281 * Create a dictionary mapping file names to stage_data objects. The
282 * dictionary contains one entry for every path with a non-zero stage entry.
284 static struct string_list *get_unmerged(void)
286 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
287 int i;
289 unmerged->strdup_strings = 1;
291 for (i = 0; i < active_nr; i++) {
292 struct string_list_item *item;
293 struct stage_data *e;
294 struct cache_entry *ce = active_cache[i];
295 if (!ce_stage(ce))
296 continue;
298 item = string_list_lookup(ce->name, unmerged);
299 if (!item) {
300 item = string_list_insert(ce->name, unmerged);
301 item->util = xcalloc(1, sizeof(struct stage_data));
303 e = item->util;
304 e->stages[ce_stage(ce)].mode = ce->ce_mode;
305 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
308 return unmerged;
311 struct rename
313 struct diff_filepair *pair;
314 struct stage_data *src_entry;
315 struct stage_data *dst_entry;
316 unsigned processed:1;
320 * Get information of all renames which occurred between 'o_tree' and
321 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
322 * 'b_tree') to be able to associate the correct cache entries with
323 * the rename information. 'tree' is always equal to either a_tree or b_tree.
325 static struct string_list *get_renames(struct merge_options *o,
326 struct tree *tree,
327 struct tree *o_tree,
328 struct tree *a_tree,
329 struct tree *b_tree,
330 struct string_list *entries)
332 int i;
333 struct string_list *renames;
334 struct diff_options opts;
336 renames = xcalloc(1, sizeof(struct string_list));
337 diff_setup(&opts);
338 DIFF_OPT_SET(&opts, RECURSIVE);
339 opts.detect_rename = DIFF_DETECT_RENAME;
340 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
341 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
342 500;
343 opts.warn_on_too_large_rename = 1;
344 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
345 if (diff_setup_done(&opts) < 0)
346 die("diff setup failed");
347 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
348 diffcore_std(&opts);
349 for (i = 0; i < diff_queued_diff.nr; ++i) {
350 struct string_list_item *item;
351 struct rename *re;
352 struct diff_filepair *pair = diff_queued_diff.queue[i];
353 if (pair->status != 'R') {
354 diff_free_filepair(pair);
355 continue;
357 re = xmalloc(sizeof(*re));
358 re->processed = 0;
359 re->pair = pair;
360 item = string_list_lookup(re->pair->one->path, entries);
361 if (!item)
362 re->src_entry = insert_stage_data(re->pair->one->path,
363 o_tree, a_tree, b_tree, entries);
364 else
365 re->src_entry = item->util;
367 item = string_list_lookup(re->pair->two->path, entries);
368 if (!item)
369 re->dst_entry = insert_stage_data(re->pair->two->path,
370 o_tree, a_tree, b_tree, entries);
371 else
372 re->dst_entry = item->util;
373 item = string_list_insert(pair->one->path, renames);
374 item->util = re;
376 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
377 diff_queued_diff.nr = 0;
378 diff_flush(&opts);
379 return renames;
382 static int update_stages(const char *path, struct diff_filespec *o,
383 struct diff_filespec *a, struct diff_filespec *b,
384 int clear)
386 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
387 if (clear)
388 if (remove_file_from_cache(path))
389 return -1;
390 if (o)
391 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
392 return -1;
393 if (a)
394 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
395 return -1;
396 if (b)
397 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
398 return -1;
399 return 0;
402 static int remove_path(const char *name)
404 int ret;
405 char *slash, *dirs;
407 ret = unlink(name);
408 if (ret)
409 return ret;
410 dirs = xstrdup(name);
411 while ((slash = strrchr(name, '/'))) {
412 *slash = '\0';
413 if (rmdir(name) != 0)
414 break;
416 free(dirs);
417 return ret;
420 static int remove_file(struct merge_options *o, int clean,
421 const char *path, int no_wd)
423 int update_cache = o->call_depth || clean;
424 int update_working_directory = !o->call_depth && !no_wd;
426 if (update_cache) {
427 if (remove_file_from_cache(path))
428 return -1;
430 if (update_working_directory) {
431 unlink(path);
432 if (errno != ENOENT || errno != EISDIR)
433 return -1;
434 remove_path(path);
436 return 0;
439 static char *unique_path(const char *path, const char *branch)
441 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
442 int suffix = 0;
443 struct stat st;
444 char *p = newpath + strlen(path);
445 strcpy(newpath, path);
446 *(p++) = '~';
447 strcpy(p, branch);
448 for (; *p; ++p)
449 if ('/' == *p)
450 *p = '_';
451 while (string_list_has_string(&current_file_set, newpath) ||
452 string_list_has_string(&current_directory_set, newpath) ||
453 lstat(newpath, &st) == 0)
454 sprintf(p, "_%d", suffix++);
456 string_list_insert(newpath, &current_file_set);
457 return newpath;
460 static void flush_buffer(int fd, const char *buf, unsigned long size)
462 while (size > 0) {
463 long ret = write_in_full(fd, buf, size);
464 if (ret < 0) {
465 /* Ignore epipe */
466 if (errno == EPIPE)
467 break;
468 die("merge-recursive: %s", strerror(errno));
469 } else if (!ret) {
470 die("merge-recursive: disk full?");
472 size -= ret;
473 buf += ret;
477 static int make_room_for_path(const char *path)
479 int status;
480 const char *msg = "failed to create path '%s'%s";
482 status = safe_create_leading_directories_const(path);
483 if (status) {
484 if (status == -3) {
485 /* something else exists */
486 error(msg, path, ": perhaps a D/F conflict?");
487 return -1;
489 die(msg, path, "");
492 /* Successful unlink is good.. */
493 if (!unlink(path))
494 return 0;
495 /* .. and so is no existing file */
496 if (errno == ENOENT)
497 return 0;
498 /* .. but not some other error (who really cares what?) */
499 return error(msg, path, ": perhaps a D/F conflict?");
502 static void update_file_flags(struct merge_options *o,
503 const unsigned char *sha,
504 unsigned mode,
505 const char *path,
506 int update_cache,
507 int update_wd)
509 if (o->call_depth)
510 update_wd = 0;
512 if (update_wd) {
513 enum object_type type;
514 void *buf;
515 unsigned long size;
517 if (S_ISGITLINK(mode))
518 die("cannot read object %s '%s': It is a submodule!",
519 sha1_to_hex(sha), path);
521 buf = read_sha1_file(sha, &type, &size);
522 if (!buf)
523 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
524 if (type != OBJ_BLOB)
525 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
526 if (S_ISREG(mode)) {
527 struct strbuf strbuf;
528 strbuf_init(&strbuf, 0);
529 if (convert_to_working_tree(path, buf, size, &strbuf)) {
530 free(buf);
531 size = strbuf.len;
532 buf = strbuf_detach(&strbuf, NULL);
536 if (make_room_for_path(path) < 0) {
537 update_wd = 0;
538 free(buf);
539 goto update_index;
541 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
542 int fd;
543 if (mode & 0100)
544 mode = 0777;
545 else
546 mode = 0666;
547 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
548 if (fd < 0)
549 die("failed to open %s: %s", path, strerror(errno));
550 flush_buffer(fd, buf, size);
551 close(fd);
552 } else if (S_ISLNK(mode)) {
553 char *lnk = xmemdupz(buf, size);
554 safe_create_leading_directories_const(path);
555 unlink(path);
556 symlink(lnk, path);
557 free(lnk);
558 } else
559 die("do not know what to do with %06o %s '%s'",
560 mode, sha1_to_hex(sha), path);
561 free(buf);
563 update_index:
564 if (update_cache)
565 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
568 static void update_file(struct merge_options *o,
569 int clean,
570 const unsigned char *sha,
571 unsigned mode,
572 const char *path)
574 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
577 /* Low level file merging, update and removal */
579 struct merge_file_info
581 unsigned char sha[20];
582 unsigned mode;
583 unsigned clean:1,
584 merge:1;
587 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
589 unsigned long size;
590 enum object_type type;
592 if (!hashcmp(sha1, null_sha1)) {
593 mm->ptr = xstrdup("");
594 mm->size = 0;
595 return;
598 mm->ptr = read_sha1_file(sha1, &type, &size);
599 if (!mm->ptr || type != OBJ_BLOB)
600 die("unable to read blob object %s", sha1_to_hex(sha1));
601 mm->size = size;
604 static int merge_3way(struct merge_options *o,
605 mmbuffer_t *result_buf,
606 struct diff_filespec *one,
607 struct diff_filespec *a,
608 struct diff_filespec *b,
609 const char *branch1,
610 const char *branch2)
612 mmfile_t orig, src1, src2;
613 char *name1, *name2;
614 int merge_status;
616 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
617 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
619 fill_mm(one->sha1, &orig);
620 fill_mm(a->sha1, &src1);
621 fill_mm(b->sha1, &src2);
623 merge_status = ll_merge(result_buf, a->path, &orig,
624 &src1, name1, &src2, name2,
625 o->call_depth);
627 free(name1);
628 free(name2);
629 free(orig.ptr);
630 free(src1.ptr);
631 free(src2.ptr);
632 return merge_status;
635 static struct merge_file_info merge_file(struct merge_options *o,
636 struct diff_filespec *one,
637 struct diff_filespec *a,
638 struct diff_filespec *b,
639 const char *branch1,
640 const char *branch2)
642 struct merge_file_info result;
643 result.merge = 0;
644 result.clean = 1;
646 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
647 result.clean = 0;
648 if (S_ISREG(a->mode)) {
649 result.mode = a->mode;
650 hashcpy(result.sha, a->sha1);
651 } else {
652 result.mode = b->mode;
653 hashcpy(result.sha, b->sha1);
655 } else {
656 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
657 result.merge = 1;
660 * Merge modes
662 if (a->mode == b->mode || a->mode == one->mode)
663 result.mode = b->mode;
664 else {
665 result.mode = a->mode;
666 if (b->mode != one->mode) {
667 result.clean = 0;
668 result.merge = 1;
672 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
673 hashcpy(result.sha, b->sha1);
674 else if (sha_eq(b->sha1, one->sha1))
675 hashcpy(result.sha, a->sha1);
676 else if (S_ISREG(a->mode)) {
677 mmbuffer_t result_buf;
678 int merge_status;
680 merge_status = merge_3way(o, &result_buf, one, a, b,
681 branch1, branch2);
683 if ((merge_status < 0) || !result_buf.ptr)
684 die("Failed to execute internal merge");
686 if (write_sha1_file(result_buf.ptr, result_buf.size,
687 blob_type, result.sha))
688 die("Unable to add %s to database",
689 a->path);
691 free(result_buf.ptr);
692 result.clean = (merge_status == 0);
693 } else if (S_ISGITLINK(a->mode)) {
694 result.clean = 0;
695 hashcpy(result.sha, a->sha1);
696 } else if (S_ISLNK(a->mode)) {
697 hashcpy(result.sha, a->sha1);
699 if (!sha_eq(a->sha1, b->sha1))
700 result.clean = 0;
701 } else {
702 die("unsupported object type in the tree");
706 return result;
709 static void conflict_rename_rename(struct merge_options *o,
710 struct rename *ren1,
711 const char *branch1,
712 struct rename *ren2,
713 const char *branch2)
715 char *del[2];
716 int delp = 0;
717 const char *ren1_dst = ren1->pair->two->path;
718 const char *ren2_dst = ren2->pair->two->path;
719 const char *dst_name1 = ren1_dst;
720 const char *dst_name2 = ren2_dst;
721 if (string_list_has_string(&current_directory_set, ren1_dst)) {
722 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
723 output(o, 1, "%s is a directory in %s adding as %s instead",
724 ren1_dst, branch2, dst_name1);
725 remove_file(o, 0, ren1_dst, 0);
727 if (string_list_has_string(&current_directory_set, ren2_dst)) {
728 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
729 output(o, 1, "%s is a directory in %s adding as %s instead",
730 ren2_dst, branch1, dst_name2);
731 remove_file(o, 0, ren2_dst, 0);
733 if (o->call_depth) {
734 remove_file_from_cache(dst_name1);
735 remove_file_from_cache(dst_name2);
737 * Uncomment to leave the conflicting names in the resulting tree
739 * update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
740 * update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
742 } else {
743 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
744 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
746 while (delp--)
747 free(del[delp]);
750 static void conflict_rename_dir(struct merge_options *o,
751 struct rename *ren1,
752 const char *branch1)
754 char *new_path = unique_path(ren1->pair->two->path, branch1);
755 output(o, 1, "Renaming %s to %s instead", ren1->pair->one->path, new_path);
756 remove_file(o, 0, ren1->pair->two->path, 0);
757 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
758 free(new_path);
761 static void conflict_rename_rename_2(struct merge_options *o,
762 struct rename *ren1,
763 const char *branch1,
764 struct rename *ren2,
765 const char *branch2)
767 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
768 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
769 output(o, 1, "Renaming %s to %s and %s to %s instead",
770 ren1->pair->one->path, new_path1,
771 ren2->pair->one->path, new_path2);
772 remove_file(o, 0, ren1->pair->two->path, 0);
773 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
774 update_file(o, 0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
775 free(new_path2);
776 free(new_path1);
779 static int process_renames(struct merge_options *o,
780 struct string_list *a_renames,
781 struct string_list *b_renames)
783 int clean_merge = 1, i, j;
784 struct string_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
785 const struct rename *sre;
787 for (i = 0; i < a_renames->nr; i++) {
788 sre = a_renames->items[i].util;
789 string_list_insert(sre->pair->two->path, &a_by_dst)->util
790 = sre->dst_entry;
792 for (i = 0; i < b_renames->nr; i++) {
793 sre = b_renames->items[i].util;
794 string_list_insert(sre->pair->two->path, &b_by_dst)->util
795 = sre->dst_entry;
798 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
799 int compare;
800 char *src;
801 struct string_list *renames1, *renames2, *renames2Dst;
802 struct rename *ren1 = NULL, *ren2 = NULL;
803 const char *branch1, *branch2;
804 const char *ren1_src, *ren1_dst;
806 if (i >= a_renames->nr) {
807 compare = 1;
808 ren2 = b_renames->items[j++].util;
809 } else if (j >= b_renames->nr) {
810 compare = -1;
811 ren1 = a_renames->items[i++].util;
812 } else {
813 compare = strcmp(a_renames->items[i].string,
814 b_renames->items[j].string);
815 if (compare <= 0)
816 ren1 = a_renames->items[i++].util;
817 if (compare >= 0)
818 ren2 = b_renames->items[j++].util;
821 /* TODO: refactor, so that 1/2 are not needed */
822 if (ren1) {
823 renames1 = a_renames;
824 renames2 = b_renames;
825 renames2Dst = &b_by_dst;
826 branch1 = o->branch1;
827 branch2 = o->branch2;
828 } else {
829 struct rename *tmp;
830 renames1 = b_renames;
831 renames2 = a_renames;
832 renames2Dst = &a_by_dst;
833 branch1 = o->branch2;
834 branch2 = o->branch1;
835 tmp = ren2;
836 ren2 = ren1;
837 ren1 = tmp;
839 src = ren1->pair->one->path;
841 ren1->dst_entry->processed = 1;
842 ren1->src_entry->processed = 1;
844 if (ren1->processed)
845 continue;
846 ren1->processed = 1;
848 ren1_src = ren1->pair->one->path;
849 ren1_dst = ren1->pair->two->path;
851 if (ren2) {
852 const char *ren2_src = ren2->pair->one->path;
853 const char *ren2_dst = ren2->pair->two->path;
854 /* Renamed in 1 and renamed in 2 */
855 if (strcmp(ren1_src, ren2_src) != 0)
856 die("ren1.src != ren2.src");
857 ren2->dst_entry->processed = 1;
858 ren2->processed = 1;
859 if (strcmp(ren1_dst, ren2_dst) != 0) {
860 clean_merge = 0;
861 output(o, 1, "CONFLICT (rename/rename): "
862 "Rename \"%s\"->\"%s\" in branch \"%s\" "
863 "rename \"%s\"->\"%s\" in \"%s\"%s",
864 src, ren1_dst, branch1,
865 src, ren2_dst, branch2,
866 o->call_depth ? " (left unresolved)": "");
867 if (o->call_depth) {
868 remove_file_from_cache(src);
869 update_file(o, 0, ren1->pair->one->sha1,
870 ren1->pair->one->mode, src);
872 conflict_rename_rename(o, ren1, branch1, ren2, branch2);
873 } else {
874 struct merge_file_info mfi;
875 remove_file(o, 1, ren1_src, 1);
876 mfi = merge_file(o,
877 ren1->pair->one,
878 ren1->pair->two,
879 ren2->pair->two,
880 branch1,
881 branch2);
882 if (mfi.merge || !mfi.clean)
883 output(o, 1, "Renaming %s->%s", src, ren1_dst);
885 if (mfi.merge)
886 output(o, 2, "Auto-merging %s", ren1_dst);
888 if (!mfi.clean) {
889 output(o, 1, "CONFLICT (content): merge conflict in %s",
890 ren1_dst);
891 clean_merge = 0;
893 if (!o->call_depth)
894 update_stages(ren1_dst,
895 ren1->pair->one,
896 ren1->pair->two,
897 ren2->pair->two,
898 1 /* clear */);
900 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
902 } else {
903 /* Renamed in 1, maybe changed in 2 */
904 struct string_list_item *item;
905 /* we only use sha1 and mode of these */
906 struct diff_filespec src_other, dst_other;
907 int try_merge, stage = a_renames == renames1 ? 3: 2;
909 remove_file(o, 1, ren1_src, o->call_depth || stage == 3);
911 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
912 src_other.mode = ren1->src_entry->stages[stage].mode;
913 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
914 dst_other.mode = ren1->dst_entry->stages[stage].mode;
916 try_merge = 0;
918 if (string_list_has_string(&current_directory_set, ren1_dst)) {
919 clean_merge = 0;
920 output(o, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
921 " directory %s added in %s",
922 ren1_src, ren1_dst, branch1,
923 ren1_dst, branch2);
924 conflict_rename_dir(o, ren1, branch1);
925 } else if (sha_eq(src_other.sha1, null_sha1)) {
926 clean_merge = 0;
927 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
928 "and deleted in %s",
929 ren1_src, ren1_dst, branch1,
930 branch2);
931 update_file(o, 0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
932 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
933 const char *new_path;
934 clean_merge = 0;
935 try_merge = 1;
936 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
937 "%s added in %s",
938 ren1_src, ren1_dst, branch1,
939 ren1_dst, branch2);
940 new_path = unique_path(ren1_dst, branch2);
941 output(o, 1, "Adding as %s instead", new_path);
942 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
943 } else if ((item = string_list_lookup(ren1_dst, renames2Dst))) {
944 ren2 = item->util;
945 clean_merge = 0;
946 ren2->processed = 1;
947 output(o, 1, "CONFLICT (rename/rename): "
948 "Rename %s->%s in %s. "
949 "Rename %s->%s in %s",
950 ren1_src, ren1_dst, branch1,
951 ren2->pair->one->path, ren2->pair->two->path, branch2);
952 conflict_rename_rename_2(o, ren1, branch1, ren2, branch2);
953 } else
954 try_merge = 1;
956 if (try_merge) {
957 struct diff_filespec *one, *a, *b;
958 struct merge_file_info mfi;
959 src_other.path = (char *)ren1_src;
961 one = ren1->pair->one;
962 if (a_renames == renames1) {
963 a = ren1->pair->two;
964 b = &src_other;
965 } else {
966 b = ren1->pair->two;
967 a = &src_other;
969 mfi = merge_file(o, one, a, b,
970 o->branch1, o->branch2);
972 if (mfi.clean &&
973 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
974 mfi.mode == ren1->pair->two->mode)
976 * This messaged is part of
977 * t6022 test. If you change
978 * it update the test too.
980 output(o, 3, "Skipped %s (merged same as existing)", ren1_dst);
981 else {
982 if (mfi.merge || !mfi.clean)
983 output(o, 1, "Renaming %s => %s", ren1_src, ren1_dst);
984 if (mfi.merge)
985 output(o, 2, "Auto-merging %s", ren1_dst);
986 if (!mfi.clean) {
987 output(o, 1, "CONFLICT (rename/modify): Merge conflict in %s",
988 ren1_dst);
989 clean_merge = 0;
991 if (!o->call_depth)
992 update_stages(ren1_dst,
993 one, a, b, 1);
995 update_file(o, mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1000 string_list_clear(&a_by_dst, 0);
1001 string_list_clear(&b_by_dst, 0);
1003 return clean_merge;
1006 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1008 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1011 /* Per entry merge function */
1012 static int process_entry(struct merge_options *o,
1013 const char *path, struct stage_data *entry)
1016 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1017 print_index_entry("\tpath: ", entry);
1019 int clean_merge = 1;
1020 unsigned o_mode = entry->stages[1].mode;
1021 unsigned a_mode = entry->stages[2].mode;
1022 unsigned b_mode = entry->stages[3].mode;
1023 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1024 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1025 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1027 if (o_sha && (!a_sha || !b_sha)) {
1028 /* Case A: Deleted in one */
1029 if ((!a_sha && !b_sha) ||
1030 (sha_eq(a_sha, o_sha) && !b_sha) ||
1031 (!a_sha && sha_eq(b_sha, o_sha))) {
1032 /* Deleted in both or deleted in one and
1033 * unchanged in the other */
1034 if (a_sha)
1035 output(o, 2, "Removing %s", path);
1036 /* do not touch working file if it did not exist */
1037 remove_file(o, 1, path, !a_sha);
1038 } else {
1039 /* Deleted in one and changed in the other */
1040 clean_merge = 0;
1041 if (!a_sha) {
1042 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1043 "and modified in %s. Version %s of %s left in tree.",
1044 path, o->branch1,
1045 o->branch2, o->branch2, path);
1046 update_file(o, 0, b_sha, b_mode, path);
1047 } else {
1048 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1049 "and modified in %s. Version %s of %s left in tree.",
1050 path, o->branch2,
1051 o->branch1, o->branch1, path);
1052 update_file(o, 0, a_sha, a_mode, path);
1056 } else if ((!o_sha && a_sha && !b_sha) ||
1057 (!o_sha && !a_sha && b_sha)) {
1058 /* Case B: Added in one. */
1059 const char *add_branch;
1060 const char *other_branch;
1061 unsigned mode;
1062 const unsigned char *sha;
1063 const char *conf;
1065 if (a_sha) {
1066 add_branch = o->branch1;
1067 other_branch = o->branch2;
1068 mode = a_mode;
1069 sha = a_sha;
1070 conf = "file/directory";
1071 } else {
1072 add_branch = o->branch2;
1073 other_branch = o->branch1;
1074 mode = b_mode;
1075 sha = b_sha;
1076 conf = "directory/file";
1078 if (string_list_has_string(&current_directory_set, path)) {
1079 const char *new_path = unique_path(path, add_branch);
1080 clean_merge = 0;
1081 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1082 "Adding %s as %s",
1083 conf, path, other_branch, path, new_path);
1084 remove_file(o, 0, path, 0);
1085 update_file(o, 0, sha, mode, new_path);
1086 } else {
1087 output(o, 2, "Adding %s", path);
1088 update_file(o, 1, sha, mode, path);
1090 } else if (a_sha && b_sha) {
1091 /* Case C: Added in both (check for same permissions) and */
1092 /* case D: Modified in both, but differently. */
1093 const char *reason = "content";
1094 struct merge_file_info mfi;
1095 struct diff_filespec one, a, b;
1097 if (!o_sha) {
1098 reason = "add/add";
1099 o_sha = (unsigned char *)null_sha1;
1101 output(o, 2, "Auto-merging %s", path);
1102 one.path = a.path = b.path = (char *)path;
1103 hashcpy(one.sha1, o_sha);
1104 one.mode = o_mode;
1105 hashcpy(a.sha1, a_sha);
1106 a.mode = a_mode;
1107 hashcpy(b.sha1, b_sha);
1108 b.mode = b_mode;
1110 mfi = merge_file(o, &one, &a, &b,
1111 o->branch1, o->branch2);
1113 clean_merge = mfi.clean;
1114 if (mfi.clean)
1115 update_file(o, 1, mfi.sha, mfi.mode, path);
1116 else if (S_ISGITLINK(mfi.mode))
1117 output(o, 1, "CONFLICT (submodule): Merge conflict in %s "
1118 "- needs %s", path, sha1_to_hex(b.sha1));
1119 else {
1120 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1121 reason, path);
1123 if (o->call_depth)
1124 update_file(o, 0, mfi.sha, mfi.mode, path);
1125 else
1126 update_file_flags(o, mfi.sha, mfi.mode, path,
1127 0 /* update_cache */, 1 /* update_working_directory */);
1129 } else if (!o_sha && !a_sha && !b_sha) {
1131 * this entry was deleted altogether. a_mode == 0 means
1132 * we had that path and want to actively remove it.
1134 remove_file(o, 1, path, !a_mode);
1135 } else
1136 die("Fatal merge failure, shouldn't happen.");
1138 return clean_merge;
1141 int merge_trees(struct merge_options *o,
1142 struct tree *head,
1143 struct tree *merge,
1144 struct tree *common,
1145 struct tree **result)
1147 int code, clean;
1149 if (o->subtree_merge) {
1150 merge = shift_tree_object(head, merge);
1151 common = shift_tree_object(head, common);
1154 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1155 output(o, 0, "Already uptodate!");
1156 *result = head;
1157 return 1;
1160 code = git_merge_trees(o->call_depth, common, head, merge);
1162 if (code != 0)
1163 die("merging of trees %s and %s failed",
1164 sha1_to_hex(head->object.sha1),
1165 sha1_to_hex(merge->object.sha1));
1167 if (unmerged_cache()) {
1168 struct string_list *entries, *re_head, *re_merge;
1169 int i;
1170 string_list_clear(&current_file_set, 1);
1171 string_list_clear(&current_directory_set, 1);
1172 get_files_dirs(head);
1173 get_files_dirs(merge);
1175 entries = get_unmerged();
1176 re_head = get_renames(o, head, common, head, merge, entries);
1177 re_merge = get_renames(o, merge, common, head, merge, entries);
1178 clean = process_renames(o, re_head, re_merge);
1179 for (i = 0; i < entries->nr; i++) {
1180 const char *path = entries->items[i].string;
1181 struct stage_data *e = entries->items[i].util;
1182 if (!e->processed
1183 && !process_entry(o, path, e))
1184 clean = 0;
1187 string_list_clear(re_merge, 0);
1188 string_list_clear(re_head, 0);
1189 string_list_clear(entries, 1);
1192 else
1193 clean = 1;
1195 if (o->call_depth)
1196 *result = write_tree_from_memory(o);
1198 return clean;
1201 static struct commit_list *reverse_commit_list(struct commit_list *list)
1203 struct commit_list *next = NULL, *current, *backup;
1204 for (current = list; current; current = backup) {
1205 backup = current->next;
1206 current->next = next;
1207 next = current;
1209 return next;
1213 * Merge the commits h1 and h2, return the resulting virtual
1214 * commit object and a flag indicating the cleanness of the merge.
1216 int merge_recursive(struct merge_options *o,
1217 struct commit *h1,
1218 struct commit *h2,
1219 struct commit_list *ca,
1220 struct commit **result)
1222 struct commit_list *iter;
1223 struct commit *merged_common_ancestors;
1224 struct tree *mrtree = mrtree;
1225 int clean;
1227 if (show(o, 4)) {
1228 output(o, 4, "Merging:");
1229 output_commit_title(o, h1);
1230 output_commit_title(o, h2);
1233 if (!ca) {
1234 ca = get_merge_bases(h1, h2, 1);
1235 ca = reverse_commit_list(ca);
1238 if (show(o, 5)) {
1239 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1240 for (iter = ca; iter; iter = iter->next)
1241 output_commit_title(o, iter->item);
1244 merged_common_ancestors = pop_commit(&ca);
1245 if (merged_common_ancestors == NULL) {
1246 /* if there is no common ancestor, make an empty tree */
1247 struct tree *tree = xcalloc(1, sizeof(struct tree));
1249 tree->object.parsed = 1;
1250 tree->object.type = OBJ_TREE;
1251 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1252 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1255 for (iter = ca; iter; iter = iter->next) {
1256 const char *saved_b1, *saved_b2;
1257 o->call_depth++;
1259 * When the merge fails, the result contains files
1260 * with conflict markers. The cleanness flag is
1261 * ignored, it was never actually used, as result of
1262 * merge_trees has always overwritten it: the committed
1263 * "conflicts" were already resolved.
1265 discard_cache();
1266 saved_b1 = o->branch1;
1267 saved_b2 = o->branch2;
1268 o->branch1 = "Temporary merge branch 1";
1269 o->branch2 = "Temporary merge branch 2";
1270 merge_recursive(o, merged_common_ancestors, iter->item,
1271 NULL, &merged_common_ancestors);
1272 o->branch1 = saved_b1;
1273 o->branch2 = saved_b2;
1274 o->call_depth--;
1276 if (!merged_common_ancestors)
1277 die("merge returned no commit");
1280 discard_cache();
1281 if (!o->call_depth)
1282 read_cache();
1284 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1285 &mrtree);
1287 if (o->call_depth) {
1288 *result = make_virtual_commit(mrtree, "merged tree");
1289 commit_list_insert(h1, &(*result)->parents);
1290 commit_list_insert(h2, &(*result)->parents->next);
1292 flush_output();
1293 return clean;
1296 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1298 struct object *object;
1300 object = deref_tag(parse_object(sha1), name, strlen(name));
1301 if (!object)
1302 return NULL;
1303 if (object->type == OBJ_TREE)
1304 return make_virtual_commit((struct tree*)object, name);
1305 if (object->type != OBJ_COMMIT)
1306 return NULL;
1307 if (parse_commit((struct commit *)object))
1308 return NULL;
1309 return (struct commit *)object;
1312 int merge_recursive_generic(struct merge_options *o,
1313 const unsigned char *head,
1314 const unsigned char *merge,
1315 int num_base_list,
1316 const unsigned char **base_list,
1317 struct commit **result)
1319 int clean, index_fd;
1320 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1321 struct commit *head_commit = get_ref(head, o->branch1);
1322 struct commit *next_commit = get_ref(merge, o->branch2);
1323 struct commit_list *ca = NULL;
1325 if (base_list) {
1326 int i;
1327 for (i = 0; i < num_base_list; ++i) {
1328 struct commit *base;
1329 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1330 return error("Could not parse object '%s'",
1331 sha1_to_hex(base_list[i]));
1332 commit_list_insert(base, &ca);
1336 index_fd = hold_locked_index(lock, 1);
1337 clean = merge_recursive(o, head_commit, next_commit, ca,
1338 result);
1339 if (active_cache_changed &&
1340 (write_cache(index_fd, active_cache, active_nr) ||
1341 commit_locked_index(lock)))
1342 return error("Unable to write index.");
1344 return clean ? 0 : 1;
1347 static int merge_recursive_config(const char *var, const char *value, void *cb)
1349 struct merge_options *o = cb;
1350 if (!strcasecmp(var, "merge.verbosity")) {
1351 o->verbosity = git_config_int(var, value);
1352 return 0;
1354 if (!strcasecmp(var, "diff.renamelimit")) {
1355 o->diff_rename_limit = git_config_int(var, value);
1356 return 0;
1358 if (!strcasecmp(var, "merge.renamelimit")) {
1359 o->merge_rename_limit = git_config_int(var, value);
1360 return 0;
1362 return git_default_config(var, value, cb);
1365 void init_merge_options(struct merge_options *o)
1367 memset(o, 0, sizeof(struct merge_options));
1368 o->verbosity = 2;
1369 o->buffer_output = 1;
1370 o->diff_rename_limit = -1;
1371 o->merge_rename_limit = -1;
1372 git_config(merge_recursive_config, o);
1373 if (getenv("GIT_MERGE_VERBOSITY"))
1374 o->verbosity =
1375 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1376 if (o->verbosity >= 5)
1377 o->buffer_output = 0;