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