]>
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 | ||
dfd0a893 | 9 | #define USE_THE_INDEX_VARIABLE |
bc5c5ec0 | 10 | #include "builtin.h" |
0b027f6c | 11 | #include "abspath.h" |
6c6ddf92 | 12 | #include "advice.h" |
b2141fc1 | 13 | #include "config.h" |
4e120823 | 14 | #include "editor.h" |
7ee24e18 | 15 | #include "environment.h" |
f394e093 | 16 | #include "gettext.h" |
41771fa4 | 17 | #include "hex.h" |
dabab1d6 | 18 | #include "object-name.h" |
1c7b76be | 19 | #include "parse-options.h" |
697cc8ef | 20 | #include "lockfile.h" |
1c7b76be | 21 | #include "run-command.h" |
5e3aba33 | 22 | #include "hook.h" |
1c7b76be | 23 | #include "diff.h" |
09322b1d | 24 | #include "diff-merges.h" |
1c7b76be | 25 | #include "refs.h" |
ec0cb496 | 26 | #include "refspec.h" |
1c7b76be MV |
27 | #include "commit.h" |
28 | #include "diffcore.h" | |
c339932b | 29 | #include "path.h" |
1c7b76be MV |
30 | #include "revision.h" |
31 | #include "unpack-trees.h" | |
32 | #include "cache-tree.h" | |
33 | #include "dir.h" | |
34 | #include "utf8.h" | |
35 | #include "log-tree.h" | |
36 | #include "color.h" | |
fcab40a3 | 37 | #include "rerere.h" |
87091b49 | 38 | #include "help.h" |
750324dd | 39 | #include "merge.h" |
18668f53 | 40 | #include "merge-recursive.h" |
14c4586c | 41 | #include "merge-ort-wrappers.h" |
cfc5789a | 42 | #include "resolve-undo.h" |
93e535a5 | 43 | #include "remote.h" |
898eacd8 | 44 | #include "fmt-merge-msg.h" |
ba3c69a9 | 45 | #include "gpg-interface.h" |
75c961b7 | 46 | #include "sequencer.h" |
02a8cfa4 | 47 | #include "string-list.h" |
3836d88a | 48 | #include "packfile.h" |
adcc94a0 | 49 | #include "tag.h" |
65b5f948 | 50 | #include "alias.h" |
b6433555 | 51 | #include "branch.h" |
64043556 | 52 | #include "commit-reach.h" |
d540b70c | 53 | #include "wt-status.h" |
b23ea979 | 54 | #include "commit-graph.h" |
1c7b76be MV |
55 | |
56 | #define DEFAULT_TWOHEAD (1<<0) | |
57 | #define DEFAULT_OCTOPUS (1<<1) | |
58 | #define NO_FAST_FORWARD (1<<2) | |
59 | #define NO_TRIVIAL (1<<3) | |
60 | ||
61 | struct strategy { | |
62 | const char *name; | |
63 | unsigned attr; | |
64 | }; | |
65 | ||
66 | static const char * const builtin_merge_usage[] = { | |
9c9b4f2f | 67 | N_("git merge [<options>] [<commit>...]"), |
a30e43f6 AH |
68 | "git merge --abort", |
69 | "git merge --continue", | |
1c7b76be MV |
70 | NULL |
71 | }; | |
72 | ||
898eacd8 | 73 | static int show_diffstat = 1, shortlog_len = -1, squash; |
1d14d0c9 | 74 | static int option_commit = -1; |
a54841e9 | 75 | static int option_edit = -1; |
efed0022 | 76 | static int allow_trivial = 1, have_message, verify_signatures; |
54887b46 | 77 | static int check_trust_level = 1; |
c1d7036b | 78 | static int overwrite_ignore = 1; |
2c47789d | 79 | static struct strbuf merge_msg = STRBUF_INIT; |
1c7b76be MV |
80 | static struct strategy **use_strategies; |
81 | static size_t use_strategies_nr, use_strategies_alloc; | |
8cc5b290 AP |
82 | static const char **xopts; |
83 | static size_t xopts_nr, xopts_alloc; | |
1c7b76be | 84 | static const char *branch; |
0d8fc3ef | 85 | static char *branch_mergeoptions; |
7f87aff2 | 86 | static int verbosity; |
cb6020bb | 87 | static int allow_rerere_auto; |
35d2fffd | 88 | static int abort_current_merge; |
f3f8311e | 89 | static int quit_current_merge; |
367ff694 | 90 | static int continue_current_merge; |
e379fdf3 | 91 | static int allow_unrelated_histories; |
99bfc669 | 92 | static int show_progress = -1; |
a01f7f2b | 93 | static int default_to_upstream = 1; |
14d01b4f | 94 | static int signoff; |
ba3c69a9 | 95 | static const char *sign_commit; |
a03b5553 | 96 | static int autostash; |
a1f3dd7e | 97 | static int no_verify; |
bd2bc942 | 98 | static char *into_name; |
1c7b76be MV |
99 | |
100 | static struct strategy all_strategy[] = { | |
6a5fb966 | 101 | { "recursive", NO_TRIVIAL }, |
1c7b76be | 102 | { "octopus", DEFAULT_OCTOPUS }, |
6a5fb966 | 103 | { "ort", DEFAULT_TWOHEAD | NO_TRIVIAL }, |
1c7b76be | 104 | { "resolve", 0 }, |
1c7b76be MV |
105 | { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, |
106 | { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, | |
107 | }; | |
108 | ||
109 | static const char *pull_twohead, *pull_octopus; | |
110 | ||
a54841e9 MV |
111 | enum ff_type { |
112 | FF_NO, | |
113 | FF_ALLOW, | |
114 | FF_ONLY | |
115 | }; | |
116 | ||
117 | static enum ff_type fast_forward = FF_ALLOW; | |
118 | ||
d540b70c DL |
119 | static const char *cleanup_arg; |
120 | static enum commit_msg_cleanup_mode cleanup_mode; | |
121 | ||
1c7b76be MV |
122 | static int option_parse_message(const struct option *opt, |
123 | const char *arg, int unset) | |
124 | { | |
125 | struct strbuf *buf = opt->value; | |
126 | ||
127 | if (unset) | |
128 | strbuf_setlen(buf, 0); | |
74f5b7fb | 129 | else if (arg) { |
ce9d823b | 130 | strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); |
1c7b76be | 131 | have_message = 1; |
74f5b7fb | 132 | } else |
bacec478 | 133 | return error(_("switch `m' requires a value")); |
1c7b76be MV |
134 | return 0; |
135 | } | |
136 | ||
f41179f1 NTND |
137 | static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx, |
138 | const struct option *opt, | |
3ebbe289 | 139 | const char *arg_not_used, |
f41179f1 | 140 | int unset) |
920f22e6 JS |
141 | { |
142 | struct strbuf *buf = opt->value; | |
143 | const char *arg; | |
144 | ||
3ebbe289 | 145 | BUG_ON_OPT_ARG(arg_not_used); |
920f22e6 JS |
146 | if (unset) |
147 | BUG("-F cannot be negated"); | |
148 | ||
149 | if (ctx->opt) { | |
150 | arg = ctx->opt; | |
151 | ctx->opt = NULL; | |
152 | } else if (ctx->argc > 1) { | |
153 | ctx->argc--; | |
154 | arg = *++ctx->argv; | |
155 | } else | |
9440b831 | 156 | return error(_("option `%s' requires a value"), opt->long_name); |
920f22e6 JS |
157 | |
158 | if (buf->len) | |
159 | strbuf_addch(buf, '\n'); | |
160 | if (ctx->prefix && !is_absolute_path(arg)) | |
161 | arg = prefix_filename(ctx->prefix, arg); | |
162 | if (strbuf_read_file(buf, arg, 0) < 0) | |
163 | return error(_("could not read file '%s'"), arg); | |
164 | have_message = 1; | |
165 | ||
166 | return 0; | |
167 | } | |
168 | ||
1c7b76be MV |
169 | static struct strategy *get_strategy(const char *name) |
170 | { | |
171 | int i; | |
87091b49 MV |
172 | struct strategy *ret; |
173 | static struct cmdnames main_cmds, other_cmds; | |
e321180e | 174 | static int loaded; |
14c4586c | 175 | char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM"); |
1c7b76be MV |
176 | |
177 | if (!name) | |
178 | return NULL; | |
179 | ||
14c4586c EN |
180 | if (default_strategy && |
181 | !strcmp(default_strategy, "ort") && | |
182 | !strcmp(name, "recursive")) { | |
183 | name = "ort"; | |
184 | } | |
185 | ||
1c7b76be MV |
186 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) |
187 | if (!strcmp(name, all_strategy[i].name)) | |
188 | return &all_strategy[i]; | |
1719b5e4 | 189 | |
e321180e | 190 | if (!loaded) { |
87091b49 | 191 | struct cmdnames not_strategies; |
e321180e | 192 | loaded = 1; |
87091b49 | 193 | |
87091b49 | 194 | memset(¬_strategies, 0, sizeof(struct cmdnames)); |
e321180e | 195 | load_command_list("git-merge-", &main_cmds, &other_cmds); |
87091b49 MV |
196 | for (i = 0; i < main_cmds.cnt; i++) { |
197 | int j, found = 0; | |
198 | struct cmdname *ent = main_cmds.names[i]; | |
0c75692e | 199 | for (j = 0; !found && j < ARRAY_SIZE(all_strategy); j++) |
87091b49 MV |
200 | if (!strncmp(ent->name, all_strategy[j].name, ent->len) |
201 | && !all_strategy[j].name[ent->len]) | |
202 | found = 1; | |
203 | if (!found) | |
204 | add_cmdname(¬_strategies, ent->name, ent->len); | |
87091b49 | 205 | } |
ed874656 | 206 | exclude_cmds(&main_cmds, ¬_strategies); |
87091b49 MV |
207 | } |
208 | if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { | |
bacec478 ÆAB |
209 | fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); |
210 | fprintf(stderr, _("Available strategies are:")); | |
131f9a10 JH |
211 | for (i = 0; i < main_cmds.cnt; i++) |
212 | fprintf(stderr, " %s", main_cmds.names[i]->name); | |
213 | fprintf(stderr, ".\n"); | |
214 | if (other_cmds.cnt) { | |
bacec478 | 215 | fprintf(stderr, _("Available custom strategies are:")); |
131f9a10 JH |
216 | for (i = 0; i < other_cmds.cnt; i++) |
217 | fprintf(stderr, " %s", other_cmds.names[i]->name); | |
218 | fprintf(stderr, ".\n"); | |
219 | } | |
87091b49 MV |
220 | exit(1); |
221 | } | |
222 | ||
ca56dadb | 223 | CALLOC_ARRAY(ret, 1); |
87091b49 | 224 | ret->name = xstrdup(name); |
52b48ef1 | 225 | ret->attr = NO_TRIVIAL; |
87091b49 | 226 | return ret; |
1c7b76be MV |
227 | } |
228 | ||
229 | static void append_strategy(struct strategy *s) | |
230 | { | |
231 | ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); | |
232 | use_strategies[use_strategies_nr++] = s; | |
233 | } | |
234 | ||
235 | static int option_parse_strategy(const struct option *opt, | |
236 | const char *name, int unset) | |
237 | { | |
1c7b76be MV |
238 | if (unset) |
239 | return 0; | |
240 | ||
1719b5e4 | 241 | append_strategy(get_strategy(name)); |
1c7b76be MV |
242 | return 0; |
243 | } | |
244 | ||
8cc5b290 AP |
245 | static int option_parse_x(const struct option *opt, |
246 | const char *arg, int unset) | |
247 | { | |
248 | if (unset) | |
249 | return 0; | |
250 | ||
251 | ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc); | |
252 | xopts[xopts_nr++] = xstrdup(arg); | |
253 | return 0; | |
254 | } | |
255 | ||
1c7b76be MV |
256 | static int option_parse_n(const struct option *opt, |
257 | const char *arg, int unset) | |
258 | { | |
517fe807 | 259 | BUG_ON_OPT_ARG(arg); |
1c7b76be MV |
260 | show_diffstat = unset; |
261 | return 0; | |
262 | } | |
263 | ||
264 | static struct option builtin_merge_options[] = { | |
203c8533 | 265 | OPT_CALLBACK_F('n', NULL, NULL, NULL, |
962e6295 | 266 | N_("do not show a diffstat at the end of the merge"), |
203c8533 | 267 | PARSE_OPT_NOARG, option_parse_n), |
d5d09d47 | 268 | OPT_BOOL(0, "stat", &show_diffstat, |
962e6295 | 269 | N_("show a diffstat at the end of the merge")), |
d5d09d47 | 270 | OPT_BOOL(0, "summary", &show_diffstat, N_("(synonym to --stat)")), |
962e6295 NTND |
271 | { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"), |
272 | N_("add (at most <n>) entries from shortlog to merge commit message"), | |
96e9420c | 273 | PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, |
d5d09d47 | 274 | OPT_BOOL(0, "squash", &squash, |
962e6295 | 275 | N_("create a single commit instead of doing a merge")), |
d5d09d47 | 276 | OPT_BOOL(0, "commit", &option_commit, |
962e6295 | 277 | N_("perform a commit if the merge succeeds (default)")), |
f8246281 | 278 | OPT_BOOL('e', "edit", &option_edit, |
962e6295 | 279 | N_("edit message before committing")), |
d540b70c | 280 | OPT_CLEANUP(&cleanup_arg), |
a54841e9 | 281 | OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW), |
3e4a67b4 NTND |
282 | OPT_SET_INT_F(0, "ff-only", &fast_forward, |
283 | N_("abort if fast-forward is not possible"), | |
284 | FF_ONLY, PARSE_OPT_NONEG), | |
cb6020bb | 285 | OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), |
efed0022 | 286 | OPT_BOOL(0, "verify-signatures", &verify_signatures, |
c8bb9d2e | 287 | N_("verify that the named commit has a valid GPG signature")), |
962e6295 NTND |
288 | OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"), |
289 | N_("merge strategy to use"), option_parse_strategy), | |
290 | OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"), | |
291 | N_("option for selected merge strategy"), option_parse_x), | |
292 | OPT_CALLBACK('m', "message", &merge_msg, N_("message"), | |
293 | N_("merge commit message (for a non-fast-forward merge)"), | |
1c7b76be | 294 | option_parse_message), |
920f22e6 JS |
295 | { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"), |
296 | N_("read message from file"), PARSE_OPT_NONEG, | |
bf3ff338 | 297 | NULL, 0, option_read_message }, |
bd2bc942 JH |
298 | OPT_STRING(0, "into-name", &into_name, N_("name"), |
299 | N_("use <name> instead of the real target")), | |
7f87aff2 | 300 | OPT__VERBOSITY(&verbosity), |
d5d09d47 | 301 | OPT_BOOL(0, "abort", &abort_current_merge, |
962e6295 | 302 | N_("abort the current in-progress merge")), |
f3f8311e NTND |
303 | OPT_BOOL(0, "quit", &quit_current_merge, |
304 | N_("--abort but leave index and working tree alone")), | |
367ff694 CP |
305 | OPT_BOOL(0, "continue", &continue_current_merge, |
306 | N_("continue the current in-progress merge")), | |
e379fdf3 JH |
307 | OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories, |
308 | N_("allow merging unrelated histories")), | |
962e6295 | 309 | OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1), |
e703d711 | 310 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"), |
962e6295 | 311 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
a03b5553 | 312 | OPT_AUTOSTASH(&autostash), |
d5d09d47 | 313 | OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), |
3abd4a67 | 314 | OPT_BOOL(0, "signoff", &signoff, N_("add a Signed-off-by trailer")), |
bc40ce4d | 315 | OPT_BOOL(0, "no-verify", &no_verify, N_("bypass pre-merge-commit and commit-msg hooks")), |
1c7b76be MV |
316 | OPT_END() |
317 | }; | |
318 | ||
52684310 | 319 | static int save_state(struct object_id *stash) |
1c7b76be MV |
320 | { |
321 | int len; | |
d3180279 | 322 | struct child_process cp = CHILD_PROCESS_INIT; |
1c7b76be | 323 | struct strbuf buffer = STRBUF_INIT; |
1369f147 EN |
324 | struct lock_file lock_file = LOCK_INIT; |
325 | int fd; | |
150888e2 | 326 | int rc = -1; |
1c7b76be | 327 | |
1369f147 | 328 | fd = repo_hold_locked_index(the_repository, &lock_file, 0); |
07047d68 | 329 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
1369f147 EN |
330 | if (0 <= fd) |
331 | repo_update_index_if_able(the_repository, &lock_file); | |
332 | rollback_lock_file(&lock_file); | |
333 | ||
2b709893 | 334 | strvec_pushl(&cp.args, "stash", "create", NULL); |
1c7b76be MV |
335 | cp.out = -1; |
336 | cp.git_cmd = 1; | |
337 | ||
338 | if (start_command(&cp)) | |
bacec478 | 339 | die(_("could not run stash.")); |
1c7b76be MV |
340 | len = strbuf_read(&buffer, cp.out, 1024); |
341 | close(cp.out); | |
342 | ||
343 | if (finish_command(&cp) || len < 0) | |
bacec478 | 344 | die(_("stash failed")); |
b4fd9406 | 345 | else if (!len) /* no changes */ |
150888e2 | 346 | goto out; |
1c7b76be | 347 | strbuf_setlen(&buffer, buffer.len-1); |
d850b7a5 | 348 | if (repo_get_oid(the_repository, buffer.buf, stash)) |
bacec478 | 349 | die(_("not a valid object: %s"), buffer.buf); |
150888e2 RS |
350 | rc = 0; |
351 | out: | |
352 | strbuf_release(&buffer); | |
353 | return rc; | |
1c7b76be MV |
354 | } |
355 | ||
9397f3cf | 356 | static void read_empty(const struct object_id *oid) |
172b6428 | 357 | { |
4120294c | 358 | struct child_process cmd = CHILD_PROCESS_INIT; |
172b6428 | 359 | |
4120294c RS |
360 | strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(), |
361 | oid_to_hex(oid), NULL); | |
362 | cmd.git_cmd = 1; | |
172b6428 | 363 | |
4120294c | 364 | if (run_command(&cmd)) |
bacec478 | 365 | die(_("read-tree failed")); |
172b6428 CB |
366 | } |
367 | ||
9397f3cf | 368 | static void reset_hard(const struct object_id *oid) |
1c7b76be | 369 | { |
4120294c | 370 | struct child_process cmd = CHILD_PROCESS_INIT; |
1c7b76be | 371 | |
4120294c RS |
372 | strvec_pushl(&cmd.args, "read-tree", "-v", "--reset", "-u", |
373 | oid_to_hex(oid), NULL); | |
374 | cmd.git_cmd = 1; | |
1c7b76be | 375 | |
4120294c | 376 | if (run_command(&cmd)) |
bacec478 | 377 | die(_("read-tree failed")); |
1c7b76be MV |
378 | } |
379 | ||
52684310 | 380 | static void restore_state(const struct object_id *head, |
381 | const struct object_id *stash) | |
1c7b76be | 382 | { |
0e906739 | 383 | struct child_process cmd = CHILD_PROCESS_INIT; |
1c7b76be | 384 | |
9397f3cf | 385 | reset_hard(head); |
1c7b76be | 386 | |
c23fc075 EN |
387 | if (is_null_oid(stash)) |
388 | goto refresh_cache; | |
389 | ||
0e906739 RS |
390 | strvec_pushl(&cmd.args, "stash", "apply", "--index", "--quiet", NULL); |
391 | strvec_push(&cmd.args, oid_to_hex(stash)); | |
1c7b76be MV |
392 | |
393 | /* | |
394 | * It is OK to ignore error here, for example when there was | |
395 | * nothing to restore. | |
396 | */ | |
0e906739 RS |
397 | cmd.git_cmd = 1; |
398 | run_command(&cmd); | |
1c7b76be | 399 | |
c23fc075 | 400 | refresh_cache: |
dfd0a893 ÆAB |
401 | discard_index(&the_index); |
402 | if (repo_read_index(the_repository) < 0) | |
c23fc075 | 403 | die(_("could not read index")); |
1c7b76be MV |
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 | |
d3a9295a EN |
504 | if (new_head) |
505 | apply_autostash(git_path_merge_autostash(the_repository)); | |
1c7b76be MV |
506 | strbuf_release(&reflog_message); |
507 | } | |
508 | ||
509 | /* Get the name for the merge commit's message. */ | |
510 | static void merge_name(const char *remote, struct strbuf *msg) | |
511 | { | |
ae8e4c9c | 512 | struct commit *remote_head; |
52684310 | 513 | struct object_id branch_head; |
c9717ee9 | 514 | struct strbuf bname = STRBUF_INIT; |
e2e5ac23 | 515 | struct merge_remote_desc *desc; |
1c7b76be | 516 | const char *ptr; |
8c05e42c | 517 | char *found_ref = NULL; |
1c7b76be MV |
518 | int len, early; |
519 | ||
0e9f62da | 520 | strbuf_branchname(&bname, remote, 0); |
a552de75 | 521 | remote = bname.buf; |
c9717ee9 | 522 | |
52684310 | 523 | oidclr(&branch_head); |
ae8e4c9c | 524 | remote_head = get_merge_parent(remote); |
1c7b76be | 525 | if (!remote_head) |
bacec478 | 526 | die(_("'%s' does not point to a commit"), remote); |
1c7b76be | 527 | |
12cb1c10 ÆAB |
528 | if (repo_dwim_ref(the_repository, remote, strlen(remote), &branch_head, |
529 | &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 JH |
670 | status = fmt_merge_msg_config(k, v, cb); |
671 | if (status) | |
672 | return status; | |
1c7b76be MV |
673 | return git_diff_ui_config(k, v, cb); |
674 | } | |
675 | ||
52684310 | 676 | static int read_tree_trivial(struct object_id *common, struct object_id *head, |
677 | struct object_id *one) | |
1c7b76be MV |
678 | { |
679 | int i, nr_trees = 0; | |
680 | struct tree *trees[MAX_UNPACK_TREES]; | |
681 | struct tree_desc t[MAX_UNPACK_TREES]; | |
682 | struct unpack_trees_options opts; | |
683 | ||
684 | memset(&opts, 0, sizeof(opts)); | |
685 | opts.head_idx = 2; | |
686 | opts.src_index = &the_index; | |
687 | opts.dst_index = &the_index; | |
688 | opts.update = 1; | |
689 | opts.verbose_update = 1; | |
690 | opts.trivial_merges_only = 1; | |
691 | opts.merge = 1; | |
1b5f3733 | 692 | opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ |
a9dbc179 | 693 | trees[nr_trees] = parse_tree_indirect(common); |
1c7b76be MV |
694 | if (!trees[nr_trees++]) |
695 | return -1; | |
a9dbc179 | 696 | trees[nr_trees] = parse_tree_indirect(head); |
1c7b76be MV |
697 | if (!trees[nr_trees++]) |
698 | return -1; | |
a9dbc179 | 699 | trees[nr_trees] = parse_tree_indirect(one); |
1c7b76be MV |
700 | if (!trees[nr_trees++]) |
701 | return -1; | |
702 | opts.fn = threeway_merge; | |
dc594180 | 703 | cache_tree_free(&the_index.cache_tree); |
1c7b76be MV |
704 | for (i = 0; i < nr_trees; i++) { |
705 | parse_tree(trees[i]); | |
706 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); | |
707 | } | |
708 | if (unpack_trees(nr_trees, t, &opts)) | |
709 | return -1; | |
710 | return 0; | |
711 | } | |
712 | ||
52684310 | 713 | static void write_tree_trivial(struct object_id *oid) |
1c7b76be | 714 | { |
99370863 | 715 | if (write_index_as_tree(oid, &the_index, get_index_file(), 0, NULL)) |
bacec478 | 716 | die(_("git write-tree failed to write a tree")); |
1c7b76be MV |
717 | } |
718 | ||
3f9083cd | 719 | static int try_merge_strategy(const char *strategy, struct commit_list *common, |
4c57bd27 | 720 | struct commit_list *remoteheads, |
b4391657 | 721 | struct commit *head) |
3f9083cd | 722 | { |
b4391657 | 723 | const char *head_arg = "HEAD"; |
668f26ff | 724 | |
07047d68 ÆAB |
725 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, |
726 | SKIP_IF_UNCHANGED, 0, NULL, NULL, | |
727 | NULL) < 0) | |
bacec478 | 728 | return error(_("Unable to write index.")); |
1c7b76be | 729 | |
14c4586c EN |
730 | if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") || |
731 | !strcmp(strategy, "ort")) { | |
e080b345 | 732 | struct lock_file lock = LOCK_INIT; |
3f9083cd | 733 | int clean, x; |
18668f53 | 734 | struct commit *result; |
18668f53 MV |
735 | struct commit_list *reversed = NULL; |
736 | struct merge_options o; | |
3f9083cd | 737 | struct commit_list *j; |
18668f53 MV |
738 | |
739 | if (remoteheads->next) { | |
bacec478 | 740 | error(_("Not handling anything other than two heads merge.")); |
18668f53 MV |
741 | return 2; |
742 | } | |
743 | ||
0d6caa2d | 744 | init_merge_options(&o, the_repository); |
18668f53 | 745 | if (!strcmp(strategy, "subtree")) |
85e51b78 | 746 | o.subtree_shift = ""; |
8cc5b290 | 747 | |
99bfc669 JK |
748 | o.show_rename_progress = |
749 | show_progress == -1 ? isatty(2) : show_progress; | |
7610fa57 | 750 | |
635a7bb1 JN |
751 | for (x = 0; x < xopts_nr; x++) |
752 | if (parse_merge_opt(&o, xopts[x])) | |
81483fe6 | 753 | die(_("unknown strategy option: -X%s"), xopts[x]); |
18668f53 MV |
754 | |
755 | o.branch1 = head_arg; | |
ae8e4c9c | 756 | o.branch2 = merge_remote_util(remoteheads->item)->name; |
18668f53 MV |
757 | |
758 | for (j = common; j; j = j->next) | |
759 | commit_list_insert(j->item, &reversed); | |
760 | ||
07047d68 ÆAB |
761 | repo_hold_locked_index(the_repository, &lock, |
762 | 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, | |
61bdc7c5 | 785 | struct diff_options *opt UNUSED, void *data) |
1c7b76be MV |
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 | ||
dc594180 ÆAB |
796 | for (i = 0; i < the_index.cache_nr; i++) |
797 | if (ce_stage(the_index.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) |
07047d68 | 871 | discard_index(&the_index); |
9f6e63b9 | 872 | } |
07047d68 | 873 | read_index_from(&the_index, index_file, get_git_dir()); |
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 | |
07047d68 ÆAB |
922 | if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, |
923 | SKIP_IF_UNCHANGED, 0, NULL, NULL, | |
924 | NULL) < 0) | |
40d71940 | 925 | return error(_("Unable to write index.")); |
1c7b76be | 926 | |
52684310 | 927 | write_tree_trivial(&result_tree); |
157efde1 | 928 | printf(_("Wonderful.\n")); |
910a09a7 RS |
929 | pptr = commit_list_append(head, pptr); |
930 | pptr = commit_list_append(remoteheads->item, pptr); | |
4c57bd27 | 931 | prepare_to_commit(remoteheads); |
5078f344 PO |
932 | if (commit_tree(merge_msg.buf, merge_msg.len, &result_tree, parents, |
933 | &result_commit, NULL, sign_commit)) | |
6b3c4c05 | 934 | die(_("failed to write commit object")); |
52684310 | 935 | finish(head, remoteheads, &result_commit, "In-index merge"); |
b6433555 | 936 | remove_merge_branch_state(the_repository); |
1c7b76be MV |
937 | return 0; |
938 | } | |
939 | ||
894642f6 | 940 | static int finish_automerge(struct commit *head, |
e78cbf8c | 941 | int head_subsumed, |
894642f6 | 942 | struct commit_list *common, |
4c57bd27 | 943 | struct commit_list *remoteheads, |
52684310 | 944 | struct object_id *result_tree, |
1c7b76be MV |
945 | const char *wt_strategy) |
946 | { | |
e78cbf8c | 947 | struct commit_list *parents = NULL; |
1c7b76be | 948 | struct strbuf buf = STRBUF_INIT; |
52684310 | 949 | struct object_id result_commit; |
1c7b76be | 950 | |
f00abe6a | 951 | write_tree_trivial(result_tree); |
1c7b76be | 952 | free_commit_list(common); |
e78cbf8c | 953 | parents = remoteheads; |
a54841e9 | 954 | if (!head_subsumed || fast_forward == FF_NO) |
894642f6 | 955 | commit_list_insert(head, &parents); |
4c57bd27 | 956 | prepare_to_commit(remoteheads); |
5078f344 PO |
957 | if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents, |
958 | &result_commit, NULL, sign_commit)) | |
6b3c4c05 | 959 | die(_("failed to write commit object")); |
f23101bf | 960 | strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); |
52684310 | 961 | finish(head, remoteheads, &result_commit, buf.buf); |
1c7b76be | 962 | strbuf_release(&buf); |
b6433555 | 963 | remove_merge_branch_state(the_repository); |
1c7b76be MV |
964 | return 0; |
965 | } | |
966 | ||
08e3ce5a | 967 | static int suggest_conflicts(void) |
1c7b76be | 968 | { |
418c9b17 | 969 | const char *filename; |
1c7b76be | 970 | FILE *fp; |
75c961b7 | 971 | struct strbuf msgbuf = STRBUF_INIT; |
1c7b76be | 972 | |
102de880 | 973 | filename = git_path_merge_msg(the_repository); |
23a9e071 | 974 | fp = xfopen(filename, "a"); |
75c961b7 | 975 | |
1055997e DL |
976 | /* |
977 | * We can't use cleanup_mode because if we're not using the editor, | |
978 | * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even | |
979 | * though the message is meant to be processed later by git-commit. | |
980 | * Thus, we will get the cleanup mode which is returned when we _are_ | |
981 | * using an editor. | |
982 | */ | |
1a2b985f DL |
983 | append_conflicts_hint(&the_index, &msgbuf, |
984 | get_cleanup_mode(cleanup_arg, 1)); | |
75c961b7 | 985 | fputs(msgbuf.buf, fp); |
8d025b7c | 986 | strbuf_release(&msgbuf); |
1c7b76be | 987 | fclose(fp); |
35843b11 | 988 | repo_rerere(the_repository, allow_rerere_auto); |
bacec478 ÆAB |
989 | printf(_("Automatic merge failed; " |
990 | "fix conflicts and then commit the result.\n")); | |
1c7b76be MV |
991 | return 1; |
992 | } | |
993 | ||
1c7b76be MV |
994 | static int evaluate_result(void) |
995 | { | |
996 | int cnt = 0; | |
997 | struct rev_info rev; | |
998 | ||
1c7b76be | 999 | /* Check how many files differ. */ |
2abf3503 | 1000 | repo_init_revisions(the_repository, &rev, ""); |
1c7b76be MV |
1001 | setup_revisions(0, NULL, &rev, NULL); |
1002 | rev.diffopt.output_format |= | |
1003 | DIFF_FORMAT_CALLBACK; | |
1004 | rev.diffopt.format_callback = count_diff_files; | |
1005 | rev.diffopt.format_callback_data = &cnt; | |
1006 | run_diff_files(&rev, 0); | |
1007 | ||
1008 | /* | |
1009 | * Check how many unmerged entries are | |
1010 | * there. | |
1011 | */ | |
1012 | cnt += count_unmerged_entries(); | |
1013 | ||
2108fe4a | 1014 | release_revisions(&rev); |
1c7b76be MV |
1015 | return cnt; |
1016 | } | |
1017 | ||
93e535a5 | 1018 | /* |
d6ac1d21 | 1019 | * Pretend as if the user told us to merge with the remote-tracking |
93e535a5 JH |
1020 | * branch we have for the upstream of the current branch |
1021 | */ | |
1022 | static int setup_with_upstream(const char ***argv) | |
1023 | { | |
1024 | struct branch *branch = branch_get(NULL); | |
1025 | int i; | |
1026 | const char **args; | |
1027 | ||
1028 | if (!branch) | |
c7f426d4 | 1029 | die(_("No current branch.")); |
9e3751d4 | 1030 | if (!branch->remote_name) |
c7f426d4 | 1031 | die(_("No remote for the current branch.")); |
93e535a5 | 1032 | if (!branch->merge_nr) |
c7f426d4 | 1033 | die(_("No default upstream defined for the current branch.")); |
93e535a5 | 1034 | |
50a6c8ef | 1035 | args = xcalloc(st_add(branch->merge_nr, 1), sizeof(char *)); |
93e535a5 JH |
1036 | for (i = 0; i < branch->merge_nr; i++) { |
1037 | if (!branch->merge[i]->dst) | |
d6ac1d21 | 1038 | die(_("No remote-tracking branch for %s from %s"), |
93e535a5 JH |
1039 | branch->merge[i]->src, branch->remote_name); |
1040 | args[i] = branch->merge[i]->dst; | |
1041 | } | |
1042 | args[i] = NULL; | |
1043 | *argv = args; | |
1044 | return i; | |
1045 | } | |
1046 | ||
8e6a6bb3 | 1047 | static void write_merge_heads(struct commit_list *remoteheads) |
66f4b98a | 1048 | { |
66f4b98a JS |
1049 | struct commit_list *j; |
1050 | struct strbuf buf = STRBUF_INIT; | |
1051 | ||
274a5c06 | 1052 | for (j = remoteheads; j; j = j->next) { |
f2fd0760 | 1053 | struct object_id *oid; |
274a5c06 | 1054 | struct commit *c = j->item; |
e2e5ac23 NTND |
1055 | struct merge_remote_desc *desc; |
1056 | ||
1057 | desc = merge_remote_util(c); | |
1058 | if (desc && desc->obj) { | |
1059 | oid = &desc->obj->oid; | |
274a5c06 | 1060 | } else { |
f2fd0760 | 1061 | oid = &c->object.oid; |
274a5c06 | 1062 | } |
f2fd0760 | 1063 | strbuf_addf(&buf, "%s\n", oid_to_hex(oid)); |
274a5c06 | 1064 | } |
102de880 | 1065 | write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len); |
418c9b17 | 1066 | |
66f4b98a | 1067 | strbuf_reset(&buf); |
a54841e9 | 1068 | if (fast_forward == FF_NO) |
a22ae753 | 1069 | strbuf_addstr(&buf, "no-ff"); |
102de880 | 1070 | write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len); |
814c4b37 | 1071 | strbuf_release(&buf); |
66f4b98a JS |
1072 | } |
1073 | ||
8e6a6bb3 MG |
1074 | static void write_merge_state(struct commit_list *remoteheads) |
1075 | { | |
1076 | write_merge_heads(remoteheads); | |
1077 | strbuf_addch(&merge_msg, '\n'); | |
102de880 SB |
1078 | write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf, |
1079 | merge_msg.len); | |
8e6a6bb3 MG |
1080 | } |
1081 | ||
f8246281 JH |
1082 | static int default_edit_option(void) |
1083 | { | |
1084 | static const char name[] = "GIT_MERGE_AUTOEDIT"; | |
1085 | const char *e = getenv(name); | |
1086 | struct stat st_stdin, st_stdout; | |
1087 | ||
1088 | if (have_message) | |
1089 | /* an explicit -m msg without --[no-]edit */ | |
1090 | return 0; | |
1091 | ||
1092 | if (e) { | |
89576613 | 1093 | int v = git_parse_maybe_bool(e); |
f8246281 | 1094 | if (v < 0) |
bef4830e | 1095 | die(_("Bad value '%s' in environment '%s'"), e, name); |
f8246281 JH |
1096 | return v; |
1097 | } | |
1098 | ||
1099 | /* Use editor if stdin and stdout are the same and is a tty */ | |
1100 | return (!fstat(0, &st_stdin) && | |
1101 | !fstat(1, &st_stdout) && | |
d46f476c | 1102 | isatty(0) && isatty(1) && |
f8246281 JH |
1103 | st_stdin.st_dev == st_stdout.st_dev && |
1104 | st_stdin.st_ino == st_stdout.st_ino && | |
1105 | st_stdin.st_mode == st_stdout.st_mode); | |
1106 | } | |
1107 | ||
34349dbf JH |
1108 | static struct commit_list *reduce_parents(struct commit *head_commit, |
1109 | int *head_subsumed, | |
1110 | struct commit_list *remoteheads) | |
b5d887f9 | 1111 | { |
e510ab89 | 1112 | struct commit_list *parents, **remotes; |
b5d887f9 | 1113 | |
0b10b8a3 JH |
1114 | /* |
1115 | * Is the current HEAD reachable from another commit being | |
1116 | * merged? If so we do not want to record it as a parent of | |
1117 | * the resulting merge, unless --no-ff is given. We will flip | |
1118 | * this variable to 0 when we find HEAD among the independent | |
1119 | * tips being merged. | |
1120 | */ | |
1121 | *head_subsumed = 1; | |
e78cbf8c | 1122 | |
0b10b8a3 | 1123 | /* Find what parents to record by checking independent ones. */ |
e78cbf8c | 1124 | parents = reduce_heads(remoteheads); |
4da72644 | 1125 | free_commit_list(remoteheads); |
e78cbf8c | 1126 | |
e510ab89 RS |
1127 | remoteheads = NULL; |
1128 | remotes = &remoteheads; | |
1129 | while (parents) { | |
1130 | struct commit *commit = pop_commit(&parents); | |
e78cbf8c JH |
1131 | if (commit == head_commit) |
1132 | *head_subsumed = 0; | |
1133 | else | |
1134 | remotes = &commit_list_insert(commit, remotes)->next; | |
1135 | } | |
b5d887f9 JH |
1136 | return remoteheads; |
1137 | } | |
f8246281 | 1138 | |
52fecab2 JH |
1139 | static void prepare_merge_message(struct strbuf *merge_names, struct strbuf *merge_msg) |
1140 | { | |
1141 | struct fmt_merge_msg_opts opts; | |
1142 | ||
1143 | memset(&opts, 0, sizeof(opts)); | |
1144 | opts.add_title = !have_message; | |
1145 | opts.shortlog_len = shortlog_len; | |
1146 | opts.credit_people = (0 < option_edit); | |
bd2bc942 | 1147 | opts.into_name = into_name; |
52fecab2 JH |
1148 | |
1149 | fmt_merge_msg(merge_names, merge_msg, &opts); | |
1150 | if (merge_msg->len) | |
1151 | strbuf_setlen(merge_msg, merge_msg->len - 1); | |
1152 | } | |
1153 | ||
74e8bc59 JH |
1154 | static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge_names) |
1155 | { | |
1156 | const char *filename; | |
1157 | int fd, pos, npos; | |
1158 | struct strbuf fetch_head_file = STRBUF_INIT; | |
ab47df2d | 1159 | const unsigned hexsz = the_hash_algo->hexsz; |
74e8bc59 JH |
1160 | |
1161 | if (!merge_names) | |
1162 | merge_names = &fetch_head_file; | |
1163 | ||
102de880 | 1164 | filename = git_path_fetch_head(the_repository); |
66e905b7 | 1165 | fd = xopen(filename, O_RDONLY); |
74e8bc59 JH |
1166 | |
1167 | if (strbuf_read(merge_names, fd, 0) < 0) | |
1168 | die_errno(_("could not read '%s'"), filename); | |
1169 | if (close(fd) < 0) | |
1170 | die_errno(_("could not close '%s'"), filename); | |
1171 | ||
1172 | for (pos = 0; pos < merge_names->len; pos = npos) { | |
52684310 | 1173 | struct object_id oid; |
74e8bc59 JH |
1174 | char *ptr; |
1175 | struct commit *commit; | |
1176 | ||
1177 | ptr = strchr(merge_names->buf + pos, '\n'); | |
1178 | if (ptr) | |
1179 | npos = ptr - merge_names->buf + 1; | |
1180 | else | |
1181 | npos = merge_names->len; | |
1182 | ||
ab47df2d | 1183 | if (npos - pos < hexsz + 2 || |
52684310 | 1184 | get_oid_hex(merge_names->buf + pos, &oid)) |
74e8bc59 | 1185 | commit = NULL; /* bad */ |
ab47df2d | 1186 | else if (memcmp(merge_names->buf + pos + hexsz, "\t\t", 2)) |
74e8bc59 JH |
1187 | continue; /* not-for-merge */ |
1188 | else { | |
ab47df2d | 1189 | char saved = merge_names->buf[pos + hexsz]; |
1190 | merge_names->buf[pos + hexsz] = '\0'; | |
74e8bc59 | 1191 | commit = get_merge_parent(merge_names->buf + pos); |
ab47df2d | 1192 | merge_names->buf[pos + hexsz] = saved; |
74e8bc59 JH |
1193 | } |
1194 | if (!commit) { | |
1195 | if (ptr) | |
1196 | *ptr = '\0'; | |
bef4830e | 1197 | die(_("not something we can merge in %s: %s"), |
74e8bc59 JH |
1198 | filename, merge_names->buf + pos); |
1199 | } | |
1200 | remotes = &commit_list_insert(commit, remotes)->next; | |
1201 | } | |
1202 | ||
1203 | if (merge_names == &fetch_head_file) | |
1204 | strbuf_release(&fetch_head_file); | |
1205 | } | |
1206 | ||
34349dbf JH |
1207 | static struct commit_list *collect_parents(struct commit *head_commit, |
1208 | int *head_subsumed, | |
1cf32f4d JH |
1209 | int argc, const char **argv, |
1210 | struct strbuf *merge_msg) | |
34349dbf JH |
1211 | { |
1212 | int i; | |
1213 | struct commit_list *remoteheads = NULL; | |
1214 | struct commit_list **remotes = &remoteheads; | |
77038015 JH |
1215 | struct strbuf merge_names = STRBUF_INIT, *autogen = NULL; |
1216 | ||
1217 | if (merge_msg && (!have_message || shortlog_len)) | |
1218 | autogen = &merge_names; | |
34349dbf JH |
1219 | |
1220 | if (head_commit) | |
1221 | remotes = &commit_list_insert(head_commit, remotes)->next; | |
34349dbf | 1222 | |
74e8bc59 JH |
1223 | if (argc == 1 && !strcmp(argv[0], "FETCH_HEAD")) { |
1224 | handle_fetch_head(remotes, autogen); | |
1225 | remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads); | |
1226 | } else { | |
1227 | for (i = 0; i < argc; i++) { | |
1228 | struct commit *commit = get_merge_parent(argv[i]); | |
1229 | if (!commit) | |
1230 | help_unknown_ref(argv[i], "merge", | |
bef4830e | 1231 | _("not something we can merge")); |
74e8bc59 JH |
1232 | remotes = &commit_list_insert(commit, remotes)->next; |
1233 | } | |
1234 | remoteheads = reduce_parents(head_commit, head_subsumed, remoteheads); | |
1235 | if (autogen) { | |
1236 | struct commit_list *p; | |
1237 | for (p = remoteheads; p; p = p->next) | |
1238 | merge_name(merge_remote_util(p->item)->name, autogen); | |
1239 | } | |
1240 | } | |
1cf32f4d | 1241 | |
77038015 | 1242 | if (autogen) { |
77038015 JH |
1243 | prepare_merge_message(autogen, merge_msg); |
1244 | strbuf_release(autogen); | |
1cf32f4d JH |
1245 | } |
1246 | ||
1247 | return remoteheads; | |
34349dbf JH |
1248 | } |
1249 | ||
adcc94a0 JH |
1250 | static int merging_a_throwaway_tag(struct commit *commit) |
1251 | { | |
1252 | char *tag_ref; | |
1253 | struct object_id oid; | |
1254 | int is_throwaway_tag = 0; | |
1255 | ||
1256 | /* Are we merging a tag? */ | |
1257 | if (!merge_remote_util(commit) || | |
1258 | !merge_remote_util(commit)->obj || | |
1259 | merge_remote_util(commit)->obj->type != OBJ_TAG) | |
1260 | return is_throwaway_tag; | |
1261 | ||
1262 | /* | |
1263 | * Now we know we are merging a tag object. Are we downstream | |
1264 | * and following the tags from upstream? If so, we must have | |
1265 | * the tag object pointed at by "refs/tags/$T" where $T is the | |
1266 | * tagname recorded in the tag object. We want to allow such | |
1267 | * a "just to catch up" merge to fast-forward. | |
1268 | * | |
1269 | * Otherwise, we are playing an integrator's role, making a | |
1270 | * merge with a throw-away tag from a contributor with | |
1271 | * something like "git pull $contributor $signed_tag". | |
1272 | * We want to forbid such a merge from fast-forwarding | |
1273 | * by default; otherwise we would not keep the signature | |
1274 | * anywhere. | |
1275 | */ | |
1276 | tag_ref = xstrfmt("refs/tags/%s", | |
1277 | ((struct tag *)merge_remote_util(commit)->obj)->tag); | |
1278 | if (!read_ref(tag_ref, &oid) && | |
4a7e27e9 | 1279 | oideq(&oid, &merge_remote_util(commit)->obj->oid)) |
adcc94a0 JH |
1280 | is_throwaway_tag = 0; |
1281 | else | |
1282 | is_throwaway_tag = 1; | |
1283 | free(tag_ref); | |
1284 | return is_throwaway_tag; | |
1285 | } | |
1286 | ||
1c7b76be MV |
1287 | int cmd_merge(int argc, const char **argv, const char *prefix) |
1288 | { | |
52684310 | 1289 | struct object_id result_tree, stash, head_oid; |
894642f6 | 1290 | struct commit *head_commit; |
f285a2d7 | 1291 | struct strbuf buf = STRBUF_INIT; |
17377b62 | 1292 | int i, ret = 0, head_subsumed; |
1c7b76be MV |
1293 | int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; |
1294 | struct commit_list *common = NULL; | |
1295 | const char *best_strategy = NULL, *wt_strategy = NULL; | |
6046f7a9 | 1296 | struct commit_list *remoteheads = NULL, *p; |
96ec7b1e | 1297 | void *branch_to_free; |
367ff694 | 1298 | int orig_argc = argc; |
1c7b76be | 1299 | |
da53eec6 NTND |
1300 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1301 | usage_with_options(builtin_merge_usage, builtin_merge_options); | |
1c7b76be | 1302 | |
a3380639 DS |
1303 | prepare_repo_settings(the_repository); |
1304 | the_repository->settings.command_requires_full_index = 0; | |
1305 | ||
1c7b76be MV |
1306 | /* |
1307 | * Check if we are _not_ on a detached HEAD, i.e. if there is a | |
1308 | * current branch. | |
1309 | */ | |
0f2dc722 | 1310 | branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL); |
de3ce210 RS |
1311 | if (branch) |
1312 | skip_prefix(branch, "refs/heads/", &branch); | |
7adf5266 | 1313 | |
14c4586c EN |
1314 | if (!pull_twohead) { |
1315 | char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM"); | |
1316 | if (default_strategy && !strcmp(default_strategy, "ort")) | |
1317 | pull_twohead = "ort"; | |
1318 | } | |
1319 | ||
7adf5266 DS |
1320 | init_diff_ui_defaults(); |
1321 | git_config(git_merge_config, NULL); | |
1322 | ||
52684310 | 1323 | if (!branch || is_null_oid(&head_oid)) |
894642f6 | 1324 | head_commit = NULL; |
baf18fc2 | 1325 | else |
bc83266a | 1326 | head_commit = lookup_commit_or_die(&head_oid, "HEAD"); |
1c7b76be | 1327 | |
0d8fc3ef JH |
1328 | if (branch_mergeoptions) |
1329 | parse_branch_merge_options(branch_mergeoptions); | |
37782920 | 1330 | argc = parse_options(argc, argv, prefix, builtin_merge_options, |
1c7b76be | 1331 | builtin_merge_usage, 0); |
898eacd8 JH |
1332 | if (shortlog_len < 0) |
1333 | shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; | |
2a22c1b3 | 1334 | |
99bfc669 JK |
1335 | if (verbosity < 0 && show_progress == -1) |
1336 | show_progress = 0; | |
1337 | ||
35d2fffd JH |
1338 | if (abort_current_merge) { |
1339 | int nargc = 2; | |
1340 | const char *nargv[] = {"reset", "--merge", NULL}; | |
a03b5553 | 1341 | struct strbuf stash_oid = STRBUF_INIT; |
35d2fffd | 1342 | |
042e290d | 1343 | if (orig_argc != 2) |
c7d227df | 1344 | usage_msg_opt(_("--abort expects no arguments"), |
042e290d CP |
1345 | builtin_merge_usage, builtin_merge_options); |
1346 | ||
102de880 | 1347 | if (!file_exists(git_path_merge_head(the_repository))) |
bacec478 | 1348 | die(_("There is no merge to abort (MERGE_HEAD missing).")); |
35d2fffd | 1349 | |
a03b5553 DL |
1350 | if (read_oneliner(&stash_oid, git_path_merge_autostash(the_repository), |
1351 | READ_ONELINER_SKIP_IF_EMPTY)) | |
1352 | unlink(git_path_merge_autostash(the_repository)); | |
1353 | ||
35d2fffd | 1354 | /* Invoke 'git reset --merge' */ |
d5a35c11 | 1355 | ret = cmd_reset(nargc, nargv, prefix); |
a03b5553 DL |
1356 | |
1357 | if (stash_oid.len) | |
1358 | apply_autostash_oid(stash_oid.buf); | |
1359 | ||
1360 | strbuf_release(&stash_oid); | |
d5a35c11 | 1361 | goto done; |
35d2fffd JH |
1362 | } |
1363 | ||
f3f8311e NTND |
1364 | if (quit_current_merge) { |
1365 | if (orig_argc != 2) | |
1366 | usage_msg_opt(_("--quit expects no arguments"), | |
1367 | builtin_merge_usage, | |
1368 | builtin_merge_options); | |
1369 | ||
1370 | remove_merge_branch_state(the_repository); | |
1371 | goto done; | |
1372 | } | |
1373 | ||
367ff694 CP |
1374 | if (continue_current_merge) { |
1375 | int nargc = 1; | |
1376 | const char *nargv[] = {"commit", NULL}; | |
1377 | ||
1378 | if (orig_argc != 2) | |
c7d227df | 1379 | usage_msg_opt(_("--continue expects no arguments"), |
367ff694 CP |
1380 | builtin_merge_usage, builtin_merge_options); |
1381 | ||
102de880 | 1382 | if (!file_exists(git_path_merge_head(the_repository))) |
367ff694 CP |
1383 | die(_("There is no merge in progress (MERGE_HEAD missing).")); |
1384 | ||
1385 | /* Invoke 'git commit' */ | |
1386 | ret = cmd_commit(nargc, nargv, prefix); | |
1387 | goto done; | |
1388 | } | |
1389 | ||
fbc1ed62 | 1390 | if (repo_read_index_unmerged(the_repository)) |
2a22c1b3 JH |
1391 | die_resolve_conflict("merge"); |
1392 | ||
102de880 | 1393 | if (file_exists(git_path_merge_head(the_repository))) { |
2a22c1b3 JH |
1394 | /* |
1395 | * There is no unmerged entry, don't advise 'git | |
1396 | * add/rm <file>', just 'git commit'. | |
1397 | */ | |
ed9bff08 | 1398 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) |
bacec478 | 1399 | die(_("You have not concluded your merge (MERGE_HEAD exists).\n" |
ad5fe377 | 1400 | "Please, commit your changes before you merge.")); |
2a22c1b3 | 1401 | else |
bacec478 | 1402 | die(_("You have not concluded your merge (MERGE_HEAD exists).")); |
2a22c1b3 | 1403 | } |
c8e4159e | 1404 | if (ref_exists("CHERRY_PICK_HEAD")) { |
ed9bff08 | 1405 | if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) |
f68f1801 | 1406 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" |
ad5fe377 | 1407 | "Please, commit your changes before you merge.")); |
d7e5c0cb | 1408 | else |
f68f1801 | 1409 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); |
2a22c1b3 | 1410 | } |
031b2033 | 1411 | resolve_undo_clear_index(&the_index); |
2a22c1b3 | 1412 | |
d540b70c DL |
1413 | if (option_edit < 0) |
1414 | option_edit = default_edit_option(); | |
1415 | ||
1416 | cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit); | |
1417 | ||
7f87aff2 TA |
1418 | if (verbosity < 0) |
1419 | show_diffstat = 0; | |
1c7b76be MV |
1420 | |
1421 | if (squash) { | |
a54841e9 | 1422 | if (fast_forward == FF_NO) |
12909b6b | 1423 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff."); |
1d14d0c9 | 1424 | if (option_commit > 0) |
12909b6b | 1425 | die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit."); |
1d14d0c9 VV |
1426 | /* |
1427 | * squash can now silently disable option_commit - this is not | |
1428 | * a problem as it is only overriding the default, not a user | |
1429 | * supplied option. | |
1430 | */ | |
1c7b76be MV |
1431 | option_commit = 0; |
1432 | } | |
1433 | ||
1d14d0c9 VV |
1434 | if (option_commit < 0) |
1435 | option_commit = 1; | |
1436 | ||
00c7e7e7 JH |
1437 | if (!argc) { |
1438 | if (default_to_upstream) | |
1439 | argc = setup_with_upstream(&argv); | |
1440 | else | |
1441 | die(_("No commit specified and merge.defaultToUpstream not set.")); | |
1442 | } else if (argc == 1 && !strcmp(argv[0], "-")) { | |
1443 | argv[0] = "@{-1}"; | |
4e8115ff | 1444 | } |
00c7e7e7 | 1445 | |
1c7b76be MV |
1446 | if (!argc) |
1447 | usage_with_options(builtin_merge_usage, | |
1448 | builtin_merge_options); | |
1449 | ||
1faac1ce | 1450 | if (!head_commit) { |
1c7b76be MV |
1451 | /* |
1452 | * If the merged head is a valid one there is no reason | |
1453 | * to forbid "git merge" into a branch yet to be born. | |
1454 | * We do the same for "git pull". | |
1455 | */ | |
52684310 | 1456 | struct object_id *remote_head_oid; |
4be636f4 | 1457 | if (squash) |
bacec478 | 1458 | die(_("Squash commit into empty head not supported yet")); |
a54841e9 | 1459 | if (fast_forward == FF_NO) |
bacec478 ÆAB |
1460 | die(_("Non-fast-forward commit does not make sense into " |
1461 | "an empty head")); | |
1cf32f4d JH |
1462 | remoteheads = collect_parents(head_commit, &head_subsumed, |
1463 | argc, argv, NULL); | |
b84e65d4 | 1464 | if (!remoteheads) |
bacec478 | 1465 | die(_("%s - not something we can merge"), argv[0]); |
eaa4e59c JH |
1466 | if (remoteheads->next) |
1467 | die(_("Can merge only exactly one commit into empty head")); | |
7488ba3e JK |
1468 | |
1469 | if (verify_signatures) | |
54887b46 HJI |
1470 | verify_merge_signature(remoteheads->item, verbosity, |
1471 | check_trust_level); | |
7488ba3e | 1472 | |
52684310 | 1473 | remote_head_oid = &remoteheads->item->object.oid; |
9397f3cf | 1474 | read_empty(remote_head_oid); |
ae077771 | 1475 | update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0, |
1476 | UPDATE_REFS_DIE_ON_ERR); | |
d5a35c11 | 1477 | goto done; |
1faac1ce | 1478 | } |
1c7b76be | 1479 | |
1faac1ce | 1480 | /* |
b4391657 JH |
1481 | * All the rest are the commits being merged; prepare |
1482 | * the standard merge summary message to be appended | |
1483 | * to the given message. | |
1faac1ce | 1484 | */ |
b4391657 JH |
1485 | remoteheads = collect_parents(head_commit, &head_subsumed, |
1486 | argc, argv, &merge_msg); | |
1c7b76be | 1487 | |
894642f6 | 1488 | if (!head_commit || !argc) |
1c7b76be MV |
1489 | usage_with_options(builtin_merge_usage, |
1490 | builtin_merge_options); | |
1491 | ||
efed0022 SG |
1492 | if (verify_signatures) { |
1493 | for (p = remoteheads; p; p = p->next) { | |
54887b46 HJI |
1494 | verify_merge_signature(p->item, verbosity, |
1495 | check_trust_level); | |
efed0022 SG |
1496 | } |
1497 | } | |
1498 | ||
1c7b76be | 1499 | strbuf_addstr(&buf, "merge"); |
b5d887f9 JH |
1500 | for (p = remoteheads; p; p = p->next) |
1501 | strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name); | |
1c7b76be MV |
1502 | setenv("GIT_REFLOG_ACTION", buf.buf, 0); |
1503 | strbuf_reset(&buf); | |
1504 | ||
b5d887f9 JH |
1505 | for (p = remoteheads; p; p = p->next) { |
1506 | struct commit *commit = p->item; | |
ae8e4c9c | 1507 | strbuf_addf(&buf, "GITHEAD_%s", |
c368dde9 | 1508 | oid_to_hex(&commit->object.oid)); |
b5d887f9 | 1509 | setenv(buf.buf, merge_remote_util(commit)->name, 1); |
1c7b76be | 1510 | strbuf_reset(&buf); |
adcc94a0 | 1511 | if (fast_forward != FF_ONLY && merging_a_throwaway_tag(commit)) |
a54841e9 | 1512 | fast_forward = FF_NO; |
1c7b76be MV |
1513 | } |
1514 | ||
6a5fb966 EN |
1515 | if (!use_strategies && !pull_twohead && |
1516 | remoteheads && !remoteheads->next) { | |
1517 | char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM"); | |
1518 | if (default_strategy) | |
1519 | append_strategy(get_strategy(default_strategy)); | |
1520 | } | |
1c7b76be | 1521 | if (!use_strategies) { |
e78cbf8c JH |
1522 | if (!remoteheads) |
1523 | ; /* already up-to-date */ | |
1524 | else if (!remoteheads->next) | |
1c7b76be MV |
1525 | add_strategies(pull_twohead, DEFAULT_TWOHEAD); |
1526 | else | |
1527 | add_strategies(pull_octopus, DEFAULT_OCTOPUS); | |
1528 | } | |
1529 | ||
1530 | for (i = 0; i < use_strategies_nr; i++) { | |
1531 | if (use_strategies[i]->attr & NO_FAST_FORWARD) | |
a54841e9 | 1532 | fast_forward = FF_NO; |
1c7b76be MV |
1533 | if (use_strategies[i]->attr & NO_TRIVIAL) |
1534 | allow_trivial = 0; | |
1535 | } | |
1536 | ||
e78cbf8c JH |
1537 | if (!remoteheads) |
1538 | ; /* already up-to-date */ | |
1539 | else if (!remoteheads->next) | |
cb338c23 ÆAB |
1540 | common = repo_get_merge_bases(the_repository, head_commit, |
1541 | remoteheads->item); | |
1c7b76be MV |
1542 | else { |
1543 | struct commit_list *list = remoteheads; | |
894642f6 | 1544 | commit_list_insert(head_commit, &list); |
1c7b76be MV |
1545 | common = get_octopus_merge_bases(list); |
1546 | free(list); | |
1547 | } | |
1548 | ||
ae077771 | 1549 | update_ref("updating ORIG_HEAD", "ORIG_HEAD", |
1550 | &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR); | |
1c7b76be | 1551 | |
e379fdf3 JH |
1552 | if (remoteheads && !common) { |
1553 | /* No common ancestors found. */ | |
1554 | if (!allow_unrelated_histories) | |
1555 | die(_("refusing to merge unrelated histories")); | |
1556 | /* otherwise, we need a real merge. */ | |
1557 | } else if (!remoteheads || | |
e78cbf8c JH |
1558 | (!remoteheads->next && !common->next && |
1559 | common->item == remoteheads->item)) { | |
1c7b76be MV |
1560 | /* |
1561 | * If head can reach all the merge then we are up to date. | |
1562 | * but first the most common case of merging one remote. | |
1563 | */ | |
ad9322da | 1564 | finish_up_to_date(); |
d5a35c11 | 1565 | goto done; |
a54841e9 | 1566 | } else if (fast_forward != FF_NO && !remoteheads->next && |
1c7b76be | 1567 | !common->next && |
4a7e27e9 | 1568 | oideq(&common->item->object.oid, &head_commit->object.oid)) { |
1c7b76be | 1569 | /* Again the most common case of merging one remote. */ |
345e216f ÆAB |
1570 | const char *msg = have_message ? |
1571 | "Fast-forward (no commit created; -m option ignored)" : | |
1572 | "Fast-forward"; | |
ae8e4c9c | 1573 | struct commit *commit; |
1c7b76be | 1574 | |
d59f765a | 1575 | if (verbosity >= 0) { |
ef2ed501 | 1576 | printf(_("Updating %s..%s\n"), |
d850b7a5 ÆAB |
1577 | repo_find_unique_abbrev(the_repository, &head_commit->object.oid, |
1578 | DEFAULT_ABBREV), | |
1579 | repo_find_unique_abbrev(the_repository, &remoteheads->item->object.oid, | |
1580 | DEFAULT_ABBREV)); | |
d59f765a | 1581 | } |
ae8e4c9c | 1582 | commit = remoteheads->item; |
b7f7c079 | 1583 | if (!commit) { |
d5a35c11 NTND |
1584 | ret = 1; |
1585 | goto done; | |
1586 | } | |
1c7b76be | 1587 | |
a03b5553 DL |
1588 | if (autostash) |
1589 | create_autostash(the_repository, | |
b7de153b | 1590 | git_path_merge_autostash(the_repository)); |
7e196c3a NTND |
1591 | if (checkout_fast_forward(the_repository, |
1592 | &head_commit->object.oid, | |
f06e90da | 1593 | &commit->object.oid, |
db699a8a | 1594 | overwrite_ignore)) { |
12510bd5 | 1595 | apply_autostash(git_path_merge_autostash(the_repository)); |
d5a35c11 NTND |
1596 | ret = 1; |
1597 | goto done; | |
1598 | } | |
1c7b76be | 1599 | |
345e216f | 1600 | finish(head_commit, remoteheads, &commit->object.oid, msg); |
b6433555 | 1601 | remove_merge_branch_state(the_repository); |
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 | */ | |
07047d68 | 1614 | refresh_index(&the_index, REFRESH_QUIET, NULL, NULL, NULL); |
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 | */ | |
ecb5091f ÆAB |
1620 | struct tree *head_tree = repo_get_commit_tree(the_repository, |
1621 | head_commit); | |
e4cdfe84 EN |
1622 | struct strbuf sb = STRBUF_INIT; |
1623 | ||
1624 | if (repo_index_has_changes(the_repository, head_tree, | |
1625 | &sb)) { | |
1626 | error(_("Your local changes to the following files would be overwritten by merge:\n %s"), | |
1627 | sb.buf); | |
1628 | strbuf_release(&sb); | |
41211db1 ÆAB |
1629 | ret = 2; |
1630 | goto done; | |
e4cdfe84 EN |
1631 | } |
1632 | ||
1c7b76be | 1633 | /* See if it is really trivial. */ |
f9bc573f | 1634 | git_committer_info(IDENT_STRICT); |
bacec478 | 1635 | printf(_("Trying really trivial in-index merge...\n")); |
52684310 | 1636 | if (!read_tree_trivial(&common->item->object.oid, |
1637 | &head_commit->object.oid, | |
1638 | &remoteheads->item->object.oid)) { | |
4c57bd27 | 1639 | ret = merge_trivial(head_commit, remoteheads); |
d5a35c11 NTND |
1640 | goto done; |
1641 | } | |
bacec478 | 1642 | printf(_("Nope.\n")); |
1c7b76be MV |
1643 | } |
1644 | } else { | |
1645 | /* | |
1646 | * An octopus. If we can reach all the remote we are up | |
1647 | * to date. | |
1648 | */ | |
1649 | int up_to_date = 1; | |
1650 | struct commit_list *j; | |
1651 | ||
1652 | for (j = remoteheads; j; j = j->next) { | |
1653 | struct commit_list *common_one; | |
1654 | ||
1655 | /* | |
1656 | * Here we *have* to calculate the individual | |
1657 | * merge_bases again, otherwise "git merge HEAD^ | |
1658 | * HEAD^^" would be missed. | |
1659 | */ | |
cb338c23 ÆAB |
1660 | common_one = repo_get_merge_bases(the_repository, |
1661 | head_commit, | |
1662 | j->item); | |
9001dc2a | 1663 | if (!oideq(&common_one->item->object.oid, &j->item->object.oid)) { |
1c7b76be MV |
1664 | up_to_date = 0; |
1665 | break; | |
1666 | } | |
1667 | } | |
1668 | if (up_to_date) { | |
ad9322da | 1669 | finish_up_to_date(); |
d5a35c11 | 1670 | goto done; |
1c7b76be MV |
1671 | } |
1672 | } | |
1673 | ||
a54841e9 | 1674 | if (fast_forward == FF_ONLY) |
3d5fc24d | 1675 | die_ff_impossible(); |
13474835 | 1676 | |
a03b5553 DL |
1677 | if (autostash) |
1678 | create_autostash(the_repository, | |
b7de153b | 1679 | git_path_merge_autostash(the_repository)); |
a03b5553 | 1680 | |
1c7b76be | 1681 | /* We are going to make a new commit. */ |
f9bc573f | 1682 | git_committer_info(IDENT_STRICT); |
1c7b76be MV |
1683 | |
1684 | /* | |
1685 | * At this point, we need a real merge. No matter what strategy | |
1686 | * we use, it would operate on the index, possibly affecting the | |
1687 | * working tree, and when resolved cleanly, have the desired | |
1688 | * tree in the index -- this means that the index must be in | |
1689 | * sync with the head commit. The strategies are responsible | |
1690 | * to ensure this. | |
034195ef EN |
1691 | * |
1692 | * Stash away the local changes so that we can try more than one | |
1693 | * and/or recover from merge strategies bailing while leaving the | |
1694 | * index and working tree polluted. | |
1c7b76be | 1695 | */ |
034195ef | 1696 | if (save_state(&stash)) |
52684310 | 1697 | oidclr(&stash); |
1c7b76be | 1698 | |
5b1d30ca | 1699 | for (i = 0; i < use_strategies_nr; i++) { |
f00abe6a | 1700 | int ret, cnt; |
1c7b76be | 1701 | if (i) { |
bacec478 | 1702 | printf(_("Rewinding the tree to pristine...\n")); |
52684310 | 1703 | restore_state(&head_commit->object.oid, &stash); |
1c7b76be MV |
1704 | } |
1705 | if (use_strategies_nr != 1) | |
bacec478 | 1706 | printf(_("Trying merge strategy %s...\n"), |
1c7b76be MV |
1707 | use_strategies[i]->name); |
1708 | /* | |
1709 | * Remember which strategy left the state in the working | |
1710 | * tree. | |
1711 | */ | |
1712 | wt_strategy = use_strategies[i]->name; | |
1713 | ||
ae15fd41 | 1714 | ret = try_merge_strategy(wt_strategy, |
4c57bd27 | 1715 | common, remoteheads, |
b4391657 | 1716 | head_commit); |
f00abe6a ECA |
1717 | /* |
1718 | * The backend exits with 1 when conflicts are | |
1719 | * left to be resolved, with 2 when it does not | |
1720 | * handle the given merge at all. | |
1721 | */ | |
1722 | if (ret < 2) { | |
1723 | if (!ret) { | |
5b1d30ca EN |
1724 | /* |
1725 | * This strategy worked; no point in trying | |
1726 | * another. | |
1727 | */ | |
f00abe6a | 1728 | merge_was_ok = 1; |
ae15fd41 | 1729 | best_strategy = wt_strategy; |
5b1d30ca | 1730 | break; |
f00abe6a | 1731 | } |
8777616e | 1732 | cnt = (use_strategies_nr > 1) ? evaluate_result() : 0; |
f00abe6a | 1733 | if (best_cnt <= 0 || cnt <= best_cnt) { |
ae15fd41 | 1734 | best_strategy = wt_strategy; |
f00abe6a | 1735 | best_cnt = cnt; |
1c7b76be | 1736 | } |
1c7b76be | 1737 | } |
1c7b76be MV |
1738 | } |
1739 | ||
1740 | /* | |
1741 | * If we have a resulting tree, that means the strategy module | |
1742 | * auto resolved the merge cleanly. | |
1743 | */ | |
5b1d30ca EN |
1744 | if (merge_was_ok && option_commit) { |
1745 | automerge_was_ok = 1; | |
e78cbf8c JH |
1746 | ret = finish_automerge(head_commit, head_subsumed, |
1747 | common, remoteheads, | |
52684310 | 1748 | &result_tree, wt_strategy); |
d5a35c11 NTND |
1749 | goto done; |
1750 | } | |
1c7b76be MV |
1751 | |
1752 | /* | |
1753 | * Pick the result from the best strategy and have the user fix | |
1754 | * it up. | |
1755 | */ | |
1756 | if (!best_strategy) { | |
52684310 | 1757 | restore_state(&head_commit->object.oid, &stash); |
1c7b76be MV |
1758 | if (use_strategies_nr > 1) |
1759 | fprintf(stderr, | |
bacec478 | 1760 | _("No merge strategy handled the merge.\n")); |
1c7b76be | 1761 | else |
bacec478 | 1762 | fprintf(stderr, _("Merge with strategy %s failed.\n"), |
1c7b76be | 1763 | use_strategies[0]->name); |
e082631e | 1764 | apply_autostash(git_path_merge_autostash(the_repository)); |
d5a35c11 NTND |
1765 | ret = 2; |
1766 | goto done; | |
1c7b76be MV |
1767 | } else if (best_strategy == wt_strategy) |
1768 | ; /* We already have its result in the working tree. */ | |
1769 | else { | |
bacec478 | 1770 | printf(_("Rewinding the tree to pristine...\n")); |
52684310 | 1771 | restore_state(&head_commit->object.oid, &stash); |
9938f30d | 1772 | printf(_("Using the %s strategy to prepare resolving by hand.\n"), |
1c7b76be | 1773 | best_strategy); |
4c57bd27 | 1774 | try_merge_strategy(best_strategy, common, remoteheads, |
b4391657 | 1775 | head_commit); |
1c7b76be MV |
1776 | } |
1777 | ||
b23ea979 | 1778 | if (squash) { |
4c57bd27 | 1779 | finish(head_commit, remoteheads, NULL, NULL); |
b23ea979 DS |
1780 | |
1781 | git_test_write_commit_graph_or_die(); | |
1782 | } else | |
4c57bd27 | 1783 | write_merge_state(remoteheads); |
1c7b76be | 1784 | |
d5a35c11 | 1785 | if (merge_was_ok) |
bacec478 ÆAB |
1786 | fprintf(stderr, _("Automatic merge went well; " |
1787 | "stopped before committing as requested\n")); | |
d5a35c11 | 1788 | else |
08e3ce5a | 1789 | ret = suggest_conflicts(); |
d3a9295a EN |
1790 | if (autostash) |
1791 | printf(_("When finished, apply stashed changes with `git stash pop`\n")); | |
d5a35c11 NTND |
1792 | |
1793 | done: | |
6046f7a9 EN |
1794 | if (!automerge_was_ok) { |
1795 | free_commit_list(common); | |
1796 | free_commit_list(remoteheads); | |
1797 | } | |
465028e0 | 1798 | strbuf_release(&buf); |
96ec7b1e | 1799 | free(branch_to_free); |
ab2cf371 | 1800 | discard_index(&the_index); |
d5a35c11 | 1801 | return ret; |
1c7b76be | 1802 | } |