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