8a0f6901f90c9728fb7521121ead030325ce3a45
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 */
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"
24 #include "rerere.h"
25 #include "help.h"
26 #include "merge-recursive.h"
27 #include "resolve-undo.h"
28 #include "remote.h"
30 #define DEFAULT_TWOHEAD (1<<0)
31 #define DEFAULT_OCTOPUS (1<<1)
32 #define NO_FAST_FORWARD (1<<2)
33 #define NO_TRIVIAL (1<<3)
35 struct strategy {
36 const char *name;
37 unsigned attr;
38 };
40 static const char * const builtin_merge_usage[] = {
41 "git merge [options] [<commit>...]",
42 "git merge [options] <msg> HEAD <commit>",
43 "git merge --abort",
44 NULL
45 };
47 static int show_diffstat = 1, shortlog_len, squash;
48 static int option_commit = 1, allow_fast_forward = 1;
49 static int fast_forward_only;
50 static int allow_trivial = 1, have_message;
51 static struct strbuf merge_msg;
52 static struct commit_list *remoteheads;
53 static unsigned char head[20], stash[20];
54 static struct strategy **use_strategies;
55 static size_t use_strategies_nr, use_strategies_alloc;
56 static const char **xopts;
57 static size_t xopts_nr, xopts_alloc;
58 static const char *branch;
59 static int option_renormalize;
60 static int verbosity;
61 static int allow_rerere_auto;
62 static int abort_current_merge;
63 static int show_progress = -1;
64 static int default_to_upstream;
66 static struct strategy all_strategy[] = {
67 { "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
68 { "octopus", DEFAULT_OCTOPUS },
69 { "resolve", 0 },
70 { "ours", NO_FAST_FORWARD | NO_TRIVIAL },
71 { "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
72 };
74 static const char *pull_twohead, *pull_octopus;
76 static int option_parse_message(const struct option *opt,
77 const char *arg, int unset)
78 {
79 struct strbuf *buf = opt->value;
81 if (unset)
82 strbuf_setlen(buf, 0);
83 else if (arg) {
84 strbuf_addf(buf, "%s%s", buf->len ? "\n\n" : "", arg);
85 have_message = 1;
86 } else
87 return error(_("switch `m' requires a value"));
88 return 0;
89 }
91 static struct strategy *get_strategy(const char *name)
92 {
93 int i;
94 struct strategy *ret;
95 static struct cmdnames main_cmds, other_cmds;
96 static int loaded;
98 if (!name)
99 return NULL;
101 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
102 if (!strcmp(name, all_strategy[i].name))
103 return &all_strategy[i];
105 if (!loaded) {
106 struct cmdnames not_strategies;
107 loaded = 1;
109 memset(¬_strategies, 0, sizeof(struct cmdnames));
110 load_command_list("git-merge-", &main_cmds, &other_cmds);
111 for (i = 0; i < main_cmds.cnt; i++) {
112 int j, found = 0;
113 struct cmdname *ent = main_cmds.names[i];
114 for (j = 0; j < ARRAY_SIZE(all_strategy); j++)
115 if (!strncmp(ent->name, all_strategy[j].name, ent->len)
116 && !all_strategy[j].name[ent->len])
117 found = 1;
118 if (!found)
119 add_cmdname(¬_strategies, ent->name, ent->len);
120 }
121 exclude_cmds(&main_cmds, ¬_strategies);
122 }
123 if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
124 fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
125 fprintf(stderr, _("Available strategies are:"));
126 for (i = 0; i < main_cmds.cnt; i++)
127 fprintf(stderr, " %s", main_cmds.names[i]->name);
128 fprintf(stderr, ".\n");
129 if (other_cmds.cnt) {
130 fprintf(stderr, _("Available custom strategies are:"));
131 for (i = 0; i < other_cmds.cnt; i++)
132 fprintf(stderr, " %s", other_cmds.names[i]->name);
133 fprintf(stderr, ".\n");
134 }
135 exit(1);
136 }
138 ret = xcalloc(1, sizeof(struct strategy));
139 ret->name = xstrdup(name);
140 ret->attr = NO_TRIVIAL;
141 return ret;
142 }
144 static void append_strategy(struct strategy *s)
145 {
146 ALLOC_GROW(use_strategies, use_strategies_nr + 1, use_strategies_alloc);
147 use_strategies[use_strategies_nr++] = s;
148 }
150 static int option_parse_strategy(const struct option *opt,
151 const char *name, int unset)
152 {
153 if (unset)
154 return 0;
156 append_strategy(get_strategy(name));
157 return 0;
158 }
160 static int option_parse_x(const struct option *opt,
161 const char *arg, int unset)
162 {
163 if (unset)
164 return 0;
166 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
167 xopts[xopts_nr++] = xstrdup(arg);
168 return 0;
169 }
171 static int option_parse_n(const struct option *opt,
172 const char *arg, int unset)
173 {
174 show_diffstat = unset;
175 return 0;
176 }
178 static struct option builtin_merge_options[] = {
179 { OPTION_CALLBACK, 'n', NULL, NULL, NULL,
180 "do not show a diffstat at the end of the merge",
181 PARSE_OPT_NOARG, option_parse_n },
182 OPT_BOOLEAN(0, "stat", &show_diffstat,
183 "show a diffstat at the end of the merge"),
184 OPT_BOOLEAN(0, "summary", &show_diffstat, "(synonym to --stat)"),
185 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
186 "add (at most <n>) entries from shortlog to merge commit message",
187 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
188 OPT_BOOLEAN(0, "squash", &squash,
189 "create a single commit instead of doing a merge"),
190 OPT_BOOLEAN(0, "commit", &option_commit,
191 "perform a commit if the merge succeeds (default)"),
192 OPT_BOOLEAN(0, "ff", &allow_fast_forward,
193 "allow fast-forward (default)"),
194 OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
195 "abort if fast-forward is not possible"),
196 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
197 OPT_CALLBACK('s', "strategy", &use_strategies, "strategy",
198 "merge strategy to use", option_parse_strategy),
199 OPT_CALLBACK('X', "strategy-option", &xopts, "option=value",
200 "option for selected merge strategy", option_parse_x),
201 OPT_CALLBACK('m', "message", &merge_msg, "message",
202 "merge commit message (for a non-fast-forward merge)",
203 option_parse_message),
204 OPT__VERBOSITY(&verbosity),
205 OPT_BOOLEAN(0, "abort", &abort_current_merge,
206 "abort the current in-progress merge"),
207 OPT_SET_INT(0, "progress", &show_progress, "force progress reporting", 1),
208 OPT_END()
209 };
211 /* Cleans up metadata that is uninteresting after a succeeded merge. */
212 static void drop_save(void)
213 {
214 unlink(git_path("MERGE_HEAD"));
215 unlink(git_path("MERGE_MSG"));
216 unlink(git_path("MERGE_MODE"));
217 }
219 static void save_state(void)
220 {
221 int len;
222 struct child_process cp;
223 struct strbuf buffer = STRBUF_INIT;
224 const char *argv[] = {"stash", "create", NULL};
226 memset(&cp, 0, sizeof(cp));
227 cp.argv = argv;
228 cp.out = -1;
229 cp.git_cmd = 1;
231 if (start_command(&cp))
232 die(_("could not run stash."));
233 len = strbuf_read(&buffer, cp.out, 1024);
234 close(cp.out);
236 if (finish_command(&cp) || len < 0)
237 die(_("stash failed"));
238 else if (!len)
239 return;
240 strbuf_setlen(&buffer, buffer.len-1);
241 if (get_sha1(buffer.buf, stash))
242 die(_("not a valid object: %s"), buffer.buf);
243 }
245 static void read_empty(unsigned const char *sha1, int verbose)
246 {
247 int i = 0;
248 const char *args[7];
250 args[i++] = "read-tree";
251 if (verbose)
252 args[i++] = "-v";
253 args[i++] = "-m";
254 args[i++] = "-u";
255 args[i++] = EMPTY_TREE_SHA1_HEX;
256 args[i++] = sha1_to_hex(sha1);
257 args[i] = NULL;
259 if (run_command_v_opt(args, RUN_GIT_CMD))
260 die(_("read-tree failed"));
261 }
263 static void reset_hard(unsigned const char *sha1, int verbose)
264 {
265 int i = 0;
266 const char *args[6];
268 args[i++] = "read-tree";
269 if (verbose)
270 args[i++] = "-v";
271 args[i++] = "--reset";
272 args[i++] = "-u";
273 args[i++] = sha1_to_hex(sha1);
274 args[i] = NULL;
276 if (run_command_v_opt(args, RUN_GIT_CMD))
277 die(_("read-tree failed"));
278 }
280 static void restore_state(void)
281 {
282 struct strbuf sb = STRBUF_INIT;
283 const char *args[] = { "stash", "apply", NULL, NULL };
285 if (is_null_sha1(stash))
286 return;
288 reset_hard(head, 1);
290 args[2] = sha1_to_hex(stash);
292 /*
293 * It is OK to ignore error here, for example when there was
294 * nothing to restore.
295 */
296 run_command_v_opt(args, RUN_GIT_CMD);
298 strbuf_release(&sb);
299 refresh_cache(REFRESH_QUIET);
300 }
302 /* This is called when no merge was necessary. */
303 static void finish_up_to_date(const char *msg)
304 {
305 if (verbosity >= 0)
306 printf("%s%s\n", squash ? _(" (nothing to squash)") : "", msg);
307 drop_save();
308 }
310 static void squash_message(void)
311 {
312 struct rev_info rev;
313 struct commit *commit;
314 struct strbuf out = STRBUF_INIT;
315 struct commit_list *j;
316 int fd;
317 struct pretty_print_context ctx = {0};
319 printf(_("Squash commit -- not updating HEAD\n"));
320 fd = open(git_path("SQUASH_MSG"), O_WRONLY | O_CREAT, 0666);
321 if (fd < 0)
322 die_errno(_("Could not write to '%s'"), git_path("SQUASH_MSG"));
324 init_revisions(&rev, NULL);
325 rev.ignore_merges = 1;
326 rev.commit_format = CMIT_FMT_MEDIUM;
328 commit = lookup_commit(head);
329 commit->object.flags |= UNINTERESTING;
330 add_pending_object(&rev, &commit->object, NULL);
332 for (j = remoteheads; j; j = j->next)
333 add_pending_object(&rev, &j->item->object, NULL);
335 setup_revisions(0, NULL, &rev, NULL);
336 if (prepare_revision_walk(&rev))
337 die(_("revision walk setup failed"));
339 ctx.abbrev = rev.abbrev;
340 ctx.date_mode = rev.date_mode;
342 strbuf_addstr(&out, "Squashed commit of the following:\n");
343 while ((commit = get_revision(&rev)) != NULL) {
344 strbuf_addch(&out, '\n');
345 strbuf_addf(&out, "commit %s\n",
346 sha1_to_hex(commit->object.sha1));
347 pretty_print_commit(rev.commit_format, commit, &out, &ctx);
348 }
349 if (write(fd, out.buf, out.len) < 0)
350 die_errno(_("Writing SQUASH_MSG"));
351 if (close(fd))
352 die_errno(_("Finishing SQUASH_MSG"));
353 strbuf_release(&out);
354 }
356 static void finish(const unsigned char *new_head, const char *msg)
357 {
358 struct strbuf reflog_message = STRBUF_INIT;
360 if (!msg)
361 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
362 else {
363 if (verbosity >= 0)
364 printf("%s\n", msg);
365 strbuf_addf(&reflog_message, "%s: %s",
366 getenv("GIT_REFLOG_ACTION"), msg);
367 }
368 if (squash) {
369 squash_message();
370 } else {
371 if (verbosity >= 0 && !merge_msg.len)
372 printf(_("No merge message -- not updating HEAD\n"));
373 else {
374 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
375 update_ref(reflog_message.buf, "HEAD",
376 new_head, head, 0,
377 DIE_ON_ERR);
378 /*
379 * We ignore errors in 'gc --auto', since the
380 * user should see them.
381 */
382 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
383 }
384 }
385 if (new_head && show_diffstat) {
386 struct diff_options opts;
387 diff_setup(&opts);
388 opts.output_format |=
389 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
390 opts.detect_rename = DIFF_DETECT_RENAME;
391 if (diff_use_color_default > 0)
392 DIFF_OPT_SET(&opts, COLOR_DIFF);
393 if (diff_setup_done(&opts) < 0)
394 die(_("diff_setup_done failed"));
395 diff_tree_sha1(head, new_head, "", &opts);
396 diffcore_std(&opts);
397 diff_flush(&opts);
398 }
400 /* Run a post-merge hook */
401 run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
403 strbuf_release(&reflog_message);
404 }
406 /* Get the name for the merge commit's message. */
407 static void merge_name(const char *remote, struct strbuf *msg)
408 {
409 struct object *remote_head;
410 unsigned char branch_head[20], buf_sha[20];
411 struct strbuf buf = STRBUF_INIT;
412 struct strbuf bname = STRBUF_INIT;
413 const char *ptr;
414 char *found_ref;
415 int len, early;
417 strbuf_branchname(&bname, remote);
418 remote = bname.buf;
420 memset(branch_head, 0, sizeof(branch_head));
421 remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
422 if (!remote_head)
423 die(_("'%s' does not point to a commit"), remote);
425 if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
426 if (!prefixcmp(found_ref, "refs/heads/")) {
427 strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
428 sha1_to_hex(branch_head), remote);
429 goto cleanup;
430 }
431 if (!prefixcmp(found_ref, "refs/remotes/")) {
432 strbuf_addf(msg, "%s\t\tremote-tracking branch '%s' of .\n",
433 sha1_to_hex(branch_head), remote);
434 goto cleanup;
435 }
436 }
438 /* See if remote matches <name>^^^.. or <name>~<number> */
439 for (len = 0, ptr = remote + strlen(remote);
440 remote < ptr && ptr[-1] == '^';
441 ptr--)
442 len++;
443 if (len)
444 early = 1;
445 else {
446 early = 0;
447 ptr = strrchr(remote, '~');
448 if (ptr) {
449 int seen_nonzero = 0;
451 len++; /* count ~ */
452 while (*++ptr && isdigit(*ptr)) {
453 seen_nonzero |= (*ptr != '0');
454 len++;
455 }
456 if (*ptr)
457 len = 0; /* not ...~<number> */
458 else if (seen_nonzero)
459 early = 1;
460 else if (len == 1)
461 early = 1; /* "name~" is "name~1"! */
462 }
463 }
464 if (len) {
465 struct strbuf truname = STRBUF_INIT;
466 strbuf_addstr(&truname, "refs/heads/");
467 strbuf_addstr(&truname, remote);
468 strbuf_setlen(&truname, truname.len - len);
469 if (resolve_ref(truname.buf, buf_sha, 1, NULL)) {
470 strbuf_addf(msg,
471 "%s\t\tbranch '%s'%s of .\n",
472 sha1_to_hex(remote_head->sha1),
473 truname.buf + 11,
474 (early ? " (early part)" : ""));
475 strbuf_release(&truname);
476 goto cleanup;
477 }
478 }
480 if (!strcmp(remote, "FETCH_HEAD") &&
481 !access(git_path("FETCH_HEAD"), R_OK)) {
482 FILE *fp;
483 struct strbuf line = STRBUF_INIT;
484 char *ptr;
486 fp = fopen(git_path("FETCH_HEAD"), "r");
487 if (!fp)
488 die_errno(_("could not open '%s' for reading"),
489 git_path("FETCH_HEAD"));
490 strbuf_getline(&line, fp, '\n');
491 fclose(fp);
492 ptr = strstr(line.buf, "\tnot-for-merge\t");
493 if (ptr)
494 strbuf_remove(&line, ptr-line.buf+1, 13);
495 strbuf_addbuf(msg, &line);
496 strbuf_release(&line);
497 goto cleanup;
498 }
499 strbuf_addf(msg, "%s\t\tcommit '%s'\n",
500 sha1_to_hex(remote_head->sha1), remote);
501 cleanup:
502 strbuf_release(&buf);
503 strbuf_release(&bname);
504 }
506 static int git_merge_config(const char *k, const char *v, void *cb)
507 {
508 if (branch && !prefixcmp(k, "branch.") &&
509 !prefixcmp(k + 7, branch) &&
510 !strcmp(k + 7 + strlen(branch), ".mergeoptions")) {
511 const char **argv;
512 int argc;
513 char *buf;
515 buf = xstrdup(v);
516 argc = split_cmdline(buf, &argv);
517 if (argc < 0)
518 die(_("Bad branch.%s.mergeoptions string: %s"), branch,
519 split_cmdline_strerror(argc));
520 argv = xrealloc(argv, sizeof(*argv) * (argc + 2));
521 memmove(argv + 1, argv, sizeof(*argv) * (argc + 1));
522 argc++;
523 parse_options(argc, argv, NULL, builtin_merge_options,
524 builtin_merge_usage, 0);
525 free(buf);
526 }
528 if (!strcmp(k, "merge.diffstat") || !strcmp(k, "merge.stat"))
529 show_diffstat = git_config_bool(k, v);
530 else if (!strcmp(k, "pull.twohead"))
531 return git_config_string(&pull_twohead, k, v);
532 else if (!strcmp(k, "pull.octopus"))
533 return git_config_string(&pull_octopus, k, v);
534 else if (!strcmp(k, "merge.renormalize"))
535 option_renormalize = git_config_bool(k, v);
536 else if (!strcmp(k, "merge.log") || !strcmp(k, "merge.summary")) {
537 int is_bool;
538 shortlog_len = git_config_bool_or_int(k, v, &is_bool);
539 if (!is_bool && shortlog_len < 0)
540 return error(_("%s: negative length %s"), k, v);
541 if (is_bool && shortlog_len)
542 shortlog_len = DEFAULT_MERGE_LOG_LEN;
543 return 0;
544 } else if (!strcmp(k, "merge.defaulttoupstream")) {
545 default_to_upstream = git_config_bool(k, v);
546 return 0;
547 }
548 return git_diff_ui_config(k, v, cb);
549 }
551 static int read_tree_trivial(unsigned char *common, unsigned char *head,
552 unsigned char *one)
553 {
554 int i, nr_trees = 0;
555 struct tree *trees[MAX_UNPACK_TREES];
556 struct tree_desc t[MAX_UNPACK_TREES];
557 struct unpack_trees_options opts;
559 memset(&opts, 0, sizeof(opts));
560 opts.head_idx = 2;
561 opts.src_index = &the_index;
562 opts.dst_index = &the_index;
563 opts.update = 1;
564 opts.verbose_update = 1;
565 opts.trivial_merges_only = 1;
566 opts.merge = 1;
567 trees[nr_trees] = parse_tree_indirect(common);
568 if (!trees[nr_trees++])
569 return -1;
570 trees[nr_trees] = parse_tree_indirect(head);
571 if (!trees[nr_trees++])
572 return -1;
573 trees[nr_trees] = parse_tree_indirect(one);
574 if (!trees[nr_trees++])
575 return -1;
576 opts.fn = threeway_merge;
577 cache_tree_free(&active_cache_tree);
578 for (i = 0; i < nr_trees; i++) {
579 parse_tree(trees[i]);
580 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
581 }
582 if (unpack_trees(nr_trees, t, &opts))
583 return -1;
584 return 0;
585 }
587 static void write_tree_trivial(unsigned char *sha1)
588 {
589 if (write_cache_as_tree(sha1, 0, NULL))
590 die(_("git write-tree failed to write a tree"));
591 }
593 static const char *merge_argument(struct commit *commit)
594 {
595 if (commit)
596 return sha1_to_hex(commit->object.sha1);
597 else
598 return EMPTY_TREE_SHA1_HEX;
599 }
601 int try_merge_command(const char *strategy, size_t xopts_nr,
602 const char **xopts, struct commit_list *common,
603 const char *head_arg, struct commit_list *remotes)
604 {
605 const char **args;
606 int i = 0, x = 0, ret;
607 struct commit_list *j;
608 struct strbuf buf = STRBUF_INIT;
610 args = xmalloc((4 + xopts_nr + commit_list_count(common) +
611 commit_list_count(remotes)) * sizeof(char *));
612 strbuf_addf(&buf, "merge-%s", strategy);
613 args[i++] = buf.buf;
614 for (x = 0; x < xopts_nr; x++) {
615 char *s = xmalloc(strlen(xopts[x])+2+1);
616 strcpy(s, "--");
617 strcpy(s+2, xopts[x]);
618 args[i++] = s;
619 }
620 for (j = common; j; j = j->next)
621 args[i++] = xstrdup(merge_argument(j->item));
622 args[i++] = "--";
623 args[i++] = head_arg;
624 for (j = remotes; j; j = j->next)
625 args[i++] = xstrdup(merge_argument(j->item));
626 args[i] = NULL;
627 ret = run_command_v_opt(args, RUN_GIT_CMD);
628 strbuf_release(&buf);
629 i = 1;
630 for (x = 0; x < xopts_nr; x++)
631 free((void *)args[i++]);
632 for (j = common; j; j = j->next)
633 free((void *)args[i++]);
634 i += 2;
635 for (j = remotes; j; j = j->next)
636 free((void *)args[i++]);
637 free(args);
638 discard_cache();
639 if (read_cache() < 0)
640 die(_("failed to read the cache"));
641 resolve_undo_clear();
643 return ret;
644 }
646 static int try_merge_strategy(const char *strategy, struct commit_list *common,
647 const char *head_arg)
648 {
649 int index_fd;
650 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
652 index_fd = hold_locked_index(lock, 1);
653 refresh_cache(REFRESH_QUIET);
654 if (active_cache_changed &&
655 (write_cache(index_fd, active_cache, active_nr) ||
656 commit_locked_index(lock)))
657 return error(_("Unable to write index."));
658 rollback_lock_file(lock);
660 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
661 int clean, x;
662 struct commit *result;
663 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
664 int index_fd;
665 struct commit_list *reversed = NULL;
666 struct merge_options o;
667 struct commit_list *j;
669 if (remoteheads->next) {
670 error(_("Not handling anything other than two heads merge."));
671 return 2;
672 }
674 init_merge_options(&o);
675 if (!strcmp(strategy, "subtree"))
676 o.subtree_shift = "";
678 o.renormalize = option_renormalize;
679 o.show_rename_progress =
680 show_progress == -1 ? isatty(2) : show_progress;
682 for (x = 0; x < xopts_nr; x++)
683 if (parse_merge_opt(&o, xopts[x]))
684 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
686 o.branch1 = head_arg;
687 o.branch2 = remoteheads->item->util;
689 for (j = common; j; j = j->next)
690 commit_list_insert(j->item, &reversed);
692 index_fd = hold_locked_index(lock, 1);
693 clean = merge_recursive(&o, lookup_commit(head),
694 remoteheads->item, reversed, &result);
695 if (active_cache_changed &&
696 (write_cache(index_fd, active_cache, active_nr) ||
697 commit_locked_index(lock)))
698 die (_("unable to write %s"), get_index_file());
699 rollback_lock_file(lock);
700 return clean ? 0 : 1;
701 } else {
702 return try_merge_command(strategy, xopts_nr, xopts,
703 common, head_arg, remoteheads);
704 }
705 }
707 static void count_diff_files(struct diff_queue_struct *q,
708 struct diff_options *opt, void *data)
709 {
710 int *count = data;
712 (*count) += q->nr;
713 }
715 static int count_unmerged_entries(void)
716 {
717 int i, ret = 0;
719 for (i = 0; i < active_nr; i++)
720 if (ce_stage(active_cache[i]))
721 ret++;
723 return ret;
724 }
726 int checkout_fast_forward(const unsigned char *head, const unsigned char *remote)
727 {
728 struct tree *trees[MAX_UNPACK_TREES];
729 struct unpack_trees_options opts;
730 struct tree_desc t[MAX_UNPACK_TREES];
731 int i, fd, nr_trees = 0;
732 struct dir_struct dir;
733 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
735 refresh_cache(REFRESH_QUIET);
737 fd = hold_locked_index(lock_file, 1);
739 memset(&trees, 0, sizeof(trees));
740 memset(&opts, 0, sizeof(opts));
741 memset(&t, 0, sizeof(t));
742 memset(&dir, 0, sizeof(dir));
743 dir.flags |= DIR_SHOW_IGNORED;
744 dir.exclude_per_dir = ".gitignore";
745 opts.dir = &dir;
747 opts.head_idx = 1;
748 opts.src_index = &the_index;
749 opts.dst_index = &the_index;
750 opts.update = 1;
751 opts.verbose_update = 1;
752 opts.merge = 1;
753 opts.fn = twoway_merge;
754 setup_unpack_trees_porcelain(&opts, "merge");
756 trees[nr_trees] = parse_tree_indirect(head);
757 if (!trees[nr_trees++])
758 return -1;
759 trees[nr_trees] = parse_tree_indirect(remote);
760 if (!trees[nr_trees++])
761 return -1;
762 for (i = 0; i < nr_trees; i++) {
763 parse_tree(trees[i]);
764 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
765 }
766 if (unpack_trees(nr_trees, t, &opts))
767 return -1;
768 if (write_cache(fd, active_cache, active_nr) ||
769 commit_locked_index(lock_file))
770 die(_("unable to write new index file"));
771 return 0;
772 }
774 static void split_merge_strategies(const char *string, struct strategy **list,
775 int *nr, int *alloc)
776 {
777 char *p, *q, *buf;
779 if (!string)
780 return;
782 buf = xstrdup(string);
783 q = buf;
784 for (;;) {
785 p = strchr(q, ' ');
786 if (!p) {
787 ALLOC_GROW(*list, *nr + 1, *alloc);
788 (*list)[(*nr)++].name = xstrdup(q);
789 free(buf);
790 return;
791 } else {
792 *p = '\0';
793 ALLOC_GROW(*list, *nr + 1, *alloc);
794 (*list)[(*nr)++].name = xstrdup(q);
795 q = ++p;
796 }
797 }
798 }
800 static void add_strategies(const char *string, unsigned attr)
801 {
802 struct strategy *list = NULL;
803 int list_alloc = 0, list_nr = 0, i;
805 memset(&list, 0, sizeof(list));
806 split_merge_strategies(string, &list, &list_nr, &list_alloc);
807 if (list) {
808 for (i = 0; i < list_nr; i++)
809 append_strategy(get_strategy(list[i].name));
810 return;
811 }
812 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
813 if (all_strategy[i].attr & attr)
814 append_strategy(&all_strategy[i]);
816 }
818 static void write_merge_msg(void)
819 {
820 int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
821 if (fd < 0)
822 die_errno(_("Could not open '%s' for writing"),
823 git_path("MERGE_MSG"));
824 if (write_in_full(fd, merge_msg.buf, merge_msg.len) != merge_msg.len)
825 die_errno(_("Could not write to '%s'"), git_path("MERGE_MSG"));
826 close(fd);
827 }
829 static void read_merge_msg(void)
830 {
831 strbuf_reset(&merge_msg);
832 if (strbuf_read_file(&merge_msg, git_path("MERGE_MSG"), 0) < 0)
833 die_errno("Could not read from '%s'", git_path("MERGE_MSG"));
834 }
836 static void run_prepare_commit_msg(void)
837 {
838 write_merge_msg();
839 run_hook(get_index_file(), "prepare-commit-msg",
840 git_path("MERGE_MSG"), "merge", NULL, NULL);
841 read_merge_msg();
842 }
844 static int merge_trivial(void)
845 {
846 unsigned char result_tree[20], result_commit[20];
847 struct commit_list *parent = xmalloc(sizeof(*parent));
849 write_tree_trivial(result_tree);
850 printf(_("Wonderful.\n"));
851 parent->item = lookup_commit(head);
852 parent->next = xmalloc(sizeof(*parent->next));
853 parent->next->item = remoteheads->item;
854 parent->next->next = NULL;
855 run_prepare_commit_msg();
856 commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
857 finish(result_commit, "In-index merge");
858 drop_save();
859 return 0;
860 }
862 static int finish_automerge(struct commit_list *common,
863 unsigned char *result_tree,
864 const char *wt_strategy)
865 {
866 struct commit_list *parents = NULL, *j;
867 struct strbuf buf = STRBUF_INIT;
868 unsigned char result_commit[20];
870 free_commit_list(common);
871 if (allow_fast_forward) {
872 parents = remoteheads;
873 commit_list_insert(lookup_commit(head), &parents);
874 parents = reduce_heads(parents);
875 } else {
876 struct commit_list **pptr = &parents;
878 pptr = &commit_list_insert(lookup_commit(head),
879 pptr)->next;
880 for (j = remoteheads; j; j = j->next)
881 pptr = &commit_list_insert(j->item, pptr)->next;
882 }
883 free_commit_list(remoteheads);
884 strbuf_addch(&merge_msg, '\n');
885 run_prepare_commit_msg();
886 commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
887 strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
888 finish(result_commit, buf.buf);
889 strbuf_release(&buf);
890 drop_save();
891 return 0;
892 }
894 static int suggest_conflicts(int renormalizing)
895 {
896 FILE *fp;
897 int pos;
899 fp = fopen(git_path("MERGE_MSG"), "a");
900 if (!fp)
901 die_errno(_("Could not open '%s' for writing"),
902 git_path("MERGE_MSG"));
903 fprintf(fp, "\nConflicts:\n");
904 for (pos = 0; pos < active_nr; pos++) {
905 struct cache_entry *ce = active_cache[pos];
907 if (ce_stage(ce)) {
908 fprintf(fp, "\t%s\n", ce->name);
909 while (pos + 1 < active_nr &&
910 !strcmp(ce->name,
911 active_cache[pos + 1]->name))
912 pos++;
913 }
914 }
915 fclose(fp);
916 rerere(allow_rerere_auto);
917 printf(_("Automatic merge failed; "
918 "fix conflicts and then commit the result.\n"));
919 return 1;
920 }
922 static struct commit *is_old_style_invocation(int argc, const char **argv)
923 {
924 struct commit *second_token = NULL;
925 if (argc > 2) {
926 unsigned char second_sha1[20];
928 if (get_sha1(argv[1], second_sha1))
929 return NULL;
930 second_token = lookup_commit_reference_gently(second_sha1, 0);
931 if (!second_token)
932 die(_("'%s' is not a commit"), argv[1]);
933 if (hashcmp(second_token->object.sha1, head))
934 return NULL;
935 }
936 return second_token;
937 }
939 static int evaluate_result(void)
940 {
941 int cnt = 0;
942 struct rev_info rev;
944 /* Check how many files differ. */
945 init_revisions(&rev, "");
946 setup_revisions(0, NULL, &rev, NULL);
947 rev.diffopt.output_format |=
948 DIFF_FORMAT_CALLBACK;
949 rev.diffopt.format_callback = count_diff_files;
950 rev.diffopt.format_callback_data = &cnt;
951 run_diff_files(&rev, 0);
953 /*
954 * Check how many unmerged entries are
955 * there.
956 */
957 cnt += count_unmerged_entries();
959 return cnt;
960 }
962 /*
963 * Pretend as if the user told us to merge with the tracking
964 * branch we have for the upstream of the current branch
965 */
966 static int setup_with_upstream(const char ***argv)
967 {
968 struct branch *branch = branch_get(NULL);
969 int i;
970 const char **args;
972 if (!branch)
973 die("No current branch.");
974 if (!branch->remote)
975 die("No remote for the current branch.");
976 if (!branch->merge_nr)
977 die("No default upstream defined for the current branch.");
979 args = xcalloc(branch->merge_nr + 1, sizeof(char *));
980 for (i = 0; i < branch->merge_nr; i++) {
981 if (!branch->merge[i]->dst)
982 die("No remote tracking branch for %s from %s",
983 branch->merge[i]->src, branch->remote_name);
984 args[i] = branch->merge[i]->dst;
985 }
986 args[i] = NULL;
987 *argv = args;
988 return i;
989 }
991 int cmd_merge(int argc, const char **argv, const char *prefix)
992 {
993 unsigned char result_tree[20];
994 struct strbuf buf = STRBUF_INIT;
995 const char *head_arg;
996 int flag, head_invalid = 0, i;
997 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
998 struct commit_list *common = NULL;
999 const char *best_strategy = NULL, *wt_strategy = NULL;
1000 struct commit_list **remotes = &remoteheads;
1002 if (argc == 2 && !strcmp(argv[1], "-h"))
1003 usage_with_options(builtin_merge_usage, builtin_merge_options);
1005 /*
1006 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1007 * current branch.
1008 */
1009 branch = resolve_ref("HEAD", head, 0, &flag);
1010 if (branch && !prefixcmp(branch, "refs/heads/"))
1011 branch += 11;
1012 if (is_null_sha1(head))
1013 head_invalid = 1;
1015 git_config(git_merge_config, NULL);
1017 /* for color.ui */
1018 if (diff_use_color_default == -1)
1019 diff_use_color_default = git_use_color_default;
1021 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1022 builtin_merge_usage, 0);
1024 if (verbosity < 0 && show_progress == -1)
1025 show_progress = 0;
1027 if (abort_current_merge) {
1028 int nargc = 2;
1029 const char *nargv[] = {"reset", "--merge", NULL};
1031 if (!file_exists(git_path("MERGE_HEAD")))
1032 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1034 /* Invoke 'git reset --merge' */
1035 return cmd_reset(nargc, nargv, prefix);
1036 }
1038 if (read_cache_unmerged())
1039 die_resolve_conflict("merge");
1041 if (file_exists(git_path("MERGE_HEAD"))) {
1042 /*
1043 * There is no unmerged entry, don't advise 'git
1044 * add/rm <file>', just 'git commit'.
1045 */
1046 if (advice_resolve_conflict)
1047 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1048 "Please, commit your changes before you can merge."));
1049 else
1050 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1051 }
1052 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1053 if (advice_resolve_conflict)
1054 die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1055 "Please, commit your changes before you can merge.");
1056 else
1057 die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).");
1058 }
1059 resolve_undo_clear();
1061 if (verbosity < 0)
1062 show_diffstat = 0;
1064 if (squash) {
1065 if (!allow_fast_forward)
1066 die(_("You cannot combine --squash with --no-ff."));
1067 option_commit = 0;
1068 }
1070 if (!allow_fast_forward && fast_forward_only)
1071 die(_("You cannot combine --no-ff with --ff-only."));
1073 if (!argc && !abort_current_merge && default_to_upstream)
1074 argc = setup_with_upstream(&argv);
1076 if (!argc)
1077 usage_with_options(builtin_merge_usage,
1078 builtin_merge_options);
1080 /*
1081 * This could be traditional "merge <msg> HEAD <commit>..." and
1082 * the way we can tell it is to see if the second token is HEAD,
1083 * but some people might have misused the interface and used a
1084 * committish that is the same as HEAD there instead.
1085 * Traditional format never would have "-m" so it is an
1086 * additional safety measure to check for it.
1087 */
1089 if (!have_message && is_old_style_invocation(argc, argv)) {
1090 strbuf_addstr(&merge_msg, argv[0]);
1091 head_arg = argv[1];
1092 argv += 2;
1093 argc -= 2;
1094 } else if (head_invalid) {
1095 struct object *remote_head;
1096 /*
1097 * If the merged head is a valid one there is no reason
1098 * to forbid "git merge" into a branch yet to be born.
1099 * We do the same for "git pull".
1100 */
1101 if (argc != 1)
1102 die(_("Can merge only exactly one commit into "
1103 "empty head"));
1104 if (squash)
1105 die(_("Squash commit into empty head not supported yet"));
1106 if (!allow_fast_forward)
1107 die(_("Non-fast-forward commit does not make sense into "
1108 "an empty head"));
1109 remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
1110 if (!remote_head)
1111 die(_("%s - not something we can merge"), argv[0]);
1112 read_empty(remote_head->sha1, 0);
1113 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
1114 DIE_ON_ERR);
1115 return 0;
1116 } else {
1117 struct strbuf merge_names = STRBUF_INIT;
1119 /* We are invoked directly as the first-class UI. */
1120 head_arg = "HEAD";
1122 /*
1123 * All the rest are the commits being merged;
1124 * prepare the standard merge summary message to
1125 * be appended to the given message. If remote
1126 * is invalid we will die later in the common
1127 * codepath so we discard the error in this
1128 * loop.
1129 */
1130 for (i = 0; i < argc; i++)
1131 merge_name(argv[i], &merge_names);
1133 if (!have_message || shortlog_len) {
1134 fmt_merge_msg(&merge_names, &merge_msg, !have_message,
1135 shortlog_len);
1136 if (merge_msg.len)
1137 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1138 }
1139 }
1141 if (head_invalid || !argc)
1142 usage_with_options(builtin_merge_usage,
1143 builtin_merge_options);
1145 strbuf_addstr(&buf, "merge");
1146 for (i = 0; i < argc; i++)
1147 strbuf_addf(&buf, " %s", argv[i]);
1148 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1149 strbuf_reset(&buf);
1151 for (i = 0; i < argc; i++) {
1152 struct object *o;
1153 struct commit *commit;
1155 o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
1156 if (!o)
1157 die(_("%s - not something we can merge"), argv[i]);
1158 commit = lookup_commit(o->sha1);
1159 commit->util = (void *)argv[i];
1160 remotes = &commit_list_insert(commit, remotes)->next;
1162 strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
1163 setenv(buf.buf, argv[i], 1);
1164 strbuf_reset(&buf);
1165 }
1167 if (!use_strategies) {
1168 if (!remoteheads->next)
1169 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1170 else
1171 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1172 }
1174 for (i = 0; i < use_strategies_nr; i++) {
1175 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1176 allow_fast_forward = 0;
1177 if (use_strategies[i]->attr & NO_TRIVIAL)
1178 allow_trivial = 0;
1179 }
1181 if (!remoteheads->next)
1182 common = get_merge_bases(lookup_commit(head),
1183 remoteheads->item, 1);
1184 else {
1185 struct commit_list *list = remoteheads;
1186 commit_list_insert(lookup_commit(head), &list);
1187 common = get_octopus_merge_bases(list);
1188 free(list);
1189 }
1191 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head, NULL, 0,
1192 DIE_ON_ERR);
1194 if (!common)
1195 ; /* No common ancestors found. We need a real merge. */
1196 else if (!remoteheads->next && !common->next &&
1197 common->item == remoteheads->item) {
1198 /*
1199 * If head can reach all the merge then we are up to date.
1200 * but first the most common case of merging one remote.
1201 */
1202 finish_up_to_date("Already up-to-date.");
1203 return 0;
1204 } else if (allow_fast_forward && !remoteheads->next &&
1205 !common->next &&
1206 !hashcmp(common->item->object.sha1, head)) {
1207 /* Again the most common case of merging one remote. */
1208 struct strbuf msg = STRBUF_INIT;
1209 struct object *o;
1210 char hex[41];
1212 strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
1214 if (verbosity >= 0)
1215 printf(_("Updating %s..%s\n"),
1216 hex,
1217 find_unique_abbrev(remoteheads->item->object.sha1,
1218 DEFAULT_ABBREV));
1219 strbuf_addstr(&msg, "Fast-forward");
1220 if (have_message)
1221 strbuf_addstr(&msg,
1222 " (no commit created; -m option ignored)");
1223 o = peel_to_type(sha1_to_hex(remoteheads->item->object.sha1),
1224 0, NULL, OBJ_COMMIT);
1225 if (!o)
1226 return 1;
1228 if (checkout_fast_forward(head, remoteheads->item->object.sha1))
1229 return 1;
1231 finish(o->sha1, msg.buf);
1232 drop_save();
1233 return 0;
1234 } else if (!remoteheads->next && common->next)
1235 ;
1236 /*
1237 * We are not doing octopus and not fast-forward. Need
1238 * a real merge.
1239 */
1240 else if (!remoteheads->next && !common->next && option_commit) {
1241 /*
1242 * We are not doing octopus, not fast-forward, and have
1243 * only one common.
1244 */
1245 refresh_cache(REFRESH_QUIET);
1246 if (allow_trivial && !fast_forward_only) {
1247 /* See if it is really trivial. */
1248 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1249 printf(_("Trying really trivial in-index merge...\n"));
1250 if (!read_tree_trivial(common->item->object.sha1,
1251 head, remoteheads->item->object.sha1))
1252 return merge_trivial();
1253 printf(_("Nope.\n"));
1254 }
1255 } else {
1256 /*
1257 * An octopus. If we can reach all the remote we are up
1258 * to date.
1259 */
1260 int up_to_date = 1;
1261 struct commit_list *j;
1263 for (j = remoteheads; j; j = j->next) {
1264 struct commit_list *common_one;
1266 /*
1267 * Here we *have* to calculate the individual
1268 * merge_bases again, otherwise "git merge HEAD^
1269 * HEAD^^" would be missed.
1270 */
1271 common_one = get_merge_bases(lookup_commit(head),
1272 j->item, 1);
1273 if (hashcmp(common_one->item->object.sha1,
1274 j->item->object.sha1)) {
1275 up_to_date = 0;
1276 break;
1277 }
1278 }
1279 if (up_to_date) {
1280 finish_up_to_date("Already up-to-date. Yeeah!");
1281 return 0;
1282 }
1283 }
1285 if (fast_forward_only)
1286 die(_("Not possible to fast-forward, aborting."));
1288 /* We are going to make a new commit. */
1289 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1291 /*
1292 * At this point, we need a real merge. No matter what strategy
1293 * we use, it would operate on the index, possibly affecting the
1294 * working tree, and when resolved cleanly, have the desired
1295 * tree in the index -- this means that the index must be in
1296 * sync with the head commit. The strategies are responsible
1297 * to ensure this.
1298 */
1299 if (use_strategies_nr != 1) {
1300 /*
1301 * Stash away the local changes so that we can try more
1302 * than one.
1303 */
1304 save_state();
1305 } else {
1306 memcpy(stash, null_sha1, 20);
1307 }
1309 for (i = 0; i < use_strategies_nr; i++) {
1310 int ret;
1311 if (i) {
1312 printf(_("Rewinding the tree to pristine...\n"));
1313 restore_state();
1314 }
1315 if (use_strategies_nr != 1)
1316 printf(_("Trying merge strategy %s...\n"),
1317 use_strategies[i]->name);
1318 /*
1319 * Remember which strategy left the state in the working
1320 * tree.
1321 */
1322 wt_strategy = use_strategies[i]->name;
1324 ret = try_merge_strategy(use_strategies[i]->name,
1325 common, head_arg);
1326 if (!option_commit && !ret) {
1327 merge_was_ok = 1;
1328 /*
1329 * This is necessary here just to avoid writing
1330 * the tree, but later we will *not* exit with
1331 * status code 1 because merge_was_ok is set.
1332 */
1333 ret = 1;
1334 }
1336 if (ret) {
1337 /*
1338 * The backend exits with 1 when conflicts are
1339 * left to be resolved, with 2 when it does not
1340 * handle the given merge at all.
1341 */
1342 if (ret == 1) {
1343 int cnt = evaluate_result();
1345 if (best_cnt <= 0 || cnt <= best_cnt) {
1346 best_strategy = use_strategies[i]->name;
1347 best_cnt = cnt;
1348 }
1349 }
1350 if (merge_was_ok)
1351 break;
1352 else
1353 continue;
1354 }
1356 /* Automerge succeeded. */
1357 write_tree_trivial(result_tree);
1358 automerge_was_ok = 1;
1359 break;
1360 }
1362 /*
1363 * If we have a resulting tree, that means the strategy module
1364 * auto resolved the merge cleanly.
1365 */
1366 if (automerge_was_ok)
1367 return finish_automerge(common, result_tree, wt_strategy);
1369 /*
1370 * Pick the result from the best strategy and have the user fix
1371 * it up.
1372 */
1373 if (!best_strategy) {
1374 restore_state();
1375 if (use_strategies_nr > 1)
1376 fprintf(stderr,
1377 _("No merge strategy handled the merge.\n"));
1378 else
1379 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1380 use_strategies[0]->name);
1381 return 2;
1382 } else if (best_strategy == wt_strategy)
1383 ; /* We already have its result in the working tree. */
1384 else {
1385 printf(_("Rewinding the tree to pristine...\n"));
1386 restore_state();
1387 printf(_("Using the %s to prepare resolving by hand.\n"),
1388 best_strategy);
1389 try_merge_strategy(best_strategy, common, head_arg);
1390 }
1392 if (squash)
1393 finish(NULL, NULL);
1394 else {
1395 int fd;
1396 struct commit_list *j;
1398 for (j = remoteheads; j; j = j->next)
1399 strbuf_addf(&buf, "%s\n",
1400 sha1_to_hex(j->item->object.sha1));
1401 fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
1402 if (fd < 0)
1403 die_errno(_("Could not open '%s' for writing"),
1404 git_path("MERGE_HEAD"));
1405 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1406 die_errno(_("Could not write to '%s'"), git_path("MERGE_HEAD"));
1407 close(fd);
1408 strbuf_addch(&merge_msg, '\n');
1409 write_merge_msg();
1410 fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
1411 if (fd < 0)
1412 die_errno(_("Could not open '%s' for writing"),
1413 git_path("MERGE_MODE"));
1414 strbuf_reset(&buf);
1415 if (!allow_fast_forward)
1416 strbuf_addf(&buf, "no-ff");
1417 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1418 die_errno(_("Could not write to '%s'"), git_path("MERGE_MODE"));
1419 close(fd);
1420 }
1422 if (merge_was_ok) {
1423 fprintf(stderr, _("Automatic merge went well; "
1424 "stopped before committing as requested\n"));
1425 return 0;
1426 } else
1427 return suggest_conflicts(option_renormalize);
1428 }