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