]>
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 | ||
9 | #include "cache.h" | |
10 | #include "parse-options.h" | |
11 | #include "builtin.h" | |
12 | #include "run-command.h" | |
13 | #include "diff.h" | |
14 | #include "refs.h" | |
15 | #include "commit.h" | |
16 | #include "diffcore.h" | |
17 | #include "revision.h" | |
18 | #include "unpack-trees.h" | |
19 | #include "cache-tree.h" | |
20 | #include "dir.h" | |
21 | #include "utf8.h" | |
22 | #include "log-tree.h" | |
23 | #include "color.h" | |
fcab40a3 | 24 | #include "rerere.h" |
87091b49 | 25 | #include "help.h" |
18668f53 | 26 | #include "merge-recursive.h" |
cfc5789a | 27 | #include "resolve-undo.h" |
93e535a5 | 28 | #include "remote.h" |
898eacd8 | 29 | #include "fmt-merge-msg.h" |
ba3c69a9 | 30 | #include "gpg-interface.h" |
1c7b76be MV |
31 | |
32 | #define DEFAULT_TWOHEAD (1<<0) | |
33 | #define DEFAULT_OCTOPUS (1<<1) | |
34 | #define NO_FAST_FORWARD (1<<2) | |
35 | #define NO_TRIVIAL (1<<3) | |
36 | ||
37 | struct strategy { | |
38 | const char *name; | |
39 | unsigned attr; | |
40 | }; | |
41 | ||
42 | static const char * const builtin_merge_usage[] = { | |
962e6295 NTND |
43 | N_("git merge [options] [<commit>...]"), |
44 | N_("git merge [options] <msg> HEAD <commit>"), | |
45 | N_("git merge --abort"), | |
1c7b76be MV |
46 | NULL |
47 | }; | |
48 | ||
898eacd8 | 49 | static int show_diffstat = 1, shortlog_len = -1, squash; |
1c7b76be | 50 | static int option_commit = 1, allow_fast_forward = 1; |
f8246281 | 51 | static int fast_forward_only, option_edit = -1; |
efed0022 | 52 | static int allow_trivial = 1, have_message, verify_signatures; |
c1d7036b | 53 | static int overwrite_ignore = 1; |
2c47789d | 54 | static struct strbuf merge_msg = STRBUF_INIT; |
1c7b76be MV |
55 | static struct strategy **use_strategies; |
56 | static size_t use_strategies_nr, use_strategies_alloc; | |
8cc5b290 AP |
57 | static const char **xopts; |
58 | static size_t xopts_nr, xopts_alloc; | |
1c7b76be | 59 | static const char *branch; |
0d8fc3ef | 60 | static char *branch_mergeoptions; |
7610fa57 | 61 | static int option_renormalize; |
7f87aff2 | 62 | static int verbosity; |
cb6020bb | 63 | static int allow_rerere_auto; |
35d2fffd | 64 | static int abort_current_merge; |
99bfc669 | 65 | static int show_progress = -1; |
93e535a5 | 66 | static int default_to_upstream; |
ba3c69a9 | 67 | static const char *sign_commit; |
1c7b76be MV |
68 | |
69 | static struct strategy all_strategy[] = { | |
1c7b76be MV |
70 | { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL }, |
71 | { "octopus", DEFAULT_OCTOPUS }, | |
72 | { "resolve", 0 }, | |
1c7b76be MV |
73 | { "ours", NO_FAST_FORWARD | NO_TRIVIAL }, |
74 | { "subtree", NO_FAST_FORWARD | NO_TRIVIAL }, | |
75 | }; | |
76 | ||
77 | static const char *pull_twohead, *pull_octopus; | |
78 | ||
79 | static int option_parse_message(const struct option *opt, | |
80 | const char *arg, int unset) | |
81 | { | |
82 | struct strbuf *buf = opt->value; | |
83 | ||
84 | if (unset) | |
85 | strbuf_setlen(buf, 0); | |
74f5b7fb | 86 | else if (arg) { |
ce9d823b | 87 | strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg); |
1c7b76be | 88 | have_message = 1; |
74f5b7fb | 89 | } else |
bacec478 | 90 | return error(_("switch `m' requires a value")); |
1c7b76be MV |
91 | return 0; |
92 | } | |
93 | ||
94 | static struct strategy *get_strategy(const char *name) | |
95 | { | |
96 | int i; | |
87091b49 MV |
97 | struct strategy *ret; |
98 | static struct cmdnames main_cmds, other_cmds; | |
e321180e | 99 | static int loaded; |
1c7b76be MV |
100 | |
101 | if (!name) | |
102 | return NULL; | |
103 | ||
104 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) | |
105 | if (!strcmp(name, all_strategy[i].name)) | |
106 | return &all_strategy[i]; | |
1719b5e4 | 107 | |
e321180e | 108 | if (!loaded) { |
87091b49 | 109 | struct cmdnames not_strategies; |
e321180e | 110 | loaded = 1; |
87091b49 | 111 | |
87091b49 | 112 | memset(¬_strategies, 0, sizeof(struct cmdnames)); |
e321180e | 113 | load_command_list("git-merge-", &main_cmds, &other_cmds); |
87091b49 MV |
114 | for (i = 0; i < main_cmds.cnt; i++) { |
115 | int j, found = 0; | |
116 | struct cmdname *ent = main_cmds.names[i]; | |
117 | for (j = 0; j < ARRAY_SIZE(all_strategy); j++) | |
118 | if (!strncmp(ent->name, all_strategy[j].name, ent->len) | |
119 | && !all_strategy[j].name[ent->len]) | |
120 | found = 1; | |
121 | if (!found) | |
122 | add_cmdname(¬_strategies, ent->name, ent->len); | |
87091b49 | 123 | } |
ed874656 | 124 | exclude_cmds(&main_cmds, ¬_strategies); |
87091b49 MV |
125 | } |
126 | if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) { | |
bacec478 ÆAB |
127 | fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name); |
128 | fprintf(stderr, _("Available strategies are:")); | |
131f9a10 JH |
129 | for (i = 0; i < main_cmds.cnt; i++) |
130 | fprintf(stderr, " %s", main_cmds.names[i]->name); | |
131 | fprintf(stderr, ".\n"); | |
132 | if (other_cmds.cnt) { | |
bacec478 | 133 | fprintf(stderr, _("Available custom strategies are:")); |
131f9a10 JH |
134 | for (i = 0; i < other_cmds.cnt; i++) |
135 | fprintf(stderr, " %s", other_cmds.names[i]->name); | |
136 | fprintf(stderr, ".\n"); | |
137 | } | |
87091b49 MV |
138 | exit(1); |
139 | } | |
140 | ||
19d4b416 | 141 | ret = xcalloc(1, sizeof(struct strategy)); |
87091b49 | 142 | ret->name = xstrdup(name); |
52b48ef1 | 143 | ret->attr = NO_TRIVIAL; |
87091b49 | 144 | return ret; |
1c7b76be MV |
145 | } |
146 | ||
147 | static void append_strategy(struct strategy *s) | |
148 | { | |
149 | ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc); | |
150 | use_strategies[use_strategies_nr++] = s; | |
151 | } | |
152 | ||
153 | static int option_parse_strategy(const struct option *opt, | |
154 | const char *name, int unset) | |
155 | { | |
1c7b76be MV |
156 | if (unset) |
157 | return 0; | |
158 | ||
1719b5e4 | 159 | append_strategy(get_strategy(name)); |
1c7b76be MV |
160 | return 0; |
161 | } | |
162 | ||
8cc5b290 AP |
163 | static int option_parse_x(const struct option *opt, |
164 | const char *arg, int unset) | |
165 | { | |
166 | if (unset) | |
167 | return 0; | |
168 | ||
169 | ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc); | |
170 | xopts[xopts_nr++] = xstrdup(arg); | |
171 | return 0; | |
172 | } | |
173 | ||
1c7b76be MV |
174 | static int option_parse_n(const struct option *opt, |
175 | const char *arg, int unset) | |
176 | { | |
177 | show_diffstat = unset; | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static struct option builtin_merge_options[] = { | |
182 | { OPTION_CALLBACK, 'n', NULL, NULL, NULL, | |
962e6295 | 183 | N_("do not show a diffstat at the end of the merge"), |
1c7b76be MV |
184 | PARSE_OPT_NOARG, option_parse_n }, |
185 | OPT_BOOLEAN(0, "stat", &show_diffstat, | |
962e6295 NTND |
186 | N_("show a diffstat at the end of the merge")), |
187 | OPT_BOOLEAN(0, "summary", &show_diffstat, N_("(synonym to --stat)")), | |
188 | { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"), | |
189 | N_("add (at most <n>) entries from shortlog to merge commit message"), | |
96e9420c | 190 | PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, |
1c7b76be | 191 | OPT_BOOLEAN(0, "squash", &squash, |
962e6295 | 192 | N_("create a single commit instead of doing a merge")), |
1c7b76be | 193 | OPT_BOOLEAN(0, "commit", &option_commit, |
962e6295 | 194 | N_("perform a commit if the merge succeeds (default)")), |
f8246281 | 195 | OPT_BOOL('e', "edit", &option_edit, |
962e6295 | 196 | N_("edit message before committing")), |
1c7b76be | 197 | OPT_BOOLEAN(0, "ff", &allow_fast_forward, |
962e6295 | 198 | N_("allow fast-forward (default)")), |
13474835 | 199 | OPT_BOOLEAN(0, "ff-only", &fast_forward_only, |
962e6295 | 200 | N_("abort if fast-forward is not possible")), |
cb6020bb | 201 | OPT_RERERE_AUTOUPDATE(&allow_rerere_auto), |
efed0022 SG |
202 | OPT_BOOL(0, "verify-signatures", &verify_signatures, |
203 | N_("Verify that the named commit has a valid GPG signature")), | |
962e6295 NTND |
204 | OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"), |
205 | N_("merge strategy to use"), option_parse_strategy), | |
206 | OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"), | |
207 | N_("option for selected merge strategy"), option_parse_x), | |
208 | OPT_CALLBACK('m', "message", &merge_msg, N_("message"), | |
209 | N_("merge commit message (for a non-fast-forward merge)"), | |
1c7b76be | 210 | option_parse_message), |
7f87aff2 | 211 | OPT__VERBOSITY(&verbosity), |
35d2fffd | 212 | OPT_BOOLEAN(0, "abort", &abort_current_merge, |
962e6295 NTND |
213 | N_("abort the current in-progress merge")), |
214 | OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1), | |
215 | { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"), | |
216 | N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
217 | OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore, N_("update ignored files (default)")), | |
1c7b76be MV |
218 | OPT_END() |
219 | }; | |
220 | ||
221 | /* Cleans up metadata that is uninteresting after a succeeded merge. */ | |
222 | static void drop_save(void) | |
223 | { | |
224 | unlink(git_path("MERGE_HEAD")); | |
225 | unlink(git_path("MERGE_MSG")); | |
cf10f9fd | 226 | unlink(git_path("MERGE_MODE")); |
1c7b76be MV |
227 | } |
228 | ||
b4fd9406 | 229 | static int save_state(unsigned char *stash) |
1c7b76be MV |
230 | { |
231 | int len; | |
232 | struct child_process cp; | |
233 | struct strbuf buffer = STRBUF_INIT; | |
234 | const char *argv[] = {"stash", "create", NULL}; | |
235 | ||
236 | memset(&cp, 0, sizeof(cp)); | |
237 | cp.argv = argv; | |
238 | cp.out = -1; | |
239 | cp.git_cmd = 1; | |
240 | ||
241 | if (start_command(&cp)) | |
bacec478 | 242 | die(_("could not run stash.")); |
1c7b76be MV |
243 | len = strbuf_read(&buffer, cp.out, 1024); |
244 | close(cp.out); | |
245 | ||
246 | if (finish_command(&cp) || len < 0) | |
bacec478 | 247 | die(_("stash failed")); |
b4fd9406 NTND |
248 | else if (!len) /* no changes */ |
249 | return -1; | |
1c7b76be MV |
250 | strbuf_setlen(&buffer, buffer.len-1); |
251 | if (get_sha1(buffer.buf, stash)) | |
bacec478 | 252 | die(_("not a valid object: %s"), buffer.buf); |
b4fd9406 | 253 | return 0; |
1c7b76be MV |
254 | } |
255 | ||
172b6428 CB |
256 | static void read_empty(unsigned const char *sha1, int verbose) |
257 | { | |
258 | int i = 0; | |
259 | const char *args[7]; | |
260 | ||
261 | args[i++] = "read-tree"; | |
262 | if (verbose) | |
263 | args[i++] = "-v"; | |
264 | args[i++] = "-m"; | |
265 | args[i++] = "-u"; | |
266 | args[i++] = EMPTY_TREE_SHA1_HEX; | |
267 | args[i++] = sha1_to_hex(sha1); | |
268 | args[i] = NULL; | |
269 | ||
270 | if (run_command_v_opt(args, RUN_GIT_CMD)) | |
bacec478 | 271 | die(_("read-tree failed")); |
172b6428 CB |
272 | } |
273 | ||
1c7b76be MV |
274 | static void reset_hard(unsigned const char *sha1, int verbose) |
275 | { | |
276 | int i = 0; | |
277 | const char *args[6]; | |
278 | ||
279 | args[i++] = "read-tree"; | |
280 | if (verbose) | |
281 | args[i++] = "-v"; | |
282 | args[i++] = "--reset"; | |
283 | args[i++] = "-u"; | |
284 | args[i++] = sha1_to_hex(sha1); | |
285 | args[i] = NULL; | |
286 | ||
287 | if (run_command_v_opt(args, RUN_GIT_CMD)) | |
bacec478 | 288 | die(_("read-tree failed")); |
1c7b76be MV |
289 | } |
290 | ||
894642f6 NTND |
291 | static void restore_state(const unsigned char *head, |
292 | const unsigned char *stash) | |
1c7b76be | 293 | { |
f285a2d7 | 294 | struct strbuf sb = STRBUF_INIT; |
1c7b76be MV |
295 | const char *args[] = { "stash", "apply", NULL, NULL }; |
296 | ||
297 | if (is_null_sha1(stash)) | |
298 | return; | |
299 | ||
300 | reset_hard(head, 1); | |
301 | ||
1c7b76be MV |
302 | args[2] = sha1_to_hex(stash); |
303 | ||
304 | /* | |
305 | * It is OK to ignore error here, for example when there was | |
306 | * nothing to restore. | |
307 | */ | |
308 | run_command_v_opt(args, RUN_GIT_CMD); | |
309 | ||
310 | strbuf_release(&sb); | |
311 | refresh_cache(REFRESH_QUIET); | |
312 | } | |
313 | ||
314 | /* This is called when no merge was necessary. */ | |
315 | static void finish_up_to_date(const char *msg) | |
316 | { | |
7f87aff2 | 317 | if (verbosity >= 0) |
bacec478 | 318 | printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg); |
1c7b76be MV |
319 | drop_save(); |
320 | } | |
321 | ||
4c57bd27 | 322 | static void squash_message(struct commit *commit, struct commit_list *remoteheads) |
1c7b76be MV |
323 | { |
324 | struct rev_info rev; | |
f285a2d7 | 325 | struct strbuf out = STRBUF_INIT; |
1c7b76be | 326 | struct commit_list *j; |
418c9b17 | 327 | const char *filename; |
1c7b76be | 328 | int fd; |
dd2e794a | 329 | struct pretty_print_context ctx = {0}; |
1c7b76be | 330 | |
bacec478 | 331 | printf(_("Squash commit -- not updating HEAD\n")); |
418c9b17 JN |
332 | filename = git_path("SQUASH_MSG"); |
333 | fd = open(filename, O_WRONLY | O_CREAT, 0666); | |
1c7b76be | 334 | if (fd < 0) |
418c9b17 | 335 | die_errno(_("Could not write to '%s'"), filename); |
1c7b76be MV |
336 | |
337 | init_revisions(&rev, NULL); | |
338 | rev.ignore_merges = 1; | |
339 | rev.commit_format = CMIT_FMT_MEDIUM; | |
340 | ||
1c7b76be MV |
341 | commit->object.flags |= UNINTERESTING; |
342 | add_pending_object(&rev, &commit->object, NULL); | |
343 | ||
344 | for (j = remoteheads; j; j = j->next) | |
345 | add_pending_object(&rev, &j->item->object, NULL); | |
346 | ||
347 | setup_revisions(0, NULL, &rev, NULL); | |
348 | if (prepare_revision_walk(&rev)) | |
bacec478 | 349 | die(_("revision walk setup failed")); |
1c7b76be | 350 | |
dd2e794a TR |
351 | ctx.abbrev = rev.abbrev; |
352 | ctx.date_mode = rev.date_mode; | |
6bf13944 | 353 | ctx.fmt = rev.commit_format; |
dd2e794a | 354 | |
1c7b76be MV |
355 | strbuf_addstr(&out, "Squashed commit of the following:\n"); |
356 | while ((commit = get_revision(&rev)) != NULL) { | |
357 | strbuf_addch(&out, '\n'); | |
358 | strbuf_addf(&out, "commit %s\n", | |
359 | sha1_to_hex(commit->object.sha1)); | |
6bf13944 | 360 | pretty_print_commit(&ctx, commit, &out); |
1c7b76be | 361 | } |
47d32af2 | 362 | if (write(fd, out.buf, out.len) < 0) |
bacec478 | 363 | die_errno(_("Writing SQUASH_MSG")); |
47d32af2 | 364 | if (close(fd)) |
bacec478 | 365 | die_errno(_("Finishing SQUASH_MSG")); |
1c7b76be MV |
366 | strbuf_release(&out); |
367 | } | |
368 | ||
894642f6 | 369 | static void finish(struct commit *head_commit, |
4c57bd27 | 370 | struct commit_list *remoteheads, |
894642f6 | 371 | const unsigned char *new_head, const char *msg) |
1c7b76be | 372 | { |
f285a2d7 | 373 | struct strbuf reflog_message = STRBUF_INIT; |
894642f6 | 374 | const unsigned char *head = head_commit->object.sha1; |
1c7b76be | 375 | |
1c7b76be MV |
376 | if (!msg) |
377 | strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION")); | |
378 | else { | |
7f87aff2 TA |
379 | if (verbosity >= 0) |
380 | printf("%s\n", msg); | |
1c7b76be MV |
381 | strbuf_addf(&reflog_message, "%s: %s", |
382 | getenv("GIT_REFLOG_ACTION"), msg); | |
383 | } | |
384 | if (squash) { | |
4c57bd27 | 385 | squash_message(head_commit, remoteheads); |
1c7b76be | 386 | } else { |
7f87aff2 | 387 | if (verbosity >= 0 && !merge_msg.len) |
bacec478 | 388 | printf(_("No merge message -- not updating HEAD\n")); |
1c7b76be MV |
389 | else { |
390 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
391 | update_ref(reflog_message.buf, "HEAD", | |
392 | new_head, head, 0, | |
393 | DIE_ON_ERR); | |
394 | /* | |
395 | * We ignore errors in 'gc --auto', since the | |
396 | * user should see them. | |
397 | */ | |
398 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
399 | } | |
400 | } | |
401 | if (new_head && show_diffstat) { | |
402 | struct diff_options opts; | |
403 | diff_setup(&opts); | |
7a7159ac | 404 | opts.stat_width = -1; /* use full terminal width */ |
df44483a | 405 | opts.stat_graph_width = -1; /* respect statGraphWidth config */ |
1c7b76be MV |
406 | opts.output_format |= |
407 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
408 | opts.detect_rename = DIFF_DETECT_RENAME; | |
28452655 | 409 | diff_setup_done(&opts); |
1c7b76be MV |
410 | diff_tree_sha1(head, new_head, "", &opts); |
411 | diffcore_std(&opts); | |
412 | diff_flush(&opts); | |
413 | } | |
414 | ||
415 | /* Run a post-merge hook */ | |
ae98a008 | 416 | run_hook(NULL, "post-merge", squash ? "1" : "0", NULL); |
1c7b76be MV |
417 | |
418 | strbuf_release(&reflog_message); | |
419 | } | |
420 | ||
421 | /* Get the name for the merge commit's message. */ | |
422 | static void merge_name(const char *remote, struct strbuf *msg) | |
423 | { | |
ae8e4c9c | 424 | struct commit *remote_head; |
c6893323 | 425 | unsigned char branch_head[20]; |
f285a2d7 | 426 | struct strbuf buf = STRBUF_INIT; |
c9717ee9 | 427 | struct strbuf bname = STRBUF_INIT; |
1c7b76be | 428 | const char *ptr; |
751c5974 | 429 | char *found_ref; |
1c7b76be MV |
430 | int len, early; |
431 | ||
a552de75 JH |
432 | strbuf_branchname(&bname, remote); |
433 | remote = bname.buf; | |
c9717ee9 | 434 | |
1c7b76be | 435 | memset(branch_head, 0, sizeof(branch_head)); |
ae8e4c9c | 436 | remote_head = get_merge_parent(remote); |
1c7b76be | 437 | if (!remote_head) |
bacec478 | 438 | die(_("'%s' does not point to a commit"), remote); |
1c7b76be | 439 | |
751c5974 JK |
440 | if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) { |
441 | if (!prefixcmp(found_ref, "refs/heads/")) { | |
442 | strbuf_addf(msg, "%s\t\tbranch '%s' of .\n", | |
443 | sha1_to_hex(branch_head), remote); | |
444 | goto cleanup; | |
445 | } | |
57b58db7 JH |
446 | if (!prefixcmp(found_ref, "refs/tags/")) { |
447 | strbuf_addf(msg, "%s\t\ttag '%s' of .\n", | |
448 | sha1_to_hex(branch_head), remote); | |
449 | goto cleanup; | |
450 | } | |
69a8b7c7 | 451 | if (!prefixcmp(found_ref, "refs/remotes/")) { |
13931236 | 452 | strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n", |
69a8b7c7 JK |
453 | sha1_to_hex(branch_head), remote); |
454 | goto cleanup; | |
455 | } | |
1c7b76be MV |
456 | } |
457 | ||
458 | /* See if remote matches <name>^^^.. or <name>~<number> */ | |
459 | for (len = 0, ptr = remote + strlen(remote); | |
460 | remote < ptr && ptr[-1] == '^'; | |
461 | ptr--) | |
462 | len++; | |
463 | if (len) | |
464 | early = 1; | |
465 | else { | |
466 | early = 0; | |
467 | ptr = strrchr(remote, '~'); | |
468 | if (ptr) { | |
469 | int seen_nonzero = 0; | |
470 | ||
471 | len++; /* count ~ */ | |
472 | while (*++ptr && isdigit(*ptr)) { | |
473 | seen_nonzero |= (*ptr != '0'); | |
474 | len++; | |
475 | } | |
476 | if (*ptr) | |
477 | len = 0; /* not ...~<number> */ | |
478 | else if (seen_nonzero) | |
479 | early = 1; | |
480 | else if (len == 1) | |
481 | early = 1; /* "name~" is "name~1"! */ | |
482 | } | |
483 | } | |
484 | if (len) { | |
485 | struct strbuf truname = STRBUF_INIT; | |
486 | strbuf_addstr(&truname, "refs/heads/"); | |
487 | strbuf_addstr(&truname, remote); | |
9b6bf4d5 | 488 | strbuf_setlen(&truname, truname.len - len); |
c6893323 | 489 | if (ref_exists(truname.buf)) { |
1c7b76be MV |
490 | strbuf_addf(msg, |
491 | "%s\t\tbranch '%s'%s of .\n", | |
ae8e4c9c | 492 | sha1_to_hex(remote_head->object.sha1), |
9b6bf4d5 | 493 | truname.buf + 11, |
1c7b76be | 494 | (early ? " (early part)" : "")); |
c9717ee9 JH |
495 | strbuf_release(&truname); |
496 | goto cleanup; | |
1c7b76be MV |
497 | } |
498 | } | |
499 | ||
500 | if (!strcmp(remote, "FETCH_HEAD") && | |
501 | !access(git_path("FETCH_HEAD"), R_OK)) { | |
418c9b17 | 502 | const char *filename; |
1c7b76be | 503 | FILE *fp; |
f285a2d7 | 504 | struct strbuf line = STRBUF_INIT; |
1c7b76be MV |
505 | char *ptr; |
506 | ||
418c9b17 JN |
507 | filename = git_path("FETCH_HEAD"); |
508 | fp = fopen(filename, "r"); | |
1c7b76be | 509 | if (!fp) |
bacec478 | 510 | die_errno(_("could not open '%s' for reading"), |
418c9b17 | 511 | filename); |
1c7b76be MV |
512 | strbuf_getline(&line, fp, '\n'); |
513 | fclose(fp); | |
514 | ptr = strstr(line.buf, "\tnot-for-merge\t"); | |
515 | if (ptr) | |
516 | strbuf_remove(&line, ptr-line.buf+1, 13); | |
517 | strbuf_addbuf(msg, &line); | |
518 | strbuf_release(&line); | |
c9717ee9 | 519 | goto cleanup; |
1c7b76be | 520 | } |
2d1495fe JH |
521 | |
522 | if (remote_head->util) { | |
523 | struct merge_remote_desc *desc; | |
524 | desc = merge_remote_util(remote_head); | |
525 | if (desc && desc->obj && desc->obj->type == OBJ_TAG) { | |
526 | strbuf_addf(msg, "%s\t\t%s '%s'\n", | |
527 | sha1_to_hex(desc->obj->sha1), | |
528 | typename(desc->obj->type), | |
529 | remote); | |
530 | goto cleanup; | |
531 | } | |
532 | } | |
533 | ||
1c7b76be | 534 | strbuf_addf(msg, "%s\t\tcommit '%s'\n", |
ae8e4c9c | 535 | sha1_to_hex(remote_head->object.sha1), remote); |
c9717ee9 JH |
536 | cleanup: |
537 | strbuf_release(&buf); | |
538 | strbuf_release(&bname); | |
1c7b76be MV |
539 | } |
540 | ||
0d8fc3ef JH |
541 | static void parse_branch_merge_options(char *bmo) |
542 | { | |
543 | const char **argv; | |
544 | int argc; | |
545 | ||
546 | if (!bmo) | |
547 | return; | |
548 | argc = split_cmdline(bmo, &argv); | |
549 | if (argc < 0) | |
c7fe5b61 JH |
550 | die(_("Bad branch.%s.mergeoptions string: %s"), branch, |
551 | split_cmdline_strerror(argc)); | |
0d8fc3ef JH |
552 | argv = xrealloc(argv, sizeof(*argv) * (argc + 2)); |
553 | memmove(argv + 1, argv, sizeof(*argv) * (argc + 1)); | |
554 | argc++; | |
555 | argv[0] = "branch.*.mergeoptions"; | |
556 | parse_options(argc, argv, NULL, builtin_merge_options, | |
557 | builtin_merge_usage, 0); | |
558 | free(argv); | |
559 | } | |
560 | ||
186458b1 | 561 | static int git_merge_config(const char *k, const char *v, void *cb) |
1c7b76be | 562 | { |
898eacd8 JH |
563 | int status; |
564 | ||
1c7b76be MV |
565 | if (branch && !prefixcmp(k, "branch.") && |
566 | !prefixcmp(k + 7, branch) && | |
567 | !strcmp(k + 7 + strlen(branch), ".mergeoptions")) { | |
0d8fc3ef JH |
568 | free(branch_mergeoptions); |
569 | branch_mergeoptions = xstrdup(v); | |
570 | return 0; | |
1c7b76be MV |
571 | } |
572 | ||
573 | if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat")) | |
574 | show_diffstat = git_config_bool(k, v); | |
575 | else if (!strcmp(k, "pull.twohead")) | |
576 | return git_config_string(&pull_twohead, k, v); | |
577 | else if (!strcmp(k, "pull.octopus")) | |
578 | return git_config_string(&pull_octopus, k, v); | |
7610fa57 JN |
579 | else if (!strcmp(k, "merge.renormalize")) |
580 | option_renormalize = git_config_bool(k, v); | |
898eacd8 | 581 | else if (!strcmp(k, "merge.ff")) { |
f23e8dec JH |
582 | int boolval = git_config_maybe_bool(k, v); |
583 | if (0 <= boolval) { | |
584 | allow_fast_forward = boolval; | |
585 | } else if (v && !strcmp(v, "only")) { | |
586 | allow_fast_forward = 1; | |
587 | fast_forward_only = 1; | |
588 | } /* do not barf on values from future versions of git */ | |
589 | return 0; | |
93e535a5 JH |
590 | } else if (!strcmp(k, "merge.defaulttoupstream")) { |
591 | default_to_upstream = git_config_bool(k, v); | |
592 | return 0; | |
96e9420c | 593 | } |
ba3c69a9 | 594 | |
898eacd8 | 595 | status = fmt_merge_msg_config(k, v, cb); |
5de89d3a JH |
596 | if (status) |
597 | return status; | |
ba3c69a9 | 598 | status = git_gpg_config(k, v, NULL); |
898eacd8 JH |
599 | if (status) |
600 | return status; | |
1c7b76be MV |
601 | return git_diff_ui_config(k, v, cb); |
602 | } | |
603 | ||
604 | static int read_tree_trivial(unsigned char *common, unsigned char *head, | |
605 | unsigned char *one) | |
606 | { | |
607 | int i, nr_trees = 0; | |
608 | struct tree *trees[MAX_UNPACK_TREES]; | |
609 | struct tree_desc t[MAX_UNPACK_TREES]; | |
610 | struct unpack_trees_options opts; | |
611 | ||
612 | memset(&opts, 0, sizeof(opts)); | |
613 | opts.head_idx = 2; | |
614 | opts.src_index = &the_index; | |
615 | opts.dst_index = &the_index; | |
616 | opts.update = 1; | |
617 | opts.verbose_update = 1; | |
618 | opts.trivial_merges_only = 1; | |
619 | opts.merge = 1; | |
620 | trees[nr_trees] = parse_tree_indirect(common); | |
621 | if (!trees[nr_trees++]) | |
622 | return -1; | |
623 | trees[nr_trees] = parse_tree_indirect(head); | |
624 | if (!trees[nr_trees++]) | |
625 | return -1; | |
626 | trees[nr_trees] = parse_tree_indirect(one); | |
627 | if (!trees[nr_trees++]) | |
628 | return -1; | |
629 | opts.fn = threeway_merge; | |
630 | cache_tree_free(&active_cache_tree); | |
631 | for (i = 0; i < nr_trees; i++) { | |
632 | parse_tree(trees[i]); | |
633 | init_tree_desc(t+i, trees[i]->buffer, trees[i]->size); | |
634 | } | |
635 | if (unpack_trees(nr_trees, t, &opts)) | |
636 | return -1; | |
637 | return 0; | |
638 | } | |
639 | ||
640 | static void write_tree_trivial(unsigned char *sha1) | |
641 | { | |
642 | if (write_cache_as_tree(sha1, 0, NULL)) | |
bacec478 | 643 | die(_("git write-tree failed to write a tree")); |
1c7b76be MV |
644 | } |
645 | ||
3f9083cd | 646 | static int try_merge_strategy(const char *strategy, struct commit_list *common, |
4c57bd27 | 647 | struct commit_list *remoteheads, |
894642f6 | 648 | struct commit *head, const char *head_arg) |
3f9083cd | 649 | { |
668f26ff MV |
650 | int index_fd; |
651 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
652 | ||
653 | index_fd = hold_locked_index(lock, 1); | |
654 | refresh_cache(REFRESH_QUIET); | |
655 | if (active_cache_changed && | |
656 | (write_cache(index_fd, active_cache, active_nr) || | |
657 | commit_locked_index(lock))) | |
bacec478 | 658 | return error(_("Unable to write index.")); |
668f26ff | 659 | rollback_lock_file(lock); |
1c7b76be | 660 | |
18668f53 | 661 | if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) { |
3f9083cd | 662 | int clean, x; |
18668f53 MV |
663 | struct commit *result; |
664 | struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); | |
665 | int index_fd; | |
666 | struct commit_list *reversed = NULL; | |
667 | struct merge_options o; | |
3f9083cd | 668 | struct commit_list *j; |
18668f53 MV |
669 | |
670 | if (remoteheads->next) { | |
bacec478 | 671 | error(_("Not handling anything other than two heads merge.")); |
18668f53 MV |
672 | return 2; |
673 | } | |
674 | ||
675 | init_merge_options(&o); | |
676 | if (!strcmp(strategy, "subtree")) | |
85e51b78 | 677 | o.subtree_shift = ""; |
8cc5b290 | 678 | |
7610fa57 | 679 | o.renormalize = option_renormalize; |
99bfc669 JK |
680 | o.show_rename_progress = |
681 | show_progress == -1 ? isatty(2) : show_progress; | |
7610fa57 | 682 | |
635a7bb1 JN |
683 | for (x = 0; x < xopts_nr; x++) |
684 | if (parse_merge_opt(&o, xopts[x])) | |
bacec478 | 685 | die(_("Unknown option for merge-recursive: -X%s"), xopts[x]); |
18668f53 MV |
686 | |
687 | o.branch1 = head_arg; | |
ae8e4c9c | 688 | o.branch2 = merge_remote_util(remoteheads->item)->name; |
18668f53 MV |
689 | |
690 | for (j = common; j; j = j->next) | |
691 | commit_list_insert(j->item, &reversed); | |
692 | ||
693 | index_fd = hold_locked_index(lock, 1); | |
894642f6 | 694 | clean = merge_recursive(&o, head, |
18668f53 MV |
695 | remoteheads->item, reversed, &result); |
696 | if (active_cache_changed && | |
697 | (write_cache(index_fd, active_cache, active_nr) || | |
698 | commit_locked_index(lock))) | |
bacec478 | 699 | die (_("unable to write %s"), get_index_file()); |
42716660 | 700 | rollback_lock_file(lock); |
18668f53 MV |
701 | return clean ? 0 : 1; |
702 | } else { | |
67ac1e1d JN |
703 | return try_merge_command(strategy, xopts_nr, xopts, |
704 | common, head_arg, remoteheads); | |
18668f53 | 705 | } |
1c7b76be MV |
706 | } |
707 | ||
708 | static void count_diff_files(struct diff_queue_struct *q, | |
709 | struct diff_options *opt, void *data) | |
710 | { | |
711 | int *count = data; | |
712 | ||
713 | (*count) += q->nr; | |
714 | } | |
715 | ||
716 | static int count_unmerged_entries(void) | |
717 | { | |
1c7b76be MV |
718 | int i, ret = 0; |
719 | ||
be6ff819 JH |
720 | for (i = 0; i < active_nr; i++) |
721 | if (ce_stage(active_cache[i])) | |
1c7b76be MV |
722 | ret++; |
723 | ||
724 | return ret; | |
725 | } | |
726 | ||
1c7b76be MV |
727 | static void split_merge_strategies(const char *string, struct strategy **list, |
728 | int *nr, int *alloc) | |
729 | { | |
730 | char *p, *q, *buf; | |
731 | ||
732 | if (!string) | |
733 | return; | |
734 | ||
735 | buf = xstrdup(string); | |
736 | q = buf; | |
737 | for (;;) { | |
738 | p = strchr(q, ' '); | |
739 | if (!p) { | |
740 | ALLOC_GROW(*list, *nr + 1, *alloc); | |
741 | (*list)[(*nr)++].name = xstrdup(q); | |
742 | free(buf); | |
743 | return; | |
744 | } else { | |
745 | *p = '\0'; | |
746 | ALLOC_GROW(*list, *nr + 1, *alloc); | |
747 | (*list)[(*nr)++].name = xstrdup(q); | |
748 | q = ++p; | |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | static void add_strategies(const char *string, unsigned attr) | |
754 | { | |
755 | struct strategy *list = NULL; | |
756 | int list_alloc = 0, list_nr = 0, i; | |
757 | ||
758 | memset(&list, 0, sizeof(list)); | |
759 | split_merge_strategies(string, &list, &list_nr, &list_alloc); | |
1719b5e4 MV |
760 | if (list) { |
761 | for (i = 0; i < list_nr; i++) | |
762 | append_strategy(get_strategy(list[i].name)); | |
1c7b76be MV |
763 | return; |
764 | } | |
765 | for (i = 0; i < ARRAY_SIZE(all_strategy); i++) | |
766 | if (all_strategy[i].attr & attr) | |
767 | append_strategy(&all_strategy[i]); | |
768 | ||
769 | } | |
770 | ||
66f4b98a | 771 | static void write_merge_msg(struct strbuf *msg) |
65969d43 | 772 | { |
418c9b17 JN |
773 | const char *filename = git_path("MERGE_MSG"); |
774 | int fd = open(filename, O_WRONLY | O_CREAT, 0666); | |
65969d43 | 775 | if (fd < 0) |
6c80cd29 | 776 | die_errno(_("Could not open '%s' for writing"), |
418c9b17 | 777 | filename); |
66f4b98a | 778 | if (write_in_full(fd, msg->buf, msg->len) != msg->len) |
418c9b17 | 779 | die_errno(_("Could not write to '%s'"), filename); |
65969d43 JS |
780 | close(fd); |
781 | } | |
782 | ||
66f4b98a | 783 | static void read_merge_msg(struct strbuf *msg) |
65969d43 | 784 | { |
418c9b17 | 785 | const char *filename = git_path("MERGE_MSG"); |
66f4b98a | 786 | strbuf_reset(msg); |
418c9b17 JN |
787 | if (strbuf_read_file(msg, filename, 0) < 0) |
788 | die_errno(_("Could not read from '%s'"), filename); | |
65969d43 JS |
789 | } |
790 | ||
4c57bd27 JH |
791 | static void write_merge_state(struct commit_list *); |
792 | static void abort_commit(struct commit_list *remoteheads, const char *err_msg) | |
65969d43 | 793 | { |
66f4b98a JS |
794 | if (err_msg) |
795 | error("%s", err_msg); | |
796 | fprintf(stderr, | |
797 | _("Not committing merge; use 'git commit' to complete the merge.\n")); | |
4c57bd27 | 798 | write_merge_state(remoteheads); |
66f4b98a JS |
799 | exit(1); |
800 | } | |
801 | ||
f26af3fc TR |
802 | static const char merge_editor_comment[] = |
803 | N_("Please enter a commit message to explain why this merge is necessary,\n" | |
804 | "especially if it merges an updated upstream into a topic branch.\n" | |
805 | "\n" | |
eff80a9f | 806 | "Lines starting with '%c' will be ignored, and an empty message aborts\n" |
f26af3fc TR |
807 | "the commit.\n"); |
808 | ||
4c57bd27 | 809 | static void prepare_to_commit(struct commit_list *remoteheads) |
66f4b98a JS |
810 | { |
811 | struct strbuf msg = STRBUF_INIT; | |
812 | strbuf_addbuf(&msg, &merge_msg); | |
813 | strbuf_addch(&msg, '\n'); | |
f26af3fc | 814 | if (0 < option_edit) |
eff80a9f | 815 | strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char); |
66f4b98a | 816 | write_merge_msg(&msg); |
3e4141d0 AP |
817 | if (run_hook(get_index_file(), "prepare-commit-msg", |
818 | git_path("MERGE_MSG"), "merge", NULL, NULL)) | |
819 | abort_commit(remoteheads, NULL); | |
f8246281 | 820 | if (0 < option_edit) { |
66f4b98a | 821 | if (launch_editor(git_path("MERGE_MSG"), NULL, NULL)) |
4c57bd27 | 822 | abort_commit(remoteheads, NULL); |
66f4b98a JS |
823 | } |
824 | read_merge_msg(&msg); | |
f8246281 | 825 | stripspace(&msg, 0 < option_edit); |
66f4b98a | 826 | if (!msg.len) |
4c57bd27 | 827 | abort_commit(remoteheads, _("Empty commit message.")); |
66f4b98a JS |
828 | strbuf_release(&merge_msg); |
829 | strbuf_addbuf(&merge_msg, &msg); | |
830 | strbuf_release(&msg); | |
65969d43 JS |
831 | } |
832 | ||
4c57bd27 | 833 | static int merge_trivial(struct commit *head, struct commit_list *remoteheads) |
1c7b76be MV |
834 | { |
835 | unsigned char result_tree[20], result_commit[20]; | |
36e40535 | 836 | struct commit_list *parent = xmalloc(sizeof(*parent)); |
1c7b76be MV |
837 | |
838 | write_tree_trivial(result_tree); | |
157efde1 | 839 | printf(_("Wonderful.\n")); |
894642f6 | 840 | parent->item = head; |
36e40535 | 841 | parent->next = xmalloc(sizeof(*parent->next)); |
446247db JH |
842 | parent->next->item = remoteheads->item; |
843 | parent->next->next = NULL; | |
4c57bd27 | 844 | prepare_to_commit(remoteheads); |
5de89d3a JH |
845 | if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL, |
846 | sign_commit)) | |
6b3c4c05 | 847 | die(_("failed to write commit object")); |
4c57bd27 | 848 | finish(head, remoteheads, result_commit, "In-index merge"); |
1c7b76be MV |
849 | drop_save(); |
850 | return 0; | |
851 | } | |
852 | ||
894642f6 | 853 | static int finish_automerge(struct commit *head, |
e78cbf8c | 854 | int head_subsumed, |
894642f6 | 855 | struct commit_list *common, |
4c57bd27 | 856 | struct commit_list *remoteheads, |
1c7b76be MV |
857 | unsigned char *result_tree, |
858 | const char *wt_strategy) | |
859 | { | |
e78cbf8c | 860 | struct commit_list *parents = NULL; |
1c7b76be MV |
861 | struct strbuf buf = STRBUF_INIT; |
862 | unsigned char result_commit[20]; | |
863 | ||
864 | free_commit_list(common); | |
e78cbf8c JH |
865 | parents = remoteheads; |
866 | if (!head_subsumed || !allow_fast_forward) | |
894642f6 | 867 | commit_list_insert(head, &parents); |
1c7b76be | 868 | strbuf_addch(&merge_msg, '\n'); |
4c57bd27 | 869 | prepare_to_commit(remoteheads); |
5de89d3a JH |
870 | if (commit_tree(&merge_msg, result_tree, parents, result_commit, |
871 | NULL, sign_commit)) | |
6b3c4c05 | 872 | die(_("failed to write commit object")); |
f23101bf | 873 | strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); |
4c57bd27 | 874 | finish(head, remoteheads, result_commit, buf.buf); |
1c7b76be MV |
875 | strbuf_release(&buf); |
876 | drop_save(); | |
877 | return 0; | |
878 | } | |
879 | ||
7610fa57 | 880 | static int suggest_conflicts(int renormalizing) |
1c7b76be | 881 | { |
418c9b17 | 882 | const char *filename; |
1c7b76be MV |
883 | FILE *fp; |
884 | int pos; | |
885 | ||
418c9b17 JN |
886 | filename = git_path("MERGE_MSG"); |
887 | fp = fopen(filename, "a"); | |
1c7b76be | 888 | if (!fp) |
418c9b17 | 889 | die_errno(_("Could not open '%s' for writing"), filename); |
1c7b76be MV |
890 | fprintf(fp, "\nConflicts:\n"); |
891 | for (pos = 0; pos < active_nr; pos++) { | |
892 | struct cache_entry *ce = active_cache[pos]; | |
893 | ||
894 | if (ce_stage(ce)) { | |
895 | fprintf(fp, "\t%s\n", ce->name); | |
896 | while (pos + 1 < active_nr && | |
897 | !strcmp(ce->name, | |
898 | active_cache[pos + 1]->name)) | |
899 | pos++; | |
900 | } | |
901 | } | |
902 | fclose(fp); | |
cb6020bb | 903 | rerere(allow_rerere_auto); |
bacec478 ÆAB |
904 | printf(_("Automatic merge failed; " |
905 | "fix conflicts and then commit the result.\n")); | |
1c7b76be MV |
906 | return 1; |
907 | } | |
908 | ||
894642f6 NTND |
909 | static struct commit *is_old_style_invocation(int argc, const char **argv, |
910 | const unsigned char *head) | |
1c7b76be MV |
911 | { |
912 | struct commit *second_token = NULL; | |
76bf488e | 913 | if (argc > 2) { |
1c7b76be MV |
914 | unsigned char second_sha1[20]; |
915 | ||
916 | if (get_sha1(argv[1], second_sha1)) | |
917 | return NULL; | |
918 | second_token = lookup_commit_reference_gently(second_sha1, 0); | |
919 | if (!second_token) | |
bacec478 | 920 | die(_("'%s' is not a commit"), argv[1]); |
1c7b76be MV |
921 | if (hashcmp(second_token->object.sha1, head)) |
922 | return NULL; | |
923 | } | |
924 | return second_token; | |
925 | } | |
926 | ||
927 | static int evaluate_result(void) | |
928 | { | |
929 | int cnt = 0; | |
930 | struct rev_info rev; | |
931 | ||
1c7b76be MV |
932 | /* Check how many files differ. */ |
933 | init_revisions(&rev, ""); | |
934 | setup_revisions(0, NULL, &rev, NULL); | |
935 | rev.diffopt.output_format |= | |
936 | DIFF_FORMAT_CALLBACK; | |
937 | rev.diffopt.format_callback = count_diff_files; | |
938 | rev.diffopt.format_callback_data = &cnt; | |
939 | run_diff_files(&rev, 0); | |
940 | ||
941 | /* | |
942 | * Check how many unmerged entries are | |
943 | * there. | |
944 | */ | |
945 | cnt += count_unmerged_entries(); | |
946 | ||
947 | return cnt; | |
948 | } | |
949 | ||
93e535a5 JH |
950 | /* |
951 | * Pretend as if the user told us to merge with the tracking | |
952 | * branch we have for the upstream of the current branch | |
953 | */ | |
954 | static int setup_with_upstream(const char ***argv) | |
955 | { | |
956 | struct branch *branch = branch_get(NULL); | |
957 | int i; | |
958 | const char **args; | |
959 | ||
960 | if (!branch) | |
c7f426d4 | 961 | die(_("No current branch.")); |
93e535a5 | 962 | if (!branch->remote) |
c7f426d4 | 963 | die(_("No remote for the current branch.")); |
93e535a5 | 964 | if (!branch->merge_nr) |
c7f426d4 | 965 | die(_("No default upstream defined for the current branch.")); |
93e535a5 JH |
966 | |
967 | args = xcalloc(branch->merge_nr + 1, sizeof(char *)); | |
968 | for (i = 0; i < branch->merge_nr; i++) { | |
969 | if (!branch->merge[i]->dst) | |
c7f426d4 | 970 | die(_("No remote tracking branch for %s from %s"), |
93e535a5 JH |
971 | branch->merge[i]->src, branch->remote_name); |
972 | args[i] = branch->merge[i]->dst; | |
973 | } | |
974 | args[i] = NULL; | |
975 | *argv = args; | |
976 | return i; | |
977 | } | |
978 | ||
4c57bd27 | 979 | static void write_merge_state(struct commit_list *remoteheads) |
66f4b98a | 980 | { |
418c9b17 | 981 | const char *filename; |
66f4b98a JS |
982 | int fd; |
983 | struct commit_list *j; | |
984 | struct strbuf buf = STRBUF_INIT; | |
985 | ||
274a5c06 JH |
986 | for (j = remoteheads; j; j = j->next) { |
987 | unsigned const char *sha1; | |
988 | struct commit *c = j->item; | |
989 | if (c->util && merge_remote_util(c)->obj) { | |
990 | sha1 = merge_remote_util(c)->obj->sha1; | |
991 | } else { | |
992 | sha1 = c->object.sha1; | |
993 | } | |
994 | strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1)); | |
995 | } | |
418c9b17 JN |
996 | filename = git_path("MERGE_HEAD"); |
997 | fd = open(filename, O_WRONLY | O_CREAT, 0666); | |
66f4b98a | 998 | if (fd < 0) |
418c9b17 | 999 | die_errno(_("Could not open '%s' for writing"), filename); |
66f4b98a | 1000 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) |
418c9b17 | 1001 | die_errno(_("Could not write to '%s'"), filename); |
66f4b98a JS |
1002 | close(fd); |
1003 | strbuf_addch(&merge_msg, '\n'); | |
1004 | write_merge_msg(&merge_msg); | |
418c9b17 JN |
1005 | |
1006 | filename = git_path("MERGE_MODE"); | |
1007 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
66f4b98a | 1008 | if (fd < 0) |
418c9b17 | 1009 | die_errno(_("Could not open '%s' for writing"), filename); |
66f4b98a JS |
1010 | strbuf_reset(&buf); |
1011 | if (!allow_fast_forward) | |
1012 | strbuf_addf(&buf, "no-ff"); | |
1013 | if (write_in_full(fd, buf.buf, buf.len) != buf.len) | |
418c9b17 | 1014 | die_errno(_("Could not write to '%s'"), filename); |
66f4b98a JS |
1015 | close(fd); |
1016 | } | |
1017 | ||
f8246281 JH |
1018 | static int default_edit_option(void) |
1019 | { | |
1020 | static const char name[] = "GIT_MERGE_AUTOEDIT"; | |
1021 | const char *e = getenv(name); | |
1022 | struct stat st_stdin, st_stdout; | |
1023 | ||
1024 | if (have_message) | |
1025 | /* an explicit -m msg without --[no-]edit */ | |
1026 | return 0; | |
1027 | ||
1028 | if (e) { | |
1029 | int v = git_config_maybe_bool(name, e); | |
1030 | if (v < 0) | |
1031 | die("Bad value '%s' in environment '%s'", e, name); | |
1032 | return v; | |
1033 | } | |
1034 | ||
1035 | /* Use editor if stdin and stdout are the same and is a tty */ | |
1036 | return (!fstat(0, &st_stdin) && | |
1037 | !fstat(1, &st_stdout) && | |
d46f476c | 1038 | isatty(0) && isatty(1) && |
f8246281 JH |
1039 | st_stdin.st_dev == st_stdout.st_dev && |
1040 | st_stdin.st_ino == st_stdout.st_ino && | |
1041 | st_stdin.st_mode == st_stdout.st_mode); | |
1042 | } | |
1043 | ||
e78cbf8c JH |
1044 | static struct commit_list *collect_parents(struct commit *head_commit, |
1045 | int *head_subsumed, | |
1046 | int argc, const char **argv) | |
b5d887f9 JH |
1047 | { |
1048 | int i; | |
e78cbf8c | 1049 | struct commit_list *remoteheads = NULL, *parents, *next; |
b5d887f9 JH |
1050 | struct commit_list **remotes = &remoteheads; |
1051 | ||
e78cbf8c JH |
1052 | if (head_commit) |
1053 | remotes = &commit_list_insert(head_commit, remotes)->next; | |
b5d887f9 JH |
1054 | for (i = 0; i < argc; i++) { |
1055 | struct commit *commit = get_merge_parent(argv[i]); | |
1056 | if (!commit) | |
f3f8af0e VV |
1057 | help_unknown_ref(argv[i], "merge", |
1058 | "not something we can merge"); | |
b5d887f9 JH |
1059 | remotes = &commit_list_insert(commit, remotes)->next; |
1060 | } | |
1061 | *remotes = NULL; | |
e78cbf8c JH |
1062 | |
1063 | parents = reduce_heads(remoteheads); | |
1064 | ||
1065 | *head_subsumed = 1; /* we will flip this to 0 when we find it */ | |
1066 | for (remoteheads = NULL, remotes = &remoteheads; | |
1067 | parents; | |
1068 | parents = next) { | |
1069 | struct commit *commit = parents->item; | |
1070 | next = parents->next; | |
1071 | if (commit == head_commit) | |
1072 | *head_subsumed = 0; | |
1073 | else | |
1074 | remotes = &commit_list_insert(commit, remotes)->next; | |
1075 | } | |
b5d887f9 JH |
1076 | return remoteheads; |
1077 | } | |
f8246281 | 1078 | |
1c7b76be MV |
1079 | int cmd_merge(int argc, const char **argv, const char *prefix) |
1080 | { | |
1081 | unsigned char result_tree[20]; | |
b4fd9406 | 1082 | unsigned char stash[20]; |
894642f6 NTND |
1083 | unsigned char head_sha1[20]; |
1084 | struct commit *head_commit; | |
f285a2d7 | 1085 | struct strbuf buf = STRBUF_INIT; |
1c7b76be | 1086 | const char *head_arg; |
e78cbf8c | 1087 | int flag, i, ret = 0, head_subsumed; |
1c7b76be MV |
1088 | int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0; |
1089 | struct commit_list *common = NULL; | |
1090 | const char *best_strategy = NULL, *wt_strategy = NULL; | |
b5d887f9 | 1091 | struct commit_list *remoteheads, *p; |
96ec7b1e | 1092 | void *branch_to_free; |
1c7b76be | 1093 | |
da53eec6 NTND |
1094 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1095 | usage_with_options(builtin_merge_usage, builtin_merge_options); | |
1c7b76be MV |
1096 | |
1097 | /* | |
1098 | * Check if we are _not_ on a detached HEAD, i.e. if there is a | |
1099 | * current branch. | |
1100 | */ | |
96ec7b1e NTND |
1101 | branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag); |
1102 | if (branch && !prefixcmp(branch, "refs/heads/")) | |
1103 | branch += 11; | |
894642f6 NTND |
1104 | if (!branch || is_null_sha1(head_sha1)) |
1105 | head_commit = NULL; | |
baf18fc2 NTND |
1106 | else |
1107 | head_commit = lookup_commit_or_die(head_sha1, "HEAD"); | |
1c7b76be MV |
1108 | |
1109 | git_config(git_merge_config, NULL); | |
1110 | ||
0d8fc3ef JH |
1111 | if (branch_mergeoptions) |
1112 | parse_branch_merge_options(branch_mergeoptions); | |
37782920 | 1113 | argc = parse_options(argc, argv, prefix, builtin_merge_options, |
1c7b76be | 1114 | builtin_merge_usage, 0); |
898eacd8 JH |
1115 | if (shortlog_len < 0) |
1116 | shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; | |
2a22c1b3 | 1117 | |
99bfc669 JK |
1118 | if (verbosity < 0 && show_progress == -1) |
1119 | show_progress = 0; | |
1120 | ||
35d2fffd JH |
1121 | if (abort_current_merge) { |
1122 | int nargc = 2; | |
1123 | const char *nargv[] = {"reset", "--merge", NULL}; | |
1124 | ||
1125 | if (!file_exists(git_path("MERGE_HEAD"))) | |
bacec478 | 1126 | die(_("There is no merge to abort (MERGE_HEAD missing).")); |
35d2fffd JH |
1127 | |
1128 | /* Invoke 'git reset --merge' */ | |
d5a35c11 NTND |
1129 | ret = cmd_reset(nargc, nargv, prefix); |
1130 | goto done; | |
35d2fffd JH |
1131 | } |
1132 | ||
2a22c1b3 JH |
1133 | if (read_cache_unmerged()) |
1134 | die_resolve_conflict("merge"); | |
1135 | ||
1136 | if (file_exists(git_path("MERGE_HEAD"))) { | |
1137 | /* | |
1138 | * There is no unmerged entry, don't advise 'git | |
1139 | * add/rm <file>', just 'git commit'. | |
1140 | */ | |
1141 | if (advice_resolve_conflict) | |
bacec478 ÆAB |
1142 | die(_("You have not concluded your merge (MERGE_HEAD exists).\n" |
1143 | "Please, commit your changes before you can merge.")); | |
2a22c1b3 | 1144 | else |
bacec478 | 1145 | die(_("You have not concluded your merge (MERGE_HEAD exists).")); |
2a22c1b3 | 1146 | } |
d7e5c0cb JS |
1147 | if (file_exists(git_path("CHERRY_PICK_HEAD"))) { |
1148 | if (advice_resolve_conflict) | |
f68f1801 ÆAB |
1149 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" |
1150 | "Please, commit your changes before you can merge.")); | |
d7e5c0cb | 1151 | else |
f68f1801 | 1152 | die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).")); |
2a22c1b3 JH |
1153 | } |
1154 | resolve_undo_clear(); | |
1155 | ||
7f87aff2 TA |
1156 | if (verbosity < 0) |
1157 | show_diffstat = 0; | |
1c7b76be MV |
1158 | |
1159 | if (squash) { | |
1160 | if (!allow_fast_forward) | |
bacec478 | 1161 | die(_("You cannot combine --squash with --no-ff.")); |
1c7b76be MV |
1162 | option_commit = 0; |
1163 | } | |
1164 | ||
13474835 | 1165 | if (!allow_fast_forward && fast_forward_only) |
bacec478 | 1166 | die(_("You cannot combine --no-ff with --ff-only.")); |
13474835 | 1167 | |
4e8115ff | 1168 | if (!abort_current_merge) { |
5480207c VR |
1169 | if (!argc) { |
1170 | if (default_to_upstream) | |
1171 | argc = setup_with_upstream(&argv); | |
1172 | else | |
1173 | die(_("No commit specified and merge.defaultToUpstream not set.")); | |
1174 | } else if (argc == 1 && !strcmp(argv[0], "-")) | |
4e8115ff JH |
1175 | argv[0] = "@{-1}"; |
1176 | } | |
1c7b76be MV |
1177 | if (!argc) |
1178 | usage_with_options(builtin_merge_usage, | |
1179 | builtin_merge_options); | |
1180 | ||
1181 | /* | |
1182 | * This could be traditional "merge <msg> HEAD <commit>..." and | |
1183 | * the way we can tell it is to see if the second token is HEAD, | |
1184 | * but some people might have misused the interface and used a | |
1185 | * committish that is the same as HEAD there instead. | |
1186 | * Traditional format never would have "-m" so it is an | |
1187 | * additional safety measure to check for it. | |
1188 | */ | |
1c7b76be | 1189 | |
894642f6 NTND |
1190 | if (!have_message && head_commit && |
1191 | is_old_style_invocation(argc, argv, head_commit->object.sha1)) { | |
1c7b76be MV |
1192 | strbuf_addstr(&merge_msg, argv[0]); |
1193 | head_arg = argv[1]; | |
1194 | argv += 2; | |
1195 | argc -= 2; | |
e78cbf8c | 1196 | remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); |
894642f6 | 1197 | } else if (!head_commit) { |
ae8e4c9c | 1198 | struct commit *remote_head; |
1c7b76be MV |
1199 | /* |
1200 | * If the merged head is a valid one there is no reason | |
1201 | * to forbid "git merge" into a branch yet to be born. | |
1202 | * We do the same for "git pull". | |
1203 | */ | |
1204 | if (argc != 1) | |
bacec478 ÆAB |
1205 | die(_("Can merge only exactly one commit into " |
1206 | "empty head")); | |
4be636f4 | 1207 | if (squash) |
bacec478 | 1208 | die(_("Squash commit into empty head not supported yet")); |
4be636f4 | 1209 | if (!allow_fast_forward) |
bacec478 ÆAB |
1210 | die(_("Non-fast-forward commit does not make sense into " |
1211 | "an empty head")); | |
e78cbf8c | 1212 | remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); |
b5d887f9 | 1213 | remote_head = remoteheads->item; |
1c7b76be | 1214 | if (!remote_head) |
bacec478 | 1215 | die(_("%s - not something we can merge"), argv[0]); |
ae8e4c9c JH |
1216 | read_empty(remote_head->object.sha1, 0); |
1217 | update_ref("initial pull", "HEAD", remote_head->object.sha1, | |
1218 | NULL, 0, DIE_ON_ERR); | |
d5a35c11 | 1219 | goto done; |
1c7b76be | 1220 | } else { |
97d45bcb | 1221 | struct strbuf merge_names = STRBUF_INIT; |
1c7b76be MV |
1222 | |
1223 | /* We are invoked directly as the first-class UI. */ | |
1224 | head_arg = "HEAD"; | |
1225 | ||
1226 | /* | |
ae8e4c9c JH |
1227 | * All the rest are the commits being merged; prepare |
1228 | * the standard merge summary message to be appended | |
1229 | * to the given message. | |
1c7b76be | 1230 | */ |
e78cbf8c | 1231 | remoteheads = collect_parents(head_commit, &head_subsumed, argc, argv); |
b5d887f9 JH |
1232 | for (p = remoteheads; p; p = p->next) |
1233 | merge_name(merge_remote_util(p->item)->name, &merge_names); | |
f0ecac2b | 1234 | |
96e9420c | 1235 | if (!have_message || shortlog_len) { |
cbda121c JH |
1236 | struct fmt_merge_msg_opts opts; |
1237 | memset(&opts, 0, sizeof(opts)); | |
1238 | opts.add_title = !have_message; | |
1239 | opts.shortlog_len = shortlog_len; | |
9bcbb1c2 | 1240 | opts.credit_people = (0 < option_edit); |
cbda121c JH |
1241 | |
1242 | fmt_merge_msg(&merge_names, &merge_msg, &opts); | |
1876166a RR |
1243 | if (merge_msg.len) |
1244 | strbuf_setlen(&merge_msg, merge_msg.len - 1); | |
1245 | } | |
1c7b76be MV |
1246 | } |
1247 | ||
894642f6 | 1248 | if (!head_commit || !argc) |
1c7b76be MV |
1249 | usage_with_options(builtin_merge_usage, |
1250 | builtin_merge_options); | |
1251 | ||
efed0022 SG |
1252 | if (verify_signatures) { |
1253 | for (p = remoteheads; p; p = p->next) { | |
1254 | struct commit *commit = p->item; | |
1255 | char hex[41]; | |
1256 | struct signature_check signature_check; | |
1257 | memset(&signature_check, 0, sizeof(signature_check)); | |
1258 | ||
1259 | check_commit_signature(commit, &signature_check); | |
1260 | ||
1261 | strcpy(hex, find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); | |
1262 | switch (signature_check.result) { | |
1263 | case 'G': | |
1264 | break; | |
eb307ae7 SG |
1265 | case 'U': |
1266 | die(_("Commit %s has an untrusted GPG signature, " | |
1267 | "allegedly by %s."), hex, signature_check.signer); | |
efed0022 SG |
1268 | case 'B': |
1269 | die(_("Commit %s has a bad GPG signature " | |
1270 | "allegedly by %s."), hex, signature_check.signer); | |
1271 | default: /* 'N' */ | |
1272 | die(_("Commit %s does not have a GPG signature."), hex); | |
1273 | } | |
1274 | if (verbosity >= 0 && signature_check.result == 'G') | |
1275 | printf(_("Commit %s has a good GPG signature by %s\n"), | |
1276 | hex, signature_check.signer); | |
1277 | ||
1278 | free(signature_check.gpg_output); | |
1279 | free(signature_check.gpg_status); | |
1280 | free(signature_check.signer); | |
1281 | free(signature_check.key); | |
1282 | } | |
1283 | } | |
1284 | ||
1c7b76be | 1285 | strbuf_addstr(&buf, "merge"); |
b5d887f9 JH |
1286 | for (p = remoteheads; p; p = p->next) |
1287 | strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name); | |
1c7b76be MV |
1288 | setenv("GIT_REFLOG_ACTION", buf.buf, 0); |
1289 | strbuf_reset(&buf); | |
1290 | ||
b5d887f9 JH |
1291 | for (p = remoteheads; p; p = p->next) { |
1292 | struct commit *commit = p->item; | |
ae8e4c9c JH |
1293 | strbuf_addf(&buf, "GITHEAD_%s", |
1294 | sha1_to_hex(commit->object.sha1)); | |
b5d887f9 | 1295 | setenv(buf.buf, merge_remote_util(commit)->name, 1); |
1c7b76be | 1296 | strbuf_reset(&buf); |
b5c9f1c1 JH |
1297 | if (!fast_forward_only && |
1298 | merge_remote_util(commit) && | |
fab47d05 | 1299 | merge_remote_util(commit)->obj && |
d82829b6 | 1300 | merge_remote_util(commit)->obj->type == OBJ_TAG) |
fab47d05 | 1301 | allow_fast_forward = 0; |
1c7b76be MV |
1302 | } |
1303 | ||
f8246281 JH |
1304 | if (option_edit < 0) |
1305 | option_edit = default_edit_option(); | |
1306 | ||
1c7b76be | 1307 | if (!use_strategies) { |
e78cbf8c JH |
1308 | if (!remoteheads) |
1309 | ; /* already up-to-date */ | |
1310 | else if (!remoteheads->next) | |
1c7b76be MV |
1311 | add_strategies(pull_twohead, DEFAULT_TWOHEAD); |
1312 | else | |
1313 | add_strategies(pull_octopus, DEFAULT_OCTOPUS); | |
1314 | } | |
1315 | ||
1316 | for (i = 0; i < use_strategies_nr; i++) { | |
1317 | if (use_strategies[i]->attr & NO_FAST_FORWARD) | |
1318 | allow_fast_forward = 0; | |
1319 | if (use_strategies[i]->attr & NO_TRIVIAL) | |
1320 | allow_trivial = 0; | |
1321 | } | |
1322 | ||
e78cbf8c JH |
1323 | if (!remoteheads) |
1324 | ; /* already up-to-date */ | |
1325 | else if (!remoteheads->next) | |
894642f6 | 1326 | common = get_merge_bases(head_commit, remoteheads->item, 1); |
1c7b76be MV |
1327 | else { |
1328 | struct commit_list *list = remoteheads; | |
894642f6 | 1329 | commit_list_insert(head_commit, &list); |
1c7b76be MV |
1330 | common = get_octopus_merge_bases(list); |
1331 | free(list); | |
1332 | } | |
1333 | ||
894642f6 NTND |
1334 | update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1, |
1335 | NULL, 0, DIE_ON_ERR); | |
1c7b76be | 1336 | |
e78cbf8c | 1337 | if (remoteheads && !common) |
1c7b76be | 1338 | ; /* No common ancestors found. We need a real merge. */ |
e78cbf8c JH |
1339 | else if (!remoteheads || |
1340 | (!remoteheads->next && !common->next && | |
1341 | common->item == remoteheads->item)) { | |
1c7b76be MV |
1342 | /* |
1343 | * If head can reach all the merge then we are up to date. | |
1344 | * but first the most common case of merging one remote. | |
1345 | */ | |
1346 | finish_up_to_date("Already up-to-date."); | |
d5a35c11 | 1347 | goto done; |
1c7b76be MV |
1348 | } else if (allow_fast_forward && !remoteheads->next && |
1349 | !common->next && | |
894642f6 | 1350 | !hashcmp(common->item->object.sha1, head_commit->object.sha1)) { |
1c7b76be | 1351 | /* Again the most common case of merging one remote. */ |
f285a2d7 | 1352 | struct strbuf msg = STRBUF_INIT; |
ae8e4c9c | 1353 | struct commit *commit; |
1c7b76be MV |
1354 | char hex[41]; |
1355 | ||
894642f6 | 1356 | strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV)); |
1c7b76be | 1357 | |
7f87aff2 | 1358 | if (verbosity >= 0) |
2ceb61e0 | 1359 | printf(_("Updating %s..%s\n"), |
7f87aff2 TA |
1360 | hex, |
1361 | find_unique_abbrev(remoteheads->item->object.sha1, | |
1362 | DEFAULT_ABBREV)); | |
a75d7b54 | 1363 | strbuf_addstr(&msg, "Fast-forward"); |
1c7b76be MV |
1364 | if (have_message) |
1365 | strbuf_addstr(&msg, | |
1366 | " (no commit created; -m option ignored)"); | |
ae8e4c9c | 1367 | commit = remoteheads->item; |
b7f7c079 | 1368 | if (!commit) { |
d5a35c11 NTND |
1369 | ret = 1; |
1370 | goto done; | |
1371 | } | |
1c7b76be | 1372 | |
ae8e4c9c | 1373 | if (checkout_fast_forward(head_commit->object.sha1, |
db699a8a NTND |
1374 | commit->object.sha1, |
1375 | overwrite_ignore)) { | |
d5a35c11 NTND |
1376 | ret = 1; |
1377 | goto done; | |
1378 | } | |
1c7b76be | 1379 | |
4c57bd27 | 1380 | finish(head_commit, remoteheads, commit->object.sha1, msg.buf); |
1c7b76be | 1381 | drop_save(); |
d5a35c11 | 1382 | goto done; |
1c7b76be MV |
1383 | } else if (!remoteheads->next && common->next) |
1384 | ; | |
1385 | /* | |
a75d7b54 | 1386 | * We are not doing octopus and not fast-forward. Need |
1c7b76be MV |
1387 | * a real merge. |
1388 | */ | |
1389 | else if (!remoteheads->next && !common->next && option_commit) { | |
1390 | /* | |
a75d7b54 | 1391 | * We are not doing octopus, not fast-forward, and have |
1c7b76be MV |
1392 | * only one common. |
1393 | */ | |
1394 | refresh_cache(REFRESH_QUIET); | |
13474835 | 1395 | if (allow_trivial && !fast_forward_only) { |
1c7b76be | 1396 | /* See if it is really trivial. */ |
f9bc573f | 1397 | git_committer_info(IDENT_STRICT); |
bacec478 | 1398 | printf(_("Trying really trivial in-index merge...\n")); |
1c7b76be | 1399 | if (!read_tree_trivial(common->item->object.sha1, |
d5a35c11 NTND |
1400 | head_commit->object.sha1, |
1401 | remoteheads->item->object.sha1)) { | |
4c57bd27 | 1402 | ret = merge_trivial(head_commit, remoteheads); |
d5a35c11 NTND |
1403 | goto done; |
1404 | } | |
bacec478 | 1405 | printf(_("Nope.\n")); |
1c7b76be MV |
1406 | } |
1407 | } else { | |
1408 | /* | |
1409 | * An octopus. If we can reach all the remote we are up | |
1410 | * to date. | |
1411 | */ | |
1412 | int up_to_date = 1; | |
1413 | struct commit_list *j; | |
1414 | ||
1415 | for (j = remoteheads; j; j = j->next) { | |
1416 | struct commit_list *common_one; | |
1417 | ||
1418 | /* | |
1419 | * Here we *have* to calculate the individual | |
1420 | * merge_bases again, otherwise "git merge HEAD^ | |
1421 | * HEAD^^" would be missed. | |
1422 | */ | |
894642f6 | 1423 | common_one = get_merge_bases(head_commit, j->item, 1); |
1c7b76be MV |
1424 | if (hashcmp(common_one->item->object.sha1, |
1425 | j->item->object.sha1)) { | |
1426 | up_to_date = 0; | |
1427 | break; | |
1428 | } | |
1429 | } | |
1430 | if (up_to_date) { | |
1431 | finish_up_to_date("Already up-to-date. Yeeah!"); | |
d5a35c11 | 1432 | goto done; |
1c7b76be MV |
1433 | } |
1434 | } | |
1435 | ||
13474835 | 1436 | if (fast_forward_only) |
bacec478 | 1437 | die(_("Not possible to fast-forward, aborting.")); |
13474835 | 1438 | |
1c7b76be | 1439 | /* We are going to make a new commit. */ |
f9bc573f | 1440 | git_committer_info(IDENT_STRICT); |
1c7b76be MV |
1441 | |
1442 | /* | |
1443 | * At this point, we need a real merge. No matter what strategy | |
1444 | * we use, it would operate on the index, possibly affecting the | |
1445 | * working tree, and when resolved cleanly, have the desired | |
1446 | * tree in the index -- this means that the index must be in | |
1447 | * sync with the head commit. The strategies are responsible | |
1448 | * to ensure this. | |
1449 | */ | |
b4fd9406 NTND |
1450 | if (use_strategies_nr == 1 || |
1451 | /* | |
1452 | * Stash away the local changes so that we can try more than one. | |
1453 | */ | |
1454 | save_state(stash)) | |
1455 | hashcpy(stash, null_sha1); | |
1c7b76be MV |
1456 | |
1457 | for (i = 0; i < use_strategies_nr; i++) { | |
1458 | int ret; | |
1459 | if (i) { | |
bacec478 | 1460 | printf(_("Rewinding the tree to pristine...\n")); |
894642f6 | 1461 | restore_state(head_commit->object.sha1, stash); |
1c7b76be MV |
1462 | } |
1463 | if (use_strategies_nr != 1) | |
bacec478 | 1464 | printf(_("Trying merge strategy %s...\n"), |
1c7b76be MV |
1465 | use_strategies[i]->name); |
1466 | /* | |
1467 | * Remember which strategy left the state in the working | |
1468 | * tree. | |
1469 | */ | |
1470 | wt_strategy = use_strategies[i]->name; | |
1471 | ||
1472 | ret = try_merge_strategy(use_strategies[i]->name, | |
4c57bd27 JH |
1473 | common, remoteheads, |
1474 | head_commit, head_arg); | |
1c7b76be MV |
1475 | if (!option_commit && !ret) { |
1476 | merge_was_ok = 1; | |
1477 | /* | |
1478 | * This is necessary here just to avoid writing | |
1479 | * the tree, but later we will *not* exit with | |
1480 | * status code 1 because merge_was_ok is set. | |
1481 | */ | |
1482 | ret = 1; | |
1483 | } | |
1484 | ||
1485 | if (ret) { | |
1486 | /* | |
1487 | * The backend exits with 1 when conflicts are | |
1488 | * left to be resolved, with 2 when it does not | |
1489 | * handle the given merge at all. | |
1490 | */ | |
1491 | if (ret == 1) { | |
1492 | int cnt = evaluate_result(); | |
1493 | ||
1494 | if (best_cnt <= 0 || cnt <= best_cnt) { | |
1495 | best_strategy = use_strategies[i]->name; | |
1496 | best_cnt = cnt; | |
1497 | } | |
1498 | } | |
1499 | if (merge_was_ok) | |
1500 | break; | |
1501 | else | |
1502 | continue; | |
1503 | } | |
1504 | ||
1505 | /* Automerge succeeded. */ | |
1506 | write_tree_trivial(result_tree); | |
1507 | automerge_was_ok = 1; | |
1508 | break; | |
1509 | } | |
1510 | ||
1511 | /* | |
1512 | * If we have a resulting tree, that means the strategy module | |
1513 | * auto resolved the merge cleanly. | |
1514 | */ | |
d5a35c11 | 1515 | if (automerge_was_ok) { |
e78cbf8c JH |
1516 | ret = finish_automerge(head_commit, head_subsumed, |
1517 | common, remoteheads, | |
4c57bd27 | 1518 | result_tree, wt_strategy); |
d5a35c11 NTND |
1519 | goto done; |
1520 | } | |
1c7b76be MV |
1521 | |
1522 | /* | |
1523 | * Pick the result from the best strategy and have the user fix | |
1524 | * it up. | |
1525 | */ | |
1526 | if (!best_strategy) { | |
894642f6 | 1527 | restore_state(head_commit->object.sha1, stash); |
1c7b76be MV |
1528 | if (use_strategies_nr > 1) |
1529 | fprintf(stderr, | |
bacec478 | 1530 | _("No merge strategy handled the merge.\n")); |
1c7b76be | 1531 | else |
bacec478 | 1532 | fprintf(stderr, _("Merge with strategy %s failed.\n"), |
1c7b76be | 1533 | use_strategies[0]->name); |
d5a35c11 NTND |
1534 | ret = 2; |
1535 | goto done; | |
1c7b76be MV |
1536 | } else if (best_strategy == wt_strategy) |
1537 | ; /* We already have its result in the working tree. */ | |
1538 | else { | |
bacec478 | 1539 | printf(_("Rewinding the tree to pristine...\n")); |
894642f6 | 1540 | restore_state(head_commit->object.sha1, stash); |
bacec478 | 1541 | printf(_("Using the %s to prepare resolving by hand.\n"), |
1c7b76be | 1542 | best_strategy); |
4c57bd27 JH |
1543 | try_merge_strategy(best_strategy, common, remoteheads, |
1544 | head_commit, head_arg); | |
1c7b76be MV |
1545 | } |
1546 | ||
1547 | if (squash) | |
4c57bd27 | 1548 | finish(head_commit, remoteheads, NULL, NULL); |
66f4b98a | 1549 | else |
4c57bd27 | 1550 | write_merge_state(remoteheads); |
1c7b76be | 1551 | |
d5a35c11 | 1552 | if (merge_was_ok) |
bacec478 ÆAB |
1553 | fprintf(stderr, _("Automatic merge went well; " |
1554 | "stopped before committing as requested\n")); | |
d5a35c11 NTND |
1555 | else |
1556 | ret = suggest_conflicts(option_renormalize); | |
1557 | ||
1558 | done: | |
96ec7b1e | 1559 | free(branch_to_free); |
d5a35c11 | 1560 | return ret; |
1c7b76be | 1561 | } |