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