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 "config.h"
8#include "advice.h"
9#include "lockfile.h"
10#include "cache-tree.h"
11#include "commit.h"
12#include "blob.h"
13#include "builtin.h"
14#include "tree-walk.h"
15#include "diff.h"
16#include "diffcore.h"
17#include "tag.h"
18#include "unpack-trees.h"
19#include "string-list.h"
20#include "xdiff-interface.h"
21#include "ll-merge.h"
22#include "attr.h"
23#include "merge-recursive.h"
24#include "dir.h"
25#include "submodule.h"
26
27struct path_hashmap_entry {
28 struct hashmap_entry e;
29 char path[FLEX_ARRAY];
30};
31
32static int path_hashmap_cmp(const void *cmp_data,
33 const void *entry,
34 const void *entry_or_key,
35 const void *keydata)
36{
37 const struct path_hashmap_entry *a = entry;
38 const struct path_hashmap_entry *b = entry_or_key;
39 const char *key = keydata;
40
41 if (ignore_case)
42 return strcasecmp(a->path, key ? key : b->path);
43 else
44 return strcmp(a->path, key ? key : b->path);
45}
46
47static unsigned int path_hash(const char *path)
48{
49 return ignore_case ? strihash(path) : strhash(path);
50}
51
52static void flush_output(struct merge_options *o)
53{
54 if (o->buffer_output < 2 && o->obuf.len) {
55 fputs(o->obuf.buf, stdout);
56 strbuf_reset(&o->obuf);
57 }
58}
59
60static int err(struct merge_options *o, const char *err, ...)
61{
62 va_list params;
63
64 if (o->buffer_output < 2)
65 flush_output(o);
66 else {
67 strbuf_complete(&o->obuf, '\n');
68 strbuf_addstr(&o->obuf, "error: ");
69 }
70 va_start(params, err);
71 strbuf_vaddf(&o->obuf, err, params);
72 va_end(params);
73 if (o->buffer_output > 1)
74 strbuf_addch(&o->obuf, '\n');
75 else {
76 error("%s", o->obuf.buf);
77 strbuf_reset(&o->obuf);
78 }
79
80 return -1;
81}
82
83static struct tree *shift_tree_object(struct tree *one, struct tree *two,
84 const char *subtree_shift)
85{
86 struct object_id shifted;
87
88 if (!*subtree_shift) {
89 shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
90 } else {
91 shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
92 subtree_shift);
93 }
94 if (!oidcmp(&two->object.oid, &shifted))
95 return two;
96 return lookup_tree(&shifted);
97}
98
99static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
100{
101 struct commit *commit = alloc_commit_node();
102
103 set_merge_remote_desc(commit, comment, (struct object *)commit);
104 commit->tree = tree;
105 commit->object.parsed = 1;
106 return commit;
107}
108
109/*
110 * Since we use get_tree_entry(), which does not put the read object into
111 * the object pool, we cannot rely on a == b.
112 */
113static int oid_eq(const struct object_id *a, const struct object_id *b)
114{
115 if (!a && !b)
116 return 2;
117 return a && b && oidcmp(a, b) == 0;
118}
119
120enum rename_type {
121 RENAME_NORMAL = 0,
122 RENAME_DELETE,
123 RENAME_ONE_FILE_TO_ONE,
124 RENAME_ONE_FILE_TO_TWO,
125 RENAME_TWO_FILES_TO_ONE
126};
127
128struct rename_conflict_info {
129 enum rename_type rename_type;
130 struct diff_filepair *pair1;
131 struct diff_filepair *pair2;
132 const char *branch1;
133 const char *branch2;
134 struct stage_data *dst_entry1;
135 struct stage_data *dst_entry2;
136 struct diff_filespec ren1_other;
137 struct diff_filespec ren2_other;
138};
139
140/*
141 * Since we want to write the index eventually, we cannot reuse the index
142 * for these (temporary) data.
143 */
144struct stage_data {
145 struct {
146 unsigned mode;
147 struct object_id oid;
148 } stages[4];
149 struct rename_conflict_info *rename_conflict_info;
150 unsigned processed:1;
151};
152
153static inline void setup_rename_conflict_info(enum rename_type rename_type,
154 struct diff_filepair *pair1,
155 struct diff_filepair *pair2,
156 const char *branch1,
157 const char *branch2,
158 struct stage_data *dst_entry1,
159 struct stage_data *dst_entry2,
160 struct merge_options *o,
161 struct stage_data *src_entry1,
162 struct stage_data *src_entry2)
163{
164 struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
165 ci->rename_type = rename_type;
166 ci->pair1 = pair1;
167 ci->branch1 = branch1;
168 ci->branch2 = branch2;
169
170 ci->dst_entry1 = dst_entry1;
171 dst_entry1->rename_conflict_info = ci;
172 dst_entry1->processed = 0;
173
174 assert(!pair2 == !dst_entry2);
175 if (dst_entry2) {
176 ci->dst_entry2 = dst_entry2;
177 ci->pair2 = pair2;
178 dst_entry2->rename_conflict_info = ci;
179 }
180
181 if (rename_type == RENAME_TWO_FILES_TO_ONE) {
182 /*
183 * For each rename, there could have been
184 * modifications on the side of history where that
185 * file was not renamed.
186 */
187 int ostage1 = o->branch1 == branch1 ? 3 : 2;
188 int ostage2 = ostage1 ^ 1;
189
190 ci->ren1_other.path = pair1->one->path;
191 oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
192 ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
193
194 ci->ren2_other.path = pair2->one->path;
195 oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
196 ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
197 }
198}
199
200static int show(struct merge_options *o, int v)
201{
202 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
203}
204
205__attribute__((format (printf, 3, 4)))
206static void output(struct merge_options *o, int v, const char *fmt, ...)
207{
208 va_list ap;
209
210 if (!show(o, v))
211 return;
212
213 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
214
215 va_start(ap, fmt);
216 strbuf_vaddf(&o->obuf, fmt, ap);
217 va_end(ap);
218
219 strbuf_addch(&o->obuf, '\n');
220 if (!o->buffer_output)
221 flush_output(o);
222}
223
224static void output_commit_title(struct merge_options *o, struct commit *commit)
225{
226 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
227 if (commit->util)
228 strbuf_addf(&o->obuf, "virtual %s\n",
229 merge_remote_util(commit)->name);
230 else {
231 strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash,
232 DEFAULT_ABBREV);
233 strbuf_addch(&o->obuf, ' ');
234 if (parse_commit(commit) != 0)
235 strbuf_addstr(&o->obuf, _("(bad commit)\n"));
236 else {
237 const char *title;
238 const char *msg = get_commit_buffer(commit, NULL);
239 int len = find_commit_subject(msg, &title);
240 if (len)
241 strbuf_addf(&o->obuf, "%.*s\n", len, title);
242 unuse_commit_buffer(commit, msg);
243 }
244 }
245 flush_output(o);
246}
247
248static int add_cacheinfo(struct merge_options *o,
249 unsigned int mode, const struct object_id *oid,
250 const char *path, int stage, int refresh, int options)
251{
252 struct cache_entry *ce;
253 int ret;
254
255 ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
256 if (!ce)
257 return err(o, _("addinfo_cache failed for path '%s'"), path);
258
259 ret = add_cache_entry(ce, options);
260 if (refresh) {
261 struct cache_entry *nce;
262
263 nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
264 if (!nce)
265 return err(o, _("addinfo_cache failed for path '%s'"), path);
266 if (nce != ce)
267 ret = add_cache_entry(nce, options);
268 }
269 return ret;
270}
271
272static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
273{
274 parse_tree(tree);
275 init_tree_desc(desc, tree->buffer, tree->size);
276}
277
278static int git_merge_trees(int index_only,
279 struct tree *common,
280 struct tree *head,
281 struct tree *merge)
282{
283 int rc;
284 struct tree_desc t[3];
285 struct unpack_trees_options opts;
286
287 memset(&opts, 0, sizeof(opts));
288 if (index_only)
289 opts.index_only = 1;
290 else
291 opts.update = 1;
292 opts.merge = 1;
293 opts.head_idx = 2;
294 opts.fn = threeway_merge;
295 opts.src_index = &the_index;
296 opts.dst_index = &the_index;
297 setup_unpack_trees_porcelain(&opts, "merge");
298
299 init_tree_desc_from_tree(t+0, common);
300 init_tree_desc_from_tree(t+1, head);
301 init_tree_desc_from_tree(t+2, merge);
302
303 rc = unpack_trees(3, t, &opts);
304 cache_tree_free(&active_cache_tree);
305 return rc;
306}
307
308struct tree *write_tree_from_memory(struct merge_options *o)
309{
310 struct tree *result = NULL;
311
312 if (unmerged_cache()) {
313 int i;
314 fprintf(stderr, "BUG: There are unmerged index entries:\n");
315 for (i = 0; i < active_nr; i++) {
316 const struct cache_entry *ce = active_cache[i];
317 if (ce_stage(ce))
318 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
319 (int)ce_namelen(ce), ce->name);
320 }
321 die("BUG: unmerged index entries in merge-recursive.c");
322 }
323
324 if (!active_cache_tree)
325 active_cache_tree = cache_tree();
326
327 if (!cache_tree_fully_valid(active_cache_tree) &&
328 cache_tree_update(&the_index, 0) < 0) {
329 err(o, _("error building trees"));
330 return NULL;
331 }
332
333 result = lookup_tree(&active_cache_tree->oid);
334
335 return result;
336}
337
338static int save_files_dirs(const unsigned char *sha1,
339 struct strbuf *base, const char *path,
340 unsigned int mode, int stage, void *context)
341{
342 struct path_hashmap_entry *entry;
343 int baselen = base->len;
344 struct merge_options *o = context;
345
346 strbuf_addstr(base, path);
347
348 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
349 hashmap_entry_init(entry, path_hash(entry->path));
350 hashmap_add(&o->current_file_dir_set, entry);
351
352 strbuf_setlen(base, baselen);
353 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
354}
355
356static void get_files_dirs(struct merge_options *o, struct tree *tree)
357{
358 struct pathspec match_all;
359 memset(&match_all, 0, sizeof(match_all));
360 read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o);
361}
362
363/*
364 * Returns an index_entry instance which doesn't have to correspond to
365 * a real cache entry in Git's index.
366 */
367static struct stage_data *insert_stage_data(const char *path,
368 struct tree *o, struct tree *a, struct tree *b,
369 struct string_list *entries)
370{
371 struct string_list_item *item;
372 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
373 get_tree_entry(o->object.oid.hash, path,
374 e->stages[1].oid.hash, &e->stages[1].mode);
375 get_tree_entry(a->object.oid.hash, path,
376 e->stages[2].oid.hash, &e->stages[2].mode);
377 get_tree_entry(b->object.oid.hash, path,
378 e->stages[3].oid.hash, &e->stages[3].mode);
379 item = string_list_insert(entries, path);
380 item->util = e;
381 return e;
382}
383
384/*
385 * Create a dictionary mapping file names to stage_data objects. The
386 * dictionary contains one entry for every path with a non-zero stage entry.
387 */
388static struct string_list *get_unmerged(void)
389{
390 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
391 int i;
392
393 unmerged->strdup_strings = 1;
394
395 for (i = 0; i < active_nr; i++) {
396 struct string_list_item *item;
397 struct stage_data *e;
398 const struct cache_entry *ce = active_cache[i];
399 if (!ce_stage(ce))
400 continue;
401
402 item = string_list_lookup(unmerged, ce->name);
403 if (!item) {
404 item = string_list_insert(unmerged, ce->name);
405 item->util = xcalloc(1, sizeof(struct stage_data));
406 }
407 e = item->util;
408 e->stages[ce_stage(ce)].mode = ce->ce_mode;
409 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
410 }
411
412 return unmerged;
413}
414
415static int string_list_df_name_compare(const char *one, const char *two)
416{
417 int onelen = strlen(one);
418 int twolen = strlen(two);
419 /*
420 * Here we only care that entries for D/F conflicts are
421 * adjacent, in particular with the file of the D/F conflict
422 * appearing before files below the corresponding directory.
423 * The order of the rest of the list is irrelevant for us.
424 *
425 * To achieve this, we sort with df_name_compare and provide
426 * the mode S_IFDIR so that D/F conflicts will sort correctly.
427 * We use the mode S_IFDIR for everything else for simplicity,
428 * since in other cases any changes in their order due to
429 * sorting cause no problems for us.
430 */
431 int cmp = df_name_compare(one, onelen, S_IFDIR,
432 two, twolen, S_IFDIR);
433 /*
434 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
435 * that 'foo' comes before 'foo/bar'.
436 */
437 if (cmp)
438 return cmp;
439 return onelen - twolen;
440}
441
442static void record_df_conflict_files(struct merge_options *o,
443 struct string_list *entries)
444{
445 /* If there is a D/F conflict and the file for such a conflict
446 * currently exist in the working tree, we want to allow it to be
447 * removed to make room for the corresponding directory if needed.
448 * The files underneath the directories of such D/F conflicts will
449 * be processed before the corresponding file involved in the D/F
450 * conflict. If the D/F directory ends up being removed by the
451 * merge, then we won't have to touch the D/F file. If the D/F
452 * directory needs to be written to the working copy, then the D/F
453 * file will simply be removed (in make_room_for_path()) to make
454 * room for the necessary paths. Note that if both the directory
455 * and the file need to be present, then the D/F file will be
456 * reinstated with a new unique name at the time it is processed.
457 */
458 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
459 const char *last_file = NULL;
460 int last_len = 0;
461 int i;
462
463 /*
464 * If we're merging merge-bases, we don't want to bother with
465 * any working directory changes.
466 */
467 if (o->call_depth)
468 return;
469
470 /* Ensure D/F conflicts are adjacent in the entries list. */
471 for (i = 0; i < entries->nr; i++) {
472 struct string_list_item *next = &entries->items[i];
473 string_list_append(&df_sorted_entries, next->string)->util =
474 next->util;
475 }
476 df_sorted_entries.cmp = string_list_df_name_compare;
477 string_list_sort(&df_sorted_entries);
478
479 string_list_clear(&o->df_conflict_file_set, 1);
480 for (i = 0; i < df_sorted_entries.nr; i++) {
481 const char *path = df_sorted_entries.items[i].string;
482 int len = strlen(path);
483 struct stage_data *e = df_sorted_entries.items[i].util;
484
485 /*
486 * Check if last_file & path correspond to a D/F conflict;
487 * i.e. whether path is last_file+'/'+<something>.
488 * If so, record that it's okay to remove last_file to make
489 * room for path and friends if needed.
490 */
491 if (last_file &&
492 len > last_len &&
493 memcmp(path, last_file, last_len) == 0 &&
494 path[last_len] == '/') {
495 string_list_insert(&o->df_conflict_file_set, last_file);
496 }
497
498 /*
499 * Determine whether path could exist as a file in the
500 * working directory as a possible D/F conflict. This
501 * will only occur when it exists in stage 2 as a
502 * file.
503 */
504 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
505 last_file = path;
506 last_len = len;
507 } else {
508 last_file = NULL;
509 }
510 }
511 string_list_clear(&df_sorted_entries, 0);
512}
513
514struct rename {
515 struct diff_filepair *pair;
516 struct stage_data *src_entry;
517 struct stage_data *dst_entry;
518 unsigned processed:1;
519};
520
521/*
522 * Get information of all renames which occurred between 'o_tree' and
523 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
524 * 'b_tree') to be able to associate the correct cache entries with
525 * the rename information. 'tree' is always equal to either a_tree or b_tree.
526 */
527static struct string_list *get_renames(struct merge_options *o,
528 struct tree *tree,
529 struct tree *o_tree,
530 struct tree *a_tree,
531 struct tree *b_tree,
532 struct string_list *entries)
533{
534 int i;
535 struct string_list *renames;
536 struct diff_options opts;
537
538 renames = xcalloc(1, sizeof(struct string_list));
539 if (!o->detect_rename)
540 return renames;
541
542 diff_setup(&opts);
543 opts.flags.recursive = 1;
544 opts.flags.rename_empty = 0;
545 opts.detect_rename = DIFF_DETECT_RENAME;
546 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
547 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
548 1000;
549 opts.rename_score = o->rename_score;
550 opts.show_rename_progress = o->show_rename_progress;
551 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
552 diff_setup_done(&opts);
553 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
554 diffcore_std(&opts);
555 if (opts.needed_rename_limit > o->needed_rename_limit)
556 o->needed_rename_limit = opts.needed_rename_limit;
557 for (i = 0; i < diff_queued_diff.nr; ++i) {
558 struct string_list_item *item;
559 struct rename *re;
560 struct diff_filepair *pair = diff_queued_diff.queue[i];
561 if (pair->status != 'R') {
562 diff_free_filepair(pair);
563 continue;
564 }
565 re = xmalloc(sizeof(*re));
566 re->processed = 0;
567 re->pair = pair;
568 item = string_list_lookup(entries, re->pair->one->path);
569 if (!item)
570 re->src_entry = insert_stage_data(re->pair->one->path,
571 o_tree, a_tree, b_tree, entries);
572 else
573 re->src_entry = item->util;
574
575 item = string_list_lookup(entries, re->pair->two->path);
576 if (!item)
577 re->dst_entry = insert_stage_data(re->pair->two->path,
578 o_tree, a_tree, b_tree, entries);
579 else
580 re->dst_entry = item->util;
581 item = string_list_insert(renames, pair->one->path);
582 item->util = re;
583 }
584 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
585 diff_queued_diff.nr = 0;
586 diff_flush(&opts);
587 return renames;
588}
589
590static int update_stages(struct merge_options *opt, const char *path,
591 const struct diff_filespec *o,
592 const struct diff_filespec *a,
593 const struct diff_filespec *b)
594{
595
596 /*
597 * NOTE: It is usually a bad idea to call update_stages on a path
598 * before calling update_file on that same path, since it can
599 * sometimes lead to spurious "refusing to lose untracked file..."
600 * messages from update_file (via make_room_for path via
601 * would_lose_untracked). Instead, reverse the order of the calls
602 * (executing update_file first and then update_stages).
603 */
604 int clear = 1;
605 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
606 if (clear)
607 if (remove_file_from_cache(path))
608 return -1;
609 if (o)
610 if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
611 return -1;
612 if (a)
613 if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
614 return -1;
615 if (b)
616 if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
617 return -1;
618 return 0;
619}
620
621static void update_entry(struct stage_data *entry,
622 struct diff_filespec *o,
623 struct diff_filespec *a,
624 struct diff_filespec *b)
625{
626 entry->processed = 0;
627 entry->stages[1].mode = o->mode;
628 entry->stages[2].mode = a->mode;
629 entry->stages[3].mode = b->mode;
630 oidcpy(&entry->stages[1].oid, &o->oid);
631 oidcpy(&entry->stages[2].oid, &a->oid);
632 oidcpy(&entry->stages[3].oid, &b->oid);
633}
634
635static int remove_file(struct merge_options *o, int clean,
636 const char *path, int no_wd)
637{
638 int update_cache = o->call_depth || clean;
639 int update_working_directory = !o->call_depth && !no_wd;
640
641 if (update_cache) {
642 if (remove_file_from_cache(path))
643 return -1;
644 }
645 if (update_working_directory) {
646 if (ignore_case) {
647 struct cache_entry *ce;
648 ce = cache_file_exists(path, strlen(path), ignore_case);
649 if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
650 return 0;
651 }
652 if (remove_path(path))
653 return -1;
654 }
655 return 0;
656}
657
658/* add a string to a strbuf, but converting "/" to "_" */
659static void add_flattened_path(struct strbuf *out, const char *s)
660{
661 size_t i = out->len;
662 strbuf_addstr(out, s);
663 for (; i < out->len; i++)
664 if (out->buf[i] == '/')
665 out->buf[i] = '_';
666}
667
668static char *unique_path(struct merge_options *o, const char *path, const char *branch)
669{
670 struct path_hashmap_entry *entry;
671 struct strbuf newpath = STRBUF_INIT;
672 int suffix = 0;
673 size_t base_len;
674
675 strbuf_addf(&newpath, "%s~", path);
676 add_flattened_path(&newpath, branch);
677
678 base_len = newpath.len;
679 while (hashmap_get_from_hash(&o->current_file_dir_set,
680 path_hash(newpath.buf), newpath.buf) ||
681 (!o->call_depth && file_exists(newpath.buf))) {
682 strbuf_setlen(&newpath, base_len);
683 strbuf_addf(&newpath, "_%d", suffix++);
684 }
685
686 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
687 hashmap_entry_init(entry, path_hash(entry->path));
688 hashmap_add(&o->current_file_dir_set, entry);
689 return strbuf_detach(&newpath, NULL);
690}
691
692/**
693 * Check whether a directory in the index is in the way of an incoming
694 * file. Return 1 if so. If check_working_copy is non-zero, also
695 * check the working directory. If empty_ok is non-zero, also return
696 * 0 in the case where the working-tree dir exists but is empty.
697 */
698static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
699{
700 int pos;
701 struct strbuf dirpath = STRBUF_INIT;
702 struct stat st;
703
704 strbuf_addstr(&dirpath, path);
705 strbuf_addch(&dirpath, '/');
706
707 pos = cache_name_pos(dirpath.buf, dirpath.len);
708
709 if (pos < 0)
710 pos = -1 - pos;
711 if (pos < active_nr &&
712 !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
713 strbuf_release(&dirpath);
714 return 1;
715 }
716
717 strbuf_release(&dirpath);
718 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
719 !(empty_ok && is_empty_dir(path));
720}
721
722static int was_tracked(const char *path)
723{
724 int pos = cache_name_pos(path, strlen(path));
725
726 if (0 <= pos)
727 /* we have been tracking this path */
728 return 1;
729
730 /*
731 * Look for an unmerged entry for the path,
732 * specifically stage #2, which would indicate
733 * that "our" side before the merge started
734 * had the path tracked (and resulted in a conflict).
735 */
736 for (pos = -1 - pos;
737 pos < active_nr && !strcmp(path, active_cache[pos]->name);
738 pos++)
739 if (ce_stage(active_cache[pos]) == 2)
740 return 1;
741 return 0;
742}
743
744static int would_lose_untracked(const char *path)
745{
746 return !was_tracked(path) && file_exists(path);
747}
748
749static int make_room_for_path(struct merge_options *o, const char *path)
750{
751 int status, i;
752 const char *msg = _("failed to create path '%s'%s");
753
754 /* Unlink any D/F conflict files that are in the way */
755 for (i = 0; i < o->df_conflict_file_set.nr; i++) {
756 const char *df_path = o->df_conflict_file_set.items[i].string;
757 size_t pathlen = strlen(path);
758 size_t df_pathlen = strlen(df_path);
759 if (df_pathlen < pathlen &&
760 path[df_pathlen] == '/' &&
761 strncmp(path, df_path, df_pathlen) == 0) {
762 output(o, 3,
763 _("Removing %s to make room for subdirectory\n"),
764 df_path);
765 unlink(df_path);
766 unsorted_string_list_delete_item(&o->df_conflict_file_set,
767 i, 0);
768 break;
769 }
770 }
771
772 /* Make sure leading directories are created */
773 status = safe_create_leading_directories_const(path);
774 if (status) {
775 if (status == SCLD_EXISTS)
776 /* something else exists */
777 return err(o, msg, path, _(": perhaps a D/F conflict?"));
778 return err(o, msg, path, "");
779 }
780
781 /*
782 * Do not unlink a file in the work tree if we are not
783 * tracking it.
784 */
785 if (would_lose_untracked(path))
786 return err(o, _("refusing to lose untracked file at '%s'"),
787 path);
788
789 /* Successful unlink is good.. */
790 if (!unlink(path))
791 return 0;
792 /* .. and so is no existing file */
793 if (errno == ENOENT)
794 return 0;
795 /* .. but not some other error (who really cares what?) */
796 return err(o, msg, path, _(": perhaps a D/F conflict?"));
797}
798
799static int update_file_flags(struct merge_options *o,
800 const struct object_id *oid,
801 unsigned mode,
802 const char *path,
803 int update_cache,
804 int update_wd)
805{
806 int ret = 0;
807
808 if (o->call_depth)
809 update_wd = 0;
810
811 if (update_wd) {
812 enum object_type type;
813 void *buf;
814 unsigned long size;
815
816 if (S_ISGITLINK(mode)) {
817 /*
818 * We may later decide to recursively descend into
819 * the submodule directory and update its index
820 * and/or work tree, but we do not do that now.
821 */
822 update_wd = 0;
823 goto update_index;
824 }
825
826 buf = read_sha1_file(oid->hash, &type, &size);
827 if (!buf)
828 return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
829 if (type != OBJ_BLOB) {
830 ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
831 goto free_buf;
832 }
833 if (S_ISREG(mode)) {
834 struct strbuf strbuf = STRBUF_INIT;
835 if (convert_to_working_tree(path, buf, size, &strbuf)) {
836 free(buf);
837 size = strbuf.len;
838 buf = strbuf_detach(&strbuf, NULL);
839 }
840 }
841
842 if (make_room_for_path(o, path) < 0) {
843 update_wd = 0;
844 goto free_buf;
845 }
846 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
847 int fd;
848 if (mode & 0100)
849 mode = 0777;
850 else
851 mode = 0666;
852 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
853 if (fd < 0) {
854 ret = err(o, _("failed to open '%s': %s"),
855 path, strerror(errno));
856 goto free_buf;
857 }
858 write_in_full(fd, buf, size);
859 close(fd);
860 } else if (S_ISLNK(mode)) {
861 char *lnk = xmemdupz(buf, size);
862 safe_create_leading_directories_const(path);
863 unlink(path);
864 if (symlink(lnk, path))
865 ret = err(o, _("failed to symlink '%s': %s"),
866 path, strerror(errno));
867 free(lnk);
868 } else
869 ret = err(o,
870 _("do not know what to do with %06o %s '%s'"),
871 mode, oid_to_hex(oid), path);
872 free_buf:
873 free(buf);
874 }
875 update_index:
876 if (!ret && update_cache)
877 add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
878 return ret;
879}
880
881static int update_file(struct merge_options *o,
882 int clean,
883 const struct object_id *oid,
884 unsigned mode,
885 const char *path)
886{
887 return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
888}
889
890/* Low level file merging, update and removal */
891
892struct merge_file_info {
893 struct object_id oid;
894 unsigned mode;
895 unsigned clean:1,
896 merge:1;
897};
898
899static int merge_3way(struct merge_options *o,
900 mmbuffer_t *result_buf,
901 const struct diff_filespec *one,
902 const struct diff_filespec *a,
903 const struct diff_filespec *b,
904 const char *branch1,
905 const char *branch2)
906{
907 mmfile_t orig, src1, src2;
908 struct ll_merge_options ll_opts = {0};
909 char *base_name, *name1, *name2;
910 int merge_status;
911
912 ll_opts.renormalize = o->renormalize;
913 ll_opts.xdl_opts = o->xdl_opts;
914
915 if (o->call_depth) {
916 ll_opts.virtual_ancestor = 1;
917 ll_opts.variant = 0;
918 } else {
919 switch (o->recursive_variant) {
920 case MERGE_RECURSIVE_OURS:
921 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
922 break;
923 case MERGE_RECURSIVE_THEIRS:
924 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
925 break;
926 default:
927 ll_opts.variant = 0;
928 break;
929 }
930 }
931
932 if (strcmp(a->path, b->path) ||
933 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
934 base_name = o->ancestor == NULL ? NULL :
935 mkpathdup("%s:%s", o->ancestor, one->path);
936 name1 = mkpathdup("%s:%s", branch1, a->path);
937 name2 = mkpathdup("%s:%s", branch2, b->path);
938 } else {
939 base_name = o->ancestor == NULL ? NULL :
940 mkpathdup("%s", o->ancestor);
941 name1 = mkpathdup("%s", branch1);
942 name2 = mkpathdup("%s", branch2);
943 }
944
945 read_mmblob(&orig, &one->oid);
946 read_mmblob(&src1, &a->oid);
947 read_mmblob(&src2, &b->oid);
948
949 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
950 &src1, name1, &src2, name2, &ll_opts);
951
952 free(base_name);
953 free(name1);
954 free(name2);
955 free(orig.ptr);
956 free(src1.ptr);
957 free(src2.ptr);
958 return merge_status;
959}
960
961static int merge_file_1(struct merge_options *o,
962 const struct diff_filespec *one,
963 const struct diff_filespec *a,
964 const struct diff_filespec *b,
965 const char *branch1,
966 const char *branch2,
967 struct merge_file_info *result)
968{
969 result->merge = 0;
970 result->clean = 1;
971
972 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
973 result->clean = 0;
974 if (S_ISREG(a->mode)) {
975 result->mode = a->mode;
976 oidcpy(&result->oid, &a->oid);
977 } else {
978 result->mode = b->mode;
979 oidcpy(&result->oid, &b->oid);
980 }
981 } else {
982 if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
983 result->merge = 1;
984
985 /*
986 * Merge modes
987 */
988 if (a->mode == b->mode || a->mode == one->mode)
989 result->mode = b->mode;
990 else {
991 result->mode = a->mode;
992 if (b->mode != one->mode) {
993 result->clean = 0;
994 result->merge = 1;
995 }
996 }
997
998 if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
999 oidcpy(&result->oid, &b->oid);
1000 else if (oid_eq(&b->oid, &one->oid))
1001 oidcpy(&result->oid, &a->oid);
1002 else if (S_ISREG(a->mode)) {
1003 mmbuffer_t result_buf;
1004 int ret = 0, merge_status;
1005
1006 merge_status = merge_3way(o, &result_buf, one, a, b,
1007 branch1, branch2);
1008
1009 if ((merge_status < 0) || !result_buf.ptr)
1010 ret = err(o, _("Failed to execute internal merge"));
1011
1012 if (!ret &&
1013 write_object_file(result_buf.ptr, result_buf.size,
1014 blob_type, &result->oid))
1015 ret = err(o, _("Unable to add %s to database"),
1016 a->path);
1017
1018 free(result_buf.ptr);
1019 if (ret)
1020 return ret;
1021 result->clean = (merge_status == 0);
1022 } else if (S_ISGITLINK(a->mode)) {
1023 result->clean = merge_submodule(&result->oid,
1024 one->path,
1025 &one->oid,
1026 &a->oid,
1027 &b->oid,
1028 !o->call_depth);
1029 } else if (S_ISLNK(a->mode)) {
1030 switch (o->recursive_variant) {
1031 case MERGE_RECURSIVE_NORMAL:
1032 oidcpy(&result->oid, &a->oid);
1033 if (!oid_eq(&a->oid, &b->oid))
1034 result->clean = 0;
1035 break;
1036 case MERGE_RECURSIVE_OURS:
1037 oidcpy(&result->oid, &a->oid);
1038 break;
1039 case MERGE_RECURSIVE_THEIRS:
1040 oidcpy(&result->oid, &b->oid);
1041 break;
1042 }
1043 } else
1044 die("BUG: unsupported object type in the tree");
1045 }
1046
1047 return 0;
1048}
1049
1050static int merge_file_special_markers(struct merge_options *o,
1051 const struct diff_filespec *one,
1052 const struct diff_filespec *a,
1053 const struct diff_filespec *b,
1054 const char *branch1,
1055 const char *filename1,
1056 const char *branch2,
1057 const char *filename2,
1058 struct merge_file_info *mfi)
1059{
1060 char *side1 = NULL;
1061 char *side2 = NULL;
1062 int ret;
1063
1064 if (filename1)
1065 side1 = xstrfmt("%s:%s", branch1, filename1);
1066 if (filename2)
1067 side2 = xstrfmt("%s:%s", branch2, filename2);
1068
1069 ret = merge_file_1(o, one, a, b,
1070 side1 ? side1 : branch1,
1071 side2 ? side2 : branch2, mfi);
1072 free(side1);
1073 free(side2);
1074 return ret;
1075}
1076
1077static int merge_file_one(struct merge_options *o,
1078 const char *path,
1079 const struct object_id *o_oid, int o_mode,
1080 const struct object_id *a_oid, int a_mode,
1081 const struct object_id *b_oid, int b_mode,
1082 const char *branch1,
1083 const char *branch2,
1084 struct merge_file_info *mfi)
1085{
1086 struct diff_filespec one, a, b;
1087
1088 one.path = a.path = b.path = (char *)path;
1089 oidcpy(&one.oid, o_oid);
1090 one.mode = o_mode;
1091 oidcpy(&a.oid, a_oid);
1092 a.mode = a_mode;
1093 oidcpy(&b.oid, b_oid);
1094 b.mode = b_mode;
1095 return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
1096}
1097
1098static int handle_change_delete(struct merge_options *o,
1099 const char *path, const char *old_path,
1100 const struct object_id *o_oid, int o_mode,
1101 const struct object_id *changed_oid,
1102 int changed_mode,
1103 const char *change_branch,
1104 const char *delete_branch,
1105 const char *change, const char *change_past)
1106{
1107 char *alt_path = NULL;
1108 const char *update_path = path;
1109 int ret = 0;
1110
1111 if (dir_in_way(path, !o->call_depth, 0)) {
1112 update_path = alt_path = unique_path(o, path, change_branch);
1113 }
1114
1115 if (o->call_depth) {
1116 /*
1117 * We cannot arbitrarily accept either a_sha or b_sha as
1118 * correct; since there is no true "middle point" between
1119 * them, simply reuse the base version for virtual merge base.
1120 */
1121 ret = remove_file_from_cache(path);
1122 if (!ret)
1123 ret = update_file(o, 0, o_oid, o_mode, update_path);
1124 } else {
1125 if (!alt_path) {
1126 if (!old_path) {
1127 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1128 "and %s in %s. Version %s of %s left in tree."),
1129 change, path, delete_branch, change_past,
1130 change_branch, change_branch, path);
1131 } else {
1132 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1133 "and %s to %s in %s. Version %s of %s left in tree."),
1134 change, old_path, delete_branch, change_past, path,
1135 change_branch, change_branch, path);
1136 }
1137 } else {
1138 if (!old_path) {
1139 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1140 "and %s in %s. Version %s of %s left in tree at %s."),
1141 change, path, delete_branch, change_past,
1142 change_branch, change_branch, path, alt_path);
1143 } else {
1144 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1145 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1146 change, old_path, delete_branch, change_past, path,
1147 change_branch, change_branch, path, alt_path);
1148 }
1149 }
1150 /*
1151 * No need to call update_file() on path when change_branch ==
1152 * o->branch1 && !alt_path, since that would needlessly touch
1153 * path. We could call update_file_flags() with update_cache=0
1154 * and update_wd=0, but that's a no-op.
1155 */
1156 if (change_branch != o->branch1 || alt_path)
1157 ret = update_file(o, 0, changed_oid, changed_mode, update_path);
1158 }
1159 free(alt_path);
1160
1161 return ret;
1162}
1163
1164static int conflict_rename_delete(struct merge_options *o,
1165 struct diff_filepair *pair,
1166 const char *rename_branch,
1167 const char *delete_branch)
1168{
1169 const struct diff_filespec *orig = pair->one;
1170 const struct diff_filespec *dest = pair->two;
1171
1172 if (handle_change_delete(o,
1173 o->call_depth ? orig->path : dest->path,
1174 o->call_depth ? NULL : orig->path,
1175 &orig->oid, orig->mode,
1176 &dest->oid, dest->mode,
1177 rename_branch, delete_branch,
1178 _("rename"), _("renamed")))
1179 return -1;
1180
1181 if (o->call_depth)
1182 return remove_file_from_cache(dest->path);
1183 else
1184 return update_stages(o, dest->path, NULL,
1185 rename_branch == o->branch1 ? dest : NULL,
1186 rename_branch == o->branch1 ? NULL : dest);
1187}
1188
1189static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
1190 struct stage_data *entry,
1191 int stage)
1192{
1193 struct object_id *oid = &entry->stages[stage].oid;
1194 unsigned mode = entry->stages[stage].mode;
1195 if (mode == 0 || is_null_oid(oid))
1196 return NULL;
1197 oidcpy(&target->oid, oid);
1198 target->mode = mode;
1199 return target;
1200}
1201
1202static int handle_file(struct merge_options *o,
1203 struct diff_filespec *rename,
1204 int stage,
1205 struct rename_conflict_info *ci)
1206{
1207 char *dst_name = rename->path;
1208 struct stage_data *dst_entry;
1209 const char *cur_branch, *other_branch;
1210 struct diff_filespec other;
1211 struct diff_filespec *add;
1212 int ret;
1213
1214 if (stage == 2) {
1215 dst_entry = ci->dst_entry1;
1216 cur_branch = ci->branch1;
1217 other_branch = ci->branch2;
1218 } else {
1219 dst_entry = ci->dst_entry2;
1220 cur_branch = ci->branch2;
1221 other_branch = ci->branch1;
1222 }
1223
1224 add = filespec_from_entry(&other, dst_entry, stage ^ 1);
1225 if (add) {
1226 char *add_name = unique_path(o, rename->path, other_branch);
1227 if (update_file(o, 0, &add->oid, add->mode, add_name))
1228 return -1;
1229
1230 remove_file(o, 0, rename->path, 0);
1231 dst_name = unique_path(o, rename->path, cur_branch);
1232 } else {
1233 if (dir_in_way(rename->path, !o->call_depth, 0)) {
1234 dst_name = unique_path(o, rename->path, cur_branch);
1235 output(o, 1, _("%s is a directory in %s adding as %s instead"),
1236 rename->path, other_branch, dst_name);
1237 }
1238 }
1239 if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
1240 ; /* fall through, do allow dst_name to be released */
1241 else if (stage == 2)
1242 ret = update_stages(o, rename->path, NULL, rename, add);
1243 else
1244 ret = update_stages(o, rename->path, NULL, add, rename);
1245
1246 if (dst_name != rename->path)
1247 free(dst_name);
1248
1249 return ret;
1250}
1251
1252static int conflict_rename_rename_1to2(struct merge_options *o,
1253 struct rename_conflict_info *ci)
1254{
1255 /* One file was renamed in both branches, but to different names. */
1256 struct diff_filespec *one = ci->pair1->one;
1257 struct diff_filespec *a = ci->pair1->two;
1258 struct diff_filespec *b = ci->pair2->two;
1259
1260 output(o, 1, _("CONFLICT (rename/rename): "
1261 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1262 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1263 one->path, a->path, ci->branch1,
1264 one->path, b->path, ci->branch2,
1265 o->call_depth ? _(" (left unresolved)") : "");
1266 if (o->call_depth) {
1267 struct merge_file_info mfi;
1268 struct diff_filespec other;
1269 struct diff_filespec *add;
1270 if (merge_file_one(o, one->path,
1271 &one->oid, one->mode,
1272 &a->oid, a->mode,
1273 &b->oid, b->mode,
1274 ci->branch1, ci->branch2, &mfi))
1275 return -1;
1276
1277 /*
1278 * FIXME: For rename/add-source conflicts (if we could detect
1279 * such), this is wrong. We should instead find a unique
1280 * pathname and then either rename the add-source file to that
1281 * unique path, or use that unique path instead of src here.
1282 */
1283 if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
1284 return -1;
1285
1286 /*
1287 * Above, we put the merged content at the merge-base's
1288 * path. Now we usually need to delete both a->path and
1289 * b->path. However, the rename on each side of the merge
1290 * could also be involved in a rename/add conflict. In
1291 * such cases, we should keep the added file around,
1292 * resolving the conflict at that path in its favor.
1293 */
1294 add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
1295 if (add) {
1296 if (update_file(o, 0, &add->oid, add->mode, a->path))
1297 return -1;
1298 }
1299 else
1300 remove_file_from_cache(a->path);
1301 add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
1302 if (add) {
1303 if (update_file(o, 0, &add->oid, add->mode, b->path))
1304 return -1;
1305 }
1306 else
1307 remove_file_from_cache(b->path);
1308 } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
1309 return -1;
1310
1311 return 0;
1312}
1313
1314static int conflict_rename_rename_2to1(struct merge_options *o,
1315 struct rename_conflict_info *ci)
1316{
1317 /* Two files, a & b, were renamed to the same thing, c. */
1318 struct diff_filespec *a = ci->pair1->one;
1319 struct diff_filespec *b = ci->pair2->one;
1320 struct diff_filespec *c1 = ci->pair1->two;
1321 struct diff_filespec *c2 = ci->pair2->two;
1322 char *path = c1->path; /* == c2->path */
1323 struct merge_file_info mfi_c1;
1324 struct merge_file_info mfi_c2;
1325 int ret;
1326
1327 output(o, 1, _("CONFLICT (rename/rename): "
1328 "Rename %s->%s in %s. "
1329 "Rename %s->%s in %s"),
1330 a->path, c1->path, ci->branch1,
1331 b->path, c2->path, ci->branch2);
1332
1333 remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
1334 remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
1335
1336 if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
1337 o->branch1, c1->path,
1338 o->branch2, ci->ren1_other.path, &mfi_c1) ||
1339 merge_file_special_markers(o, b, &ci->ren2_other, c2,
1340 o->branch1, ci->ren2_other.path,
1341 o->branch2, c2->path, &mfi_c2))
1342 return -1;
1343
1344 if (o->call_depth) {
1345 /*
1346 * If mfi_c1.clean && mfi_c2.clean, then it might make
1347 * sense to do a two-way merge of those results. But, I
1348 * think in all cases, it makes sense to have the virtual
1349 * merge base just undo the renames; they can be detected
1350 * again later for the non-recursive merge.
1351 */
1352 remove_file(o, 0, path, 0);
1353 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
1354 if (!ret)
1355 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1356 b->path);
1357 } else {
1358 char *new_path1 = unique_path(o, path, ci->branch1);
1359 char *new_path2 = unique_path(o, path, ci->branch2);
1360 output(o, 1, _("Renaming %s to %s and %s to %s instead"),
1361 a->path, new_path1, b->path, new_path2);
1362 remove_file(o, 0, path, 0);
1363 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
1364 if (!ret)
1365 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1366 new_path2);
1367 free(new_path2);
1368 free(new_path1);
1369 }
1370
1371 return ret;
1372}
1373
1374static int process_renames(struct merge_options *o,
1375 struct string_list *a_renames,
1376 struct string_list *b_renames)
1377{
1378 int clean_merge = 1, i, j;
1379 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
1380 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
1381 const struct rename *sre;
1382
1383 for (i = 0; i < a_renames->nr; i++) {
1384 sre = a_renames->items[i].util;
1385 string_list_insert(&a_by_dst, sre->pair->two->path)->util
1386 = (void *)sre;
1387 }
1388 for (i = 0; i < b_renames->nr; i++) {
1389 sre = b_renames->items[i].util;
1390 string_list_insert(&b_by_dst, sre->pair->two->path)->util
1391 = (void *)sre;
1392 }
1393
1394 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
1395 struct string_list *renames1, *renames2Dst;
1396 struct rename *ren1 = NULL, *ren2 = NULL;
1397 const char *branch1, *branch2;
1398 const char *ren1_src, *ren1_dst;
1399 struct string_list_item *lookup;
1400
1401 if (i >= a_renames->nr) {
1402 ren2 = b_renames->items[j++].util;
1403 } else if (j >= b_renames->nr) {
1404 ren1 = a_renames->items[i++].util;
1405 } else {
1406 int compare = strcmp(a_renames->items[i].string,
1407 b_renames->items[j].string);
1408 if (compare <= 0)
1409 ren1 = a_renames->items[i++].util;
1410 if (compare >= 0)
1411 ren2 = b_renames->items[j++].util;
1412 }
1413
1414 /* TODO: refactor, so that 1/2 are not needed */
1415 if (ren1) {
1416 renames1 = a_renames;
1417 renames2Dst = &b_by_dst;
1418 branch1 = o->branch1;
1419 branch2 = o->branch2;
1420 } else {
1421 renames1 = b_renames;
1422 renames2Dst = &a_by_dst;
1423 branch1 = o->branch2;
1424 branch2 = o->branch1;
1425 SWAP(ren2, ren1);
1426 }
1427
1428 if (ren1->processed)
1429 continue;
1430 ren1->processed = 1;
1431 ren1->dst_entry->processed = 1;
1432 /* BUG: We should only mark src_entry as processed if we
1433 * are not dealing with a rename + add-source case.
1434 */
1435 ren1->src_entry->processed = 1;
1436
1437 ren1_src = ren1->pair->one->path;
1438 ren1_dst = ren1->pair->two->path;
1439
1440 if (ren2) {
1441 /* One file renamed on both sides */
1442 const char *ren2_src = ren2->pair->one->path;
1443 const char *ren2_dst = ren2->pair->two->path;
1444 enum rename_type rename_type;
1445 if (strcmp(ren1_src, ren2_src) != 0)
1446 die("BUG: ren1_src != ren2_src");
1447 ren2->dst_entry->processed = 1;
1448 ren2->processed = 1;
1449 if (strcmp(ren1_dst, ren2_dst) != 0) {
1450 rename_type = RENAME_ONE_FILE_TO_TWO;
1451 clean_merge = 0;
1452 } else {
1453 rename_type = RENAME_ONE_FILE_TO_ONE;
1454 /* BUG: We should only remove ren1_src in
1455 * the base stage (think of rename +
1456 * add-source cases).
1457 */
1458 remove_file(o, 1, ren1_src, 1);
1459 update_entry(ren1->dst_entry,
1460 ren1->pair->one,
1461 ren1->pair->two,
1462 ren2->pair->two);
1463 }
1464 setup_rename_conflict_info(rename_type,
1465 ren1->pair,
1466 ren2->pair,
1467 branch1,
1468 branch2,
1469 ren1->dst_entry,
1470 ren2->dst_entry,
1471 o,
1472 NULL,
1473 NULL);
1474 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
1475 /* Two different files renamed to the same thing */
1476 char *ren2_dst;
1477 ren2 = lookup->util;
1478 ren2_dst = ren2->pair->two->path;
1479 if (strcmp(ren1_dst, ren2_dst) != 0)
1480 die("BUG: ren1_dst != ren2_dst");
1481
1482 clean_merge = 0;
1483 ren2->processed = 1;
1484 /*
1485 * BUG: We should only mark src_entry as processed
1486 * if we are not dealing with a rename + add-source
1487 * case.
1488 */
1489 ren2->src_entry->processed = 1;
1490
1491 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
1492 ren1->pair,
1493 ren2->pair,
1494 branch1,
1495 branch2,
1496 ren1->dst_entry,
1497 ren2->dst_entry,
1498 o,
1499 ren1->src_entry,
1500 ren2->src_entry);
1501
1502 } else {
1503 /* Renamed in 1, maybe changed in 2 */
1504 /* we only use sha1 and mode of these */
1505 struct diff_filespec src_other, dst_other;
1506 int try_merge;
1507
1508 /*
1509 * unpack_trees loads entries from common-commit
1510 * into stage 1, from head-commit into stage 2, and
1511 * from merge-commit into stage 3. We keep track
1512 * of which side corresponds to the rename.
1513 */
1514 int renamed_stage = a_renames == renames1 ? 2 : 3;
1515 int other_stage = a_renames == renames1 ? 3 : 2;
1516
1517 /* BUG: We should only remove ren1_src in the base
1518 * stage and in other_stage (think of rename +
1519 * add-source case).
1520 */
1521 remove_file(o, 1, ren1_src,
1522 renamed_stage == 2 || !was_tracked(ren1_src));
1523
1524 oidcpy(&src_other.oid,
1525 &ren1->src_entry->stages[other_stage].oid);
1526 src_other.mode = ren1->src_entry->stages[other_stage].mode;
1527 oidcpy(&dst_other.oid,
1528 &ren1->dst_entry->stages[other_stage].oid);
1529 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
1530 try_merge = 0;
1531
1532 if (oid_eq(&src_other.oid, &null_oid)) {
1533 setup_rename_conflict_info(RENAME_DELETE,
1534 ren1->pair,
1535 NULL,
1536 branch1,
1537 branch2,
1538 ren1->dst_entry,
1539 NULL,
1540 o,
1541 NULL,
1542 NULL);
1543 } else if ((dst_other.mode == ren1->pair->two->mode) &&
1544 oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
1545 /*
1546 * Added file on the other side identical to
1547 * the file being renamed: clean merge.
1548 * Also, there is no need to overwrite the
1549 * file already in the working copy, so call
1550 * update_file_flags() instead of
1551 * update_file().
1552 */
1553 if (update_file_flags(o,
1554 &ren1->pair->two->oid,
1555 ren1->pair->two->mode,
1556 ren1_dst,
1557 1, /* update_cache */
1558 0 /* update_wd */))
1559 clean_merge = -1;
1560 } else if (!oid_eq(&dst_other.oid, &null_oid)) {
1561 clean_merge = 0;
1562 try_merge = 1;
1563 output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1564 "%s added in %s"),
1565 ren1_src, ren1_dst, branch1,
1566 ren1_dst, branch2);
1567 if (o->call_depth) {
1568 struct merge_file_info mfi;
1569 if (merge_file_one(o, ren1_dst, &null_oid, 0,
1570 &ren1->pair->two->oid,
1571 ren1->pair->two->mode,
1572 &dst_other.oid,
1573 dst_other.mode,
1574 branch1, branch2, &mfi)) {
1575 clean_merge = -1;
1576 goto cleanup_and_return;
1577 }
1578 output(o, 1, _("Adding merged %s"), ren1_dst);
1579 if (update_file(o, 0, &mfi.oid,
1580 mfi.mode, ren1_dst))
1581 clean_merge = -1;
1582 try_merge = 0;
1583 } else {
1584 char *new_path = unique_path(o, ren1_dst, branch2);
1585 output(o, 1, _("Adding as %s instead"), new_path);
1586 if (update_file(o, 0, &dst_other.oid,
1587 dst_other.mode, new_path))
1588 clean_merge = -1;
1589 free(new_path);
1590 }
1591 } else
1592 try_merge = 1;
1593
1594 if (clean_merge < 0)
1595 goto cleanup_and_return;
1596 if (try_merge) {
1597 struct diff_filespec *one, *a, *b;
1598 src_other.path = (char *)ren1_src;
1599
1600 one = ren1->pair->one;
1601 if (a_renames == renames1) {
1602 a = ren1->pair->two;
1603 b = &src_other;
1604 } else {
1605 b = ren1->pair->two;
1606 a = &src_other;
1607 }
1608 update_entry(ren1->dst_entry, one, a, b);
1609 setup_rename_conflict_info(RENAME_NORMAL,
1610 ren1->pair,
1611 NULL,
1612 branch1,
1613 NULL,
1614 ren1->dst_entry,
1615 NULL,
1616 o,
1617 NULL,
1618 NULL);
1619 }
1620 }
1621 }
1622cleanup_and_return:
1623 string_list_clear(&a_by_dst, 0);
1624 string_list_clear(&b_by_dst, 0);
1625
1626 return clean_merge;
1627}
1628
1629static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
1630{
1631 return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
1632}
1633
1634static int read_oid_strbuf(struct merge_options *o,
1635 const struct object_id *oid, struct strbuf *dst)
1636{
1637 void *buf;
1638 enum object_type type;
1639 unsigned long size;
1640 buf = read_sha1_file(oid->hash, &type, &size);
1641 if (!buf)
1642 return err(o, _("cannot read object %s"), oid_to_hex(oid));
1643 if (type != OBJ_BLOB) {
1644 free(buf);
1645 return err(o, _("object %s is not a blob"), oid_to_hex(oid));
1646 }
1647 strbuf_attach(dst, buf, size, size + 1);
1648 return 0;
1649}
1650
1651static int blob_unchanged(struct merge_options *opt,
1652 const struct object_id *o_oid,
1653 unsigned o_mode,
1654 const struct object_id *a_oid,
1655 unsigned a_mode,
1656 int renormalize, const char *path)
1657{
1658 struct strbuf o = STRBUF_INIT;
1659 struct strbuf a = STRBUF_INIT;
1660 int ret = 0; /* assume changed for safety */
1661
1662 if (a_mode != o_mode)
1663 return 0;
1664 if (oid_eq(o_oid, a_oid))
1665 return 1;
1666 if (!renormalize)
1667 return 0;
1668
1669 assert(o_oid && a_oid);
1670 if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
1671 goto error_return;
1672 /*
1673 * Note: binary | is used so that both renormalizations are
1674 * performed. Comparison can be skipped if both files are
1675 * unchanged since their sha1s have already been compared.
1676 */
1677 if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
1678 renormalize_buffer(&the_index, path, a.buf, a.len, &a))
1679 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1680
1681error_return:
1682 strbuf_release(&o);
1683 strbuf_release(&a);
1684 return ret;
1685}
1686
1687static int handle_modify_delete(struct merge_options *o,
1688 const char *path,
1689 struct object_id *o_oid, int o_mode,
1690 struct object_id *a_oid, int a_mode,
1691 struct object_id *b_oid, int b_mode)
1692{
1693 const char *modify_branch, *delete_branch;
1694 struct object_id *changed_oid;
1695 int changed_mode;
1696
1697 if (a_oid) {
1698 modify_branch = o->branch1;
1699 delete_branch = o->branch2;
1700 changed_oid = a_oid;
1701 changed_mode = a_mode;
1702 } else {
1703 modify_branch = o->branch2;
1704 delete_branch = o->branch1;
1705 changed_oid = b_oid;
1706 changed_mode = b_mode;
1707 }
1708
1709 return handle_change_delete(o,
1710 path, NULL,
1711 o_oid, o_mode,
1712 changed_oid, changed_mode,
1713 modify_branch, delete_branch,
1714 _("modify"), _("modified"));
1715}
1716
1717static int merge_content(struct merge_options *o,
1718 const char *path,
1719 struct object_id *o_oid, int o_mode,
1720 struct object_id *a_oid, int a_mode,
1721 struct object_id *b_oid, int b_mode,
1722 struct rename_conflict_info *rename_conflict_info)
1723{
1724 const char *reason = _("content");
1725 const char *path1 = NULL, *path2 = NULL;
1726 struct merge_file_info mfi;
1727 struct diff_filespec one, a, b;
1728 unsigned df_conflict_remains = 0;
1729
1730 if (!o_oid) {
1731 reason = _("add/add");
1732 o_oid = (struct object_id *)&null_oid;
1733 }
1734 one.path = a.path = b.path = (char *)path;
1735 oidcpy(&one.oid, o_oid);
1736 one.mode = o_mode;
1737 oidcpy(&a.oid, a_oid);
1738 a.mode = a_mode;
1739 oidcpy(&b.oid, b_oid);
1740 b.mode = b_mode;
1741
1742 if (rename_conflict_info) {
1743 struct diff_filepair *pair1 = rename_conflict_info->pair1;
1744
1745 path1 = (o->branch1 == rename_conflict_info->branch1) ?
1746 pair1->two->path : pair1->one->path;
1747 /* If rename_conflict_info->pair2 != NULL, we are in
1748 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1749 * normal rename.
1750 */
1751 path2 = (rename_conflict_info->pair2 ||
1752 o->branch2 == rename_conflict_info->branch1) ?
1753 pair1->two->path : pair1->one->path;
1754
1755 if (dir_in_way(path, !o->call_depth,
1756 S_ISGITLINK(pair1->two->mode)))
1757 df_conflict_remains = 1;
1758 }
1759 if (merge_file_special_markers(o, &one, &a, &b,
1760 o->branch1, path1,
1761 o->branch2, path2, &mfi))
1762 return -1;
1763
1764 if (mfi.clean && !df_conflict_remains &&
1765 oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
1766 int path_renamed_outside_HEAD;
1767 output(o, 3, _("Skipped %s (merged same as existing)"), path);
1768 /*
1769 * The content merge resulted in the same file contents we
1770 * already had. We can return early if those file contents
1771 * are recorded at the correct path (which may not be true
1772 * if the merge involves a rename).
1773 */
1774 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
1775 if (!path_renamed_outside_HEAD) {
1776 add_cacheinfo(o, mfi.mode, &mfi.oid, path,
1777 0, (!o->call_depth), 0);
1778 return mfi.clean;
1779 }
1780 } else
1781 output(o, 2, _("Auto-merging %s"), path);
1782
1783 if (!mfi.clean) {
1784 if (S_ISGITLINK(mfi.mode))
1785 reason = _("submodule");
1786 output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
1787 reason, path);
1788 if (rename_conflict_info && !df_conflict_remains)
1789 if (update_stages(o, path, &one, &a, &b))
1790 return -1;
1791 }
1792
1793 if (df_conflict_remains) {
1794 char *new_path;
1795 if (o->call_depth) {
1796 remove_file_from_cache(path);
1797 } else {
1798 if (!mfi.clean) {
1799 if (update_stages(o, path, &one, &a, &b))
1800 return -1;
1801 } else {
1802 int file_from_stage2 = was_tracked(path);
1803 struct diff_filespec merged;
1804 oidcpy(&merged.oid, &mfi.oid);
1805 merged.mode = mfi.mode;
1806
1807 if (update_stages(o, path, NULL,
1808 file_from_stage2 ? &merged : NULL,
1809 file_from_stage2 ? NULL : &merged))
1810 return -1;
1811 }
1812
1813 }
1814 new_path = unique_path(o, path, rename_conflict_info->branch1);
1815 output(o, 1, _("Adding as %s instead"), new_path);
1816 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
1817 free(new_path);
1818 return -1;
1819 }
1820 free(new_path);
1821 mfi.clean = 0;
1822 } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
1823 return -1;
1824 return mfi.clean;
1825}
1826
1827/* Per entry merge function */
1828static int process_entry(struct merge_options *o,
1829 const char *path, struct stage_data *entry)
1830{
1831 int clean_merge = 1;
1832 int normalize = o->renormalize;
1833 unsigned o_mode = entry->stages[1].mode;
1834 unsigned a_mode = entry->stages[2].mode;
1835 unsigned b_mode = entry->stages[3].mode;
1836 struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
1837 struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
1838 struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
1839
1840 entry->processed = 1;
1841 if (entry->rename_conflict_info) {
1842 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
1843 switch (conflict_info->rename_type) {
1844 case RENAME_NORMAL:
1845 case RENAME_ONE_FILE_TO_ONE:
1846 clean_merge = merge_content(o, path,
1847 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1848 conflict_info);
1849 break;
1850 case RENAME_DELETE:
1851 clean_merge = 0;
1852 if (conflict_rename_delete(o,
1853 conflict_info->pair1,
1854 conflict_info->branch1,
1855 conflict_info->branch2))
1856 clean_merge = -1;
1857 break;
1858 case RENAME_ONE_FILE_TO_TWO:
1859 clean_merge = 0;
1860 if (conflict_rename_rename_1to2(o, conflict_info))
1861 clean_merge = -1;
1862 break;
1863 case RENAME_TWO_FILES_TO_ONE:
1864 clean_merge = 0;
1865 if (conflict_rename_rename_2to1(o, conflict_info))
1866 clean_merge = -1;
1867 break;
1868 default:
1869 entry->processed = 0;
1870 break;
1871 }
1872 } else if (o_oid && (!a_oid || !b_oid)) {
1873 /* Case A: Deleted in one */
1874 if ((!a_oid && !b_oid) ||
1875 (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
1876 (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
1877 /* Deleted in both or deleted in one and
1878 * unchanged in the other */
1879 if (a_oid)
1880 output(o, 2, _("Removing %s"), path);
1881 /* do not touch working file if it did not exist */
1882 remove_file(o, 1, path, !a_oid);
1883 } else {
1884 /* Modify/delete; deleted side may have put a directory in the way */
1885 clean_merge = 0;
1886 if (handle_modify_delete(o, path, o_oid, o_mode,
1887 a_oid, a_mode, b_oid, b_mode))
1888 clean_merge = -1;
1889 }
1890 } else if ((!o_oid && a_oid && !b_oid) ||
1891 (!o_oid && !a_oid && b_oid)) {
1892 /* Case B: Added in one. */
1893 /* [nothing|directory] -> ([nothing|directory], file) */
1894
1895 const char *add_branch;
1896 const char *other_branch;
1897 unsigned mode;
1898 const struct object_id *oid;
1899 const char *conf;
1900
1901 if (a_oid) {
1902 add_branch = o->branch1;
1903 other_branch = o->branch2;
1904 mode = a_mode;
1905 oid = a_oid;
1906 conf = _("file/directory");
1907 } else {
1908 add_branch = o->branch2;
1909 other_branch = o->branch1;
1910 mode = b_mode;
1911 oid = b_oid;
1912 conf = _("directory/file");
1913 }
1914 if (dir_in_way(path,
1915 !o->call_depth && !S_ISGITLINK(a_mode),
1916 0)) {
1917 char *new_path = unique_path(o, path, add_branch);
1918 clean_merge = 0;
1919 output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1920 "Adding %s as %s"),
1921 conf, path, other_branch, path, new_path);
1922 if (update_file(o, 0, oid, mode, new_path))
1923 clean_merge = -1;
1924 else if (o->call_depth)
1925 remove_file_from_cache(path);
1926 free(new_path);
1927 } else {
1928 output(o, 2, _("Adding %s"), path);
1929 /* do not overwrite file if already present */
1930 if (update_file_flags(o, oid, mode, path, 1, !a_oid))
1931 clean_merge = -1;
1932 }
1933 } else if (a_oid && b_oid) {
1934 /* Case C: Added in both (check for same permissions) and */
1935 /* case D: Modified in both, but differently. */
1936 clean_merge = merge_content(o, path,
1937 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
1938 NULL);
1939 } else if (!o_oid && !a_oid && !b_oid) {
1940 /*
1941 * this entry was deleted altogether. a_mode == 0 means
1942 * we had that path and want to actively remove it.
1943 */
1944 remove_file(o, 1, path, !a_mode);
1945 } else
1946 die("BUG: fatal merge failure, shouldn't happen.");
1947
1948 return clean_merge;
1949}
1950
1951int merge_trees(struct merge_options *o,
1952 struct tree *head,
1953 struct tree *merge,
1954 struct tree *common,
1955 struct tree **result)
1956{
1957 int code, clean;
1958
1959 if (o->subtree_shift) {
1960 merge = shift_tree_object(head, merge, o->subtree_shift);
1961 common = shift_tree_object(head, common, o->subtree_shift);
1962 }
1963
1964 if (oid_eq(&common->object.oid, &merge->object.oid)) {
1965 struct strbuf sb = STRBUF_INIT;
1966
1967 if (!o->call_depth && index_has_changes(&sb)) {
1968 err(o, _("Dirty index: cannot merge (dirty: %s)"),
1969 sb.buf);
1970 return 0;
1971 }
1972 output(o, 0, _("Already up to date!"));
1973 *result = head;
1974 return 1;
1975 }
1976
1977 code = git_merge_trees(o->call_depth, common, head, merge);
1978
1979 if (code != 0) {
1980 if (show(o, 4) || o->call_depth)
1981 err(o, _("merging of trees %s and %s failed"),
1982 oid_to_hex(&head->object.oid),
1983 oid_to_hex(&merge->object.oid));
1984 return -1;
1985 }
1986
1987 if (unmerged_cache()) {
1988 struct string_list *entries, *re_head, *re_merge;
1989 int i;
1990 /*
1991 * Only need the hashmap while processing entries, so
1992 * initialize it here and free it when we are done running
1993 * through the entries. Keeping it in the merge_options as
1994 * opposed to decaring a local hashmap is for convenience
1995 * so that we don't have to pass it to around.
1996 */
1997 hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512);
1998 get_files_dirs(o, head);
1999 get_files_dirs(o, merge);
2000
2001 entries = get_unmerged();
2002 record_df_conflict_files(o, entries);
2003 re_head = get_renames(o, head, common, head, merge, entries);
2004 re_merge = get_renames(o, merge, common, head, merge, entries);
2005 clean = process_renames(o, re_head, re_merge);
2006 if (clean < 0)
2007 goto cleanup;
2008 for (i = entries->nr-1; 0 <= i; i--) {
2009 const char *path = entries->items[i].string;
2010 struct stage_data *e = entries->items[i].util;
2011 if (!e->processed) {
2012 int ret = process_entry(o, path, e);
2013 if (!ret)
2014 clean = 0;
2015 else if (ret < 0) {
2016 clean = ret;
2017 goto cleanup;
2018 }
2019 }
2020 }
2021 for (i = 0; i < entries->nr; i++) {
2022 struct stage_data *e = entries->items[i].util;
2023 if (!e->processed)
2024 die("BUG: unprocessed path??? %s",
2025 entries->items[i].string);
2026 }
2027
2028cleanup:
2029 string_list_clear(re_merge, 0);
2030 string_list_clear(re_head, 0);
2031 string_list_clear(entries, 1);
2032
2033 hashmap_free(&o->current_file_dir_set, 1);
2034
2035 free(re_merge);
2036 free(re_head);
2037 free(entries);
2038
2039 if (clean < 0)
2040 return clean;
2041 }
2042 else
2043 clean = 1;
2044
2045 if (o->call_depth && !(*result = write_tree_from_memory(o)))
2046 return -1;
2047
2048 return clean;
2049}
2050
2051static struct commit_list *reverse_commit_list(struct commit_list *list)
2052{
2053 struct commit_list *next = NULL, *current, *backup;
2054 for (current = list; current; current = backup) {
2055 backup = current->next;
2056 current->next = next;
2057 next = current;
2058 }
2059 return next;
2060}
2061
2062/*
2063 * Merge the commits h1 and h2, return the resulting virtual
2064 * commit object and a flag indicating the cleanness of the merge.
2065 */
2066int merge_recursive(struct merge_options *o,
2067 struct commit *h1,
2068 struct commit *h2,
2069 struct commit_list *ca,
2070 struct commit **result)
2071{
2072 struct commit_list *iter;
2073 struct commit *merged_common_ancestors;
2074 struct tree *mrtree = mrtree;
2075 int clean;
2076
2077 if (show(o, 4)) {
2078 output(o, 4, _("Merging:"));
2079 output_commit_title(o, h1);
2080 output_commit_title(o, h2);
2081 }
2082
2083 if (!ca) {
2084 ca = get_merge_bases(h1, h2);
2085 ca = reverse_commit_list(ca);
2086 }
2087
2088 if (show(o, 5)) {
2089 unsigned cnt = commit_list_count(ca);
2090
2091 output(o, 5, Q_("found %u common ancestor:",
2092 "found %u common ancestors:", cnt), cnt);
2093 for (iter = ca; iter; iter = iter->next)
2094 output_commit_title(o, iter->item);
2095 }
2096
2097 merged_common_ancestors = pop_commit(&ca);
2098 if (merged_common_ancestors == NULL) {
2099 /* if there is no common ancestor, use an empty tree */
2100 struct tree *tree;
2101
2102 tree = lookup_tree(the_hash_algo->empty_tree);
2103 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
2104 }
2105
2106 for (iter = ca; iter; iter = iter->next) {
2107 const char *saved_b1, *saved_b2;
2108 o->call_depth++;
2109 /*
2110 * When the merge fails, the result contains files
2111 * with conflict markers. The cleanness flag is
2112 * ignored (unless indicating an error), it was never
2113 * actually used, as result of merge_trees has always
2114 * overwritten it: the committed "conflicts" were
2115 * already resolved.
2116 */
2117 discard_cache();
2118 saved_b1 = o->branch1;
2119 saved_b2 = o->branch2;
2120 o->branch1 = "Temporary merge branch 1";
2121 o->branch2 = "Temporary merge branch 2";
2122 if (merge_recursive(o, merged_common_ancestors, iter->item,
2123 NULL, &merged_common_ancestors) < 0)
2124 return -1;
2125 o->branch1 = saved_b1;
2126 o->branch2 = saved_b2;
2127 o->call_depth--;
2128
2129 if (!merged_common_ancestors)
2130 return err(o, _("merge returned no commit"));
2131 }
2132
2133 discard_cache();
2134 if (!o->call_depth)
2135 read_cache();
2136
2137 o->ancestor = "merged common ancestors";
2138 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
2139 &mrtree);
2140 if (clean < 0) {
2141 flush_output(o);
2142 return clean;
2143 }
2144
2145 if (o->call_depth) {
2146 *result = make_virtual_commit(mrtree, "merged tree");
2147 commit_list_insert(h1, &(*result)->parents);
2148 commit_list_insert(h2, &(*result)->parents->next);
2149 }
2150 flush_output(o);
2151 if (!o->call_depth && o->buffer_output < 2)
2152 strbuf_release(&o->obuf);
2153 if (show(o, 2))
2154 diff_warn_rename_limit("merge.renamelimit",
2155 o->needed_rename_limit, 0);
2156 return clean;
2157}
2158
2159static struct commit *get_ref(const struct object_id *oid, const char *name)
2160{
2161 struct object *object;
2162
2163 object = deref_tag(parse_object(oid), name, strlen(name));
2164 if (!object)
2165 return NULL;
2166 if (object->type == OBJ_TREE)
2167 return make_virtual_commit((struct tree*)object, name);
2168 if (object->type != OBJ_COMMIT)
2169 return NULL;
2170 if (parse_commit((struct commit *)object))
2171 return NULL;
2172 return (struct commit *)object;
2173}
2174
2175int merge_recursive_generic(struct merge_options *o,
2176 const struct object_id *head,
2177 const struct object_id *merge,
2178 int num_base_list,
2179 const struct object_id **base_list,
2180 struct commit **result)
2181{
2182 int clean;
2183 struct lock_file lock = LOCK_INIT;
2184 struct commit *head_commit = get_ref(head, o->branch1);
2185 struct commit *next_commit = get_ref(merge, o->branch2);
2186 struct commit_list *ca = NULL;
2187
2188 if (base_list) {
2189 int i;
2190 for (i = 0; i < num_base_list; ++i) {
2191 struct commit *base;
2192 if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
2193 return err(o, _("Could not parse object '%s'"),
2194 oid_to_hex(base_list[i]));
2195 commit_list_insert(base, &ca);
2196 }
2197 }
2198
2199 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
2200 clean = merge_recursive(o, head_commit, next_commit, ca,
2201 result);
2202 if (clean < 0)
2203 return clean;
2204
2205 if (active_cache_changed &&
2206 write_locked_index(&the_index, &lock, COMMIT_LOCK))
2207 return err(o, _("Unable to write index."));
2208
2209 return clean ? 0 : 1;
2210}
2211
2212static void merge_recursive_config(struct merge_options *o)
2213{
2214 git_config_get_int("merge.verbosity", &o->verbosity);
2215 git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
2216 git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
2217 git_config(git_xmerge_config, NULL);
2218}
2219
2220void init_merge_options(struct merge_options *o)
2221{
2222 const char *merge_verbosity;
2223 memset(o, 0, sizeof(struct merge_options));
2224 o->verbosity = 2;
2225 o->buffer_output = 1;
2226 o->diff_rename_limit = -1;
2227 o->merge_rename_limit = -1;
2228 o->renormalize = 0;
2229 o->detect_rename = 1;
2230 merge_recursive_config(o);
2231 merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
2232 if (merge_verbosity)
2233 o->verbosity = strtol(merge_verbosity, NULL, 10);
2234 if (o->verbosity >= 5)
2235 o->buffer_output = 0;
2236 strbuf_init(&o->obuf, 0);
2237 string_list_init(&o->df_conflict_file_set, 1);
2238}
2239
2240int parse_merge_opt(struct merge_options *o, const char *s)
2241{
2242 const char *arg;
2243
2244 if (!s || !*s)
2245 return -1;
2246 if (!strcmp(s, "ours"))
2247 o->recursive_variant = MERGE_RECURSIVE_OURS;
2248 else if (!strcmp(s, "theirs"))
2249 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
2250 else if (!strcmp(s, "subtree"))
2251 o->subtree_shift = "";
2252 else if (skip_prefix(s, "subtree=", &arg))
2253 o->subtree_shift = arg;
2254 else if (!strcmp(s, "patience"))
2255 o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
2256 else if (!strcmp(s, "histogram"))
2257 o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
2258 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
2259 long value = parse_algorithm_value(arg);
2260 if (value < 0)
2261 return -1;
2262 /* clear out previous settings */
2263 DIFF_XDL_CLR(o, NEED_MINIMAL);
2264 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
2265 o->xdl_opts |= value;
2266 }
2267 else if (!strcmp(s, "ignore-space-change"))
2268 DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);
2269 else if (!strcmp(s, "ignore-all-space"))
2270 DIFF_XDL_SET(o, IGNORE_WHITESPACE);
2271 else if (!strcmp(s, "ignore-space-at-eol"))
2272 DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);
2273 else if (!strcmp(s, "ignore-cr-at-eol"))
2274 DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);
2275 else if (!strcmp(s, "renormalize"))
2276 o->renormalize = 1;
2277 else if (!strcmp(s, "no-renormalize"))
2278 o->renormalize = 0;
2279 else if (!strcmp(s, "no-renames"))
2280 o->detect_rename = 0;
2281 else if (!strcmp(s, "find-renames")) {
2282 o->detect_rename = 1;
2283 o->rename_score = 0;
2284 }
2285 else if (skip_prefix(s, "find-renames=", &arg) ||
2286 skip_prefix(s, "rename-threshold=", &arg)) {
2287 if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
2288 return -1;
2289 o->detect_rename = 1;
2290 }
2291 else
2292 return -1;
2293 return 0;
2294}