blob: de68910177fbff00d19a0f8ef824776de0458636 [file] [log] [blame]
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001/*
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
Ævar Arnfjörð Bjarmasondfd0a892023-02-10 11:28:39 +01009#define USE_THE_INDEX_VARIABLE
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +000010#include "builtin.h"
Elijah Newren0b027f62023-03-21 06:25:58 +000011#include "abspath.h"
Elijah Newren6c6ddf92023-04-11 03:00:39 +000012#include "advice.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070013#include "config.h"
Elijah Newren4e120822023-04-11 00:41:57 -070014#include "editor.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +000015#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000016#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000017#include "hex.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070018#include "object-name.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020019#include "parse-options.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020020#include "lockfile.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020021#include "run-command.h"
Ævar Arnfjörð Bjarmason5e3aba32021-09-26 21:03:26 +020022#include "hook.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020023#include "diff.h"
Sergey Organov09322b12020-12-21 18:19:38 +030024#include "diff-merges.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020025#include "refs.h"
Brandon Williamsec0cb492018-05-16 15:57:48 -070026#include "refspec.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020027#include "commit.h"
28#include "diffcore.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000029#include "path.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020030#include "revision.h"
31#include "unpack-trees.h"
32#include "cache-tree.h"
33#include "dir.h"
34#include "utf8.h"
35#include "log-tree.h"
36#include "color.h"
Junio C Hamanofcab40a2008-07-15 19:09:46 -070037#include "rerere.h"
Miklos Vajna87091b42008-07-30 01:16:59 +020038#include "help.h"
Elijah Newren750324d2023-05-16 06:33:54 +000039#include "merge.h"
Miklos Vajna18668f52008-08-28 15:43:00 +020040#include "merge-recursive.h"
Elijah Newren14c45862020-11-02 23:45:34 +000041#include "merge-ort-wrappers.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080042#include "resolve-undo.h"
Junio C Hamano93e535a2011-03-23 23:48:24 -070043#include "remote.h"
Junio C Hamano898eacd2011-10-06 23:12:09 -070044#include "fmt-merge-msg.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070045#include "gpg-interface.h"
Junio C Hamano75c961b2014-10-24 11:34:59 -070046#include "sequencer.h"
René Scharfe02a8cfa2016-08-05 23:01:35 +020047#include "string-list.h"
Jonathan Tan3836d882017-08-18 15:20:21 -070048#include "packfile.h"
Junio C Hamanoadcc94a2018-02-14 10:18:55 -080049#include "tag.h"
Nguyễn Thái Ngọc Duy65b5f942018-05-20 20:40:06 +020050#include "alias.h"
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +070051#include "branch.h"
Derrick Stolee64043552018-07-20 16:33:04 +000052#include "commit-reach.h"
Denton Liud540b702019-04-17 11:23:27 +010053#include "wt-status.h"
Derrick Stoleeb23ea972020-04-16 20:14:03 +000054#include "commit-graph.h"
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020055
56#define DEFAULT_TWOHEAD (1<<0)
57#define DEFAULT_OCTOPUS (1<<1)
58#define NO_FAST_FORWARD (1<<2)
59#define NO_TRIVIAL (1<<3)
60
61struct strategy {
62 const char *name;
63 unsigned attr;
64};
65
66static const char * const builtin_merge_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070067 N_("git merge [<options>] [<commit>...]"),
Alex Henriea30e43f2021-05-15 14:01:11 -060068 "git merge --abort",
69 "git merge --continue",
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020070 NULL
71};
72
Junio C Hamano898eacd2011-10-06 23:12:09 -070073static int show_diffstat = 1, shortlog_len = -1, squash;
Vishal Verma1d14d0c2019-05-24 12:36:17 -060074static int option_commit = -1;
Miklos Vajnaa54841e2013-07-02 16:47:57 +020075static int option_edit = -1;
Sebastian Götteefed0022013-03-31 18:02:24 +020076static int allow_trivial = 1, have_message, verify_signatures;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +000077static int check_trust_level = 1;
Nguyễn Thái Ngọc Duyc1d70362011-11-27 17:15:33 +070078static int overwrite_ignore = 1;
Jeff King2c477892011-12-18 00:03:22 -050079static struct strbuf merge_msg = STRBUF_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020080static struct strategy **use_strategies;
81static size_t use_strategies_nr, use_strategies_alloc;
Avery Pennarun8cc5b292009-11-25 21:23:55 -050082static const char **xopts;
83static size_t xopts_nr, xopts_alloc;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020084static const char *branch;
Junio C Hamano0d8fc3e2011-05-04 17:42:51 -070085static char *branch_mergeoptions;
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +010086static int verbosity;
Junio C Hamanocb6020b2009-12-04 00:20:48 -080087static int allow_rerere_auto;
Johan Herland35d2fff2010-11-09 22:49:59 +010088static int abort_current_merge;
Nguyễn Thái Ngọc Duyf3f83112019-05-18 18:30:43 +070089static int quit_current_merge;
Chris Packham367ff692016-12-14 21:37:55 +130090static int continue_current_merge;
Junio C Hamanoe379fdf2016-03-18 13:21:09 -070091static int allow_unrelated_histories;
Jeff King99bfc662011-02-20 04:53:21 -050092static int show_progress = -1;
Felipe Contrerasa01f7f22014-04-20 19:17:33 -050093static int default_to_upstream = 1;
Łukasz Gryglicki14d01b42017-07-04 09:33:06 +000094static int signoff;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070095static const char *sign_commit;
Denton Liua03b5552020-04-07 10:28:07 -040096static int autostash;
Michael J Grubera1f3dd72019-08-07 11:57:06 -070097static int no_verify;
Junio C Hamanobd2bc942021-12-20 14:53:43 -080098static char *into_name;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +020099
100static struct strategy all_strategy[] = {
Elijah Newren6a5fb962021-08-04 05:38:01 +0000101 { "recursive", NO_TRIVIAL },
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200102 { "octopus", DEFAULT_OCTOPUS },
Elijah Newren6a5fb962021-08-04 05:38:01 +0000103 { "ort", DEFAULT_TWOHEAD | NO_TRIVIAL },
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200104 { "resolve", 0 },
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200105 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
106 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
107};
108
109static const char *pull_twohead, *pull_octopus;
110
Miklos Vajnaa54841e2013-07-02 16:47:57 +0200111enum ff_type {
112 FF_NO,
113 FF_ALLOW,
114 FF_ONLY
115};
116
117static enum ff_type fast_forward = FF_ALLOW;
118
Denton Liud540b702019-04-17 11:23:27 +0100119static const char *cleanup_arg;
120static enum commit_msg_cleanup_mode cleanup_mode;
121
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200122static int option_parse_message(const struct option *opt,
123 const char *arg, int unset)
124{
125 struct strbuf *buf = opt->value;
126
127 if (unset)
128 strbuf_setlen(buf, 0);
Michele Ballabio74f5b7f2008-07-20 14:34:47 +0200129 else if (arg) {
Junio C Hamanoce9d8232009-12-02 10:00:58 -0800130 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200131 have_message = 1;
Michele Ballabio74f5b7f2008-07-20 14:34:47 +0200132 } else
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000133 return error(_("switch `m' requires a value"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200134 return 0;
135}
136
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700137static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx,
138 const struct option *opt,
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700139 const char *arg_not_used,
Nguyễn Thái Ngọc Duyf41179f2019-01-27 07:35:27 +0700140 int unset)
Johannes Schindelin920f22e2017-12-22 15:10:02 +0100141{
142 struct strbuf *buf = opt->value;
143 const char *arg;
144
Nguyễn Thái Ngọc Duy3ebbe282019-01-27 07:35:28 +0700145 BUG_ON_OPT_ARG(arg_not_used);
Johannes Schindelin920f22e2017-12-22 15:10:02 +0100146 if (unset)
147 BUG("-F cannot be negated");
148
149 if (ctx->opt) {
150 arg = ctx->opt;
151 ctx->opt = NULL;
152 } else if (ctx->argc > 1) {
153 ctx->argc--;
154 arg = *++ctx->argv;
155 } else
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +0100156 return error(_("option `%s' requires a value"), opt->long_name);
Johannes Schindelin920f22e2017-12-22 15:10:02 +0100157
158 if (buf->len)
159 strbuf_addch(buf, '\n');
160 if (ctx->prefix && !is_absolute_path(arg))
161 arg = prefix_filename(ctx->prefix, arg);
162 if (strbuf_read_file(buf, arg, 0) < 0)
163 return error(_("could not read file '%s'"), arg);
164 have_message = 1;
165
166 return 0;
167}
168
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200169static struct strategy *get_strategy(const char *name)
170{
171 int i;
Miklos Vajna87091b42008-07-30 01:16:59 +0200172 struct strategy *ret;
173 static struct cmdnames main_cmds, other_cmds;
Alex Riesene3211802008-08-28 19:15:33 +0200174 static int loaded;
Elijah Newren14c45862020-11-02 23:45:34 +0000175 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200176
177 if (!name)
178 return NULL;
179
Elijah Newren14c45862020-11-02 23:45:34 +0000180 if (default_strategy &&
181 !strcmp(default_strategy, "ort") &&
182 !strcmp(name, "recursive")) {
183 name = "ort";
184 }
185
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200186 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
187 if (!strcmp(name, all_strategy[i].name))
188 return &all_strategy[i];
Miklos Vajna1719b5e2008-07-21 18:10:47 +0200189
Alex Riesene3211802008-08-28 19:15:33 +0200190 if (!loaded) {
Miklos Vajna87091b42008-07-30 01:16:59 +0200191 struct cmdnames not_strategies;
Alex Riesene3211802008-08-28 19:15:33 +0200192 loaded = 1;
Miklos Vajna87091b42008-07-30 01:16:59 +0200193
Miklos Vajna87091b42008-07-30 01:16:59 +0200194 memset(&not_strategies, 0, sizeof(struct cmdnames));
Alex Riesene3211802008-08-28 19:15:33 +0200195 load_command_list("git-merge-", &main_cmds, &other_cmds);
Miklos Vajna87091b42008-07-30 01:16:59 +0200196 for (i = 0; i < main_cmds.cnt; i++) {
197 int j, found = 0;
198 struct cmdname *ent = main_cmds.names[i];
Seija Kijin0c756922023-01-09 17:34:28 +0000199 for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++)
Miklos Vajna87091b42008-07-30 01:16:59 +0200200 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
201 && !all_strategy[j].name[ent->len])
202 found = 1;
203 if (!found)
204 add_cmdname(&not_strategies, ent->name, ent->len);
Miklos Vajna87091b42008-07-30 01:16:59 +0200205 }
Avery Pennaruned874652009-11-25 21:23:54 -0500206 exclude_cmds(&main_cmds, &not_strategies);
Miklos Vajna87091b42008-07-30 01:16:59 +0200207 }
208 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000209 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
210 fprintf(stderr, _("Available strategies are:"));
Junio C Hamano131f9a12008-08-20 22:07:55 -0700211 for (i = 0; i < main_cmds.cnt; i++)
212 fprintf(stderr, " %s", main_cmds.names[i]->name);
213 fprintf(stderr, ".\n");
214 if (other_cmds.cnt) {
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000215 fprintf(stderr, _("Available custom strategies are:"));
Junio C Hamano131f9a12008-08-20 22:07:55 -0700216 for (i = 0; i < other_cmds.cnt; i++)
217 fprintf(stderr, " %s", other_cmds.names[i]->name);
218 fprintf(stderr, ".\n");
219 }
Miklos Vajna87091b42008-07-30 01:16:59 +0200220 exit(1);
221 }
222
René Scharfeca56dad2021-03-13 17:17:22 +0100223 CALLOC_ARRAY(ret, 1);
Miklos Vajna87091b42008-07-30 01:16:59 +0200224 ret->name = xstrdup(name);
Jonathan Nieder52b48ef2010-08-15 20:11:06 -0500225 ret->attr = NO_TRIVIAL;
Miklos Vajna87091b42008-07-30 01:16:59 +0200226 return ret;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200227}
228
229static void append_strategy(struct strategy *s)
230{
231 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
232 use_strategies[use_strategies_nr++] = s;
233}
234
235static int option_parse_strategy(const struct option *opt,
236 const char *name, int unset)
237{
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200238 if (unset)
239 return 0;
240
Miklos Vajna1719b5e2008-07-21 18:10:47 +0200241 append_strategy(get_strategy(name));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200242 return 0;
243}
244
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500245static int option_parse_x(const struct option *opt,
246 const char *arg, int unset)
247{
248 if (unset)
249 return 0;
250
251 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
252 xopts[xopts_nr++] = xstrdup(arg);
253 return 0;
254}
255
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200256static int option_parse_n(const struct option *opt,
257 const char *arg, int unset)
258{
Jeff King517fe802018-11-05 01:45:42 -0500259 BUG_ON_OPT_ARG(arg);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200260 show_diffstat = unset;
261 return 0;
262}
263
264static struct option builtin_merge_options[] = {
Denton Liu203c8532020-04-28 04:36:28 -0400265 OPT_CALLBACK_F('n', NULL, NULL, NULL,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700266 N_("do not show a diffstat at the end of the merge"),
Denton Liu203c8532020-04-28 04:36:28 -0400267 PARSE_OPT_NOARG, option_parse_n),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200268 OPT_BOOL(0, "stat", &show_diffstat,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700269 N_("show a diffstat at the end of the merge")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200270 OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")),
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700271 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
272 N_("add (at most <n>) entries from shortlog to merge commit message"),
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +0530273 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200274 OPT_BOOL(0, "squash", &squash,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700275 N_("create a single commit instead of doing a merge")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200276 OPT_BOOL(0, "commit", &option_commit,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700277 N_("perform a commit if the merge succeeds (default)")),
Junio C Hamanof8246282012-01-10 22:44:45 -0800278 OPT_BOOL('e', "edit", &option_edit,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700279 N_("edit message before committing")),
Denton Liud540b702019-04-17 11:23:27 +0100280 OPT_CLEANUP(&cleanup_arg),
Miklos Vajnaa54841e2013-07-02 16:47:57 +0200281 OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200282 OPT_SET_INT_F(0, "ff-only", &fast_forward,
283 N_("abort if fast-forward is not possible"),
284 FF_ONLY, PARSE_OPT_NONEG),
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800285 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
Sebastian Götteefed0022013-03-31 18:02:24 +0200286 OPT_BOOL(0, "verify-signatures", &verify_signatures,
Vasco Almeidac8bb9d22016-06-17 20:21:18 +0000287 N_("verify that the named commit has a valid GPG signature")),
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700288 OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
289 N_("merge strategy to use"), option_parse_strategy),
290 OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
291 N_("option for selected merge strategy"), option_parse_x),
292 OPT_CALLBACK('m', "message", &merge_msg, N_("message"),
293 N_("merge commit message (for a non-fast-forward merge)"),
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200294 option_parse_message),
Johannes Schindelin920f22e2017-12-22 15:10:02 +0100295 { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
296 N_("read message from file"), PARSE_OPT_NONEG,
Nguyễn Thái Ngọc Duybf3ff332019-01-27 07:35:26 +0700297 NULL, 0, option_read_message },
Junio C Hamanobd2bc942021-12-20 14:53:43 -0800298 OPT_STRING(0, "into-name", &into_name, N_("name"),
299 N_("use <name> instead of the real target")),
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100300 OPT__VERBOSITY(&verbosity),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200301 OPT_BOOL(0, "abort", &abort_current_merge,
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700302 N_("abort the current in-progress merge")),
Nguyễn Thái Ngọc Duyf3f83112019-05-18 18:30:43 +0700303 OPT_BOOL(0, "quit", &quit_current_merge,
304 N_("--abort but leave index and working tree alone")),
Chris Packham367ff692016-12-14 21:37:55 +1300305 OPT_BOOL(0, "continue", &continue_current_merge,
306 N_("continue the current in-progress merge")),
Junio C Hamanoe379fdf2016-03-18 13:21:09 -0700307 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
308 N_("allow merging unrelated histories")),
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700309 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
Junio C Hamanoe703d712014-03-23 15:58:12 -0700310 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
Nguyễn Thái Ngọc Duy962e6292012-08-20 19:32:24 +0700311 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
Denton Liua03b5552020-04-07 10:28:07 -0400312 OPT_AUTOSTASH(&autostash),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200313 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")),
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -0700314 OPT_BOOL(0, "signoff", &signoff, N_("add a Signed-off-by trailer")),
Michael J Gruberbc40ce42019-08-07 11:57:08 -0700315 OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")),
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200316 OPT_END()
317};
318
brian m. carlson52684312017-02-21 23:47:28 +0000319static int save_state(struct object_id *stash)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200320{
321 int len;
René Scharfed3180272014-08-19 21:09:35 +0200322 struct child_process cp = CHILD_PROCESS_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200323 struct strbuf buffer = STRBUF_INIT;
Elijah Newren1369f142022-07-23 01:53:15 +0000324 struct lock_file lock_file = LOCK_INIT;
325 int fd;
Rene Scharfe150888e2017-08-30 19:49:49 +0200326 int rc = -1;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200327
Elijah Newren1369f142022-07-23 01:53:15 +0000328 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100329 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Elijah Newren1369f142022-07-23 01:53:15 +0000330 if (0 <= fd)
331 repo_update_index_if_able(the_repository, &lock_file);
332 rollback_lock_file(&lock_file);
333
Ævar Arnfjörð Bjarmason2b709892021-11-25 23:52:20 +0100334 strvec_pushl(&cp.args, "stash", "create", NULL);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200335 cp.out = -1;
336 cp.git_cmd = 1;
337
338 if (start_command(&cp))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000339 die(_("could not run stash."));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200340 len = strbuf_read(&buffer, cp.out, 1024);
341 close(cp.out);
342
343 if (finish_command(&cp) || len < 0)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000344 die(_("stash failed"));
Nguyễn Thái Ngọc Duyb4fd9402011-08-19 21:50:05 +0700345 else if (!len) /* no changes */
Rene Scharfe150888e2017-08-30 19:49:49 +0200346 goto out;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200347 strbuf_setlen(&buffer, buffer.len-1);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200348 if (repo_get_oid(the_repository, buffer.buf, stash))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000349 die(_("not a valid object: %s"), buffer.buf);
Rene Scharfe150888e2017-08-30 19:49:49 +0200350 rc = 0;
351out:
352 strbuf_release(&buffer);
353 return rc;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200354}
355
Ævar Arnfjörð Bjarmason9397f3c2022-10-30 12:42:59 +0100356static void read_empty(const struct object_id *oid)
Clemens Buchacher172b6422010-11-14 23:07:49 +0100357{
René Scharfe41202942022-10-30 12:50:27 +0100358 struct child_process cmd = CHILD_PROCESS_INIT;
Clemens Buchacher172b6422010-11-14 23:07:49 +0100359
René Scharfe41202942022-10-30 12:50:27 +0100360 strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(),
361 oid_to_hex(oid), NULL);
362 cmd.git_cmd = 1;
Clemens Buchacher172b6422010-11-14 23:07:49 +0100363
René Scharfe41202942022-10-30 12:50:27 +0100364 if (run_command(&cmd))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000365 die(_("read-tree failed"));
Clemens Buchacher172b6422010-11-14 23:07:49 +0100366}
367
Ævar Arnfjörð Bjarmason9397f3c2022-10-30 12:42:59 +0100368static void reset_hard(const struct object_id *oid)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200369{
René Scharfe41202942022-10-30 12:50:27 +0100370 struct child_process cmd = CHILD_PROCESS_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200371
René Scharfe41202942022-10-30 12:50:27 +0100372 strvec_pushl(&cmd.args, "read-tree", "-v", "--reset", "-u",
373 oid_to_hex(oid), NULL);
374 cmd.git_cmd = 1;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200375
René Scharfe41202942022-10-30 12:50:27 +0100376 if (run_command(&cmd))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000377 die(_("read-tree failed"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200378}
379
brian m. carlson52684312017-02-21 23:47:28 +0000380static void restore_state(const struct object_id *head,
381 const struct object_id *stash)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200382{
René Scharfe0e906732022-10-30 12:51:14 +0100383 struct child_process cmd = CHILD_PROCESS_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200384
Ævar Arnfjörð Bjarmason9397f3c2022-10-30 12:42:59 +0100385 reset_hard(head);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200386
Elijah Newrenc23fc072022-07-23 01:53:18 +0000387 if (is_null_oid(stash))
388 goto refresh_cache;
389
René Scharfe0e906732022-10-30 12:51:14 +0100390 strvec_pushl(&cmd.args, "stash", "apply", "--index", "--quiet", NULL);
391 strvec_push(&cmd.args, oid_to_hex(stash));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200392
393 /*
394 * It is OK to ignore error here, for example when there was
395 * nothing to restore.
396 */
René Scharfe0e906732022-10-30 12:51:14 +0100397 cmd.git_cmd = 1;
398 run_command(&cmd);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200399
Elijah Newrenc23fc072022-07-23 01:53:18 +0000400refresh_cache:
Ævar Arnfjörð Bjarmasondfd0a892023-02-10 11:28:39 +0100401 discard_index(&the_index);
402 if (repo_read_index(the_repository) < 0)
Elijah Newrenc23fc072022-07-23 01:53:18 +0000403 die(_("could not read index"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200404}
405
406/* This is called when no merge was necessary. */
Josh Sorefad9322d2021-05-02 01:14:23 -0400407static void finish_up_to_date(void)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200408{
Josh Sorefad9322d2021-05-02 01:14:23 -0400409 if (verbosity >= 0) {
410 if (squash)
411 puts(_("Already up to date. (nothing to squash)"));
412 else
413 puts(_("Already up to date."));
414 }
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +0700415 remove_merge_branch_state(the_repository);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200416}
417
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700418static void squash_message(struct commit *commit, struct commit_list *remoteheads)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200419{
420 struct rev_info rev;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500421 struct strbuf out = STRBUF_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200422 struct commit_list *j;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200423 struct pretty_print_context ctx = {0};
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200424
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000425 printf(_("Squash commit -- not updating HEAD\n"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200426
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200427 repo_init_revisions(the_repository, &rev, NULL);
Sergey Organov09322b12020-12-21 18:19:38 +0300428 diff_merges_suppress(&rev);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200429 rev.commit_format = CMIT_FMT_MEDIUM;
430
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200431 commit->object.flags |= UNINTERESTING;
432 add_pending_object(&rev, &commit->object, NULL);
433
434 for (j = remoteheads; j; j = j->next)
435 add_pending_object(&rev, &j->item->object, NULL);
436
437 setup_revisions(0, NULL, &rev, NULL);
438 if (prepare_revision_walk(&rev))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000439 die(_("revision walk setup failed"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200440
Thomas Rastdd2e7942009-10-19 17:48:08 +0200441 ctx.abbrev = rev.abbrev;
442 ctx.date_mode = rev.date_mode;
Jeff King6bf13942011-05-26 18:27:49 -0400443 ctx.fmt = rev.commit_format;
Thomas Rastdd2e7942009-10-19 17:48:08 +0200444
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200445 strbuf_addstr(&out, "Squashed commit of the following:\n");
446 while ((commit = get_revision(&rev)) != NULL) {
447 strbuf_addch(&out, '\n');
448 strbuf_addf(&out, "commit %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000449 oid_to_hex(&commit->object.oid));
Jeff King6bf13942011-05-26 18:27:49 -0400450 pretty_print_commit(&ctx, commit, &out);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200451 }
Stefan Beller102de882018-05-17 15:51:51 -0700452 write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200453 strbuf_release(&out);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +0200454 release_revisions(&rev);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200455}
456
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +1000457static void finish(struct commit *head_commit,
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700458 struct commit_list *remoteheads,
brian m. carlson52684312017-02-21 23:47:28 +0000459 const struct object_id *new_head, const char *msg)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200460{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500461 struct strbuf reflog_message = STRBUF_INIT;
brian m. carlson52684312017-02-21 23:47:28 +0000462 const struct object_id *head = &head_commit->object.oid;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200463
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200464 if (!msg)
465 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
466 else {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100467 if (verbosity >= 0)
468 printf("%s\n", msg);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200469 strbuf_addf(&reflog_message, "%s: %s",
470 getenv("GIT_REFLOG_ACTION"), msg);
471 }
472 if (squash) {
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700473 squash_message(head_commit, remoteheads);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200474 } else {
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100475 if (verbosity >= 0 && !merge_msg.len)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000476 printf(_("No merge message -- not updating HEAD\n"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200477 else {
brian m. carlsonae077772017-10-15 22:06:51 +0000478 update_ref(reflog_message.buf, "HEAD", new_head, head,
479 0, UPDATE_REFS_DIE_ON_ERR);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200480 /*
481 * We ignore errors in 'gc --auto', since the
482 * user should see them.
483 */
Derrick Stoleea95ce122020-09-17 18:11:44 +0000484 run_auto_maintenance(verbosity < 0);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200485 }
486 }
487 if (new_head && show_diffstat) {
488 struct diff_options opts;
Nguyễn Thái Ngọc Duye6757652018-09-21 17:57:24 +0200489 repo_diff_setup(the_repository, &opts);
Zbigniew Jędrzejewski-Szmek7a7159a2012-03-01 13:26:42 +0100490 opts.stat_width = -1; /* use full terminal width */
Zbigniew Jędrzejewski-Szmekdf444832012-03-01 13:26:46 +0100491 opts.stat_graph_width = -1; /* respect statGraphWidth config */
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200492 opts.output_format |=
493 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
494 opts.detect_rename = DIFF_DETECT_RENAME;
Thomas Rast28452652012-08-03 14:16:24 +0200495 diff_setup_done(&opts);
Brandon Williams66f414f2017-05-30 10:31:03 -0700496 diff_tree_oid(head, new_head, "", &opts);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200497 diffcore_std(&opts);
498 diff_flush(&opts);
499 }
500
501 /* Run a post-merge hook */
Emily Shaffer67ad6302021-12-22 04:59:34 +0100502 run_hooks_l("post-merge", squash ? "1" : "0", NULL);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200503
Elijah Newrend3a92952022-08-23 02:42:19 +0000504 if (new_head)
505 apply_autostash(git_path_merge_autostash(the_repository));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200506 strbuf_release(&reflog_message);
507}
508
509/* Get the name for the merge commit's message. */
510static void merge_name(const char *remote, struct strbuf *msg)
511{
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800512 struct commit *remote_head;
brian m. carlson52684312017-02-21 23:47:28 +0000513 struct object_id branch_head;
Junio C Hamanoc9717ee2009-02-13 23:26:12 -0800514 struct strbuf bname = STRBUF_INIT;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200515 struct merge_remote_desc *desc;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200516 const char *ptr;
Andrzej Hunt8c05e422021-07-25 15:08:28 +0200517 char *found_ref = NULL;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200518 int len, early;
519
Jeff King0e9f62d2017-03-02 03:23:01 -0500520 strbuf_branchname(&bname, remote, 0);
Junio C Hamanoa552de72009-03-21 13:17:30 -0700521 remote = bname.buf;
Junio C Hamanoc9717ee2009-02-13 23:26:12 -0800522
brian m. carlson52684312017-02-21 23:47:28 +0000523 oidclr(&branch_head);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800524 remote_head = get_merge_parent(remote);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200525 if (!remote_head)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000526 die(_("'%s' does not point to a commit"), remote);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200527
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +0200528 if (repo_dwim_ref(the_repository, remote, strlen(remote), &branch_head,
529 &found_ref, 0) > 0) {
Christian Couder59556542013-11-30 21:55:40 +0100530 if (starts_with(found_ref, "refs/heads/")) {
Jeff King751c5972009-08-09 06:02:24 -0400531 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
brian m. carlson52684312017-02-21 23:47:28 +0000532 oid_to_hex(&branch_head), remote);
Jeff King751c5972009-08-09 06:02:24 -0400533 goto cleanup;
534 }
Christian Couder59556542013-11-30 21:55:40 +0100535 if (starts_with(found_ref, "refs/tags/")) {
Junio C Hamano57b58db2011-11-04 21:31:28 -0700536 strbuf_addf(msg, "%s\t\ttag '%s' of .\n",
brian m. carlson52684312017-02-21 23:47:28 +0000537 oid_to_hex(&branch_head), remote);
Junio C Hamano57b58db2011-11-04 21:31:28 -0700538 goto cleanup;
539 }
Christian Couder59556542013-11-30 21:55:40 +0100540 if (starts_with(found_ref, "refs/remotes/")) {
Matthieu Moy13931232010-11-02 16:31:25 +0100541 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
brian m. carlson52684312017-02-21 23:47:28 +0000542 oid_to_hex(&branch_head), remote);
Jeff King69a8b7c2009-08-09 06:02:51 -0400543 goto cleanup;
544 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200545 }
546
547 /* See if remote matches <name>^^^.. or <name>~<number> */
548 for (len = 0, ptr = remote + strlen(remote);
549 remote < ptr && ptr[-1] == '^';
550 ptr--)
551 len++;
552 if (len)
553 early = 1;
554 else {
555 early = 0;
556 ptr = strrchr(remote, '~');
557 if (ptr) {
558 int seen_nonzero = 0;
559
560 len++; /* count ~ */
561 while (*++ptr && isdigit(*ptr)) {
562 seen_nonzero |= (*ptr != '0');
563 len++;
564 }
565 if (*ptr)
566 len = 0; /* not ...~<number> */
567 else if (seen_nonzero)
568 early = 1;
569 else if (len == 1)
570 early = 1; /* "name~" is "name~1"! */
571 }
572 }
573 if (len) {
574 struct strbuf truname = STRBUF_INIT;
Junio C Hamano10166582015-04-23 14:37:13 -0700575 strbuf_addf(&truname, "refs/heads/%s", remote);
Junio C Hamano9b6bf4d2008-07-30 01:12:19 -0700576 strbuf_setlen(&truname, truname.len - len);
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700577 if (ref_exists(truname.buf)) {
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200578 strbuf_addf(msg,
579 "%s\t\tbranch '%s'%s of .\n",
brian m. carlsonc368dde2016-06-24 23:09:22 +0000580 oid_to_hex(&remote_head->object.oid),
Junio C Hamano9b6bf4d2008-07-30 01:12:19 -0700581 truname.buf + 11,
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200582 (early ? " (early part)" : ""));
Junio C Hamanoc9717ee2009-02-13 23:26:12 -0800583 strbuf_release(&truname);
584 goto cleanup;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200585 }
Junio C Hamano10166582015-04-23 14:37:13 -0700586 strbuf_release(&truname);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200587 }
Junio C Hamano2d1495f2013-03-19 09:55:34 -0700588
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200589 desc = merge_remote_util(remote_head);
590 if (desc && desc->obj && desc->obj->type == OBJ_TAG) {
591 strbuf_addf(msg, "%s\t\t%s '%s'\n",
592 oid_to_hex(&desc->obj->oid),
593 type_name(desc->obj->type),
594 remote);
595 goto cleanup;
Junio C Hamano2d1495f2013-03-19 09:55:34 -0700596 }
597
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200598 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
brian m. carlsonc368dde2016-06-24 23:09:22 +0000599 oid_to_hex(&remote_head->object.oid), remote);
Junio C Hamanoc9717ee2009-02-13 23:26:12 -0800600cleanup:
Andrzej Hunt8c05e422021-07-25 15:08:28 +0200601 free(found_ref);
Junio C Hamanoc9717ee2009-02-13 23:26:12 -0800602 strbuf_release(&bname);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200603}
604
Junio C Hamano0d8fc3e2011-05-04 17:42:51 -0700605static void parse_branch_merge_options(char *bmo)
606{
607 const char **argv;
608 int argc;
609
610 if (!bmo)
611 return;
612 argc = split_cmdline(bmo, &argv);
613 if (argc < 0)
Junio C Hamanoc7fe5b62011-05-11 11:38:36 -0700614 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
Nguyễn Thái Ngọc Duya7412ae2018-11-10 06:16:01 +0100615 _(split_cmdline_strerror(argc)));
René Scharfe2756ca42014-09-16 20:56:57 +0200616 REALLOC_ARRAY(argv, argc + 2);
René Scharfef331ab92017-07-15 22:00:45 +0200617 MOVE_ARRAY(argv + 1, argv, argc + 1);
Junio C Hamano0d8fc3e2011-05-04 17:42:51 -0700618 argc++;
619 argv[0] = "branch.*.mergeoptions";
620 parse_options(argc, argv, NULL, builtin_merge_options,
621 builtin_merge_usage, 0);
622 free(argv);
623}
624
Glen Chooa4e7e312023-06-28 19:26:22 +0000625static int git_merge_config(const char *k, const char *v,
626 const struct config_context *ctx, void *cb)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200627{
Junio C Hamano898eacd2011-10-06 23:12:09 -0700628 int status;
Martin Ågren9881b452020-04-11 09:11:45 +0200629 const char *str;
Junio C Hamano898eacd2011-10-06 23:12:09 -0700630
Martin Ågren9881b452020-04-11 09:11:45 +0200631 if (branch &&
632 skip_prefix(k, "branch.", &str) &&
633 skip_prefix(str, branch, &str) &&
634 !strcmp(str, ".mergeoptions")) {
Junio C Hamano0d8fc3e2011-05-04 17:42:51 -0700635 free(branch_mergeoptions);
636 branch_mergeoptions = xstrdup(v);
637 return 0;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200638 }
639
640 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
641 show_diffstat = git_config_bool(k, v);
Hans Jerry Illikainenca779e82017-12-10 06:53:57 +0000642 else if (!strcmp(k, "merge.verifysignatures"))
643 verify_signatures = git_config_bool(k, v);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200644 else if (!strcmp(k, "pull.twohead"))
645 return git_config_string(&pull_twohead, k, v);
646 else if (!strcmp(k, "pull.octopus"))
647 return git_config_string(&pull_octopus, k, v);
Denton Liud540b702019-04-17 11:23:27 +0100648 else if (!strcmp(k, "commit.cleanup"))
649 return git_config_string(&cleanup_arg, k, v);
Junio C Hamano898eacd2011-10-06 23:12:09 -0700650 else if (!strcmp(k, "merge.ff")) {
Martin Ågren89576612017-08-07 20:20:49 +0200651 int boolval = git_parse_maybe_bool(v);
Junio C Hamanof23e8de2011-05-06 12:27:05 -0700652 if (0 <= boolval) {
Miklos Vajnaa54841e2013-07-02 16:47:57 +0200653 fast_forward = boolval ? FF_ALLOW : FF_NO;
Junio C Hamanof23e8de2011-05-06 12:27:05 -0700654 } else if (v && !strcmp(v, "only")) {
Miklos Vajnaa54841e2013-07-02 16:47:57 +0200655 fast_forward = FF_ONLY;
Junio C Hamanof23e8de2011-05-06 12:27:05 -0700656 } /* do not barf on values from future versions of git */
657 return 0;
Junio C Hamano93e535a2011-03-23 23:48:24 -0700658 } else if (!strcmp(k, "merge.defaulttoupstream")) {
659 default_to_upstream = git_config_bool(k, v);
660 return 0;
Nicolas Vigierd95bfb12013-11-05 00:14:41 +0100661 } else if (!strcmp(k, "commit.gpgsign")) {
662 sign_commit = git_config_bool(k, v) ? "" : NULL;
663 return 0;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +0000664 } else if (!strcmp(k, "gpg.mintrustlevel")) {
665 check_trust_level = 0;
Denton Liua03b5552020-04-07 10:28:07 -0400666 } else if (!strcmp(k, "merge.autostash")) {
667 autostash = git_config_bool(k, v);
668 return 0;
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +0530669 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700670
Glen Chooa4e7e312023-06-28 19:26:22 +0000671 status = fmt_merge_msg_config(k, v, ctx, cb);
Junio C Hamano898eacd2011-10-06 23:12:09 -0700672 if (status)
673 return status;
Glen Chooa4e7e312023-06-28 19:26:22 +0000674 return git_diff_ui_config(k, v, ctx, cb);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200675}
676
brian m. carlson52684312017-02-21 23:47:28 +0000677static int read_tree_trivial(struct object_id *common, struct object_id *head,
678 struct object_id *one)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200679{
680 int i, nr_trees = 0;
681 struct tree *trees[MAX_UNPACK_TREES];
682 struct tree_desc t[MAX_UNPACK_TREES];
683 struct unpack_trees_options opts;
684
685 memset(&opts, 0, sizeof(opts));
686 opts.head_idx = 2;
687 opts.src_index = &the_index;
688 opts.dst_index = &the_index;
689 opts.update = 1;
690 opts.verbose_update = 1;
691 opts.trivial_merges_only = 1;
692 opts.merge = 1;
Elijah Newren1b5f3732021-09-27 16:33:43 +0000693 opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
brian m. carlsona9dbc172017-05-06 22:10:37 +0000694 trees[nr_trees] = parse_tree_indirect(common);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200695 if (!trees[nr_trees++])
696 return -1;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000697 trees[nr_trees] = parse_tree_indirect(head);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200698 if (!trees[nr_trees++])
699 return -1;
brian m. carlsona9dbc172017-05-06 22:10:37 +0000700 trees[nr_trees] = parse_tree_indirect(one);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200701 if (!trees[nr_trees++])
702 return -1;
703 opts.fn = threeway_merge;
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100704 cache_tree_free(&the_index.cache_tree);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200705 for (i = 0; i < nr_trees; i++) {
706 parse_tree(trees[i]);
707 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
708 }
709 if (unpack_trees(nr_trees, t, &opts))
710 return -1;
711 return 0;
712}
713
brian m. carlson52684312017-02-21 23:47:28 +0000714static void write_tree_trivial(struct object_id *oid)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200715{
Ævar Arnfjörð Bjarmason99370862023-02-10 11:28:37 +0100716 if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000717 die(_("git write-tree failed to write a tree"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200718}
719
Christian Couder3f9083c2010-03-31 21:22:06 +0200720static int try_merge_strategy(const char *strategy, struct commit_list *common,
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700721 struct commit_list *remoteheads,
Junio C Hamanob4391652015-03-25 22:00:48 -0700722 struct commit *head)
Christian Couder3f9083c2010-03-31 21:22:06 +0200723{
Junio C Hamanob4391652015-03-25 22:00:48 -0700724 const char *head_arg = "HEAD";
Miklos Vajna668f26f2008-10-03 15:02:31 +0200725
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100726 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
727 SKIP_IF_UNCHANGED, 0, NULL, NULL,
728 NULL) < 0)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000729 return error(_("Unable to write index."));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200730
Elijah Newren14c45862020-11-02 23:45:34 +0000731 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
732 !strcmp(strategy, "ort")) {
Thomas Gummerere080b342019-09-11 19:20:26 +0100733 struct lock_file lock = LOCK_INIT;
Christian Couder3f9083c2010-03-31 21:22:06 +0200734 int clean, x;
Miklos Vajna18668f52008-08-28 15:43:00 +0200735 struct commit *result;
Miklos Vajna18668f52008-08-28 15:43:00 +0200736 struct commit_list *reversed = NULL;
737 struct merge_options o;
Christian Couder3f9083c2010-03-31 21:22:06 +0200738 struct commit_list *j;
Miklos Vajna18668f52008-08-28 15:43:00 +0200739
740 if (remoteheads->next) {
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000741 error(_("Not handling anything other than two heads merge."));
Miklos Vajna18668f52008-08-28 15:43:00 +0200742 return 2;
743 }
744
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700745 init_merge_options(&o, the_repository);
Miklos Vajna18668f52008-08-28 15:43:00 +0200746 if (!strcmp(strategy, "subtree"))
Junio C Hamano85e51b72008-06-30 22:18:57 -0700747 o.subtree_shift = "";
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500748
Jeff King99bfc662011-02-20 04:53:21 -0500749 o.show_rename_progress =
750 show_progress == -1 ? isatty(2) : show_progress;
Jonathan Nieder7610fa52010-08-05 06:32:41 -0500751
Jonathan Nieder635a7bb2010-08-26 00:47:58 -0500752 for (x = 0; x < xopts_nr; x++)
753 if (parse_merge_opt(&o, xopts[x]))
Elijah Newren81483fe2021-08-04 23:50:54 +0000754 die(_("unknown strategy option: -X%s"), xopts[x]);
Miklos Vajna18668f52008-08-28 15:43:00 +0200755
756 o.branch1 = head_arg;
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800757 o.branch2 = merge_remote_util(remoteheads->item)->name;
Miklos Vajna18668f52008-08-28 15:43:00 +0200758
759 for (j = common; j; j = j->next)
760 commit_list_insert(j->item, &reversed);
761
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100762 repo_hold_locked_index(the_repository, &lock,
763 LOCK_DIE_ON_ERROR);
Elijah Newren14c45862020-11-02 23:45:34 +0000764 if (!strcmp(strategy, "ort"))
765 clean = merge_ort_recursive(&o, head, remoteheads->item,
766 reversed, &result);
767 else
768 clean = merge_recursive(&o, head, remoteheads->item,
769 reversed, &result);
Elijah Newren8f240b82022-07-23 01:53:14 +0000770 if (clean < 0) {
771 rollback_lock_file(&lock);
772 return 2;
773 }
Martin Ågren61000812018-03-01 21:40:20 +0100774 if (write_locked_index(&the_index, &lock,
775 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200776 die(_("unable to write %s"), get_index_file());
Miklos Vajna18668f52008-08-28 15:43:00 +0200777 return clean ? 0 : 1;
778 } else {
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +0200779 return try_merge_command(the_repository,
780 strategy, xopts_nr, xopts,
781 common, head_arg, remoteheads);
Miklos Vajna18668f52008-08-28 15:43:00 +0200782 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200783}
784
785static void count_diff_files(struct diff_queue_struct *q,
Jeff King61bdc7c2022-12-13 06:13:48 -0500786 struct diff_options *opt UNUSED, void *data)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200787{
788 int *count = data;
789
790 (*count) += q->nr;
791}
792
793static int count_unmerged_entries(void)
794{
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200795 int i, ret = 0;
796
Ævar Arnfjörð Bjarmasondc594182022-11-19 14:07:34 +0100797 for (i = 0; i < the_index.cache_nr; i++)
798 if (ce_stage(the_index.cache[i]))
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200799 ret++;
800
801 return ret;
802}
803
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200804static void add_strategies(const char *string, unsigned attr)
805{
René Scharfe02a8cfa2016-08-05 23:01:35 +0200806 int i;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200807
René Scharfe02a8cfa2016-08-05 23:01:35 +0200808 if (string) {
809 struct string_list list = STRING_LIST_INIT_DUP;
810 struct string_list_item *item;
811 string_list_split(&list, string, ' ', -1);
812 for_each_string_list_item(item, &list)
813 append_strategy(get_strategy(item->string));
814 string_list_clear(&list, 0);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200815 return;
816 }
817 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
818 if (all_strategy[i].attr & attr)
819 append_strategy(&all_strategy[i]);
820
821}
822
Jay Soffian66f4b982011-10-08 14:39:52 -0400823static void read_merge_msg(struct strbuf *msg)
Jay Soffian65969d42011-02-14 20:07:50 -0500824{
Stefan Beller102de882018-05-17 15:51:51 -0700825 const char *filename = git_path_merge_msg(the_repository);
Jay Soffian66f4b982011-10-08 14:39:52 -0400826 strbuf_reset(msg);
Jonathan Nieder418c9b12011-11-16 02:03:36 -0600827 if (strbuf_read_file(msg, filename, 0) < 0)
828 die_errno(_("Could not read from '%s'"), filename);
Jay Soffian65969d42011-02-14 20:07:50 -0500829}
830
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700831static void write_merge_state(struct commit_list *);
832static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
Jay Soffian65969d42011-02-14 20:07:50 -0500833{
Jay Soffian66f4b982011-10-08 14:39:52 -0400834 if (err_msg)
835 error("%s", err_msg);
836 fprintf(stderr,
837 _("Not committing merge; use 'git commit' to complete the merge.\n"));
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700838 write_merge_state(remoteheads);
Jay Soffian66f4b982011-10-08 14:39:52 -0400839 exit(1);
840}
841
Thomas Rastf26af3f2012-01-30 21:25:30 +0100842static const char merge_editor_comment[] =
843N_("Please enter a commit message to explain why this merge is necessary,\n"
844 "especially if it merges an updated upstream into a topic branch.\n"
Denton Liud540b702019-04-17 11:23:27 +0100845 "\n");
846
847static const char scissors_editor_comment[] =
848N_("An empty message aborts the commit.\n");
849
850static const char no_scissors_editor_comment[] =
851N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
Thomas Rastf26af3f2012-01-30 21:25:30 +0100852 "the commit.\n");
853
Michael J Gruber9d89b352017-08-23 14:10:45 +0200854static void write_merge_heads(struct commit_list *);
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700855static void prepare_to_commit(struct commit_list *remoteheads)
Jay Soffian66f4b982011-10-08 14:39:52 -0400856{
857 struct strbuf msg = STRBUF_INIT;
Michael J Gruber60988172019-08-07 11:57:07 -0700858 const char *index_file = get_index_file();
859
Ævar Arnfjörð Bjarmason9f6e63b2022-03-07 13:33:45 +0100860 if (!no_verify) {
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100861 int invoked_hook;
862
863 if (run_commit_hook(0 < option_edit, index_file, &invoked_hook,
Ævar Arnfjörð Bjarmason9f6e63b2022-03-07 13:33:45 +0100864 "pre-merge-commit", NULL))
865 abort_commit(remoteheads, NULL);
866 /*
867 * Re-read the index as pre-merge-commit hook could have updated it,
868 * and write it out as a tree. We must do this before we invoke
869 * the editor and after we invoke run_status above.
870 */
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100871 if (invoked_hook)
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100872 discard_index(&the_index);
Ævar Arnfjörð Bjarmason9f6e63b2022-03-07 13:33:45 +0100873 }
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100874 read_index_from(&the_index, index_file, get_git_dir());
Jay Soffian66f4b982011-10-08 14:39:52 -0400875 strbuf_addbuf(&msg, &merge_msg);
Michael J Gruber62dc42b2017-08-23 14:10:43 +0200876 if (squash)
877 BUG("the control must not reach here under --squash");
Denton Liud540b702019-04-17 11:23:27 +0100878 if (0 < option_edit) {
879 strbuf_addch(&msg, '\n');
880 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
881 wt_status_append_cut_line(&msg);
Calvin Wan787cb8a2023-06-06 19:48:43 +0000882 strbuf_commented_addf(&msg, comment_line_char, "\n");
Denton Liud540b702019-04-17 11:23:27 +0100883 }
Calvin Wan787cb8a2023-06-06 19:48:43 +0000884 strbuf_commented_addf(&msg, comment_line_char,
885 _(merge_editor_comment));
Carlo Marcelo Arenas Belón00e302d2021-08-07 20:38:34 -0700886 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
Calvin Wan787cb8a2023-06-06 19:48:43 +0000887 strbuf_commented_addf(&msg, comment_line_char,
888 _(scissors_editor_comment));
Carlo Marcelo Arenas Belón00e302d2021-08-07 20:38:34 -0700889 else
Calvin Wan787cb8a2023-06-06 19:48:43 +0000890 strbuf_commented_addf(&msg, comment_line_char,
Carlo Marcelo Arenas Belón00e302d2021-08-07 20:38:34 -0700891 _(no_scissors_editor_comment), comment_line_char);
Denton Liud540b702019-04-17 11:23:27 +0100892 }
Łukasz Gryglicki14d01b42017-07-04 09:33:06 +0000893 if (signoff)
894 append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
Michael J Gruber9d89b352017-08-23 14:10:45 +0200895 write_merge_heads(remoteheads);
Stefan Beller102de882018-05-17 15:51:51 -0700896 write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100897 if (run_commit_hook(0 < option_edit, get_index_file(), NULL,
898 "prepare-commit-msg",
Stefan Beller102de882018-05-17 15:51:51 -0700899 git_path_merge_msg(the_repository), "merge", NULL))
Antoine Pelisse3e4141d2013-01-02 19:42:50 +0100900 abort_commit(remoteheads, NULL);
Junio C Hamanof8246282012-01-10 22:44:45 -0800901 if (0 < option_edit) {
Stefan Beller102de882018-05-17 15:51:51 -0700902 if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700903 abort_commit(remoteheads, NULL);
Jay Soffian66f4b982011-10-08 14:39:52 -0400904 }
Stefan Bellerf8b86352017-09-07 15:04:29 -0700905
Michael J Grubera1f3dd72019-08-07 11:57:06 -0700906 if (!no_verify && run_commit_hook(0 < option_edit, get_index_file(),
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +0100907 NULL, "commit-msg",
Stefan Beller102de882018-05-17 15:51:51 -0700908 git_path_merge_msg(the_repository), NULL))
Stefan Bellerf8b86352017-09-07 15:04:29 -0700909 abort_commit(remoteheads, NULL);
910
Jay Soffian66f4b982011-10-08 14:39:52 -0400911 read_merge_msg(&msg);
Denton Liud540b702019-04-17 11:23:27 +0100912 cleanup_message(&msg, cleanup_mode, 0);
Jay Soffian66f4b982011-10-08 14:39:52 -0400913 if (!msg.len)
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700914 abort_commit(remoteheads, _("Empty commit message."));
Jay Soffian66f4b982011-10-08 14:39:52 -0400915 strbuf_release(&merge_msg);
916 strbuf_addbuf(&merge_msg, &msg);
917 strbuf_release(&msg);
Jay Soffian65969d42011-02-14 20:07:50 -0500918}
919
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700920static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200921{
brian m. carlson52684312017-02-21 23:47:28 +0000922 struct object_id result_tree, result_commit;
René Scharfe910a09a2014-07-10 11:41:40 +0200923 struct commit_list *parents, **pptr = &parents;
Elijah Newren40d71942016-04-09 23:13:40 -0700924
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +0100925 if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET,
926 SKIP_IF_UNCHANGED, 0, NULL, NULL,
927 NULL) < 0)
Elijah Newren40d71942016-04-09 23:13:40 -0700928 return error(_("Unable to write index."));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200929
brian m. carlson52684312017-02-21 23:47:28 +0000930 write_tree_trivial(&result_tree);
Ævar Arnfjörð Bjarmason157efde2011-02-22 23:42:02 +0000931 printf(_("Wonderful.\n"));
René Scharfe910a09a2014-07-10 11:41:40 +0200932 pptr = commit_list_append(head, pptr);
933 pptr = commit_list_append(remoteheads->item, pptr);
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700934 prepare_to_commit(remoteheads);
Patryk Obara5078f342018-01-28 01:13:16 +0100935 if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents,
936 &result_commit, NULL, sign_commit))
Nguyễn Thái Ngọc Duy6b3c4c02011-12-15 20:47:21 +0700937 die(_("failed to write commit object"));
brian m. carlson52684312017-02-21 23:47:28 +0000938 finish(head, remoteheads, &result_commit, "In-index merge");
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +0700939 remove_merge_branch_state(the_repository);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200940 return 0;
941}
942
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +1000943static int finish_automerge(struct commit *head,
Junio C Hamanoe78cbf82012-04-17 12:22:26 -0700944 int head_subsumed,
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +1000945 struct commit_list *common,
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700946 struct commit_list *remoteheads,
brian m. carlson52684312017-02-21 23:47:28 +0000947 struct object_id *result_tree,
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200948 const char *wt_strategy)
949{
Junio C Hamanoe78cbf82012-04-17 12:22:26 -0700950 struct commit_list *parents = NULL;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200951 struct strbuf buf = STRBUF_INIT;
brian m. carlson52684312017-02-21 23:47:28 +0000952 struct object_id result_commit;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200953
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -0600954 write_tree_trivial(result_tree);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200955 free_commit_list(common);
Junio C Hamanoe78cbf82012-04-17 12:22:26 -0700956 parents = remoteheads;
Miklos Vajnaa54841e2013-07-02 16:47:57 +0200957 if (!head_subsumed || fast_forward == FF_NO)
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +1000958 commit_list_insert(head, &parents);
Junio C Hamano4c57bd22012-04-16 16:15:13 -0700959 prepare_to_commit(remoteheads);
Patryk Obara5078f342018-01-28 01:13:16 +0100960 if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
961 &result_commit, NULL, sign_commit))
Nguyễn Thái Ngọc Duy6b3c4c02011-12-15 20:47:21 +0700962 die(_("failed to write commit object"));
Junio C Hamanof23101b2011-05-25 12:43:59 -0700963 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
brian m. carlson52684312017-02-21 23:47:28 +0000964 finish(head, remoteheads, &result_commit, buf.buf);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200965 strbuf_release(&buf);
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +0700966 remove_merge_branch_state(the_repository);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200967 return 0;
968}
969
Junio C Hamano08e3ce52014-10-24 11:27:22 -0700970static int suggest_conflicts(void)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200971{
Jonathan Nieder418c9b12011-11-16 02:03:36 -0600972 const char *filename;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200973 FILE *fp;
Junio C Hamano75c961b2014-10-24 11:34:59 -0700974 struct strbuf msgbuf = STRBUF_INIT;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200975
Stefan Beller102de882018-05-17 15:51:51 -0700976 filename = git_path_merge_msg(the_repository);
Nguyễn Thái Ngọc Duy23a9e072017-05-03 17:16:46 +0700977 fp = xfopen(filename, "a");
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200978
Denton Liu10559972019-04-17 11:23:28 +0100979 /*
980 * We can't use cleanup_mode because if we're not using the editor,
981 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
982 * though the message is meant to be processed later by git-commit.
983 * Thus, we will get the cleanup mode which is returned when we _are_
984 * using an editor.
985 */
Denton Liu1a2b9852019-04-17 11:23:30 +0100986 append_conflicts_hint(&the_index, &msgbuf,
987 get_cleanup_mode(cleanup_arg, 1));
Junio C Hamano75c961b2014-10-24 11:34:59 -0700988 fputs(msgbuf.buf, fp);
René Scharfe8d025b72014-12-24 01:18:38 +0100989 strbuf_release(&msgbuf);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200990 fclose(fp);
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200991 repo_rerere(the_repository, allow_rerere_auto);
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +0000992 printf(_("Automatic merge failed; "
993 "fix conflicts and then commit the result.\n"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200994 return 1;
995}
996
Miklos Vajna1c7b76b2008-07-07 19:24:20 +0200997static int evaluate_result(void)
998{
999 int cnt = 0;
1000 struct rev_info rev;
1001
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001002 /* Check how many files differ. */
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02001003 repo_init_revisions(the_repository, &rev, "");
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001004 setup_revisions(0, NULL, &rev, NULL);
1005 rev.diffopt.output_format |=
1006 DIFF_FORMAT_CALLBACK;
1007 rev.diffopt.format_callback = count_diff_files;
1008 rev.diffopt.format_callback_data = &cnt;
1009 run_diff_files(&rev, 0);
1010
1011 /*
1012 * Check how many unmerged entries are
1013 * there.
1014 */
1015 cnt += count_unmerged_entries();
1016
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001017 release_revisions(&rev);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001018 return cnt;
1019}
1020
Junio C Hamano93e535a2011-03-23 23:48:24 -07001021/*
Michael Schubertd6ac1d22013-07-03 11:12:34 +02001022 * Pretend as if the user told us to merge with the remote-tracking
Junio C Hamano93e535a2011-03-23 23:48:24 -07001023 * branch we have for the upstream of the current branch
1024 */
1025static int setup_with_upstream(const char ***argv)
1026{
1027 struct branch *branch = branch_get(NULL);
1028 int i;
1029 const char **args;
1030
1031 if (!branch)
Ævar Arnfjörð Bjarmasonc7f426d2011-04-10 19:34:04 +00001032 die(_("No current branch."));
Jeff King9e3751d2015-05-21 00:45:13 -04001033 if (!branch->remote_name)
Ævar Arnfjörð Bjarmasonc7f426d2011-04-10 19:34:04 +00001034 die(_("No remote for the current branch."));
Junio C Hamano93e535a2011-03-23 23:48:24 -07001035 if (!branch->merge_nr)
Ævar Arnfjörð Bjarmasonc7f426d2011-04-10 19:34:04 +00001036 die(_("No default upstream defined for the current branch."));
Junio C Hamano93e535a2011-03-23 23:48:24 -07001037
Jeff King50a6c8e2016-02-22 17:44:35 -05001038 args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *));
Junio C Hamano93e535a2011-03-23 23:48:24 -07001039 for (i = 0; i < branch->merge_nr; i++) {
1040 if (!branch->merge[i]->dst)
Michael Schubertd6ac1d22013-07-03 11:12:34 +02001041 die(_("No remote-tracking branch for %s from %s"),
Junio C Hamano93e535a2011-03-23 23:48:24 -07001042 branch->merge[i]->src, branch->remote_name);
1043 args[i] = branch->merge[i]->dst;
1044 }
1045 args[i] = NULL;
1046 *argv = args;
1047 return i;
1048}
1049
Michael J Gruber8e6a6bb2017-08-23 14:10:44 +02001050static void write_merge_heads(struct commit_list *remoteheads)
Jay Soffian66f4b982011-10-08 14:39:52 -04001051{
Jay Soffian66f4b982011-10-08 14:39:52 -04001052 struct commit_list *j;
1053 struct strbuf buf = STRBUF_INIT;
1054
Junio C Hamano274a5c02011-11-07 14:45:10 -08001055 for (j = remoteheads; j; j = j->next) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001056 struct object_id *oid;
Junio C Hamano274a5c02011-11-07 14:45:10 -08001057 struct commit *c = j->item;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001058 struct merge_remote_desc *desc;
1059
1060 desc = merge_remote_util(c);
1061 if (desc && desc->obj) {
1062 oid = &desc->obj->oid;
Junio C Hamano274a5c02011-11-07 14:45:10 -08001063 } else {
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001064 oid = &c->object.oid;
Junio C Hamano274a5c02011-11-07 14:45:10 -08001065 }
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001066 strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
Junio C Hamano274a5c02011-11-07 14:45:10 -08001067 }
Stefan Beller102de882018-05-17 15:51:51 -07001068 write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
Jonathan Nieder418c9b12011-11-16 02:03:36 -06001069
Jay Soffian66f4b982011-10-08 14:39:52 -04001070 strbuf_reset(&buf);
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001071 if (fast_forward == FF_NO)
René Scharfea22ae752016-09-15 20:31:00 +02001072 strbuf_addstr(&buf, "no-ff");
Stefan Beller102de882018-05-17 15:51:51 -07001073 write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
Rene Scharfe814c4b32017-08-30 19:49:50 +02001074 strbuf_release(&buf);
Jay Soffian66f4b982011-10-08 14:39:52 -04001075}
1076
Michael J Gruber8e6a6bb2017-08-23 14:10:44 +02001077static void write_merge_state(struct commit_list *remoteheads)
1078{
1079 write_merge_heads(remoteheads);
1080 strbuf_addch(&merge_msg, '\n');
Stefan Beller102de882018-05-17 15:51:51 -07001081 write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
1082 merge_msg.len);
Michael J Gruber8e6a6bb2017-08-23 14:10:44 +02001083}
1084
Junio C Hamanof8246282012-01-10 22:44:45 -08001085static int default_edit_option(void)
1086{
1087 static const char name[] = "GIT_MERGE_AUTOEDIT";
1088 const char *e = getenv(name);
1089 struct stat st_stdin, st_stdout;
1090
1091 if (have_message)
1092 /* an explicit -m msg without --[no-]edit */
1093 return 0;
1094
1095 if (e) {
Martin Ågren89576612017-08-07 20:20:49 +02001096 int v = git_parse_maybe_bool(e);
Junio C Hamanof8246282012-01-10 22:44:45 -08001097 if (v < 0)
Vasco Almeidabef48302016-06-17 20:21:17 +00001098 die(_("Bad value '%s' in environment '%s'"), e, name);
Junio C Hamanof8246282012-01-10 22:44:45 -08001099 return v;
1100 }
1101
1102 /* Use editor if stdin and stdout are the same and is a tty */
1103 return (!fstat(0, &st_stdin) &&
1104 !fstat(1, &st_stdout) &&
Junio C Hamanod46f4762012-02-23 11:24:44 -08001105 isatty(0) && isatty(1) &&
Junio C Hamanof8246282012-01-10 22:44:45 -08001106 st_stdin.st_dev == st_stdout.st_dev &&
1107 st_stdin.st_ino == st_stdout.st_ino &&
1108 st_stdin.st_mode == st_stdout.st_mode);
1109}
1110
Junio C Hamano34349db2015-04-25 12:00:14 -07001111static struct commit_list *reduce_parents(struct commit *head_commit,
1112 int *head_subsumed,
1113 struct commit_list *remoteheads)
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001114{
René Scharfee510ab82015-10-24 18:21:31 +02001115 struct commit_list *parents, **remotes;
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001116
Junio C Hamano0b10b8a2015-04-25 10:25:43 -07001117 /*
1118 * Is the current HEAD reachable from another commit being
1119 * merged? If so we do not want to record it as a parent of
1120 * the resulting merge, unless --no-ff is given. We will flip
1121 * this variable to 0 when we find HEAD among the independent
1122 * tips being merged.
1123 */
1124 *head_subsumed = 1;
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001125
Junio C Hamano0b10b8a2015-04-25 10:25:43 -07001126 /* Find what parents to record by checking independent ones. */
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001127 parents = reduce_heads(remoteheads);
Martin Ågren4da72642017-11-07 21:39:45 +01001128 free_commit_list(remoteheads);
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001129
René Scharfee510ab82015-10-24 18:21:31 +02001130 remoteheads = NULL;
1131 remotes = &remoteheads;
1132 while (parents) {
1133 struct commit *commit = pop_commit(&parents);
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001134 if (commit == head_commit)
1135 *head_subsumed = 0;
1136 else
1137 remotes = &commit_list_insert(commit, remotes)->next;
1138 }
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001139 return remoteheads;
1140}
Junio C Hamanof8246282012-01-10 22:44:45 -08001141
Junio C Hamano52fecab2015-04-25 18:29:44 -07001142static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg)
1143{
1144 struct fmt_merge_msg_opts opts;
1145
1146 memset(&opts, 0, sizeof(opts));
1147 opts.add_title = !have_message;
1148 opts.shortlog_len = shortlog_len;
1149 opts.credit_people = (0 < option_edit);
Junio C Hamanobd2bc942021-12-20 14:53:43 -08001150 opts.into_name = into_name;
Junio C Hamano52fecab2015-04-25 18:29:44 -07001151
1152 fmt_merge_msg(merge_names, merge_msg, &opts);
1153 if (merge_msg->len)
1154 strbuf_setlen(merge_msg, merge_msg->len - 1);
1155}
1156
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001157static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names)
1158{
1159 const char *filename;
1160 int fd, pos, npos;
1161 struct strbuf fetch_head_file = STRBUF_INIT;
brian m. carlsonab47df22018-07-16 01:28:03 +00001162 const unsigned hexsz = the_hash_algo->hexsz;
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001163
1164 if (!merge_names)
1165 merge_names = &fetch_head_file;
1166
Stefan Beller102de882018-05-17 15:51:51 -07001167 filename = git_path_fetch_head(the_repository);
René Scharfe66e905b2021-08-25 22:16:46 +02001168 fd = xopen(filename, O_RDONLY);
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001169
1170 if (strbuf_read(merge_names, fd, 0) < 0)
1171 die_errno(_("could not read '%s'"), filename);
1172 if (close(fd) < 0)
1173 die_errno(_("could not close '%s'"), filename);
1174
1175 for (pos = 0; pos < merge_names->len; pos = npos) {
brian m. carlson52684312017-02-21 23:47:28 +00001176 struct object_id oid;
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001177 char *ptr;
1178 struct commit *commit;
1179
1180 ptr = strchr(merge_names->buf + pos, '\n');
1181 if (ptr)
1182 npos = ptr - merge_names->buf + 1;
1183 else
1184 npos = merge_names->len;
1185
brian m. carlsonab47df22018-07-16 01:28:03 +00001186 if (npos - pos < hexsz + 2 ||
brian m. carlson52684312017-02-21 23:47:28 +00001187 get_oid_hex(merge_names->buf + pos, &oid))
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001188 commit = NULL; /* bad */
brian m. carlsonab47df22018-07-16 01:28:03 +00001189 else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2))
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001190 continue; /* not-for-merge */
1191 else {
brian m. carlsonab47df22018-07-16 01:28:03 +00001192 char saved = merge_names->buf[pos + hexsz];
1193 merge_names->buf[pos + hexsz] = '\0';
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001194 commit = get_merge_parent(merge_names->buf + pos);
brian m. carlsonab47df22018-07-16 01:28:03 +00001195 merge_names->buf[pos + hexsz] = saved;
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001196 }
1197 if (!commit) {
1198 if (ptr)
1199 *ptr = '\0';
Vasco Almeidabef48302016-06-17 20:21:17 +00001200 die(_("not something we can merge in %s: %s"),
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001201 filename, merge_names->buf + pos);
1202 }
1203 remotes = &commit_list_insert(commit, remotes)->next;
1204 }
1205
1206 if (merge_names == &fetch_head_file)
1207 strbuf_release(&fetch_head_file);
1208}
1209
Junio C Hamano34349db2015-04-25 12:00:14 -07001210static struct commit_list *collect_parents(struct commit *head_commit,
1211 int *head_subsumed,
Junio C Hamano1cf32f42015-04-25 18:34:22 -07001212 int argc, const char **argv,
1213 struct strbuf *merge_msg)
Junio C Hamano34349db2015-04-25 12:00:14 -07001214{
1215 int i;
1216 struct commit_list *remoteheads = NULL;
1217 struct commit_list **remotes = &remoteheads;
Junio C Hamano77038012015-04-25 18:39:43 -07001218 struct strbuf merge_names = STRBUF_INIT, *autogen = NULL;
1219
1220 if (merge_msg && (!have_message || shortlog_len))
1221 autogen = &merge_names;
Junio C Hamano34349db2015-04-25 12:00:14 -07001222
1223 if (head_commit)
1224 remotes = &commit_list_insert(head_commit, remotes)->next;
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001225
1226 if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) {
1227 handle_fetch_head(remotes, autogen);
1228 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1229 } else {
1230 for (i = 0; i < argc; i++) {
1231 struct commit *commit = get_merge_parent(argv[i]);
1232 if (!commit)
1233 help_unknown_ref(argv[i], "merge",
Vasco Almeidabef48302016-06-17 20:21:17 +00001234 _("not something we can merge"));
Junio C Hamano74e8bc52015-04-25 18:47:21 -07001235 remotes = &commit_list_insert(commit, remotes)->next;
1236 }
1237 remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads);
1238 if (autogen) {
1239 struct commit_list *p;
1240 for (p = remoteheads; p; p = p->next)
1241 merge_name(merge_remote_util(p->item)->name, autogen);
1242 }
Junio C Hamano34349db2015-04-25 12:00:14 -07001243 }
1244
Junio C Hamano77038012015-04-25 18:39:43 -07001245 if (autogen) {
Junio C Hamano77038012015-04-25 18:39:43 -07001246 prepare_merge_message(autogen, merge_msg);
1247 strbuf_release(autogen);
Junio C Hamano1cf32f42015-04-25 18:34:22 -07001248 }
1249
1250 return remoteheads;
Junio C Hamano34349db2015-04-25 12:00:14 -07001251}
1252
Junio C Hamanoadcc94a2018-02-14 10:18:55 -08001253static int merging_a_throwaway_tag(struct commit *commit)
1254{
1255 char *tag_ref;
1256 struct object_id oid;
1257 int is_throwaway_tag = 0;
1258
1259 /* Are we merging a tag? */
1260 if (!merge_remote_util(commit) ||
1261 !merge_remote_util(commit)->obj ||
1262 merge_remote_util(commit)->obj->type != OBJ_TAG)
1263 return is_throwaway_tag;
1264
1265 /*
1266 * Now we know we are merging a tag object. Are we downstream
1267 * and following the tags from upstream? If so, we must have
1268 * the tag object pointed at by "refs/tags/$T" where $T is the
1269 * tagname recorded in the tag object. We want to allow such
1270 * a "just to catch up" merge to fast-forward.
1271 *
1272 * Otherwise, we are playing an integrator's role, making a
1273 * merge with a throw-away tag from a contributor with
1274 * something like "git pull $contributor $signed_tag".
1275 * We want to forbid such a merge from fast-forwarding
1276 * by default; otherwise we would not keep the signature
1277 * anywhere.
1278 */
1279 tag_ref = xstrfmt("refs/tags/%s",
1280 ((struct tag *)merge_remote_util(commit)->obj)->tag);
1281 if (!read_ref(tag_ref, &oid) &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001282 oideq(&oid, &merge_remote_util(commit)->obj->oid))
Junio C Hamanoadcc94a2018-02-14 10:18:55 -08001283 is_throwaway_tag = 0;
1284 else
1285 is_throwaway_tag = 1;
1286 free(tag_ref);
1287 return is_throwaway_tag;
1288}
1289
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001290int cmd_merge(int argc, const char **argv, const char *prefix)
1291{
brian m. carlson52684312017-02-21 23:47:28 +00001292 struct object_id result_tree, stash, head_oid;
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +10001293 struct commit *head_commit;
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001294 struct strbuf buf = STRBUF_INIT;
Michael Haggerty17377b62016-04-07 15:03:06 -04001295 int i, ret = 0, head_subsumed;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001296 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1297 struct commit_list *common = NULL;
1298 const char *best_strategy = NULL, *wt_strategy = NULL;
Elijah Newren6046f7a2022-01-20 07:47:15 +00001299 struct commit_list *remoteheads = NULL, *p;
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001300 void *branch_to_free;
Chris Packham367ff692016-12-14 21:37:55 +13001301 int orig_argc = argc;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001302
Nguyễn Thái Ngọc Duyda53eec2010-10-22 01:49:45 -05001303 if (argc == 2 && !strcmp(argv[1], "-h"))
1304 usage_with_options(builtin_merge_usage, builtin_merge_options);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001305
Derrick Stoleea3380632021-09-08 11:23:57 +00001306 prepare_repo_settings(the_repository);
1307 the_repository->settings.command_requires_full_index = 0;
1308
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001309 /*
1310 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1311 * current branch.
1312 */
brian m. carlson0f2dc722017-10-15 22:06:55 +00001313 branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL);
René Scharfede3ce212017-08-10 18:47:55 +02001314 if (branch)
1315 skip_prefix(branch, "refs/heads/", &branch);
Derrick Stolee7adf5262018-05-01 12:47:23 +00001316
Elijah Newren14c45862020-11-02 23:45:34 +00001317 if (!pull_twohead) {
1318 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1319 if (default_strategy && !strcmp(default_strategy, "ort"))
1320 pull_twohead = "ort";
1321 }
1322
Derrick Stolee7adf5262018-05-01 12:47:23 +00001323 init_diff_ui_defaults();
1324 git_config(git_merge_config, NULL);
1325
brian m. carlson52684312017-02-21 23:47:28 +00001326 if (!branch || is_null_oid(&head_oid))
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +10001327 head_commit = NULL;
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +10001328 else
brian m. carlsonbc832662017-05-06 22:10:10 +00001329 head_commit = lookup_commit_or_die(&head_oid, "HEAD");
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001330
Junio C Hamano0d8fc3e2011-05-04 17:42:51 -07001331 if (branch_mergeoptions)
1332 parse_branch_merge_options(branch_mergeoptions);
Stephen Boyd37782922009-05-23 11:53:12 -07001333 argc = parse_options(argc, argv, prefix, builtin_merge_options,
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001334 builtin_merge_usage, 0);
Junio C Hamano898eacd2011-10-06 23:12:09 -07001335 if (shortlog_len < 0)
1336 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
Johan Herland2a22c1b2010-11-09 22:49:58 +01001337
Jeff King99bfc662011-02-20 04:53:21 -05001338 if (verbosity < 0 && show_progress == -1)
1339 show_progress = 0;
1340
Johan Herland35d2fff2010-11-09 22:49:59 +01001341 if (abort_current_merge) {
1342 int nargc = 2;
1343 const char *nargv[] = {"reset", "--merge", NULL};
Denton Liua03b5552020-04-07 10:28:07 -04001344 struct strbuf stash_oid = STRBUF_INIT;
Johan Herland35d2fff2010-11-09 22:49:59 +01001345
Chris Packham042e2902016-12-14 21:37:57 +13001346 if (orig_argc != 2)
Jeff Kingc7d227d2016-12-15 12:43:46 -05001347 usage_msg_opt(_("--abort expects no arguments"),
Chris Packham042e2902016-12-14 21:37:57 +13001348 builtin_merge_usage, builtin_merge_options);
1349
Stefan Beller102de882018-05-17 15:51:51 -07001350 if (!file_exists(git_path_merge_head(the_repository)))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001351 die(_("There is no merge to abort (MERGE_HEAD missing)."));
Johan Herland35d2fff2010-11-09 22:49:59 +01001352
Denton Liua03b5552020-04-07 10:28:07 -04001353 if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository),
1354 READ_ONELINER_SKIP_IF_EMPTY))
1355 unlink(git_path_merge_autostash(the_repository));
1356
Johan Herland35d2fff2010-11-09 22:49:59 +01001357 /* Invoke 'git reset --merge' */
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001358 ret = cmd_reset(nargc, nargv, prefix);
Denton Liua03b5552020-04-07 10:28:07 -04001359
1360 if (stash_oid.len)
1361 apply_autostash_oid(stash_oid.buf);
1362
1363 strbuf_release(&stash_oid);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001364 goto done;
Johan Herland35d2fff2010-11-09 22:49:59 +01001365 }
1366
Nguyễn Thái Ngọc Duyf3f83112019-05-18 18:30:43 +07001367 if (quit_current_merge) {
1368 if (orig_argc != 2)
1369 usage_msg_opt(_("--quit expects no arguments"),
1370 builtin_merge_usage,
1371 builtin_merge_options);
1372
1373 remove_merge_branch_state(the_repository);
1374 goto done;
1375 }
1376
Chris Packham367ff692016-12-14 21:37:55 +13001377 if (continue_current_merge) {
1378 int nargc = 1;
1379 const char *nargv[] = {"commit", NULL};
1380
1381 if (orig_argc != 2)
Jeff Kingc7d227d2016-12-15 12:43:46 -05001382 usage_msg_opt(_("--continue expects no arguments"),
Chris Packham367ff692016-12-14 21:37:55 +13001383 builtin_merge_usage, builtin_merge_options);
1384
Stefan Beller102de882018-05-17 15:51:51 -07001385 if (!file_exists(git_path_merge_head(the_repository)))
Chris Packham367ff692016-12-14 21:37:55 +13001386 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1387
1388 /* Invoke 'git commit' */
1389 ret = cmd_commit(nargc, nargv, prefix);
1390 goto done;
1391 }
1392
Ævar Arnfjörð Bjarmasonfbc1ed62022-11-19 14:07:30 +01001393 if (repo_read_index_unmerged(the_repository))
Johan Herland2a22c1b2010-11-09 22:49:58 +01001394 die_resolve_conflict("merge");
1395
Stefan Beller102de882018-05-17 15:51:51 -07001396 if (file_exists(git_path_merge_head(the_repository))) {
Johan Herland2a22c1b2010-11-09 22:49:58 +01001397 /*
1398 * There is no unmerged entry, don't advise 'git
1399 * add/rm <file>', just 'git commit'.
1400 */
Ben Boeckeled9bff02021-08-23 12:44:00 +02001401 if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001402 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
Alex Henriead5fe372014-08-30 13:56:01 -06001403 "Please, commit your changes before you merge."));
Johan Herland2a22c1b2010-11-09 22:49:58 +01001404 else
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001405 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
Johan Herland2a22c1b2010-11-09 22:49:58 +01001406 }
Han-Wen Nienhuysc8e41592020-08-21 16:59:35 +00001407 if (ref_exists("CHERRY_PICK_HEAD")) {
Ben Boeckeled9bff02021-08-23 12:44:00 +02001408 if (advice_enabled(ADVICE_RESOLVE_CONFLICT))
Ævar Arnfjörð Bjarmasonf68f1802011-04-10 19:34:05 +00001409 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
Alex Henriead5fe372014-08-30 13:56:01 -06001410 "Please, commit your changes before you merge."));
Jay Soffiand7e5c0c2011-02-19 23:12:27 -05001411 else
Ævar Arnfjörð Bjarmasonf68f1802011-04-10 19:34:05 +00001412 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
Jay Soffiand7e5c0c2011-02-19 23:12:27 -05001413 }
Ævar Arnfjörð Bjarmason031b2032022-11-19 14:07:33 +01001414 resolve_undo_clear_index(&the_index);
Johan Herland2a22c1b2010-11-09 22:49:58 +01001415
Denton Liud540b702019-04-17 11:23:27 +01001416 if (option_edit < 0)
1417 option_edit = default_edit_option();
1418
1419 cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
1420
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +01001421 if (verbosity < 0)
1422 show_diffstat = 0;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001423
1424 if (squash) {
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001425 if (fast_forward == FF_NO)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001426 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
Vishal Verma1d14d0c2019-05-24 12:36:17 -06001427 if (option_commit > 0)
Jean-Noël Avila12909b62022-01-05 20:02:16 +00001428 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
Vishal Verma1d14d0c2019-05-24 12:36:17 -06001429 /*
1430 * squash can now silently disable option_commit - this is not
1431 * a problem as it is only overriding the default, not a user
1432 * supplied option.
1433 */
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001434 option_commit = 0;
1435 }
1436
Vishal Verma1d14d0c2019-05-24 12:36:17 -06001437 if (option_commit < 0)
1438 option_commit = 1;
1439
Junio C Hamano00c7e7e2015-04-23 13:01:44 -07001440 if (!argc) {
1441 if (default_to_upstream)
1442 argc = setup_with_upstream(&argv);
1443 else
1444 die(_("No commit specified and merge.defaultToUpstream not set."));
1445 } else if (argc == 1 && !strcmp(argv[0], "-")) {
1446 argv[0] = "@{-1}";
Junio C Hamano4e8115f2011-04-07 15:57:57 -07001447 }
Junio C Hamano00c7e7e2015-04-23 13:01:44 -07001448
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001449 if (!argc)
1450 usage_with_options(builtin_merge_usage,
1451 builtin_merge_options);
1452
Junio C Hamano1faac1c2015-04-23 13:46:44 -07001453 if (!head_commit) {
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001454 /*
1455 * If the merged head is a valid one there is no reason
1456 * to forbid "git merge" into a branch yet to be born.
1457 * We do the same for "git pull".
1458 */
brian m. carlson52684312017-02-21 23:47:28 +00001459 struct object_id *remote_head_oid;
Paolo Bonzini4be636f2008-08-21 14:14:18 +02001460 if (squash)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001461 die(_("Squash commit into empty head not supported yet"));
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001462 if (fast_forward == FF_NO)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001463 die(_("Non-fast-forward commit does not make sense into "
1464 "an empty head"));
Junio C Hamano1cf32f42015-04-25 18:34:22 -07001465 remoteheads = collect_parents(head_commit, &head_subsumed,
1466 argc, argv, NULL);
Junio C Hamanob84e65d2016-03-21 12:01:43 -07001467 if (!remoteheads)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001468 die(_("%s - not something we can merge"), argv[0]);
Junio C Hamanoeaa4e592015-04-23 13:56:34 -07001469 if (remoteheads->next)
1470 die(_("Can merge only exactly one commit into empty head"));
Jeff King7488ba32018-11-06 02:51:15 -05001471
1472 if (verify_signatures)
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001473 verify_merge_signature(remoteheads->item, verbosity,
1474 check_trust_level);
Jeff King7488ba32018-11-06 02:51:15 -05001475
brian m. carlson52684312017-02-21 23:47:28 +00001476 remote_head_oid = &remoteheads->item->object.oid;
Ævar Arnfjörð Bjarmason9397f3c2022-10-30 12:42:59 +01001477 read_empty(remote_head_oid);
brian m. carlsonae077772017-10-15 22:06:51 +00001478 update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0,
1479 UPDATE_REFS_DIE_ON_ERR);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001480 goto done;
Junio C Hamano1faac1c2015-04-23 13:46:44 -07001481 }
1482
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001483 /*
Junio C Hamanob4391652015-03-25 22:00:48 -07001484 * All the rest are the commits being merged; prepare
1485 * the standard merge summary message to be appended
1486 * to the given message.
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001487 */
Junio C Hamanob4391652015-03-25 22:00:48 -07001488 remoteheads = collect_parents(head_commit, &head_subsumed,
1489 argc, argv, &merge_msg);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001490
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +10001491 if (!head_commit || !argc)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001492 usage_with_options(builtin_merge_usage,
1493 builtin_merge_options);
1494
Sebastian Götteefed0022013-03-31 18:02:24 +02001495 if (verify_signatures) {
1496 for (p = remoteheads; p; p = p->next) {
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001497 verify_merge_signature(p->item, verbosity,
1498 check_trust_level);
Sebastian Götteefed0022013-03-31 18:02:24 +02001499 }
1500 }
1501
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001502 strbuf_addstr(&buf, "merge");
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001503 for (p = remoteheads; p; p = p->next)
1504 strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001505 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1506 strbuf_reset(&buf);
1507
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001508 for (p = remoteheads; p; p = p->next) {
1509 struct commit *commit = p->item;
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001510 strbuf_addf(&buf, "GITHEAD_%s",
brian m. carlsonc368dde2016-06-24 23:09:22 +00001511 oid_to_hex(&commit->object.oid));
Junio C Hamanob5d887f2012-04-17 11:31:10 -07001512 setenv(buf.buf, merge_remote_util(commit)->name, 1);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001513 strbuf_reset(&buf);
Junio C Hamanoadcc94a2018-02-14 10:18:55 -08001514 if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit))
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001515 fast_forward = FF_NO;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001516 }
1517
Elijah Newren6a5fb962021-08-04 05:38:01 +00001518 if (!use_strategies && !pull_twohead &&
1519 remoteheads && !remoteheads->next) {
1520 char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1521 if (default_strategy)
1522 append_strategy(get_strategy(default_strategy));
1523 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001524 if (!use_strategies) {
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001525 if (!remoteheads)
1526 ; /* already up-to-date */
1527 else if (!remoteheads->next)
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001528 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1529 else
1530 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1531 }
1532
1533 for (i = 0; i < use_strategies_nr; i++) {
1534 if (use_strategies[i]->attr & NO_FAST_FORWARD)
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001535 fast_forward = FF_NO;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001536 if (use_strategies[i]->attr & NO_TRIVIAL)
1537 allow_trivial = 0;
1538 }
1539
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001540 if (!remoteheads)
1541 ; /* already up-to-date */
1542 else if (!remoteheads->next)
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +02001543 common = repo_get_merge_bases(the_repository, head_commit,
1544 remoteheads->item);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001545 else {
1546 struct commit_list *list = remoteheads;
Nguyễn Thái Ngọc Duy894642f2011-09-17 21:57:44 +10001547 commit_list_insert(head_commit, &list);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001548 common = get_octopus_merge_bases(list);
1549 free(list);
1550 }
1551
brian m. carlsonae077772017-10-15 22:06:51 +00001552 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1553 &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001554
Junio C Hamanoe379fdf2016-03-18 13:21:09 -07001555 if (remoteheads && !common) {
1556 /* No common ancestors found. */
1557 if (!allow_unrelated_histories)
1558 die(_("refusing to merge unrelated histories"));
1559 /* otherwise, we need a real merge. */
1560 } else if (!remoteheads ||
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001561 (!remoteheads->next && !common->next &&
1562 common->item == remoteheads->item)) {
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001563 /*
1564 * If head can reach all the merge then we are up to date.
1565 * but first the most common case of merging one remote.
1566 */
Josh Sorefad9322d2021-05-02 01:14:23 -04001567 finish_up_to_date();
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001568 goto done;
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001569 } else if (fast_forward != FF_NO && !remoteheads->next &&
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001570 !common->next &&
Jeff King4a7e27e2018-08-28 17:22:40 -04001571 oideq(&common->item->object.oid, &head_commit->object.oid)) {
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001572 /* Again the most common case of merging one remote. */
Ævar Arnfjörð Bjarmason345e2162023-02-07 00:07:48 +01001573 const char *msg = have_message ?
1574 "Fast-forward (no commit created; -m option ignored)" :
1575 "Fast-forward";
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001576 struct commit *commit;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001577
Jeff Kingd59f7652015-09-24 17:08:03 -04001578 if (verbosity >= 0) {
Jeff Kingef2ed502016-10-20 02:19:19 -04001579 printf(_("Updating %s..%s\n"),
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001580 repo_find_unique_abbrev(the_repository, &head_commit->object.oid,
1581 DEFAULT_ABBREV),
1582 repo_find_unique_abbrev(the_repository, &remoteheads->item->object.oid,
1583 DEFAULT_ABBREV));
Jeff Kingd59f7652015-09-24 17:08:03 -04001584 }
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001585 commit = remoteheads->item;
Junio C Hamanob7f7c072011-12-09 13:37:14 -08001586 if (!commit) {
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001587 ret = 1;
1588 goto done;
1589 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001590
Denton Liua03b5552020-04-07 10:28:07 -04001591 if (autostash)
1592 create_autostash(the_repository,
Phillip Woodb7de1532022-01-26 13:05:44 +00001593 git_path_merge_autostash(the_repository));
Nguyễn Thái Ngọc Duy7e196c32018-09-21 17:57:29 +02001594 if (checkout_fast_forward(the_repository,
1595 &head_commit->object.oid,
brian m. carlsonf06e90d2017-05-06 22:10:33 +00001596 &commit->object.oid,
Nguyễn Thái Ngọc Duydb699a82012-10-26 22:53:49 +07001597 overwrite_ignore)) {
Philippe Blain12510bd2021-07-23 12:14:29 +00001598 apply_autostash(git_path_merge_autostash(the_repository));
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001599 ret = 1;
1600 goto done;
1601 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001602
Ævar Arnfjörð Bjarmason345e2162023-02-07 00:07:48 +01001603 finish(head_commit, remoteheads, &commit->object.oid, msg);
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +07001604 remove_merge_branch_state(the_repository);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001605 goto done;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001606 } else if (!remoteheads->next && common->next)
1607 ;
1608 /*
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03001609 * We are not doing octopus and not fast-forward. Need
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001610 * a real merge.
1611 */
1612 else if (!remoteheads->next && !common->next && option_commit) {
1613 /*
Felipe Contrerasa75d7b52009-10-24 11:31:32 +03001614 * We are not doing octopus, not fast-forward, and have
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001615 * only one common.
1616 */
Ævar Arnfjörð Bjarmason07047d62022-11-19 14:07:38 +01001617 refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL);
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001618 if (allow_trivial && fast_forward != FF_ONLY) {
Elijah Newrene4cdfe82022-07-23 01:53:13 +00001619 /*
1620 * Must first ensure that index matches HEAD before
1621 * attempting a trivial merge.
1622 */
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001623 struct tree *head_tree = repo_get_commit_tree(the_repository,
1624 head_commit);
Elijah Newrene4cdfe82022-07-23 01:53:13 +00001625 struct strbuf sb = STRBUF_INIT;
1626
1627 if (repo_index_has_changes(the_repository, head_tree,
1628 &sb)) {
1629 error(_("Your local changes to the following files would be overwritten by merge:\n %s"),
1630 sb.buf);
1631 strbuf_release(&sb);
Ævar Arnfjörð Bjarmason41211db2023-02-07 00:07:49 +01001632 ret = 2;
1633 goto done;
Elijah Newrene4cdfe82022-07-23 01:53:13 +00001634 }
1635
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001636 /* See if it is really trivial. */
Jeff Kingf9bc5732012-05-24 19:28:40 -04001637 git_committer_info(IDENT_STRICT);
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001638 printf(_("Trying really trivial in-index merge...\n"));
brian m. carlson52684312017-02-21 23:47:28 +00001639 if (!read_tree_trivial(&common->item->object.oid,
1640 &head_commit->object.oid,
1641 &remoteheads->item->object.oid)) {
Junio C Hamano4c57bd22012-04-16 16:15:13 -07001642 ret = merge_trivial(head_commit, remoteheads);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001643 goto done;
1644 }
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001645 printf(_("Nope.\n"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001646 }
1647 } else {
1648 /*
1649 * An octopus. If we can reach all the remote we are up
1650 * to date.
1651 */
1652 int up_to_date = 1;
1653 struct commit_list *j;
1654
1655 for (j = remoteheads; j; j = j->next) {
1656 struct commit_list *common_one;
1657
1658 /*
1659 * Here we *have* to calculate the individual
1660 * merge_bases again, otherwise "git merge HEAD^
1661 * HEAD^^" would be missed.
1662 */
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +02001663 common_one = repo_get_merge_bases(the_repository,
1664 head_commit,
1665 j->item);
Jeff King9001dc22018-08-28 17:22:48 -04001666 if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) {
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001667 up_to_date = 0;
1668 break;
1669 }
1670 }
1671 if (up_to_date) {
Josh Sorefad9322d2021-05-02 01:14:23 -04001672 finish_up_to_date();
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001673 goto done;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001674 }
1675 }
1676
Miklos Vajnaa54841e2013-07-02 16:47:57 +02001677 if (fast_forward == FF_ONLY)
Alex Henrie3d5fc242021-07-21 01:42:19 +00001678 die_ff_impossible();
Björn Gustavsson13474832009-10-29 23:08:31 +01001679
Denton Liua03b5552020-04-07 10:28:07 -04001680 if (autostash)
1681 create_autostash(the_repository,
Phillip Woodb7de1532022-01-26 13:05:44 +00001682 git_path_merge_autostash(the_repository));
Denton Liua03b5552020-04-07 10:28:07 -04001683
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001684 /* We are going to make a new commit. */
Jeff Kingf9bc5732012-05-24 19:28:40 -04001685 git_committer_info(IDENT_STRICT);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001686
1687 /*
1688 * At this point, we need a real merge. No matter what strategy
1689 * we use, it would operate on the index, possibly affecting the
1690 * working tree, and when resolved cleanly, have the desired
1691 * tree in the index -- this means that the index must be in
1692 * sync with the head commit. The strategies are responsible
1693 * to ensure this.
Elijah Newren034195e2022-07-23 01:53:17 +00001694 *
1695 * Stash away the local changes so that we can try more than one
1696 * and/or recover from merge strategies bailing while leaving the
1697 * index and working tree polluted.
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001698 */
Elijah Newren034195e2022-07-23 01:53:17 +00001699 if (save_state(&stash))
brian m. carlson52684312017-02-21 23:47:28 +00001700 oidclr(&stash);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001701
Elijah Newren5b1d30c2022-08-23 02:42:20 +00001702 for (i = 0; i < use_strategies_nr; i++) {
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -06001703 int ret, cnt;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001704 if (i) {
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001705 printf(_("Rewinding the tree to pristine...\n"));
brian m. carlson52684312017-02-21 23:47:28 +00001706 restore_state(&head_commit->object.oid, &stash);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001707 }
1708 if (use_strategies_nr != 1)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001709 printf(_("Trying merge strategy %s...\n"),
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001710 use_strategies[i]->name);
1711 /*
1712 * Remember which strategy left the state in the working
1713 * tree.
1714 */
1715 wt_strategy = use_strategies[i]->name;
1716
Elijah Newrenae15fd42022-08-23 02:42:21 +00001717 ret = try_merge_strategy(wt_strategy,
Junio C Hamano4c57bd22012-04-16 16:15:13 -07001718 common, remoteheads,
Junio C Hamanob4391652015-03-25 22:00:48 -07001719 head_commit);
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -06001720 /*
1721 * The backend exits with 1 when conflicts are
1722 * left to be resolved, with 2 when it does not
1723 * handle the given merge at all.
1724 */
1725 if (ret < 2) {
1726 if (!ret) {
Elijah Newren5b1d30c2022-08-23 02:42:20 +00001727 /*
1728 * This strategy worked; no point in trying
1729 * another.
1730 */
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -06001731 merge_was_ok = 1;
Elijah Newrenae15fd42022-08-23 02:42:21 +00001732 best_strategy = wt_strategy;
Elijah Newren5b1d30c2022-08-23 02:42:20 +00001733 break;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001734 }
Andrew Ng87776162020-05-19 14:05:35 +01001735 cnt = (use_strategies_nr > 1) ? evaluate_result() : 0;
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -06001736 if (best_cnt <= 0 || cnt <= best_cnt) {
Elijah Newrenae15fd42022-08-23 02:42:21 +00001737 best_strategy = wt_strategy;
Edmundo Carmona Antoranzf00abe62019-07-08 21:15:59 -06001738 best_cnt = cnt;
1739 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001740 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001741 }
1742
1743 /*
1744 * If we have a resulting tree, that means the strategy module
1745 * auto resolved the merge cleanly.
1746 */
Elijah Newren5b1d30c2022-08-23 02:42:20 +00001747 if (merge_was_ok && option_commit) {
1748 automerge_was_ok = 1;
Junio C Hamanoe78cbf82012-04-17 12:22:26 -07001749 ret = finish_automerge(head_commit, head_subsumed,
1750 common, remoteheads,
brian m. carlson52684312017-02-21 23:47:28 +00001751 &result_tree, wt_strategy);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001752 goto done;
1753 }
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001754
1755 /*
1756 * Pick the result from the best strategy and have the user fix
1757 * it up.
1758 */
1759 if (!best_strategy) {
brian m. carlson52684312017-02-21 23:47:28 +00001760 restore_state(&head_commit->object.oid, &stash);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001761 if (use_strategies_nr > 1)
1762 fprintf(stderr,
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001763 _("No merge strategy handled the merge.\n"));
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001764 else
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001765 fprintf(stderr, _("Merge with strategy %s failed.\n"),
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001766 use_strategies[0]->name);
Philippe Blaine0826312021-07-23 12:14:30 +00001767 apply_autostash(git_path_merge_autostash(the_repository));
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001768 ret = 2;
1769 goto done;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001770 } else if (best_strategy == wt_strategy)
1771 ; /* We already have its result in the working tree. */
1772 else {
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001773 printf(_("Rewinding the tree to pristine...\n"));
brian m. carlson52684312017-02-21 23:47:28 +00001774 restore_state(&head_commit->object.oid, &stash);
Philippe Blain9938f302021-07-23 12:14:27 +00001775 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001776 best_strategy);
Junio C Hamano4c57bd22012-04-16 16:15:13 -07001777 try_merge_strategy(best_strategy, common, remoteheads,
Junio C Hamanob4391652015-03-25 22:00:48 -07001778 head_commit);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001779 }
1780
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001781 if (squash) {
Junio C Hamano4c57bd22012-04-16 16:15:13 -07001782 finish(head_commit, remoteheads, NULL, NULL);
Derrick Stoleeb23ea972020-04-16 20:14:03 +00001783
1784 git_test_write_commit_graph_or_die();
1785 } else
Junio C Hamano4c57bd22012-04-16 16:15:13 -07001786 write_merge_state(remoteheads);
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001787
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001788 if (merge_was_ok)
Ævar Arnfjörð Bjarmasonbacec472011-02-22 23:41:59 +00001789 fprintf(stderr, _("Automatic merge went well; "
1790 "stopped before committing as requested\n"));
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001791 else
Junio C Hamano08e3ce52014-10-24 11:27:22 -07001792 ret = suggest_conflicts();
Elijah Newrend3a92952022-08-23 02:42:19 +00001793 if (autostash)
1794 printf(_("When finished, apply stashed changes with `git stash pop`\n"));
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001795
1796done:
Elijah Newren6046f7a2022-01-20 07:47:15 +00001797 if (!automerge_was_ok) {
1798 free_commit_list(common);
1799 free_commit_list(remoteheads);
1800 }
Ævar Arnfjörð Bjarmason465028e2021-10-07 12:01:37 +02001801 strbuf_release(&buf);
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +07001802 free(branch_to_free);
Ævar Arnfjörð Bjarmasonab2cf372022-11-08 19:17:38 +01001803 discard_index(&the_index);
Nguyễn Thái Ngọc Duyd5a35c12011-11-13 17:22:15 +07001804 return ret;
Miklos Vajna1c7b76b2008-07-07 19:24:20 +02001805}