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"
24#include "rerere.h"
25#include "help.h"
26#include "merge-recursive.h"
27#include "resolve-undo.h"
28#include "remote.h"
29
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)
34
35struct strategy {
36 const char *name;
37 unsigned attr;
38};
39
40static 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};
46
47static int show_diffstat = 1, shortlog_len, squash;
48static int option_commit = 1, allow_fast_forward = 1;
49static int fast_forward_only;
50static int allow_trivial = 1, have_message;
51static struct strbuf merge_msg;
52static struct commit_list *remoteheads;
53static unsigned char head[20], stash[20];
54static struct strategy **use_strategies;
55static size_t use_strategies_nr, use_strategies_alloc;
56static const char **xopts;
57static size_t xopts_nr, xopts_alloc;
58static const char *branch;
59static int option_renormalize;
60static int verbosity;
61static int allow_rerere_auto;
62static int abort_current_merge;
63static int show_progress = -1;
64static int default_to_upstream;
65
66static 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};
73
74static const char *pull_twohead, *pull_octopus;
75
76static int option_parse_message(const struct option *opt,
77 const char *arg, int unset)
78{
79 struct strbuf *buf = opt->value;
80
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}
90
91static 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;
97
98 if (!name)
99 return NULL;
100
101 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
102 if (!strcmp(name, all_strategy[i].name))
103 return &all_strategy[i];
104
105 if (!loaded) {
106 struct cmdnames not_strategies;
107 loaded = 1;
108
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 }
137
138 ret = xcalloc(1, sizeof(struct strategy));
139 ret->name = xstrdup(name);
140 ret->attr = NO_TRIVIAL;
141 return ret;
142}
143
144static 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}
149
150static int option_parse_strategy(const struct option *opt,
151 const char *name, int unset)
152{
153 if (unset)
154 return 0;
155
156 append_strategy(get_strategy(name));
157 return 0;
158}
159
160static int option_parse_x(const struct option *opt,
161 const char *arg, int unset)
162{
163 if (unset)
164 return 0;
165
166 ALLOC_GROW(xopts, xopts_nr + 1, xopts_alloc);
167 xopts[xopts_nr++] = xstrdup(arg);
168 return 0;
169}
170
171static int option_parse_n(const struct option *opt,
172 const char *arg, int unset)
173{
174 show_diffstat = unset;
175 return 0;
176}
177
178static 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};
210
211/* Cleans up metadata that is uninteresting after a succeeded merge. */
212static void drop_save(void)
213{
214 unlink(git_path("MERGE_HEAD"));
215 unlink(git_path("MERGE_MSG"));
216 unlink(git_path("MERGE_MODE"));
217}
218
219static 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};
225
226 memset(&cp, 0, sizeof(cp));
227 cp.argv = argv;
228 cp.out = -1;
229 cp.git_cmd = 1;
230
231 if (start_command(&cp))
232 die(_("could not run stash."));
233 len = strbuf_read(&buffer, cp.out, 1024);
234 close(cp.out);
235
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}
244
245static void read_empty(unsigned const char *sha1, int verbose)
246{
247 int i = 0;
248 const char *args[7];
249
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;
258
259 if (run_command_v_opt(args, RUN_GIT_CMD))
260 die(_("read-tree failed"));
261}
262
263static void reset_hard(unsigned const char *sha1, int verbose)
264{
265 int i = 0;
266 const char *args[6];
267
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;
275
276 if (run_command_v_opt(args, RUN_GIT_CMD))
277 die(_("read-tree failed"));
278}
279
280static void restore_state(void)
281{
282 struct strbuf sb = STRBUF_INIT;
283 const char *args[] = { "stash", "apply", NULL, NULL };
284
285 if (is_null_sha1(stash))
286 return;
287
288 reset_hard(head, 1);
289
290 args[2] = sha1_to_hex(stash);
291
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);
297
298 strbuf_release(&sb);
299 refresh_cache(REFRESH_QUIET);
300}
301
302/* This is called when no merge was necessary. */
303static 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}
309
310static 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};
318
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"));
323
324 init_revisions(&rev, NULL);
325 rev.ignore_merges = 1;
326 rev.commit_format = CMIT_FMT_MEDIUM;
327
328 commit = lookup_commit(head);
329 commit->object.flags |= UNINTERESTING;
330 add_pending_object(&rev, &commit->object, NULL);
331
332 for (j = remoteheads; j; j = j->next)
333 add_pending_object(&rev, &j->item->object, NULL);
334
335 setup_revisions(0, NULL, &rev, NULL);
336 if (prepare_revision_walk(&rev))
337 die(_("revision walk setup failed"));
338
339 ctx.abbrev = rev.abbrev;
340 ctx.date_mode = rev.date_mode;
341
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}
355
356static void finish(const unsigned char *new_head, const char *msg)
357{
358 struct strbuf reflog_message = STRBUF_INIT;
359
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 }
399
400 /* Run a post-merge hook */
401 run_hook(NULL, "post-merge", squash ? "1" : "0", NULL);
402
403 strbuf_release(&reflog_message);
404}
405
406/* Get the name for the merge commit's message. */
407static 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;
416
417 strbuf_branchname(&bname, remote);
418 remote = bname.buf;
419
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);
424
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 }
437
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;
450
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 }
479
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;
485
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);
501cleanup:
502 strbuf_release(&buf);
503 strbuf_release(&bname);
504}
505
506static 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;
514
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 }
527
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}
550
551static 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;
558
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}
586
587static 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}
592
593int try_merge_command(const char *strategy, size_t xopts_nr,
594 const char **xopts, struct commit_list *common,
595 const char *head_arg, struct commit_list *remotes)
596{
597 const char **args;
598 int i = 0, x = 0, ret;
599 struct commit_list *j;
600 struct strbuf buf = STRBUF_INIT;
601
602 args = xmalloc((4 + xopts_nr + commit_list_count(common) +
603 commit_list_count(remotes)) * sizeof(char *));
604 strbuf_addf(&buf, "merge-%s", strategy);
605 args[i++] = buf.buf;
606 for (x = 0; x < xopts_nr; x++) {
607 char *s = xmalloc(strlen(xopts[x])+2+1);
608 strcpy(s, "--");
609 strcpy(s+2, xopts[x]);
610 args[i++] = s;
611 }
612 for (j = common; j; j = j->next)
613 args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
614 args[i++] = "--";
615 args[i++] = head_arg;
616 for (j = remotes; j; j = j->next)
617 args[i++] = xstrdup(sha1_to_hex(j->item->object.sha1));
618 args[i] = NULL;
619 ret = run_command_v_opt(args, RUN_GIT_CMD);
620 strbuf_release(&buf);
621 i = 1;
622 for (x = 0; x < xopts_nr; x++)
623 free((void *)args[i++]);
624 for (j = common; j; j = j->next)
625 free((void *)args[i++]);
626 i += 2;
627 for (j = remotes; j; j = j->next)
628 free((void *)args[i++]);
629 free(args);
630 discard_cache();
631 if (read_cache() < 0)
632 die(_("failed to read the cache"));
633 resolve_undo_clear();
634
635 return ret;
636}
637
638static int try_merge_strategy(const char *strategy, struct commit_list *common,
639 const char *head_arg)
640{
641 int index_fd;
642 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
643
644 index_fd = hold_locked_index(lock, 1);
645 refresh_cache(REFRESH_QUIET);
646 if (active_cache_changed &&
647 (write_cache(index_fd, active_cache, active_nr) ||
648 commit_locked_index(lock)))
649 return error(_("Unable to write index."));
650 rollback_lock_file(lock);
651
652 if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
653 int clean, x;
654 struct commit *result;
655 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
656 int index_fd;
657 struct commit_list *reversed = NULL;
658 struct merge_options o;
659 struct commit_list *j;
660
661 if (remoteheads->next) {
662 error(_("Not handling anything other than two heads merge."));
663 return 2;
664 }
665
666 init_merge_options(&o);
667 if (!strcmp(strategy, "subtree"))
668 o.subtree_shift = "";
669
670 o.renormalize = option_renormalize;
671 o.show_rename_progress =
672 show_progress == -1 ? isatty(2) : show_progress;
673
674 for (x = 0; x < xopts_nr; x++)
675 if (parse_merge_opt(&o, xopts[x]))
676 die(_("Unknown option for merge-recursive: -X%s"), xopts[x]);
677
678 o.branch1 = head_arg;
679 o.branch2 = remoteheads->item->util;
680
681 for (j = common; j; j = j->next)
682 commit_list_insert(j->item, &reversed);
683
684 index_fd = hold_locked_index(lock, 1);
685 clean = merge_recursive(&o, lookup_commit(head),
686 remoteheads->item, reversed, &result);
687 if (active_cache_changed &&
688 (write_cache(index_fd, active_cache, active_nr) ||
689 commit_locked_index(lock)))
690 die (_("unable to write %s"), get_index_file());
691 rollback_lock_file(lock);
692 return clean ? 0 : 1;
693 } else {
694 return try_merge_command(strategy, xopts_nr, xopts,
695 common, head_arg, remoteheads);
696 }
697}
698
699static void count_diff_files(struct diff_queue_struct *q,
700 struct diff_options *opt, void *data)
701{
702 int *count = data;
703
704 (*count) += q->nr;
705}
706
707static int count_unmerged_entries(void)
708{
709 int i, ret = 0;
710
711 for (i = 0; i < active_nr; i++)
712 if (ce_stage(active_cache[i]))
713 ret++;
714
715 return ret;
716}
717
718int checkout_fast_forward(const unsigned char *head, const unsigned char *remote)
719{
720 struct tree *trees[MAX_UNPACK_TREES];
721 struct unpack_trees_options opts;
722 struct tree_desc t[MAX_UNPACK_TREES];
723 int i, fd, nr_trees = 0;
724 struct dir_struct dir;
725 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
726
727 refresh_cache(REFRESH_QUIET);
728
729 fd = hold_locked_index(lock_file, 1);
730
731 memset(&trees, 0, sizeof(trees));
732 memset(&opts, 0, sizeof(opts));
733 memset(&t, 0, sizeof(t));
734 memset(&dir, 0, sizeof(dir));
735 dir.flags |= DIR_SHOW_IGNORED;
736 dir.exclude_per_dir = ".gitignore";
737 opts.dir = &dir;
738
739 opts.head_idx = 1;
740 opts.src_index = &the_index;
741 opts.dst_index = &the_index;
742 opts.update = 1;
743 opts.verbose_update = 1;
744 opts.merge = 1;
745 opts.fn = twoway_merge;
746 setup_unpack_trees_porcelain(&opts, "merge");
747
748 trees[nr_trees] = parse_tree_indirect(head);
749 if (!trees[nr_trees++])
750 return -1;
751 trees[nr_trees] = parse_tree_indirect(remote);
752 if (!trees[nr_trees++])
753 return -1;
754 for (i = 0; i < nr_trees; i++) {
755 parse_tree(trees[i]);
756 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
757 }
758 if (unpack_trees(nr_trees, t, &opts))
759 return -1;
760 if (write_cache(fd, active_cache, active_nr) ||
761 commit_locked_index(lock_file))
762 die(_("unable to write new index file"));
763 return 0;
764}
765
766static void split_merge_strategies(const char *string, struct strategy **list,
767 int *nr, int *alloc)
768{
769 char *p, *q, *buf;
770
771 if (!string)
772 return;
773
774 buf = xstrdup(string);
775 q = buf;
776 for (;;) {
777 p = strchr(q, ' ');
778 if (!p) {
779 ALLOC_GROW(*list, *nr + 1, *alloc);
780 (*list)[(*nr)++].name = xstrdup(q);
781 free(buf);
782 return;
783 } else {
784 *p = '\0';
785 ALLOC_GROW(*list, *nr + 1, *alloc);
786 (*list)[(*nr)++].name = xstrdup(q);
787 q = ++p;
788 }
789 }
790}
791
792static void add_strategies(const char *string, unsigned attr)
793{
794 struct strategy *list = NULL;
795 int list_alloc = 0, list_nr = 0, i;
796
797 memset(&list, 0, sizeof(list));
798 split_merge_strategies(string, &list, &list_nr, &list_alloc);
799 if (list) {
800 for (i = 0; i < list_nr; i++)
801 append_strategy(get_strategy(list[i].name));
802 return;
803 }
804 for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
805 if (all_strategy[i].attr & attr)
806 append_strategy(&all_strategy[i]);
807
808}
809
810static void write_merge_msg(void)
811{
812 int fd = open(git_path("MERGE_MSG"), O_WRONLY | O_CREAT, 0666);
813 if (fd < 0)
814 die_errno(_("Could not open '%s' for writing"),
815 git_path("MERGE_MSG"));
816 if (write_in_full(fd, merge_msg.buf, merge_msg.len) != merge_msg.len)
817 die_errno(_("Could not write to '%s'"), git_path("MERGE_MSG"));
818 close(fd);
819}
820
821static void read_merge_msg(void)
822{
823 strbuf_reset(&merge_msg);
824 if (strbuf_read_file(&merge_msg, git_path("MERGE_MSG"), 0) < 0)
825 die_errno("Could not read from '%s'", git_path("MERGE_MSG"));
826}
827
828static void run_prepare_commit_msg(void)
829{
830 write_merge_msg();
831 run_hook(get_index_file(), "prepare-commit-msg",
832 git_path("MERGE_MSG"), "merge", NULL, NULL);
833 read_merge_msg();
834}
835
836static int merge_trivial(void)
837{
838 unsigned char result_tree[20], result_commit[20];
839 struct commit_list *parent = xmalloc(sizeof(*parent));
840
841 write_tree_trivial(result_tree);
842 printf(_("Wonderful.\n"));
843 parent->item = lookup_commit(head);
844 parent->next = xmalloc(sizeof(*parent->next));
845 parent->next->item = remoteheads->item;
846 parent->next->next = NULL;
847 run_prepare_commit_msg();
848 commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
849 finish(result_commit, "In-index merge");
850 drop_save();
851 return 0;
852}
853
854static int finish_automerge(struct commit_list *common,
855 unsigned char *result_tree,
856 const char *wt_strategy)
857{
858 struct commit_list *parents = NULL, *j;
859 struct strbuf buf = STRBUF_INIT;
860 unsigned char result_commit[20];
861
862 free_commit_list(common);
863 if (allow_fast_forward) {
864 parents = remoteheads;
865 commit_list_insert(lookup_commit(head), &parents);
866 parents = reduce_heads(parents);
867 } else {
868 struct commit_list **pptr = &parents;
869
870 pptr = &commit_list_insert(lookup_commit(head),
871 pptr)->next;
872 for (j = remoteheads; j; j = j->next)
873 pptr = &commit_list_insert(j->item, pptr)->next;
874 }
875 free_commit_list(remoteheads);
876 strbuf_addch(&merge_msg, '\n');
877 run_prepare_commit_msg();
878 commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
879 strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
880 finish(result_commit, buf.buf);
881 strbuf_release(&buf);
882 drop_save();
883 return 0;
884}
885
886static int suggest_conflicts(int renormalizing)
887{
888 FILE *fp;
889 int pos;
890
891 fp = fopen(git_path("MERGE_MSG"), "a");
892 if (!fp)
893 die_errno(_("Could not open '%s' for writing"),
894 git_path("MERGE_MSG"));
895 fprintf(fp, "\nConflicts:\n");
896 for (pos = 0; pos < active_nr; pos++) {
897 struct cache_entry *ce = active_cache[pos];
898
899 if (ce_stage(ce)) {
900 fprintf(fp, "\t%s\n", ce->name);
901 while (pos + 1 < active_nr &&
902 !strcmp(ce->name,
903 active_cache[pos + 1]->name))
904 pos++;
905 }
906 }
907 fclose(fp);
908 rerere(allow_rerere_auto);
909 printf(_("Automatic merge failed; "
910 "fix conflicts and then commit the result.\n"));
911 return 1;
912}
913
914static struct commit *is_old_style_invocation(int argc, const char **argv)
915{
916 struct commit *second_token = NULL;
917 if (argc > 2) {
918 unsigned char second_sha1[20];
919
920 if (get_sha1(argv[1], second_sha1))
921 return NULL;
922 second_token = lookup_commit_reference_gently(second_sha1, 0);
923 if (!second_token)
924 die(_("'%s' is not a commit"), argv[1]);
925 if (hashcmp(second_token->object.sha1, head))
926 return NULL;
927 }
928 return second_token;
929}
930
931static int evaluate_result(void)
932{
933 int cnt = 0;
934 struct rev_info rev;
935
936 /* Check how many files differ. */
937 init_revisions(&rev, "");
938 setup_revisions(0, NULL, &rev, NULL);
939 rev.diffopt.output_format |=
940 DIFF_FORMAT_CALLBACK;
941 rev.diffopt.format_callback = count_diff_files;
942 rev.diffopt.format_callback_data = &cnt;
943 run_diff_files(&rev, 0);
944
945 /*
946 * Check how many unmerged entries are
947 * there.
948 */
949 cnt += count_unmerged_entries();
950
951 return cnt;
952}
953
954/*
955 * Pretend as if the user told us to merge with the tracking
956 * branch we have for the upstream of the current branch
957 */
958static int setup_with_upstream(const char ***argv)
959{
960 struct branch *branch = branch_get(NULL);
961 int i;
962 const char **args;
963
964 if (!branch)
965 die("No current branch.");
966 if (!branch->remote)
967 die("No remote for the current branch.");
968 if (!branch->merge_nr)
969 die("No default upstream defined for the current branch.");
970
971 args = xcalloc(branch->merge_nr + 1, sizeof(char *));
972 for (i = 0; i < branch->merge_nr; i++) {
973 if (!branch->merge[i]->dst)
974 die("No remote tracking branch for %s from %s",
975 branch->merge[i]->src, branch->remote_name);
976 args[i] = branch->merge[i]->dst;
977 }
978 args[i] = NULL;
979 *argv = args;
980 return i;
981}
982
983int cmd_merge(int argc, const char **argv, const char *prefix)
984{
985 unsigned char result_tree[20];
986 struct strbuf buf = STRBUF_INIT;
987 const char *head_arg;
988 int flag, head_invalid = 0, i;
989 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
990 struct commit_list *common = NULL;
991 const char *best_strategy = NULL, *wt_strategy = NULL;
992 struct commit_list **remotes = &remoteheads;
993
994 if (argc == 2 && !strcmp(argv[1], "-h"))
995 usage_with_options(builtin_merge_usage, builtin_merge_options);
996
997 /*
998 * Check if we are _not_ on a detached HEAD, i.e. if there is a
999 * current branch.
1000 */
1001 branch = resolve_ref("HEAD", head, 0, &flag);
1002 if (branch && !prefixcmp(branch, "refs/heads/"))
1003 branch += 11;
1004 if (is_null_sha1(head))
1005 head_invalid = 1;
1006
1007 git_config(git_merge_config, NULL);
1008
1009 /* for color.ui */
1010 if (diff_use_color_default == -1)
1011 diff_use_color_default = git_use_color_default;
1012
1013 argc = parse_options(argc, argv, prefix, builtin_merge_options,
1014 builtin_merge_usage, 0);
1015
1016 if (verbosity < 0 && show_progress == -1)
1017 show_progress = 0;
1018
1019 if (abort_current_merge) {
1020 int nargc = 2;
1021 const char *nargv[] = {"reset", "--merge", NULL};
1022
1023 if (!file_exists(git_path("MERGE_HEAD")))
1024 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1025
1026 /* Invoke 'git reset --merge' */
1027 return cmd_reset(nargc, nargv, prefix);
1028 }
1029
1030 if (read_cache_unmerged())
1031 die_resolve_conflict("merge");
1032
1033 if (file_exists(git_path("MERGE_HEAD"))) {
1034 /*
1035 * There is no unmerged entry, don't advise 'git
1036 * add/rm <file>', just 'git commit'.
1037 */
1038 if (advice_resolve_conflict)
1039 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1040 "Please, commit your changes before you can merge."));
1041 else
1042 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1043 }
1044 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1045 if (advice_resolve_conflict)
1046 die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1047 "Please, commit your changes before you can merge.");
1048 else
1049 die("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).");
1050 }
1051 resolve_undo_clear();
1052
1053 if (verbosity < 0)
1054 show_diffstat = 0;
1055
1056 if (squash) {
1057 if (!allow_fast_forward)
1058 die(_("You cannot combine --squash with --no-ff."));
1059 option_commit = 0;
1060 }
1061
1062 if (!allow_fast_forward && fast_forward_only)
1063 die(_("You cannot combine --no-ff with --ff-only."));
1064
1065 if (!argc && !abort_current_merge && default_to_upstream)
1066 argc = setup_with_upstream(&argv);
1067
1068 if (!argc)
1069 usage_with_options(builtin_merge_usage,
1070 builtin_merge_options);
1071
1072 /*
1073 * This could be traditional "merge <msg> HEAD <commit>..." and
1074 * the way we can tell it is to see if the second token is HEAD,
1075 * but some people might have misused the interface and used a
1076 * committish that is the same as HEAD there instead.
1077 * Traditional format never would have "-m" so it is an
1078 * additional safety measure to check for it.
1079 */
1080
1081 if (!have_message && is_old_style_invocation(argc, argv)) {
1082 strbuf_addstr(&merge_msg, argv[0]);
1083 head_arg = argv[1];
1084 argv += 2;
1085 argc -= 2;
1086 } else if (head_invalid) {
1087 struct object *remote_head;
1088 /*
1089 * If the merged head is a valid one there is no reason
1090 * to forbid "git merge" into a branch yet to be born.
1091 * We do the same for "git pull".
1092 */
1093 if (argc != 1)
1094 die(_("Can merge only exactly one commit into "
1095 "empty head"));
1096 if (squash)
1097 die(_("Squash commit into empty head not supported yet"));
1098 if (!allow_fast_forward)
1099 die(_("Non-fast-forward commit does not make sense into "
1100 "an empty head"));
1101 remote_head = peel_to_type(argv[0], 0, NULL, OBJ_COMMIT);
1102 if (!remote_head)
1103 die(_("%s - not something we can merge"), argv[0]);
1104 read_empty(remote_head->sha1, 0);
1105 update_ref("initial pull", "HEAD", remote_head->sha1, NULL, 0,
1106 DIE_ON_ERR);
1107 return 0;
1108 } else {
1109 struct strbuf merge_names = STRBUF_INIT;
1110
1111 /* We are invoked directly as the first-class UI. */
1112 head_arg = "HEAD";
1113
1114 /*
1115 * All the rest are the commits being merged;
1116 * prepare the standard merge summary message to
1117 * be appended to the given message. If remote
1118 * is invalid we will die later in the common
1119 * codepath so we discard the error in this
1120 * loop.
1121 */
1122 for (i = 0; i < argc; i++)
1123 merge_name(argv[i], &merge_names);
1124
1125 if (!have_message || shortlog_len) {
1126 fmt_merge_msg(&merge_names, &merge_msg, !have_message,
1127 shortlog_len);
1128 if (merge_msg.len)
1129 strbuf_setlen(&merge_msg, merge_msg.len - 1);
1130 }
1131 }
1132
1133 if (head_invalid || !argc)
1134 usage_with_options(builtin_merge_usage,
1135 builtin_merge_options);
1136
1137 strbuf_addstr(&buf, "merge");
1138 for (i = 0; i < argc; i++)
1139 strbuf_addf(&buf, " %s", argv[i]);
1140 setenv("GIT_REFLOG_ACTION", buf.buf, 0);
1141 strbuf_reset(&buf);
1142
1143 for (i = 0; i < argc; i++) {
1144 struct object *o;
1145 struct commit *commit;
1146
1147 o = peel_to_type(argv[i], 0, NULL, OBJ_COMMIT);
1148 if (!o)
1149 die(_("%s - not something we can merge"), argv[i]);
1150 commit = lookup_commit(o->sha1);
1151 commit->util = (void *)argv[i];
1152 remotes = &commit_list_insert(commit, remotes)->next;
1153
1154 strbuf_addf(&buf, "GITHEAD_%s", sha1_to_hex(o->sha1));
1155 setenv(buf.buf, argv[i], 1);
1156 strbuf_reset(&buf);
1157 }
1158
1159 if (!use_strategies) {
1160 if (!remoteheads->next)
1161 add_strategies(pull_twohead, DEFAULT_TWOHEAD);
1162 else
1163 add_strategies(pull_octopus, DEFAULT_OCTOPUS);
1164 }
1165
1166 for (i = 0; i < use_strategies_nr; i++) {
1167 if (use_strategies[i]->attr & NO_FAST_FORWARD)
1168 allow_fast_forward = 0;
1169 if (use_strategies[i]->attr & NO_TRIVIAL)
1170 allow_trivial = 0;
1171 }
1172
1173 if (!remoteheads->next)
1174 common = get_merge_bases(lookup_commit(head),
1175 remoteheads->item, 1);
1176 else {
1177 struct commit_list *list = remoteheads;
1178 commit_list_insert(lookup_commit(head), &list);
1179 common = get_octopus_merge_bases(list);
1180 free(list);
1181 }
1182
1183 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head, NULL, 0,
1184 DIE_ON_ERR);
1185
1186 if (!common)
1187 ; /* No common ancestors found. We need a real merge. */
1188 else if (!remoteheads->next && !common->next &&
1189 common->item == remoteheads->item) {
1190 /*
1191 * If head can reach all the merge then we are up to date.
1192 * but first the most common case of merging one remote.
1193 */
1194 finish_up_to_date("Already up-to-date.");
1195 return 0;
1196 } else if (allow_fast_forward && !remoteheads->next &&
1197 !common->next &&
1198 !hashcmp(common->item->object.sha1, head)) {
1199 /* Again the most common case of merging one remote. */
1200 struct strbuf msg = STRBUF_INIT;
1201 struct object *o;
1202 char hex[41];
1203
1204 strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
1205
1206 if (verbosity >= 0)
1207 printf(_("Updating %s..%s\n"),
1208 hex,
1209 find_unique_abbrev(remoteheads->item->object.sha1,
1210 DEFAULT_ABBREV));
1211 strbuf_addstr(&msg, "Fast-forward");
1212 if (have_message)
1213 strbuf_addstr(&msg,
1214 " (no commit created; -m option ignored)");
1215 o = peel_to_type(sha1_to_hex(remoteheads->item->object.sha1),
1216 0, NULL, OBJ_COMMIT);
1217 if (!o)
1218 return 1;
1219
1220 if (checkout_fast_forward(head, remoteheads->item->object.sha1))
1221 return 1;
1222
1223 finish(o->sha1, msg.buf);
1224 drop_save();
1225 return 0;
1226 } else if (!remoteheads->next && common->next)
1227 ;
1228 /*
1229 * We are not doing octopus and not fast-forward. Need
1230 * a real merge.
1231 */
1232 else if (!remoteheads->next && !common->next && option_commit) {
1233 /*
1234 * We are not doing octopus, not fast-forward, and have
1235 * only one common.
1236 */
1237 refresh_cache(REFRESH_QUIET);
1238 if (allow_trivial && !fast_forward_only) {
1239 /* See if it is really trivial. */
1240 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1241 printf(_("Trying really trivial in-index merge...\n"));
1242 if (!read_tree_trivial(common->item->object.sha1,
1243 head, remoteheads->item->object.sha1))
1244 return merge_trivial();
1245 printf(_("Nope.\n"));
1246 }
1247 } else {
1248 /*
1249 * An octopus. If we can reach all the remote we are up
1250 * to date.
1251 */
1252 int up_to_date = 1;
1253 struct commit_list *j;
1254
1255 for (j = remoteheads; j; j = j->next) {
1256 struct commit_list *common_one;
1257
1258 /*
1259 * Here we *have* to calculate the individual
1260 * merge_bases again, otherwise "git merge HEAD^
1261 * HEAD^^" would be missed.
1262 */
1263 common_one = get_merge_bases(lookup_commit(head),
1264 j->item, 1);
1265 if (hashcmp(common_one->item->object.sha1,
1266 j->item->object.sha1)) {
1267 up_to_date = 0;
1268 break;
1269 }
1270 }
1271 if (up_to_date) {
1272 finish_up_to_date("Already up-to-date. Yeeah!");
1273 return 0;
1274 }
1275 }
1276
1277 if (fast_forward_only)
1278 die(_("Not possible to fast-forward, aborting."));
1279
1280 /* We are going to make a new commit. */
1281 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1282
1283 /*
1284 * At this point, we need a real merge. No matter what strategy
1285 * we use, it would operate on the index, possibly affecting the
1286 * working tree, and when resolved cleanly, have the desired
1287 * tree in the index -- this means that the index must be in
1288 * sync with the head commit. The strategies are responsible
1289 * to ensure this.
1290 */
1291 if (use_strategies_nr != 1) {
1292 /*
1293 * Stash away the local changes so that we can try more
1294 * than one.
1295 */
1296 save_state();
1297 } else {
1298 memcpy(stash, null_sha1, 20);
1299 }
1300
1301 for (i = 0; i < use_strategies_nr; i++) {
1302 int ret;
1303 if (i) {
1304 printf(_("Rewinding the tree to pristine...\n"));
1305 restore_state();
1306 }
1307 if (use_strategies_nr != 1)
1308 printf(_("Trying merge strategy %s...\n"),
1309 use_strategies[i]->name);
1310 /*
1311 * Remember which strategy left the state in the working
1312 * tree.
1313 */
1314 wt_strategy = use_strategies[i]->name;
1315
1316 ret = try_merge_strategy(use_strategies[i]->name,
1317 common, head_arg);
1318 if (!option_commit && !ret) {
1319 merge_was_ok = 1;
1320 /*
1321 * This is necessary here just to avoid writing
1322 * the tree, but later we will *not* exit with
1323 * status code 1 because merge_was_ok is set.
1324 */
1325 ret = 1;
1326 }
1327
1328 if (ret) {
1329 /*
1330 * The backend exits with 1 when conflicts are
1331 * left to be resolved, with 2 when it does not
1332 * handle the given merge at all.
1333 */
1334 if (ret == 1) {
1335 int cnt = evaluate_result();
1336
1337 if (best_cnt <= 0 || cnt <= best_cnt) {
1338 best_strategy = use_strategies[i]->name;
1339 best_cnt = cnt;
1340 }
1341 }
1342 if (merge_was_ok)
1343 break;
1344 else
1345 continue;
1346 }
1347
1348 /* Automerge succeeded. */
1349 write_tree_trivial(result_tree);
1350 automerge_was_ok = 1;
1351 break;
1352 }
1353
1354 /*
1355 * If we have a resulting tree, that means the strategy module
1356 * auto resolved the merge cleanly.
1357 */
1358 if (automerge_was_ok)
1359 return finish_automerge(common, result_tree, wt_strategy);
1360
1361 /*
1362 * Pick the result from the best strategy and have the user fix
1363 * it up.
1364 */
1365 if (!best_strategy) {
1366 restore_state();
1367 if (use_strategies_nr > 1)
1368 fprintf(stderr,
1369 _("No merge strategy handled the merge.\n"));
1370 else
1371 fprintf(stderr, _("Merge with strategy %s failed.\n"),
1372 use_strategies[0]->name);
1373 return 2;
1374 } else if (best_strategy == wt_strategy)
1375 ; /* We already have its result in the working tree. */
1376 else {
1377 printf(_("Rewinding the tree to pristine...\n"));
1378 restore_state();
1379 printf(_("Using the %s to prepare resolving by hand.\n"),
1380 best_strategy);
1381 try_merge_strategy(best_strategy, common, head_arg);
1382 }
1383
1384 if (squash)
1385 finish(NULL, NULL);
1386 else {
1387 int fd;
1388 struct commit_list *j;
1389
1390 for (j = remoteheads; j; j = j->next)
1391 strbuf_addf(&buf, "%s\n",
1392 sha1_to_hex(j->item->object.sha1));
1393 fd = open(git_path("MERGE_HEAD"), O_WRONLY | O_CREAT, 0666);
1394 if (fd < 0)
1395 die_errno(_("Could not open '%s' for writing"),
1396 git_path("MERGE_HEAD"));
1397 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1398 die_errno(_("Could not write to '%s'"), git_path("MERGE_HEAD"));
1399 close(fd);
1400 strbuf_addch(&merge_msg, '\n');
1401 write_merge_msg();
1402 fd = open(git_path("MERGE_MODE"), O_WRONLY | O_CREAT | O_TRUNC, 0666);
1403 if (fd < 0)
1404 die_errno(_("Could not open '%s' for writing"),
1405 git_path("MERGE_MODE"));
1406 strbuf_reset(&buf);
1407 if (!allow_fast_forward)
1408 strbuf_addf(&buf, "no-ff");
1409 if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1410 die_errno(_("Could not write to '%s'"), git_path("MERGE_MODE"));
1411 close(fd);
1412 }
1413
1414 if (merge_was_ok) {
1415 fprintf(stderr, _("Automatic merge went well; "
1416 "stopped before committing as requested\n"));
1417 return 0;
1418 } else
1419 return suggest_conflicts(option_renormalize);
1420}