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