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