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