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