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