ccb934356371f38ea989cb8912e6c3afb81cc8fb
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 struct tree *shift_tree_object(struct tree *one, struct tree *two,
26 const char *subtree_shift)
27 {
28 unsigned char shifted[20];
30 if (!*subtree_shift) {
31 shift_tree(one->object.sha1, two->object.sha1, shifted, 0);
32 } else {
33 shift_tree_by(one->object.sha1, two->object.sha1, shifted,
34 subtree_shift);
35 }
36 if (!hashcmp(two->object.sha1, shifted))
37 return two;
38 return lookup_tree(shifted);
39 }
41 /*
42 * A virtual commit has (const char *)commit->util set to the name.
43 */
45 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
46 {
47 struct commit *commit = xcalloc(1, sizeof(struct commit));
48 commit->tree = tree;
49 commit->util = (void*)comment;
50 /* avoid warnings */
51 commit->object.parsed = 1;
52 return commit;
53 }
55 /*
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.
58 */
59 static int sha_eq(const unsigned char *a, const unsigned char *b)
60 {
61 if (!a && !b)
62 return 2;
63 return a && b && hashcmp(a, b) == 0;
64 }
66 enum rename_type {
67 RENAME_NORMAL = 0,
68 RENAME_DELETE,
69 RENAME_ONE_FILE_TO_ONE,
70 RENAME_ONE_FILE_TO_TWO,
71 RENAME_TWO_FILES_TO_ONE
72 };
74 struct rename_conflict_info {
75 enum rename_type rename_type;
76 struct diff_filepair *pair1;
77 struct diff_filepair *pair2;
78 const char *branch1;
79 const char *branch2;
80 struct stage_data *dst_entry1;
81 struct stage_data *dst_entry2;
82 };
84 /*
85 * Since we want to write the index eventually, we cannot reuse the index
86 * for these (temporary) data.
87 */
88 struct stage_data {
89 struct {
90 unsigned mode;
91 unsigned char sha[20];
92 } stages[4];
93 struct rename_conflict_info *rename_conflict_info;
94 unsigned processed:1;
95 };
97 static inline void setup_rename_conflict_info(enum rename_type rename_type,
98 struct diff_filepair *pair1,
99 struct diff_filepair *pair2,
100 const char *branch1,
101 const char *branch2,
102 struct stage_data *dst_entry1,
103 struct stage_data *dst_entry2)
104 {
105 struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
106 ci->rename_type = rename_type;
107 ci->pair1 = pair1;
108 ci->branch1 = branch1;
109 ci->branch2 = branch2;
111 ci->dst_entry1 = dst_entry1;
112 dst_entry1->rename_conflict_info = ci;
113 dst_entry1->processed = 0;
115 assert(!pair2 == !dst_entry2);
116 if (dst_entry2) {
117 ci->dst_entry2 = dst_entry2;
118 ci->pair2 = pair2;
119 dst_entry2->rename_conflict_info = ci;
120 }
121 }
123 static int show(struct merge_options *o, int v)
124 {
125 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
126 }
128 static void flush_output(struct merge_options *o)
129 {
130 if (o->obuf.len) {
131 fputs(o->obuf.buf, stdout);
132 strbuf_reset(&o->obuf);
133 }
134 }
136 __attribute__((format (printf, 3, 4)))
137 static void output(struct merge_options *o, int v, const char *fmt, ...)
138 {
139 va_list ap;
141 if (!show(o, v))
142 return;
144 strbuf_grow(&o->obuf, o->call_depth * 2 + 2);
145 memset(o->obuf.buf + o->obuf.len, ' ', o->call_depth * 2);
146 strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);
148 va_start(ap, fmt);
149 strbuf_vaddf(&o->obuf, fmt, ap);
150 va_end(ap);
152 strbuf_add(&o->obuf, "\n", 1);
153 if (!o->buffer_output)
154 flush_output(o);
155 }
157 static void output_commit_title(struct merge_options *o, struct commit *commit)
158 {
159 int i;
160 flush_output(o);
161 for (i = o->call_depth; i--;)
162 fputs(" ", stdout);
163 if (commit->util)
164 printf("virtual %s\n", (char *)commit->util);
165 else {
166 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
167 if (parse_commit(commit) != 0)
168 printf("(bad commit)\n");
169 else {
170 const char *title;
171 int len = find_commit_subject(commit->buffer, &title);
172 if (len)
173 printf("%.*s\n", len, title);
174 }
175 }
176 }
178 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
179 const char *path, int stage, int refresh, int options)
180 {
181 struct cache_entry *ce;
182 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
183 if (!ce)
184 return error("addinfo_cache failed for path '%s'", path);
185 return add_cache_entry(ce, options);
186 }
188 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
189 {
190 parse_tree(tree);
191 init_tree_desc(desc, tree->buffer, tree->size);
192 }
194 static int git_merge_trees(int index_only,
195 struct tree *common,
196 struct tree *head,
197 struct tree *merge)
198 {
199 int rc;
200 struct tree_desc t[3];
201 struct unpack_trees_options opts;
203 memset(&opts, 0, sizeof(opts));
204 if (index_only)
205 opts.index_only = 1;
206 else
207 opts.update = 1;
208 opts.merge = 1;
209 opts.head_idx = 2;
210 opts.fn = threeway_merge;
211 opts.src_index = &the_index;
212 opts.dst_index = &the_index;
213 setup_unpack_trees_porcelain(&opts, "merge");
215 init_tree_desc_from_tree(t+0, common);
216 init_tree_desc_from_tree(t+1, head);
217 init_tree_desc_from_tree(t+2, merge);
219 rc = unpack_trees(3, t, &opts);
220 cache_tree_free(&active_cache_tree);
221 return rc;
222 }
224 struct tree *write_tree_from_memory(struct merge_options *o)
225 {
226 struct tree *result = NULL;
228 if (unmerged_cache()) {
229 int i;
230 fprintf(stderr, "BUG: There are unmerged index entries:\n");
231 for (i = 0; i < active_nr; i++) {
232 struct cache_entry *ce = active_cache[i];
233 if (ce_stage(ce))
234 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
235 (int)ce_namelen(ce), ce->name);
236 }
237 die("Bug in merge-recursive.c");
238 }
240 if (!active_cache_tree)
241 active_cache_tree = cache_tree();
243 if (!cache_tree_fully_valid(active_cache_tree) &&
244 cache_tree_update(active_cache_tree,
245 active_cache, active_nr, 0, 0) < 0)
246 die("error building trees");
248 result = lookup_tree(active_cache_tree->sha1);
250 return result;
251 }
253 static int save_files_dirs(const unsigned char *sha1,
254 const char *base, int baselen, const char *path,
255 unsigned int mode, int stage, void *context)
256 {
257 int len = strlen(path);
258 char *newpath = xmalloc(baselen + len + 1);
259 struct merge_options *o = context;
261 memcpy(newpath, base, baselen);
262 memcpy(newpath + baselen, path, len);
263 newpath[baselen + len] = '\0';
265 if (S_ISDIR(mode))
266 string_list_insert(&o->current_directory_set, newpath);
267 else
268 string_list_insert(&o->current_file_set, newpath);
269 free(newpath);
271 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
272 }
274 static int get_files_dirs(struct merge_options *o, struct tree *tree)
275 {
276 int n;
277 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
278 return 0;
279 n = o->current_file_set.nr + o->current_directory_set.nr;
280 return n;
281 }
283 /*
284 * Returns an index_entry instance which doesn't have to correspond to
285 * a real cache entry in Git's index.
286 */
287 static struct stage_data *insert_stage_data(const char *path,
288 struct tree *o, struct tree *a, struct tree *b,
289 struct string_list *entries)
290 {
291 struct string_list_item *item;
292 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
293 get_tree_entry(o->object.sha1, path,
294 e->stages[1].sha, &e->stages[1].mode);
295 get_tree_entry(a->object.sha1, path,
296 e->stages[2].sha, &e->stages[2].mode);
297 get_tree_entry(b->object.sha1, path,
298 e->stages[3].sha, &e->stages[3].mode);
299 item = string_list_insert(entries, path);
300 item->util = e;
301 return e;
302 }
304 /*
305 * Create a dictionary mapping file names to stage_data objects. The
306 * dictionary contains one entry for every path with a non-zero stage entry.
307 */
308 static struct string_list *get_unmerged(void)
309 {
310 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
311 int i;
313 unmerged->strdup_strings = 1;
315 for (i = 0; i < active_nr; i++) {
316 struct string_list_item *item;
317 struct stage_data *e;
318 struct cache_entry *ce = active_cache[i];
319 if (!ce_stage(ce))
320 continue;
322 item = string_list_lookup(unmerged, ce->name);
323 if (!item) {
324 item = string_list_insert(unmerged, ce->name);
325 item->util = xcalloc(1, sizeof(struct stage_data));
326 }
327 e = item->util;
328 e->stages[ce_stage(ce)].mode = ce->ce_mode;
329 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
330 }
332 return unmerged;
333 }
335 static int string_list_df_name_compare(const void *a, const void *b)
336 {
337 const struct string_list_item *one = a;
338 const struct string_list_item *two = b;
339 int onelen = strlen(one->string);
340 int twolen = strlen(two->string);
341 /*
342 * Here we only care that entries for D/F conflicts are
343 * adjacent, in particular with the file of the D/F conflict
344 * appearing before files below the corresponding directory.
345 * The order of the rest of the list is irrelevant for us.
346 *
347 * To achieve this, we sort with df_name_compare and provide
348 * the mode S_IFDIR so that D/F conflicts will sort correctly.
349 * We use the mode S_IFDIR for everything else for simplicity,
350 * since in other cases any changes in their order due to
351 * sorting cause no problems for us.
352 */
353 int cmp = df_name_compare(one->string, onelen, S_IFDIR,
354 two->string, twolen, S_IFDIR);
355 /*
356 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
357 * that 'foo' comes before 'foo/bar'.
358 */
359 if (cmp)
360 return cmp;
361 return onelen - twolen;
362 }
364 static void record_df_conflict_files(struct merge_options *o,
365 struct string_list *entries)
366 {
367 /* If there is a D/F conflict and the file for such a conflict
368 * currently exist in the working copy, we want to allow it to be
369 * removed to make room for the corresponding directory if needed.
370 * The files underneath the directories of such D/F conflicts will
371 * be processed before the corresponding file involved in the D/F
372 * conflict. If the D/F directory ends up being removed by the
373 * merge, then we won't have to touch the D/F file. If the D/F
374 * directory needs to be written to the working copy, then the D/F
375 * file will simply be removed (in make_room_for_path()) to make
376 * room for the necessary paths. Note that if both the directory
377 * and the file need to be present, then the D/F file will be
378 * reinstated with a new unique name at the time it is processed.
379 */
380 const char *last_file = NULL;
381 int last_len = 0;
382 int i;
384 /*
385 * If we're merging merge-bases, we don't want to bother with
386 * any working directory changes.
387 */
388 if (o->call_depth)
389 return;
391 /* Ensure D/F conflicts are adjacent in the entries list. */
392 qsort(entries->items, entries->nr, sizeof(*entries->items),
393 string_list_df_name_compare);
395 string_list_clear(&o->df_conflict_file_set, 1);
396 for (i = 0; i < entries->nr; i++) {
397 const char *path = entries->items[i].string;
398 int len = strlen(path);
399 struct stage_data *e = entries->items[i].util;
401 /*
402 * Check if last_file & path correspond to a D/F conflict;
403 * i.e. whether path is last_file+'/'+<something>.
404 * If so, record that it's okay to remove last_file to make
405 * room for path and friends if needed.
406 */
407 if (last_file &&
408 len > last_len &&
409 memcmp(path, last_file, last_len) == 0 &&
410 path[last_len] == '/') {
411 string_list_insert(&o->df_conflict_file_set, last_file);
412 }
414 /*
415 * Determine whether path could exist as a file in the
416 * working directory as a possible D/F conflict. This
417 * will only occur when it exists in stage 2 as a
418 * file.
419 */
420 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
421 last_file = path;
422 last_len = len;
423 } else {
424 last_file = NULL;
425 }
426 }
427 }
429 struct rename {
430 struct diff_filepair *pair;
431 struct stage_data *src_entry;
432 struct stage_data *dst_entry;
433 unsigned processed:1;
434 };
436 /*
437 * Get information of all renames which occurred between 'o_tree' and
438 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
439 * 'b_tree') to be able to associate the correct cache entries with
440 * the rename information. 'tree' is always equal to either a_tree or b_tree.
441 */
442 static struct string_list *get_renames(struct merge_options *o,
443 struct tree *tree,
444 struct tree *o_tree,
445 struct tree *a_tree,
446 struct tree *b_tree,
447 struct string_list *entries)
448 {
449 int i;
450 struct string_list *renames;
451 struct diff_options opts;
453 renames = xcalloc(1, sizeof(struct string_list));
454 diff_setup(&opts);
455 DIFF_OPT_SET(&opts, RECURSIVE);
456 opts.detect_rename = DIFF_DETECT_RENAME;
457 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
458 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
459 1000;
460 opts.rename_score = o->rename_score;
461 opts.show_rename_progress = o->show_rename_progress;
462 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
463 if (diff_setup_done(&opts) < 0)
464 die("diff setup failed");
465 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
466 diffcore_std(&opts);
467 if (opts.needed_rename_limit > o->needed_rename_limit)
468 o->needed_rename_limit = opts.needed_rename_limit;
469 for (i = 0; i < diff_queued_diff.nr; ++i) {
470 struct string_list_item *item;
471 struct rename *re;
472 struct diff_filepair *pair = diff_queued_diff.queue[i];
473 if (pair->status != 'R') {
474 diff_free_filepair(pair);
475 continue;
476 }
477 re = xmalloc(sizeof(*re));
478 re->processed = 0;
479 re->pair = pair;
480 item = string_list_lookup(entries, re->pair->one->path);
481 if (!item)
482 re->src_entry = insert_stage_data(re->pair->one->path,
483 o_tree, a_tree, b_tree, entries);
484 else
485 re->src_entry = item->util;
487 item = string_list_lookup(entries, re->pair->two->path);
488 if (!item)
489 re->dst_entry = insert_stage_data(re->pair->two->path,
490 o_tree, a_tree, b_tree, entries);
491 else
492 re->dst_entry = item->util;
493 item = string_list_insert(renames, pair->one->path);
494 item->util = re;
495 }
496 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
497 diff_queued_diff.nr = 0;
498 diff_flush(&opts);
499 return renames;
500 }
502 static int update_stages(const char *path, const struct diff_filespec *o,
503 const struct diff_filespec *a,
504 const struct diff_filespec *b)
505 {
506 int clear = 1;
507 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
508 if (clear)
509 if (remove_file_from_cache(path))
510 return -1;
511 if (o)
512 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
513 return -1;
514 if (a)
515 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
516 return -1;
517 if (b)
518 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
519 return -1;
520 return 0;
521 }
523 static void update_entry(struct stage_data *entry,
524 struct diff_filespec *o,
525 struct diff_filespec *a,
526 struct diff_filespec *b)
527 {
528 entry->processed = 0;
529 entry->stages[1].mode = o->mode;
530 entry->stages[2].mode = a->mode;
531 entry->stages[3].mode = b->mode;
532 hashcpy(entry->stages[1].sha, o->sha1);
533 hashcpy(entry->stages[2].sha, a->sha1);
534 hashcpy(entry->stages[3].sha, b->sha1);
535 }
537 static int remove_file(struct merge_options *o, int clean,
538 const char *path, int no_wd)
539 {
540 int update_cache = o->call_depth || clean;
541 int update_working_directory = !o->call_depth && !no_wd;
543 if (update_cache) {
544 if (remove_file_from_cache(path))
545 return -1;
546 }
547 if (update_working_directory) {
548 if (remove_path(path))
549 return -1;
550 }
551 return 0;
552 }
554 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
555 {
556 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
557 int suffix = 0;
558 struct stat st;
559 char *p = newpath + strlen(path);
560 strcpy(newpath, path);
561 *(p++) = '~';
562 strcpy(p, branch);
563 for (; *p; ++p)
564 if ('/' == *p)
565 *p = '_';
566 while (string_list_has_string(&o->current_file_set, newpath) ||
567 string_list_has_string(&o->current_directory_set, newpath) ||
568 lstat(newpath, &st) == 0)
569 sprintf(p, "_%d", suffix++);
571 string_list_insert(&o->current_file_set, newpath);
572 return newpath;
573 }
575 static void flush_buffer(int fd, const char *buf, unsigned long size)
576 {
577 while (size > 0) {
578 long ret = write_in_full(fd, buf, size);
579 if (ret < 0) {
580 /* Ignore epipe */
581 if (errno == EPIPE)
582 break;
583 die_errno("merge-recursive");
584 } else if (!ret) {
585 die("merge-recursive: disk full?");
586 }
587 size -= ret;
588 buf += ret;
589 }
590 }
592 static int dir_in_way(const char *path, int check_working_copy)
593 {
594 int pos, pathlen = strlen(path);
595 char *dirpath = xmalloc(pathlen + 2);
596 struct stat st;
598 strcpy(dirpath, path);
599 dirpath[pathlen] = '/';
600 dirpath[pathlen+1] = '\0';
602 pos = cache_name_pos(dirpath, pathlen+1);
604 if (pos < 0)
605 pos = -1 - pos;
606 if (pos < active_nr &&
607 !strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
608 free(dirpath);
609 return 1;
610 }
612 free(dirpath);
613 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
614 }
616 static int was_tracked(const char *path)
617 {
618 int pos = cache_name_pos(path, strlen(path));
620 if (pos < 0)
621 pos = -1 - pos;
622 while (pos < active_nr &&
623 !strcmp(path, active_cache[pos]->name)) {
624 /*
625 * If stage #0, it is definitely tracked.
626 * If it has stage #2 then it was tracked
627 * before this merge started. All other
628 * cases the path was not tracked.
629 */
630 switch (ce_stage(active_cache[pos])) {
631 case 0:
632 case 2:
633 return 1;
634 }
635 pos++;
636 }
637 return 0;
638 }
640 static int would_lose_untracked(const char *path)
641 {
642 return !was_tracked(path) && file_exists(path);
643 }
645 static int make_room_for_path(struct merge_options *o, const char *path)
646 {
647 int status, i;
648 const char *msg = "failed to create path '%s'%s";
650 /* Unlink any D/F conflict files that are in the way */
651 for (i = 0; i < o->df_conflict_file_set.nr; i++) {
652 const char *df_path = o->df_conflict_file_set.items[i].string;
653 size_t pathlen = strlen(path);
654 size_t df_pathlen = strlen(df_path);
655 if (df_pathlen < pathlen &&
656 path[df_pathlen] == '/' &&
657 strncmp(path, df_path, df_pathlen) == 0) {
658 output(o, 3,
659 "Removing %s to make room for subdirectory\n",
660 df_path);
661 unlink(df_path);
662 unsorted_string_list_delete_item(&o->df_conflict_file_set,
663 i, 0);
664 break;
665 }
666 }
668 /* Make sure leading directories are created */
669 status = safe_create_leading_directories_const(path);
670 if (status) {
671 if (status == -3) {
672 /* something else exists */
673 error(msg, path, ": perhaps a D/F conflict?");
674 return -1;
675 }
676 die(msg, path, "");
677 }
679 /*
680 * Do not unlink a file in the work tree if we are not
681 * tracking it.
682 */
683 if (would_lose_untracked(path))
684 return error("refusing to lose untracked file at '%s'",
685 path);
687 /* Successful unlink is good.. */
688 if (!unlink(path))
689 return 0;
690 /* .. and so is no existing file */
691 if (errno == ENOENT)
692 return 0;
693 /* .. but not some other error (who really cares what?) */
694 return error(msg, path, ": perhaps a D/F conflict?");
695 }
697 static void update_file_flags(struct merge_options *o,
698 const unsigned char *sha,
699 unsigned mode,
700 const char *path,
701 int update_cache,
702 int update_wd)
703 {
704 if (o->call_depth)
705 update_wd = 0;
707 if (update_wd) {
708 enum object_type type;
709 void *buf;
710 unsigned long size;
712 if (S_ISGITLINK(mode)) {
713 /*
714 * We may later decide to recursively descend into
715 * the submodule directory and update its index
716 * and/or work tree, but we do not do that now.
717 */
718 update_wd = 0;
719 goto update_index;
720 }
722 buf = read_sha1_file(sha, &type, &size);
723 if (!buf)
724 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
725 if (type != OBJ_BLOB)
726 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
727 if (S_ISREG(mode)) {
728 struct strbuf strbuf = STRBUF_INIT;
729 if (convert_to_working_tree(path, buf, size, &strbuf)) {
730 free(buf);
731 size = strbuf.len;
732 buf = strbuf_detach(&strbuf, NULL);
733 }
734 }
736 if (make_room_for_path(o, path) < 0) {
737 update_wd = 0;
738 free(buf);
739 goto update_index;
740 }
741 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
742 int fd;
743 if (mode & 0100)
744 mode = 0777;
745 else
746 mode = 0666;
747 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
748 if (fd < 0)
749 die_errno("failed to open '%s'", path);
750 flush_buffer(fd, buf, size);
751 close(fd);
752 } else if (S_ISLNK(mode)) {
753 char *lnk = xmemdupz(buf, size);
754 safe_create_leading_directories_const(path);
755 unlink(path);
756 if (symlink(lnk, path))
757 die_errno("failed to symlink '%s'", path);
758 free(lnk);
759 } else
760 die("do not know what to do with %06o %s '%s'",
761 mode, sha1_to_hex(sha), path);
762 free(buf);
763 }
764 update_index:
765 if (update_cache)
766 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
767 }
769 static void update_file(struct merge_options *o,
770 int clean,
771 const unsigned char *sha,
772 unsigned mode,
773 const char *path)
774 {
775 update_file_flags(o, sha, mode, path, o->call_depth || clean, !o->call_depth);
776 }
778 /* Low level file merging, update and removal */
780 struct merge_file_info {
781 unsigned char sha[20];
782 unsigned mode;
783 unsigned clean:1,
784 merge:1;
785 };
787 static int merge_3way(struct merge_options *o,
788 mmbuffer_t *result_buf,
789 const struct diff_filespec *one,
790 const struct diff_filespec *a,
791 const struct diff_filespec *b,
792 const char *branch1,
793 const char *branch2)
794 {
795 mmfile_t orig, src1, src2;
796 struct ll_merge_options ll_opts = {0};
797 char *base_name, *name1, *name2;
798 int merge_status;
800 ll_opts.renormalize = o->renormalize;
801 ll_opts.xdl_opts = o->xdl_opts;
803 if (o->call_depth) {
804 ll_opts.virtual_ancestor = 1;
805 ll_opts.variant = 0;
806 } else {
807 switch (o->recursive_variant) {
808 case MERGE_RECURSIVE_OURS:
809 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
810 break;
811 case MERGE_RECURSIVE_THEIRS:
812 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
813 break;
814 default:
815 ll_opts.variant = 0;
816 break;
817 }
818 }
820 if (strcmp(a->path, b->path) ||
821 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
822 base_name = o->ancestor == NULL ? NULL :
823 xstrdup(mkpath("%s:%s", o->ancestor, one->path));
824 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
825 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
826 } else {
827 base_name = o->ancestor == NULL ? NULL :
828 xstrdup(mkpath("%s", o->ancestor));
829 name1 = xstrdup(mkpath("%s", branch1));
830 name2 = xstrdup(mkpath("%s", branch2));
831 }
833 read_mmblob(&orig, one->sha1);
834 read_mmblob(&src1, a->sha1);
835 read_mmblob(&src2, b->sha1);
837 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
838 &src1, name1, &src2, name2, &ll_opts);
840 free(name1);
841 free(name2);
842 free(orig.ptr);
843 free(src1.ptr);
844 free(src2.ptr);
845 return merge_status;
846 }
848 static struct merge_file_info merge_file_1(struct merge_options *o,
849 const struct diff_filespec *one,
850 const struct diff_filespec *a,
851 const struct diff_filespec *b,
852 const char *branch1,
853 const char *branch2)
854 {
855 struct merge_file_info result;
856 result.merge = 0;
857 result.clean = 1;
859 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
860 result.clean = 0;
861 if (S_ISREG(a->mode)) {
862 result.mode = a->mode;
863 hashcpy(result.sha, a->sha1);
864 } else {
865 result.mode = b->mode;
866 hashcpy(result.sha, b->sha1);
867 }
868 } else {
869 if (!sha_eq(a->sha1, one->sha1) && !sha_eq(b->sha1, one->sha1))
870 result.merge = 1;
872 /*
873 * Merge modes
874 */
875 if (a->mode == b->mode || a->mode == one->mode)
876 result.mode = b->mode;
877 else {
878 result.mode = a->mode;
879 if (b->mode != one->mode) {
880 result.clean = 0;
881 result.merge = 1;
882 }
883 }
885 if (sha_eq(a->sha1, b->sha1) || sha_eq(a->sha1, one->sha1))
886 hashcpy(result.sha, b->sha1);
887 else if (sha_eq(b->sha1, one->sha1))
888 hashcpy(result.sha, a->sha1);
889 else if (S_ISREG(a->mode)) {
890 mmbuffer_t result_buf;
891 int merge_status;
893 merge_status = merge_3way(o, &result_buf, one, a, b,
894 branch1, branch2);
896 if ((merge_status < 0) || !result_buf.ptr)
897 die("Failed to execute internal merge");
899 if (write_sha1_file(result_buf.ptr, result_buf.size,
900 blob_type, result.sha))
901 die("Unable to add %s to database",
902 a->path);
904 free(result_buf.ptr);
905 result.clean = (merge_status == 0);
906 } else if (S_ISGITLINK(a->mode)) {
907 result.clean = merge_submodule(result.sha, one->path, one->sha1,
908 a->sha1, b->sha1);
909 } else if (S_ISLNK(a->mode)) {
910 hashcpy(result.sha, a->sha1);
912 if (!sha_eq(a->sha1, b->sha1))
913 result.clean = 0;
914 } else {
915 die("unsupported object type in the tree");
916 }
917 }
919 return result;
920 }
922 static struct merge_file_info merge_file(struct merge_options *o,
923 const char *path,
924 const unsigned char *o_sha, int o_mode,
925 const unsigned char *a_sha, int a_mode,
926 const unsigned char *b_sha, int b_mode,
927 const char *branch1,
928 const char *branch2)
929 {
930 struct diff_filespec one, a, b;
932 one.path = a.path = b.path = (char *)path;
933 hashcpy(one.sha1, o_sha);
934 one.mode = o_mode;
935 hashcpy(a.sha1, a_sha);
936 a.mode = a_mode;
937 hashcpy(b.sha1, b_sha);
938 b.mode = b_mode;
939 return merge_file_1(o, &one, &a, &b, branch1, branch2);
940 }
942 static void conflict_rename_delete(struct merge_options *o,
943 struct diff_filepair *pair,
944 const char *rename_branch,
945 const char *other_branch)
946 {
947 char *dest_name = pair->two->path;
948 int df_conflict = 0;
950 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
951 "and deleted in %s",
952 pair->one->path, pair->two->path, rename_branch,
953 other_branch);
954 if (!o->call_depth)
955 update_stages(dest_name, NULL,
956 rename_branch == o->branch1 ? pair->two : NULL,
957 rename_branch == o->branch1 ? NULL : pair->two);
958 if (dir_in_way(dest_name, !o->call_depth)) {
959 dest_name = unique_path(o, dest_name, rename_branch);
960 df_conflict = 1;
961 }
962 update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
963 if (df_conflict)
964 free(dest_name);
965 }
967 static void conflict_rename_rename_1to2(struct merge_options *o,
968 struct rename_conflict_info *ci)
969 {
970 /* One file was renamed in both branches, but to different names. */
971 struct diff_filespec *one = ci->pair1->one;
972 struct diff_filespec *a = ci->pair1->two;
973 struct diff_filespec *b = ci->pair2->two;
974 const char *dst_name_a = a->path;
975 const char *dst_name_b = b->path;
976 char *del[2];
977 int delp = 0;
979 output(o, 1, "CONFLICT (rename/rename): "
980 "Rename \"%s\"->\"%s\" in branch \"%s\" "
981 "rename \"%s\"->\"%s\" in \"%s\"%s",
982 one->path, a->path, ci->branch1,
983 one->path, b->path, ci->branch2,
984 o->call_depth ? " (left unresolved)" : "");
985 if (dir_in_way(a->path, !o->call_depth)) {
986 dst_name_a = del[delp++] = unique_path(o, a->path, ci->branch1);
987 output(o, 1, "%s is a directory in %s adding as %s instead",
988 a->path, ci->branch2, dst_name_a);
989 }
990 if (dir_in_way(b->path, !o->call_depth)) {
991 dst_name_b = del[delp++] = unique_path(o, b->path, ci->branch2);
992 output(o, 1, "%s is a directory in %s adding as %s instead",
993 b->path, ci->branch1, dst_name_b);
994 }
995 if (o->call_depth) {
996 struct merge_file_info mfi;
997 mfi = merge_file(o, one->path,
998 one->sha1, one->mode,
999 a->sha1, a->mode,
1000 b->sha1, b->mode,
1001 ci->branch1, ci->branch2);
1002 /*
1003 * FIXME: For rename/add-source conflicts (if we could detect
1004 * such), this is wrong. We should instead find a unique
1005 * pathname and then either rename the add-source file to that
1006 * unique path, or use that unique path instead of src here.
1007 */
1008 update_file(o, 0, mfi.sha, mfi.mode, one->path);
1009 remove_file_from_cache(a->path);
1010 remove_file_from_cache(b->path);
1011 } else {
1012 update_stages(a->path, NULL, a, NULL);
1013 update_stages(b->path, NULL, NULL, b);
1015 update_file(o, 0, a->sha1, a->mode, dst_name_a);
1016 update_file(o, 0, b->sha1, b->mode, dst_name_b);
1017 }
1018 while (delp--)
1019 free(del[delp]);
1020 }
1022 static void conflict_rename_rename_2to1(struct merge_options *o,
1023 struct rename_conflict_info *ci)
1024 {
1025 /* Two files, a & b, were renamed to the same thing, c. */
1026 struct diff_filespec *a = ci->pair1->one;
1027 struct diff_filespec *b = ci->pair2->one;
1028 struct diff_filespec *c1 = ci->pair1->two;
1029 struct diff_filespec *c2 = ci->pair2->two;
1030 char *path = c1->path; /* == c2->path */
1032 output(o, 1, "CONFLICT (rename/rename): "
1033 "Rename %s->%s in %s. "
1034 "Rename %s->%s in %s",
1035 a->path, c1->path, ci->branch1,
1036 b->path, c2->path, ci->branch2);
1038 remove_file(o, 1, a->path, would_lose_untracked(a->path));
1039 remove_file(o, 1, b->path, would_lose_untracked(b->path));
1041 if (o->call_depth) {
1042 struct merge_file_info mfi;
1043 mfi = merge_file(o, path, null_sha1, 0,
1044 c1->sha1, c1->mode,
1045 c2->sha1, c2->mode,
1046 ci->branch1, ci->branch2);
1047 output(o, 1, "Adding merged %s", path);
1048 update_file(o, 0, mfi.sha, mfi.mode, path);
1049 } else {
1050 char *new_path1 = unique_path(o, path, ci->branch1);
1051 char *new_path2 = unique_path(o, path, ci->branch2);
1052 output(o, 1, "Renaming %s to %s and %s to %s instead",
1053 a->path, new_path1, b->path, new_path2);
1054 remove_file(o, 0, path, 0);
1055 update_file(o, 0, c1->sha1, c1->mode, new_path1);
1056 update_file(o, 0, c2->sha1, c2->mode, new_path2);
1057 free(new_path2);
1058 free(new_path1);
1059 }
1060 }
1062 static int process_renames(struct merge_options *o,
1063 struct string_list *a_renames,
1064 struct string_list *b_renames)
1065 {
1066 int clean_merge = 1, i, j;
1067 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
1068 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
1069 const struct rename *sre;
1071 for (i = 0; i < a_renames->nr; i++) {
1072 sre = a_renames->items[i].util;
1073 string_list_insert(&a_by_dst, sre->pair->two->path)->util
1074 = (void *)sre;
1075 }
1076 for (i = 0; i < b_renames->nr; i++) {
1077 sre = b_renames->items[i].util;
1078 string_list_insert(&b_by_dst, sre->pair->two->path)->util
1079 = (void *)sre;
1080 }
1082 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
1083 struct string_list *renames1, *renames2Dst;
1084 struct rename *ren1 = NULL, *ren2 = NULL;
1085 const char *branch1, *branch2;
1086 const char *ren1_src, *ren1_dst;
1087 struct string_list_item *lookup;
1089 if (i >= a_renames->nr) {
1090 ren2 = b_renames->items[j++].util;
1091 } else if (j >= b_renames->nr) {
1092 ren1 = a_renames->items[i++].util;
1093 } else {
1094 int compare = strcmp(a_renames->items[i].string,
1095 b_renames->items[j].string);
1096 if (compare <= 0)
1097 ren1 = a_renames->items[i++].util;
1098 if (compare >= 0)
1099 ren2 = b_renames->items[j++].util;
1100 }
1102 /* TODO: refactor, so that 1/2 are not needed */
1103 if (ren1) {
1104 renames1 = a_renames;
1105 renames2Dst = &b_by_dst;
1106 branch1 = o->branch1;
1107 branch2 = o->branch2;
1108 } else {
1109 struct rename *tmp;
1110 renames1 = b_renames;
1111 renames2Dst = &a_by_dst;
1112 branch1 = o->branch2;
1113 branch2 = o->branch1;
1114 tmp = ren2;
1115 ren2 = ren1;
1116 ren1 = tmp;
1117 }
1119 if (ren1->processed)
1120 continue;
1121 ren1->processed = 1;
1122 ren1->dst_entry->processed = 1;
1123 /* BUG: We should only mark src_entry as processed if we
1124 * are not dealing with a rename + add-source case.
1125 */
1126 ren1->src_entry->processed = 1;
1128 ren1_src = ren1->pair->one->path;
1129 ren1_dst = ren1->pair->two->path;
1131 if (ren2) {
1132 /* One file renamed on both sides */
1133 const char *ren2_src = ren2->pair->one->path;
1134 const char *ren2_dst = ren2->pair->two->path;
1135 enum rename_type rename_type;
1136 if (strcmp(ren1_src, ren2_src) != 0)
1137 die("ren1_src != ren2_src");
1138 ren2->dst_entry->processed = 1;
1139 ren2->processed = 1;
1140 if (strcmp(ren1_dst, ren2_dst) != 0) {
1141 rename_type = RENAME_ONE_FILE_TO_TWO;
1142 clean_merge = 0;
1143 } else {
1144 rename_type = RENAME_ONE_FILE_TO_ONE;
1145 /* BUG: We should only remove ren1_src in
1146 * the base stage (think of rename +
1147 * add-source cases).
1148 */
1149 remove_file(o, 1, ren1_src, 1);
1150 update_entry(ren1->dst_entry,
1151 ren1->pair->one,
1152 ren1->pair->two,
1153 ren2->pair->two);
1154 }
1155 setup_rename_conflict_info(rename_type,
1156 ren1->pair,
1157 ren2->pair,
1158 branch1,
1159 branch2,
1160 ren1->dst_entry,
1161 ren2->dst_entry);
1162 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
1163 /* Two different files renamed to the same thing */
1164 char *ren2_dst;
1165 ren2 = lookup->util;
1166 ren2_dst = ren2->pair->two->path;
1167 if (strcmp(ren1_dst, ren2_dst) != 0)
1168 die("ren1_dst != ren2_dst");
1170 clean_merge = 0;
1171 ren2->processed = 1;
1172 /*
1173 * BUG: We should only mark src_entry as processed
1174 * if we are not dealing with a rename + add-source
1175 * case.
1176 */
1177 ren2->src_entry->processed = 1;
1179 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
1180 ren1->pair,
1181 ren2->pair,
1182 branch1,
1183 branch2,
1184 ren1->dst_entry,
1185 ren2->dst_entry);
1186 } else {
1187 /* Renamed in 1, maybe changed in 2 */
1188 /* we only use sha1 and mode of these */
1189 struct diff_filespec src_other, dst_other;
1190 int try_merge;
1192 /*
1193 * unpack_trees loads entries from common-commit
1194 * into stage 1, from head-commit into stage 2, and
1195 * from merge-commit into stage 3. We keep track
1196 * of which side corresponds to the rename.
1197 */
1198 int renamed_stage = a_renames == renames1 ? 2 : 3;
1199 int other_stage = a_renames == renames1 ? 3 : 2;
1201 /* BUG: We should only remove ren1_src in the base
1202 * stage and in other_stage (think of rename +
1203 * add-source case).
1204 */
1205 remove_file(o, 1, ren1_src,
1206 renamed_stage == 2 || !was_tracked(ren1_src));
1208 hashcpy(src_other.sha1, ren1->src_entry->stages[other_stage].sha);
1209 src_other.mode = ren1->src_entry->stages[other_stage].mode;
1210 hashcpy(dst_other.sha1, ren1->dst_entry->stages[other_stage].sha);
1211 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
1212 try_merge = 0;
1214 if (sha_eq(src_other.sha1, null_sha1)) {
1215 setup_rename_conflict_info(RENAME_DELETE,
1216 ren1->pair,
1217 NULL,
1218 branch1,
1219 branch2,
1220 ren1->dst_entry,
1221 NULL);
1222 } else if ((dst_other.mode == ren1->pair->two->mode) &&
1223 sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
1224 /* Added file on the other side
1225 identical to the file being
1226 renamed: clean merge */
1227 update_file(o, 1, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
1228 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
1229 clean_merge = 0;
1230 try_merge = 1;
1231 output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
1232 "%s added in %s",
1233 ren1_src, ren1_dst, branch1,
1234 ren1_dst, branch2);
1235 if (o->call_depth) {
1236 struct merge_file_info mfi;
1237 mfi = merge_file(o, ren1_dst, null_sha1, 0,
1238 ren1->pair->two->sha1, ren1->pair->two->mode,
1239 dst_other.sha1, dst_other.mode,
1240 branch1, branch2);
1241 output(o, 1, "Adding merged %s", ren1_dst);
1242 update_file(o, 0, mfi.sha, mfi.mode, ren1_dst);
1243 try_merge = 0;
1244 } else {
1245 char *new_path = unique_path(o, ren1_dst, branch2);
1246 output(o, 1, "Adding as %s instead", new_path);
1247 update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
1248 free(new_path);
1249 }
1250 } else
1251 try_merge = 1;
1253 if (try_merge) {
1254 struct diff_filespec *one, *a, *b;
1255 src_other.path = (char *)ren1_src;
1257 one = ren1->pair->one;
1258 if (a_renames == renames1) {
1259 a = ren1->pair->two;
1260 b = &src_other;
1261 } else {
1262 b = ren1->pair->two;
1263 a = &src_other;
1264 }
1265 update_entry(ren1->dst_entry, one, a, b);
1266 setup_rename_conflict_info(RENAME_NORMAL,
1267 ren1->pair,
1268 NULL,
1269 branch1,
1270 NULL,
1271 ren1->dst_entry,
1272 NULL);
1273 }
1274 }
1275 }
1276 string_list_clear(&a_by_dst, 0);
1277 string_list_clear(&b_by_dst, 0);
1279 return clean_merge;
1280 }
1282 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1283 {
1284 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1285 }
1287 static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
1288 {
1289 void *buf;
1290 enum object_type type;
1291 unsigned long size;
1292 buf = read_sha1_file(sha1, &type, &size);
1293 if (!buf)
1294 return error("cannot read object %s", sha1_to_hex(sha1));
1295 if (type != OBJ_BLOB) {
1296 free(buf);
1297 return error("object %s is not a blob", sha1_to_hex(sha1));
1298 }
1299 strbuf_attach(dst, buf, size, size + 1);
1300 return 0;
1301 }
1303 static int blob_unchanged(const unsigned char *o_sha,
1304 const unsigned char *a_sha,
1305 int renormalize, const char *path)
1306 {
1307 struct strbuf o = STRBUF_INIT;
1308 struct strbuf a = STRBUF_INIT;
1309 int ret = 0; /* assume changed for safety */
1311 if (sha_eq(o_sha, a_sha))
1312 return 1;
1313 if (!renormalize)
1314 return 0;
1316 assert(o_sha && a_sha);
1317 if (read_sha1_strbuf(o_sha, &o) || read_sha1_strbuf(a_sha, &a))
1318 goto error_return;
1319 /*
1320 * Note: binary | is used so that both renormalizations are
1321 * performed. Comparison can be skipped if both files are
1322 * unchanged since their sha1s have already been compared.
1323 */
1324 if (renormalize_buffer(path, o.buf, o.len, &o) |
1325 renormalize_buffer(path, a.buf, o.len, &a))
1326 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1328 error_return:
1329 strbuf_release(&o);
1330 strbuf_release(&a);
1331 return ret;
1332 }
1334 static void handle_delete_modify(struct merge_options *o,
1335 const char *path,
1336 unsigned char *o_sha, int o_mode,
1337 unsigned char *a_sha, int a_mode,
1338 unsigned char *b_sha, int b_mode)
1339 {
1340 char *renamed = NULL;
1341 if (dir_in_way(path, !o->call_depth)) {
1342 renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
1343 }
1345 if (o->call_depth) {
1346 /*
1347 * We cannot arbitrarily accept either a_sha or b_sha as
1348 * correct; since there is no true "middle point" between
1349 * them, simply reuse the base version for virtual merge base.
1350 */
1351 remove_file_from_cache(path);
1352 update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
1353 } else if (!a_sha) {
1354 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1355 "and modified in %s. Version %s of %s left in tree%s%s.",
1356 path, o->branch1,
1357 o->branch2, o->branch2, path,
1358 NULL == renamed ? "" : " at ",
1359 NULL == renamed ? "" : renamed);
1360 update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
1361 } else {
1362 output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1363 "and modified in %s. Version %s of %s left in tree%s%s.",
1364 path, o->branch2,
1365 o->branch1, o->branch1, path,
1366 NULL == renamed ? "" : " at ",
1367 NULL == renamed ? "" : renamed);
1368 update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
1369 }
1370 free(renamed);
1372 }
1374 static int merge_content(struct merge_options *o,
1375 const char *path,
1376 unsigned char *o_sha, int o_mode,
1377 unsigned char *a_sha, int a_mode,
1378 unsigned char *b_sha, int b_mode,
1379 struct rename_conflict_info *rename_conflict_info)
1380 {
1381 const char *reason = "content";
1382 char *side1 = NULL, *side2 = NULL;
1383 const char *path1 = NULL, *path2 = NULL;
1384 struct merge_file_info mfi;
1385 struct diff_filespec one, a, b;
1386 unsigned df_conflict_remains = 0;
1388 if (!o_sha) {
1389 reason = "add/add";
1390 o_sha = (unsigned char *)null_sha1;
1391 }
1392 one.path = a.path = b.path = (char *)path;
1393 hashcpy(one.sha1, o_sha);
1394 one.mode = o_mode;
1395 hashcpy(a.sha1, a_sha);
1396 a.mode = a_mode;
1397 hashcpy(b.sha1, b_sha);
1398 b.mode = b_mode;
1400 if (rename_conflict_info) {
1401 struct diff_filepair *pair1 = rename_conflict_info->pair1;
1403 path1 = (o->branch1 == rename_conflict_info->branch1) ?
1404 pair1->two->path : pair1->one->path;
1405 /* If rename_conflict_info->pair2 != NULL, we are in
1406 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1407 * normal rename.
1408 */
1409 path2 = (rename_conflict_info->pair2 ||
1410 o->branch2 == rename_conflict_info->branch1) ?
1411 pair1->two->path : pair1->one->path;
1412 side1 = xmalloc(strlen(o->branch1) + strlen(path1) + 2);
1413 side2 = xmalloc(strlen(o->branch2) + strlen(path2) + 2);
1414 sprintf(side1, "%s:%s", o->branch1, path1);
1415 sprintf(side2, "%s:%s", o->branch2, path2);
1417 if (dir_in_way(path, !o->call_depth))
1418 df_conflict_remains = 1;
1419 }
1420 mfi = merge_file_1(o, &one, &a, &b,
1421 side1 ? side1 : o->branch1, side2 ? side2 : o->branch2);
1422 free(side1);
1423 free(side2);
1425 if (mfi.clean && !df_conflict_remains &&
1426 sha_eq(mfi.sha, a_sha) && mfi.mode == a_mode) {
1427 int path_renamed_outside_HEAD;
1428 output(o, 3, "Skipped %s (merged same as existing)", path);
1429 /*
1430 * The content merge resulted in the same file contents we
1431 * already had. We can return early if those file contents
1432 * are recorded at the correct path (which may not be true
1433 * if the merge involves a rename).
1434 */
1435 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
1436 if (!path_renamed_outside_HEAD) {
1437 add_cacheinfo(mfi.mode, mfi.sha, path,
1438 0 /*stage*/, 1 /*refresh*/, 0 /*options*/);
1439 return mfi.clean;
1440 }
1441 } else
1442 output(o, 2, "Auto-merging %s", path);
1444 if (!mfi.clean) {
1445 if (S_ISGITLINK(mfi.mode))
1446 reason = "submodule";
1447 output(o, 1, "CONFLICT (%s): Merge conflict in %s",
1448 reason, path);
1449 if (rename_conflict_info && !df_conflict_remains)
1450 update_stages(path, &one, &a, &b);
1451 }
1453 if (df_conflict_remains) {
1454 char *new_path;
1455 if (o->call_depth) {
1456 remove_file_from_cache(path);
1457 } else {
1458 if (!mfi.clean)
1459 update_stages(path, &one, &a, &b);
1460 else {
1461 int file_from_stage2 = was_tracked(path);
1462 struct diff_filespec merged;
1463 hashcpy(merged.sha1, mfi.sha);
1464 merged.mode = mfi.mode;
1466 update_stages(path, NULL,
1467 file_from_stage2 ? &merged : NULL,
1468 file_from_stage2 ? NULL : &merged);
1469 }
1471 }
1472 new_path = unique_path(o, path, rename_conflict_info->branch1);
1473 output(o, 1, "Adding as %s instead", new_path);
1474 update_file(o, 0, mfi.sha, mfi.mode, new_path);
1475 free(new_path);
1476 mfi.clean = 0;
1477 } else {
1478 update_file(o, mfi.clean, mfi.sha, mfi.mode, path);
1479 }
1480 return mfi.clean;
1482 }
1484 /* Per entry merge function */
1485 static int process_entry(struct merge_options *o,
1486 const char *path, struct stage_data *entry)
1487 {
1488 /*
1489 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1490 print_index_entry("\tpath: ", entry);
1491 */
1492 int clean_merge = 1;
1493 int normalize = o->renormalize;
1494 unsigned o_mode = entry->stages[1].mode;
1495 unsigned a_mode = entry->stages[2].mode;
1496 unsigned b_mode = entry->stages[3].mode;
1497 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1498 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1499 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1501 entry->processed = 1;
1502 if (entry->rename_conflict_info) {
1503 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
1504 switch (conflict_info->rename_type) {
1505 case RENAME_NORMAL:
1506 case RENAME_ONE_FILE_TO_ONE:
1507 clean_merge = merge_content(o, path,
1508 o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
1509 conflict_info);
1510 break;
1511 case RENAME_DELETE:
1512 clean_merge = 0;
1513 conflict_rename_delete(o, conflict_info->pair1,
1514 conflict_info->branch1,
1515 conflict_info->branch2);
1516 break;
1517 case RENAME_ONE_FILE_TO_TWO:
1518 clean_merge = 0;
1519 conflict_rename_rename_1to2(o, conflict_info);
1520 break;
1521 case RENAME_TWO_FILES_TO_ONE:
1522 clean_merge = 0;
1523 conflict_rename_rename_2to1(o, conflict_info);
1524 break;
1525 default:
1526 entry->processed = 0;
1527 break;
1528 }
1529 } else if (o_sha && (!a_sha || !b_sha)) {
1530 /* Case A: Deleted in one */
1531 if ((!a_sha && !b_sha) ||
1532 (!b_sha && blob_unchanged(o_sha, a_sha, normalize, path)) ||
1533 (!a_sha && blob_unchanged(o_sha, b_sha, normalize, path))) {
1534 /* Deleted in both or deleted in one and
1535 * unchanged in the other */
1536 if (a_sha)
1537 output(o, 2, "Removing %s", path);
1538 /* do not touch working file if it did not exist */
1539 remove_file(o, 1, path, !a_sha);
1540 } else {
1541 /* Modify/delete; deleted side may have put a directory in the way */
1542 clean_merge = 0;
1543 handle_delete_modify(o, path, o_sha, o_mode,
1544 a_sha, a_mode, b_sha, b_mode);
1545 }
1546 } else if ((!o_sha && a_sha && !b_sha) ||
1547 (!o_sha && !a_sha && b_sha)) {
1548 /* Case B: Added in one. */
1549 /* [nothing|directory] -> ([nothing|directory], file) */
1551 const char *add_branch;
1552 const char *other_branch;
1553 unsigned mode;
1554 const unsigned char *sha;
1555 const char *conf;
1557 if (a_sha) {
1558 add_branch = o->branch1;
1559 other_branch = o->branch2;
1560 mode = a_mode;
1561 sha = a_sha;
1562 conf = "file/directory";
1563 } else {
1564 add_branch = o->branch2;
1565 other_branch = o->branch1;
1566 mode = b_mode;
1567 sha = b_sha;
1568 conf = "directory/file";
1569 }
1570 if (dir_in_way(path, !o->call_depth)) {
1571 char *new_path = unique_path(o, path, add_branch);
1572 clean_merge = 0;
1573 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1574 "Adding %s as %s",
1575 conf, path, other_branch, path, new_path);
1576 if (o->call_depth)
1577 remove_file_from_cache(path);
1578 update_file(o, 0, sha, mode, new_path);
1579 if (o->call_depth)
1580 remove_file_from_cache(path);
1581 free(new_path);
1582 } else {
1583 output(o, 2, "Adding %s", path);
1584 update_file(o, 1, sha, mode, path);
1585 }
1586 } else if (a_sha && b_sha) {
1587 /* Case C: Added in both (check for same permissions) and */
1588 /* case D: Modified in both, but differently. */
1589 clean_merge = merge_content(o, path,
1590 o_sha, o_mode, a_sha, a_mode, b_sha, b_mode,
1591 NULL);
1592 } else if (!o_sha && !a_sha && !b_sha) {
1593 /*
1594 * this entry was deleted altogether. a_mode == 0 means
1595 * we had that path and want to actively remove it.
1596 */
1597 remove_file(o, 1, path, !a_mode);
1598 } else
1599 die("Fatal merge failure, shouldn't happen.");
1601 return clean_merge;
1602 }
1604 int merge_trees(struct merge_options *o,
1605 struct tree *head,
1606 struct tree *merge,
1607 struct tree *common,
1608 struct tree **result)
1609 {
1610 int code, clean;
1612 if (o->subtree_shift) {
1613 merge = shift_tree_object(head, merge, o->subtree_shift);
1614 common = shift_tree_object(head, common, o->subtree_shift);
1615 }
1617 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1618 output(o, 0, "Already up-to-date!");
1619 *result = head;
1620 return 1;
1621 }
1623 code = git_merge_trees(o->call_depth, common, head, merge);
1625 if (code != 0) {
1626 if (show(o, 4) || o->call_depth)
1627 die("merging of trees %s and %s failed",
1628 sha1_to_hex(head->object.sha1),
1629 sha1_to_hex(merge->object.sha1));
1630 else
1631 exit(128);
1632 }
1634 if (unmerged_cache()) {
1635 struct string_list *entries, *re_head, *re_merge;
1636 int i;
1637 string_list_clear(&o->current_file_set, 1);
1638 string_list_clear(&o->current_directory_set, 1);
1639 get_files_dirs(o, head);
1640 get_files_dirs(o, merge);
1642 entries = get_unmerged();
1643 record_df_conflict_files(o, entries);
1644 re_head = get_renames(o, head, common, head, merge, entries);
1645 re_merge = get_renames(o, merge, common, head, merge, entries);
1646 clean = process_renames(o, re_head, re_merge);
1647 for (i = entries->nr-1; 0 <= i; i--) {
1648 const char *path = entries->items[i].string;
1649 struct stage_data *e = entries->items[i].util;
1650 if (!e->processed
1651 && !process_entry(o, path, e))
1652 clean = 0;
1653 }
1654 for (i = 0; i < entries->nr; i++) {
1655 struct stage_data *e = entries->items[i].util;
1656 if (!e->processed)
1657 die("Unprocessed path??? %s",
1658 entries->items[i].string);
1659 }
1661 string_list_clear(re_merge, 0);
1662 string_list_clear(re_head, 0);
1663 string_list_clear(entries, 1);
1665 }
1666 else
1667 clean = 1;
1669 if (o->call_depth)
1670 *result = write_tree_from_memory(o);
1672 return clean;
1673 }
1675 static struct commit_list *reverse_commit_list(struct commit_list *list)
1676 {
1677 struct commit_list *next = NULL, *current, *backup;
1678 for (current = list; current; current = backup) {
1679 backup = current->next;
1680 current->next = next;
1681 next = current;
1682 }
1683 return next;
1684 }
1686 /*
1687 * Merge the commits h1 and h2, return the resulting virtual
1688 * commit object and a flag indicating the cleanness of the merge.
1689 */
1690 int merge_recursive(struct merge_options *o,
1691 struct commit *h1,
1692 struct commit *h2,
1693 struct commit_list *ca,
1694 struct commit **result)
1695 {
1696 struct commit_list *iter;
1697 struct commit *merged_common_ancestors;
1698 struct tree *mrtree = mrtree;
1699 int clean;
1701 if (show(o, 4)) {
1702 output(o, 4, "Merging:");
1703 output_commit_title(o, h1);
1704 output_commit_title(o, h2);
1705 }
1707 if (!ca) {
1708 ca = get_merge_bases(h1, h2, 1);
1709 ca = reverse_commit_list(ca);
1710 }
1712 if (show(o, 5)) {
1713 output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
1714 for (iter = ca; iter; iter = iter->next)
1715 output_commit_title(o, iter->item);
1716 }
1718 merged_common_ancestors = pop_commit(&ca);
1719 if (merged_common_ancestors == NULL) {
1720 /* if there is no common ancestor, make an empty tree */
1721 struct tree *tree = xcalloc(1, sizeof(struct tree));
1723 tree->object.parsed = 1;
1724 tree->object.type = OBJ_TREE;
1725 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1726 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1727 }
1729 for (iter = ca; iter; iter = iter->next) {
1730 const char *saved_b1, *saved_b2;
1731 o->call_depth++;
1732 /*
1733 * When the merge fails, the result contains files
1734 * with conflict markers. The cleanness flag is
1735 * ignored, it was never actually used, as result of
1736 * merge_trees has always overwritten it: the committed
1737 * "conflicts" were already resolved.
1738 */
1739 discard_cache();
1740 saved_b1 = o->branch1;
1741 saved_b2 = o->branch2;
1742 o->branch1 = "Temporary merge branch 1";
1743 o->branch2 = "Temporary merge branch 2";
1744 merge_recursive(o, merged_common_ancestors, iter->item,
1745 NULL, &merged_common_ancestors);
1746 o->branch1 = saved_b1;
1747 o->branch2 = saved_b2;
1748 o->call_depth--;
1750 if (!merged_common_ancestors)
1751 die("merge returned no commit");
1752 }
1754 discard_cache();
1755 if (!o->call_depth)
1756 read_cache();
1758 o->ancestor = "merged common ancestors";
1759 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
1760 &mrtree);
1762 if (o->call_depth) {
1763 *result = make_virtual_commit(mrtree, "merged tree");
1764 commit_list_insert(h1, &(*result)->parents);
1765 commit_list_insert(h2, &(*result)->parents->next);
1766 }
1767 flush_output(o);
1768 if (show(o, 2))
1769 diff_warn_rename_limit("merge.renamelimit",
1770 o->needed_rename_limit, 0);
1771 return clean;
1772 }
1774 static struct commit *get_ref(const unsigned char *sha1, const char *name)
1775 {
1776 struct object *object;
1778 object = deref_tag(parse_object(sha1), name, strlen(name));
1779 if (!object)
1780 return NULL;
1781 if (object->type == OBJ_TREE)
1782 return make_virtual_commit((struct tree*)object, name);
1783 if (object->type != OBJ_COMMIT)
1784 return NULL;
1785 if (parse_commit((struct commit *)object))
1786 return NULL;
1787 return (struct commit *)object;
1788 }
1790 int merge_recursive_generic(struct merge_options *o,
1791 const unsigned char *head,
1792 const unsigned char *merge,
1793 int num_base_list,
1794 const unsigned char **base_list,
1795 struct commit **result)
1796 {
1797 int clean, index_fd;
1798 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1799 struct commit *head_commit = get_ref(head, o->branch1);
1800 struct commit *next_commit = get_ref(merge, o->branch2);
1801 struct commit_list *ca = NULL;
1803 if (base_list) {
1804 int i;
1805 for (i = 0; i < num_base_list; ++i) {
1806 struct commit *base;
1807 if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
1808 return error("Could not parse object '%s'",
1809 sha1_to_hex(base_list[i]));
1810 commit_list_insert(base, &ca);
1811 }
1812 }
1814 index_fd = hold_locked_index(lock, 1);
1815 clean = merge_recursive(o, head_commit, next_commit, ca,
1816 result);
1817 if (active_cache_changed &&
1818 (write_cache(index_fd, active_cache, active_nr) ||
1819 commit_locked_index(lock)))
1820 return error("Unable to write index.");
1822 return clean ? 0 : 1;
1823 }
1825 static int merge_recursive_config(const char *var, const char *value, void *cb)
1826 {
1827 struct merge_options *o = cb;
1828 if (!strcmp(var, "merge.verbosity")) {
1829 o->verbosity = git_config_int(var, value);
1830 return 0;
1831 }
1832 if (!strcmp(var, "diff.renamelimit")) {
1833 o->diff_rename_limit = git_config_int(var, value);
1834 return 0;
1835 }
1836 if (!strcmp(var, "merge.renamelimit")) {
1837 o->merge_rename_limit = git_config_int(var, value);
1838 return 0;
1839 }
1840 return git_xmerge_config(var, value, cb);
1841 }
1843 void init_merge_options(struct merge_options *o)
1844 {
1845 memset(o, 0, sizeof(struct merge_options));
1846 o->verbosity = 2;
1847 o->buffer_output = 1;
1848 o->diff_rename_limit = -1;
1849 o->merge_rename_limit = -1;
1850 o->renormalize = 0;
1851 git_config(merge_recursive_config, o);
1852 if (getenv("GIT_MERGE_VERBOSITY"))
1853 o->verbosity =
1854 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1855 if (o->verbosity >= 5)
1856 o->buffer_output = 0;
1857 strbuf_init(&o->obuf, 0);
1858 memset(&o->current_file_set, 0, sizeof(struct string_list));
1859 o->current_file_set.strdup_strings = 1;
1860 memset(&o->current_directory_set, 0, sizeof(struct string_list));
1861 o->current_directory_set.strdup_strings = 1;
1862 memset(&o->df_conflict_file_set, 0, sizeof(struct string_list));
1863 o->df_conflict_file_set.strdup_strings = 1;
1864 }
1866 int parse_merge_opt(struct merge_options *o, const char *s)
1867 {
1868 if (!s || !*s)
1869 return -1;
1870 if (!strcmp(s, "ours"))
1871 o->recursive_variant = MERGE_RECURSIVE_OURS;
1872 else if (!strcmp(s, "theirs"))
1873 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
1874 else if (!strcmp(s, "subtree"))
1875 o->subtree_shift = "";
1876 else if (!prefixcmp(s, "subtree="))
1877 o->subtree_shift = s + strlen("subtree=");
1878 else if (!strcmp(s, "patience"))
1879 o->xdl_opts |= XDF_PATIENCE_DIFF;
1880 else if (!strcmp(s, "ignore-space-change"))
1881 o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1882 else if (!strcmp(s, "ignore-all-space"))
1883 o->xdl_opts |= XDF_IGNORE_WHITESPACE;
1884 else if (!strcmp(s, "ignore-space-at-eol"))
1885 o->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
1886 else if (!strcmp(s, "renormalize"))
1887 o->renormalize = 1;
1888 else if (!strcmp(s, "no-renormalize"))
1889 o->renormalize = 0;
1890 else if (!prefixcmp(s, "rename-threshold=")) {
1891 const char *score = s + strlen("rename-threshold=");
1892 if ((o->rename_score = parse_rename_score(&score)) == -1 || *score != 0)
1893 return -1;
1894 }
1895 else
1896 return -1;
1897 return 0;
1898 }