Merge branch 'np/pack'
[git] / merge-recursive.c
1 /*
2  * Recursive Merge algorithm stolen from git-merge-recursive.py by
3  * Fredrik Kuivinen.
4  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
5  */
6 #include "cache.h"
7 #include "cache-tree.h"
8 #include "commit.h"
9 #include "blob.h"
10 #include "tree-walk.h"
11 #include "diff.h"
12 #include "diffcore.h"
13 #include "run-command.h"
14 #include "tag.h"
15 #include "unpack-trees.h"
16 #include "path-list.h"
17 #include "xdiff-interface.h"
18
19 static int subtree_merge;
20
21 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
22 {
23         unsigned char shifted[20];
24
25         /*
26          * NEEDSWORK: this limits the recursion depth to hardcoded
27          * value '2' to avoid excessive overhead.
28          */
29         shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
30         if (!hashcmp(two->object.sha1, shifted))
31                 return two;
32         return lookup_tree(shifted);
33 }
34
35 /*
36  * A virtual commit has
37  * - (const char *)commit->util set to the name, and
38  * - *(int *)commit->object.sha1 set to the virtual id.
39  */
40
41 static unsigned commit_list_count(const struct commit_list *l)
42 {
43         unsigned c = 0;
44         for (; l; l = l->next )
45                 c++;
46         return c;
47 }
48
49 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
50 {
51         struct commit *commit = xcalloc(1, sizeof(struct commit));
52         static unsigned virtual_id = 1;
53         commit->tree = tree;
54         commit->util = (void*)comment;
55         *(int*)commit->object.sha1 = virtual_id++;
56         /* avoid warnings */
57         commit->object.parsed = 1;
58         return commit;
59 }
60
61 /*
62  * Since we use get_tree_entry(), which does not put the read object into
63  * the object pool, we cannot rely on a == b.
64  */
65 static int sha_eq(const unsigned char *a, const unsigned char *b)
66 {
67         if (!a && !b)
68                 return 2;
69         return a && b && hashcmp(a, b) == 0;
70 }
71
72 /*
73  * Since we want to write the index eventually, we cannot reuse the index
74  * for these (temporary) data.
75  */
76 struct stage_data
77 {
78         struct
79         {
80                 unsigned mode;
81                 unsigned char sha[20];
82         } stages[4];
83         unsigned processed:1;
84 };
85
86 struct output_buffer
87 {
88         struct output_buffer *next;
89         char *str;
90 };
91
92 static struct path_list current_file_set = {NULL, 0, 0, 1};
93 static struct path_list current_directory_set = {NULL, 0, 0, 1};
94
95 static int call_depth = 0;
96 static int verbosity = 2;
97 static int buffer_output = 1;
98 static struct output_buffer *output_list, *output_end;
99
100 static int show (int v)
101 {
102         return (!call_depth && verbosity >= v) || verbosity >= 5;
103 }
104
105 static void output(int v, const char *fmt, ...)
106 {
107         va_list args;
108         va_start(args, fmt);
109         if (buffer_output && show(v)) {
110                 struct output_buffer *b = xmalloc(sizeof(*b));
111                 nfvasprintf(&b->str, fmt, args);
112                 b->next = NULL;
113                 if (output_end)
114                         output_end->next = b;
115                 else
116                         output_list = b;
117                 output_end = b;
118         } else if (show(v)) {
119                 int i;
120                 for (i = call_depth; i--;)
121                         fputs("  ", stdout);
122                 vfprintf(stdout, fmt, args);
123                 fputc('\n', stdout);
124         }
125         va_end(args);
126 }
127
128 static void flush_output()
129 {
130         struct output_buffer *b, *n;
131         for (b = output_list; b; b = n) {
132                 int i;
133                 for (i = call_depth; i--;)
134                         fputs("  ", stdout);
135                 fputs(b->str, stdout);
136                 fputc('\n', stdout);
137                 n = b->next;
138                 free(b->str);
139                 free(b);
140         }
141         output_list = NULL;
142         output_end = NULL;
143 }
144
145 static void output_commit_title(struct commit *commit)
146 {
147         int i;
148         flush_output();
149         for (i = call_depth; i--;)
150                 fputs("  ", stdout);
151         if (commit->util)
152                 printf("virtual %s\n", (char *)commit->util);
153         else {
154                 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
155                 if (parse_commit(commit) != 0)
156                         printf("(bad commit)\n");
157                 else {
158                         const char *s;
159                         int len;
160                         for (s = commit->buffer; *s; s++)
161                                 if (*s == '\n' && s[1] == '\n') {
162                                         s += 2;
163                                         break;
164                                 }
165                         for (len = 0; s[len] && '\n' != s[len]; len++)
166                                 ; /* do nothing */
167                         printf("%.*s\n", len, s);
168                 }
169         }
170 }
171
172 static struct cache_entry *make_cache_entry(unsigned int mode,
173                 const unsigned char *sha1, const char *path, int stage, int refresh)
174 {
175         int size, len;
176         struct cache_entry *ce;
177
178         if (!verify_path(path))
179                 return NULL;
180
181         len = strlen(path);
182         size = cache_entry_size(len);
183         ce = xcalloc(1, size);
184
185         hashcpy(ce->sha1, sha1);
186         memcpy(ce->name, path, len);
187         ce->ce_flags = create_ce_flags(len, stage);
188         ce->ce_mode = create_ce_mode(mode);
189
190         if (refresh)
191                 return refresh_cache_entry(ce, 0);
192
193         return ce;
194 }
195
196 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
197                 const char *path, int stage, int refresh, int options)
198 {
199         struct cache_entry *ce;
200         ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
201         if (!ce)
202                 return error("addinfo_cache failed for path '%s'", path);
203         return add_cache_entry(ce, options);
204 }
205
206 /*
207  * This is a global variable which is used in a number of places but
208  * only written to in the 'merge' function.
209  *
210  * index_only == 1    => Don't leave any non-stage 0 entries in the cache and
211  *                       don't update the working directory.
212  *               0    => Leave unmerged entries in the cache and update
213  *                       the working directory.
214  */
215 static int index_only = 0;
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 object_list *trees = NULL;
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
235         object_list_append(&common->object, &trees);
236         object_list_append(&head->object, &trees);
237         object_list_append(&merge->object, &trees);
238
239         rc = unpack_trees(trees, &opts);
240         cache_tree_free(&active_cache_tree);
241         return rc;
242 }
243
244 static int unmerged_index(void)
245 {
246         int i;
247         for (i = 0; i < active_nr; i++) {
248                 struct cache_entry *ce = active_cache[i];
249                 if (ce_stage(ce))
250                         return 1;
251         }
252         return 0;
253 }
254
255 static struct tree *git_write_tree(void)
256 {
257         struct tree *result = NULL;
258
259         if (unmerged_index()) {
260                 int i;
261                 output(0, "There are unmerged index entries:");
262                 for (i = 0; i < active_nr; i++) {
263                         struct cache_entry *ce = active_cache[i];
264                         if (ce_stage(ce))
265                                 output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
266                 }
267                 return NULL;
268         }
269
270         if (!active_cache_tree)
271                 active_cache_tree = cache_tree();
272
273         if (!cache_tree_fully_valid(active_cache_tree) &&
274             cache_tree_update(active_cache_tree,
275                               active_cache, active_nr, 0, 0) < 0)
276                 die("error building trees");
277
278         result = lookup_tree(active_cache_tree->sha1);
279
280         return result;
281 }
282
283 static int save_files_dirs(const unsigned char *sha1,
284                 const char *base, int baselen, const char *path,
285                 unsigned int mode, int stage)
286 {
287         int len = strlen(path);
288         char *newpath = xmalloc(baselen + len + 1);
289         memcpy(newpath, base, baselen);
290         memcpy(newpath + baselen, path, len);
291         newpath[baselen + len] = '\0';
292
293         if (S_ISDIR(mode))
294                 path_list_insert(newpath, &current_directory_set);
295         else
296                 path_list_insert(newpath, &current_file_set);
297         free(newpath);
298
299         return READ_TREE_RECURSIVE;
300 }
301
302 static int get_files_dirs(struct tree *tree)
303 {
304         int n;
305         if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
306                 return 0;
307         n = current_file_set.nr + current_directory_set.nr;
308         return n;
309 }
310
311 /*
312  * Returns a index_entry instance which doesn't have to correspond to
313  * a real cache entry in Git's index.
314  */
315 static struct stage_data *insert_stage_data(const char *path,
316                 struct tree *o, struct tree *a, struct tree *b,
317                 struct path_list *entries)
318 {
319         struct path_list_item *item;
320         struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
321         get_tree_entry(o->object.sha1, path,
322                         e->stages[1].sha, &e->stages[1].mode);
323         get_tree_entry(a->object.sha1, path,
324                         e->stages[2].sha, &e->stages[2].mode);
325         get_tree_entry(b->object.sha1, path,
326                         e->stages[3].sha, &e->stages[3].mode);
327         item = path_list_insert(path, entries);
328         item->util = e;
329         return e;
330 }
331
332 /*
333  * Create a dictionary mapping file names to stage_data objects. The
334  * dictionary contains one entry for every path with a non-zero stage entry.
335  */
336 static struct path_list *get_unmerged(void)
337 {
338         struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
339         int i;
340
341         unmerged->strdup_paths = 1;
342
343         for (i = 0; i < active_nr; i++) {
344                 struct path_list_item *item;
345                 struct stage_data *e;
346                 struct cache_entry *ce = active_cache[i];
347                 if (!ce_stage(ce))
348                         continue;
349
350                 item = path_list_lookup(ce->name, unmerged);
351                 if (!item) {
352                         item = path_list_insert(ce->name, unmerged);
353                         item->util = xcalloc(1, sizeof(struct stage_data));
354                 }
355                 e = item->util;
356                 e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
357                 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
358         }
359
360         return unmerged;
361 }
362
363 struct rename
364 {
365         struct diff_filepair *pair;
366         struct stage_data *src_entry;
367         struct stage_data *dst_entry;
368         unsigned processed:1;
369 };
370
371 /*
372  * Get information of all renames which occurred between 'o_tree' and
373  * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
374  * 'b_tree') to be able to associate the correct cache entries with
375  * the rename information. 'tree' is always equal to either a_tree or b_tree.
376  */
377 static struct path_list *get_renames(struct tree *tree,
378                                         struct tree *o_tree,
379                                         struct tree *a_tree,
380                                         struct tree *b_tree,
381                                         struct path_list *entries)
382 {
383         int i;
384         struct path_list *renames;
385         struct diff_options opts;
386
387         renames = xcalloc(1, sizeof(struct path_list));
388         diff_setup(&opts);
389         opts.recursive = 1;
390         opts.detect_rename = DIFF_DETECT_RENAME;
391         opts.output_format = DIFF_FORMAT_NO_OUTPUT;
392         if (diff_setup_done(&opts) < 0)
393                 die("diff setup failed");
394         diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
395         diffcore_std(&opts);
396         for (i = 0; i < diff_queued_diff.nr; ++i) {
397                 struct path_list_item *item;
398                 struct rename *re;
399                 struct diff_filepair *pair = diff_queued_diff.queue[i];
400                 if (pair->status != 'R') {
401                         diff_free_filepair(pair);
402                         continue;
403                 }
404                 re = xmalloc(sizeof(*re));
405                 re->processed = 0;
406                 re->pair = pair;
407                 item = path_list_lookup(re->pair->one->path, entries);
408                 if (!item)
409                         re->src_entry = insert_stage_data(re->pair->one->path,
410                                         o_tree, a_tree, b_tree, entries);
411                 else
412                         re->src_entry = item->util;
413
414                 item = path_list_lookup(re->pair->two->path, entries);
415                 if (!item)
416                         re->dst_entry = insert_stage_data(re->pair->two->path,
417                                         o_tree, a_tree, b_tree, entries);
418                 else
419                         re->dst_entry = item->util;
420                 item = path_list_insert(pair->one->path, renames);
421                 item->util = re;
422         }
423         opts.output_format = DIFF_FORMAT_NO_OUTPUT;
424         diff_queued_diff.nr = 0;
425         diff_flush(&opts);
426         return renames;
427 }
428
429 static int update_stages(const char *path, struct diff_filespec *o,
430                          struct diff_filespec *a, struct diff_filespec *b,
431                          int clear)
432 {
433         int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
434         if (clear)
435                 if (remove_file_from_cache(path))
436                         return -1;
437         if (o)
438                 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
439                         return -1;
440         if (a)
441                 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
442                         return -1;
443         if (b)
444                 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
445                         return -1;
446         return 0;
447 }
448
449 static int remove_path(const char *name)
450 {
451         int ret, len;
452         char *slash, *dirs;
453
454         ret = unlink(name);
455         if (ret)
456                 return ret;
457         len = strlen(name);
458         dirs = xmalloc(len+1);
459         memcpy(dirs, name, len);
460         dirs[len] = '\0';
461         while ((slash = strrchr(name, '/'))) {
462                 *slash = '\0';
463                 len = slash - name;
464                 if (rmdir(name) != 0)
465                         break;
466         }
467         free(dirs);
468         return ret;
469 }
470
471 static int remove_file(int clean, const char *path, int no_wd)
472 {
473         int update_cache = index_only || clean;
474         int update_working_directory = !index_only && !no_wd;
475
476         if (update_cache) {
477                 if (remove_file_from_cache(path))
478                         return -1;
479         }
480         if (update_working_directory) {
481                 unlink(path);
482                 if (errno != ENOENT || errno != EISDIR)
483                         return -1;
484                 remove_path(path);
485         }
486         return 0;
487 }
488
489 static char *unique_path(const char *path, const char *branch)
490 {
491         char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
492         int suffix = 0;
493         struct stat st;
494         char *p = newpath + strlen(path);
495         strcpy(newpath, path);
496         *(p++) = '~';
497         strcpy(p, branch);
498         for (; *p; ++p)
499                 if ('/' == *p)
500                         *p = '_';
501         while (path_list_has_path(&current_file_set, newpath) ||
502                path_list_has_path(&current_directory_set, newpath) ||
503                lstat(newpath, &st) == 0)
504                 sprintf(p, "_%d", suffix++);
505
506         path_list_insert(newpath, &current_file_set);
507         return newpath;
508 }
509
510 static int mkdir_p(const char *path, unsigned long mode)
511 {
512         /* path points to cache entries, so xstrdup before messing with it */
513         char *buf = xstrdup(path);
514         int result = safe_create_leading_directories(buf);
515         free(buf);
516         return result;
517 }
518
519 static void flush_buffer(int fd, const char *buf, unsigned long size)
520 {
521         while (size > 0) {
522                 long ret = write_in_full(fd, buf, size);
523                 if (ret < 0) {
524                         /* Ignore epipe */
525                         if (errno == EPIPE)
526                                 break;
527                         die("merge-recursive: %s", strerror(errno));
528                 } else if (!ret) {
529                         die("merge-recursive: disk full?");
530                 }
531                 size -= ret;
532                 buf += ret;
533         }
534 }
535
536 static int make_room_for_path(const char *path)
537 {
538         int status;
539         const char *msg = "failed to create path '%s'%s";
540
541         status = mkdir_p(path, 0777);
542         if (status) {
543                 if (status == -3) {
544                         /* something else exists */
545                         error(msg, path, ": perhaps a D/F conflict?");
546                         return -1;
547                 }
548                 die(msg, path, "");
549         }
550
551         /* Successful unlink is good.. */
552         if (!unlink(path))
553                 return 0;
554         /* .. and so is no existing file */
555         if (errno == ENOENT)
556                 return 0;
557         /* .. but not some other error (who really cares what?) */
558         return error(msg, path, ": perhaps a D/F conflict?");
559 }
560
561 static void update_file_flags(const unsigned char *sha,
562                               unsigned mode,
563                               const char *path,
564                               int update_cache,
565                               int update_wd)
566 {
567         if (index_only)
568                 update_wd = 0;
569
570         if (update_wd) {
571                 enum object_type type;
572                 void *buf;
573                 unsigned long size;
574
575                 buf = read_sha1_file(sha, &type, &size);
576                 if (!buf)
577                         die("cannot read object %s '%s'", sha1_to_hex(sha), path);
578                 if (type != OBJ_BLOB)
579                         die("blob expected for %s '%s'", sha1_to_hex(sha), path);
580
581                 if (make_room_for_path(path) < 0) {
582                         update_wd = 0;
583                         goto update_index;
584                 }
585                 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
586                         int fd;
587                         if (mode & 0100)
588                                 mode = 0777;
589                         else
590                                 mode = 0666;
591                         fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
592                         if (fd < 0)
593                                 die("failed to open %s: %s", path, strerror(errno));
594                         flush_buffer(fd, buf, size);
595                         close(fd);
596                 } else if (S_ISLNK(mode)) {
597                         char *lnk = xmalloc(size + 1);
598                         memcpy(lnk, buf, size);
599                         lnk[size] = '\0';
600                         mkdir_p(path, 0777);
601                         unlink(path);
602                         symlink(lnk, path);
603                         free(lnk);
604                 } else
605                         die("do not know what to do with %06o %s '%s'",
606                             mode, sha1_to_hex(sha), path);
607         }
608  update_index:
609         if (update_cache)
610                 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
611 }
612
613 static void update_file(int clean,
614                         const unsigned char *sha,
615                         unsigned mode,
616                         const char *path)
617 {
618         update_file_flags(sha, mode, path, index_only || clean, !index_only);
619 }
620
621 /* Low level file merging, update and removal */
622
623 struct merge_file_info
624 {
625         unsigned char sha[20];
626         unsigned mode;
627         unsigned clean:1,
628                  merge:1;
629 };
630
631 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
632 {
633         unsigned long size;
634         enum object_type type;
635
636         if (!hashcmp(sha1, null_sha1)) {
637                 mm->ptr = xstrdup("");
638                 mm->size = 0;
639                 return;
640         }
641
642         mm->ptr = read_sha1_file(sha1, &type, &size);
643         if (!mm->ptr || type != OBJ_BLOB)
644                 die("unable to read blob object %s", sha1_to_hex(sha1));
645         mm->size = size;
646 }
647
648 static struct merge_file_info merge_file(struct diff_filespec *o,
649                 struct diff_filespec *a, struct diff_filespec *b,
650                 const char *branch1, const char *branch2)
651 {
652         struct merge_file_info result;
653         result.merge = 0;
654         result.clean = 1;
655
656         if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
657                 result.clean = 0;
658                 if (S_ISREG(a->mode)) {
659                         result.mode = a->mode;
660                         hashcpy(result.sha, a->sha1);
661                 } else {
662                         result.mode = b->mode;
663                         hashcpy(result.sha, b->sha1);
664                 }
665         } else {
666                 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
667                         result.merge = 1;
668
669                 result.mode = a->mode == o->mode ? b->mode: a->mode;
670
671                 if (sha_eq(a->sha1, o->sha1))
672                         hashcpy(result.sha, b->sha1);
673                 else if (sha_eq(b->sha1, o->sha1))
674                         hashcpy(result.sha, a->sha1);
675                 else if (S_ISREG(a->mode)) {
676                         mmfile_t orig, src1, src2;
677                         mmbuffer_t result_buf;
678                         xpparam_t xpp;
679                         char *name1, *name2;
680                         int merge_status;
681
682                         name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
683                         name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
684
685                         fill_mm(o->sha1, &orig);
686                         fill_mm(a->sha1, &src1);
687                         fill_mm(b->sha1, &src2);
688
689                         memset(&xpp, 0, sizeof(xpp));
690                         merge_status = xdl_merge(&orig,
691                                                  &src1, name1,
692                                                  &src2, name2,
693                                                  &xpp, XDL_MERGE_ZEALOUS,
694                                                  &result_buf);
695                         free(name1);
696                         free(name2);
697                         free(orig.ptr);
698                         free(src1.ptr);
699                         free(src2.ptr);
700
701                         if ((merge_status < 0) || !result_buf.ptr)
702                                 die("Failed to execute internal merge");
703
704                         if (write_sha1_file(result_buf.ptr, result_buf.size,
705                                             blob_type, result.sha))
706                                 die("Unable to add %s to database",
707                                     a->path);
708
709                         free(result_buf.ptr);
710                         result.clean = (merge_status == 0);
711                 } else {
712                         if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
713                                 die("cannot merge modes?");
714
715                         hashcpy(result.sha, a->sha1);
716
717                         if (!sha_eq(a->sha1, b->sha1))
718                                 result.clean = 0;
719                 }
720         }
721
722         return result;
723 }
724
725 static void conflict_rename_rename(struct rename *ren1,
726                                    const char *branch1,
727                                    struct rename *ren2,
728                                    const char *branch2)
729 {
730         char *del[2];
731         int delp = 0;
732         const char *ren1_dst = ren1->pair->two->path;
733         const char *ren2_dst = ren2->pair->two->path;
734         const char *dst_name1 = ren1_dst;
735         const char *dst_name2 = ren2_dst;
736         if (path_list_has_path(&current_directory_set, ren1_dst)) {
737                 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
738                 output(1, "%s is a directory in %s added as %s instead",
739                        ren1_dst, branch2, dst_name1);
740                 remove_file(0, ren1_dst, 0);
741         }
742         if (path_list_has_path(&current_directory_set, ren2_dst)) {
743                 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
744                 output(1, "%s is a directory in %s added as %s instead",
745                        ren2_dst, branch1, dst_name2);
746                 remove_file(0, ren2_dst, 0);
747         }
748         if (index_only) {
749                 remove_file_from_cache(dst_name1);
750                 remove_file_from_cache(dst_name2);
751                 /*
752                  * Uncomment to leave the conflicting names in the resulting tree
753                  *
754                  * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
755                  * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
756                  */
757         } else {
758                 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
759                 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
760         }
761         while (delp--)
762                 free(del[delp]);
763 }
764
765 static void conflict_rename_dir(struct rename *ren1,
766                                 const char *branch1)
767 {
768         char *new_path = unique_path(ren1->pair->two->path, branch1);
769         output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
770         remove_file(0, ren1->pair->two->path, 0);
771         update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
772         free(new_path);
773 }
774
775 static void conflict_rename_rename_2(struct rename *ren1,
776                                      const char *branch1,
777                                      struct rename *ren2,
778                                      const char *branch2)
779 {
780         char *new_path1 = unique_path(ren1->pair->two->path, branch1);
781         char *new_path2 = unique_path(ren2->pair->two->path, branch2);
782         output(1, "Renamed %s to %s and %s to %s instead",
783                ren1->pair->one->path, new_path1,
784                ren2->pair->one->path, new_path2);
785         remove_file(0, ren1->pair->two->path, 0);
786         update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
787         update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
788         free(new_path2);
789         free(new_path1);
790 }
791
792 static int process_renames(struct path_list *a_renames,
793                            struct path_list *b_renames,
794                            const char *a_branch,
795                            const char *b_branch)
796 {
797         int clean_merge = 1, i, j;
798         struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
799         const struct rename *sre;
800
801         for (i = 0; i < a_renames->nr; i++) {
802                 sre = a_renames->items[i].util;
803                 path_list_insert(sre->pair->two->path, &a_by_dst)->util
804                         = sre->dst_entry;
805         }
806         for (i = 0; i < b_renames->nr; i++) {
807                 sre = b_renames->items[i].util;
808                 path_list_insert(sre->pair->two->path, &b_by_dst)->util
809                         = sre->dst_entry;
810         }
811
812         for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
813                 int compare;
814                 char *src;
815                 struct path_list *renames1, *renames2, *renames2Dst;
816                 struct rename *ren1 = NULL, *ren2 = NULL;
817                 const char *branch1, *branch2;
818                 const char *ren1_src, *ren1_dst;
819
820                 if (i >= a_renames->nr) {
821                         compare = 1;
822                         ren2 = b_renames->items[j++].util;
823                 } else if (j >= b_renames->nr) {
824                         compare = -1;
825                         ren1 = a_renames->items[i++].util;
826                 } else {
827                         compare = strcmp(a_renames->items[i].path,
828                                         b_renames->items[j].path);
829                         if (compare <= 0)
830                                 ren1 = a_renames->items[i++].util;
831                         if (compare >= 0)
832                                 ren2 = b_renames->items[j++].util;
833                 }
834
835                 /* TODO: refactor, so that 1/2 are not needed */
836                 if (ren1) {
837                         renames1 = a_renames;
838                         renames2 = b_renames;
839                         renames2Dst = &b_by_dst;
840                         branch1 = a_branch;
841                         branch2 = b_branch;
842                 } else {
843                         struct rename *tmp;
844                         renames1 = b_renames;
845                         renames2 = a_renames;
846                         renames2Dst = &a_by_dst;
847                         branch1 = b_branch;
848                         branch2 = a_branch;
849                         tmp = ren2;
850                         ren2 = ren1;
851                         ren1 = tmp;
852                 }
853                 src = ren1->pair->one->path;
854
855                 ren1->dst_entry->processed = 1;
856                 ren1->src_entry->processed = 1;
857
858                 if (ren1->processed)
859                         continue;
860                 ren1->processed = 1;
861
862                 ren1_src = ren1->pair->one->path;
863                 ren1_dst = ren1->pair->two->path;
864
865                 if (ren2) {
866                         const char *ren2_src = ren2->pair->one->path;
867                         const char *ren2_dst = ren2->pair->two->path;
868                         /* Renamed in 1 and renamed in 2 */
869                         if (strcmp(ren1_src, ren2_src) != 0)
870                                 die("ren1.src != ren2.src");
871                         ren2->dst_entry->processed = 1;
872                         ren2->processed = 1;
873                         if (strcmp(ren1_dst, ren2_dst) != 0) {
874                                 clean_merge = 0;
875                                 output(1, "CONFLICT (rename/rename): "
876                                        "Rename \"%s\"->\"%s\" in branch \"%s\" "
877                                        "rename \"%s\"->\"%s\" in \"%s\"%s",
878                                        src, ren1_dst, branch1,
879                                        src, ren2_dst, branch2,
880                                        index_only ? " (left unresolved)": "");
881                                 if (index_only) {
882                                         remove_file_from_cache(src);
883                                         update_file(0, ren1->pair->one->sha1,
884                                                     ren1->pair->one->mode, src);
885                                 }
886                                 conflict_rename_rename(ren1, branch1, ren2, branch2);
887                         } else {
888                                 struct merge_file_info mfi;
889                                 remove_file(1, ren1_src, 1);
890                                 mfi = merge_file(ren1->pair->one,
891                                                  ren1->pair->two,
892                                                  ren2->pair->two,
893                                                  branch1,
894                                                  branch2);
895                                 if (mfi.merge || !mfi.clean)
896                                         output(1, "Renamed %s->%s", src, ren1_dst);
897
898                                 if (mfi.merge)
899                                         output(2, "Auto-merged %s", ren1_dst);
900
901                                 if (!mfi.clean) {
902                                         output(1, "CONFLICT (content): merge conflict in %s",
903                                                ren1_dst);
904                                         clean_merge = 0;
905
906                                         if (!index_only)
907                                                 update_stages(ren1_dst,
908                                                               ren1->pair->one,
909                                                               ren1->pair->two,
910                                                               ren2->pair->two,
911                                                               1 /* clear */);
912                                 }
913                                 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
914                         }
915                 } else {
916                         /* Renamed in 1, maybe changed in 2 */
917                         struct path_list_item *item;
918                         /* we only use sha1 and mode of these */
919                         struct diff_filespec src_other, dst_other;
920                         int try_merge, stage = a_renames == renames1 ? 3: 2;
921
922                         remove_file(1, ren1_src, index_only || stage == 3);
923
924                         hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
925                         src_other.mode = ren1->src_entry->stages[stage].mode;
926                         hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
927                         dst_other.mode = ren1->dst_entry->stages[stage].mode;
928
929                         try_merge = 0;
930
931                         if (path_list_has_path(&current_directory_set, ren1_dst)) {
932                                 clean_merge = 0;
933                                 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
934                                        " directory %s added in %s",
935                                        ren1_src, ren1_dst, branch1,
936                                        ren1_dst, branch2);
937                                 conflict_rename_dir(ren1, branch1);
938                         } else if (sha_eq(src_other.sha1, null_sha1)) {
939                                 clean_merge = 0;
940                                 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
941                                        "and deleted in %s",
942                                        ren1_src, ren1_dst, branch1,
943                                        branch2);
944                                 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
945                         } else if (!sha_eq(dst_other.sha1, null_sha1)) {
946                                 const char *new_path;
947                                 clean_merge = 0;
948                                 try_merge = 1;
949                                 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
950                                        "%s added in %s",
951                                        ren1_src, ren1_dst, branch1,
952                                        ren1_dst, branch2);
953                                 new_path = unique_path(ren1_dst, branch2);
954                                 output(1, "Added as %s instead", new_path);
955                                 update_file(0, dst_other.sha1, dst_other.mode, new_path);
956                         } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
957                                 ren2 = item->util;
958                                 clean_merge = 0;
959                                 ren2->processed = 1;
960                                 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
961                                        "Renamed %s->%s in %s",
962                                        ren1_src, ren1_dst, branch1,
963                                        ren2->pair->one->path, ren2->pair->two->path, branch2);
964                                 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
965                         } else
966                                 try_merge = 1;
967
968                         if (try_merge) {
969                                 struct diff_filespec *o, *a, *b;
970                                 struct merge_file_info mfi;
971                                 src_other.path = (char *)ren1_src;
972
973                                 o = ren1->pair->one;
974                                 if (a_renames == renames1) {
975                                         a = ren1->pair->two;
976                                         b = &src_other;
977                                 } else {
978                                         b = ren1->pair->two;
979                                         a = &src_other;
980                                 }
981                                 mfi = merge_file(o, a, b,
982                                                 a_branch, b_branch);
983
984                                 if (mfi.merge || !mfi.clean)
985                                         output(1, "Renamed %s => %s", ren1_src, ren1_dst);
986                                 if (mfi.merge)
987                                         output(2, "Auto-merged %s", ren1_dst);
988                                 if (!mfi.clean) {
989                                         output(1, "CONFLICT (rename/modify): Merge conflict in %s",
990                                                ren1_dst);
991                                         clean_merge = 0;
992
993                                         if (!index_only)
994                                                 update_stages(ren1_dst,
995                                                                 o, a, b, 1);
996                                 }
997                                 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
998                         }
999                 }
1000         }
1001         path_list_clear(&a_by_dst, 0);
1002         path_list_clear(&b_by_dst, 0);
1003
1004         return clean_merge;
1005 }
1006
1007 static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
1008 {
1009         return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
1010 }
1011
1012 /* Per entry merge function */
1013 static int process_entry(const char *path, struct stage_data *entry,
1014                          const char *branch1,
1015                          const char *branch2)
1016 {
1017         /*
1018         printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1019         print_index_entry("\tpath: ", entry);
1020         */
1021         int clean_merge = 1;
1022         unsigned o_mode = entry->stages[1].mode;
1023         unsigned a_mode = entry->stages[2].mode;
1024         unsigned b_mode = entry->stages[3].mode;
1025         unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1026         unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1027         unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1028
1029         if (o_sha && (!a_sha || !b_sha)) {
1030                 /* Case A: Deleted in one */
1031                 if ((!a_sha && !b_sha) ||
1032                     (sha_eq(a_sha, o_sha) && !b_sha) ||
1033                     (!a_sha && sha_eq(b_sha, o_sha))) {
1034                         /* Deleted in both or deleted in one and
1035                          * unchanged in the other */
1036                         if (a_sha)
1037                                 output(2, "Removed %s", path);
1038                         /* do not touch working file if it did not exist */
1039                         remove_file(1, path, !a_sha);
1040                 } else {
1041                         /* Deleted in one and changed in the other */
1042                         clean_merge = 0;
1043                         if (!a_sha) {
1044                                 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1045                                        "and modified in %s. Version %s of %s left in tree.",
1046                                        path, branch1,
1047                                        branch2, branch2, path);
1048                                 update_file(0, b_sha, b_mode, path);
1049                         } else {
1050                                 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1051                                        "and modified in %s. Version %s of %s left in tree.",
1052                                        path, branch2,
1053                                        branch1, branch1, path);
1054                                 update_file(0, a_sha, a_mode, path);
1055                         }
1056                 }
1057
1058         } else if ((!o_sha && a_sha && !b_sha) ||
1059                    (!o_sha && !a_sha && b_sha)) {
1060                 /* Case B: Added in one. */
1061                 const char *add_branch;
1062                 const char *other_branch;
1063                 unsigned mode;
1064                 const unsigned char *sha;
1065                 const char *conf;
1066
1067                 if (a_sha) {
1068                         add_branch = branch1;
1069                         other_branch = branch2;
1070                         mode = a_mode;
1071                         sha = a_sha;
1072                         conf = "file/directory";
1073                 } else {
1074                         add_branch = branch2;
1075                         other_branch = branch1;
1076                         mode = b_mode;
1077                         sha = b_sha;
1078                         conf = "directory/file";
1079                 }
1080                 if (path_list_has_path(&current_directory_set, path)) {
1081                         const char *new_path = unique_path(path, add_branch);
1082                         clean_merge = 0;
1083                         output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1084                                "Added %s as %s",
1085                                conf, path, other_branch, path, new_path);
1086                         remove_file(0, path, 0);
1087                         update_file(0, sha, mode, new_path);
1088                 } else {
1089                         output(2, "Added %s", path);
1090                         update_file(1, sha, mode, path);
1091                 }
1092         } else if (a_sha && b_sha) {
1093                 /* Case C: Added in both (check for same permissions) and */
1094                 /* case D: Modified in both, but differently. */
1095                 const char *reason = "content";
1096                 struct merge_file_info mfi;
1097                 struct diff_filespec o, a, b;
1098
1099                 if (!o_sha) {
1100                         reason = "add/add";
1101                         o_sha = (unsigned char *)null_sha1;
1102                 }
1103                 output(2, "Auto-merged %s", path);
1104                 o.path = a.path = b.path = (char *)path;
1105                 hashcpy(o.sha1, o_sha);
1106                 o.mode = o_mode;
1107                 hashcpy(a.sha1, a_sha);
1108                 a.mode = a_mode;
1109                 hashcpy(b.sha1, b_sha);
1110                 b.mode = b_mode;
1111
1112                 mfi = merge_file(&o, &a, &b,
1113                                  branch1, branch2);
1114
1115                 if (mfi.clean)
1116                         update_file(1, mfi.sha, mfi.mode, path);
1117                 else {
1118                         clean_merge = 0;
1119                         output(1, "CONFLICT (%s): Merge conflict in %s",
1120                                         reason, path);
1121
1122                         if (index_only)
1123                                 update_file(0, mfi.sha, mfi.mode, path);
1124                         else
1125                                 update_file_flags(mfi.sha, mfi.mode, path,
1126                                               0 /* update_cache */, 1 /* update_working_directory */);
1127                 }
1128         } else if (!o_sha && !a_sha && !b_sha) {
1129                 /*
1130                  * this entry was deleted altogether. a_mode == 0 means
1131                  * we had that path and want to actively remove it.
1132                  */
1133                 remove_file(1, path, !a_mode);
1134         } else
1135                 die("Fatal merge failure, shouldn't happen.");
1136
1137         return clean_merge;
1138 }
1139
1140 static int merge_trees(struct tree *head,
1141                        struct tree *merge,
1142                        struct tree *common,
1143                        const char *branch1,
1144                        const char *branch2,
1145                        struct tree **result)
1146 {
1147         int code, clean;
1148
1149         if (subtree_merge) {
1150                 merge = shift_tree_object(head, merge);
1151                 common = shift_tree_object(head, common);
1152         }
1153
1154         if (sha_eq(common->object.sha1, merge->object.sha1)) {
1155                 output(0, "Already uptodate!");
1156                 *result = head;
1157                 return 1;
1158         }
1159
1160         code = git_merge_trees(index_only, common, head, merge);
1161
1162         if (code != 0)
1163                 die("merging of trees %s and %s failed",
1164                     sha1_to_hex(head->object.sha1),
1165                     sha1_to_hex(merge->object.sha1));
1166
1167         if (unmerged_index()) {
1168                 struct path_list *entries, *re_head, *re_merge;
1169                 int i;
1170                 path_list_clear(&current_file_set, 1);
1171                 path_list_clear(&current_directory_set, 1);
1172                 get_files_dirs(head);
1173                 get_files_dirs(merge);
1174
1175                 entries = get_unmerged();
1176                 re_head  = get_renames(head, common, head, merge, entries);
1177                 re_merge = get_renames(merge, common, head, merge, entries);
1178                 clean = process_renames(re_head, re_merge,
1179                                 branch1, branch2);
1180                 for (i = 0; i < entries->nr; i++) {
1181                         const char *path = entries->items[i].path;
1182                         struct stage_data *e = entries->items[i].util;
1183                         if (!e->processed
1184                                 && !process_entry(path, e, branch1, branch2))
1185                                 clean = 0;
1186                 }
1187
1188                 path_list_clear(re_merge, 0);
1189                 path_list_clear(re_head, 0);
1190                 path_list_clear(entries, 1);
1191
1192         }
1193         else
1194                 clean = 1;
1195
1196         if (index_only)
1197                 *result = git_write_tree();
1198
1199         return clean;
1200 }
1201
1202 static struct commit_list *reverse_commit_list(struct commit_list *list)
1203 {
1204         struct commit_list *next = NULL, *current, *backup;
1205         for (current = list; current; current = backup) {
1206                 backup = current->next;
1207                 current->next = next;
1208                 next = current;
1209         }
1210         return next;
1211 }
1212
1213 /*
1214  * Merge the commits h1 and h2, return the resulting virtual
1215  * commit object and a flag indicating the cleanness of the merge.
1216  */
1217 static int merge(struct commit *h1,
1218                  struct commit *h2,
1219                  const char *branch1,
1220                  const char *branch2,
1221                  struct commit_list *ca,
1222                  struct commit **result)
1223 {
1224         struct commit_list *iter;
1225         struct commit *merged_common_ancestors;
1226         struct tree *mrtree;
1227         int clean;
1228
1229         if (show(4)) {
1230                 output(4, "Merging:");
1231                 output_commit_title(h1);
1232                 output_commit_title(h2);
1233         }
1234
1235         if (!ca) {
1236                 ca = get_merge_bases(h1, h2, 1);
1237                 ca = reverse_commit_list(ca);
1238         }
1239
1240         if (show(5)) {
1241                 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1242                 for (iter = ca; iter; iter = iter->next)
1243                         output_commit_title(iter->item);
1244         }
1245
1246         merged_common_ancestors = pop_commit(&ca);
1247         if (merged_common_ancestors == NULL) {
1248                 /* if there is no common ancestor, make an empty tree */
1249                 struct tree *tree = xcalloc(1, sizeof(struct tree));
1250
1251                 tree->object.parsed = 1;
1252                 tree->object.type = OBJ_TREE;
1253                 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1254                 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1255         }
1256
1257         for (iter = ca; iter; iter = iter->next) {
1258                 call_depth++;
1259                 /*
1260                  * When the merge fails, the result contains files
1261                  * with conflict markers. The cleanness flag is
1262                  * ignored, it was never actually used, as result of
1263                  * merge_trees has always overwritten it: the committed
1264                  * "conflicts" were already resolved.
1265                  */
1266                 discard_cache();
1267                 merge(merged_common_ancestors, iter->item,
1268                       "Temporary merge branch 1",
1269                       "Temporary merge branch 2",
1270                       NULL,
1271                       &merged_common_ancestors);
1272                 call_depth--;
1273
1274                 if (!merged_common_ancestors)
1275                         die("merge returned no commit");
1276         }
1277
1278         discard_cache();
1279         if (!call_depth) {
1280                 read_cache();
1281                 index_only = 0;
1282         } else
1283                 index_only = 1;
1284
1285         clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1286                             branch1, branch2, &mrtree);
1287
1288         if (index_only) {
1289                 *result = make_virtual_commit(mrtree, "merged tree");
1290                 commit_list_insert(h1, &(*result)->parents);
1291                 commit_list_insert(h2, &(*result)->parents->next);
1292         }
1293         flush_output();
1294         return clean;
1295 }
1296
1297 static const char *better_branch_name(const char *branch)
1298 {
1299         static char githead_env[8 + 40 + 1];
1300         char *name;
1301
1302         if (strlen(branch) != 40)
1303                 return branch;
1304         sprintf(githead_env, "GITHEAD_%s", branch);
1305         name = getenv(githead_env);
1306         return name ? name : branch;
1307 }
1308
1309 static struct commit *get_ref(const char *ref)
1310 {
1311         unsigned char sha1[20];
1312         struct object *object;
1313
1314         if (get_sha1(ref, sha1))
1315                 die("Could not resolve ref '%s'", ref);
1316         object = deref_tag(parse_object(sha1), ref, strlen(ref));
1317         if (object->type == OBJ_TREE)
1318                 return make_virtual_commit((struct tree*)object,
1319                         better_branch_name(ref));
1320         if (object->type != OBJ_COMMIT)
1321                 return NULL;
1322         if (parse_commit((struct commit *)object))
1323                 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1324         return (struct commit *)object;
1325 }
1326
1327 static int merge_config(const char *var, const char *value)
1328 {
1329         if (!strcasecmp(var, "merge.verbosity")) {
1330                 verbosity = git_config_int(var, value);
1331                 return 0;
1332         }
1333         return git_default_config(var, value);
1334 }
1335
1336 int main(int argc, char *argv[])
1337 {
1338         static const char *bases[20];
1339         static unsigned bases_count = 0;
1340         int i, clean;
1341         const char *branch1, *branch2;
1342         struct commit *result, *h1, *h2;
1343         struct commit_list *ca = NULL;
1344         struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1345         int index_fd;
1346
1347         if (argv[0]) {
1348                 int namelen = strlen(argv[0]);
1349                 if (8 < namelen &&
1350                     !strcmp(argv[0] + namelen - 8, "-subtree"))
1351                         subtree_merge = 1;
1352         }
1353
1354         git_config(merge_config);
1355         if (getenv("GIT_MERGE_VERBOSITY"))
1356                 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1357
1358         if (argc < 4)
1359                 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1360
1361         for (i = 1; i < argc; ++i) {
1362                 if (!strcmp(argv[i], "--"))
1363                         break;
1364                 if (bases_count < sizeof(bases)/sizeof(*bases))
1365                         bases[bases_count++] = argv[i];
1366         }
1367         if (argc - i != 3) /* "--" "<head>" "<remote>" */
1368                 die("Not handling anything other than two heads merge.");
1369         if (verbosity >= 5)
1370                 buffer_output = 0;
1371
1372         branch1 = argv[++i];
1373         branch2 = argv[++i];
1374
1375         h1 = get_ref(branch1);
1376         h2 = get_ref(branch2);
1377
1378         branch1 = better_branch_name(branch1);
1379         branch2 = better_branch_name(branch2);
1380
1381         if (show(3))
1382                 printf("Merging %s with %s\n", branch1, branch2);
1383
1384         index_fd = hold_locked_index(lock, 1);
1385
1386         for (i = 0; i < bases_count; i++) {
1387                 struct commit *ancestor = get_ref(bases[i]);
1388                 ca = commit_list_insert(ancestor, &ca);
1389         }
1390         clean = merge(h1, h2, branch1, branch2, ca, &result);
1391
1392         if (active_cache_changed &&
1393             (write_cache(index_fd, active_cache, active_nr) ||
1394              close(index_fd) || commit_locked_index(lock)))
1395                         die ("unable to write %s", get_index_file());
1396
1397         return clean ? 0: 1;
1398 }