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