checkout,merge: disallow overwriting ignored files with --no-overwrite-ignore
[git/jnareb-git.git] / builtin / merge.c
blob07102c45fa6df150b029b0e9f7f3e11831716123
1 /*
2 * Builtin "git merge"
4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
7 */
9 #include "cache.h"
10 #include "parse-options.h"
11 #include "builtin.h"
12 #include "run-command.h"
13 #include "diff.h"
14 #include "refs.h"
15 #include "commit.h"
16 #include "diffcore.h"
17 #include "revision.h"
18 #include "unpack-trees.h"
19 #include "cache-tree.h"
20 #include "dir.h"
21 #include "utf8.h"
22 #include "log-tree.h"
23 #include "color.h"
24 #include "rerere.h"
25 #include "help.h"
26 #include "merge-recursive.h"
27 #include "resolve-undo.h"
28 #include "remote.h"
30 #define DEFAULT_TWOHEAD (1<<0)
31 #define DEFAULT_OCTOPUS (1<<1)
32 #define NO_FAST_FORWARD (1<<2)
33 #define NO_TRIVIAL (1<<3)
35 struct strategy {
36 const char *name;
37 unsigned attr;
40 static const char * const builtin_merge_usage[] = {
41 "git merge [options] [<commit>...]",
42 "git merge [options] <msg> HEAD <commit>",
43 "git merge --abort",
44 NULL
47 static int show_diffstat = 1, shortlog_len, squash;
48 static int option_commit = 1, allow_fast_forward = 1;
49 static int fast_forward_only, option_edit;
50 static int allow_trivial = 1, have_message;
51 static int overwrite_ignore = 1;
52 static struct strbuf merge_msg;
53 static struct commit_list *remoteheads;
54 static struct strategy **use_strategies;
55 static size_t use_strategies_nr, use_strategies_alloc;
56 static const char **xopts;
57 static size_t xopts_nr, xopts_alloc;
58 static const char *branch;
59 static char *branch_mergeoptions;
60 static int option_renormalize;
61 static int verbosity;
62 static int allow_rerere_auto;
63 static int abort_current_merge;
64 static int show_progress = -1;
65 static int default_to_upstream;
67 static struct strategy all_strategy[] = {
68 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
69 { "octopus", DEFAULT_OCTOPUS },
70 { "resolve", 0 },
71 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
72 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
75 static const char *pull_twohead, *pull_octopus;
77 static int option_parse_message(const struct option *opt,
78 const char *arg, int unset)
80 struct strbuf *buf = opt->value;
82 if (unset)
83 strbuf_setlen(buf, 0);
84 else if (arg) {
85 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
86 have_message = 1;
87 } else
88 return error(_("switch `m' requires a value"));
89 return 0;
92 static struct strategy *get_strategy(const char *name)
94 int i;
95 struct strategy *ret;
96 static struct cmdnames main_cmds, other_cmds;
97 static int loaded;
99 if (!name)
100 return NULL;
102 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
103 if (!strcmp(name, all_strategy[i].name))
104 return &all_strategy[i];
106 if (!loaded) {
107 struct cmdnames not_strategies;
108 loaded = 1;
110 memset(&not_strategies, 0, sizeof(struct cmdnames));
111 load_command_list("git-merge-", &main_cmds, &other_cmds);
112 for (i = 0; i < main_cmds.cnt; i++) {
113 int j, found = 0;
114 struct cmdname *ent = main_cmds.names[i];
115 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
116 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
117 && !all_strategy[j].name[ent->len])
118 found = 1;
119 if (!found)
120 add_cmdname(&not_strategies, ent->name, ent->len);
122 exclude_cmds(&main_cmds, &not_strategies);
124 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
125 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
126 fprintf(stderr, _("Available strategies are:"));
127 for (i = 0; i < main_cmds.cnt; i++)
128 fprintf(stderr, " %s", main_cmds.names[i]->name);
129 fprintf(stderr, ".\n");
130 if (other_cmds.cnt) {
131 fprintf(stderr, _("Available custom strategies are:"));
132 for (i = 0; i < other_cmds.cnt; i++)
133 fprintf(stderr, " %s", other_cmds.names[i]->name);
134 fprintf(stderr, ".\n");
136 exit(1);
139 ret = xcalloc(1, sizeof(struct strategy));
140 ret->name = xstrdup(name);
141 ret->attr = NO_TRIVIAL;
142 return ret;
145 static void append_strategy(struct strategy *s)
147 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
148 use_strategies[use_strategies_nr++] = s;
151 static int option_parse_strategy(const struct option *opt,
152 const char *name, int unset)
154 if (unset)
155 return 0;
157 append_strategy(get_strategy(name));
158 return 0;
161 static int option_parse_x(const struct option *opt,
162 const char *arg, int unset)
164 if (unset)
165 return 0;
167 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
168 xopts[xopts_nr++] = xstrdup(arg);
169 return 0;
172 static int option_parse_n(const struct option *opt,
173 const char *arg, int unset)
175 show_diffstat = unset;
176 return 0;
179 static struct option builtin_merge_options[] = {
180 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
181 "do not show a diffstat at the end of the merge",
182 PARSE_OPT_NOARG, option_parse_n },
183 OPT_BOOLEAN(0, "stat", &show_diffstat,
184 "show a diffstat at the end of the merge"),
185 OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"),
186 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
187 "add (at most <n>) entries from shortlog to merge commit message",
188 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
189 OPT_BOOLEAN(0, "squash", &squash,
190 "create a single commit instead of doing a merge"),
191 OPT_BOOLEAN(0, "commit", &option_commit,
192 "perform a commit if the merge succeeds (default)"),
193 OPT_BOOLEAN('e', "edit", &option_edit,
194 "edit message before committing"),
195 OPT_BOOLEAN(0, "ff", &allow_fast_forward,
196 "allow fast-forward (default)"),
197 OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
198 "abort if fast-forward is not possible"),
199 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
200 OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
201 "merge strategy to use", option_parse_strategy),
202 OPT_CALLBACK('X', "strategy-option", &xopts, "option=value",
203 "option for selected merge strategy", option_parse_x),
204 OPT_CALLBACK('m', "message", &merge_msg, "message",
205 "merge commit message (for a non-fast-forward merge)",
206 option_parse_message),
207 OPT__VERBOSITY(&verbosity),
208 OPT_BOOLEAN(0, "abort", &abort_current_merge,
209 "abort the current in-progress merge"),
210 OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1),
211 OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, "update ignored files (default)"),
212 OPT_END()
215 /* Cleans up metadata that is uninteresting after a succeeded merge. */
216 static void drop_save(void)
218 unlink(git_path("MERGE_HEAD"));
219 unlink(git_path("MERGE_MSG"));
220 unlink(git_path("MERGE_MODE"));
223 static int save_state(unsigned char *stash)
225 int len;
226 struct child_process cp;
227 struct strbuf buffer = STRBUF_INIT;
228 const char *argv[] = {"stash", "create", NULL};
230 memset(&cp, 0, sizeof(cp));
231 cp.argv = argv;
232 cp.out = -1;
233 cp.git_cmd = 1;
235 if (start_command(&cp))
236 die(_("could not run stash."));
237 len = strbuf_read(&buffer, cp.out, 1024);
238 close(cp.out);
240 if (finish_command(&cp) || len < 0)
241 die(_("stash failed"));
242 else if (!len) /* no changes */
243 return -1;
244 strbuf_setlen(&buffer, buffer.len-1);
245 if (get_sha1(buffer.buf, stash))
246 die(_("not a valid object: %s"), buffer.buf);
247 return 0;
250 static void read_empty(unsigned const char *sha1, int verbose)
252 int i = 0;
253 const char *args[7];
255 args[i++] = "read-tree";
256 if (verbose)
257 args[i++] = "-v";
258 args[i++] = "-m";
259 args[i++] = "-u";
260 args[i++] = EMPTY_TREE_SHA1_HEX;
261 args[i++] = sha1_to_hex(sha1);
262 args[i] = NULL;
264 if (run_command_v_opt(args, RUN_GIT_CMD))
265 die(_("read-tree failed"));
268 static void reset_hard(unsigned const char *sha1, int verbose)
270 int i = 0;
271 const char *args[6];
273 args[i++] = "read-tree";
274 if (verbose)
275 args[i++] = "-v";
276 args[i++] = "--reset";
277 args[i++] = "-u";
278 args[i++] = sha1_to_hex(sha1);
279 args[i] = NULL;
281 if (run_command_v_opt(args, RUN_GIT_CMD))
282 die(_("read-tree failed"));
285 static void restore_state(const unsigned char *head,
286 const unsigned char *stash)
288 struct strbuf sb = STRBUF_INIT;
289 const char *args[] = { "stash", "apply", NULL, NULL };
291 if (is_null_sha1(stash))
292 return;
294 reset_hard(head, 1);
296 args[2] = sha1_to_hex(stash);
299 * It is OK to ignore error here, for example when there was
300 * nothing to restore.
302 run_command_v_opt(args, RUN_GIT_CMD);
304 strbuf_release(&sb);
305 refresh_cache(REFRESH_QUIET);
308 /* This is called when no merge was necessary. */
309 static void finish_up_to_date(const char *msg)
311 if (verbosity >= 0)
312 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
313 drop_save();
316 static void squash_message(struct commit *commit)
318 struct rev_info rev;
319 struct strbuf out = STRBUF_INIT;
320 struct commit_list *j;
321 const char *filename;
322 int fd;
323 struct pretty_print_context ctx = {0};
325 printf(_("Squash commit -- not updating HEAD\n"));
326 filename = git_path("SQUASH_MSG");
327 fd = open(filename, O_WRONLY | O_CREAT, 0666);
328 if (fd < 0)
329 die_errno(_("Could not write to '%s'"), filename);
331 init_revisions(&rev, NULL);
332 rev.ignore_merges = 1;
333 rev.commit_format = CMIT_FMT_MEDIUM;
335 commit->object.flags |= UNINTERESTING;
336 add_pending_object(&rev, &commit->object, NULL);
338 for (j = remoteheads; j; j = j->next)
339 add_pending_object(&rev, &j->item->object, NULL);
341 setup_revisions(0, NULL, &rev, NULL);
342 if (prepare_revision_walk(&rev))
343 die(_("revision walk setup failed"));
345 ctx.abbrev = rev.abbrev;
346 ctx.date_mode = rev.date_mode;
347 ctx.fmt = rev.commit_format;
349 strbuf_addstr(&out, "Squashed commit of the following:\n");
350 while ((commit = get_revision(&rev)) != NULL) {
351 strbuf_addch(&out, '\n');
352 strbuf_addf(&out, "commit %s\n",
353 sha1_to_hex(commit->object.sha1));
354 pretty_print_commit(&ctx, commit, &out);
356 if (write(fd, out.buf, out.len) < 0)
357 die_errno(_("Writing SQUASH_MSG"));
358 if (close(fd))
359 die_errno(_("Finishing SQUASH_MSG"));
360 strbuf_release(&out);
363 static void finish(struct commit *head_commit,
364 const unsigned char *new_head, const char *msg)
366 struct strbuf reflog_message = STRBUF_INIT;
367 const unsigned char *head = head_commit->object.sha1;
369 if (!msg)
370 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
371 else {
372 if (verbosity >= 0)
373 printf("%s\n", msg);
374 strbuf_addf(&reflog_message, "%s: %s",
375 getenv("GIT_REFLOG_ACTION"), msg);
377 if (squash) {
378 squash_message(head_commit);
379 } else {
380 if (verbosity >= 0 && !merge_msg.len)
381 printf(_("No merge message -- not updating HEAD\n"));
382 else {
383 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
384 update_ref(reflog_message.buf, "HEAD",
385 new_head, head, 0,
386 DIE_ON_ERR);
388 * We ignore errors in 'gc --auto', since the
389 * user should see them.
391 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
394 if (new_head && show_diffstat) {
395 struct diff_options opts;
396 diff_setup(&opts);
397 opts.output_format |=
398 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
399 opts.detect_rename = DIFF_DETECT_RENAME;
400 if (diff_setup_done(&opts) < 0)
401 die(_("diff_setup_done failed"));
402 diff_tree_sha1(head, new_head, "", &opts);
403 diffcore_std(&opts);
404 diff_flush(&opts);
407 /* Run a post-merge hook */
408 run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
410 strbuf_release(&reflog_message);
413 static struct object *want_commit(const char *name)
415 struct object *obj;
416 unsigned char sha1[20];
417 if (get_sha1(name, sha1))
418 return NULL;
419 obj = parse_object(sha1);
420 return peel_to_type(name, 0, obj, OBJ_COMMIT);
423 /* Get the name for the merge commit's message. */
424 static void merge_name(const char *remote, struct strbuf *msg)
426 struct object *remote_head;
427 unsigned char branch_head[20], buf_sha[20];
428 struct strbuf buf = STRBUF_INIT;
429 struct strbuf bname = STRBUF_INIT;
430 const char *ptr;
431 char *found_ref;
432 int len, early;
434 strbuf_branchname(&bname, remote);
435 remote = bname.buf;
437 memset(branch_head, 0, sizeof(branch_head));
438 remote_head = want_commit(remote);
439 if (!remote_head)
440 die(_("'%s' does not point to a commit"), remote);
442 if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
443 if (!prefixcmp(found_ref, "refs/heads/")) {
444 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
445 sha1_to_hex(branch_head), remote);
446 goto cleanup;
448 if (!prefixcmp(found_ref, "refs/remotes/")) {
449 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
450 sha1_to_hex(branch_head), remote);
451 goto cleanup;
455 /* See if remote matches <name>^^^.. or <name>~<number> */
456 for (len = 0, ptr = remote + strlen(remote);
457 remote < ptr && ptr[-1] == '^';
458 ptr--)
459 len++;
460 if (len)
461 early = 1;
462 else {
463 early = 0;
464 ptr = strrchr(remote, '~');
465 if (ptr) {
466 int seen_nonzero = 0;
468 len++; /* count ~ */
469 while (*++ptr && isdigit(*ptr)) {
470 seen_nonzero |= (*ptr != '0');
471 len++;
473 if (*ptr)
474 len = 0; /* not ...~<number> */
475 else if (seen_nonzero)
476 early = 1;
477 else if (len == 1)
478 early = 1; /* "name~" is "name~1"! */
481 if (len) {
482 struct strbuf truname = STRBUF_INIT;
483 strbuf_addstr(&truname, "refs/heads/");
484 strbuf_addstr(&truname, remote);
485 strbuf_setlen(&truname, truname.len - len);
486 if (resolve_ref(truname.buf, buf_sha, 1, NULL)) {
487 strbuf_addf(msg,
488 "%s\t\tbranch '%s'%s of .\n",
489 sha1_to_hex(remote_head->sha1),
490 truname.buf + 11,
491 (early ? " (early part)" : ""));
492 strbuf_release(&truname);
493 goto cleanup;
497 if (!strcmp(remote, "FETCH_HEAD") &&
498 !access(git_path("FETCH_HEAD"), R_OK)) {
499 const char *filename;
500 FILE *fp;
501 struct strbuf line = STRBUF_INIT;
502 char *ptr;
504 filename = git_path("FETCH_HEAD");
505 fp = fopen(filename, "r");
506 if (!fp)
507 die_errno(_("could not open '%s' for reading"),
508 filename);
509 strbuf_getline(&line, fp, '\n');
510 fclose(fp);
511 ptr = strstr(line.buf, "\tnot-for-merge\t");
512 if (ptr)
513 strbuf_remove(&line, ptr-line.buf+1, 13);
514 strbuf_addbuf(msg, &line);
515 strbuf_release(&line);
516 goto cleanup;
518 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
519 sha1_to_hex(remote_head->sha1), remote);
520 cleanup:
521 strbuf_release(&buf);
522 strbuf_release(&bname);
525 static void parse_branch_merge_options(char *bmo)
527 const char **argv;
528 int argc;
530 if (!bmo)
531 return;
532 argc = split_cmdline(bmo, &argv);
533 if (argc < 0)
534 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
535 split_cmdline_strerror(argc));
536 argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
537 memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
538 argc++;
539 argv[0] = "branch.*.mergeoptions";
540 parse_options(argc, argv, NULL, builtin_merge_options,
541 builtin_merge_usage, 0);
542 free(argv);
545 static int git_merge_config(const char *k, const char *v, void *cb)
547 if (branch && !prefixcmp(k, "branch.") &&
548 !prefixcmp(k + 7, branch) &&
549 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
550 free(branch_mergeoptions);
551 branch_mergeoptions = xstrdup(v);
552 return 0;
555 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
556 show_diffstat = git_config_bool(k, v);
557 else if (!strcmp(k, "pull.twohead"))
558 return git_config_string(&pull_twohead, k, v);
559 else if (!strcmp(k, "pull.octopus"))
560 return git_config_string(&pull_octopus, k, v);
561 else if (!strcmp(k, "merge.renormalize"))
562 option_renormalize = git_config_bool(k, v);
563 else if (!strcmp(k, "merge.log") || !strcmp(k, "merge.summary")) {
564 int is_bool;
565 shortlog_len = git_config_bool_or_int(k, v, &is_bool);
566 if (!is_bool && shortlog_len < 0)
567 return error(_("%s: negative length %s"), k, v);
568 if (is_bool && shortlog_len)
569 shortlog_len = DEFAULT_MERGE_LOG_LEN;
570 return 0;
571 } else if (!strcmp(k, "merge.ff")) {
572 int boolval = git_config_maybe_bool(k, v);
573 if (0 <= boolval) {
574 allow_fast_forward = boolval;
575 } else if (v && !strcmp(v, "only")) {
576 allow_fast_forward = 1;
577 fast_forward_only = 1;
578 } /* do not barf on values from future versions of git */
579 return 0;
580 } else if (!strcmp(k, "merge.defaulttoupstream")) {
581 default_to_upstream = git_config_bool(k, v);
582 return 0;
584 return git_diff_ui_config(k, v, cb);
587 static int read_tree_trivial(unsigned char *common, unsigned char *head,
588 unsigned char *one)
590 int i, nr_trees = 0;
591 struct tree *trees[MAX_UNPACK_TREES];
592 struct tree_desc t[MAX_UNPACK_TREES];
593 struct unpack_trees_options opts;
595 memset(&opts, 0, sizeof(opts));
596 opts.head_idx = 2;
597 opts.src_index = &the_index;
598 opts.dst_index = &the_index;
599 opts.update = 1;
600 opts.verbose_update = 1;
601 opts.trivial_merges_only = 1;
602 opts.merge = 1;
603 trees[nr_trees] = parse_tree_indirect(common);
604 if (!trees[nr_trees++])
605 return -1;
606 trees[nr_trees] = parse_tree_indirect(head);
607 if (!trees[nr_trees++])
608 return -1;
609 trees[nr_trees] = parse_tree_indirect(one);
610 if (!trees[nr_trees++])
611 return -1;
612 opts.fn = threeway_merge;
613 cache_tree_free(&active_cache_tree);
614 for (i = 0; i < nr_trees; i++) {
615 parse_tree(trees[i]);
616 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
618 if (unpack_trees(nr_trees, t, &opts))
619 return -1;
620 return 0;
623 static void write_tree_trivial(unsigned char *sha1)
625 if (write_cache_as_tree(sha1, 0, NULL))
626 die(_("git write-tree failed to write a tree"));
629 static const char *merge_argument(struct commit *commit)
631 if (commit)
632 return sha1_to_hex(commit->object.sha1);
633 else
634 return EMPTY_TREE_SHA1_HEX;
637 int try_merge_command(const char *strategy, size_t xopts_nr,
638 const char **xopts, struct commit_list *common,
639 const char *head_arg, struct commit_list *remotes)
641 const char **args;
642 int i = 0, x = 0, ret;
643 struct commit_list *j;
644 struct strbuf buf = STRBUF_INIT;
646 args = xmalloc((4 + xopts_nr + commit_list_count(common) +
647 commit_list_count(remotes)) * sizeof(char *));
648 strbuf_addf(&buf, "merge-%s", strategy);
649 args[i++] = buf.buf;
650 for (x = 0; x < xopts_nr; x++) {
651 char *s = xmalloc(strlen(xopts[x])+2+1);
652 strcpy(s, "--");
653 strcpy(s+2, xopts[x]);
654 args[i++] = s;
656 for (j = common; j; j = j->next)
657 args[i++] = xstrdup(merge_argument(j->item));
658 args[i++] = "--";
659 args[i++] = head_arg;
660 for (j = remotes; j; j = j->next)
661 args[i++] = xstrdup(merge_argument(j->item));
662 args[i] = NULL;
663 ret = run_command_v_opt(args, RUN_GIT_CMD);
664 strbuf_release(&buf);
665 i = 1;
666 for (x = 0; x < xopts_nr; x++)
667 free((void *)args[i++]);
668 for (j = common; j; j = j->next)
669 free((void *)args[i++]);
670 i += 2;
671 for (j = remotes; j; j = j->next)
672 free((void *)args[i++]);
673 free(args);
674 discard_cache();
675 if (read_cache() < 0)
676 die(_("failed to read the cache"));
677 resolve_undo_clear();
679 return ret;
682 static int try_merge_strategy(const char *strategy, struct commit_list *common,
683 struct commit *head, const char *head_arg)
685 int index_fd;
686 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
688 index_fd = hold_locked_index(lock, 1);
689 refresh_cache(REFRESH_QUIET);
690 if (active_cache_changed &&
691 (write_cache(index_fd, active_cache, active_nr) ||
692 commit_locked_index(lock)))
693 return error(_("Unable to write index."));
694 rollback_lock_file(lock);
696 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
697 int clean, x;
698 struct commit *result;
699 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
700 int index_fd;
701 struct commit_list *reversed = NULL;
702 struct merge_options o;
703 struct commit_list *j;
705 if (remoteheads->next) {
706 error(_("Not handling anything other than two heads merge."));
707 return 2;
710 init_merge_options(&o);
711 if (!strcmp(strategy, "subtree"))
712 o.subtree_shift = "";
714 o.renormalize = option_renormalize;
715 o.show_rename_progress =
716 show_progress == -1 ? isatty(2) : show_progress;
718 for (x = 0; x < xopts_nr; x++)
719 if (parse_merge_opt(&o, xopts[x]))
720 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
722 o.branch1 = head_arg;
723 o.branch2 = remoteheads->item->util;
725 for (j = common; j; j = j->next)
726 commit_list_insert(j->item, &reversed);
728 index_fd = hold_locked_index(lock, 1);
729 clean = merge_recursive(&o, head,
730 remoteheads->item, reversed, &result);
731 if (active_cache_changed &&
732 (write_cache(index_fd, active_cache, active_nr) ||
733 commit_locked_index(lock)))
734 die (_("unable to write %s"), get_index_file());
735 rollback_lock_file(lock);
736 return clean ? 0 : 1;
737 } else {
738 return try_merge_command(strategy, xopts_nr, xopts,
739 common, head_arg, remoteheads);
743 static void count_diff_files(struct diff_queue_struct *q,
744 struct diff_options *opt, void *data)
746 int *count = data;
748 (*count) += q->nr;
751 static int count_unmerged_entries(void)
753 int i, ret = 0;
755 for (i = 0; i < active_nr; i++)
756 if (ce_stage(active_cache[i]))
757 ret++;
759 return ret;
762 int checkout_fast_forward(const unsigned char *head, const unsigned char *remote)
764 struct tree *trees[MAX_UNPACK_TREES];
765 struct unpack_trees_options opts;
766 struct tree_desc t[MAX_UNPACK_TREES];
767 int i, fd, nr_trees = 0;
768 struct dir_struct dir;
769 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
771 refresh_cache(REFRESH_QUIET);
773 fd = hold_locked_index(lock_file, 1);
775 memset(&trees, 0, sizeof(trees));
776 memset(&opts, 0, sizeof(opts));
777 memset(&t, 0, sizeof(t));
778 if (overwrite_ignore) {
779 memset(&dir, 0, sizeof(dir));
780 dir.flags |= DIR_SHOW_IGNORED;
781 setup_standard_excludes(&dir);
782 opts.dir = &dir;
785 opts.head_idx = 1;
786 opts.src_index = &the_index;
787 opts.dst_index = &the_index;
788 opts.update = 1;
789 opts.verbose_update = 1;
790 opts.merge = 1;
791 opts.fn = twoway_merge;
792 setup_unpack_trees_porcelain(&opts, "merge");
794 trees[nr_trees] = parse_tree_indirect(head);
795 if (!trees[nr_trees++])
796 return -1;
797 trees[nr_trees] = parse_tree_indirect(remote);
798 if (!trees[nr_trees++])
799 return -1;
800 for (i = 0; i < nr_trees; i++) {
801 parse_tree(trees[i]);
802 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
804 if (unpack_trees(nr_trees, t, &opts))
805 return -1;
806 if (write_cache(fd, active_cache, active_nr) ||
807 commit_locked_index(lock_file))
808 die(_("unable to write new index file"));
809 return 0;
812 static void split_merge_strategies(const char *string, struct strategy **list,
813 int *nr, int *alloc)
815 char *p, *q, *buf;
817 if (!string)
818 return;
820 buf = xstrdup(string);
821 q = buf;
822 for (;;) {
823 p = strchr(q, ' ');
824 if (!p) {
825 ALLOC_GROW(*list, *nr + 1, *alloc);
826 (*list)[(*nr)++].name = xstrdup(q);
827 free(buf);
828 return;
829 } else {
830 *p = '\0';
831 ALLOC_GROW(*list, *nr + 1, *alloc);
832 (*list)[(*nr)++].name = xstrdup(q);
833 q = ++p;
838 static void add_strategies(const char *string, unsigned attr)
840 struct strategy *list = NULL;
841 int list_alloc = 0, list_nr = 0, i;
843 memset(&list, 0, sizeof(list));
844 split_merge_strategies(string, &list, &list_nr, &list_alloc);
845 if (list) {
846 for (i = 0; i < list_nr; i++)
847 append_strategy(get_strategy(list[i].name));
848 return;
850 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
851 if (all_strategy[i].attr & attr)
852 append_strategy(&all_strategy[i]);
856 static void write_merge_msg(struct strbuf *msg)
858 const char *filename = git_path("MERGE_MSG");
859 int fd = open(filename, O_WRONLY | O_CREAT, 0666);
860 if (fd < 0)
861 die_errno(_("Could not open '%s' for writing"),
862 filename);
863 if (write_in_full(fd, msg->buf, msg->len) != msg->len)
864 die_errno(_("Could not write to '%s'"), filename);
865 close(fd);
868 static void read_merge_msg(struct strbuf *msg)
870 const char *filename = git_path("MERGE_MSG");
871 strbuf_reset(msg);
872 if (strbuf_read_file(msg, filename, 0) < 0)
873 die_errno(_("Could not read from '%s'"), filename);
876 static void write_merge_state(void);
877 static void abort_commit(const char *err_msg)
879 if (err_msg)
880 error("%s", err_msg);
881 fprintf(stderr,
882 _("Not committing merge; use 'git commit' to complete the merge.\n"));
883 write_merge_state();
884 exit(1);
887 static void prepare_to_commit(void)
889 struct strbuf msg = STRBUF_INIT;
890 strbuf_addbuf(&msg, &merge_msg);
891 strbuf_addch(&msg, '\n');
892 write_merge_msg(&msg);
893 run_hook(get_index_file(), "prepare-commit-msg",
894 git_path("MERGE_MSG"), "merge", NULL, NULL);
895 if (option_edit) {
896 if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
897 abort_commit(NULL);
899 read_merge_msg(&msg);
900 stripspace(&msg, option_edit);
901 if (!msg.len)
902 abort_commit(_("Empty commit message."));
903 strbuf_release(&merge_msg);
904 strbuf_addbuf(&merge_msg, &msg);
905 strbuf_release(&msg);
908 static int merge_trivial(struct commit *head)
910 unsigned char result_tree[20], result_commit[20];
911 struct commit_list *parent = xmalloc(sizeof(*parent));
913 write_tree_trivial(result_tree);
914 printf(_("Wonderful.\n"));
915 parent->item = head;
916 parent->next = xmalloc(sizeof(*parent->next));
917 parent->next->item = remoteheads->item;
918 parent->next->next = NULL;
919 prepare_to_commit();
920 commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
921 finish(head, result_commit, "In-index merge");
922 drop_save();
923 return 0;
926 static int finish_automerge(struct commit *head,
927 struct commit_list *common,
928 unsigned char *result_tree,
929 const char *wt_strategy)
931 struct commit_list *parents = NULL, *j;
932 struct strbuf buf = STRBUF_INIT;
933 unsigned char result_commit[20];
935 free_commit_list(common);
936 if (allow_fast_forward) {
937 parents = remoteheads;
938 commit_list_insert(head, &parents);
939 parents = reduce_heads(parents);
940 } else {
941 struct commit_list **pptr = &parents;
943 pptr = &commit_list_insert(head,
944 pptr)->next;
945 for (j = remoteheads; j; j = j->next)
946 pptr = &commit_list_insert(j->item, pptr)->next;
948 strbuf_addch(&merge_msg, '\n');
949 prepare_to_commit();
950 free_commit_list(remoteheads);
951 commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
952 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
953 finish(head, result_commit, buf.buf);
954 strbuf_release(&buf);
955 drop_save();
956 return 0;
959 static int suggest_conflicts(int renormalizing)
961 const char *filename;
962 FILE *fp;
963 int pos;
965 filename = git_path("MERGE_MSG");
966 fp = fopen(filename, "a");
967 if (!fp)
968 die_errno(_("Could not open '%s' for writing"), filename);
969 fprintf(fp, "\nConflicts:\n");
970 for (pos = 0; pos < active_nr; pos++) {
971 struct cache_entry *ce = active_cache[pos];
973 if (ce_stage(ce)) {
974 fprintf(fp, "\t%s\n", ce->name);
975 while (pos + 1 < active_nr &&
976 !strcmp(ce->name,
977 active_cache[pos + 1]->name))
978 pos++;
981 fclose(fp);
982 rerere(allow_rerere_auto);
983 printf(_("Automatic merge failed; "
984 "fix conflicts and then commit the result.\n"));
985 return 1;
988 static struct commit *is_old_style_invocation(int argc, const char **argv,
989 const unsigned char *head)
991 struct commit *second_token = NULL;
992 if (argc > 2) {
993 unsigned char second_sha1[20];
995 if (get_sha1(argv[1], second_sha1))
996 return NULL;
997 second_token = lookup_commit_reference_gently(second_sha1, 0);
998 if (!second_token)
999 die(_("'%s' is not a commit"), argv[1]);
1000 if (hashcmp(second_token->object.sha1, head))
1001 return NULL;
1003 return second_token;
1006 static int evaluate_result(void)
1008 int cnt = 0;
1009 struct rev_info rev;
1011 /* Check how many files differ. */
1012 init_revisions(&rev, "");
1013 setup_revisions(0, NULL, &rev, NULL);
1014 rev.diffopt.output_format |=
1015 DIFF_FORMAT_CALLBACK;
1016 rev.diffopt.format_callback = count_diff_files;
1017 rev.diffopt.format_callback_data = &cnt;
1018 run_diff_files(&rev, 0);
1021 * Check how many unmerged entries are
1022 * there.
1024 cnt += count_unmerged_entries();
1026 return cnt;
1030 * Pretend as if the user told us to merge with the tracking
1031 * branch we have for the upstream of the current branch
1033 static int setup_with_upstream(const char ***argv)
1035 struct branch *branch = branch_get(NULL);
1036 int i;
1037 const char **args;
1039 if (!branch)
1040 die(_("No current branch."));
1041 if (!branch->remote)
1042 die(_("No remote for the current branch."));
1043 if (!branch->merge_nr)
1044 die(_("No default upstream defined for the current branch."));
1046 args = xcalloc(branch->merge_nr + 1, sizeof(char *));
1047 for (i = 0; i < branch->merge_nr; i++) {
1048 if (!branch->merge[i]->dst)
1049 die(_("No remote tracking branch for %s from %s"),
1050 branch->merge[i]->src, branch->remote_name);
1051 args[i] = branch->merge[i]->dst;
1053 args[i] = NULL;
1054 *argv = args;
1055 return i;
1058 static void write_merge_state(void)
1060 const char *filename;
1061 int fd;
1062 struct commit_list *j;
1063 struct strbuf buf = STRBUF_INIT;
1065 for (j = remoteheads; j; j = j->next)
1066 strbuf_addf(&buf, "%s\n",
1067 sha1_to_hex(j->item->object.sha1));
1068 filename = git_path("MERGE_HEAD");
1069 fd = open(filename, O_WRONLY | O_CREAT, 0666);
1070 if (fd < 0)
1071 die_errno(_("Could not open '%s' for writing"), filename);
1072 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1073 die_errno(_("Could not write to '%s'"), filename);
1074 close(fd);
1075 strbuf_addch(&merge_msg, '\n');
1076 write_merge_msg(&merge_msg);
1078 filename = git_path("MERGE_MODE");
1079 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1080 if (fd < 0)
1081 die_errno(_("Could not open '%s' for writing"), filename);
1082 strbuf_reset(&buf);
1083 if (!allow_fast_forward)
1084 strbuf_addf(&buf, "no-ff");
1085 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1086 die_errno(_("Could not write to '%s'"), filename);
1087 close(fd);
1090 int cmd_merge(int argc, const char **argv, const char *prefix)
1092 unsigned char result_tree[20];
1093 unsigned char stash[20];
1094 unsigned char head_sha1[20];
1095 struct commit *head_commit;
1096 struct strbuf buf = STRBUF_INIT;
1097 const char *head_arg;
1098 int flag, i;
1099 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1100 struct commit_list *common = NULL;
1101 const char *best_strategy = NULL, *wt_strategy = NULL;
1102 struct commit_list **remotes = &remoteheads;
1104 if (argc == 2 && !strcmp(argv[1], "-h"))
1105 usage_with_options(builtin_merge_usage, builtin_merge_options);
1108 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1109 * current branch.
1111 branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1112 if (branch && !prefixcmp(branch, "refs/heads/"))
1113 branch += 11;
1114 if (!branch || is_null_sha1(head_sha1))
1115 head_commit = NULL;
1116 else
1117 head_commit = lookup_commit_or_die(head_sha1, "HEAD");
1119 git_config(git_merge_config, NULL);
1121 if (branch_mergeoptions)
1122 parse_branch_merge_options(branch_mergeoptions);
1123 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1124 builtin_merge_usage, 0);
1126 if (verbosity < 0 && show_progress == -1)
1127 show_progress = 0;
1129 if (abort_current_merge) {
1130 int nargc = 2;
1131 const char *nargv[] = {"reset", "--merge", NULL};
1133 if (!file_exists(git_path("MERGE_HEAD")))
1134 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1136 /* Invoke 'git reset --merge' */
1137 return cmd_reset(nargc, nargv, prefix);
1140 if (read_cache_unmerged())
1141 die_resolve_conflict("merge");
1143 if (file_exists(git_path("MERGE_HEAD"))) {
1145 * There is no unmerged entry, don't advise 'git
1146 * add/rm <file>', just 'git commit'.
1148 if (advice_resolve_conflict)
1149 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1150 "Please, commit your changes before you can merge."));
1151 else
1152 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1154 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1155 if (advice_resolve_conflict)
1156 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1157 "Please, commit your changes before you can merge."));
1158 else
1159 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1161 resolve_undo_clear();
1163 if (verbosity < 0)
1164 show_diffstat = 0;
1166 if (squash) {
1167 if (!allow_fast_forward)
1168 die(_("You cannot combine --squash with --no-ff."));
1169 option_commit = 0;
1172 if (!allow_fast_forward && fast_forward_only)
1173 die(_("You cannot combine --no-ff with --ff-only."));
1175 if (!abort_current_merge) {
1176 if (!argc && default_to_upstream)
1177 argc = setup_with_upstream(&argv);
1178 else if (argc == 1 && !strcmp(argv[0], "-"))
1179 argv[0] = "@{-1}";
1181 if (!argc)
1182 usage_with_options(builtin_merge_usage,
1183 builtin_merge_options);
1186 * This could be traditional "merge <msg> HEAD <commit>..." and
1187 * the way we can tell it is to see if the second token is HEAD,
1188 * but some people might have misused the interface and used a
1189 * committish that is the same as HEAD there instead.
1190 * Traditional format never would have "-m" so it is an
1191 * additional safety measure to check for it.
1194 if (!have_message && head_commit &&
1195 is_old_style_invocation(argc, argv, head_commit->object.sha1)) {
1196 strbuf_addstr(&merge_msg, argv[0]);
1197 head_arg = argv[1];
1198 argv += 2;
1199 argc -= 2;
1200 } else if (!head_commit) {
1201 struct object *remote_head;
1203 * If the merged head is a valid one there is no reason
1204 * to forbid "git merge" into a branch yet to be born.
1205 * We do the same for "git pull".
1207 if (argc != 1)
1208 die(_("Can merge only exactly one commit into "
1209 "empty head"));
1210 if (squash)
1211 die(_("Squash commit into empty head not supported yet"));
1212 if (!allow_fast_forward)
1213 die(_("Non-fast-forward commit does not make sense into "
1214 "an empty head"));
1215 remote_head = want_commit(argv[0]);
1216 if (!remote_head)
1217 die(_("%s - not something we can merge"), argv[0]);
1218 read_empty(remote_head->sha1, 0);
1219 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
1220 DIE_ON_ERR);
1221 return 0;
1222 } else {
1223 struct strbuf merge_names = STRBUF_INIT;
1225 /* We are invoked directly as the first-class UI. */
1226 head_arg = "HEAD";
1229 * All the rest are the commits being merged;
1230 * prepare the standard merge summary message to
1231 * be appended to the given message. If remote
1232 * is invalid we will die later in the common
1233 * codepath so we discard the error in this
1234 * loop.
1236 for (i = 0; i < argc; i++)
1237 merge_name(argv[i], &merge_names);
1239 if (!have_message || shortlog_len) {
1240 fmt_merge_msg(&merge_names, &merge_msg, !have_message,
1241 shortlog_len);
1242 if (merge_msg.len)
1243 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1247 if (!head_commit || !argc)
1248 usage_with_options(builtin_merge_usage,
1249 builtin_merge_options);
1251 strbuf_addstr(&buf, "merge");
1252 for (i = 0; i < argc; i++)
1253 strbuf_addf(&buf, " %s", argv[i]);
1254 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1255 strbuf_reset(&buf);
1257 for (i = 0; i < argc; i++) {
1258 struct object *o;
1259 struct commit *commit;
1261 o = want_commit(argv[i]);
1262 if (!o)
1263 die(_("%s - not something we can merge"), argv[i]);
1264 commit = lookup_commit(o->sha1);
1265 commit->util = (void *)argv[i];
1266 remotes = &commit_list_insert(commit, remotes)->next;
1268 strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
1269 setenv(buf.buf, argv[i], 1);
1270 strbuf_reset(&buf);
1273 if (!use_strategies) {
1274 if (!remoteheads->next)
1275 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1276 else
1277 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1280 for (i = 0; i < use_strategies_nr; i++) {
1281 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1282 allow_fast_forward = 0;
1283 if (use_strategies[i]->attr & NO_TRIVIAL)
1284 allow_trivial = 0;
1287 if (!remoteheads->next)
1288 common = get_merge_bases(head_commit, remoteheads->item, 1);
1289 else {
1290 struct commit_list *list = remoteheads;
1291 commit_list_insert(head_commit, &list);
1292 common = get_octopus_merge_bases(list);
1293 free(list);
1296 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1,
1297 NULL, 0, DIE_ON_ERR);
1299 if (!common)
1300 ; /* No common ancestors found. We need a real merge. */
1301 else if (!remoteheads->next && !common->next &&
1302 common->item == remoteheads->item) {
1304 * If head can reach all the merge then we are up to date.
1305 * but first the most common case of merging one remote.
1307 finish_up_to_date("Already up-to-date.");
1308 return 0;
1309 } else if (allow_fast_forward && !remoteheads->next &&
1310 !common->next &&
1311 !hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
1312 /* Again the most common case of merging one remote. */
1313 struct strbuf msg = STRBUF_INIT;
1314 struct object *o;
1315 char hex[41];
1317 strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
1319 if (verbosity >= 0)
1320 printf(_("Updating %s..%s\n"),
1321 hex,
1322 find_unique_abbrev(remoteheads->item->object.sha1,
1323 DEFAULT_ABBREV));
1324 strbuf_addstr(&msg, "Fast-forward");
1325 if (have_message)
1326 strbuf_addstr(&msg,
1327 " (no commit created; -m option ignored)");
1328 o = want_commit(sha1_to_hex(remoteheads->item->object.sha1));
1329 if (!o)
1330 return 1;
1332 if (checkout_fast_forward(head_commit->object.sha1, remoteheads->item->object.sha1))
1333 return 1;
1335 finish(head_commit, o->sha1, msg.buf);
1336 drop_save();
1337 return 0;
1338 } else if (!remoteheads->next && common->next)
1341 * We are not doing octopus and not fast-forward. Need
1342 * a real merge.
1344 else if (!remoteheads->next && !common->next && option_commit) {
1346 * We are not doing octopus, not fast-forward, and have
1347 * only one common.
1349 refresh_cache(REFRESH_QUIET);
1350 if (allow_trivial && !fast_forward_only) {
1351 /* See if it is really trivial. */
1352 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1353 printf(_("Trying really trivial in-index merge...\n"));
1354 if (!read_tree_trivial(common->item->object.sha1,
1355 head_commit->object.sha1, remoteheads->item->object.sha1))
1356 return merge_trivial(head_commit);
1357 printf(_("Nope.\n"));
1359 } else {
1361 * An octopus. If we can reach all the remote we are up
1362 * to date.
1364 int up_to_date = 1;
1365 struct commit_list *j;
1367 for (j = remoteheads; j; j = j->next) {
1368 struct commit_list *common_one;
1371 * Here we *have* to calculate the individual
1372 * merge_bases again, otherwise "git merge HEAD^
1373 * HEAD^^" would be missed.
1375 common_one = get_merge_bases(head_commit, j->item, 1);
1376 if (hashcmp(common_one->item->object.sha1,
1377 j->item->object.sha1)) {
1378 up_to_date = 0;
1379 break;
1382 if (up_to_date) {
1383 finish_up_to_date("Already up-to-date. Yeeah!");
1384 return 0;
1388 if (fast_forward_only)
1389 die(_("Not possible to fast-forward, aborting."));
1391 /* We are going to make a new commit. */
1392 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1395 * At this point, we need a real merge. No matter what strategy
1396 * we use, it would operate on the index, possibly affecting the
1397 * working tree, and when resolved cleanly, have the desired
1398 * tree in the index -- this means that the index must be in
1399 * sync with the head commit. The strategies are responsible
1400 * to ensure this.
1402 if (use_strategies_nr == 1 ||
1404 * Stash away the local changes so that we can try more than one.
1406 save_state(stash))
1407 hashcpy(stash, null_sha1);
1409 for (i = 0; i < use_strategies_nr; i++) {
1410 int ret;
1411 if (i) {
1412 printf(_("Rewinding the tree to pristine...\n"));
1413 restore_state(head_commit->object.sha1, stash);
1415 if (use_strategies_nr != 1)
1416 printf(_("Trying merge strategy %s...\n"),
1417 use_strategies[i]->name);
1419 * Remember which strategy left the state in the working
1420 * tree.
1422 wt_strategy = use_strategies[i]->name;
1424 ret = try_merge_strategy(use_strategies[i]->name,
1425 common, head_commit, head_arg);
1426 if (!option_commit && !ret) {
1427 merge_was_ok = 1;
1429 * This is necessary here just to avoid writing
1430 * the tree, but later we will *not* exit with
1431 * status code 1 because merge_was_ok is set.
1433 ret = 1;
1436 if (ret) {
1438 * The backend exits with 1 when conflicts are
1439 * left to be resolved, with 2 when it does not
1440 * handle the given merge at all.
1442 if (ret == 1) {
1443 int cnt = evaluate_result();
1445 if (best_cnt <= 0 || cnt <= best_cnt) {
1446 best_strategy = use_strategies[i]->name;
1447 best_cnt = cnt;
1450 if (merge_was_ok)
1451 break;
1452 else
1453 continue;
1456 /* Automerge succeeded. */
1457 write_tree_trivial(result_tree);
1458 automerge_was_ok = 1;
1459 break;
1463 * If we have a resulting tree, that means the strategy module
1464 * auto resolved the merge cleanly.
1466 if (automerge_was_ok)
1467 return finish_automerge(head_commit, common, result_tree,
1468 wt_strategy);
1471 * Pick the result from the best strategy and have the user fix
1472 * it up.
1474 if (!best_strategy) {
1475 restore_state(head_commit->object.sha1, stash);
1476 if (use_strategies_nr > 1)
1477 fprintf(stderr,
1478 _("No merge strategy handled the merge.\n"));
1479 else
1480 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1481 use_strategies[0]->name);
1482 return 2;
1483 } else if (best_strategy == wt_strategy)
1484 ; /* We already have its result in the working tree. */
1485 else {
1486 printf(_("Rewinding the tree to pristine...\n"));
1487 restore_state(head_commit->object.sha1, stash);
1488 printf(_("Using the %s to prepare resolving by hand.\n"),
1489 best_strategy);
1490 try_merge_strategy(best_strategy, common, head_commit, head_arg);
1493 if (squash)
1494 finish(head_commit, NULL, NULL);
1495 else
1496 write_merge_state();
1498 if (merge_was_ok) {
1499 fprintf(stderr, _("Automatic merge went well; "
1500 "stopped before committing as requested\n"));
1501 return 0;
1502 } else
1503 return suggest_conflicts(option_renormalize);