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