]> git.ipfire.org Git - thirdparty/git.git/blob - merge-recursive.c
Allow the default low-level merge driver to be configured.
[thirdparty/git.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 #include "interpolate.h"
19 #include "attr.h"
20
21 static int subtree_merge;
22
23 static struct tree *shift_tree_object(struct tree *one, struct tree *two)
24 {
25 unsigned char shifted[20];
26
27 /*
28 * NEEDSWORK: this limits the recursion depth to hardcoded
29 * value '2' to avoid excessive overhead.
30 */
31 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
32 if (!hashcmp(two->object.sha1, shifted))
33 return two;
34 return lookup_tree(shifted);
35 }
36
37 /*
38 * A virtual commit has
39 * - (const char *)commit->util set to the name, and
40 * - *(int *)commit->object.sha1 set to the virtual id.
41 */
42
43 static unsigned commit_list_count(const struct commit_list *l)
44 {
45 unsigned c = 0;
46 for (; l; l = l->next )
47 c++;
48 return c;
49 }
50
51 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
52 {
53 struct commit *commit = xcalloc(1, sizeof(struct commit));
54 static unsigned virtual_id = 1;
55 commit->tree = tree;
56 commit->util = (void*)comment;
57 *(int*)commit->object.sha1 = virtual_id++;
58 /* avoid warnings */
59 commit->object.parsed = 1;
60 return commit;
61 }
62
63 /*
64 * Since we use get_tree_entry(), which does not put the read object into
65 * the object pool, we cannot rely on a == b.
66 */
67 static int sha_eq(const unsigned char *a, const unsigned char *b)
68 {
69 if (!a && !b)
70 return 2;
71 return a && b && hashcmp(a, b) == 0;
72 }
73
74 /*
75 * Since we want to write the index eventually, we cannot reuse the index
76 * for these (temporary) data.
77 */
78 struct stage_data
79 {
80 struct
81 {
82 unsigned mode;
83 unsigned char sha[20];
84 } stages[4];
85 unsigned processed:1;
86 };
87
88 struct output_buffer
89 {
90 struct output_buffer *next;
91 char *str;
92 };
93
94 static struct path_list current_file_set = {NULL, 0, 0, 1};
95 static struct path_list current_directory_set = {NULL, 0, 0, 1};
96
97 static int call_depth = 0;
98 static int verbosity = 2;
99 static int buffer_output = 1;
100 static int do_progress = 1;
101 static unsigned last_percent;
102 static unsigned merged_cnt;
103 static unsigned total_cnt;
104 static volatile sig_atomic_t progress_update;
105 static struct output_buffer *output_list, *output_end;
106
107 static int show (int v)
108 {
109 return (!call_depth && verbosity >= v) || verbosity >= 5;
110 }
111
112 static void output(int v, const char *fmt, ...)
113 {
114 va_list args;
115 va_start(args, fmt);
116 if (buffer_output && show(v)) {
117 struct output_buffer *b = xmalloc(sizeof(*b));
118 nfvasprintf(&b->str, fmt, args);
119 b->next = NULL;
120 if (output_end)
121 output_end->next = b;
122 else
123 output_list = b;
124 output_end = b;
125 } else if (show(v)) {
126 int i;
127 for (i = call_depth; i--;)
128 fputs(" ", stdout);
129 vfprintf(stdout, fmt, args);
130 fputc('\n', stdout);
131 }
132 va_end(args);
133 }
134
135 static void flush_output()
136 {
137 struct output_buffer *b, *n;
138 for (b = output_list; b; b = n) {
139 int i;
140 for (i = call_depth; i--;)
141 fputs(" ", stdout);
142 fputs(b->str, stdout);
143 fputc('\n', stdout);
144 n = b->next;
145 free(b->str);
146 free(b);
147 }
148 output_list = NULL;
149 output_end = NULL;
150 }
151
152 static void output_commit_title(struct commit *commit)
153 {
154 int i;
155 flush_output();
156 for (i = call_depth; i--;)
157 fputs(" ", stdout);
158 if (commit->util)
159 printf("virtual %s\n", (char *)commit->util);
160 else {
161 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
162 if (parse_commit(commit) != 0)
163 printf("(bad commit)\n");
164 else {
165 const char *s;
166 int len;
167 for (s = commit->buffer; *s; s++)
168 if (*s == '\n' && s[1] == '\n') {
169 s += 2;
170 break;
171 }
172 for (len = 0; s[len] && '\n' != s[len]; len++)
173 ; /* do nothing */
174 printf("%.*s\n", len, s);
175 }
176 }
177 }
178
179 static void progress_interval(int signum)
180 {
181 progress_update = 1;
182 }
183
184 static void setup_progress_signal(void)
185 {
186 struct sigaction sa;
187 struct itimerval v;
188
189 memset(&sa, 0, sizeof(sa));
190 sa.sa_handler = progress_interval;
191 sigemptyset(&sa.sa_mask);
192 sa.sa_flags = SA_RESTART;
193 sigaction(SIGALRM, &sa, NULL);
194
195 v.it_interval.tv_sec = 1;
196 v.it_interval.tv_usec = 0;
197 v.it_value = v.it_interval;
198 setitimer(ITIMER_REAL, &v, NULL);
199 }
200
201 static void display_progress()
202 {
203 unsigned percent = total_cnt ? merged_cnt * 100 / total_cnt : 0;
204 if (progress_update || percent != last_percent) {
205 fprintf(stderr, "%4u%% (%u/%u) done\r",
206 percent, merged_cnt, total_cnt);
207 progress_update = 0;
208 last_percent = percent;
209 }
210 }
211
212 static struct cache_entry *make_cache_entry(unsigned int mode,
213 const unsigned char *sha1, const char *path, int stage, int refresh)
214 {
215 int size, len;
216 struct cache_entry *ce;
217
218 if (!verify_path(path))
219 return NULL;
220
221 len = strlen(path);
222 size = cache_entry_size(len);
223 ce = xcalloc(1, size);
224
225 hashcpy(ce->sha1, sha1);
226 memcpy(ce->name, path, len);
227 ce->ce_flags = create_ce_flags(len, stage);
228 ce->ce_mode = create_ce_mode(mode);
229
230 if (refresh)
231 return refresh_cache_entry(ce, 0);
232
233 return ce;
234 }
235
236 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
237 const char *path, int stage, int refresh, int options)
238 {
239 struct cache_entry *ce;
240 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
241 if (!ce)
242 return error("addinfo_cache failed for path '%s'", path);
243 return add_cache_entry(ce, options);
244 }
245
246 /*
247 * This is a global variable which is used in a number of places but
248 * only written to in the 'merge' function.
249 *
250 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
251 * don't update the working directory.
252 * 0 => Leave unmerged entries in the cache and update
253 * the working directory.
254 */
255 static int index_only = 0;
256
257 static int git_merge_trees(int index_only,
258 struct tree *common,
259 struct tree *head,
260 struct tree *merge)
261 {
262 int rc;
263 struct object_list *trees = NULL;
264 struct unpack_trees_options opts;
265
266 memset(&opts, 0, sizeof(opts));
267 if (index_only)
268 opts.index_only = 1;
269 else
270 opts.update = 1;
271 opts.merge = 1;
272 opts.head_idx = 2;
273 opts.fn = threeway_merge;
274
275 object_list_append(&common->object, &trees);
276 object_list_append(&head->object, &trees);
277 object_list_append(&merge->object, &trees);
278
279 rc = unpack_trees(trees, &opts);
280 cache_tree_free(&active_cache_tree);
281 return rc;
282 }
283
284 static int unmerged_index(void)
285 {
286 int i;
287 for (i = 0; i < active_nr; i++) {
288 struct cache_entry *ce = active_cache[i];
289 if (ce_stage(ce))
290 return 1;
291 }
292 return 0;
293 }
294
295 static struct tree *git_write_tree(void)
296 {
297 struct tree *result = NULL;
298
299 if (unmerged_index()) {
300 int i;
301 output(0, "There are unmerged index entries:");
302 for (i = 0; i < active_nr; i++) {
303 struct cache_entry *ce = active_cache[i];
304 if (ce_stage(ce))
305 output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
306 }
307 return NULL;
308 }
309
310 if (!active_cache_tree)
311 active_cache_tree = cache_tree();
312
313 if (!cache_tree_fully_valid(active_cache_tree) &&
314 cache_tree_update(active_cache_tree,
315 active_cache, active_nr, 0, 0) < 0)
316 die("error building trees");
317
318 result = lookup_tree(active_cache_tree->sha1);
319
320 return result;
321 }
322
323 static int save_files_dirs(const unsigned char *sha1,
324 const char *base, int baselen, const char *path,
325 unsigned int mode, int stage)
326 {
327 int len = strlen(path);
328 char *newpath = xmalloc(baselen + len + 1);
329 memcpy(newpath, base, baselen);
330 memcpy(newpath + baselen, path, len);
331 newpath[baselen + len] = '\0';
332
333 if (S_ISDIR(mode))
334 path_list_insert(newpath, &current_directory_set);
335 else
336 path_list_insert(newpath, &current_file_set);
337 free(newpath);
338
339 return READ_TREE_RECURSIVE;
340 }
341
342 static int get_files_dirs(struct tree *tree)
343 {
344 int n;
345 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
346 return 0;
347 n = current_file_set.nr + current_directory_set.nr;
348 return n;
349 }
350
351 /*
352 * Returns a index_entry instance which doesn't have to correspond to
353 * a real cache entry in Git's index.
354 */
355 static struct stage_data *insert_stage_data(const char *path,
356 struct tree *o, struct tree *a, struct tree *b,
357 struct path_list *entries)
358 {
359 struct path_list_item *item;
360 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
361 get_tree_entry(o->object.sha1, path,
362 e->stages[1].sha, &e->stages[1].mode);
363 get_tree_entry(a->object.sha1, path,
364 e->stages[2].sha, &e->stages[2].mode);
365 get_tree_entry(b->object.sha1, path,
366 e->stages[3].sha, &e->stages[3].mode);
367 item = path_list_insert(path, entries);
368 item->util = e;
369 return e;
370 }
371
372 /*
373 * Create a dictionary mapping file names to stage_data objects. The
374 * dictionary contains one entry for every path with a non-zero stage entry.
375 */
376 static struct path_list *get_unmerged(void)
377 {
378 struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
379 int i;
380
381 unmerged->strdup_paths = 1;
382 total_cnt += active_nr;
383
384 for (i = 0; i < active_nr; i++, merged_cnt++) {
385 struct path_list_item *item;
386 struct stage_data *e;
387 struct cache_entry *ce = active_cache[i];
388 if (do_progress)
389 display_progress();
390 if (!ce_stage(ce))
391 continue;
392
393 item = path_list_lookup(ce->name, unmerged);
394 if (!item) {
395 item = path_list_insert(ce->name, unmerged);
396 item->util = xcalloc(1, sizeof(struct stage_data));
397 }
398 e = item->util;
399 e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
400 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
401 }
402
403 return unmerged;
404 }
405
406 struct rename
407 {
408 struct diff_filepair *pair;
409 struct stage_data *src_entry;
410 struct stage_data *dst_entry;
411 unsigned processed:1;
412 };
413
414 /*
415 * Get information of all renames which occurred between 'o_tree' and
416 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
417 * 'b_tree') to be able to associate the correct cache entries with
418 * the rename information. 'tree' is always equal to either a_tree or b_tree.
419 */
420 static struct path_list *get_renames(struct tree *tree,
421 struct tree *o_tree,
422 struct tree *a_tree,
423 struct tree *b_tree,
424 struct path_list *entries)
425 {
426 int i;
427 struct path_list *renames;
428 struct diff_options opts;
429
430 renames = xcalloc(1, sizeof(struct path_list));
431 diff_setup(&opts);
432 opts.recursive = 1;
433 opts.detect_rename = DIFF_DETECT_RENAME;
434 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
435 if (diff_setup_done(&opts) < 0)
436 die("diff setup failed");
437 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
438 diffcore_std(&opts);
439 for (i = 0; i < diff_queued_diff.nr; ++i) {
440 struct path_list_item *item;
441 struct rename *re;
442 struct diff_filepair *pair = diff_queued_diff.queue[i];
443 if (pair->status != 'R') {
444 diff_free_filepair(pair);
445 continue;
446 }
447 re = xmalloc(sizeof(*re));
448 re->processed = 0;
449 re->pair = pair;
450 item = path_list_lookup(re->pair->one->path, entries);
451 if (!item)
452 re->src_entry = insert_stage_data(re->pair->one->path,
453 o_tree, a_tree, b_tree, entries);
454 else
455 re->src_entry = item->util;
456
457 item = path_list_lookup(re->pair->two->path, entries);
458 if (!item)
459 re->dst_entry = insert_stage_data(re->pair->two->path,
460 o_tree, a_tree, b_tree, entries);
461 else
462 re->dst_entry = item->util;
463 item = path_list_insert(pair->one->path, renames);
464 item->util = re;
465 }
466 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
467 diff_queued_diff.nr = 0;
468 diff_flush(&opts);
469 return renames;
470 }
471
472 static int update_stages(const char *path, struct diff_filespec *o,
473 struct diff_filespec *a, struct diff_filespec *b,
474 int clear)
475 {
476 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
477 if (clear)
478 if (remove_file_from_cache(path))
479 return -1;
480 if (o)
481 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
482 return -1;
483 if (a)
484 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
485 return -1;
486 if (b)
487 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
488 return -1;
489 return 0;
490 }
491
492 static int remove_path(const char *name)
493 {
494 int ret, len;
495 char *slash, *dirs;
496
497 ret = unlink(name);
498 if (ret)
499 return ret;
500 len = strlen(name);
501 dirs = xmalloc(len+1);
502 memcpy(dirs, name, len);
503 dirs[len] = '\0';
504 while ((slash = strrchr(name, '/'))) {
505 *slash = '\0';
506 len = slash - name;
507 if (rmdir(name) != 0)
508 break;
509 }
510 free(dirs);
511 return ret;
512 }
513
514 static int remove_file(int clean, const char *path, int no_wd)
515 {
516 int update_cache = index_only || clean;
517 int update_working_directory = !index_only && !no_wd;
518
519 if (update_cache) {
520 if (remove_file_from_cache(path))
521 return -1;
522 }
523 if (update_working_directory) {
524 unlink(path);
525 if (errno != ENOENT || errno != EISDIR)
526 return -1;
527 remove_path(path);
528 }
529 return 0;
530 }
531
532 static char *unique_path(const char *path, const char *branch)
533 {
534 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
535 int suffix = 0;
536 struct stat st;
537 char *p = newpath + strlen(path);
538 strcpy(newpath, path);
539 *(p++) = '~';
540 strcpy(p, branch);
541 for (; *p; ++p)
542 if ('/' == *p)
543 *p = '_';
544 while (path_list_has_path(&current_file_set, newpath) ||
545 path_list_has_path(&current_directory_set, newpath) ||
546 lstat(newpath, &st) == 0)
547 sprintf(p, "_%d", suffix++);
548
549 path_list_insert(newpath, &current_file_set);
550 return newpath;
551 }
552
553 static int mkdir_p(const char *path, unsigned long mode)
554 {
555 /* path points to cache entries, so xstrdup before messing with it */
556 char *buf = xstrdup(path);
557 int result = safe_create_leading_directories(buf);
558 free(buf);
559 return result;
560 }
561
562 static void flush_buffer(int fd, const char *buf, unsigned long size)
563 {
564 while (size > 0) {
565 long ret = write_in_full(fd, buf, size);
566 if (ret < 0) {
567 /* Ignore epipe */
568 if (errno == EPIPE)
569 break;
570 die("merge-recursive: %s", strerror(errno));
571 } else if (!ret) {
572 die("merge-recursive: disk full?");
573 }
574 size -= ret;
575 buf += ret;
576 }
577 }
578
579 static void update_file_flags(const unsigned char *sha,
580 unsigned mode,
581 const char *path,
582 int update_cache,
583 int update_wd)
584 {
585 if (index_only)
586 update_wd = 0;
587
588 if (update_wd) {
589 enum object_type type;
590 void *buf;
591 unsigned long size;
592
593 buf = read_sha1_file(sha, &type, &size);
594 if (!buf)
595 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
596 if (type != OBJ_BLOB)
597 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
598
599 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
600 int fd;
601 if (mkdir_p(path, 0777))
602 die("failed to create path %s: %s", path, strerror(errno));
603 unlink(path);
604 if (mode & 0100)
605 mode = 0777;
606 else
607 mode = 0666;
608 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
609 if (fd < 0)
610 die("failed to open %s: %s", path, strerror(errno));
611 flush_buffer(fd, buf, size);
612 close(fd);
613 } else if (S_ISLNK(mode)) {
614 char *lnk = xmalloc(size + 1);
615 memcpy(lnk, buf, size);
616 lnk[size] = '\0';
617 mkdir_p(path, 0777);
618 unlink(path);
619 symlink(lnk, path);
620 free(lnk);
621 } else
622 die("do not know what to do with %06o %s '%s'",
623 mode, sha1_to_hex(sha), path);
624 }
625 if (update_cache)
626 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
627 }
628
629 static void update_file(int clean,
630 const unsigned char *sha,
631 unsigned mode,
632 const char *path)
633 {
634 update_file_flags(sha, mode, path, index_only || clean, !index_only);
635 }
636
637 /* Low level file merging, update and removal */
638
639 struct merge_file_info
640 {
641 unsigned char sha[20];
642 unsigned mode;
643 unsigned clean:1,
644 merge:1;
645 };
646
647 static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
648 {
649 unsigned long size;
650 enum object_type type;
651
652 if (!hashcmp(sha1, null_sha1)) {
653 mm->ptr = xstrdup("");
654 mm->size = 0;
655 return;
656 }
657
658 mm->ptr = read_sha1_file(sha1, &type, &size);
659 if (!mm->ptr || type != OBJ_BLOB)
660 die("unable to read blob object %s", sha1_to_hex(sha1));
661 mm->size = size;
662 }
663
664 /* Low-level merge functions */
665 typedef int (*ll_merge_fn)(const char *cmd,
666 mmfile_t *orig,
667 mmfile_t *src1, const char *name1,
668 mmfile_t *src2, const char *name2,
669 mmbuffer_t *result);
670
671 static int ll_xdl_merge(const char *cmd__unused,
672 mmfile_t *orig,
673 mmfile_t *src1, const char *name1,
674 mmfile_t *src2, const char *name2,
675 mmbuffer_t *result)
676 {
677 xpparam_t xpp;
678
679 memset(&xpp, 0, sizeof(xpp));
680 return xdl_merge(orig,
681 src1, name1,
682 src2, name2,
683 &xpp, XDL_MERGE_ZEALOUS,
684 result);
685 }
686
687 static int ll_union_merge(const char *cmd__unused,
688 mmfile_t *orig,
689 mmfile_t *src1, const char *name1,
690 mmfile_t *src2, const char *name2,
691 mmbuffer_t *result)
692 {
693 char *src, *dst;
694 long size;
695 const int marker_size = 7;
696
697 int status = ll_xdl_merge(cmd__unused, orig,
698 src1, NULL, src2, NULL, result);
699 if (status <= 0)
700 return status;
701 size = result->size;
702 src = dst = result->ptr;
703 while (size) {
704 char ch;
705 if ((marker_size < size) &&
706 (*src == '<' || *src == '=' || *src == '>')) {
707 int i;
708 ch = *src;
709 for (i = 0; i < marker_size; i++)
710 if (src[i] != ch)
711 goto not_a_marker;
712 if (src[marker_size] != '\n')
713 goto not_a_marker;
714 src += marker_size + 1;
715 size -= marker_size + 1;
716 continue;
717 }
718 not_a_marker:
719 do {
720 ch = *src++;
721 *dst++ = ch;
722 size--;
723 } while (ch != '\n' && size);
724 }
725 result->size = dst - result->ptr;
726 return 0;
727 }
728
729 static int ll_binary_merge(const char *cmd__unused,
730 mmfile_t *orig,
731 mmfile_t *src1, const char *name1,
732 mmfile_t *src2, const char *name2,
733 mmbuffer_t *result)
734 {
735 /*
736 * The tentative merge result is "ours" for the final round,
737 * or common ancestor for an internal merge. Still return
738 * "conflicted merge" status.
739 */
740 mmfile_t *stolen = index_only ? orig : src1;
741
742 result->ptr = stolen->ptr;
743 result->size = stolen->size;
744 stolen->ptr = NULL;
745 return 1;
746 }
747
748 static struct {
749 const char *name;
750 ll_merge_fn fn;
751 } ll_merge_fns[] = {
752 { "binary", ll_binary_merge },
753 { "text", ll_xdl_merge },
754 { "union", ll_union_merge },
755 { NULL, NULL },
756 };
757
758 static void create_temp(mmfile_t *src, char *path)
759 {
760 int fd;
761
762 strcpy(path, ".merge_file_XXXXXX");
763 fd = mkstemp(path);
764 if (fd < 0)
765 die("unable to create temp-file");
766 if (write_in_full(fd, src->ptr, src->size) != src->size)
767 die("unable to write temp-file");
768 close(fd);
769 }
770
771 static int ll_ext_merge(const char *cmd,
772 mmfile_t *orig,
773 mmfile_t *src1, const char *name1,
774 mmfile_t *src2, const char *name2,
775 mmbuffer_t *result)
776 {
777 char temp[3][50];
778 char cmdbuf[2048];
779 struct interp table[] = {
780 { "%O" },
781 { "%A" },
782 { "%B" },
783 };
784 struct child_process child;
785 const char *args[20];
786 int status, fd, i;
787 struct stat st;
788
789 result->ptr = NULL;
790 result->size = 0;
791 create_temp(orig, temp[0]);
792 create_temp(src1, temp[1]);
793 create_temp(src2, temp[2]);
794
795 interp_set_entry(table, 0, temp[0]);
796 interp_set_entry(table, 1, temp[1]);
797 interp_set_entry(table, 2, temp[2]);
798
799 interpolate(cmdbuf, sizeof(cmdbuf), cmd, table, 3);
800
801 memset(&child, 0, sizeof(child));
802 child.argv = args;
803 args[0] = "sh";
804 args[1] = "-c";
805 args[2] = cmdbuf;
806 args[3] = NULL;
807
808 status = run_command(&child);
809 if (status < -ERR_RUN_COMMAND_FORK)
810 ; /* failure in run-command */
811 else
812 status = -status;
813 fd = open(temp[1], O_RDONLY);
814 if (fd < 0)
815 goto bad;
816 if (fstat(fd, &st))
817 goto close_bad;
818 result->size = st.st_size;
819 result->ptr = xmalloc(result->size + 1);
820 if (read_in_full(fd, result->ptr, result->size) != result->size) {
821 free(result->ptr);
822 result->ptr = NULL;
823 result->size = 0;
824 }
825 close_bad:
826 close(fd);
827 bad:
828 for (i = 0; i < 3; i++)
829 unlink(temp[i]);
830 return status;
831 }
832
833 /*
834 * merge.default and merge.driver configuration items
835 */
836 static struct user_merge_fn {
837 struct user_merge_fn *next;
838 const char *name;
839 char *cmdline;
840 char b_[1];
841 } *ll_user_merge_fns, **ll_user_merge_fns_tail;
842 static const char *default_ll_merge;
843
844 static int read_merge_config(const char *var, const char *value)
845 {
846 struct user_merge_fn *fn;
847 int blen, nlen;
848
849 if (!strcmp(var, "merge.default")) {
850 default_ll_merge = strdup(value);
851 return 0;
852 }
853
854 if (strcmp(var, "merge.driver"))
855 return 0;
856 if (!value)
857 return error("%s: lacks value", var);
858 /*
859 * merge.driver is a multi-valued configuration, whose value is
860 * of form:
861 *
862 * name command-line
863 *
864 * The command-line will be interpolated with the following
865 * tokens and is given to the shell:
866 *
867 * %O - temporary file name for the merge base.
868 * %A - temporary file name for our version.
869 * %B - temporary file name for the other branches' version.
870 *
871 * The external merge driver should write the results in the file
872 * named by %A, and signal that it has done with exit status 0.
873 */
874 for (nlen = -1, blen = 0; value[blen]; blen++)
875 if (nlen < 0 && isspace(value[blen]))
876 nlen = blen;
877 if (nlen < 0)
878 return error("%s '%s': lacks command line", var, value);
879 fn = xcalloc(1, sizeof(struct user_merge_fn) + blen + 1);
880 memcpy(fn->b_, value, blen + 1);
881 fn->name = fn->b_;
882 fn->b_[nlen] = 0;
883 fn->cmdline = fn->b_ + nlen + 1;
884 fn->next = *ll_user_merge_fns_tail;
885 *ll_user_merge_fns_tail = fn;
886 return 0;
887 }
888
889 static void initialize_ll_merge(void)
890 {
891 if (ll_user_merge_fns_tail)
892 return;
893 ll_user_merge_fns_tail = &ll_user_merge_fns;
894 git_config(read_merge_config);
895 }
896
897 static ll_merge_fn find_ll_merge_fn(void *merge_attr, const char **cmdline)
898 {
899 struct user_merge_fn *fn;
900 const char *name;
901 int i;
902
903 initialize_ll_merge();
904
905 if (ATTR_TRUE(merge_attr))
906 return ll_xdl_merge;
907 else if (ATTR_FALSE(merge_attr))
908 return ll_binary_merge;
909 else if (ATTR_UNSET(merge_attr)) {
910 if (!default_ll_merge)
911 return ll_xdl_merge;
912 else
913 name = default_ll_merge;
914 }
915 else
916 name = merge_attr;
917
918 for (fn = ll_user_merge_fns; fn; fn = fn->next) {
919 if (!strcmp(fn->name, name)) {
920 *cmdline = fn->cmdline;
921 return ll_ext_merge;
922 }
923 }
924
925 for (i = 0; ll_merge_fns[i].name; i++)
926 if (!strcmp(ll_merge_fns[i].name, name))
927 return ll_merge_fns[i].fn;
928
929 /* default to the 3-way */
930 return ll_xdl_merge;
931 }
932
933 static void *git_path_check_merge(const char *path)
934 {
935 static struct git_attr_check attr_merge_check;
936
937 if (!attr_merge_check.attr)
938 attr_merge_check.attr = git_attr("merge", 5);
939
940 if (git_checkattr(path, 1, &attr_merge_check))
941 return ATTR__UNSET;
942 return attr_merge_check.value;
943 }
944
945 static int ll_merge(mmbuffer_t *result_buf,
946 struct diff_filespec *o,
947 struct diff_filespec *a,
948 struct diff_filespec *b,
949 const char *branch1,
950 const char *branch2)
951 {
952 mmfile_t orig, src1, src2;
953 char *name1, *name2;
954 int merge_status;
955 void *merge_attr;
956 ll_merge_fn fn;
957 const char *driver = NULL;
958
959 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
960 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
961
962 fill_mm(o->sha1, &orig);
963 fill_mm(a->sha1, &src1);
964 fill_mm(b->sha1, &src2);
965
966 merge_attr = git_path_check_merge(a->path);
967 fn = find_ll_merge_fn(merge_attr, &driver);
968
969 merge_status = fn(driver, &orig,
970 &src1, name1, &src2, name2, result_buf);
971
972 free(name1);
973 free(name2);
974 free(orig.ptr);
975 free(src1.ptr);
976 free(src2.ptr);
977 return merge_status;
978 }
979
980 static struct merge_file_info merge_file(struct diff_filespec *o,
981 struct diff_filespec *a, struct diff_filespec *b,
982 const char *branch1, const char *branch2)
983 {
984 struct merge_file_info result;
985 result.merge = 0;
986 result.clean = 1;
987
988 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
989 result.clean = 0;
990 if (S_ISREG(a->mode)) {
991 result.mode = a->mode;
992 hashcpy(result.sha, a->sha1);
993 } else {
994 result.mode = b->mode;
995 hashcpy(result.sha, b->sha1);
996 }
997 } else {
998 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
999 result.merge = 1;
1000
1001 result.mode = a->mode == o->mode ? b->mode: a->mode;
1002
1003 if (sha_eq(a->sha1, o->sha1))
1004 hashcpy(result.sha, b->sha1);
1005 else if (sha_eq(b->sha1, o->sha1))
1006 hashcpy(result.sha, a->sha1);
1007 else if (S_ISREG(a->mode)) {
1008 mmbuffer_t result_buf;
1009 int merge_status;
1010
1011 merge_status = ll_merge(&result_buf, o, a, b,
1012 branch1, branch2);
1013
1014 if ((merge_status < 0) || !result_buf.ptr)
1015 die("Failed to execute internal merge");
1016
1017 if (write_sha1_file(result_buf.ptr, result_buf.size,
1018 blob_type, result.sha))
1019 die("Unable to add %s to database",
1020 a->path);
1021
1022 free(result_buf.ptr);
1023 result.clean = (merge_status == 0);
1024 } else {
1025 if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
1026 die("cannot merge modes?");
1027
1028 hashcpy(result.sha, a->sha1);
1029
1030 if (!sha_eq(a->sha1, b->sha1))
1031 result.clean = 0;
1032 }
1033 }
1034
1035 return result;
1036 }
1037
1038 static void conflict_rename_rename(struct rename *ren1,
1039 const char *branch1,
1040 struct rename *ren2,
1041 const char *branch2)
1042 {
1043 char *del[2];
1044 int delp = 0;
1045 const char *ren1_dst = ren1->pair->two->path;
1046 const char *ren2_dst = ren2->pair->two->path;
1047 const char *dst_name1 = ren1_dst;
1048 const char *dst_name2 = ren2_dst;
1049 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1050 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
1051 output(1, "%s is a directory in %s added as %s instead",
1052 ren1_dst, branch2, dst_name1);
1053 remove_file(0, ren1_dst, 0);
1054 }
1055 if (path_list_has_path(&current_directory_set, ren2_dst)) {
1056 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
1057 output(1, "%s is a directory in %s added as %s instead",
1058 ren2_dst, branch1, dst_name2);
1059 remove_file(0, ren2_dst, 0);
1060 }
1061 if (index_only) {
1062 remove_file_from_cache(dst_name1);
1063 remove_file_from_cache(dst_name2);
1064 /*
1065 * Uncomment to leave the conflicting names in the resulting tree
1066 *
1067 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
1068 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
1069 */
1070 } else {
1071 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
1072 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
1073 }
1074 while (delp--)
1075 free(del[delp]);
1076 }
1077
1078 static void conflict_rename_dir(struct rename *ren1,
1079 const char *branch1)
1080 {
1081 char *new_path = unique_path(ren1->pair->two->path, branch1);
1082 output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
1083 remove_file(0, ren1->pair->two->path, 0);
1084 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
1085 free(new_path);
1086 }
1087
1088 static void conflict_rename_rename_2(struct rename *ren1,
1089 const char *branch1,
1090 struct rename *ren2,
1091 const char *branch2)
1092 {
1093 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
1094 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
1095 output(1, "Renamed %s to %s and %s to %s instead",
1096 ren1->pair->one->path, new_path1,
1097 ren2->pair->one->path, new_path2);
1098 remove_file(0, ren1->pair->two->path, 0);
1099 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
1100 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
1101 free(new_path2);
1102 free(new_path1);
1103 }
1104
1105 static int process_renames(struct path_list *a_renames,
1106 struct path_list *b_renames,
1107 const char *a_branch,
1108 const char *b_branch)
1109 {
1110 int clean_merge = 1, i, j;
1111 struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
1112 const struct rename *sre;
1113
1114 for (i = 0; i < a_renames->nr; i++) {
1115 sre = a_renames->items[i].util;
1116 path_list_insert(sre->pair->two->path, &a_by_dst)->util
1117 = sre->dst_entry;
1118 }
1119 for (i = 0; i < b_renames->nr; i++) {
1120 sre = b_renames->items[i].util;
1121 path_list_insert(sre->pair->two->path, &b_by_dst)->util
1122 = sre->dst_entry;
1123 }
1124
1125 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
1126 int compare;
1127 char *src;
1128 struct path_list *renames1, *renames2, *renames2Dst;
1129 struct rename *ren1 = NULL, *ren2 = NULL;
1130 const char *branch1, *branch2;
1131 const char *ren1_src, *ren1_dst;
1132
1133 if (i >= a_renames->nr) {
1134 compare = 1;
1135 ren2 = b_renames->items[j++].util;
1136 } else if (j >= b_renames->nr) {
1137 compare = -1;
1138 ren1 = a_renames->items[i++].util;
1139 } else {
1140 compare = strcmp(a_renames->items[i].path,
1141 b_renames->items[j].path);
1142 if (compare <= 0)
1143 ren1 = a_renames->items[i++].util;
1144 if (compare >= 0)
1145 ren2 = b_renames->items[j++].util;
1146 }
1147
1148 /* TODO: refactor, so that 1/2 are not needed */
1149 if (ren1) {
1150 renames1 = a_renames;
1151 renames2 = b_renames;
1152 renames2Dst = &b_by_dst;
1153 branch1 = a_branch;
1154 branch2 = b_branch;
1155 } else {
1156 struct rename *tmp;
1157 renames1 = b_renames;
1158 renames2 = a_renames;
1159 renames2Dst = &a_by_dst;
1160 branch1 = b_branch;
1161 branch2 = a_branch;
1162 tmp = ren2;
1163 ren2 = ren1;
1164 ren1 = tmp;
1165 }
1166 src = ren1->pair->one->path;
1167
1168 ren1->dst_entry->processed = 1;
1169 ren1->src_entry->processed = 1;
1170
1171 if (ren1->processed)
1172 continue;
1173 ren1->processed = 1;
1174
1175 ren1_src = ren1->pair->one->path;
1176 ren1_dst = ren1->pair->two->path;
1177
1178 if (ren2) {
1179 const char *ren2_src = ren2->pair->one->path;
1180 const char *ren2_dst = ren2->pair->two->path;
1181 /* Renamed in 1 and renamed in 2 */
1182 if (strcmp(ren1_src, ren2_src) != 0)
1183 die("ren1.src != ren2.src");
1184 ren2->dst_entry->processed = 1;
1185 ren2->processed = 1;
1186 if (strcmp(ren1_dst, ren2_dst) != 0) {
1187 clean_merge = 0;
1188 output(1, "CONFLICT (rename/rename): "
1189 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1190 "rename \"%s\"->\"%s\" in \"%s\"%s",
1191 src, ren1_dst, branch1,
1192 src, ren2_dst, branch2,
1193 index_only ? " (left unresolved)": "");
1194 if (index_only) {
1195 remove_file_from_cache(src);
1196 update_file(0, ren1->pair->one->sha1,
1197 ren1->pair->one->mode, src);
1198 }
1199 conflict_rename_rename(ren1, branch1, ren2, branch2);
1200 } else {
1201 struct merge_file_info mfi;
1202 remove_file(1, ren1_src, 1);
1203 mfi = merge_file(ren1->pair->one,
1204 ren1->pair->two,
1205 ren2->pair->two,
1206 branch1,
1207 branch2);
1208 if (mfi.merge || !mfi.clean)
1209 output(1, "Renamed %s->%s", src, ren1_dst);
1210
1211 if (mfi.merge)
1212 output(2, "Auto-merged %s", ren1_dst);
1213
1214 if (!mfi.clean) {
1215 output(1, "CONFLICT (content): merge conflict in %s",
1216 ren1_dst);
1217 clean_merge = 0;
1218
1219 if (!index_only)
1220 update_stages(ren1_dst,
1221 ren1->pair->one,
1222 ren1->pair->two,
1223 ren2->pair->two,
1224 1 /* clear */);
1225 }
1226 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1227 }
1228 } else {
1229 /* Renamed in 1, maybe changed in 2 */
1230 struct path_list_item *item;
1231 /* we only use sha1 and mode of these */
1232 struct diff_filespec src_other, dst_other;
1233 int try_merge, stage = a_renames == renames1 ? 3: 2;
1234
1235 remove_file(1, ren1_src, index_only || stage == 3);
1236
1237 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
1238 src_other.mode = ren1->src_entry->stages[stage].mode;
1239 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
1240 dst_other.mode = ren1->dst_entry->stages[stage].mode;
1241
1242 try_merge = 0;
1243
1244 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1245 clean_merge = 0;
1246 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
1247 " directory %s added in %s",
1248 ren1_src, ren1_dst, branch1,
1249 ren1_dst, branch2);
1250 conflict_rename_dir(ren1, branch1);
1251 } else if (sha_eq(src_other.sha1, null_sha1)) {
1252 clean_merge = 0;
1253 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
1254 "and deleted in %s",
1255 ren1_src, ren1_dst, branch1,
1256 branch2);
1257 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
1258 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
1259 const char *new_path;
1260 clean_merge = 0;
1261 try_merge = 1;
1262 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
1263 "%s added in %s",
1264 ren1_src, ren1_dst, branch1,
1265 ren1_dst, branch2);
1266 new_path = unique_path(ren1_dst, branch2);
1267 output(1, "Added as %s instead", new_path);
1268 update_file(0, dst_other.sha1, dst_other.mode, new_path);
1269 } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
1270 ren2 = item->util;
1271 clean_merge = 0;
1272 ren2->processed = 1;
1273 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
1274 "Renamed %s->%s in %s",
1275 ren1_src, ren1_dst, branch1,
1276 ren2->pair->one->path, ren2->pair->two->path, branch2);
1277 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
1278 } else
1279 try_merge = 1;
1280
1281 if (try_merge) {
1282 struct diff_filespec *o, *a, *b;
1283 struct merge_file_info mfi;
1284 src_other.path = (char *)ren1_src;
1285
1286 o = ren1->pair->one;
1287 if (a_renames == renames1) {
1288 a = ren1->pair->two;
1289 b = &src_other;
1290 } else {
1291 b = ren1->pair->two;
1292 a = &src_other;
1293 }
1294 mfi = merge_file(o, a, b,
1295 a_branch, b_branch);
1296
1297 if (mfi.merge || !mfi.clean)
1298 output(1, "Renamed %s => %s", ren1_src, ren1_dst);
1299 if (mfi.merge)
1300 output(2, "Auto-merged %s", ren1_dst);
1301 if (!mfi.clean) {
1302 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
1303 ren1_dst);
1304 clean_merge = 0;
1305
1306 if (!index_only)
1307 update_stages(ren1_dst,
1308 o, a, b, 1);
1309 }
1310 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1311 }
1312 }
1313 }
1314 path_list_clear(&a_by_dst, 0);
1315 path_list_clear(&b_by_dst, 0);
1316
1317 return clean_merge;
1318 }
1319
1320 static unsigned char *has_sha(const unsigned char *sha)
1321 {
1322 return is_null_sha1(sha) ? NULL: (unsigned char *)sha;
1323 }
1324
1325 /* Per entry merge function */
1326 static int process_entry(const char *path, struct stage_data *entry,
1327 const char *branch1,
1328 const char *branch2)
1329 {
1330 /*
1331 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1332 print_index_entry("\tpath: ", entry);
1333 */
1334 int clean_merge = 1;
1335 unsigned char *o_sha = has_sha(entry->stages[1].sha);
1336 unsigned char *a_sha = has_sha(entry->stages[2].sha);
1337 unsigned char *b_sha = has_sha(entry->stages[3].sha);
1338 unsigned o_mode = entry->stages[1].mode;
1339 unsigned a_mode = entry->stages[2].mode;
1340 unsigned b_mode = entry->stages[3].mode;
1341
1342 if (o_sha && (!a_sha || !b_sha)) {
1343 /* Case A: Deleted in one */
1344 if ((!a_sha && !b_sha) ||
1345 (sha_eq(a_sha, o_sha) && !b_sha) ||
1346 (!a_sha && sha_eq(b_sha, o_sha))) {
1347 /* Deleted in both or deleted in one and
1348 * unchanged in the other */
1349 if (a_sha)
1350 output(2, "Removed %s", path);
1351 /* do not touch working file if it did not exist */
1352 remove_file(1, path, !a_sha);
1353 } else {
1354 /* Deleted in one and changed in the other */
1355 clean_merge = 0;
1356 if (!a_sha) {
1357 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1358 "and modified in %s. Version %s of %s left in tree.",
1359 path, branch1,
1360 branch2, branch2, path);
1361 update_file(0, b_sha, b_mode, path);
1362 } else {
1363 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1364 "and modified in %s. Version %s of %s left in tree.",
1365 path, branch2,
1366 branch1, branch1, path);
1367 update_file(0, a_sha, a_mode, path);
1368 }
1369 }
1370
1371 } else if ((!o_sha && a_sha && !b_sha) ||
1372 (!o_sha && !a_sha && b_sha)) {
1373 /* Case B: Added in one. */
1374 const char *add_branch;
1375 const char *other_branch;
1376 unsigned mode;
1377 const unsigned char *sha;
1378 const char *conf;
1379
1380 if (a_sha) {
1381 add_branch = branch1;
1382 other_branch = branch2;
1383 mode = a_mode;
1384 sha = a_sha;
1385 conf = "file/directory";
1386 } else {
1387 add_branch = branch2;
1388 other_branch = branch1;
1389 mode = b_mode;
1390 sha = b_sha;
1391 conf = "directory/file";
1392 }
1393 if (path_list_has_path(&current_directory_set, path)) {
1394 const char *new_path = unique_path(path, add_branch);
1395 clean_merge = 0;
1396 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1397 "Added %s as %s",
1398 conf, path, other_branch, path, new_path);
1399 remove_file(0, path, 0);
1400 update_file(0, sha, mode, new_path);
1401 } else {
1402 output(2, "Added %s", path);
1403 update_file(1, sha, mode, path);
1404 }
1405 } else if (a_sha && b_sha) {
1406 /* Case C: Added in both (check for same permissions) and */
1407 /* case D: Modified in both, but differently. */
1408 const char *reason = "content";
1409 struct merge_file_info mfi;
1410 struct diff_filespec o, a, b;
1411
1412 if (!o_sha) {
1413 reason = "add/add";
1414 o_sha = (unsigned char *)null_sha1;
1415 }
1416 output(2, "Auto-merged %s", path);
1417 o.path = a.path = b.path = (char *)path;
1418 hashcpy(o.sha1, o_sha);
1419 o.mode = o_mode;
1420 hashcpy(a.sha1, a_sha);
1421 a.mode = a_mode;
1422 hashcpy(b.sha1, b_sha);
1423 b.mode = b_mode;
1424
1425 mfi = merge_file(&o, &a, &b,
1426 branch1, branch2);
1427
1428 if (mfi.clean)
1429 update_file(1, mfi.sha, mfi.mode, path);
1430 else {
1431 clean_merge = 0;
1432 output(1, "CONFLICT (%s): Merge conflict in %s",
1433 reason, path);
1434
1435 if (index_only)
1436 update_file(0, mfi.sha, mfi.mode, path);
1437 else
1438 update_file_flags(mfi.sha, mfi.mode, path,
1439 0 /* update_cache */, 1 /* update_working_directory */);
1440 }
1441 } else
1442 die("Fatal merge failure, shouldn't happen.");
1443
1444 return clean_merge;
1445 }
1446
1447 static int merge_trees(struct tree *head,
1448 struct tree *merge,
1449 struct tree *common,
1450 const char *branch1,
1451 const char *branch2,
1452 struct tree **result)
1453 {
1454 int code, clean;
1455
1456 if (subtree_merge) {
1457 merge = shift_tree_object(head, merge);
1458 common = shift_tree_object(head, common);
1459 }
1460
1461 if (sha_eq(common->object.sha1, merge->object.sha1)) {
1462 output(0, "Already uptodate!");
1463 *result = head;
1464 return 1;
1465 }
1466
1467 code = git_merge_trees(index_only, common, head, merge);
1468
1469 if (code != 0)
1470 die("merging of trees %s and %s failed",
1471 sha1_to_hex(head->object.sha1),
1472 sha1_to_hex(merge->object.sha1));
1473
1474 if (unmerged_index()) {
1475 struct path_list *entries, *re_head, *re_merge;
1476 int i;
1477 path_list_clear(&current_file_set, 1);
1478 path_list_clear(&current_directory_set, 1);
1479 get_files_dirs(head);
1480 get_files_dirs(merge);
1481
1482 entries = get_unmerged();
1483 re_head = get_renames(head, common, head, merge, entries);
1484 re_merge = get_renames(merge, common, head, merge, entries);
1485 clean = process_renames(re_head, re_merge,
1486 branch1, branch2);
1487 total_cnt += entries->nr;
1488 for (i = 0; i < entries->nr; i++, merged_cnt++) {
1489 const char *path = entries->items[i].path;
1490 struct stage_data *e = entries->items[i].util;
1491 if (!e->processed
1492 && !process_entry(path, e, branch1, branch2))
1493 clean = 0;
1494 if (do_progress)
1495 display_progress();
1496 }
1497
1498 path_list_clear(re_merge, 0);
1499 path_list_clear(re_head, 0);
1500 path_list_clear(entries, 1);
1501
1502 }
1503 else
1504 clean = 1;
1505
1506 if (index_only)
1507 *result = git_write_tree();
1508
1509 return clean;
1510 }
1511
1512 static struct commit_list *reverse_commit_list(struct commit_list *list)
1513 {
1514 struct commit_list *next = NULL, *current, *backup;
1515 for (current = list; current; current = backup) {
1516 backup = current->next;
1517 current->next = next;
1518 next = current;
1519 }
1520 return next;
1521 }
1522
1523 /*
1524 * Merge the commits h1 and h2, return the resulting virtual
1525 * commit object and a flag indicating the cleanness of the merge.
1526 */
1527 static int merge(struct commit *h1,
1528 struct commit *h2,
1529 const char *branch1,
1530 const char *branch2,
1531 struct commit_list *ca,
1532 struct commit **result)
1533 {
1534 struct commit_list *iter;
1535 struct commit *merged_common_ancestors;
1536 struct tree *mrtree;
1537 int clean;
1538
1539 if (show(4)) {
1540 output(4, "Merging:");
1541 output_commit_title(h1);
1542 output_commit_title(h2);
1543 }
1544
1545 if (!ca) {
1546 ca = get_merge_bases(h1, h2, 1);
1547 ca = reverse_commit_list(ca);
1548 }
1549
1550 if (show(5)) {
1551 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1552 for (iter = ca; iter; iter = iter->next)
1553 output_commit_title(iter->item);
1554 }
1555
1556 merged_common_ancestors = pop_commit(&ca);
1557 if (merged_common_ancestors == NULL) {
1558 /* if there is no common ancestor, make an empty tree */
1559 struct tree *tree = xcalloc(1, sizeof(struct tree));
1560
1561 tree->object.parsed = 1;
1562 tree->object.type = OBJ_TREE;
1563 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
1564 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1565 }
1566
1567 for (iter = ca; iter; iter = iter->next) {
1568 call_depth++;
1569 /*
1570 * When the merge fails, the result contains files
1571 * with conflict markers. The cleanness flag is
1572 * ignored, it was never actually used, as result of
1573 * merge_trees has always overwritten it: the committed
1574 * "conflicts" were already resolved.
1575 */
1576 discard_cache();
1577 merge(merged_common_ancestors, iter->item,
1578 "Temporary merge branch 1",
1579 "Temporary merge branch 2",
1580 NULL,
1581 &merged_common_ancestors);
1582 call_depth--;
1583
1584 if (!merged_common_ancestors)
1585 die("merge returned no commit");
1586 }
1587
1588 discard_cache();
1589 if (!call_depth) {
1590 read_cache();
1591 index_only = 0;
1592 } else
1593 index_only = 1;
1594
1595 clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
1596 branch1, branch2, &mrtree);
1597
1598 if (index_only) {
1599 *result = make_virtual_commit(mrtree, "merged tree");
1600 commit_list_insert(h1, &(*result)->parents);
1601 commit_list_insert(h2, &(*result)->parents->next);
1602 }
1603 if (!call_depth && do_progress) {
1604 /* Make sure we end at 100% */
1605 if (!total_cnt)
1606 total_cnt = 1;
1607 merged_cnt = total_cnt;
1608 progress_update = 1;
1609 display_progress();
1610 fputc('\n', stderr);
1611 }
1612 flush_output();
1613 return clean;
1614 }
1615
1616 static const char *better_branch_name(const char *branch)
1617 {
1618 static char githead_env[8 + 40 + 1];
1619 char *name;
1620
1621 if (strlen(branch) != 40)
1622 return branch;
1623 sprintf(githead_env, "GITHEAD_%s", branch);
1624 name = getenv(githead_env);
1625 return name ? name : branch;
1626 }
1627
1628 static struct commit *get_ref(const char *ref)
1629 {
1630 unsigned char sha1[20];
1631 struct object *object;
1632
1633 if (get_sha1(ref, sha1))
1634 die("Could not resolve ref '%s'", ref);
1635 object = deref_tag(parse_object(sha1), ref, strlen(ref));
1636 if (object->type == OBJ_TREE)
1637 return make_virtual_commit((struct tree*)object,
1638 better_branch_name(ref));
1639 if (object->type != OBJ_COMMIT)
1640 return NULL;
1641 if (parse_commit((struct commit *)object))
1642 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1643 return (struct commit *)object;
1644 }
1645
1646 static int merge_config(const char *var, const char *value)
1647 {
1648 if (!strcasecmp(var, "merge.verbosity")) {
1649 verbosity = git_config_int(var, value);
1650 return 0;
1651 }
1652 return git_default_config(var, value);
1653 }
1654
1655 int main(int argc, char *argv[])
1656 {
1657 static const char *bases[20];
1658 static unsigned bases_count = 0;
1659 int i, clean;
1660 const char *branch1, *branch2;
1661 struct commit *result, *h1, *h2;
1662 struct commit_list *ca = NULL;
1663 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1664 int index_fd;
1665
1666 if (argv[0]) {
1667 int namelen = strlen(argv[0]);
1668 if (8 < namelen &&
1669 !strcmp(argv[0] + namelen - 8, "-subtree"))
1670 subtree_merge = 1;
1671 }
1672
1673 git_config(merge_config);
1674 if (getenv("GIT_MERGE_VERBOSITY"))
1675 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
1676
1677 if (argc < 4)
1678 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1679
1680 for (i = 1; i < argc; ++i) {
1681 if (!strcmp(argv[i], "--"))
1682 break;
1683 if (bases_count < sizeof(bases)/sizeof(*bases))
1684 bases[bases_count++] = argv[i];
1685 }
1686 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1687 die("Not handling anything other than two heads merge.");
1688 if (verbosity >= 5) {
1689 buffer_output = 0;
1690 do_progress = 0;
1691 }
1692 else
1693 do_progress = isatty(1);
1694
1695 branch1 = argv[++i];
1696 branch2 = argv[++i];
1697
1698 h1 = get_ref(branch1);
1699 h2 = get_ref(branch2);
1700
1701 branch1 = better_branch_name(branch1);
1702 branch2 = better_branch_name(branch2);
1703
1704 if (do_progress)
1705 setup_progress_signal();
1706 if (show(3))
1707 printf("Merging %s with %s\n", branch1, branch2);
1708
1709 index_fd = hold_locked_index(lock, 1);
1710
1711 for (i = 0; i < bases_count; i++) {
1712 struct commit *ancestor = get_ref(bases[i]);
1713 ca = commit_list_insert(ancestor, &ca);
1714 }
1715 clean = merge(h1, h2, branch1, branch2, ca, &result);
1716
1717 if (active_cache_changed &&
1718 (write_cache(index_fd, active_cache, active_nr) ||
1719 close(index_fd) || commit_locked_index(lock)))
1720 die ("unable to write %s", get_index_file());
1721
1722 return clean ? 0: 1;
1723 }