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