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