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