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