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