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