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