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