merge-recursive: apply necessary modifications for directory renames
[git] / merge-recursive.c
1 /*
2  * Recursive Merge algorithm stolen from git-merge-recursive.py by
3  * Fredrik Kuivinen.
4  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
5  */
6 #include "cache.h"
7 #include "config.h"
8 #include "advice.h"
9 #include "lockfile.h"
10 #include "cache-tree.h"
11 #include "commit.h"
12 #include "blob.h"
13 #include "builtin.h"
14 #include "tree-walk.h"
15 #include "diff.h"
16 #include "diffcore.h"
17 #include "tag.h"
18 #include "unpack-trees.h"
19 #include "string-list.h"
20 #include "xdiff-interface.h"
21 #include "ll-merge.h"
22 #include "attr.h"
23 #include "merge-recursive.h"
24 #include "dir.h"
25 #include "submodule.h"
26
27 struct path_hashmap_entry {
28         struct hashmap_entry e;
29         char path[FLEX_ARRAY];
30 };
31
32 static int path_hashmap_cmp(const void *cmp_data,
33                             const void *entry,
34                             const void *entry_or_key,
35                             const void *keydata)
36 {
37         const struct path_hashmap_entry *a = entry;
38         const struct path_hashmap_entry *b = entry_or_key;
39         const char *key = keydata;
40
41         if (ignore_case)
42                 return strcasecmp(a->path, key ? key : b->path);
43         else
44                 return strcmp(a->path, key ? key : b->path);
45 }
46
47 static unsigned int path_hash(const char *path)
48 {
49         return ignore_case ? strihash(path) : strhash(path);
50 }
51
52 static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
53                                                       char *dir)
54 {
55         struct dir_rename_entry key;
56
57         if (dir == NULL)
58                 return NULL;
59         hashmap_entry_init(&key, strhash(dir));
60         key.dir = dir;
61         return hashmap_get(hashmap, &key, NULL);
62 }
63
64 static int dir_rename_cmp(const void *unused_cmp_data,
65                           const void *entry,
66                           const void *entry_or_key,
67                           const void *unused_keydata)
68 {
69         const struct dir_rename_entry *e1 = entry;
70         const struct dir_rename_entry *e2 = entry_or_key;
71
72         return strcmp(e1->dir, e2->dir);
73 }
74
75 static void dir_rename_init(struct hashmap *map)
76 {
77         hashmap_init(map, dir_rename_cmp, NULL, 0);
78 }
79
80 static void dir_rename_entry_init(struct dir_rename_entry *entry,
81                                   char *directory)
82 {
83         hashmap_entry_init(entry, strhash(directory));
84         entry->dir = directory;
85         entry->non_unique_new_dir = 0;
86         strbuf_init(&entry->new_dir, 0);
87         string_list_init(&entry->possible_new_dirs, 0);
88 }
89
90 static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
91                                                     char *target_file)
92 {
93         struct collision_entry key;
94
95         hashmap_entry_init(&key, strhash(target_file));
96         key.target_file = target_file;
97         return hashmap_get(hashmap, &key, NULL);
98 }
99
100 static int collision_cmp(void *unused_cmp_data,
101                          const struct collision_entry *e1,
102                          const struct collision_entry *e2,
103                          const void *unused_keydata)
104 {
105         return strcmp(e1->target_file, e2->target_file);
106 }
107
108 static void collision_init(struct hashmap *map)
109 {
110         hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL, 0);
111 }
112
113 static void flush_output(struct merge_options *o)
114 {
115         if (o->buffer_output < 2 && o->obuf.len) {
116                 fputs(o->obuf.buf, stdout);
117                 strbuf_reset(&o->obuf);
118         }
119 }
120
121 static int err(struct merge_options *o, const char *err, ...)
122 {
123         va_list params;
124
125         if (o->buffer_output < 2)
126                 flush_output(o);
127         else {
128                 strbuf_complete(&o->obuf, '\n');
129                 strbuf_addstr(&o->obuf, "error: ");
130         }
131         va_start(params, err);
132         strbuf_vaddf(&o->obuf, err, params);
133         va_end(params);
134         if (o->buffer_output > 1)
135                 strbuf_addch(&o->obuf, '\n');
136         else {
137                 error("%s", o->obuf.buf);
138                 strbuf_reset(&o->obuf);
139         }
140
141         return -1;
142 }
143
144 static struct tree *shift_tree_object(struct tree *one, struct tree *two,
145                                       const char *subtree_shift)
146 {
147         struct object_id shifted;
148
149         if (!*subtree_shift) {
150                 shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
151         } else {
152                 shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
153                               subtree_shift);
154         }
155         if (!oidcmp(&two->object.oid, &shifted))
156                 return two;
157         return lookup_tree(&shifted);
158 }
159
160 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
161 {
162         struct commit *commit = alloc_commit_node();
163
164         set_merge_remote_desc(commit, comment, (struct object *)commit);
165         commit->tree = tree;
166         commit->object.parsed = 1;
167         return commit;
168 }
169
170 /*
171  * Since we use get_tree_entry(), which does not put the read object into
172  * the object pool, we cannot rely on a == b.
173  */
174 static int oid_eq(const struct object_id *a, const struct object_id *b)
175 {
176         if (!a && !b)
177                 return 2;
178         return a && b && oidcmp(a, b) == 0;
179 }
180
181 enum rename_type {
182         RENAME_NORMAL = 0,
183         RENAME_DIR,
184         RENAME_DELETE,
185         RENAME_ONE_FILE_TO_ONE,
186         RENAME_ONE_FILE_TO_TWO,
187         RENAME_TWO_FILES_TO_ONE
188 };
189
190 struct rename_conflict_info {
191         enum rename_type rename_type;
192         struct diff_filepair *pair1;
193         struct diff_filepair *pair2;
194         const char *branch1;
195         const char *branch2;
196         struct stage_data *dst_entry1;
197         struct stage_data *dst_entry2;
198         struct diff_filespec ren1_other;
199         struct diff_filespec ren2_other;
200 };
201
202 /*
203  * Since we want to write the index eventually, we cannot reuse the index
204  * for these (temporary) data.
205  */
206 struct stage_data {
207         struct {
208                 unsigned mode;
209                 struct object_id oid;
210         } stages[4];
211         struct rename_conflict_info *rename_conflict_info;
212         unsigned processed:1;
213 };
214
215 static inline void setup_rename_conflict_info(enum rename_type rename_type,
216                                               struct diff_filepair *pair1,
217                                               struct diff_filepair *pair2,
218                                               const char *branch1,
219                                               const char *branch2,
220                                               struct stage_data *dst_entry1,
221                                               struct stage_data *dst_entry2,
222                                               struct merge_options *o,
223                                               struct stage_data *src_entry1,
224                                               struct stage_data *src_entry2)
225 {
226         struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
227         ci->rename_type = rename_type;
228         ci->pair1 = pair1;
229         ci->branch1 = branch1;
230         ci->branch2 = branch2;
231
232         ci->dst_entry1 = dst_entry1;
233         dst_entry1->rename_conflict_info = ci;
234         dst_entry1->processed = 0;
235
236         assert(!pair2 == !dst_entry2);
237         if (dst_entry2) {
238                 ci->dst_entry2 = dst_entry2;
239                 ci->pair2 = pair2;
240                 dst_entry2->rename_conflict_info = ci;
241         }
242
243         if (rename_type == RENAME_TWO_FILES_TO_ONE) {
244                 /*
245                  * For each rename, there could have been
246                  * modifications on the side of history where that
247                  * file was not renamed.
248                  */
249                 int ostage1 = o->branch1 == branch1 ? 3 : 2;
250                 int ostage2 = ostage1 ^ 1;
251
252                 ci->ren1_other.path = pair1->one->path;
253                 oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
254                 ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
255
256                 ci->ren2_other.path = pair2->one->path;
257                 oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
258                 ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
259         }
260 }
261
262 static int show(struct merge_options *o, int v)
263 {
264         return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
265 }
266
267 __attribute__((format (printf, 3, 4)))
268 static void output(struct merge_options *o, int v, const char *fmt, ...)
269 {
270         va_list ap;
271
272         if (!show(o, v))
273                 return;
274
275         strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
276
277         va_start(ap, fmt);
278         strbuf_vaddf(&o->obuf, fmt, ap);
279         va_end(ap);
280
281         strbuf_addch(&o->obuf, '\n');
282         if (!o->buffer_output)
283                 flush_output(o);
284 }
285
286 static void output_commit_title(struct merge_options *o, struct commit *commit)
287 {
288         strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
289         if (commit->util)
290                 strbuf_addf(&o->obuf, "virtual %s\n",
291                         merge_remote_util(commit)->name);
292         else {
293                 strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid,
294                                          DEFAULT_ABBREV);
295                 strbuf_addch(&o->obuf, ' ');
296                 if (parse_commit(commit) != 0)
297                         strbuf_addstr(&o->obuf, _("(bad commit)\n"));
298                 else {
299                         const char *title;
300                         const char *msg = get_commit_buffer(commit, NULL);
301                         int len = find_commit_subject(msg, &title);
302                         if (len)
303                                 strbuf_addf(&o->obuf, "%.*s\n", len, title);
304                         unuse_commit_buffer(commit, msg);
305                 }
306         }
307         flush_output(o);
308 }
309
310 static int add_cacheinfo(struct merge_options *o,
311                 unsigned int mode, const struct object_id *oid,
312                 const char *path, int stage, int refresh, int options)
313 {
314         struct cache_entry *ce;
315         int ret;
316
317         ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
318         if (!ce)
319                 return err(o, _("addinfo_cache failed for path '%s'"), path);
320
321         ret = add_cache_entry(ce, options);
322         if (refresh) {
323                 struct cache_entry *nce;
324
325                 nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
326                 if (!nce)
327                         return err(o, _("addinfo_cache failed for path '%s'"), path);
328                 if (nce != ce)
329                         ret = add_cache_entry(nce, options);
330         }
331         return ret;
332 }
333
334 static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
335 {
336         parse_tree(tree);
337         init_tree_desc(desc, tree->buffer, tree->size);
338 }
339
340 static int git_merge_trees(int index_only,
341                            struct tree *common,
342                            struct tree *head,
343                            struct tree *merge)
344 {
345         int rc;
346         struct tree_desc t[3];
347         struct unpack_trees_options opts;
348
349         memset(&opts, 0, sizeof(opts));
350         if (index_only)
351                 opts.index_only = 1;
352         else
353                 opts.update = 1;
354         opts.merge = 1;
355         opts.head_idx = 2;
356         opts.fn = threeway_merge;
357         opts.src_index = &the_index;
358         opts.dst_index = &the_index;
359         setup_unpack_trees_porcelain(&opts, "merge");
360
361         init_tree_desc_from_tree(t+0, common);
362         init_tree_desc_from_tree(t+1, head);
363         init_tree_desc_from_tree(t+2, merge);
364
365         rc = unpack_trees(3, t, &opts);
366         cache_tree_free(&active_cache_tree);
367         return rc;
368 }
369
370 struct tree *write_tree_from_memory(struct merge_options *o)
371 {
372         struct tree *result = NULL;
373
374         if (unmerged_cache()) {
375                 int i;
376                 fprintf(stderr, "BUG: There are unmerged index entries:\n");
377                 for (i = 0; i < active_nr; i++) {
378                         const struct cache_entry *ce = active_cache[i];
379                         if (ce_stage(ce))
380                                 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
381                                         (int)ce_namelen(ce), ce->name);
382                 }
383                 die("BUG: unmerged index entries in merge-recursive.c");
384         }
385
386         if (!active_cache_tree)
387                 active_cache_tree = cache_tree();
388
389         if (!cache_tree_fully_valid(active_cache_tree) &&
390             cache_tree_update(&the_index, 0) < 0) {
391                 err(o, _("error building trees"));
392                 return NULL;
393         }
394
395         result = lookup_tree(&active_cache_tree->oid);
396
397         return result;
398 }
399
400 static int save_files_dirs(const struct object_id *oid,
401                 struct strbuf *base, const char *path,
402                 unsigned int mode, int stage, void *context)
403 {
404         struct path_hashmap_entry *entry;
405         int baselen = base->len;
406         struct merge_options *o = context;
407
408         strbuf_addstr(base, path);
409
410         FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
411         hashmap_entry_init(entry, path_hash(entry->path));
412         hashmap_add(&o->current_file_dir_set, entry);
413
414         strbuf_setlen(base, baselen);
415         return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
416 }
417
418 static void get_files_dirs(struct merge_options *o, struct tree *tree)
419 {
420         struct pathspec match_all;
421         memset(&match_all, 0, sizeof(match_all));
422         read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o);
423 }
424
425 static int get_tree_entry_if_blob(const struct object_id *tree,
426                                   const char *path,
427                                   struct object_id *hashy,
428                                   unsigned int *mode_o)
429 {
430         int ret;
431
432         ret = get_tree_entry(tree, path, hashy, mode_o);
433         if (S_ISDIR(*mode_o)) {
434                 oidcpy(hashy, &null_oid);
435                 *mode_o = 0;
436         }
437         return ret;
438 }
439
440 /*
441  * Returns an index_entry instance which doesn't have to correspond to
442  * a real cache entry in Git's index.
443  */
444 static struct stage_data *insert_stage_data(const char *path,
445                 struct tree *o, struct tree *a, struct tree *b,
446                 struct string_list *entries)
447 {
448         struct string_list_item *item;
449         struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
450         get_tree_entry_if_blob(&o->object.oid, path,
451                                &e->stages[1].oid, &e->stages[1].mode);
452         get_tree_entry_if_blob(&a->object.oid, path,
453                                &e->stages[2].oid, &e->stages[2].mode);
454         get_tree_entry_if_blob(&b->object.oid, path,
455                                &e->stages[3].oid, &e->stages[3].mode);
456         item = string_list_insert(entries, path);
457         item->util = e;
458         return e;
459 }
460
461 /*
462  * Create a dictionary mapping file names to stage_data objects. The
463  * dictionary contains one entry for every path with a non-zero stage entry.
464  */
465 static struct string_list *get_unmerged(void)
466 {
467         struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
468         int i;
469
470         unmerged->strdup_strings = 1;
471
472         for (i = 0; i < active_nr; i++) {
473                 struct string_list_item *item;
474                 struct stage_data *e;
475                 const struct cache_entry *ce = active_cache[i];
476                 if (!ce_stage(ce))
477                         continue;
478
479                 item = string_list_lookup(unmerged, ce->name);
480                 if (!item) {
481                         item = string_list_insert(unmerged, ce->name);
482                         item->util = xcalloc(1, sizeof(struct stage_data));
483                 }
484                 e = item->util;
485                 e->stages[ce_stage(ce)].mode = ce->ce_mode;
486                 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
487         }
488
489         return unmerged;
490 }
491
492 static int string_list_df_name_compare(const char *one, const char *two)
493 {
494         int onelen = strlen(one);
495         int twolen = strlen(two);
496         /*
497          * Here we only care that entries for D/F conflicts are
498          * adjacent, in particular with the file of the D/F conflict
499          * appearing before files below the corresponding directory.
500          * The order of the rest of the list is irrelevant for us.
501          *
502          * To achieve this, we sort with df_name_compare and provide
503          * the mode S_IFDIR so that D/F conflicts will sort correctly.
504          * We use the mode S_IFDIR for everything else for simplicity,
505          * since in other cases any changes in their order due to
506          * sorting cause no problems for us.
507          */
508         int cmp = df_name_compare(one, onelen, S_IFDIR,
509                                   two, twolen, S_IFDIR);
510         /*
511          * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
512          * that 'foo' comes before 'foo/bar'.
513          */
514         if (cmp)
515                 return cmp;
516         return onelen - twolen;
517 }
518
519 static void record_df_conflict_files(struct merge_options *o,
520                                      struct string_list *entries)
521 {
522         /* If there is a D/F conflict and the file for such a conflict
523          * currently exist in the working tree, we want to allow it to be
524          * removed to make room for the corresponding directory if needed.
525          * The files underneath the directories of such D/F conflicts will
526          * be processed before the corresponding file involved in the D/F
527          * conflict.  If the D/F directory ends up being removed by the
528          * merge, then we won't have to touch the D/F file.  If the D/F
529          * directory needs to be written to the working copy, then the D/F
530          * file will simply be removed (in make_room_for_path()) to make
531          * room for the necessary paths.  Note that if both the directory
532          * and the file need to be present, then the D/F file will be
533          * reinstated with a new unique name at the time it is processed.
534          */
535         struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
536         const char *last_file = NULL;
537         int last_len = 0;
538         int i;
539
540         /*
541          * If we're merging merge-bases, we don't want to bother with
542          * any working directory changes.
543          */
544         if (o->call_depth)
545                 return;
546
547         /* Ensure D/F conflicts are adjacent in the entries list. */
548         for (i = 0; i < entries->nr; i++) {
549                 struct string_list_item *next = &entries->items[i];
550                 string_list_append(&df_sorted_entries, next->string)->util =
551                                    next->util;
552         }
553         df_sorted_entries.cmp = string_list_df_name_compare;
554         string_list_sort(&df_sorted_entries);
555
556         string_list_clear(&o->df_conflict_file_set, 1);
557         for (i = 0; i < df_sorted_entries.nr; i++) {
558                 const char *path = df_sorted_entries.items[i].string;
559                 int len = strlen(path);
560                 struct stage_data *e = df_sorted_entries.items[i].util;
561
562                 /*
563                  * Check if last_file & path correspond to a D/F conflict;
564                  * i.e. whether path is last_file+'/'+<something>.
565                  * If so, record that it's okay to remove last_file to make
566                  * room for path and friends if needed.
567                  */
568                 if (last_file &&
569                     len > last_len &&
570                     memcmp(path, last_file, last_len) == 0 &&
571                     path[last_len] == '/') {
572                         string_list_insert(&o->df_conflict_file_set, last_file);
573                 }
574
575                 /*
576                  * Determine whether path could exist as a file in the
577                  * working directory as a possible D/F conflict.  This
578                  * will only occur when it exists in stage 2 as a
579                  * file.
580                  */
581                 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
582                         last_file = path;
583                         last_len = len;
584                 } else {
585                         last_file = NULL;
586                 }
587         }
588         string_list_clear(&df_sorted_entries, 0);
589 }
590
591 struct rename {
592         struct diff_filepair *pair;
593         /*
594          * Purpose of src_entry and dst_entry:
595          *
596          * If 'before' is renamed to 'after' then src_entry will contain
597          * the versions of 'before' from the merge_base, HEAD, and MERGE in
598          * stages 1, 2, and 3; dst_entry will contain the respective
599          * versions of 'after' in corresponding locations.  Thus, we have a
600          * total of six modes and oids, though some will be null.  (Stage 0
601          * is ignored; we're interested in handling conflicts.)
602          *
603          * Since we don't turn on break-rewrites by default, neither
604          * src_entry nor dst_entry can have all three of their stages have
605          * non-null oids, meaning at most four of the six will be non-null.
606          * Also, since this is a rename, both src_entry and dst_entry will
607          * have at least one non-null oid, meaning at least two will be
608          * non-null.  Of the six oids, a typical rename will have three be
609          * non-null.  Only two implies a rename/delete, and four implies a
610          * rename/add.
611          */
612         struct stage_data *src_entry;
613         struct stage_data *dst_entry;
614         unsigned add_turned_into_rename:1;
615         unsigned processed:1;
616 };
617
618 static int update_stages(struct merge_options *opt, const char *path,
619                          const struct diff_filespec *o,
620                          const struct diff_filespec *a,
621                          const struct diff_filespec *b)
622 {
623
624         /*
625          * NOTE: It is usually a bad idea to call update_stages on a path
626          * before calling update_file on that same path, since it can
627          * sometimes lead to spurious "refusing to lose untracked file..."
628          * messages from update_file (via make_room_for path via
629          * would_lose_untracked).  Instead, reverse the order of the calls
630          * (executing update_file first and then update_stages).
631          */
632         int clear = 1;
633         int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
634         if (clear)
635                 if (remove_file_from_cache(path))
636                         return -1;
637         if (o)
638                 if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
639                         return -1;
640         if (a)
641                 if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
642                         return -1;
643         if (b)
644                 if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
645                         return -1;
646         return 0;
647 }
648
649 static int update_stages_for_stage_data(struct merge_options *opt,
650                                         const char *path,
651                                         const struct stage_data *stage_data)
652 {
653         struct diff_filespec o, a, b;
654
655         o.mode = stage_data->stages[1].mode;
656         oidcpy(&o.oid, &stage_data->stages[1].oid);
657
658         a.mode = stage_data->stages[2].mode;
659         oidcpy(&a.oid, &stage_data->stages[2].oid);
660
661         b.mode = stage_data->stages[3].mode;
662         oidcpy(&b.oid, &stage_data->stages[3].oid);
663
664         return update_stages(opt, path,
665                              is_null_oid(&o.oid) ? NULL : &o,
666                              is_null_oid(&a.oid) ? NULL : &a,
667                              is_null_oid(&b.oid) ? NULL : &b);
668 }
669
670 static void update_entry(struct stage_data *entry,
671                          struct diff_filespec *o,
672                          struct diff_filespec *a,
673                          struct diff_filespec *b)
674 {
675         entry->processed = 0;
676         entry->stages[1].mode = o->mode;
677         entry->stages[2].mode = a->mode;
678         entry->stages[3].mode = b->mode;
679         oidcpy(&entry->stages[1].oid, &o->oid);
680         oidcpy(&entry->stages[2].oid, &a->oid);
681         oidcpy(&entry->stages[3].oid, &b->oid);
682 }
683
684 static int remove_file(struct merge_options *o, int clean,
685                        const char *path, int no_wd)
686 {
687         int update_cache = o->call_depth || clean;
688         int update_working_directory = !o->call_depth && !no_wd;
689
690         if (update_cache) {
691                 if (remove_file_from_cache(path))
692                         return -1;
693         }
694         if (update_working_directory) {
695                 if (ignore_case) {
696                         struct cache_entry *ce;
697                         ce = cache_file_exists(path, strlen(path), ignore_case);
698                         if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
699                                 return 0;
700                 }
701                 if (remove_path(path))
702                         return -1;
703         }
704         return 0;
705 }
706
707 /* add a string to a strbuf, but converting "/" to "_" */
708 static void add_flattened_path(struct strbuf *out, const char *s)
709 {
710         size_t i = out->len;
711         strbuf_addstr(out, s);
712         for (; i < out->len; i++)
713                 if (out->buf[i] == '/')
714                         out->buf[i] = '_';
715 }
716
717 static char *unique_path(struct merge_options *o, const char *path, const char *branch)
718 {
719         struct path_hashmap_entry *entry;
720         struct strbuf newpath = STRBUF_INIT;
721         int suffix = 0;
722         size_t base_len;
723
724         strbuf_addf(&newpath, "%s~", path);
725         add_flattened_path(&newpath, branch);
726
727         base_len = newpath.len;
728         while (hashmap_get_from_hash(&o->current_file_dir_set,
729                                      path_hash(newpath.buf), newpath.buf) ||
730                (!o->call_depth && file_exists(newpath.buf))) {
731                 strbuf_setlen(&newpath, base_len);
732                 strbuf_addf(&newpath, "_%d", suffix++);
733         }
734
735         FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
736         hashmap_entry_init(entry, path_hash(entry->path));
737         hashmap_add(&o->current_file_dir_set, entry);
738         return strbuf_detach(&newpath, NULL);
739 }
740
741 /**
742  * Check whether a directory in the index is in the way of an incoming
743  * file.  Return 1 if so.  If check_working_copy is non-zero, also
744  * check the working directory.  If empty_ok is non-zero, also return
745  * 0 in the case where the working-tree dir exists but is empty.
746  */
747 static int dir_in_way(const char *path, int check_working_copy, int empty_ok)
748 {
749         int pos;
750         struct strbuf dirpath = STRBUF_INIT;
751         struct stat st;
752
753         strbuf_addstr(&dirpath, path);
754         strbuf_addch(&dirpath, '/');
755
756         pos = cache_name_pos(dirpath.buf, dirpath.len);
757
758         if (pos < 0)
759                 pos = -1 - pos;
760         if (pos < active_nr &&
761             !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
762                 strbuf_release(&dirpath);
763                 return 1;
764         }
765
766         strbuf_release(&dirpath);
767         return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
768                 !(empty_ok && is_empty_dir(path));
769 }
770
771 static int was_tracked(const char *path)
772 {
773         int pos = cache_name_pos(path, strlen(path));
774
775         if (0 <= pos)
776                 /* we have been tracking this path */
777                 return 1;
778
779         /*
780          * Look for an unmerged entry for the path,
781          * specifically stage #2, which would indicate
782          * that "our" side before the merge started
783          * had the path tracked (and resulted in a conflict).
784          */
785         for (pos = -1 - pos;
786              pos < active_nr && !strcmp(path, active_cache[pos]->name);
787              pos++)
788                 if (ce_stage(active_cache[pos]) == 2)
789                         return 1;
790         return 0;
791 }
792
793 static int would_lose_untracked(const char *path)
794 {
795         return !was_tracked(path) && file_exists(path);
796 }
797
798 static int make_room_for_path(struct merge_options *o, const char *path)
799 {
800         int status, i;
801         const char *msg = _("failed to create path '%s'%s");
802
803         /* Unlink any D/F conflict files that are in the way */
804         for (i = 0; i < o->df_conflict_file_set.nr; i++) {
805                 const char *df_path = o->df_conflict_file_set.items[i].string;
806                 size_t pathlen = strlen(path);
807                 size_t df_pathlen = strlen(df_path);
808                 if (df_pathlen < pathlen &&
809                     path[df_pathlen] == '/' &&
810                     strncmp(path, df_path, df_pathlen) == 0) {
811                         output(o, 3,
812                                _("Removing %s to make room for subdirectory\n"),
813                                df_path);
814                         unlink(df_path);
815                         unsorted_string_list_delete_item(&o->df_conflict_file_set,
816                                                          i, 0);
817                         break;
818                 }
819         }
820
821         /* Make sure leading directories are created */
822         status = safe_create_leading_directories_const(path);
823         if (status) {
824                 if (status == SCLD_EXISTS)
825                         /* something else exists */
826                         return err(o, msg, path, _(": perhaps a D/F conflict?"));
827                 return err(o, msg, path, "");
828         }
829
830         /*
831          * Do not unlink a file in the work tree if we are not
832          * tracking it.
833          */
834         if (would_lose_untracked(path))
835                 return err(o, _("refusing to lose untracked file at '%s'"),
836                              path);
837
838         /* Successful unlink is good.. */
839         if (!unlink(path))
840                 return 0;
841         /* .. and so is no existing file */
842         if (errno == ENOENT)
843                 return 0;
844         /* .. but not some other error (who really cares what?) */
845         return err(o, msg, path, _(": perhaps a D/F conflict?"));
846 }
847
848 static int update_file_flags(struct merge_options *o,
849                              const struct object_id *oid,
850                              unsigned mode,
851                              const char *path,
852                              int update_cache,
853                              int update_wd)
854 {
855         int ret = 0;
856
857         if (o->call_depth)
858                 update_wd = 0;
859
860         if (update_wd) {
861                 enum object_type type;
862                 void *buf;
863                 unsigned long size;
864
865                 if (S_ISGITLINK(mode)) {
866                         /*
867                          * We may later decide to recursively descend into
868                          * the submodule directory and update its index
869                          * and/or work tree, but we do not do that now.
870                          */
871                         update_wd = 0;
872                         goto update_index;
873                 }
874
875                 buf = read_object_file(oid, &type, &size);
876                 if (!buf)
877                         return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
878                 if (type != OBJ_BLOB) {
879                         ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
880                         goto free_buf;
881                 }
882                 if (S_ISREG(mode)) {
883                         struct strbuf strbuf = STRBUF_INIT;
884                         if (convert_to_working_tree(path, buf, size, &strbuf)) {
885                                 free(buf);
886                                 size = strbuf.len;
887                                 buf = strbuf_detach(&strbuf, NULL);
888                         }
889                 }
890
891                 if (make_room_for_path(o, path) < 0) {
892                         update_wd = 0;
893                         goto free_buf;
894                 }
895                 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
896                         int fd;
897                         if (mode & 0100)
898                                 mode = 0777;
899                         else
900                                 mode = 0666;
901                         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
902                         if (fd < 0) {
903                                 ret = err(o, _("failed to open '%s': %s"),
904                                           path, strerror(errno));
905                                 goto free_buf;
906                         }
907                         write_in_full(fd, buf, size);
908                         close(fd);
909                 } else if (S_ISLNK(mode)) {
910                         char *lnk = xmemdupz(buf, size);
911                         safe_create_leading_directories_const(path);
912                         unlink(path);
913                         if (symlink(lnk, path))
914                                 ret = err(o, _("failed to symlink '%s': %s"),
915                                         path, strerror(errno));
916                         free(lnk);
917                 } else
918                         ret = err(o,
919                                   _("do not know what to do with %06o %s '%s'"),
920                                   mode, oid_to_hex(oid), path);
921  free_buf:
922                 free(buf);
923         }
924  update_index:
925         if (!ret && update_cache)
926                 add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
927         return ret;
928 }
929
930 static int update_file(struct merge_options *o,
931                        int clean,
932                        const struct object_id *oid,
933                        unsigned mode,
934                        const char *path)
935 {
936         return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
937 }
938
939 /* Low level file merging, update and removal */
940
941 struct merge_file_info {
942         struct object_id oid;
943         unsigned mode;
944         unsigned clean:1,
945                  merge:1;
946 };
947
948 static int merge_3way(struct merge_options *o,
949                       mmbuffer_t *result_buf,
950                       const struct diff_filespec *one,
951                       const struct diff_filespec *a,
952                       const struct diff_filespec *b,
953                       const char *branch1,
954                       const char *branch2)
955 {
956         mmfile_t orig, src1, src2;
957         struct ll_merge_options ll_opts = {0};
958         char *base_name, *name1, *name2;
959         int merge_status;
960
961         ll_opts.renormalize = o->renormalize;
962         ll_opts.xdl_opts = o->xdl_opts;
963
964         if (o->call_depth) {
965                 ll_opts.virtual_ancestor = 1;
966                 ll_opts.variant = 0;
967         } else {
968                 switch (o->recursive_variant) {
969                 case MERGE_RECURSIVE_OURS:
970                         ll_opts.variant = XDL_MERGE_FAVOR_OURS;
971                         break;
972                 case MERGE_RECURSIVE_THEIRS:
973                         ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
974                         break;
975                 default:
976                         ll_opts.variant = 0;
977                         break;
978                 }
979         }
980
981         if (strcmp(a->path, b->path) ||
982             (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
983                 base_name = o->ancestor == NULL ? NULL :
984                         mkpathdup("%s:%s", o->ancestor, one->path);
985                 name1 = mkpathdup("%s:%s", branch1, a->path);
986                 name2 = mkpathdup("%s:%s", branch2, b->path);
987         } else {
988                 base_name = o->ancestor == NULL ? NULL :
989                         mkpathdup("%s", o->ancestor);
990                 name1 = mkpathdup("%s", branch1);
991                 name2 = mkpathdup("%s", branch2);
992         }
993
994         read_mmblob(&orig, &one->oid);
995         read_mmblob(&src1, &a->oid);
996         read_mmblob(&src2, &b->oid);
997
998         merge_status = ll_merge(result_buf, a->path, &orig, base_name,
999                                 &src1, name1, &src2, name2, &ll_opts);
1000
1001         free(base_name);
1002         free(name1);
1003         free(name2);
1004         free(orig.ptr);
1005         free(src1.ptr);
1006         free(src2.ptr);
1007         return merge_status;
1008 }
1009
1010 static int merge_file_1(struct merge_options *o,
1011                                            const struct diff_filespec *one,
1012                                            const struct diff_filespec *a,
1013                                            const struct diff_filespec *b,
1014                                            const char *branch1,
1015                                            const char *branch2,
1016                                            struct merge_file_info *result)
1017 {
1018         result->merge = 0;
1019         result->clean = 1;
1020
1021         if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
1022                 result->clean = 0;
1023                 if (S_ISREG(a->mode)) {
1024                         result->mode = a->mode;
1025                         oidcpy(&result->oid, &a->oid);
1026                 } else {
1027                         result->mode = b->mode;
1028                         oidcpy(&result->oid, &b->oid);
1029                 }
1030         } else {
1031                 if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
1032                         result->merge = 1;
1033
1034                 /*
1035                  * Merge modes
1036                  */
1037                 if (a->mode == b->mode || a->mode == one->mode)
1038                         result->mode = b->mode;
1039                 else {
1040                         result->mode = a->mode;
1041                         if (b->mode != one->mode) {
1042                                 result->clean = 0;
1043                                 result->merge = 1;
1044                         }
1045                 }
1046
1047                 if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
1048                         oidcpy(&result->oid, &b->oid);
1049                 else if (oid_eq(&b->oid, &one->oid))
1050                         oidcpy(&result->oid, &a->oid);
1051                 else if (S_ISREG(a->mode)) {
1052                         mmbuffer_t result_buf;
1053                         int ret = 0, merge_status;
1054
1055                         merge_status = merge_3way(o, &result_buf, one, a, b,
1056                                                   branch1, branch2);
1057
1058                         if ((merge_status < 0) || !result_buf.ptr)
1059                                 ret = err(o, _("Failed to execute internal merge"));
1060
1061                         if (!ret &&
1062                             write_object_file(result_buf.ptr, result_buf.size,
1063                                               blob_type, &result->oid))
1064                                 ret = err(o, _("Unable to add %s to database"),
1065                                           a->path);
1066
1067                         free(result_buf.ptr);
1068                         if (ret)
1069                                 return ret;
1070                         result->clean = (merge_status == 0);
1071                 } else if (S_ISGITLINK(a->mode)) {
1072                         result->clean = merge_submodule(&result->oid,
1073                                                        one->path,
1074                                                        &one->oid,
1075                                                        &a->oid,
1076                                                        &b->oid,
1077                                                        !o->call_depth);
1078                 } else if (S_ISLNK(a->mode)) {
1079                         switch (o->recursive_variant) {
1080                         case MERGE_RECURSIVE_NORMAL:
1081                                 oidcpy(&result->oid, &a->oid);
1082                                 if (!oid_eq(&a->oid, &b->oid))
1083                                         result->clean = 0;
1084                                 break;
1085                         case MERGE_RECURSIVE_OURS:
1086                                 oidcpy(&result->oid, &a->oid);
1087                                 break;
1088                         case MERGE_RECURSIVE_THEIRS:
1089                                 oidcpy(&result->oid, &b->oid);
1090                                 break;
1091                         }
1092                 } else
1093                         die("BUG: unsupported object type in the tree");
1094         }
1095
1096         return 0;
1097 }
1098
1099 static int merge_file_special_markers(struct merge_options *o,
1100                            const struct diff_filespec *one,
1101                            const struct diff_filespec *a,
1102                            const struct diff_filespec *b,
1103                            const char *branch1,
1104                            const char *filename1,
1105                            const char *branch2,
1106                            const char *filename2,
1107                            struct merge_file_info *mfi)
1108 {
1109         char *side1 = NULL;
1110         char *side2 = NULL;
1111         int ret;
1112
1113         if (filename1)
1114                 side1 = xstrfmt("%s:%s", branch1, filename1);
1115         if (filename2)
1116                 side2 = xstrfmt("%s:%s", branch2, filename2);
1117
1118         ret = merge_file_1(o, one, a, b,
1119                            side1 ? side1 : branch1,
1120                            side2 ? side2 : branch2, mfi);
1121         free(side1);
1122         free(side2);
1123         return ret;
1124 }
1125
1126 static int merge_file_one(struct merge_options *o,
1127                                          const char *path,
1128                                          const struct object_id *o_oid, int o_mode,
1129                                          const struct object_id *a_oid, int a_mode,
1130                                          const struct object_id *b_oid, int b_mode,
1131                                          const char *branch1,
1132                                          const char *branch2,
1133                                          struct merge_file_info *mfi)
1134 {
1135         struct diff_filespec one, a, b;
1136
1137         one.path = a.path = b.path = (char *)path;
1138         oidcpy(&one.oid, o_oid);
1139         one.mode = o_mode;
1140         oidcpy(&a.oid, a_oid);
1141         a.mode = a_mode;
1142         oidcpy(&b.oid, b_oid);
1143         b.mode = b_mode;
1144         return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
1145 }
1146
1147 static int conflict_rename_dir(struct merge_options *o,
1148                                struct diff_filepair *pair,
1149                                const char *rename_branch,
1150                                const char *other_branch)
1151 {
1152         const struct diff_filespec *dest = pair->two;
1153
1154         if (update_file(o, 1, &dest->oid, dest->mode, dest->path))
1155                 return -1;
1156         return 0;
1157 }
1158
1159 static int handle_change_delete(struct merge_options *o,
1160                                  const char *path, const char *old_path,
1161                                  const struct object_id *o_oid, int o_mode,
1162                                  const struct object_id *changed_oid,
1163                                  int changed_mode,
1164                                  const char *change_branch,
1165                                  const char *delete_branch,
1166                                  const char *change, const char *change_past)
1167 {
1168         char *alt_path = NULL;
1169         const char *update_path = path;
1170         int ret = 0;
1171
1172         if (dir_in_way(path, !o->call_depth, 0)) {
1173                 update_path = alt_path = unique_path(o, path, change_branch);
1174         }
1175
1176         if (o->call_depth) {
1177                 /*
1178                  * We cannot arbitrarily accept either a_sha or b_sha as
1179                  * correct; since there is no true "middle point" between
1180                  * them, simply reuse the base version for virtual merge base.
1181                  */
1182                 ret = remove_file_from_cache(path);
1183                 if (!ret)
1184                         ret = update_file(o, 0, o_oid, o_mode, update_path);
1185         } else {
1186                 if (!alt_path) {
1187                         if (!old_path) {
1188                                 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1189                                        "and %s in %s. Version %s of %s left in tree."),
1190                                        change, path, delete_branch, change_past,
1191                                        change_branch, change_branch, path);
1192                         } else {
1193                                 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1194                                        "and %s to %s in %s. Version %s of %s left in tree."),
1195                                        change, old_path, delete_branch, change_past, path,
1196                                        change_branch, change_branch, path);
1197                         }
1198                 } else {
1199                         if (!old_path) {
1200                                 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1201                                        "and %s in %s. Version %s of %s left in tree at %s."),
1202                                        change, path, delete_branch, change_past,
1203                                        change_branch, change_branch, path, alt_path);
1204                         } else {
1205                                 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1206                                        "and %s to %s in %s. Version %s of %s left in tree at %s."),
1207                                        change, old_path, delete_branch, change_past, path,
1208                                        change_branch, change_branch, path, alt_path);
1209                         }
1210                 }
1211                 /*
1212                  * No need to call update_file() on path when change_branch ==
1213                  * o->branch1 && !alt_path, since that would needlessly touch
1214                  * path.  We could call update_file_flags() with update_cache=0
1215                  * and update_wd=0, but that's a no-op.
1216                  */
1217                 if (change_branch != o->branch1 || alt_path)
1218                         ret = update_file(o, 0, changed_oid, changed_mode, update_path);
1219         }
1220         free(alt_path);
1221
1222         return ret;
1223 }
1224
1225 static int conflict_rename_delete(struct merge_options *o,
1226                                    struct diff_filepair *pair,
1227                                    const char *rename_branch,
1228                                    const char *delete_branch)
1229 {
1230         const struct diff_filespec *orig = pair->one;
1231         const struct diff_filespec *dest = pair->two;
1232
1233         if (handle_change_delete(o,
1234                                  o->call_depth ? orig->path : dest->path,
1235                                  o->call_depth ? NULL : orig->path,
1236                                  &orig->oid, orig->mode,
1237                                  &dest->oid, dest->mode,
1238                                  rename_branch, delete_branch,
1239                                  _("rename"), _("renamed")))
1240                 return -1;
1241
1242         if (o->call_depth)
1243                 return remove_file_from_cache(dest->path);
1244         else
1245                 return update_stages(o, dest->path, NULL,
1246                                      rename_branch == o->branch1 ? dest : NULL,
1247                                      rename_branch == o->branch1 ? NULL : dest);
1248 }
1249
1250 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
1251                                                  struct stage_data *entry,
1252                                                  int stage)
1253 {
1254         struct object_id *oid = &entry->stages[stage].oid;
1255         unsigned mode = entry->stages[stage].mode;
1256         if (mode == 0 || is_null_oid(oid))
1257                 return NULL;
1258         oidcpy(&target->oid, oid);
1259         target->mode = mode;
1260         return target;
1261 }
1262
1263 static int handle_file(struct merge_options *o,
1264                         struct diff_filespec *rename,
1265                         int stage,
1266                         struct rename_conflict_info *ci)
1267 {
1268         char *dst_name = rename->path;
1269         struct stage_data *dst_entry;
1270         const char *cur_branch, *other_branch;
1271         struct diff_filespec other;
1272         struct diff_filespec *add;
1273         int ret;
1274
1275         if (stage == 2) {
1276                 dst_entry = ci->dst_entry1;
1277                 cur_branch = ci->branch1;
1278                 other_branch = ci->branch2;
1279         } else {
1280                 dst_entry = ci->dst_entry2;
1281                 cur_branch = ci->branch2;
1282                 other_branch = ci->branch1;
1283         }
1284
1285         add = filespec_from_entry(&other, dst_entry, stage ^ 1);
1286         if (add) {
1287                 char *add_name = unique_path(o, rename->path, other_branch);
1288                 if (update_file(o, 0, &add->oid, add->mode, add_name))
1289                         return -1;
1290
1291                 remove_file(o, 0, rename->path, 0);
1292                 dst_name = unique_path(o, rename->path, cur_branch);
1293         } else {
1294                 if (dir_in_way(rename->path, !o->call_depth, 0)) {
1295                         dst_name = unique_path(o, rename->path, cur_branch);
1296                         output(o, 1, _("%s is a directory in %s adding as %s instead"),
1297                                rename->path, other_branch, dst_name);
1298                 }
1299         }
1300         if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
1301                 ; /* fall through, do allow dst_name to be released */
1302         else if (stage == 2)
1303                 ret = update_stages(o, rename->path, NULL, rename, add);
1304         else
1305                 ret = update_stages(o, rename->path, NULL, add, rename);
1306
1307         if (dst_name != rename->path)
1308                 free(dst_name);
1309
1310         return ret;
1311 }
1312
1313 static int conflict_rename_rename_1to2(struct merge_options *o,
1314                                         struct rename_conflict_info *ci)
1315 {
1316         /* One file was renamed in both branches, but to different names. */
1317         struct diff_filespec *one = ci->pair1->one;
1318         struct diff_filespec *a = ci->pair1->two;
1319         struct diff_filespec *b = ci->pair2->two;
1320
1321         output(o, 1, _("CONFLICT (rename/rename): "
1322                "Rename \"%s\"->\"%s\" in branch \"%s\" "
1323                "rename \"%s\"->\"%s\" in \"%s\"%s"),
1324                one->path, a->path, ci->branch1,
1325                one->path, b->path, ci->branch2,
1326                o->call_depth ? _(" (left unresolved)") : "");
1327         if (o->call_depth) {
1328                 struct merge_file_info mfi;
1329                 struct diff_filespec other;
1330                 struct diff_filespec *add;
1331                 if (merge_file_one(o, one->path,
1332                                  &one->oid, one->mode,
1333                                  &a->oid, a->mode,
1334                                  &b->oid, b->mode,
1335                                  ci->branch1, ci->branch2, &mfi))
1336                         return -1;
1337
1338                 /*
1339                  * FIXME: For rename/add-source conflicts (if we could detect
1340                  * such), this is wrong.  We should instead find a unique
1341                  * pathname and then either rename the add-source file to that
1342                  * unique path, or use that unique path instead of src here.
1343                  */
1344                 if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
1345                         return -1;
1346
1347                 /*
1348                  * Above, we put the merged content at the merge-base's
1349                  * path.  Now we usually need to delete both a->path and
1350                  * b->path.  However, the rename on each side of the merge
1351                  * could also be involved in a rename/add conflict.  In
1352                  * such cases, we should keep the added file around,
1353                  * resolving the conflict at that path in its favor.
1354                  */
1355                 add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
1356                 if (add) {
1357                         if (update_file(o, 0, &add->oid, add->mode, a->path))
1358                                 return -1;
1359                 }
1360                 else
1361                         remove_file_from_cache(a->path);
1362                 add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
1363                 if (add) {
1364                         if (update_file(o, 0, &add->oid, add->mode, b->path))
1365                                 return -1;
1366                 }
1367                 else
1368                         remove_file_from_cache(b->path);
1369         } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
1370                 return -1;
1371
1372         return 0;
1373 }
1374
1375 static int conflict_rename_rename_2to1(struct merge_options *o,
1376                                         struct rename_conflict_info *ci)
1377 {
1378         /* Two files, a & b, were renamed to the same thing, c. */
1379         struct diff_filespec *a = ci->pair1->one;
1380         struct diff_filespec *b = ci->pair2->one;
1381         struct diff_filespec *c1 = ci->pair1->two;
1382         struct diff_filespec *c2 = ci->pair2->two;
1383         char *path = c1->path; /* == c2->path */
1384         struct merge_file_info mfi_c1;
1385         struct merge_file_info mfi_c2;
1386         int ret;
1387
1388         output(o, 1, _("CONFLICT (rename/rename): "
1389                "Rename %s->%s in %s. "
1390                "Rename %s->%s in %s"),
1391                a->path, c1->path, ci->branch1,
1392                b->path, c2->path, ci->branch2);
1393
1394         remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
1395         remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
1396
1397         if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
1398                                        o->branch1, c1->path,
1399                                        o->branch2, ci->ren1_other.path, &mfi_c1) ||
1400             merge_file_special_markers(o, b, &ci->ren2_other, c2,
1401                                        o->branch1, ci->ren2_other.path,
1402                                        o->branch2, c2->path, &mfi_c2))
1403                 return -1;
1404
1405         if (o->call_depth) {
1406                 /*
1407                  * If mfi_c1.clean && mfi_c2.clean, then it might make
1408                  * sense to do a two-way merge of those results.  But, I
1409                  * think in all cases, it makes sense to have the virtual
1410                  * merge base just undo the renames; they can be detected
1411                  * again later for the non-recursive merge.
1412                  */
1413                 remove_file(o, 0, path, 0);
1414                 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
1415                 if (!ret)
1416                         ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1417                                           b->path);
1418         } else {
1419                 char *new_path1 = unique_path(o, path, ci->branch1);
1420                 char *new_path2 = unique_path(o, path, ci->branch2);
1421                 output(o, 1, _("Renaming %s to %s and %s to %s instead"),
1422                        a->path, new_path1, b->path, new_path2);
1423                 remove_file(o, 0, path, 0);
1424                 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
1425                 if (!ret)
1426                         ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1427                                           new_path2);
1428                 /*
1429                  * unpack_trees() actually populates the index for us for
1430                  * "normal" rename/rename(2to1) situtations so that the
1431                  * correct entries are at the higher stages, which would
1432                  * make the call below to update_stages_for_stage_data
1433                  * unnecessary.  However, if either of the renames came
1434                  * from a directory rename, then unpack_trees() will not
1435                  * have gotten the right data loaded into the index, so we
1436                  * need to do so now.  (While it'd be tempting to move this
1437                  * call to update_stages_for_stage_data() to
1438                  * apply_directory_rename_modifications(), that would break
1439                  * our intermediate calls to would_lose_untracked() since
1440                  * those rely on the current in-memory index.  See also the
1441                  * big "NOTE" in update_stages()).
1442                  */
1443                 if (update_stages_for_stage_data(o, path, ci->dst_entry1))
1444                         ret = -1;
1445
1446                 free(new_path2);
1447                 free(new_path1);
1448         }
1449
1450         return ret;
1451 }
1452
1453 /*
1454  * Get the diff_filepairs changed between o_tree and tree.
1455  */
1456 static struct diff_queue_struct *get_diffpairs(struct merge_options *o,
1457                                                struct tree *o_tree,
1458                                                struct tree *tree)
1459 {
1460         struct diff_queue_struct *ret;
1461         struct diff_options opts;
1462
1463         diff_setup(&opts);
1464         opts.flags.recursive = 1;
1465         opts.flags.rename_empty = 0;
1466         opts.detect_rename = DIFF_DETECT_RENAME;
1467         opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
1468                             o->diff_rename_limit >= 0 ? o->diff_rename_limit :
1469                             1000;
1470         opts.rename_score = o->rename_score;
1471         opts.show_rename_progress = o->show_rename_progress;
1472         opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1473         diff_setup_done(&opts);
1474         diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
1475         diffcore_std(&opts);
1476         if (opts.needed_rename_limit > o->needed_rename_limit)
1477                 o->needed_rename_limit = opts.needed_rename_limit;
1478
1479         ret = xmalloc(sizeof(*ret));
1480         *ret = diff_queued_diff;
1481
1482         opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1483         diff_queued_diff.nr = 0;
1484         diff_queued_diff.queue = NULL;
1485         diff_flush(&opts);
1486         return ret;
1487 }
1488
1489 static int tree_has_path(struct tree *tree, const char *path)
1490 {
1491         struct object_id hashy;
1492         unsigned int mode_o;
1493
1494         return !get_tree_entry(&tree->object.oid, path,
1495                                &hashy, &mode_o);
1496 }
1497
1498 /*
1499  * Return a new string that replaces the beginning portion (which matches
1500  * entry->dir), with entry->new_dir.  In perl-speak:
1501  *   new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1502  * NOTE:
1503  *   Caller must ensure that old_path starts with entry->dir + '/'.
1504  */
1505 static char *apply_dir_rename(struct dir_rename_entry *entry,
1506                               const char *old_path)
1507 {
1508         struct strbuf new_path = STRBUF_INIT;
1509         int oldlen, newlen;
1510
1511         if (entry->non_unique_new_dir)
1512                 return NULL;
1513
1514         oldlen = strlen(entry->dir);
1515         newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
1516         strbuf_grow(&new_path, newlen);
1517         strbuf_addbuf(&new_path, &entry->new_dir);
1518         strbuf_addstr(&new_path, &old_path[oldlen]);
1519
1520         return strbuf_detach(&new_path, NULL);
1521 }
1522
1523 static void get_renamed_dir_portion(const char *old_path, const char *new_path,
1524                                     char **old_dir, char **new_dir)
1525 {
1526         char *end_of_old, *end_of_new;
1527         int old_len, new_len;
1528
1529         *old_dir = NULL;
1530         *new_dir = NULL;
1531
1532         /*
1533          * For
1534          *    "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1535          * the "e/foo.c" part is the same, we just want to know that
1536          *    "a/b/c/d" was renamed to "a/b/some/thing/else"
1537          * so, for this example, this function returns "a/b/c/d" in
1538          * *old_dir and "a/b/some/thing/else" in *new_dir.
1539          *
1540          * Also, if the basename of the file changed, we don't care.  We
1541          * want to know which portion of the directory, if any, changed.
1542          */
1543         end_of_old = strrchr(old_path, '/');
1544         end_of_new = strrchr(new_path, '/');
1545
1546         if (end_of_old == NULL || end_of_new == NULL)
1547                 return;
1548         while (*--end_of_new == *--end_of_old &&
1549                end_of_old != old_path &&
1550                end_of_new != new_path)
1551                 ; /* Do nothing; all in the while loop */
1552         /*
1553          * We've found the first non-matching character in the directory
1554          * paths.  That means the current directory we were comparing
1555          * represents the rename.  Move end_of_old and end_of_new back
1556          * to the full directory name.
1557          */
1558         if (*end_of_old == '/')
1559                 end_of_old++;
1560         if (*end_of_old != '/')
1561                 end_of_new++;
1562         end_of_old = strchr(end_of_old, '/');
1563         end_of_new = strchr(end_of_new, '/');
1564
1565         /*
1566          * It may have been the case that old_path and new_path were the same
1567          * directory all along.  Don't claim a rename if they're the same.
1568          */
1569         old_len = end_of_old - old_path;
1570         new_len = end_of_new - new_path;
1571
1572         if (old_len != new_len || strncmp(old_path, new_path, old_len)) {
1573                 *old_dir = xstrndup(old_path, old_len);
1574                 *new_dir = xstrndup(new_path, new_len);
1575         }
1576 }
1577
1578 static void remove_hashmap_entries(struct hashmap *dir_renames,
1579                                    struct string_list *items_to_remove)
1580 {
1581         int i;
1582         struct dir_rename_entry *entry;
1583
1584         for (i = 0; i < items_to_remove->nr; i++) {
1585                 entry = items_to_remove->items[i].util;
1586                 hashmap_remove(dir_renames, entry, NULL);
1587         }
1588         string_list_clear(items_to_remove, 0);
1589 }
1590
1591 /*
1592  * See if there is a directory rename for path, and if there are any file
1593  * level conflicts for the renamed location.  If there is a rename and
1594  * there are no conflicts, return the new name.  Otherwise, return NULL.
1595  */
1596 static char *handle_path_level_conflicts(struct merge_options *o,
1597                                          const char *path,
1598                                          struct dir_rename_entry *entry,
1599                                          struct hashmap *collisions,
1600                                          struct tree *tree)
1601 {
1602         char *new_path = NULL;
1603         struct collision_entry *collision_ent;
1604         int clean = 1;
1605         struct strbuf collision_paths = STRBUF_INIT;
1606
1607         /*
1608          * entry has the mapping of old directory name to new directory name
1609          * that we want to apply to path.
1610          */
1611         new_path = apply_dir_rename(entry, path);
1612
1613         if (!new_path) {
1614                 /* This should only happen when entry->non_unique_new_dir set */
1615                 if (!entry->non_unique_new_dir)
1616                         BUG("entry->non_unqiue_dir not set and !new_path");
1617                 output(o, 1, _("CONFLICT (directory rename split): "
1618                                "Unclear where to place %s because directory "
1619                                "%s was renamed to multiple other directories, "
1620                                "with no destination getting a majority of the "
1621                                "files."),
1622                        path, entry->dir);
1623                 clean = 0;
1624                 return NULL;
1625         }
1626
1627         /*
1628          * The caller needs to have ensured that it has pre-populated
1629          * collisions with all paths that map to new_path.  Do a quick check
1630          * to ensure that's the case.
1631          */
1632         collision_ent = collision_find_entry(collisions, new_path);
1633         if (collision_ent == NULL)
1634                 BUG("collision_ent is NULL");
1635
1636         /*
1637          * Check for one-sided add/add/.../add conflicts, i.e.
1638          * where implicit renames from the other side doing
1639          * directory rename(s) can affect this side of history
1640          * to put multiple paths into the same location.  Warn
1641          * and bail on directory renames for such paths.
1642          */
1643         if (collision_ent->reported_already) {
1644                 clean = 0;
1645         } else if (tree_has_path(tree, new_path)) {
1646                 collision_ent->reported_already = 1;
1647                 strbuf_add_separated_string_list(&collision_paths, ", ",
1648                                                  &collision_ent->source_files);
1649                 output(o, 1, _("CONFLICT (implicit dir rename): Existing "
1650                                "file/dir at %s in the way of implicit "
1651                                "directory rename(s) putting the following "
1652                                "path(s) there: %s."),
1653                        new_path, collision_paths.buf);
1654                 clean = 0;
1655         } else if (collision_ent->source_files.nr > 1) {
1656                 collision_ent->reported_already = 1;
1657                 strbuf_add_separated_string_list(&collision_paths, ", ",
1658                                                  &collision_ent->source_files);
1659                 output(o, 1, _("CONFLICT (implicit dir rename): Cannot map "
1660                                "more than one path to %s; implicit directory "
1661                                "renames tried to put these paths there: %s"),
1662                        new_path, collision_paths.buf);
1663                 clean = 0;
1664         }
1665
1666         /* Free memory we no longer need */
1667         strbuf_release(&collision_paths);
1668         if (!clean && new_path) {
1669                 free(new_path);
1670                 return NULL;
1671         }
1672
1673         return new_path;
1674 }
1675
1676 /*
1677  * There are a couple things we want to do at the directory level:
1678  *   1. Check for both sides renaming to the same thing, in order to avoid
1679  *      implicit renaming of files that should be left in place.  (See
1680  *      testcase 6b in t6043 for details.)
1681  *   2. Prune directory renames if there are still files left in the
1682  *      the original directory.  These represent a partial directory rename,
1683  *      i.e. a rename where only some of the files within the directory
1684  *      were renamed elsewhere.  (Technically, this could be done earlier
1685  *      in get_directory_renames(), except that would prevent us from
1686  *      doing the previous check and thus failing testcase 6b.)
1687  *   3. Check for rename/rename(1to2) conflicts (at the directory level).
1688  *      In the future, we could potentially record this info as well and
1689  *      omit reporting rename/rename(1to2) conflicts for each path within
1690  *      the affected directories, thus cleaning up the merge output.
1691  *   NOTE: We do NOT check for rename/rename(2to1) conflicts at the
1692  *         directory level, because merging directories is fine.  If it
1693  *         causes conflicts for files within those merged directories, then
1694  *         that should be detected at the individual path level.
1695  */
1696 static void handle_directory_level_conflicts(struct merge_options *o,
1697                                              struct hashmap *dir_re_head,
1698                                              struct tree *head,
1699                                              struct hashmap *dir_re_merge,
1700                                              struct tree *merge)
1701 {
1702         struct hashmap_iter iter;
1703         struct dir_rename_entry *head_ent;
1704         struct dir_rename_entry *merge_ent;
1705
1706         struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
1707         struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
1708
1709         hashmap_iter_init(dir_re_head, &iter);
1710         while ((head_ent = hashmap_iter_next(&iter))) {
1711                 merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);
1712                 if (merge_ent &&
1713                     !head_ent->non_unique_new_dir &&
1714                     !merge_ent->non_unique_new_dir &&
1715                     !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {
1716                         /* 1. Renamed identically; remove it from both sides */
1717                         string_list_append(&remove_from_head,
1718                                            head_ent->dir)->util = head_ent;
1719                         strbuf_release(&head_ent->new_dir);
1720                         string_list_append(&remove_from_merge,
1721                                            merge_ent->dir)->util = merge_ent;
1722                         strbuf_release(&merge_ent->new_dir);
1723                 } else if (tree_has_path(head, head_ent->dir)) {
1724                         /* 2. This wasn't a directory rename after all */
1725                         string_list_append(&remove_from_head,
1726                                            head_ent->dir)->util = head_ent;
1727                         strbuf_release(&head_ent->new_dir);
1728                 }
1729         }
1730
1731         remove_hashmap_entries(dir_re_head, &remove_from_head);
1732         remove_hashmap_entries(dir_re_merge, &remove_from_merge);
1733
1734         hashmap_iter_init(dir_re_merge, &iter);
1735         while ((merge_ent = hashmap_iter_next(&iter))) {
1736                 head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
1737                 if (tree_has_path(merge, merge_ent->dir)) {
1738                         /* 2. This wasn't a directory rename after all */
1739                         string_list_append(&remove_from_merge,
1740                                            merge_ent->dir)->util = merge_ent;
1741                 } else if (head_ent &&
1742                            !head_ent->non_unique_new_dir &&
1743                            !merge_ent->non_unique_new_dir) {
1744                         /* 3. rename/rename(1to2) */
1745                         /*
1746                          * We can assume it's not rename/rename(1to1) because
1747                          * that was case (1), already checked above.  So we
1748                          * know that head_ent->new_dir and merge_ent->new_dir
1749                          * are different strings.
1750                          */
1751                         output(o, 1, _("CONFLICT (rename/rename): "
1752                                        "Rename directory %s->%s in %s. "
1753                                        "Rename directory %s->%s in %s"),
1754                                head_ent->dir, head_ent->new_dir.buf, o->branch1,
1755                                head_ent->dir, merge_ent->new_dir.buf, o->branch2);
1756                         string_list_append(&remove_from_head,
1757                                            head_ent->dir)->util = head_ent;
1758                         strbuf_release(&head_ent->new_dir);
1759                         string_list_append(&remove_from_merge,
1760                                            merge_ent->dir)->util = merge_ent;
1761                         strbuf_release(&merge_ent->new_dir);
1762                 }
1763         }
1764
1765         remove_hashmap_entries(dir_re_head, &remove_from_head);
1766         remove_hashmap_entries(dir_re_merge, &remove_from_merge);
1767 }
1768
1769 static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,
1770                                              struct tree *tree)
1771 {
1772         struct hashmap *dir_renames;
1773         struct hashmap_iter iter;
1774         struct dir_rename_entry *entry;
1775         int i;
1776
1777         /*
1778          * Typically, we think of a directory rename as all files from a
1779          * certain directory being moved to a target directory.  However,
1780          * what if someone first moved two files from the original
1781          * directory in one commit, and then renamed the directory
1782          * somewhere else in a later commit?  At merge time, we just know
1783          * that files from the original directory went to two different
1784          * places, and that the bulk of them ended up in the same place.
1785          * We want each directory rename to represent where the bulk of the
1786          * files from that directory end up; this function exists to find
1787          * where the bulk of the files went.
1788          *
1789          * The first loop below simply iterates through the list of file
1790          * renames, finding out how often each directory rename pair
1791          * possibility occurs.
1792          */
1793         dir_renames = xmalloc(sizeof(*dir_renames));
1794         dir_rename_init(dir_renames);
1795         for (i = 0; i < pairs->nr; ++i) {
1796                 struct string_list_item *item;
1797                 int *count;
1798                 struct diff_filepair *pair = pairs->queue[i];
1799                 char *old_dir, *new_dir;
1800
1801                 /* File not part of directory rename if it wasn't renamed */
1802                 if (pair->status != 'R')
1803                         continue;
1804
1805                 get_renamed_dir_portion(pair->one->path, pair->two->path,
1806                                         &old_dir,        &new_dir);
1807                 if (!old_dir)
1808                         /* Directory didn't change at all; ignore this one. */
1809                         continue;
1810
1811                 entry = dir_rename_find_entry(dir_renames, old_dir);
1812                 if (!entry) {
1813                         entry = xmalloc(sizeof(*entry));
1814                         dir_rename_entry_init(entry, old_dir);
1815                         hashmap_put(dir_renames, entry);
1816                 } else {
1817                         free(old_dir);
1818                 }
1819                 item = string_list_lookup(&entry->possible_new_dirs, new_dir);
1820                 if (!item) {
1821                         item = string_list_insert(&entry->possible_new_dirs,
1822                                                   new_dir);
1823                         item->util = xcalloc(1, sizeof(int));
1824                 } else {
1825                         free(new_dir);
1826                 }
1827                 count = item->util;
1828                 *count += 1;
1829         }
1830
1831         /*
1832          * For each directory with files moved out of it, we find out which
1833          * target directory received the most files so we can declare it to
1834          * be the "winning" target location for the directory rename.  This
1835          * winner gets recorded in new_dir.  If there is no winner
1836          * (multiple target directories received the same number of files),
1837          * we set non_unique_new_dir.  Once we've determined the winner (or
1838          * that there is no winner), we no longer need possible_new_dirs.
1839          */
1840         hashmap_iter_init(dir_renames, &iter);
1841         while ((entry = hashmap_iter_next(&iter))) {
1842                 int max = 0;
1843                 int bad_max = 0;
1844                 char *best = NULL;
1845
1846                 for (i = 0; i < entry->possible_new_dirs.nr; i++) {
1847                         int *count = entry->possible_new_dirs.items[i].util;
1848
1849                         if (*count == max)
1850                                 bad_max = max;
1851                         else if (*count > max) {
1852                                 max = *count;
1853                                 best = entry->possible_new_dirs.items[i].string;
1854                         }
1855                 }
1856                 if (bad_max == max)
1857                         entry->non_unique_new_dir = 1;
1858                 else {
1859                         assert(entry->new_dir.len == 0);
1860                         strbuf_addstr(&entry->new_dir, best);
1861                 }
1862                 /*
1863                  * The relevant directory sub-portion of the original full
1864                  * filepaths were xstrndup'ed before inserting into
1865                  * possible_new_dirs, and instead of manually iterating the
1866                  * list and free'ing each, just lie and tell
1867                  * possible_new_dirs that it did the strdup'ing so that it
1868                  * will free them for us.
1869                  */
1870                 entry->possible_new_dirs.strdup_strings = 1;
1871                 string_list_clear(&entry->possible_new_dirs, 1);
1872         }
1873
1874         return dir_renames;
1875 }
1876
1877 static struct dir_rename_entry *check_dir_renamed(const char *path,
1878                                                   struct hashmap *dir_renames)
1879 {
1880         char temp[PATH_MAX];
1881         char *end;
1882         struct dir_rename_entry *entry;
1883
1884         strcpy(temp, path);
1885         while ((end = strrchr(temp, '/'))) {
1886                 *end = '\0';
1887                 entry = dir_rename_find_entry(dir_renames, temp);
1888                 if (entry)
1889                         return entry;
1890         }
1891         return NULL;
1892 }
1893
1894 static void compute_collisions(struct hashmap *collisions,
1895                                struct hashmap *dir_renames,
1896                                struct diff_queue_struct *pairs)
1897 {
1898         int i;
1899
1900         /*
1901          * Multiple files can be mapped to the same path due to directory
1902          * renames done by the other side of history.  Since that other
1903          * side of history could have merged multiple directories into one,
1904          * if our side of history added the same file basename to each of
1905          * those directories, then all N of them would get implicitly
1906          * renamed by the directory rename detection into the same path,
1907          * and we'd get an add/add/.../add conflict, and all those adds
1908          * from *this* side of history.  This is not representable in the
1909          * index, and users aren't going to easily be able to make sense of
1910          * it.  So we need to provide a good warning about what's
1911          * happening, and fall back to no-directory-rename detection
1912          * behavior for those paths.
1913          *
1914          * See testcases 9e and all of section 5 from t6043 for examples.
1915          */
1916         collision_init(collisions);
1917
1918         for (i = 0; i < pairs->nr; ++i) {
1919                 struct dir_rename_entry *dir_rename_ent;
1920                 struct collision_entry *collision_ent;
1921                 char *new_path;
1922                 struct diff_filepair *pair = pairs->queue[i];
1923
1924                 if (pair->status == 'D')
1925                         continue;
1926                 dir_rename_ent = check_dir_renamed(pair->two->path,
1927                                                    dir_renames);
1928                 if (!dir_rename_ent)
1929                         continue;
1930
1931                 new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
1932                 if (!new_path)
1933                         /*
1934                          * dir_rename_ent->non_unique_new_path is true, which
1935                          * means there is no directory rename for us to use,
1936                          * which means it won't cause us any additional
1937                          * collisions.
1938                          */
1939                         continue;
1940                 collision_ent = collision_find_entry(collisions, new_path);
1941                 if (!collision_ent) {
1942                         collision_ent = xcalloc(1,
1943                                                 sizeof(struct collision_entry));
1944                         hashmap_entry_init(collision_ent, strhash(new_path));
1945                         hashmap_put(collisions, collision_ent);
1946                         collision_ent->target_file = new_path;
1947                 } else {
1948                         free(new_path);
1949                 }
1950                 string_list_insert(&collision_ent->source_files,
1951                                    pair->two->path);
1952         }
1953 }
1954
1955 static char *check_for_directory_rename(struct merge_options *o,
1956                                         const char *path,
1957                                         struct tree *tree,
1958                                         struct hashmap *dir_renames,
1959                                         struct hashmap *dir_rename_exclusions,
1960                                         struct hashmap *collisions,
1961                                         int *clean_merge)
1962 {
1963         char *new_path = NULL;
1964         struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
1965         struct dir_rename_entry *oentry = NULL;
1966
1967         if (!entry)
1968                 return new_path;
1969
1970         /*
1971          * This next part is a little weird.  We do not want to do an
1972          * implicit rename into a directory we renamed on our side, because
1973          * that will result in a spurious rename/rename(1to2) conflict.  An
1974          * example:
1975          *   Base commit: dumbdir/afile, otherdir/bfile
1976          *   Side 1:      smrtdir/afile, otherdir/bfile
1977          *   Side 2:      dumbdir/afile, dumbdir/bfile
1978          * Here, while working on Side 1, we could notice that otherdir was
1979          * renamed/merged to dumbdir, and change the diff_filepair for
1980          * otherdir/bfile into a rename into dumbdir/bfile.  However, Side
1981          * 2 will notice the rename from dumbdir to smrtdir, and do the
1982          * transitive rename to move it from dumbdir/bfile to
1983          * smrtdir/bfile.  That gives us bfile in dumbdir vs being in
1984          * smrtdir, a rename/rename(1to2) conflict.  We really just want
1985          * the file to end up in smrtdir.  And the way to achieve that is
1986          * to not let Side1 do the rename to dumbdir, since we know that is
1987          * the source of one of our directory renames.
1988          *
1989          * That's why oentry and dir_rename_exclusions is here.
1990          *
1991          * As it turns out, this also prevents N-way transient rename
1992          * confusion; See testcases 9c and 9d of t6043.
1993          */
1994         oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
1995         if (oentry) {
1996                 output(o, 1, _("WARNING: Avoiding applying %s -> %s rename "
1997                                "to %s, because %s itself was renamed."),
1998                        entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
1999         } else {
2000                 new_path = handle_path_level_conflicts(o, path, entry,
2001                                                        collisions, tree);
2002                 *clean_merge &= (new_path != NULL);
2003         }
2004
2005         return new_path;
2006 }
2007
2008 static void apply_directory_rename_modifications(struct merge_options *o,
2009                                                  struct diff_filepair *pair,
2010                                                  char *new_path,
2011                                                  struct rename *re,
2012                                                  struct tree *tree,
2013                                                  struct tree *o_tree,
2014                                                  struct tree *a_tree,
2015                                                  struct tree *b_tree,
2016                                                  struct string_list *entries,
2017                                                  int *clean)
2018 {
2019         struct string_list_item *item;
2020         int stage = (tree == a_tree ? 2 : 3);
2021
2022         /*
2023          * In all cases where we can do directory rename detection,
2024          * unpack_trees() will have read pair->two->path into the
2025          * index and the working copy.  We need to remove it so that
2026          * we can instead place it at new_path.  It is guaranteed to
2027          * not be untracked (unpack_trees() would have errored out
2028          * saying the file would have been overwritten), but it might
2029          * be dirty, though.
2030          */
2031         remove_file(o, 1, pair->two->path, 0 /* no_wd */);
2032
2033         /* Find or create a new re->dst_entry */
2034         item = string_list_lookup(entries, new_path);
2035         if (item) {
2036                 /*
2037                  * Since we're renaming on this side of history, and it's
2038                  * due to a directory rename on the other side of history
2039                  * (which we only allow when the directory in question no
2040                  * longer exists on the other side of history), the
2041                  * original entry for re->dst_entry is no longer
2042                  * necessary...
2043                  */
2044                 re->dst_entry->processed = 1;
2045
2046                 /*
2047                  * ...because we'll be using this new one.
2048                  */
2049                 re->dst_entry = item->util;
2050         } else {
2051                 /*
2052                  * re->dst_entry is for the before-dir-rename path, and we
2053                  * need it to hold information for the after-dir-rename
2054                  * path.  Before creating a new entry, we need to mark the
2055                  * old one as unnecessary (...unless it is shared by
2056                  * src_entry, i.e. this didn't use to be a rename, in which
2057                  * case we can just allow the normal processing to happen
2058                  * for it).
2059                  */
2060                 if (pair->status == 'R')
2061                         re->dst_entry->processed = 1;
2062
2063                 re->dst_entry = insert_stage_data(new_path,
2064                                                   o_tree, a_tree, b_tree,
2065                                                   entries);
2066                 item = string_list_insert(entries, new_path);
2067                 item->util = re->dst_entry;
2068         }
2069
2070         /*
2071          * Update the stage_data with the information about the path we are
2072          * moving into place.  That slot will be empty and available for us
2073          * to write to because of the collision checks in
2074          * handle_path_level_conflicts().  In other words,
2075          * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2076          * open for us to write to.
2077          *
2078          * It may be tempting to actually update the index at this point as
2079          * well, using update_stages_for_stage_data(), but as per the big
2080          * "NOTE" in update_stages(), doing so will modify the current
2081          * in-memory index which will break calls to would_lose_untracked()
2082          * that we need to make.  Instead, we need to just make sure that
2083          * the various conflict_rename_*() functions update the index
2084          * explicitly rather than relying on unpack_trees() to have done it.
2085          */
2086         get_tree_entry(&tree->object.oid,
2087                        pair->two->path,
2088                        &re->dst_entry->stages[stage].oid,
2089                        &re->dst_entry->stages[stage].mode);
2090
2091         /* Update pair status */
2092         if (pair->status == 'A') {
2093                 /*
2094                  * Recording rename information for this add makes it look
2095                  * like a rename/delete conflict.  Make sure we can
2096                  * correctly handle this as an add that was moved to a new
2097                  * directory instead of reporting a rename/delete conflict.
2098                  */
2099                 re->add_turned_into_rename = 1;
2100         }
2101         /*
2102          * We don't actually look at pair->status again, but it seems
2103          * pedagogically correct to adjust it.
2104          */
2105         pair->status = 'R';
2106
2107         /*
2108          * Finally, record the new location.
2109          */
2110         pair->two->path = new_path;
2111 }
2112
2113 /*
2114  * Get information of all renames which occurred in 'pairs', making use of
2115  * any implicit directory renames inferred from the other side of history.
2116  * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2117  * to be able to associate the correct cache entries with the rename
2118  * information; tree is always equal to either a_tree or b_tree.
2119  */
2120 static struct string_list *get_renames(struct merge_options *o,
2121                                        struct diff_queue_struct *pairs,
2122                                        struct hashmap *dir_renames,
2123                                        struct hashmap *dir_rename_exclusions,
2124                                        struct tree *tree,
2125                                        struct tree *o_tree,
2126                                        struct tree *a_tree,
2127                                        struct tree *b_tree,
2128                                        struct string_list *entries,
2129                                        int *clean_merge)
2130 {
2131         int i;
2132         struct hashmap collisions;
2133         struct hashmap_iter iter;
2134         struct collision_entry *e;
2135         struct string_list *renames;
2136
2137         compute_collisions(&collisions, dir_renames, pairs);
2138         renames = xcalloc(1, sizeof(struct string_list));
2139
2140         for (i = 0; i < pairs->nr; ++i) {
2141                 struct string_list_item *item;
2142                 struct rename *re;
2143                 struct diff_filepair *pair = pairs->queue[i];
2144                 char *new_path; /* non-NULL only with directory renames */
2145
2146                 if (pair->status == 'D') {
2147                         diff_free_filepair(pair);
2148                         continue;
2149                 }
2150                 new_path = check_for_directory_rename(o, pair->two->path, tree,
2151                                                       dir_renames,
2152                                                       dir_rename_exclusions,
2153                                                       &collisions,
2154                                                       clean_merge);
2155                 if (pair->status != 'R' && !new_path) {
2156                         diff_free_filepair(pair);
2157                         continue;
2158                 }
2159
2160                 re = xmalloc(sizeof(*re));
2161                 re->processed = 0;
2162                 re->add_turned_into_rename = 0;
2163                 re->pair = pair;
2164                 item = string_list_lookup(entries, re->pair->one->path);
2165                 if (!item)
2166                         re->src_entry = insert_stage_data(re->pair->one->path,
2167                                         o_tree, a_tree, b_tree, entries);
2168                 else
2169                         re->src_entry = item->util;
2170
2171                 item = string_list_lookup(entries, re->pair->two->path);
2172                 if (!item)
2173                         re->dst_entry = insert_stage_data(re->pair->two->path,
2174                                         o_tree, a_tree, b_tree, entries);
2175                 else
2176                         re->dst_entry = item->util;
2177                 item = string_list_insert(renames, pair->one->path);
2178                 item->util = re;
2179                 if (new_path)
2180                         apply_directory_rename_modifications(o, pair, new_path,
2181                                                              re, tree, o_tree,
2182                                                              a_tree, b_tree,
2183                                                              entries,
2184                                                              clean_merge);
2185         }
2186
2187         hashmap_iter_init(&collisions, &iter);
2188         while ((e = hashmap_iter_next(&iter))) {
2189                 free(e->target_file);
2190                 string_list_clear(&e->source_files, 0);
2191         }
2192         hashmap_free(&collisions, 1);
2193         return renames;
2194 }
2195
2196 static int process_renames(struct merge_options *o,
2197                            struct string_list *a_renames,
2198                            struct string_list *b_renames)
2199 {
2200         int clean_merge = 1, i, j;
2201         struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
2202         struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
2203         const struct rename *sre;
2204
2205         for (i = 0; i < a_renames->nr; i++) {
2206                 sre = a_renames->items[i].util;
2207                 string_list_insert(&a_by_dst, sre->pair->two->path)->util
2208                         = (void *)sre;
2209         }
2210         for (i = 0; i < b_renames->nr; i++) {
2211                 sre = b_renames->items[i].util;
2212                 string_list_insert(&b_by_dst, sre->pair->two->path)->util
2213                         = (void *)sre;
2214         }
2215
2216         for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
2217                 struct string_list *renames1, *renames2Dst;
2218                 struct rename *ren1 = NULL, *ren2 = NULL;
2219                 const char *branch1, *branch2;
2220                 const char *ren1_src, *ren1_dst;
2221                 struct string_list_item *lookup;
2222
2223                 if (i >= a_renames->nr) {
2224                         ren2 = b_renames->items[j++].util;
2225                 } else if (j >= b_renames->nr) {
2226                         ren1 = a_renames->items[i++].util;
2227                 } else {
2228                         int compare = strcmp(a_renames->items[i].string,
2229                                              b_renames->items[j].string);
2230                         if (compare <= 0)
2231                                 ren1 = a_renames->items[i++].util;
2232                         if (compare >= 0)
2233                                 ren2 = b_renames->items[j++].util;
2234                 }
2235
2236                 /* TODO: refactor, so that 1/2 are not needed */
2237                 if (ren1) {
2238                         renames1 = a_renames;
2239                         renames2Dst = &b_by_dst;
2240                         branch1 = o->branch1;
2241                         branch2 = o->branch2;
2242                 } else {
2243                         renames1 = b_renames;
2244                         renames2Dst = &a_by_dst;
2245                         branch1 = o->branch2;
2246                         branch2 = o->branch1;
2247                         SWAP(ren2, ren1);
2248                 }
2249
2250                 if (ren1->processed)
2251                         continue;
2252                 ren1->processed = 1;
2253                 ren1->dst_entry->processed = 1;
2254                 /* BUG: We should only mark src_entry as processed if we
2255                  * are not dealing with a rename + add-source case.
2256                  */
2257                 ren1->src_entry->processed = 1;
2258
2259                 ren1_src = ren1->pair->one->path;
2260                 ren1_dst = ren1->pair->two->path;
2261
2262                 if (ren2) {
2263                         /* One file renamed on both sides */
2264                         const char *ren2_src = ren2->pair->one->path;
2265                         const char *ren2_dst = ren2->pair->two->path;
2266                         enum rename_type rename_type;
2267                         if (strcmp(ren1_src, ren2_src) != 0)
2268                                 die("BUG: ren1_src != ren2_src");
2269                         ren2->dst_entry->processed = 1;
2270                         ren2->processed = 1;
2271                         if (strcmp(ren1_dst, ren2_dst) != 0) {
2272                                 rename_type = RENAME_ONE_FILE_TO_TWO;
2273                                 clean_merge = 0;
2274                         } else {
2275                                 rename_type = RENAME_ONE_FILE_TO_ONE;
2276                                 /* BUG: We should only remove ren1_src in
2277                                  * the base stage (think of rename +
2278                                  * add-source cases).
2279                                  */
2280                                 remove_file(o, 1, ren1_src, 1);
2281                                 update_entry(ren1->dst_entry,
2282                                              ren1->pair->one,
2283                                              ren1->pair->two,
2284                                              ren2->pair->two);
2285                         }
2286                         setup_rename_conflict_info(rename_type,
2287                                                    ren1->pair,
2288                                                    ren2->pair,
2289                                                    branch1,
2290                                                    branch2,
2291                                                    ren1->dst_entry,
2292                                                    ren2->dst_entry,
2293                                                    o,
2294                                                    NULL,
2295                                                    NULL);
2296                 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
2297                         /* Two different files renamed to the same thing */
2298                         char *ren2_dst;
2299                         ren2 = lookup->util;
2300                         ren2_dst = ren2->pair->two->path;
2301                         if (strcmp(ren1_dst, ren2_dst) != 0)
2302                                 die("BUG: ren1_dst != ren2_dst");
2303
2304                         clean_merge = 0;
2305                         ren2->processed = 1;
2306                         /*
2307                          * BUG: We should only mark src_entry as processed
2308                          * if we are not dealing with a rename + add-source
2309                          * case.
2310                          */
2311                         ren2->src_entry->processed = 1;
2312
2313                         setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
2314                                                    ren1->pair,
2315                                                    ren2->pair,
2316                                                    branch1,
2317                                                    branch2,
2318                                                    ren1->dst_entry,
2319                                                    ren2->dst_entry,
2320                                                    o,
2321                                                    ren1->src_entry,
2322                                                    ren2->src_entry);
2323
2324                 } else {
2325                         /* Renamed in 1, maybe changed in 2 */
2326                         /* we only use sha1 and mode of these */
2327                         struct diff_filespec src_other, dst_other;
2328                         int try_merge;
2329
2330                         /*
2331                          * unpack_trees loads entries from common-commit
2332                          * into stage 1, from head-commit into stage 2, and
2333                          * from merge-commit into stage 3.  We keep track
2334                          * of which side corresponds to the rename.
2335                          */
2336                         int renamed_stage = a_renames == renames1 ? 2 : 3;
2337                         int other_stage =   a_renames == renames1 ? 3 : 2;
2338
2339                         /* BUG: We should only remove ren1_src in the base
2340                          * stage and in other_stage (think of rename +
2341                          * add-source case).
2342                          */
2343                         remove_file(o, 1, ren1_src,
2344                                     renamed_stage == 2 || !was_tracked(ren1_src));
2345
2346                         oidcpy(&src_other.oid,
2347                                &ren1->src_entry->stages[other_stage].oid);
2348                         src_other.mode = ren1->src_entry->stages[other_stage].mode;
2349                         oidcpy(&dst_other.oid,
2350                                &ren1->dst_entry->stages[other_stage].oid);
2351                         dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
2352                         try_merge = 0;
2353
2354                         if (oid_eq(&src_other.oid, &null_oid) &&
2355                             ren1->add_turned_into_rename) {
2356                                 setup_rename_conflict_info(RENAME_DIR,
2357                                                            ren1->pair,
2358                                                            NULL,
2359                                                            branch1,
2360                                                            branch2,
2361                                                            ren1->dst_entry,
2362                                                            NULL,
2363                                                            o,
2364                                                            NULL,
2365                                                            NULL);
2366                         } else if (oid_eq(&src_other.oid, &null_oid)) {
2367                                 setup_rename_conflict_info(RENAME_DELETE,
2368                                                            ren1->pair,
2369                                                            NULL,
2370                                                            branch1,
2371                                                            branch2,
2372                                                            ren1->dst_entry,
2373                                                            NULL,
2374                                                            o,
2375                                                            NULL,
2376                                                            NULL);
2377                         } else if ((dst_other.mode == ren1->pair->two->mode) &&
2378                                    oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
2379                                 /*
2380                                  * Added file on the other side identical to
2381                                  * the file being renamed: clean merge.
2382                                  * Also, there is no need to overwrite the
2383                                  * file already in the working copy, so call
2384                                  * update_file_flags() instead of
2385                                  * update_file().
2386                                  */
2387                                 if (update_file_flags(o,
2388                                                       &ren1->pair->two->oid,
2389                                                       ren1->pair->two->mode,
2390                                                       ren1_dst,
2391                                                       1, /* update_cache */
2392                                                       0  /* update_wd    */))
2393                                         clean_merge = -1;
2394                         } else if (!oid_eq(&dst_other.oid, &null_oid)) {
2395                                 clean_merge = 0;
2396                                 try_merge = 1;
2397                                 output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
2398                                        "%s added in %s"),
2399                                        ren1_src, ren1_dst, branch1,
2400                                        ren1_dst, branch2);
2401                                 if (o->call_depth) {
2402                                         struct merge_file_info mfi;
2403                                         if (merge_file_one(o, ren1_dst, &null_oid, 0,
2404                                                            &ren1->pair->two->oid,
2405                                                            ren1->pair->two->mode,
2406                                                            &dst_other.oid,
2407                                                            dst_other.mode,
2408                                                            branch1, branch2, &mfi)) {
2409                                                 clean_merge = -1;
2410                                                 goto cleanup_and_return;
2411                                         }
2412                                         output(o, 1, _("Adding merged %s"), ren1_dst);
2413                                         if (update_file(o, 0, &mfi.oid,
2414                                                         mfi.mode, ren1_dst))
2415                                                 clean_merge = -1;
2416                                         try_merge = 0;
2417                                 } else {
2418                                         char *new_path = unique_path(o, ren1_dst, branch2);
2419                                         output(o, 1, _("Adding as %s instead"), new_path);
2420                                         if (update_file(o, 0, &dst_other.oid,
2421                                                         dst_other.mode, new_path))
2422                                                 clean_merge = -1;
2423                                         free(new_path);
2424                                 }
2425                         } else
2426                                 try_merge = 1;
2427
2428                         if (clean_merge < 0)
2429                                 goto cleanup_and_return;
2430                         if (try_merge) {
2431                                 struct diff_filespec *one, *a, *b;
2432                                 src_other.path = (char *)ren1_src;
2433
2434                                 one = ren1->pair->one;
2435                                 if (a_renames == renames1) {
2436                                         a = ren1->pair->two;
2437                                         b = &src_other;
2438                                 } else {
2439                                         b = ren1->pair->two;
2440                                         a = &src_other;
2441                                 }
2442                                 update_entry(ren1->dst_entry, one, a, b);
2443                                 setup_rename_conflict_info(RENAME_NORMAL,
2444                                                            ren1->pair,
2445                                                            NULL,
2446                                                            branch1,
2447                                                            NULL,
2448                                                            ren1->dst_entry,
2449                                                            NULL,
2450                                                            o,
2451                                                            NULL,
2452                                                            NULL);
2453                         }
2454                 }
2455         }
2456 cleanup_and_return:
2457         string_list_clear(&a_by_dst, 0);
2458         string_list_clear(&b_by_dst, 0);
2459
2460         return clean_merge;
2461 }
2462
2463 struct rename_info {
2464         struct string_list *head_renames;
2465         struct string_list *merge_renames;
2466 };
2467
2468 static void initial_cleanup_rename(struct diff_queue_struct *pairs,
2469                                    struct hashmap *dir_renames)
2470 {
2471         struct hashmap_iter iter;
2472         struct dir_rename_entry *e;
2473
2474         hashmap_iter_init(dir_renames, &iter);
2475         while ((e = hashmap_iter_next(&iter))) {
2476                 free(e->dir);
2477                 strbuf_release(&e->new_dir);
2478                 /* possible_new_dirs already cleared in get_directory_renames */
2479         }
2480         hashmap_free(dir_renames, 1);
2481         free(dir_renames);
2482
2483         free(pairs->queue);
2484         free(pairs);
2485 }
2486
2487 static int handle_renames(struct merge_options *o,
2488                           struct tree *common,
2489                           struct tree *head,
2490                           struct tree *merge,
2491                           struct string_list *entries,
2492                           struct rename_info *ri)
2493 {
2494         struct diff_queue_struct *head_pairs, *merge_pairs;
2495         struct hashmap *dir_re_head, *dir_re_merge;
2496         int clean = 1;
2497
2498         ri->head_renames = NULL;
2499         ri->merge_renames = NULL;
2500
2501         if (!o->detect_rename)
2502                 return 1;
2503
2504         head_pairs = get_diffpairs(o, common, head);
2505         merge_pairs = get_diffpairs(o, common, merge);
2506
2507         dir_re_head = get_directory_renames(head_pairs, head);
2508         dir_re_merge = get_directory_renames(merge_pairs, merge);
2509
2510         handle_directory_level_conflicts(o,
2511                                          dir_re_head, head,
2512                                          dir_re_merge, merge);
2513
2514         ri->head_renames  = get_renames(o, head_pairs,
2515                                         dir_re_merge, dir_re_head, head,
2516                                         common, head, merge, entries,
2517                                         &clean);
2518         if (clean < 0)
2519                 goto cleanup;
2520         ri->merge_renames = get_renames(o, merge_pairs,
2521                                         dir_re_head, dir_re_merge, merge,
2522                                         common, head, merge, entries,
2523                                         &clean);
2524         if (clean < 0)
2525                 goto cleanup;
2526         clean &= process_renames(o, ri->head_renames, ri->merge_renames);
2527
2528 cleanup:
2529         /*
2530          * Some cleanup is deferred until cleanup_renames() because the
2531          * data structures are still needed and referenced in
2532          * process_entry().  But there are a few things we can free now.
2533          */
2534         initial_cleanup_rename(head_pairs, dir_re_head);
2535         initial_cleanup_rename(merge_pairs, dir_re_merge);
2536
2537         return clean;
2538 }
2539
2540 static void final_cleanup_rename(struct string_list *rename)
2541 {
2542         const struct rename *re;
2543         int i;
2544
2545         if (rename == NULL)
2546                 return;
2547
2548         for (i = 0; i < rename->nr; i++) {
2549                 re = rename->items[i].util;
2550                 diff_free_filepair(re->pair);
2551         }
2552         string_list_clear(rename, 1);
2553         free(rename);
2554 }
2555
2556 static void final_cleanup_renames(struct rename_info *re_info)
2557 {
2558         final_cleanup_rename(re_info->head_renames);
2559         final_cleanup_rename(re_info->merge_renames);
2560 }
2561
2562 static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
2563 {
2564         return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
2565 }
2566
2567 static int read_oid_strbuf(struct merge_options *o,
2568         const struct object_id *oid, struct strbuf *dst)
2569 {
2570         void *buf;
2571         enum object_type type;
2572         unsigned long size;
2573         buf = read_object_file(oid, &type, &size);
2574         if (!buf)
2575                 return err(o, _("cannot read object %s"), oid_to_hex(oid));
2576         if (type != OBJ_BLOB) {
2577                 free(buf);
2578                 return err(o, _("object %s is not a blob"), oid_to_hex(oid));
2579         }
2580         strbuf_attach(dst, buf, size, size + 1);
2581         return 0;
2582 }
2583
2584 static int blob_unchanged(struct merge_options *opt,
2585                           const struct object_id *o_oid,
2586                           unsigned o_mode,
2587                           const struct object_id *a_oid,
2588                           unsigned a_mode,
2589                           int renormalize, const char *path)
2590 {
2591         struct strbuf o = STRBUF_INIT;
2592         struct strbuf a = STRBUF_INIT;
2593         int ret = 0; /* assume changed for safety */
2594
2595         if (a_mode != o_mode)
2596                 return 0;
2597         if (oid_eq(o_oid, a_oid))
2598                 return 1;
2599         if (!renormalize)
2600                 return 0;
2601
2602         assert(o_oid && a_oid);
2603         if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
2604                 goto error_return;
2605         /*
2606          * Note: binary | is used so that both renormalizations are
2607          * performed.  Comparison can be skipped if both files are
2608          * unchanged since their sha1s have already been compared.
2609          */
2610         if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) |
2611             renormalize_buffer(&the_index, path, a.buf, a.len, &a))
2612                 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
2613
2614 error_return:
2615         strbuf_release(&o);
2616         strbuf_release(&a);
2617         return ret;
2618 }
2619
2620 static int handle_modify_delete(struct merge_options *o,
2621                                  const char *path,
2622                                  struct object_id *o_oid, int o_mode,
2623                                  struct object_id *a_oid, int a_mode,
2624                                  struct object_id *b_oid, int b_mode)
2625 {
2626         const char *modify_branch, *delete_branch;
2627         struct object_id *changed_oid;
2628         int changed_mode;
2629
2630         if (a_oid) {
2631                 modify_branch = o->branch1;
2632                 delete_branch = o->branch2;
2633                 changed_oid = a_oid;
2634                 changed_mode = a_mode;
2635         } else {
2636                 modify_branch = o->branch2;
2637                 delete_branch = o->branch1;
2638                 changed_oid = b_oid;
2639                 changed_mode = b_mode;
2640         }
2641
2642         return handle_change_delete(o,
2643                                     path, NULL,
2644                                     o_oid, o_mode,
2645                                     changed_oid, changed_mode,
2646                                     modify_branch, delete_branch,
2647                                     _("modify"), _("modified"));
2648 }
2649
2650 static int merge_content(struct merge_options *o,
2651                          const char *path,
2652                          struct object_id *o_oid, int o_mode,
2653                          struct object_id *a_oid, int a_mode,
2654                          struct object_id *b_oid, int b_mode,
2655                          struct rename_conflict_info *rename_conflict_info)
2656 {
2657         const char *reason = _("content");
2658         const char *path1 = NULL, *path2 = NULL;
2659         struct merge_file_info mfi;
2660         struct diff_filespec one, a, b;
2661         unsigned df_conflict_remains = 0;
2662
2663         if (!o_oid) {
2664                 reason = _("add/add");
2665                 o_oid = (struct object_id *)&null_oid;
2666         }
2667         one.path = a.path = b.path = (char *)path;
2668         oidcpy(&one.oid, o_oid);
2669         one.mode = o_mode;
2670         oidcpy(&a.oid, a_oid);
2671         a.mode = a_mode;
2672         oidcpy(&b.oid, b_oid);
2673         b.mode = b_mode;
2674
2675         if (rename_conflict_info) {
2676                 struct diff_filepair *pair1 = rename_conflict_info->pair1;
2677
2678                 path1 = (o->branch1 == rename_conflict_info->branch1) ?
2679                         pair1->two->path : pair1->one->path;
2680                 /* If rename_conflict_info->pair2 != NULL, we are in
2681                  * RENAME_ONE_FILE_TO_ONE case.  Otherwise, we have a
2682                  * normal rename.
2683                  */
2684                 path2 = (rename_conflict_info->pair2 ||
2685                          o->branch2 == rename_conflict_info->branch1) ?
2686                         pair1->two->path : pair1->one->path;
2687
2688                 if (dir_in_way(path, !o->call_depth,
2689                                S_ISGITLINK(pair1->two->mode)))
2690                         df_conflict_remains = 1;
2691         }
2692         if (merge_file_special_markers(o, &one, &a, &b,
2693                                        o->branch1, path1,
2694                                        o->branch2, path2, &mfi))
2695                 return -1;
2696
2697         if (mfi.clean && !df_conflict_remains &&
2698             oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
2699                 int path_renamed_outside_HEAD;
2700                 output(o, 3, _("Skipped %s (merged same as existing)"), path);
2701                 /*
2702                  * The content merge resulted in the same file contents we
2703                  * already had.  We can return early if those file contents
2704                  * are recorded at the correct path (which may not be true
2705                  * if the merge involves a rename).
2706                  */
2707                 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
2708                 if (!path_renamed_outside_HEAD) {
2709                         add_cacheinfo(o, mfi.mode, &mfi.oid, path,
2710                                       0, (!o->call_depth), 0);
2711                         return mfi.clean;
2712                 }
2713         } else
2714                 output(o, 2, _("Auto-merging %s"), path);
2715
2716         if (!mfi.clean) {
2717                 if (S_ISGITLINK(mfi.mode))
2718                         reason = _("submodule");
2719                 output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
2720                                 reason, path);
2721                 if (rename_conflict_info && !df_conflict_remains)
2722                         if (update_stages(o, path, &one, &a, &b))
2723                                 return -1;
2724         }
2725
2726         if (df_conflict_remains) {
2727                 char *new_path;
2728                 if (o->call_depth) {
2729                         remove_file_from_cache(path);
2730                 } else {
2731                         if (!mfi.clean) {
2732                                 if (update_stages(o, path, &one, &a, &b))
2733                                         return -1;
2734                         } else {
2735                                 int file_from_stage2 = was_tracked(path);
2736                                 struct diff_filespec merged;
2737                                 oidcpy(&merged.oid, &mfi.oid);
2738                                 merged.mode = mfi.mode;
2739
2740                                 if (update_stages(o, path, NULL,
2741                                                   file_from_stage2 ? &merged : NULL,
2742                                                   file_from_stage2 ? NULL : &merged))
2743                                         return -1;
2744                         }
2745
2746                 }
2747                 new_path = unique_path(o, path, rename_conflict_info->branch1);
2748                 output(o, 1, _("Adding as %s instead"), new_path);
2749                 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
2750                         free(new_path);
2751                         return -1;
2752                 }
2753                 free(new_path);
2754                 mfi.clean = 0;
2755         } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
2756                 return -1;
2757         return mfi.clean;
2758 }
2759
2760 /* Per entry merge function */
2761 static int process_entry(struct merge_options *o,
2762                          const char *path, struct stage_data *entry)
2763 {
2764         int clean_merge = 1;
2765         int normalize = o->renormalize;
2766         unsigned o_mode = entry->stages[1].mode;
2767         unsigned a_mode = entry->stages[2].mode;
2768         unsigned b_mode = entry->stages[3].mode;
2769         struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
2770         struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
2771         struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
2772
2773         entry->processed = 1;
2774         if (entry->rename_conflict_info) {
2775                 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
2776                 switch (conflict_info->rename_type) {
2777                 case RENAME_NORMAL:
2778                 case RENAME_ONE_FILE_TO_ONE:
2779                         clean_merge = merge_content(o, path,
2780                                                     o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
2781                                                     conflict_info);
2782                         break;
2783                 case RENAME_DIR:
2784                         clean_merge = 1;
2785                         if (conflict_rename_dir(o,
2786                                                 conflict_info->pair1,
2787                                                 conflict_info->branch1,
2788                                                 conflict_info->branch2))
2789                                 clean_merge = -1;
2790                         break;
2791                 case RENAME_DELETE:
2792                         clean_merge = 0;
2793                         if (conflict_rename_delete(o,
2794                                                    conflict_info->pair1,
2795                                                    conflict_info->branch1,
2796                                                    conflict_info->branch2))
2797                                 clean_merge = -1;
2798                         break;
2799                 case RENAME_ONE_FILE_TO_TWO:
2800                         clean_merge = 0;
2801                         if (conflict_rename_rename_1to2(o, conflict_info))
2802                                 clean_merge = -1;
2803                         break;
2804                 case RENAME_TWO_FILES_TO_ONE:
2805                         clean_merge = 0;
2806                         if (conflict_rename_rename_2to1(o, conflict_info))
2807                                 clean_merge = -1;
2808                         break;
2809                 default:
2810                         entry->processed = 0;
2811                         break;
2812                 }
2813         } else if (o_oid && (!a_oid || !b_oid)) {
2814                 /* Case A: Deleted in one */
2815                 if ((!a_oid && !b_oid) ||
2816                     (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
2817                     (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
2818                         /* Deleted in both or deleted in one and
2819                          * unchanged in the other */
2820                         if (a_oid)
2821                                 output(o, 2, _("Removing %s"), path);
2822                         /* do not touch working file if it did not exist */
2823                         remove_file(o, 1, path, !a_oid);
2824                 } else {
2825                         /* Modify/delete; deleted side may have put a directory in the way */
2826                         clean_merge = 0;
2827                         if (handle_modify_delete(o, path, o_oid, o_mode,
2828                                                  a_oid, a_mode, b_oid, b_mode))
2829                                 clean_merge = -1;
2830                 }
2831         } else if ((!o_oid && a_oid && !b_oid) ||
2832                    (!o_oid && !a_oid && b_oid)) {
2833                 /* Case B: Added in one. */
2834                 /* [nothing|directory] -> ([nothing|directory], file) */
2835
2836                 const char *add_branch;
2837                 const char *other_branch;
2838                 unsigned mode;
2839                 const struct object_id *oid;
2840                 const char *conf;
2841
2842                 if (a_oid) {
2843                         add_branch = o->branch1;
2844                         other_branch = o->branch2;
2845                         mode = a_mode;
2846                         oid = a_oid;
2847                         conf = _("file/directory");
2848                 } else {
2849                         add_branch = o->branch2;
2850                         other_branch = o->branch1;
2851                         mode = b_mode;
2852                         oid = b_oid;
2853                         conf = _("directory/file");
2854                 }
2855                 if (dir_in_way(path,
2856                                !o->call_depth && !S_ISGITLINK(a_mode),
2857                                0)) {
2858                         char *new_path = unique_path(o, path, add_branch);
2859                         clean_merge = 0;
2860                         output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
2861                                "Adding %s as %s"),
2862                                conf, path, other_branch, path, new_path);
2863                         if (update_file(o, 0, oid, mode, new_path))
2864                                 clean_merge = -1;
2865                         else if (o->call_depth)
2866                                 remove_file_from_cache(path);
2867                         free(new_path);
2868                 } else {
2869                         output(o, 2, _("Adding %s"), path);
2870                         /* do not overwrite file if already present */
2871                         if (update_file_flags(o, oid, mode, path, 1, !a_oid))
2872                                 clean_merge = -1;
2873                 }
2874         } else if (a_oid && b_oid) {
2875                 /* Case C: Added in both (check for same permissions) and */
2876                 /* case D: Modified in both, but differently. */
2877                 clean_merge = merge_content(o, path,
2878                                             o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
2879                                             NULL);
2880         } else if (!o_oid && !a_oid && !b_oid) {
2881                 /*
2882                  * this entry was deleted altogether. a_mode == 0 means
2883                  * we had that path and want to actively remove it.
2884                  */
2885                 remove_file(o, 1, path, !a_mode);
2886         } else
2887                 die("BUG: fatal merge failure, shouldn't happen.");
2888
2889         return clean_merge;
2890 }
2891
2892 int merge_trees(struct merge_options *o,
2893                 struct tree *head,
2894                 struct tree *merge,
2895                 struct tree *common,
2896                 struct tree **result)
2897 {
2898         int code, clean;
2899
2900         if (o->subtree_shift) {
2901                 merge = shift_tree_object(head, merge, o->subtree_shift);
2902                 common = shift_tree_object(head, common, o->subtree_shift);
2903         }
2904
2905         if (oid_eq(&common->object.oid, &merge->object.oid)) {
2906                 struct strbuf sb = STRBUF_INIT;
2907
2908                 if (!o->call_depth && index_has_changes(&sb)) {
2909                         err(o, _("Dirty index: cannot merge (dirty: %s)"),
2910                             sb.buf);
2911                         return 0;
2912                 }
2913                 output(o, 0, _("Already up to date!"));
2914                 *result = head;
2915                 return 1;
2916         }
2917
2918         code = git_merge_trees(o->call_depth, common, head, merge);
2919
2920         if (code != 0) {
2921                 if (show(o, 4) || o->call_depth)
2922                         err(o, _("merging of trees %s and %s failed"),
2923                             oid_to_hex(&head->object.oid),
2924                             oid_to_hex(&merge->object.oid));
2925                 return -1;
2926         }
2927
2928         if (unmerged_cache()) {
2929                 struct string_list *entries;
2930                 struct rename_info re_info;
2931                 int i;
2932                 /*
2933                  * Only need the hashmap while processing entries, so
2934                  * initialize it here and free it when we are done running
2935                  * through the entries. Keeping it in the merge_options as
2936                  * opposed to decaring a local hashmap is for convenience
2937                  * so that we don't have to pass it to around.
2938                  */
2939                 hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512);
2940                 get_files_dirs(o, head);
2941                 get_files_dirs(o, merge);
2942
2943                 entries = get_unmerged();
2944                 clean = handle_renames(o, common, head, merge, entries,
2945                                        &re_info);
2946                 record_df_conflict_files(o, entries);
2947                 if (clean < 0)
2948                         goto cleanup;
2949                 for (i = entries->nr-1; 0 <= i; i--) {
2950                         const char *path = entries->items[i].string;
2951                         struct stage_data *e = entries->items[i].util;
2952                         if (!e->processed) {
2953                                 int ret = process_entry(o, path, e);
2954                                 if (!ret)
2955                                         clean = 0;
2956                                 else if (ret < 0) {
2957                                         clean = ret;
2958                                         goto cleanup;
2959                                 }
2960                         }
2961                 }
2962                 for (i = 0; i < entries->nr; i++) {
2963                         struct stage_data *e = entries->items[i].util;
2964                         if (!e->processed)
2965                                 die("BUG: unprocessed path??? %s",
2966                                     entries->items[i].string);
2967                 }
2968
2969 cleanup:
2970                 final_cleanup_renames(&re_info);
2971
2972                 string_list_clear(entries, 1);
2973                 free(entries);
2974
2975                 hashmap_free(&o->current_file_dir_set, 1);
2976
2977                 if (clean < 0)
2978                         return clean;
2979         }
2980         else
2981                 clean = 1;
2982
2983         if (o->call_depth && !(*result = write_tree_from_memory(o)))
2984                 return -1;
2985
2986         return clean;
2987 }
2988
2989 static struct commit_list *reverse_commit_list(struct commit_list *list)
2990 {
2991         struct commit_list *next = NULL, *current, *backup;
2992         for (current = list; current; current = backup) {
2993                 backup = current->next;
2994                 current->next = next;
2995                 next = current;
2996         }
2997         return next;
2998 }
2999
3000 /*
3001  * Merge the commits h1 and h2, return the resulting virtual
3002  * commit object and a flag indicating the cleanness of the merge.
3003  */
3004 int merge_recursive(struct merge_options *o,
3005                     struct commit *h1,
3006                     struct commit *h2,
3007                     struct commit_list *ca,
3008                     struct commit **result)
3009 {
3010         struct commit_list *iter;
3011         struct commit *merged_common_ancestors;
3012         struct tree *mrtree;
3013         int clean;
3014
3015         if (show(o, 4)) {
3016                 output(o, 4, _("Merging:"));
3017                 output_commit_title(o, h1);
3018                 output_commit_title(o, h2);
3019         }
3020
3021         if (!ca) {
3022                 ca = get_merge_bases(h1, h2);
3023                 ca = reverse_commit_list(ca);
3024         }
3025
3026         if (show(o, 5)) {
3027                 unsigned cnt = commit_list_count(ca);
3028
3029                 output(o, 5, Q_("found %u common ancestor:",
3030                                 "found %u common ancestors:", cnt), cnt);
3031                 for (iter = ca; iter; iter = iter->next)
3032                         output_commit_title(o, iter->item);
3033         }
3034
3035         merged_common_ancestors = pop_commit(&ca);
3036         if (merged_common_ancestors == NULL) {
3037                 /* if there is no common ancestor, use an empty tree */
3038                 struct tree *tree;
3039
3040                 tree = lookup_tree(the_hash_algo->empty_tree);
3041                 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
3042         }
3043
3044         for (iter = ca; iter; iter = iter->next) {
3045                 const char *saved_b1, *saved_b2;
3046                 o->call_depth++;
3047                 /*
3048                  * When the merge fails, the result contains files
3049                  * with conflict markers. The cleanness flag is
3050                  * ignored (unless indicating an error), it was never
3051                  * actually used, as result of merge_trees has always
3052                  * overwritten it: the committed "conflicts" were
3053                  * already resolved.
3054                  */
3055                 discard_cache();
3056                 saved_b1 = o->branch1;
3057                 saved_b2 = o->branch2;
3058                 o->branch1 = "Temporary merge branch 1";
3059                 o->branch2 = "Temporary merge branch 2";
3060                 if (merge_recursive(o, merged_common_ancestors, iter->item,
3061                                     NULL, &merged_common_ancestors) < 0)
3062                         return -1;
3063                 o->branch1 = saved_b1;
3064                 o->branch2 = saved_b2;
3065                 o->call_depth--;
3066
3067                 if (!merged_common_ancestors)
3068                         return err(o, _("merge returned no commit"));
3069         }
3070
3071         discard_cache();
3072         if (!o->call_depth)
3073                 read_cache();
3074
3075         o->ancestor = "merged common ancestors";
3076         clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
3077                             &mrtree);
3078         if (clean < 0) {
3079                 flush_output(o);
3080                 return clean;
3081         }
3082
3083         if (o->call_depth) {
3084                 *result = make_virtual_commit(mrtree, "merged tree");
3085                 commit_list_insert(h1, &(*result)->parents);
3086                 commit_list_insert(h2, &(*result)->parents->next);
3087         }
3088         flush_output(o);
3089         if (!o->call_depth && o->buffer_output < 2)
3090                 strbuf_release(&o->obuf);
3091         if (show(o, 2))
3092                 diff_warn_rename_limit("merge.renamelimit",
3093                                        o->needed_rename_limit, 0);
3094         return clean;
3095 }
3096
3097 static struct commit *get_ref(const struct object_id *oid, const char *name)
3098 {
3099         struct object *object;
3100
3101         object = deref_tag(parse_object(oid), name, strlen(name));
3102         if (!object)
3103                 return NULL;
3104         if (object->type == OBJ_TREE)
3105                 return make_virtual_commit((struct tree*)object, name);
3106         if (object->type != OBJ_COMMIT)
3107                 return NULL;
3108         if (parse_commit((struct commit *)object))
3109                 return NULL;
3110         return (struct commit *)object;
3111 }
3112
3113 int merge_recursive_generic(struct merge_options *o,
3114                             const struct object_id *head,
3115                             const struct object_id *merge,
3116                             int num_base_list,
3117                             const struct object_id **base_list,
3118                             struct commit **result)
3119 {
3120         int clean;
3121         struct lock_file lock = LOCK_INIT;
3122         struct commit *head_commit = get_ref(head, o->branch1);
3123         struct commit *next_commit = get_ref(merge, o->branch2);
3124         struct commit_list *ca = NULL;
3125
3126         if (base_list) {
3127                 int i;
3128                 for (i = 0; i < num_base_list; ++i) {
3129                         struct commit *base;
3130                         if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
3131                                 return err(o, _("Could not parse object '%s'"),
3132                                         oid_to_hex(base_list[i]));
3133                         commit_list_insert(base, &ca);
3134                 }
3135         }
3136
3137         hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
3138         clean = merge_recursive(o, head_commit, next_commit, ca,
3139                         result);
3140         if (clean < 0) {
3141                 rollback_lock_file(&lock);
3142                 return clean;
3143         }
3144
3145         if (write_locked_index(&the_index, &lock,
3146                                COMMIT_LOCK | SKIP_IF_UNCHANGED))
3147                 return err(o, _("Unable to write index."));
3148
3149         return clean ? 0 : 1;
3150 }
3151
3152 static void merge_recursive_config(struct merge_options *o)
3153 {
3154         git_config_get_int("merge.verbosity", &o->verbosity);
3155         git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
3156         git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
3157         git_config(git_xmerge_config, NULL);
3158 }
3159
3160 void init_merge_options(struct merge_options *o)
3161 {
3162         const char *merge_verbosity;
3163         memset(o, 0, sizeof(struct merge_options));
3164         o->verbosity = 2;
3165         o->buffer_output = 1;
3166         o->diff_rename_limit = -1;
3167         o->merge_rename_limit = -1;
3168         o->renormalize = 0;
3169         o->detect_rename = 1;
3170         merge_recursive_config(o);
3171         merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
3172         if (merge_verbosity)
3173                 o->verbosity = strtol(merge_verbosity, NULL, 10);
3174         if (o->verbosity >= 5)
3175                 o->buffer_output = 0;
3176         strbuf_init(&o->obuf, 0);
3177         string_list_init(&o->df_conflict_file_set, 1);
3178 }
3179
3180 int parse_merge_opt(struct merge_options *o, const char *s)
3181 {
3182         const char *arg;
3183
3184         if (!s || !*s)
3185                 return -1;
3186         if (!strcmp(s, "ours"))
3187                 o->recursive_variant = MERGE_RECURSIVE_OURS;
3188         else if (!strcmp(s, "theirs"))
3189                 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
3190         else if (!strcmp(s, "subtree"))
3191                 o->subtree_shift = "";
3192         else if (skip_prefix(s, "subtree=", &arg))
3193                 o->subtree_shift = arg;
3194         else if (!strcmp(s, "patience"))
3195                 o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
3196         else if (!strcmp(s, "histogram"))
3197                 o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
3198         else if (skip_prefix(s, "diff-algorithm=", &arg)) {
3199                 long value = parse_algorithm_value(arg);
3200                 if (value < 0)
3201                         return -1;
3202                 /* clear out previous settings */
3203                 DIFF_XDL_CLR(o, NEED_MINIMAL);
3204                 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
3205                 o->xdl_opts |= value;
3206         }
3207         else if (!strcmp(s, "ignore-space-change"))
3208                 DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE);
3209         else if (!strcmp(s, "ignore-all-space"))
3210                 DIFF_XDL_SET(o, IGNORE_WHITESPACE);
3211         else if (!strcmp(s, "ignore-space-at-eol"))
3212                 DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL);
3213         else if (!strcmp(s, "ignore-cr-at-eol"))
3214                 DIFF_XDL_SET(o, IGNORE_CR_AT_EOL);
3215         else if (!strcmp(s, "renormalize"))
3216                 o->renormalize = 1;
3217         else if (!strcmp(s, "no-renormalize"))
3218                 o->renormalize = 0;
3219         else if (!strcmp(s, "no-renames"))
3220                 o->detect_rename = 0;
3221         else if (!strcmp(s, "find-renames")) {
3222                 o->detect_rename = 1;
3223                 o->rename_score = 0;
3224         }
3225         else if (skip_prefix(s, "find-renames=", &arg) ||
3226                  skip_prefix(s, "rename-threshold=", &arg)) {
3227                 if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
3228                         return -1;
3229                 o->detect_rename = 1;
3230         }
3231         else
3232                 return -1;
3233         return 0;
3234 }