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