]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/merge.c
Merge branch 'jh/notes-fanout-fix'
[thirdparty/git.git] / builtin / merge.c
CommitLineData
1c7b76be
MV
1/*
2 * Builtin "git merge"
3 *
4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
5 *
6 * Based on git-merge.sh by Junio C Hamano.
7 */
8
f8adbec9 9#define USE_THE_INDEX_COMPATIBILITY_MACROS
1c7b76be 10#include "cache.h"
b2141fc1 11#include "config.h"
1c7b76be
MV
12#include "parse-options.h"
13#include "builtin.h"
697cc8ef 14#include "lockfile.h"
1c7b76be
MV
15#include "run-command.h"
16#include "diff.h"
17#include "refs.h"
ec0cb496 18#include "refspec.h"
1c7b76be
MV
19#include "commit.h"
20#include "diffcore.h"
21#include "revision.h"
22#include "unpack-trees.h"
23#include "cache-tree.h"
24#include "dir.h"
25#include "utf8.h"
26#include "log-tree.h"
27#include "color.h"
fcab40a3 28#include "rerere.h"
87091b49 29#include "help.h"
18668f53 30#include "merge-recursive.h"
cfc5789a 31#include "resolve-undo.h"
93e535a5 32#include "remote.h"
898eacd8 33#include "fmt-merge-msg.h"
ba3c69a9 34#include "gpg-interface.h"
75c961b7 35#include "sequencer.h"
02a8cfa4 36#include "string-list.h"
3836d88a 37#include "packfile.h"
adcc94a0 38#include "tag.h"
65b5f948 39#include "alias.h"
b6433555 40#include "branch.h"
64043556 41#include "commit-reach.h"
d540b70c 42#include "wt-status.h"
1c7b76be
MV
43
44#define DEFAULT_TWOHEAD (1<<0)
45#define DEFAULT_OCTOPUS (1<<1)
46#define NO_FAST_FORWARD (1<<2)
47#define NO_TRIVIAL (1<<3)
48
49struct strategy {
50 const char *name;
51 unsigned attr;
52};
53
54static const char * const builtin_merge_usage[] = {
9c9b4f2f 55 N_("git merge [<options>] [<commit>...]"),
962e6295 56 N_("git merge --abort"),
367ff694 57 N_("git merge --continue"),
1c7b76be
MV
58 NULL
59};
60
898eacd8 61static int show_diffstat = 1, shortlog_len = -1, squash;
1d14d0c9 62static int option_commit = -1;
a54841e9 63static int option_edit = -1;
efed0022 64static int allow_trivial = 1, have_message, verify_signatures;
54887b46 65static int check_trust_level = 1;
c1d7036b 66static int overwrite_ignore = 1;
2c47789d 67static struct strbuf merge_msg = STRBUF_INIT;
1c7b76be
MV
68static struct strategy **use_strategies;
69static size_t use_strategies_nr, use_strategies_alloc;
8cc5b290
AP
70static const char **xopts;
71static size_t xopts_nr, xopts_alloc;
1c7b76be 72static const char *branch;
0d8fc3ef 73static char *branch_mergeoptions;
7610fa57 74static int option_renormalize;
7f87aff2 75static int verbosity;
cb6020bb 76static int allow_rerere_auto;
35d2fffd 77static int abort_current_merge;
f3f8311e 78static int quit_current_merge;
367ff694 79static int continue_current_merge;
e379fdf3 80static int allow_unrelated_histories;
99bfc669 81static int show_progress = -1;
a01f7f2b 82static int default_to_upstream = 1;
14d01b4f 83static int signoff;
ba3c69a9 84static const char *sign_commit;
a1f3dd7e 85static int no_verify;
1c7b76be
MV
86
87static struct strategy all_strategy[] = {
1c7b76be
MV
88 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
89 { "octopus", DEFAULT_OCTOPUS },
90 { "resolve", 0 },
1c7b76be
MV
91 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
92 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
93};
94
95static const char *pull_twohead, *pull_octopus;
96
a54841e9
MV
97enum ff_type {
98 FF_NO,
99 FF_ALLOW,
100 FF_ONLY
101};
102
103static enum ff_type fast_forward = FF_ALLOW;
104
d540b70c
DL
105static const char *cleanup_arg;
106static enum commit_msg_cleanup_mode cleanup_mode;
107
1c7b76be
MV
108static int option_parse_message(const struct option *opt,
109 const char *arg, int unset)
110{
111 struct strbuf *buf = opt->value;
112
113 if (unset)
114 strbuf_setlen(buf, 0);
74f5b7fb 115 else if (arg) {
ce9d823b 116 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
1c7b76be 117 have_message = 1;
74f5b7fb 118 } else
bacec478 119 return error(_("switch `m' requires a value"));
1c7b76be
MV
120 return 0;
121}
122
f41179f1
NTND
123static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx,
124 const struct option *opt,
3ebbe289 125 const char *arg_not_used,
f41179f1 126 int unset)
920f22e6
JS
127{
128 struct strbuf *buf = opt->value;
129 const char *arg;
130
3ebbe289 131 BUG_ON_OPT_ARG(arg_not_used);
920f22e6
JS
132 if (unset)
133 BUG("-F cannot be negated");
134
135 if (ctx->opt) {
136 arg = ctx->opt;
137 ctx->opt = NULL;
138 } else if (ctx->argc > 1) {
139 ctx->argc--;
140 arg = *++ctx->argv;
141 } else
9440b831 142 return error(_("option `%s' requires a value"), opt->long_name);
920f22e6
JS
143
144 if (buf->len)
145 strbuf_addch(buf, '\n');
146 if (ctx->prefix && !is_absolute_path(arg))
147 arg = prefix_filename(ctx->prefix, arg);
148 if (strbuf_read_file(buf, arg, 0) < 0)
149 return error(_("could not read file '%s'"), arg);
150 have_message = 1;
151
152 return 0;
153}
154
1c7b76be
MV
155static struct strategy *get_strategy(const char *name)
156{
157 int i;
87091b49
MV
158 struct strategy *ret;
159 static struct cmdnames main_cmds, other_cmds;
e321180e 160 static int loaded;
1c7b76be
MV
161
162 if (!name)
163 return NULL;
164
165 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
166 if (!strcmp(name, all_strategy[i].name))
167 return &all_strategy[i];
1719b5e4 168
e321180e 169 if (!loaded) {
87091b49 170 struct cmdnames not_strategies;
e321180e 171 loaded = 1;
87091b49 172
87091b49 173 memset(&not_strategies, 0, sizeof(struct cmdnames));
e321180e 174 load_command_list("git-merge-", &main_cmds, &other_cmds);
87091b49
MV
175 for (i = 0; i < main_cmds.cnt; i++) {
176 int j, found = 0;
177 struct cmdname *ent = main_cmds.names[i];
178 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
179 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
180 && !all_strategy[j].name[ent->len])
181 found = 1;
182 if (!found)
183 add_cmdname(&not_strategies, ent->name, ent->len);
87091b49 184 }
ed874656 185 exclude_cmds(&main_cmds, &not_strategies);
87091b49
MV
186 }
187 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
bacec478
ÆAB
188 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
189 fprintf(stderr, _("Available strategies are:"));
131f9a10
JH
190 for (i = 0; i < main_cmds.cnt; i++)
191 fprintf(stderr, " %s", main_cmds.names[i]->name);
192 fprintf(stderr, ".\n");
193 if (other_cmds.cnt) {
bacec478 194 fprintf(stderr, _("Available custom strategies are:"));
131f9a10
JH
195 for (i = 0; i < other_cmds.cnt; i++)
196 fprintf(stderr, " %s", other_cmds.names[i]->name);
197 fprintf(stderr, ".\n");
198 }
87091b49
MV
199 exit(1);
200 }
201
19d4b416 202 ret = xcalloc(1, sizeof(struct strategy));
87091b49 203 ret->name = xstrdup(name);
52b48ef1 204 ret->attr = NO_TRIVIAL;
87091b49 205 return ret;
1c7b76be
MV
206}
207
208static void append_strategy(struct strategy *s)
209{
210 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
211 use_strategies[use_strategies_nr++] = s;
212}
213
214static int option_parse_strategy(const struct option *opt,
215 const char *name, int unset)
216{
1c7b76be
MV
217 if (unset)
218 return 0;
219
1719b5e4 220 append_strategy(get_strategy(name));
1c7b76be
MV
221 return 0;
222}
223
8cc5b290
AP
224static int option_parse_x(const struct option *opt,
225 const char *arg, int unset)
226{
227 if (unset)
228 return 0;
229
230 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
231 xopts[xopts_nr++] = xstrdup(arg);
232 return 0;
233}
234
1c7b76be
MV
235static int option_parse_n(const struct option *opt,
236 const char *arg, int unset)
237{
517fe807 238 BUG_ON_OPT_ARG(arg);
1c7b76be
MV
239 show_diffstat = unset;
240 return 0;
241}
242
243static struct option builtin_merge_options[] = {
244 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
962e6295 245 N_("do not show a diffstat at the end of the merge"),
1c7b76be 246 PARSE_OPT_NOARG, option_parse_n },
d5d09d47 247 OPT_BOOL(0, "stat", &show_diffstat,
962e6295 248 N_("show a diffstat at the end of the merge")),
d5d09d47 249 OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")),
962e6295
NTND
250 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
251 N_("add (at most <n>) entries from shortlog to merge commit message"),
96e9420c 252 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
d5d09d47 253 OPT_BOOL(0, "squash", &squash,
962e6295 254 N_("create a single commit instead of doing a merge")),
d5d09d47 255 OPT_BOOL(0, "commit", &option_commit,
962e6295 256 N_("perform a commit if the merge succeeds (default)")),
f8246281 257 OPT_BOOL('e', "edit", &option_edit,
962e6295 258 N_("edit message before committing")),
d540b70c 259 OPT_CLEANUP(&cleanup_arg),
a54841e9 260 OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW),
3e4a67b4
NTND
261 OPT_SET_INT_F(0, "ff-only", &fast_forward,
262 N_("abort if fast-forward is not possible"),
263 FF_ONLY, PARSE_OPT_NONEG),
cb6020bb 264 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
efed0022 265 OPT_BOOL(0, "verify-signatures", &verify_signatures,
c8bb9d2e 266 N_("verify that the named commit has a valid GPG signature")),
962e6295
NTND
267 OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
268 N_("merge strategy to use"), option_parse_strategy),
269 OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
270 N_("option for selected merge strategy"), option_parse_x),
271 OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
272 N_("merge commit message (for a non-fast-forward merge)"),
1c7b76be 273 option_parse_message),
920f22e6
JS
274 { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
275 N_("read message from file"), PARSE_OPT_NONEG,
bf3ff338 276 NULL, 0, option_read_message },
7f87aff2 277 OPT__VERBOSITY(&verbosity),
d5d09d47 278 OPT_BOOL(0, "abort", &abort_current_merge,
962e6295 279 N_("abort the current in-progress merge")),
f3f8311e
NTND
280 OPT_BOOL(0, "quit", &quit_current_merge,
281 N_("--abort but leave index and working tree alone")),
367ff694
CP
282 OPT_BOOL(0, "continue", &continue_current_merge,
283 N_("continue the current in-progress merge")),
e379fdf3
JH
284 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
285 N_("allow merging unrelated histories")),
962e6295 286 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
e703d711 287 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
962e6295 288 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
d5d09d47 289 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
14d01b4f 290 OPT_BOOL(0, "signoff", &signoff, N_("add Signed-off-by:")),
bc40ce4d 291 OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
1c7b76be
MV
292 OPT_END()
293};
294
52684310 295static int save_state(struct object_id *stash)
1c7b76be
MV
296{
297 int len;
d3180279 298 struct child_process cp = CHILD_PROCESS_INIT;
1c7b76be
MV
299 struct strbuf buffer = STRBUF_INIT;
300 const char *argv[] = {"stash", "create", NULL};
150888e2 301 int rc = -1;
1c7b76be 302
1c7b76be
MV
303 cp.argv = argv;
304 cp.out = -1;
305 cp.git_cmd = 1;
306
307 if (start_command(&cp))
bacec478 308 die(_("could not run stash."));
1c7b76be
MV
309 len = strbuf_read(&buffer, cp.out, 1024);
310 close(cp.out);
311
312 if (finish_command(&cp) || len < 0)
bacec478 313 die(_("stash failed"));
b4fd9406 314 else if (!len) /* no changes */
150888e2 315 goto out;
1c7b76be 316 strbuf_setlen(&buffer, buffer.len-1);
52684310 317 if (get_oid(buffer.buf, stash))
bacec478 318 die(_("not a valid object: %s"), buffer.buf);
150888e2
RS
319 rc = 0;
320out:
321 strbuf_release(&buffer);
322 return rc;
1c7b76be
MV
323}
324
cb91022c 325static void read_empty(const struct object_id *oid, int verbose)
172b6428
CB
326{
327 int i = 0;
328 const char *args[7];
329
330 args[i++] = "read-tree";
331 if (verbose)
332 args[i++] = "-v";
333 args[i++] = "-m";
334 args[i++] = "-u";
cb91022c 335 args[i++] = empty_tree_oid_hex();
336 args[i++] = oid_to_hex(oid);
172b6428
CB
337 args[i] = NULL;
338
339 if (run_command_v_opt(args, RUN_GIT_CMD))
bacec478 340 die(_("read-tree failed"));
172b6428
CB
341}
342
cb91022c 343static void reset_hard(const struct object_id *oid, int verbose)
1c7b76be
MV
344{
345 int i = 0;
346 const char *args[6];
347
348 args[i++] = "read-tree";
349 if (verbose)
350 args[i++] = "-v";
351 args[i++] = "--reset";
352 args[i++] = "-u";
cb91022c 353 args[i++] = oid_to_hex(oid);
1c7b76be
MV
354 args[i] = NULL;
355
356 if (run_command_v_opt(args, RUN_GIT_CMD))
bacec478 357 die(_("read-tree failed"));
1c7b76be
MV
358}
359
52684310 360static void restore_state(const struct object_id *head,
361 const struct object_id *stash)
1c7b76be 362{
f285a2d7 363 struct strbuf sb = STRBUF_INIT;
1c7b76be
MV
364 const char *args[] = { "stash", "apply", NULL, NULL };
365
52684310 366 if (is_null_oid(stash))
1c7b76be
MV
367 return;
368
cb91022c 369 reset_hard(head, 1);
1c7b76be 370
52684310 371 args[2] = oid_to_hex(stash);
1c7b76be
MV
372
373 /*
374 * It is OK to ignore error here, for example when there was
375 * nothing to restore.
376 */
377 run_command_v_opt(args, RUN_GIT_CMD);
378
379 strbuf_release(&sb);
380 refresh_cache(REFRESH_QUIET);
381}
382
383/* This is called when no merge was necessary. */
384static void finish_up_to_date(const char *msg)
385{
7f87aff2 386 if (verbosity >= 0)
bacec478 387 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
b6433555 388 remove_merge_branch_state(the_repository);
1c7b76be
MV
389}
390
4c57bd27 391static void squash_message(struct commit *commit, struct commit_list *remoteheads)
1c7b76be
MV
392{
393 struct rev_info rev;
f285a2d7 394 struct strbuf out = STRBUF_INIT;
1c7b76be 395 struct commit_list *j;
dd2e794a 396 struct pretty_print_context ctx = {0};
1c7b76be 397
bacec478 398 printf(_("Squash commit -- not updating HEAD\n"));
1c7b76be 399
2abf3503 400 repo_init_revisions(the_repository, &rev, NULL);
1c7b76be
MV
401 rev.ignore_merges = 1;
402 rev.commit_format = CMIT_FMT_MEDIUM;
403
1c7b76be
MV
404 commit->object.flags |= UNINTERESTING;
405 add_pending_object(&rev, &commit->object, NULL);
406
407 for (j = remoteheads; j; j = j->next)
408 add_pending_object(&rev, &j->item->object, NULL);
409
410 setup_revisions(0, NULL, &rev, NULL);
411 if (prepare_revision_walk(&rev))
bacec478 412 die(_("revision walk setup failed"));
1c7b76be 413
dd2e794a
TR
414 ctx.abbrev = rev.abbrev;
415 ctx.date_mode = rev.date_mode;
6bf13944 416 ctx.fmt = rev.commit_format;
dd2e794a 417
1c7b76be
MV
418 strbuf_addstr(&out, "Squashed commit of the following:\n");
419 while ((commit = get_revision(&rev)) != NULL) {
420 strbuf_addch(&out, '\n');
421 strbuf_addf(&out, "commit %s\n",
f2fd0760 422 oid_to_hex(&commit->object.oid));
6bf13944 423 pretty_print_commit(&ctx, commit, &out);
1c7b76be 424 }
102de880 425 write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
1c7b76be
MV
426 strbuf_release(&out);
427}
428
894642f6 429static void finish(struct commit *head_commit,
4c57bd27 430 struct commit_list *remoteheads,
52684310 431 const struct object_id *new_head, const char *msg)
1c7b76be 432{
f285a2d7 433 struct strbuf reflog_message = STRBUF_INIT;
52684310 434 const struct object_id *head = &head_commit->object.oid;
1c7b76be 435
1c7b76be
MV
436 if (!msg)
437 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
438 else {
7f87aff2
TA
439 if (verbosity >= 0)
440 printf("%s\n", msg);
1c7b76be
MV
441 strbuf_addf(&reflog_message, "%s: %s",
442 getenv("GIT_REFLOG_ACTION"), msg);
443 }
444 if (squash) {
4c57bd27 445 squash_message(head_commit, remoteheads);
1c7b76be 446 } else {
7f87aff2 447 if (verbosity >= 0 && !merge_msg.len)
bacec478 448 printf(_("No merge message -- not updating HEAD\n"));
1c7b76be
MV
449 else {
450 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
ae077771 451 update_ref(reflog_message.buf, "HEAD", new_head, head,
452 0, UPDATE_REFS_DIE_ON_ERR);
1c7b76be
MV
453 /*
454 * We ignore errors in 'gc --auto', since the
455 * user should see them.
456 */
2d511cfc 457 close_object_store(the_repository->objects);
1c7b76be
MV
458 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
459 }
460 }
461 if (new_head && show_diffstat) {
462 struct diff_options opts;
e6757652 463 repo_diff_setup(the_repository, &opts);
7a7159ac 464 opts.stat_width = -1; /* use full terminal width */
df44483a 465 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1c7b76be
MV
466 opts.output_format |=
467 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
468 opts.detect_rename = DIFF_DETECT_RENAME;
28452655 469 diff_setup_done(&opts);
66f414f8 470 diff_tree_oid(head, new_head, "", &opts);
1c7b76be
MV
471 diffcore_std(&opts);
472 diff_flush(&opts);
473 }
474
475 /* Run a post-merge hook */
15048f8a 476 run_hook_le(NULL, "post-merge", squash ? "1" : "0", NULL);
1c7b76be
MV
477
478 strbuf_release(&reflog_message);
479}
480
481/* Get the name for the merge commit's message. */
482static void merge_name(const char *remote, struct strbuf *msg)
483{
ae8e4c9c 484 struct commit *remote_head;
52684310 485 struct object_id branch_head;
f285a2d7 486 struct strbuf buf = STRBUF_INIT;
c9717ee9 487 struct strbuf bname = STRBUF_INIT;
e2e5ac23 488 struct merge_remote_desc *desc;
1c7b76be 489 const char *ptr;
751c5974 490 char *found_ref;
1c7b76be
MV
491 int len, early;
492
0e9f62da 493 strbuf_branchname(&bname, remote, 0);
a552de75 494 remote = bname.buf;
c9717ee9 495
52684310 496 oidclr(&branch_head);
ae8e4c9c 497 remote_head = get_merge_parent(remote);
1c7b76be 498 if (!remote_head)
bacec478 499 die(_("'%s' does not point to a commit"), remote);
1c7b76be 500
cca5fa64 501 if (dwim_ref(remote, strlen(remote), &branch_head, &found_ref) > 0) {
59556548 502 if (starts_with(found_ref, "refs/heads/")) {
751c5974 503 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
52684310 504 oid_to_hex(&branch_head), remote);
751c5974
JK
505 goto cleanup;
506 }
59556548 507 if (starts_with(found_ref, "refs/tags/")) {
57b58db7 508 strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
52684310 509 oid_to_hex(&branch_head), remote);
57b58db7
JH
510 goto cleanup;
511 }
59556548 512 if (starts_with(found_ref, "refs/remotes/")) {
13931236 513 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
52684310 514 oid_to_hex(&branch_head), remote);
69a8b7c7
JK
515 goto cleanup;
516 }
1c7b76be
MV
517 }
518
519 /* See if remote matches <name>^^^.. or <name>~<number> */
520 for (len = 0, ptr = remote + strlen(remote);
521 remote < ptr && ptr[-1] == '^';
522 ptr--)
523 len++;
524 if (len)
525 early = 1;
526 else {
527 early = 0;
528 ptr = strrchr(remote, '~');
529 if (ptr) {
530 int seen_nonzero = 0;
531
532 len++; /* count ~ */
533 while (*++ptr && isdigit(*ptr)) {
534 seen_nonzero |= (*ptr != '0');
535 len++;
536 }
537 if (*ptr)
538 len = 0; /* not ...~<number> */
539 else if (seen_nonzero)
540 early = 1;
541 else if (len == 1)
542 early = 1; /* "name~" is "name~1"! */
543 }
544 }
545 if (len) {
546 struct strbuf truname = STRBUF_INIT;
1016658d 547 strbuf_addf(&truname, "refs/heads/%s", remote);
9b6bf4d5 548 strbuf_setlen(&truname, truname.len - len);
c6893323 549 if (ref_exists(truname.buf)) {
1c7b76be
MV
550 strbuf_addf(msg,
551 "%s\t\tbranch '%s'%s of .\n",
c368dde9 552 oid_to_hex(&remote_head->object.oid),
9b6bf4d5 553 truname.buf + 11,
1c7b76be 554 (early ? " (early part)" : ""));
c9717ee9
JH
555 strbuf_release(&truname);
556 goto cleanup;
1c7b76be 557 }
1016658d 558 strbuf_release(&truname);
1c7b76be 559 }
2d1495fe 560
e2e5ac23
NTND
561 desc = merge_remote_util(remote_head);
562 if (desc && desc->obj && desc->obj->type == OBJ_TAG) {
563 strbuf_addf(msg, "%s\t\t%s '%s'\n",
564 oid_to_hex(&desc->obj->oid),
565 type_name(desc->obj->type),
566 remote);
567 goto cleanup;
2d1495fe
JH
568 }
569
1c7b76be 570 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
c368dde9 571 oid_to_hex(&remote_head->object.oid), remote);
c9717ee9
JH
572cleanup:
573 strbuf_release(&buf);
574 strbuf_release(&bname);
1c7b76be
MV
575}
576
0d8fc3ef
JH
577static void parse_branch_merge_options(char *bmo)
578{
579 const char **argv;
580 int argc;
581
582 if (!bmo)
583 return;
584 argc = split_cmdline(bmo, &argv);
585 if (argc < 0)
c7fe5b61 586 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
a7412ae1 587 _(split_cmdline_strerror(argc)));
2756ca43 588 REALLOC_ARRAY(argv, argc + 2);
f331ab9d 589 MOVE_ARRAY(argv + 1, argv, argc + 1);
0d8fc3ef
JH
590 argc++;
591 argv[0] = "branch.*.mergeoptions";
592 parse_options(argc, argv, NULL, builtin_merge_options,
593 builtin_merge_usage, 0);
594 free(argv);
595}
596
186458b1 597static int git_merge_config(const char *k, const char *v, void *cb)
1c7b76be 598{
898eacd8
JH
599 int status;
600
59556548
CC
601 if (branch && starts_with(k, "branch.") &&
602 starts_with(k + 7, branch) &&
1c7b76be 603 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
0d8fc3ef
JH
604 free(branch_mergeoptions);
605 branch_mergeoptions = xstrdup(v);
606 return 0;
1c7b76be
MV
607 }
608
609 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
610 show_diffstat = git_config_bool(k, v);
ca779e82
HJI
611 else if (!strcmp(k, "merge.verifysignatures"))
612 verify_signatures = git_config_bool(k, v);
1c7b76be
MV
613 else if (!strcmp(k, "pull.twohead"))
614 return git_config_string(&pull_twohead, k, v);
615 else if (!strcmp(k, "pull.octopus"))
616 return git_config_string(&pull_octopus, k, v);
d540b70c
DL
617 else if (!strcmp(k, "commit.cleanup"))
618 return git_config_string(&cleanup_arg, k, v);
7610fa57
JN
619 else if (!strcmp(k, "merge.renormalize"))
620 option_renormalize = git_config_bool(k, v);
898eacd8 621 else if (!strcmp(k, "merge.ff")) {
89576613 622 int boolval = git_parse_maybe_bool(v);
f23e8dec 623 if (0 <= boolval) {
a54841e9 624 fast_forward = boolval ? FF_ALLOW : FF_NO;
f23e8dec 625 } else if (v && !strcmp(v, "only")) {
a54841e9 626 fast_forward = FF_ONLY;
f23e8dec
JH
627 } /* do not barf on values from future versions of git */
628 return 0;
93e535a5
JH
629 } else if (!strcmp(k, "merge.defaulttoupstream")) {
630 default_to_upstream = git_config_bool(k, v);
631 return 0;
d95bfb12
NV
632 } else if (!strcmp(k, "commit.gpgsign")) {
633 sign_commit = git_config_bool(k, v) ? "" : NULL;
634 return 0;
54887b46
HJI
635 } else if (!strcmp(k, "gpg.mintrustlevel")) {
636 check_trust_level = 0;
96e9420c 637 }
ba3c69a9 638
898eacd8 639 status = fmt_merge_msg_config(k, v, cb);
5de89d3a
JH
640 if (status)
641 return status;
ba3c69a9 642 status = git_gpg_config(k, v, NULL);
898eacd8
JH
643 if (status)
644 return status;
1c7b76be
MV
645 return git_diff_ui_config(k, v, cb);
646}
647
52684310 648static int read_tree_trivial(struct object_id *common, struct object_id *head,
649 struct object_id *one)
1c7b76be
MV
650{
651 int i, nr_trees = 0;
652 struct tree *trees[MAX_UNPACK_TREES];
653 struct tree_desc t[MAX_UNPACK_TREES];
654 struct unpack_trees_options opts;
655
656 memset(&opts, 0, sizeof(opts));
657 opts.head_idx = 2;
658 opts.src_index = &the_index;
659 opts.dst_index = &the_index;
660 opts.update = 1;
661 opts.verbose_update = 1;
662 opts.trivial_merges_only = 1;
663 opts.merge = 1;
a9dbc179 664 trees[nr_trees] = parse_tree_indirect(common);
1c7b76be
MV
665 if (!trees[nr_trees++])
666 return -1;
a9dbc179 667 trees[nr_trees] = parse_tree_indirect(head);
1c7b76be
MV
668 if (!trees[nr_trees++])
669 return -1;
a9dbc179 670 trees[nr_trees] = parse_tree_indirect(one);
1c7b76be
MV
671 if (!trees[nr_trees++])
672 return -1;
673 opts.fn = threeway_merge;
674 cache_tree_free(&active_cache_tree);
675 for (i = 0; i < nr_trees; i++) {
676 parse_tree(trees[i]);
677 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
678 }
679 if (unpack_trees(nr_trees, t, &opts))
680 return -1;
681 return 0;
682}
683
52684310 684static void write_tree_trivial(struct object_id *oid)
1c7b76be 685{
fc5cb99f 686 if (write_cache_as_tree(oid, 0, NULL))
bacec478 687 die(_("git write-tree failed to write a tree"));
1c7b76be
MV
688}
689
3f9083cd 690static int try_merge_strategy(const char *strategy, struct commit_list *common,
4c57bd27 691 struct commit_list *remoteheads,
b4391657 692 struct commit *head)
3f9083cd 693{
b4391657 694 const char *head_arg = "HEAD";
668f26ff 695
e080b345 696 if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
bacec478 697 return error(_("Unable to write index."));
1c7b76be 698
18668f53 699 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
e080b345 700 struct lock_file lock = LOCK_INIT;
3f9083cd 701 int clean, x;
18668f53 702 struct commit *result;
18668f53
MV
703 struct commit_list *reversed = NULL;
704 struct merge_options o;
3f9083cd 705 struct commit_list *j;
18668f53
MV
706
707 if (remoteheads->next) {
bacec478 708 error(_("Not handling anything other than two heads merge."));
18668f53
MV
709 return 2;
710 }
711
0d6caa2d 712 init_merge_options(&o, the_repository);
18668f53 713 if (!strcmp(strategy, "subtree"))
85e51b78 714 o.subtree_shift = "";
8cc5b290 715
7610fa57 716 o.renormalize = option_renormalize;
99bfc669
JK
717 o.show_rename_progress =
718 show_progress == -1 ? isatty(2) : show_progress;
7610fa57 719
635a7bb1
JN
720 for (x = 0; x < xopts_nr; x++)
721 if (parse_merge_opt(&o, xopts[x]))
bacec478 722 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
18668f53
MV
723
724 o.branch1 = head_arg;
ae8e4c9c 725 o.branch2 = merge_remote_util(remoteheads->item)->name;
18668f53
MV
726
727 for (j = common; j; j = j->next)
728 commit_list_insert(j->item, &reversed);
729
b3e83cc7 730 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
894642f6 731 clean = merge_recursive(&o, head,
18668f53 732 remoteheads->item, reversed, &result);
f241ff0d
JS
733 if (clean < 0)
734 exit(128);
61000814
735 if (write_locked_index(&the_index, &lock,
736 COMMIT_LOCK | SKIP_IF_UNCHANGED))
1a07e59c 737 die(_("unable to write %s"), get_index_file());
18668f53
MV
738 return clean ? 0 : 1;
739 } else {
7e196c3a
NTND
740 return try_merge_command(the_repository,
741 strategy, xopts_nr, xopts,
742 common, head_arg, remoteheads);
18668f53 743 }
1c7b76be
MV
744}
745
746static void count_diff_files(struct diff_queue_struct *q,
747 struct diff_options *opt, void *data)
748{
749 int *count = data;
750
751 (*count) += q->nr;
752}
753
754static int count_unmerged_entries(void)
755{
1c7b76be
MV
756 int i, ret = 0;
757
be6ff819
JH
758 for (i = 0; i < active_nr; i++)
759 if (ce_stage(active_cache[i]))
1c7b76be
MV
760 ret++;
761
762 return ret;
763}
764
1c7b76be
MV
765static void add_strategies(const char *string, unsigned attr)
766{
02a8cfa4
RS
767 int i;
768
769 if (string) {
770 struct string_list list = STRING_LIST_INIT_DUP;
771 struct string_list_item *item;
772 string_list_split(&list, string, ' ', -1);
773 for_each_string_list_item(item, &list)
774 append_strategy(get_strategy(item->string));
775 string_list_clear(&list, 0);
1c7b76be
MV
776 return;
777 }
778 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
779 if (all_strategy[i].attr & attr)
780 append_strategy(&all_strategy[i]);
781
782}
783
66f4b98a 784static void read_merge_msg(struct strbuf *msg)
65969d43 785{
102de880 786 const char *filename = git_path_merge_msg(the_repository);
66f4b98a 787 strbuf_reset(msg);
418c9b17
JN
788 if (strbuf_read_file(msg, filename, 0) < 0)
789 die_errno(_("Could not read from '%s'"), filename);
65969d43
JS
790}
791
4c57bd27
JH
792static void write_merge_state(struct commit_list *);
793static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
65969d43 794{
66f4b98a
JS
795 if (err_msg)
796 error("%s", err_msg);
797 fprintf(stderr,
798 _("Not committing merge; use 'git commit' to complete the merge.\n"));
4c57bd27 799 write_merge_state(remoteheads);
66f4b98a
JS
800 exit(1);
801}
802
f26af3fc
TR
803static const char merge_editor_comment[] =
804N_("Please enter a commit message to explain why this merge is necessary,\n"
805 "especially if it merges an updated upstream into a topic branch.\n"
d540b70c
DL
806 "\n");
807
808static const char scissors_editor_comment[] =
809N_("An empty message aborts the commit.\n");
810
811static const char no_scissors_editor_comment[] =
812N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
f26af3fc
TR
813 "the commit.\n");
814
9d89b355 815static void write_merge_heads(struct commit_list *);
4c57bd27 816static void prepare_to_commit(struct commit_list *remoteheads)
66f4b98a
JS
817{
818 struct strbuf msg = STRBUF_INIT;
6098817f
MG
819 const char *index_file = get_index_file();
820
bc40ce4d 821 if (!no_verify && run_commit_hook(0 < option_edit, index_file, "pre-merge-commit", NULL))
6098817f
MG
822 abort_commit(remoteheads, NULL);
823 /*
824 * Re-read the index as pre-merge-commit hook could have updated it,
825 * and write it out as a tree. We must do this before we invoke
826 * the editor and after we invoke run_status above.
827 */
828 if (find_hook("pre-merge-commit"))
829 discard_cache();
830 read_cache_from(index_file);
66f4b98a 831 strbuf_addbuf(&msg, &merge_msg);
62dc42b9
MG
832 if (squash)
833 BUG("the control must not reach here under --squash");
d540b70c
DL
834 if (0 < option_edit) {
835 strbuf_addch(&msg, '\n');
836 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
837 wt_status_append_cut_line(&msg);
838 strbuf_commented_addf(&msg, "\n");
839 }
840 strbuf_commented_addf(&msg, _(merge_editor_comment));
841 strbuf_commented_addf(&msg, _(cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS ?
842 scissors_editor_comment :
843 no_scissors_editor_comment), comment_line_char);
844 }
14d01b4f
ŁG
845 if (signoff)
846 append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
9d89b355 847 write_merge_heads(remoteheads);
102de880 848 write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
0a3beb0e 849 if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
102de880 850 git_path_merge_msg(the_repository), "merge", NULL))
3e4141d0 851 abort_commit(remoteheads, NULL);
f8246281 852 if (0 < option_edit) {
102de880 853 if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
4c57bd27 854 abort_commit(remoteheads, NULL);
66f4b98a 855 }
f8b86359 856
a1f3dd7e 857 if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
f8b86359 858 "commit-msg",
102de880 859 git_path_merge_msg(the_repository), NULL))
f8b86359
SB
860 abort_commit(remoteheads, NULL);
861
66f4b98a 862 read_merge_msg(&msg);
d540b70c 863 cleanup_message(&msg, cleanup_mode, 0);
66f4b98a 864 if (!msg.len)
4c57bd27 865 abort_commit(remoteheads, _("Empty commit message."));
66f4b98a
JS
866 strbuf_release(&merge_msg);
867 strbuf_addbuf(&merge_msg, &msg);
868 strbuf_release(&msg);
65969d43
JS
869}
870
4c57bd27 871static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
1c7b76be 872{
52684310 873 struct object_id result_tree, result_commit;
910a09a7 874 struct commit_list *parents, **pptr = &parents;
40d71940 875
e080b345 876 if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
40d71940 877 return error(_("Unable to write index."));
1c7b76be 878
52684310 879 write_tree_trivial(&result_tree);
157efde1 880 printf(_("Wonderful.\n"));
910a09a7
RS
881 pptr = commit_list_append(head, pptr);
882 pptr = commit_list_append(remoteheads->item, pptr);
4c57bd27 883 prepare_to_commit(remoteheads);
5078f344
PO
884 if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents,
885 &result_commit, NULL, sign_commit))
6b3c4c05 886 die(_("failed to write commit object"));
52684310 887 finish(head, remoteheads, &result_commit, "In-index merge");
b6433555 888 remove_merge_branch_state(the_repository);
1c7b76be
MV
889 return 0;
890}
891
894642f6 892static int finish_automerge(struct commit *head,
e78cbf8c 893 int head_subsumed,
894642f6 894 struct commit_list *common,
4c57bd27 895 struct commit_list *remoteheads,
52684310 896 struct object_id *result_tree,
1c7b76be
MV
897 const char *wt_strategy)
898{
e78cbf8c 899 struct commit_list *parents = NULL;
1c7b76be 900 struct strbuf buf = STRBUF_INIT;
52684310 901 struct object_id result_commit;
1c7b76be 902
f00abe6a 903 write_tree_trivial(result_tree);
1c7b76be 904 free_commit_list(common);
e78cbf8c 905 parents = remoteheads;
a54841e9 906 if (!head_subsumed || fast_forward == FF_NO)
894642f6 907 commit_list_insert(head, &parents);
4c57bd27 908 prepare_to_commit(remoteheads);
5078f344
PO
909 if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
910 &result_commit, NULL, sign_commit))
6b3c4c05 911 die(_("failed to write commit object"));
f23101bf 912 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
52684310 913 finish(head, remoteheads, &result_commit, buf.buf);
1c7b76be 914 strbuf_release(&buf);
b6433555 915 remove_merge_branch_state(the_repository);
1c7b76be
MV
916 return 0;
917}
918
08e3ce5a 919static int suggest_conflicts(void)
1c7b76be 920{
418c9b17 921 const char *filename;
1c7b76be 922 FILE *fp;
75c961b7 923 struct strbuf msgbuf = STRBUF_INIT;
1c7b76be 924
102de880 925 filename = git_path_merge_msg(the_repository);
23a9e071 926 fp = xfopen(filename, "a");
75c961b7 927
1055997e
DL
928 /*
929 * We can't use cleanup_mode because if we're not using the editor,
930 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
931 * though the message is meant to be processed later by git-commit.
932 * Thus, we will get the cleanup mode which is returned when we _are_
933 * using an editor.
934 */
1a2b985f
DL
935 append_conflicts_hint(&the_index, &msgbuf,
936 get_cleanup_mode(cleanup_arg, 1));
75c961b7 937 fputs(msgbuf.buf, fp);
8d025b7c 938 strbuf_release(&msgbuf);
1c7b76be 939 fclose(fp);
35843b11 940 repo_rerere(the_repository, allow_rerere_auto);
bacec478
ÆAB
941 printf(_("Automatic merge failed; "
942 "fix conflicts and then commit the result.\n"));
1c7b76be
MV
943 return 1;
944}
945
1c7b76be
MV
946static int evaluate_result(void)
947{
948 int cnt = 0;
949 struct rev_info rev;
950
1c7b76be 951 /* Check how many files differ. */
2abf3503 952 repo_init_revisions(the_repository, &rev, "");
1c7b76be
MV
953 setup_revisions(0, NULL, &rev, NULL);
954 rev.diffopt.output_format |=
955 DIFF_FORMAT_CALLBACK;
956 rev.diffopt.format_callback = count_diff_files;
957 rev.diffopt.format_callback_data = &cnt;
958 run_diff_files(&rev, 0);
959
960 /*
961 * Check how many unmerged entries are
962 * there.
963 */
964 cnt += count_unmerged_entries();
965
966 return cnt;
967}
968
93e535a5 969/*
d6ac1d21 970 * Pretend as if the user told us to merge with the remote-tracking
93e535a5
JH
971 * branch we have for the upstream of the current branch
972 */
973static int setup_with_upstream(const char ***argv)
974{
975 struct branch *branch = branch_get(NULL);
976 int i;
977 const char **args;
978
979 if (!branch)
c7f426d4 980 die(_("No current branch."));
9e3751d4 981 if (!branch->remote_name)
c7f426d4 982 die(_("No remote for the current branch."));
93e535a5 983 if (!branch->merge_nr)
c7f426d4 984 die(_("No default upstream defined for the current branch."));
93e535a5 985
50a6c8ef 986 args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *));
93e535a5
JH
987 for (i = 0; i < branch->merge_nr; i++) {
988 if (!branch->merge[i]->dst)
d6ac1d21 989 die(_("No remote-tracking branch for %s from %s"),
93e535a5
JH
990 branch->merge[i]->src, branch->remote_name);
991 args[i] = branch->merge[i]->dst;
992 }
993 args[i] = NULL;
994 *argv = args;
995 return i;
996}
997
8e6a6bb3 998static void write_merge_heads(struct commit_list *remoteheads)
66f4b98a 999{
66f4b98a
JS
1000 struct commit_list *j;
1001 struct strbuf buf = STRBUF_INIT;
1002
274a5c06 1003 for (j = remoteheads; j; j = j->next) {
f2fd0760 1004 struct object_id *oid;
274a5c06 1005 struct commit *c = j->item;
e2e5ac23
NTND
1006 struct merge_remote_desc *desc;
1007
1008 desc = merge_remote_util(c);
1009 if (desc && desc->obj) {
1010 oid = &desc->obj->oid;
274a5c06 1011 } else {
f2fd0760 1012 oid = &c->object.oid;
274a5c06 1013 }
f2fd0760 1014 strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
274a5c06 1015 }
102de880 1016 write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
418c9b17 1017
66f4b98a 1018 strbuf_reset(&buf);
a54841e9 1019 if (fast_forward == FF_NO)
a22ae753 1020 strbuf_addstr(&buf, "no-ff");
102de880 1021 write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
814c4b37 1022 strbuf_release(&buf);
66f4b98a
JS
1023}
1024
8e6a6bb3
MG
1025static void write_merge_state(struct commit_list *remoteheads)
1026{
1027 write_merge_heads(remoteheads);
1028 strbuf_addch(&merge_msg, '\n');
102de880
SB
1029 write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
1030 merge_msg.len);
8e6a6bb3
MG
1031}
1032
f8246281
JH
1033static int default_edit_option(void)
1034{
1035 static const char name[] = "GIT_MERGE_AUTOEDIT";
1036 const char *e = getenv(name);
1037 struct stat st_stdin, st_stdout;
1038
1039 if (have_message)
1040 /* an explicit -m msg without --[no-]edit */
1041 return 0;
1042
1043 if (e) {
89576613 1044 int v = git_parse_maybe_bool(e);
f8246281 1045 if (v < 0)
bef4830e 1046 die(_("Bad value '%s' in environment '%s'"), e, name);
f8246281
JH
1047 return v;
1048 }
1049
1050 /* Use editor if stdin and stdout are the same and is a tty */
1051 return (!fstat(0, &st_stdin) &&
1052 !fstat(1, &st_stdout) &&
d46f476c 1053 isatty(0) && isatty(1) &&
f8246281
JH
1054 st_stdin.st_dev == st_stdout.st_dev &&
1055 st_stdin.st_ino == st_stdout.st_ino &&
1056 st_stdin.st_mode == st_stdout.st_mode);
1057}
1058
34349dbf
JH
1059static struct commit_list *reduce_parents(struct commit *head_commit,
1060 int *head_subsumed,
1061 struct commit_list *remoteheads)
b5d887f9 1062{
e510ab89 1063 struct commit_list *parents, **remotes;
b5d887f9 1064
0b10b8a3
JH
1065 /*
1066 * Is the current HEAD reachable from another commit being
1067 * merged? If so we do not want to record it as a parent of
1068 * the resulting merge, unless --no-ff is given. We will flip
1069 * this variable to 0 when we find HEAD among the independent
1070 * tips being merged.
1071 */
1072 *head_subsumed = 1;
e78cbf8c 1073
0b10b8a3 1074 /* Find what parents to record by checking independent ones. */
e78cbf8c 1075 parents = reduce_heads(remoteheads);
4da72644 1076 free_commit_list(remoteheads);
e78cbf8c 1077
e510ab89
RS
1078 remoteheads = NULL;
1079 remotes = &remoteheads;
1080 while (parents) {
1081 struct commit *commit = pop_commit(&parents);
e78cbf8c
JH
1082 if (commit == head_commit)
1083 *head_subsumed = 0;
1084 else
1085 remotes = &commit_list_insert(commit, remotes)->next;
1086 }
b5d887f9
JH
1087 return remoteheads;
1088}
f8246281 1089
52fecab2
JH
1090static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg)
1091{
1092 struct fmt_merge_msg_opts opts;
1093
1094 memset(&opts, 0, sizeof(opts));
1095 opts.add_title = !have_message;
1096 opts.shortlog_len = shortlog_len;
1097 opts.credit_people = (0 < option_edit);
1098
1099 fmt_merge_msg(merge_names, merge_msg, &opts);
1100 if (merge_msg->len)
1101 strbuf_setlen(merge_msg, merge_msg->len - 1);
1102}
1103
74e8bc59
JH
1104static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names)
1105{
1106 const char *filename;
1107 int fd, pos, npos;
1108 struct strbuf fetch_head_file = STRBUF_INIT;
ab47df2d 1109 const unsigned hexsz = the_hash_algo->hexsz;
74e8bc59
JH
1110
1111 if (!merge_names)
1112 merge_names = &fetch_head_file;
1113
102de880 1114 filename = git_path_fetch_head(the_repository);
74e8bc59
JH
1115 fd = open(filename, O_RDONLY);
1116 if (fd < 0)
1117 die_errno(_("could not open '%s' for reading"), filename);
1118
1119 if (strbuf_read(merge_names, fd, 0) < 0)
1120 die_errno(_("could not read '%s'"), filename);
1121 if (close(fd) < 0)
1122 die_errno(_("could not close '%s'"), filename);
1123
1124 for (pos = 0; pos < merge_names->len; pos = npos) {
52684310 1125 struct object_id oid;
74e8bc59
JH
1126 char *ptr;
1127 struct commit *commit;
1128
1129 ptr = strchr(merge_names->buf + pos, '\n');
1130 if (ptr)
1131 npos = ptr - merge_names->buf + 1;
1132 else
1133 npos = merge_names->len;
1134
ab47df2d 1135 if (npos - pos < hexsz + 2 ||
52684310 1136 get_oid_hex(merge_names->buf + pos, &oid))
74e8bc59 1137 commit = NULL; /* bad */
ab47df2d 1138 else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
74e8bc59
JH
1139 continue; /* not-for-merge */
1140 else {
ab47df2d 1141 char saved = merge_names->buf[pos + hexsz];
1142 merge_names->buf[pos + hexsz] = '\0';
74e8bc59 1143 commit = get_merge_parent(merge_names->buf + pos);
ab47df2d 1144 merge_names->buf[pos + hexsz] = saved;
74e8bc59
JH
1145 }
1146 if (!commit) {
1147 if (ptr)
1148 *ptr = '\0';
bef4830e 1149 die(_("not something we can merge in %s: %s"),
74e8bc59
JH
1150 filename, merge_names->buf + pos);
1151 }
1152 remotes = &commit_list_insert(commit, remotes)->next;
1153 }
1154
1155 if (merge_names == &fetch_head_file)
1156 strbuf_release(&fetch_head_file);
1157}
1158
34349dbf
JH
1159static struct commit_list *collect_parents(struct commit *head_commit,
1160 int *head_subsumed,
1cf32f4d
JH
1161 int argc, const char **argv,
1162 struct strbuf *merge_msg)
34349dbf
JH
1163{
1164 int i;
1165 struct commit_list *remoteheads = NULL;
1166 struct commit_list **remotes = &remoteheads;
77038015
JH
1167 struct strbuf merge_names = STRBUF_INIT, *autogen = NULL;
1168
1169 if (merge_msg && (!have_message || shortlog_len))
1170 autogen = &merge_names;
34349dbf
JH
1171
1172 if (head_commit)
1173 remotes = &commit_list_insert(head_commit, remotes)->next;
34349dbf 1174
74e8bc59
JH
1175 if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) {
1176 handle_fetch_head(remotes, autogen);
1177 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1178 } else {
1179 for (i = 0; i < argc; i++) {
1180 struct commit *commit = get_merge_parent(argv[i]);
1181 if (!commit)
1182 help_unknown_ref(argv[i], "merge",
bef4830e 1183 _("not something we can merge"));
74e8bc59
JH
1184 remotes = &commit_list_insert(commit, remotes)->next;
1185 }
1186 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1187 if (autogen) {
1188 struct commit_list *p;
1189 for (p = remoteheads; p; p = p->next)
1190 merge_name(merge_remote_util(p->item)->name, autogen);
1191 }
1192 }
1cf32f4d 1193
77038015 1194 if (autogen) {
77038015
JH
1195 prepare_merge_message(autogen, merge_msg);
1196 strbuf_release(autogen);
1cf32f4d
JH
1197 }
1198
1199 return remoteheads;
34349dbf
JH
1200}
1201
adcc94a0
JH
1202static int merging_a_throwaway_tag(struct commit *commit)
1203{
1204 char *tag_ref;
1205 struct object_id oid;
1206 int is_throwaway_tag = 0;
1207
1208 /* Are we merging a tag? */
1209 if (!merge_remote_util(commit) ||
1210 !merge_remote_util(commit)->obj ||
1211 merge_remote_util(commit)->obj->type != OBJ_TAG)
1212 return is_throwaway_tag;
1213
1214 /*
1215 * Now we know we are merging a tag object. Are we downstream
1216 * and following the tags from upstream? If so, we must have
1217 * the tag object pointed at by "refs/tags/$T" where $T is the
1218 * tagname recorded in the tag object. We want to allow such
1219 * a "just to catch up" merge to fast-forward.
1220 *
1221 * Otherwise, we are playing an integrator's role, making a
1222 * merge with a throw-away tag from a contributor with
1223 * something like "git pull $contributor $signed_tag".
1224 * We want to forbid such a merge from fast-forwarding
1225 * by default; otherwise we would not keep the signature
1226 * anywhere.
1227 */
1228 tag_ref = xstrfmt("refs/tags/%s",
1229 ((struct tag *)merge_remote_util(commit)->obj)->tag);
1230 if (!read_ref(tag_ref, &oid) &&
4a7e27e9 1231 oideq(&oid, &merge_remote_util(commit)->obj->oid))
adcc94a0
JH
1232 is_throwaway_tag = 0;
1233 else
1234 is_throwaway_tag = 1;
1235 free(tag_ref);
1236 return is_throwaway_tag;
1237}
1238
1c7b76be
MV
1239int cmd_merge(int argc, const char **argv, const char *prefix)
1240{
52684310 1241 struct object_id result_tree, stash, head_oid;
894642f6 1242 struct commit *head_commit;
f285a2d7 1243 struct strbuf buf = STRBUF_INIT;
17377b62 1244 int i, ret = 0, head_subsumed;
1c7b76be
MV
1245 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1246 struct commit_list *common = NULL;
1247 const char *best_strategy = NULL, *wt_strategy = NULL;
b5d887f9 1248 struct commit_list *remoteheads, *p;
96ec7b1e 1249 void *branch_to_free;
367ff694 1250 int orig_argc = argc;
1c7b76be 1251
da53eec6
NTND
1252 if (argc == 2 && !strcmp(argv[1], "-h"))
1253 usage_with_options(builtin_merge_usage, builtin_merge_options);
1c7b76be
MV
1254
1255 /*
1256 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1257 * current branch.
1258 */
0f2dc722 1259 branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL);
de3ce210
RS
1260 if (branch)
1261 skip_prefix(branch, "refs/heads/", &branch);
7adf5266
DS
1262
1263 init_diff_ui_defaults();
1264 git_config(git_merge_config, NULL);
1265
52684310 1266 if (!branch || is_null_oid(&head_oid))
894642f6 1267 head_commit = NULL;
baf18fc2 1268 else
bc83266a 1269 head_commit = lookup_commit_or_die(&head_oid, "HEAD");
1c7b76be 1270
0d8fc3ef
JH
1271 if (branch_mergeoptions)
1272 parse_branch_merge_options(branch_mergeoptions);
37782920 1273 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1c7b76be 1274 builtin_merge_usage, 0);
898eacd8
JH
1275 if (shortlog_len < 0)
1276 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
2a22c1b3 1277
99bfc669
JK
1278 if (verbosity < 0 && show_progress == -1)
1279 show_progress = 0;
1280
35d2fffd
JH
1281 if (abort_current_merge) {
1282 int nargc = 2;
1283 const char *nargv[] = {"reset", "--merge", NULL};
1284
042e290d 1285 if (orig_argc != 2)
c7d227df 1286 usage_msg_opt(_("--abort expects no arguments"),
042e290d
CP
1287 builtin_merge_usage, builtin_merge_options);
1288
102de880 1289 if (!file_exists(git_path_merge_head(the_repository)))
bacec478 1290 die(_("There is no merge to abort (MERGE_HEAD missing)."));
35d2fffd
JH
1291
1292 /* Invoke 'git reset --merge' */
d5a35c11
NTND
1293 ret = cmd_reset(nargc, nargv, prefix);
1294 goto done;
35d2fffd
JH
1295 }
1296
f3f8311e
NTND
1297 if (quit_current_merge) {
1298 if (orig_argc != 2)
1299 usage_msg_opt(_("--quit expects no arguments"),
1300 builtin_merge_usage,
1301 builtin_merge_options);
1302
1303 remove_merge_branch_state(the_repository);
1304 goto done;
1305 }
1306
367ff694
CP
1307 if (continue_current_merge) {
1308 int nargc = 1;
1309 const char *nargv[] = {"commit", NULL};
1310
1311 if (orig_argc != 2)
c7d227df 1312 usage_msg_opt(_("--continue expects no arguments"),
367ff694
CP
1313 builtin_merge_usage, builtin_merge_options);
1314
102de880 1315 if (!file_exists(git_path_merge_head(the_repository)))
367ff694
CP
1316 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1317
1318 /* Invoke 'git commit' */
1319 ret = cmd_commit(nargc, nargv, prefix);
1320 goto done;
1321 }
1322
2a22c1b3
JH
1323 if (read_cache_unmerged())
1324 die_resolve_conflict("merge");
1325
102de880 1326 if (file_exists(git_path_merge_head(the_repository))) {
2a22c1b3
JH
1327 /*
1328 * There is no unmerged entry, don't advise 'git
1329 * add/rm <file>', just 'git commit'.
1330 */
1331 if (advice_resolve_conflict)
bacec478 1332 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
ad5fe377 1333 "Please, commit your changes before you merge."));
2a22c1b3 1334 else
bacec478 1335 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
2a22c1b3 1336 }
102de880 1337 if (file_exists(git_path_cherry_pick_head(the_repository))) {
d7e5c0cb 1338 if (advice_resolve_conflict)
f68f1801 1339 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
ad5fe377 1340 "Please, commit your changes before you merge."));
d7e5c0cb 1341 else
f68f1801 1342 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
2a22c1b3
JH
1343 }
1344 resolve_undo_clear();
1345
d540b70c
DL
1346 if (option_edit < 0)
1347 option_edit = default_edit_option();
1348
1349 cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
1350
7f87aff2
TA
1351 if (verbosity < 0)
1352 show_diffstat = 0;
1c7b76be
MV
1353
1354 if (squash) {
a54841e9 1355 if (fast_forward == FF_NO)
bacec478 1356 die(_("You cannot combine --squash with --no-ff."));
1d14d0c9
VV
1357 if (option_commit > 0)
1358 die(_("You cannot combine --squash with --commit."));
1359 /*
1360 * squash can now silently disable option_commit - this is not
1361 * a problem as it is only overriding the default, not a user
1362 * supplied option.
1363 */
1c7b76be
MV
1364 option_commit = 0;
1365 }
1366
1d14d0c9
VV
1367 if (option_commit < 0)
1368 option_commit = 1;
1369
00c7e7e7
JH
1370 if (!argc) {
1371 if (default_to_upstream)
1372 argc = setup_with_upstream(&argv);
1373 else
1374 die(_("No commit specified and merge.defaultToUpstream not set."));
1375 } else if (argc == 1 && !strcmp(argv[0], "-")) {
1376 argv[0] = "@{-1}";
4e8115ff 1377 }
00c7e7e7 1378
1c7b76be
MV
1379 if (!argc)
1380 usage_with_options(builtin_merge_usage,
1381 builtin_merge_options);
1382
1faac1ce 1383 if (!head_commit) {
1c7b76be
MV
1384 /*
1385 * If the merged head is a valid one there is no reason
1386 * to forbid "git merge" into a branch yet to be born.
1387 * We do the same for "git pull".
1388 */
52684310 1389 struct object_id *remote_head_oid;
4be636f4 1390 if (squash)
bacec478 1391 die(_("Squash commit into empty head not supported yet"));
a54841e9 1392 if (fast_forward == FF_NO)
bacec478
ÆAB
1393 die(_("Non-fast-forward commit does not make sense into "
1394 "an empty head"));
1cf32f4d
JH
1395 remoteheads = collect_parents(head_commit, &head_subsumed,
1396 argc, argv, NULL);
b84e65d4 1397 if (!remoteheads)
bacec478 1398 die(_("%s - not something we can merge"), argv[0]);
eaa4e59c
JH
1399 if (remoteheads->next)
1400 die(_("Can merge only exactly one commit into empty head"));
7488ba3e
JK
1401
1402 if (verify_signatures)
54887b46
HJI
1403 verify_merge_signature(remoteheads->item, verbosity,
1404 check_trust_level);
7488ba3e 1405
52684310 1406 remote_head_oid = &remoteheads->item->object.oid;
cb91022c 1407 read_empty(remote_head_oid, 0);
ae077771 1408 update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0,
1409 UPDATE_REFS_DIE_ON_ERR);
d5a35c11 1410 goto done;
1faac1ce 1411 }
1c7b76be 1412
1faac1ce 1413 /*
b4391657
JH
1414 * All the rest are the commits being merged; prepare
1415 * the standard merge summary message to be appended
1416 * to the given message.
1faac1ce 1417 */
b4391657
JH
1418 remoteheads = collect_parents(head_commit, &head_subsumed,
1419 argc, argv, &merge_msg);
1c7b76be 1420
894642f6 1421 if (!head_commit || !argc)
1c7b76be
MV
1422 usage_with_options(builtin_merge_usage,
1423 builtin_merge_options);
1424
efed0022
SG
1425 if (verify_signatures) {
1426 for (p = remoteheads; p; p = p->next) {
54887b46
HJI
1427 verify_merge_signature(p->item, verbosity,
1428 check_trust_level);
efed0022
SG
1429 }
1430 }
1431
1c7b76be 1432 strbuf_addstr(&buf, "merge");
b5d887f9
JH
1433 for (p = remoteheads; p; p = p->next)
1434 strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
1c7b76be
MV
1435 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1436 strbuf_reset(&buf);
1437
b5d887f9
JH
1438 for (p = remoteheads; p; p = p->next) {
1439 struct commit *commit = p->item;
ae8e4c9c 1440 strbuf_addf(&buf, "GITHEAD_%s",
c368dde9 1441 oid_to_hex(&commit->object.oid));
b5d887f9 1442 setenv(buf.buf, merge_remote_util(commit)->name, 1);
1c7b76be 1443 strbuf_reset(&buf);
adcc94a0 1444 if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit))
a54841e9 1445 fast_forward = FF_NO;
1c7b76be
MV
1446 }
1447
1448 if (!use_strategies) {
e78cbf8c
JH
1449 if (!remoteheads)
1450 ; /* already up-to-date */
1451 else if (!remoteheads->next)
1c7b76be
MV
1452 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1453 else
1454 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1455 }
1456
1457 for (i = 0; i < use_strategies_nr; i++) {
1458 if (use_strategies[i]->attr & NO_FAST_FORWARD)
a54841e9 1459 fast_forward = FF_NO;
1c7b76be
MV
1460 if (use_strategies[i]->attr & NO_TRIVIAL)
1461 allow_trivial = 0;
1462 }
1463
e78cbf8c
JH
1464 if (!remoteheads)
1465 ; /* already up-to-date */
1466 else if (!remoteheads->next)
2ce406cc 1467 common = get_merge_bases(head_commit, remoteheads->item);
1c7b76be
MV
1468 else {
1469 struct commit_list *list = remoteheads;
894642f6 1470 commit_list_insert(head_commit, &list);
1c7b76be
MV
1471 common = get_octopus_merge_bases(list);
1472 free(list);
1473 }
1474
ae077771 1475 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1476 &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1c7b76be 1477
e379fdf3
JH
1478 if (remoteheads && !common) {
1479 /* No common ancestors found. */
1480 if (!allow_unrelated_histories)
1481 die(_("refusing to merge unrelated histories"));
1482 /* otherwise, we need a real merge. */
1483 } else if (!remoteheads ||
e78cbf8c
JH
1484 (!remoteheads->next && !common->next &&
1485 common->item == remoteheads->item)) {
1c7b76be
MV
1486 /*
1487 * If head can reach all the merge then we are up to date.
1488 * but first the most common case of merging one remote.
1489 */
7560f547 1490 finish_up_to_date(_("Already up to date."));
d5a35c11 1491 goto done;
a54841e9 1492 } else if (fast_forward != FF_NO && !remoteheads->next &&
1c7b76be 1493 !common->next &&
4a7e27e9 1494 oideq(&common->item->object.oid, &head_commit->object.oid)) {
1c7b76be 1495 /* Again the most common case of merging one remote. */
f285a2d7 1496 struct strbuf msg = STRBUF_INIT;
ae8e4c9c 1497 struct commit *commit;
1c7b76be 1498
d59f765a 1499 if (verbosity >= 0) {
ef2ed501 1500 printf(_("Updating %s..%s\n"),
aab9583f 1501 find_unique_abbrev(&head_commit->object.oid,
ef2ed501 1502 DEFAULT_ABBREV),
aab9583f 1503 find_unique_abbrev(&remoteheads->item->object.oid,
ef2ed501 1504 DEFAULT_ABBREV));
d59f765a 1505 }
a75d7b54 1506 strbuf_addstr(&msg, "Fast-forward");
1c7b76be
MV
1507 if (have_message)
1508 strbuf_addstr(&msg,
1509 " (no commit created; -m option ignored)");
ae8e4c9c 1510 commit = remoteheads->item;
b7f7c079 1511 if (!commit) {
d5a35c11
NTND
1512 ret = 1;
1513 goto done;
1514 }
1c7b76be 1515
7e196c3a
NTND
1516 if (checkout_fast_forward(the_repository,
1517 &head_commit->object.oid,
f06e90da 1518 &commit->object.oid,
db699a8a 1519 overwrite_ignore)) {
d5a35c11
NTND
1520 ret = 1;
1521 goto done;
1522 }
1c7b76be 1523
52684310 1524 finish(head_commit, remoteheads, &commit->object.oid, msg.buf);
b6433555 1525 remove_merge_branch_state(the_repository);
d5a35c11 1526 goto done;
1c7b76be
MV
1527 } else if (!remoteheads->next && common->next)
1528 ;
1529 /*
a75d7b54 1530 * We are not doing octopus and not fast-forward. Need
1c7b76be
MV
1531 * a real merge.
1532 */
1533 else if (!remoteheads->next && !common->next && option_commit) {
1534 /*
a75d7b54 1535 * We are not doing octopus, not fast-forward, and have
1c7b76be
MV
1536 * only one common.
1537 */
1538 refresh_cache(REFRESH_QUIET);
a54841e9 1539 if (allow_trivial && fast_forward != FF_ONLY) {
1c7b76be 1540 /* See if it is really trivial. */
f9bc573f 1541 git_committer_info(IDENT_STRICT);
bacec478 1542 printf(_("Trying really trivial in-index merge...\n"));
52684310 1543 if (!read_tree_trivial(&common->item->object.oid,
1544 &head_commit->object.oid,
1545 &remoteheads->item->object.oid)) {
4c57bd27 1546 ret = merge_trivial(head_commit, remoteheads);
d5a35c11
NTND
1547 goto done;
1548 }
bacec478 1549 printf(_("Nope.\n"));
1c7b76be
MV
1550 }
1551 } else {
1552 /*
1553 * An octopus. If we can reach all the remote we are up
1554 * to date.
1555 */
1556 int up_to_date = 1;
1557 struct commit_list *j;
1558
1559 for (j = remoteheads; j; j = j->next) {
1560 struct commit_list *common_one;
1561
1562 /*
1563 * Here we *have* to calculate the individual
1564 * merge_bases again, otherwise "git merge HEAD^
1565 * HEAD^^" would be missed.
1566 */
2ce406cc 1567 common_one = get_merge_bases(head_commit, j->item);
9001dc2a 1568 if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) {
1c7b76be
MV
1569 up_to_date = 0;
1570 break;
1571 }
1572 }
1573 if (up_to_date) {
7560f547 1574 finish_up_to_date(_("Already up to date. Yeeah!"));
d5a35c11 1575 goto done;
1c7b76be
MV
1576 }
1577 }
1578
a54841e9 1579 if (fast_forward == FF_ONLY)
bacec478 1580 die(_("Not possible to fast-forward, aborting."));
13474835 1581
1c7b76be 1582 /* We are going to make a new commit. */
f9bc573f 1583 git_committer_info(IDENT_STRICT);
1c7b76be
MV
1584
1585 /*
1586 * At this point, we need a real merge. No matter what strategy
1587 * we use, it would operate on the index, possibly affecting the
1588 * working tree, and when resolved cleanly, have the desired
1589 * tree in the index -- this means that the index must be in
1590 * sync with the head commit. The strategies are responsible
1591 * to ensure this.
1592 */
b4fd9406
NTND
1593 if (use_strategies_nr == 1 ||
1594 /*
1595 * Stash away the local changes so that we can try more than one.
1596 */
52684310 1597 save_state(&stash))
1598 oidclr(&stash);
1c7b76be 1599
f00abe6a
ECA
1600 for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
1601 int ret, cnt;
1c7b76be 1602 if (i) {
bacec478 1603 printf(_("Rewinding the tree to pristine...\n"));
52684310 1604 restore_state(&head_commit->object.oid, &stash);
1c7b76be
MV
1605 }
1606 if (use_strategies_nr != 1)
bacec478 1607 printf(_("Trying merge strategy %s...\n"),
1c7b76be
MV
1608 use_strategies[i]->name);
1609 /*
1610 * Remember which strategy left the state in the working
1611 * tree.
1612 */
1613 wt_strategy = use_strategies[i]->name;
1614
1615 ret = try_merge_strategy(use_strategies[i]->name,
4c57bd27 1616 common, remoteheads,
b4391657 1617 head_commit);
f00abe6a
ECA
1618 /*
1619 * The backend exits with 1 when conflicts are
1620 * left to be resolved, with 2 when it does not
1621 * handle the given merge at all.
1622 */
1623 if (ret < 2) {
1624 if (!ret) {
1625 if (option_commit) {
1626 /* Automerge succeeded. */
1627 automerge_was_ok = 1;
1628 break;
1c7b76be 1629 }
f00abe6a
ECA
1630 merge_was_ok = 1;
1631 }
1632 cnt = evaluate_result();
1633 if (best_cnt <= 0 || cnt <= best_cnt) {
1634 best_strategy = use_strategies[i]->name;
1635 best_cnt = cnt;
1c7b76be 1636 }
1c7b76be 1637 }
1c7b76be
MV
1638 }
1639
1640 /*
1641 * If we have a resulting tree, that means the strategy module
1642 * auto resolved the merge cleanly.
1643 */
d5a35c11 1644 if (automerge_was_ok) {
e78cbf8c
JH
1645 ret = finish_automerge(head_commit, head_subsumed,
1646 common, remoteheads,
52684310 1647 &result_tree, wt_strategy);
d5a35c11
NTND
1648 goto done;
1649 }
1c7b76be
MV
1650
1651 /*
1652 * Pick the result from the best strategy and have the user fix
1653 * it up.
1654 */
1655 if (!best_strategy) {
52684310 1656 restore_state(&head_commit->object.oid, &stash);
1c7b76be
MV
1657 if (use_strategies_nr > 1)
1658 fprintf(stderr,
bacec478 1659 _("No merge strategy handled the merge.\n"));
1c7b76be 1660 else
bacec478 1661 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1c7b76be 1662 use_strategies[0]->name);
d5a35c11
NTND
1663 ret = 2;
1664 goto done;
1c7b76be
MV
1665 } else if (best_strategy == wt_strategy)
1666 ; /* We already have its result in the working tree. */
1667 else {
bacec478 1668 printf(_("Rewinding the tree to pristine...\n"));
52684310 1669 restore_state(&head_commit->object.oid, &stash);
bacec478 1670 printf(_("Using the %s to prepare resolving by hand.\n"),
1c7b76be 1671 best_strategy);
4c57bd27 1672 try_merge_strategy(best_strategy, common, remoteheads,
b4391657 1673 head_commit);
1c7b76be
MV
1674 }
1675
1676 if (squash)
4c57bd27 1677 finish(head_commit, remoteheads, NULL, NULL);
66f4b98a 1678 else
4c57bd27 1679 write_merge_state(remoteheads);
1c7b76be 1680
d5a35c11 1681 if (merge_was_ok)
bacec478
ÆAB
1682 fprintf(stderr, _("Automatic merge went well; "
1683 "stopped before committing as requested\n"));
d5a35c11 1684 else
08e3ce5a 1685 ret = suggest_conflicts();
d5a35c11
NTND
1686
1687done:
96ec7b1e 1688 free(branch_to_free);
d5a35c11 1689 return ret;
1c7b76be 1690}