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