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