4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
10 #include "parse-options.h"
12 #include "run-command.h"
18 #include "unpack-trees.h"
19 #include "cache-tree.h"
26 #include "merge-recursive.h"
27 #include "resolve-undo.h"
29 #include "fmt-merge-msg.h"
30 #include "gpg-interface.h"
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)
42 static const char * const builtin_merge_usage
[] = {
43 N_("git merge [options] [<commit>...]"),
44 N_("git merge [options] <msg> HEAD <commit>"),
45 N_("git merge --abort"),
49 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
50 static int option_commit
= 1, allow_fast_forward
= 1;
51 static int fast_forward_only
, option_edit
= -1;
52 static int allow_trivial
= 1, have_message
;
53 static int overwrite_ignore
= 1;
54 static struct strbuf merge_msg
= STRBUF_INIT
;
55 static struct strategy
**use_strategies
;
56 static size_t use_strategies_nr
, use_strategies_alloc
;
57 static const char **xopts
;
58 static size_t xopts_nr
, xopts_alloc
;
59 static const char *branch
;
60 static char *branch_mergeoptions
;
61 static int option_renormalize
;
63 static int allow_rerere_auto
;
64 static int abort_current_merge
;
65 static int show_progress
= -1;
66 static int default_to_upstream
;
67 static const char *sign_commit
;
69 static struct strategy all_strategy
[] = {
70 { "recursive", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
71 { "octopus", DEFAULT_OCTOPUS
},
73 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
74 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
77 static const char *pull_twohead
, *pull_octopus
;
79 static int option_parse_message(const struct option
*opt
,
80 const char *arg
, int unset
)
82 struct strbuf
*buf
= opt
->value
;
85 strbuf_setlen(buf
, 0);
87 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
90 return error(_("switch `m' requires a value"));
94 static struct strategy
*get_strategy(const char *name
)
98 static struct cmdnames main_cmds
, other_cmds
;
104 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
105 if (!strcmp(name
, all_strategy
[i
].name
))
106 return &all_strategy
[i
];
109 struct cmdnames not_strategies
;
112 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
113 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
114 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
116 struct cmdname
*ent
= main_cmds
.names
[i
];
117 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
118 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
119 && !all_strategy
[j
].name
[ent
->len
])
122 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
124 exclude_cmds(&main_cmds
, ¬_strategies
);
126 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
127 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
128 fprintf(stderr
, _("Available strategies are:"));
129 for (i
= 0; i
< main_cmds
.cnt
; i
++)
130 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
131 fprintf(stderr
, ".\n");
132 if (other_cmds
.cnt
) {
133 fprintf(stderr
, _("Available custom strategies are:"));
134 for (i
= 0; i
< other_cmds
.cnt
; i
++)
135 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
136 fprintf(stderr
, ".\n");
141 ret
= xcalloc(1, sizeof(struct strategy
));
142 ret
->name
= xstrdup(name
);
143 ret
->attr
= NO_TRIVIAL
;
147 static void append_strategy(struct strategy
*s
)
149 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
150 use_strategies
[use_strategies_nr
++] = s
;
153 static int option_parse_strategy(const struct option
*opt
,
154 const char *name
, int unset
)
159 append_strategy(get_strategy(name
));
163 static int option_parse_x(const struct option
*opt
,
164 const char *arg
, int unset
)
169 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
170 xopts
[xopts_nr
++] = xstrdup(arg
);
174 static int option_parse_n(const struct option
*opt
,
175 const char *arg
, int unset
)
177 show_diffstat
= unset
;
181 static struct option builtin_merge_options
[] = {
182 { OPTION_CALLBACK
, 'n', NULL
, NULL
, NULL
,
183 N_("do not show a diffstat at the end of the merge"),
184 PARSE_OPT_NOARG
, option_parse_n
},
185 OPT_BOOLEAN(0, "stat", &show_diffstat
,
186 N_("show a diffstat at the end of the merge")),
187 OPT_BOOLEAN(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
188 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
189 N_("add (at most <n>) entries from shortlog to merge commit message"),
190 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
191 OPT_BOOLEAN(0, "squash", &squash
,
192 N_("create a single commit instead of doing a merge")),
193 OPT_BOOLEAN(0, "commit", &option_commit
,
194 N_("perform a commit if the merge succeeds (default)")),
195 OPT_BOOL('e', "edit", &option_edit
,
196 N_("edit message before committing")),
197 OPT_BOOLEAN(0, "ff", &allow_fast_forward
,
198 N_("allow fast-forward (default)")),
199 OPT_BOOLEAN(0, "ff-only", &fast_forward_only
,
200 N_("abort if fast-forward is not possible")),
201 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
202 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
203 N_("merge strategy to use"), option_parse_strategy
),
204 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
205 N_("option for selected merge strategy"), option_parse_x
),
206 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
207 N_("merge commit message (for a non-fast-forward merge)"),
208 option_parse_message
),
209 OPT__VERBOSITY(&verbosity
),
210 OPT_BOOLEAN(0, "abort", &abort_current_merge
,
211 N_("abort the current in-progress merge")),
212 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
213 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key id"),
214 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
215 OPT_BOOLEAN(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
219 /* Cleans up metadata that is uninteresting after a succeeded merge. */
220 static void drop_save(void)
222 unlink(git_path("MERGE_HEAD"));
223 unlink(git_path("MERGE_MSG"));
224 unlink(git_path("MERGE_MODE"));
227 static int save_state(unsigned char *stash
)
230 struct child_process cp
;
231 struct strbuf buffer
= STRBUF_INIT
;
232 const char *argv
[] = {"stash", "create", NULL
};
234 memset(&cp
, 0, sizeof(cp
));
239 if (start_command(&cp
))
240 die(_("could not run stash."));
241 len
= strbuf_read(&buffer
, cp
.out
, 1024);
244 if (finish_command(&cp
) || len
< 0)
245 die(_("stash failed"));
246 else if (!len
) /* no changes */
248 strbuf_setlen(&buffer
, buffer
.len
-1);
249 if (get_sha1(buffer
.buf
, stash
))
250 die(_("not a valid object: %s"), buffer
.buf
);
254 static void read_empty(unsigned const char *sha1
, int verbose
)
259 args
[i
++] = "read-tree";
264 args
[i
++] = EMPTY_TREE_SHA1_HEX
;
265 args
[i
++] = sha1_to_hex(sha1
);
268 if (run_command_v_opt(args
, RUN_GIT_CMD
))
269 die(_("read-tree failed"));
272 static void reset_hard(unsigned const char *sha1
, int verbose
)
277 args
[i
++] = "read-tree";
280 args
[i
++] = "--reset";
282 args
[i
++] = sha1_to_hex(sha1
);
285 if (run_command_v_opt(args
, RUN_GIT_CMD
))
286 die(_("read-tree failed"));
289 static void restore_state(const unsigned char *head
,
290 const unsigned char *stash
)
292 struct strbuf sb
= STRBUF_INIT
;
293 const char *args
[] = { "stash", "apply", NULL
, NULL
};
295 if (is_null_sha1(stash
))
300 args
[2] = sha1_to_hex(stash
);
303 * It is OK to ignore error here, for example when there was
304 * nothing to restore.
306 run_command_v_opt(args
, RUN_GIT_CMD
);
309 refresh_cache(REFRESH_QUIET
);
312 /* This is called when no merge was necessary. */
313 static void finish_up_to_date(const char *msg
)
316 printf("%s%s\n", squash
? _(" (nothing to squash)") : "", msg
);
320 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
323 struct strbuf out
= STRBUF_INIT
;
324 struct commit_list
*j
;
325 const char *filename
;
327 struct pretty_print_context ctx
= {0};
329 printf(_("Squash commit -- not updating HEAD\n"));
330 filename
= git_path("SQUASH_MSG");
331 fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
333 die_errno(_("Could not write to '%s'"), filename
);
335 init_revisions(&rev
, NULL
);
336 rev
.ignore_merges
= 1;
337 rev
.commit_format
= CMIT_FMT_MEDIUM
;
339 commit
->object
.flags
|= UNINTERESTING
;
340 add_pending_object(&rev
, &commit
->object
, NULL
);
342 for (j
= remoteheads
; j
; j
= j
->next
)
343 add_pending_object(&rev
, &j
->item
->object
, NULL
);
345 setup_revisions(0, NULL
, &rev
, NULL
);
346 if (prepare_revision_walk(&rev
))
347 die(_("revision walk setup failed"));
349 ctx
.abbrev
= rev
.abbrev
;
350 ctx
.date_mode
= rev
.date_mode
;
351 ctx
.fmt
= rev
.commit_format
;
353 strbuf_addstr(&out
, "Squashed commit of the following:\n");
354 while ((commit
= get_revision(&rev
)) != NULL
) {
355 strbuf_addch(&out
, '\n');
356 strbuf_addf(&out
, "commit %s\n",
357 sha1_to_hex(commit
->object
.sha1
));
358 pretty_print_commit(&ctx
, commit
, &out
);
360 if (write(fd
, out
.buf
, out
.len
) < 0)
361 die_errno(_("Writing SQUASH_MSG"));
363 die_errno(_("Finishing SQUASH_MSG"));
364 strbuf_release(&out
);
367 static void finish(struct commit
*head_commit
,
368 struct commit_list
*remoteheads
,
369 const unsigned char *new_head
, const char *msg
)
371 struct strbuf reflog_message
= STRBUF_INIT
;
372 const unsigned char *head
= head_commit
->object
.sha1
;
375 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
379 strbuf_addf(&reflog_message
, "%s: %s",
380 getenv("GIT_REFLOG_ACTION"), msg
);
383 squash_message(head_commit
, remoteheads
);
385 if (verbosity
>= 0 && !merge_msg
.len
)
386 printf(_("No merge message -- not updating HEAD\n"));
388 const char *argv_gc_auto
[] = { "gc", "--auto", NULL
};
389 update_ref(reflog_message
.buf
, "HEAD",
393 * We ignore errors in 'gc --auto', since the
394 * user should see them.
396 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
399 if (new_head
&& show_diffstat
) {
400 struct diff_options opts
;
402 opts
.stat_width
= -1; /* use full terminal width */
403 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
404 opts
.output_format
|=
405 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
406 opts
.detect_rename
= DIFF_DETECT_RENAME
;
407 diff_setup_done(&opts
);
408 diff_tree_sha1(head
, new_head
, "", &opts
);
413 /* Run a post-merge hook */
414 run_hook(NULL
, "post-merge", squash
? "1" : "0", NULL
);
416 strbuf_release(&reflog_message
);
419 /* Get the name for the merge commit's message. */
420 static void merge_name(const char *remote
, struct strbuf
*msg
)
422 struct commit
*remote_head
;
423 unsigned char branch_head
[20];
424 struct strbuf buf
= STRBUF_INIT
;
425 struct strbuf bname
= STRBUF_INIT
;
430 strbuf_branchname(&bname
, remote
);
433 memset(branch_head
, 0, sizeof(branch_head
));
434 remote_head
= get_merge_parent(remote
);
436 die(_("'%s' does not point to a commit"), remote
);
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
);
444 if (!prefixcmp(found_ref
, "refs/tags/")) {
445 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
446 sha1_to_hex(branch_head
), remote
);
449 if (!prefixcmp(found_ref
, "refs/remotes/")) {
450 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
451 sha1_to_hex(branch_head
), remote
);
456 /* See if remote matches <name>^^^.. or <name>~<number> */
457 for (len
= 0, ptr
= remote
+ strlen(remote
);
458 remote
< ptr
&& ptr
[-1] == '^';
465 ptr
= strrchr(remote
, '~');
467 int seen_nonzero
= 0;
470 while (*++ptr
&& isdigit(*ptr
)) {
471 seen_nonzero
|= (*ptr
!= '0');
475 len
= 0; /* not ...~<number> */
476 else if (seen_nonzero
)
479 early
= 1; /* "name~" is "name~1"! */
483 struct strbuf truname
= STRBUF_INIT
;
484 strbuf_addstr(&truname
, "refs/heads/");
485 strbuf_addstr(&truname
, remote
);
486 strbuf_setlen(&truname
, truname
.len
- len
);
487 if (ref_exists(truname
.buf
)) {
489 "%s\t\tbranch '%s'%s of .\n",
490 sha1_to_hex(remote_head
->object
.sha1
),
492 (early
? " (early part)" : ""));
493 strbuf_release(&truname
);
498 if (!strcmp(remote
, "FETCH_HEAD") &&
499 !access(git_path("FETCH_HEAD"), R_OK
)) {
500 const char *filename
;
502 struct strbuf line
= STRBUF_INIT
;
505 filename
= git_path("FETCH_HEAD");
506 fp
= fopen(filename
, "r");
508 die_errno(_("could not open '%s' for reading"),
510 strbuf_getline(&line
, fp
, '\n');
512 ptr
= strstr(line
.buf
, "\tnot-for-merge\t");
514 strbuf_remove(&line
, ptr
-line
.buf
+1, 13);
515 strbuf_addbuf(msg
, &line
);
516 strbuf_release(&line
);
519 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
520 sha1_to_hex(remote_head
->object
.sha1
), remote
);
522 strbuf_release(&buf
);
523 strbuf_release(&bname
);
526 static void parse_branch_merge_options(char *bmo
)
533 argc
= split_cmdline(bmo
, &argv
);
535 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
536 split_cmdline_strerror(argc
));
537 argv
= xrealloc(argv
, sizeof(*argv
) * (argc
+ 2));
538 memmove(argv
+ 1, argv
, sizeof(*argv
) * (argc
+ 1));
540 argv
[0] = "branch.*.mergeoptions";
541 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
542 builtin_merge_usage
, 0);
546 static int git_merge_config(const char *k
, const char *v
, void *cb
)
550 if (branch
&& !prefixcmp(k
, "branch.") &&
551 !prefixcmp(k
+ 7, branch
) &&
552 !strcmp(k
+ 7 + strlen(branch
), ".mergeoptions")) {
553 free(branch_mergeoptions
);
554 branch_mergeoptions
= xstrdup(v
);
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
);
564 else if (!strcmp(k
, "merge.renormalize"))
565 option_renormalize
= git_config_bool(k
, v
);
566 else if (!strcmp(k
, "merge.ff")) {
567 int boolval
= git_config_maybe_bool(k
, v
);
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 */
575 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
576 default_to_upstream
= git_config_bool(k
, v
);
580 status
= fmt_merge_msg_config(k
, v
, cb
);
583 status
= git_gpg_config(k
, v
, NULL
);
586 return git_diff_ui_config(k
, v
, cb
);
589 static int read_tree_trivial(unsigned char *common
, unsigned char *head
,
593 struct tree
*trees
[MAX_UNPACK_TREES
];
594 struct tree_desc t
[MAX_UNPACK_TREES
];
595 struct unpack_trees_options opts
;
597 memset(&opts
, 0, sizeof(opts
));
599 opts
.src_index
= &the_index
;
600 opts
.dst_index
= &the_index
;
602 opts
.verbose_update
= 1;
603 opts
.trivial_merges_only
= 1;
605 trees
[nr_trees
] = parse_tree_indirect(common
);
606 if (!trees
[nr_trees
++])
608 trees
[nr_trees
] = parse_tree_indirect(head
);
609 if (!trees
[nr_trees
++])
611 trees
[nr_trees
] = parse_tree_indirect(one
);
612 if (!trees
[nr_trees
++])
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
);
620 if (unpack_trees(nr_trees
, t
, &opts
))
625 static void write_tree_trivial(unsigned char *sha1
)
627 if (write_cache_as_tree(sha1
, 0, NULL
))
628 die(_("git write-tree failed to write a tree"));
631 static const char *merge_argument(struct commit
*commit
)
634 return sha1_to_hex(commit
->object
.sha1
);
636 return EMPTY_TREE_SHA1_HEX
;
639 int try_merge_command(const char *strategy
, size_t xopts_nr
,
640 const char **xopts
, struct commit_list
*common
,
641 const char *head_arg
, struct commit_list
*remotes
)
644 int i
= 0, x
= 0, ret
;
645 struct commit_list
*j
;
646 struct strbuf buf
= STRBUF_INIT
;
648 args
= xmalloc((4 + xopts_nr
+ commit_list_count(common
) +
649 commit_list_count(remotes
)) * sizeof(char *));
650 strbuf_addf(&buf
, "merge-%s", strategy
);
652 for (x
= 0; x
< xopts_nr
; x
++) {
653 char *s
= xmalloc(strlen(xopts
[x
])+2+1);
655 strcpy(s
+2, xopts
[x
]);
658 for (j
= common
; j
; j
= j
->next
)
659 args
[i
++] = xstrdup(merge_argument(j
->item
));
661 args
[i
++] = head_arg
;
662 for (j
= remotes
; j
; j
= j
->next
)
663 args
[i
++] = xstrdup(merge_argument(j
->item
));
665 ret
= run_command_v_opt(args
, RUN_GIT_CMD
);
666 strbuf_release(&buf
);
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
++]);
673 for (j
= remotes
; j
; j
= j
->next
)
674 free((void *)args
[i
++]);
677 if (read_cache() < 0)
678 die(_("failed to read the cache"));
679 resolve_undo_clear();
684 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
685 struct commit_list
*remoteheads
,
686 struct commit
*head
, const char *head_arg
)
689 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
691 index_fd
= hold_locked_index(lock
, 1);
692 refresh_cache(REFRESH_QUIET
);
693 if (active_cache_changed
&&
694 (write_cache(index_fd
, active_cache
, active_nr
) ||
695 commit_locked_index(lock
)))
696 return error(_("Unable to write index."));
697 rollback_lock_file(lock
);
699 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree")) {
701 struct commit
*result
;
702 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
704 struct commit_list
*reversed
= NULL
;
705 struct merge_options o
;
706 struct commit_list
*j
;
708 if (remoteheads
->next
) {
709 error(_("Not handling anything other than two heads merge."));
713 init_merge_options(&o
);
714 if (!strcmp(strategy
, "subtree"))
715 o
.subtree_shift
= "";
717 o
.renormalize
= option_renormalize
;
718 o
.show_rename_progress
=
719 show_progress
== -1 ? isatty(2) : show_progress
;
721 for (x
= 0; x
< xopts_nr
; x
++)
722 if (parse_merge_opt(&o
, xopts
[x
]))
723 die(_("Unknown option for merge-recursive: -X%s"), xopts
[x
]);
725 o
.branch1
= head_arg
;
726 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
728 for (j
= common
; j
; j
= j
->next
)
729 commit_list_insert(j
->item
, &reversed
);
731 index_fd
= hold_locked_index(lock
, 1);
732 clean
= merge_recursive(&o
, head
,
733 remoteheads
->item
, reversed
, &result
);
734 if (active_cache_changed
&&
735 (write_cache(index_fd
, active_cache
, active_nr
) ||
736 commit_locked_index(lock
)))
737 die (_("unable to write %s"), get_index_file());
738 rollback_lock_file(lock
);
739 return clean
? 0 : 1;
741 return try_merge_command(strategy
, xopts_nr
, xopts
,
742 common
, head_arg
, remoteheads
);
746 static void count_diff_files(struct diff_queue_struct
*q
,
747 struct diff_options
*opt
, void *data
)
754 static int count_unmerged_entries(void)
758 for (i
= 0; i
< active_nr
; i
++)
759 if (ce_stage(active_cache
[i
]))
765 int checkout_fast_forward(const unsigned char *head
, const unsigned char *remote
)
767 struct tree
*trees
[MAX_UNPACK_TREES
];
768 struct unpack_trees_options opts
;
769 struct tree_desc t
[MAX_UNPACK_TREES
];
770 int i
, fd
, nr_trees
= 0;
771 struct dir_struct dir
;
772 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
774 refresh_cache(REFRESH_QUIET
);
776 fd
= hold_locked_index(lock_file
, 1);
778 memset(&trees
, 0, sizeof(trees
));
779 memset(&opts
, 0, sizeof(opts
));
780 memset(&t
, 0, sizeof(t
));
781 if (overwrite_ignore
) {
782 memset(&dir
, 0, sizeof(dir
));
783 dir
.flags
|= DIR_SHOW_IGNORED
;
784 setup_standard_excludes(&dir
);
789 opts
.src_index
= &the_index
;
790 opts
.dst_index
= &the_index
;
792 opts
.verbose_update
= 1;
794 opts
.fn
= twoway_merge
;
795 setup_unpack_trees_porcelain(&opts
, "merge");
797 trees
[nr_trees
] = parse_tree_indirect(head
);
798 if (!trees
[nr_trees
++])
800 trees
[nr_trees
] = parse_tree_indirect(remote
);
801 if (!trees
[nr_trees
++])
803 for (i
= 0; i
< nr_trees
; i
++) {
804 parse_tree(trees
[i
]);
805 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
807 if (unpack_trees(nr_trees
, t
, &opts
))
809 if (write_cache(fd
, active_cache
, active_nr
) ||
810 commit_locked_index(lock_file
))
811 die(_("unable to write new index file"));
815 static void split_merge_strategies(const char *string
, struct strategy
**list
,
823 buf
= xstrdup(string
);
828 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
829 (*list
)[(*nr
)++].name
= xstrdup(q
);
834 ALLOC_GROW(*list
, *nr
+ 1, *alloc
);
835 (*list
)[(*nr
)++].name
= xstrdup(q
);
841 static void add_strategies(const char *string
, unsigned attr
)
843 struct strategy
*list
= NULL
;
844 int list_alloc
= 0, list_nr
= 0, i
;
846 memset(&list
, 0, sizeof(list
));
847 split_merge_strategies(string
, &list
, &list_nr
, &list_alloc
);
849 for (i
= 0; i
< list_nr
; i
++)
850 append_strategy(get_strategy(list
[i
].name
));
853 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
854 if (all_strategy
[i
].attr
& attr
)
855 append_strategy(&all_strategy
[i
]);
859 static void write_merge_msg(struct strbuf
*msg
)
861 const char *filename
= git_path("MERGE_MSG");
862 int fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
864 die_errno(_("Could not open '%s' for writing"),
866 if (write_in_full(fd
, msg
->buf
, msg
->len
) != msg
->len
)
867 die_errno(_("Could not write to '%s'"), filename
);
871 static void read_merge_msg(struct strbuf
*msg
)
873 const char *filename
= git_path("MERGE_MSG");
875 if (strbuf_read_file(msg
, filename
, 0) < 0)
876 die_errno(_("Could not read from '%s'"), filename
);
879 static void write_merge_state(struct commit_list
*);
880 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
883 error("%s", err_msg
);
885 _("Not committing merge; use 'git commit' to complete the merge.\n"));
886 write_merge_state(remoteheads
);
890 static const char merge_editor_comment
[] =
891 N_("Please enter a commit message to explain why this merge is necessary,\n"
892 "especially if it merges an updated upstream into a topic branch.\n"
894 "Lines starting with '#' will be ignored, and an empty message aborts\n"
897 static void prepare_to_commit(struct commit_list
*remoteheads
)
899 struct strbuf msg
= STRBUF_INIT
;
900 const char *comment
= _(merge_editor_comment
);
901 strbuf_addbuf(&msg
, &merge_msg
);
902 strbuf_addch(&msg
, '\n');
904 strbuf_add_lines(&msg
, "# ", comment
, strlen(comment
));
905 write_merge_msg(&msg
);
906 run_hook(get_index_file(), "prepare-commit-msg",
907 git_path("MERGE_MSG"), "merge", NULL
, NULL
);
908 if (0 < option_edit
) {
909 if (launch_editor(git_path("MERGE_MSG"), NULL
, NULL
))
910 abort_commit(remoteheads
, NULL
);
912 read_merge_msg(&msg
);
913 stripspace(&msg
, 0 < option_edit
);
915 abort_commit(remoteheads
, _("Empty commit message."));
916 strbuf_release(&merge_msg
);
917 strbuf_addbuf(&merge_msg
, &msg
);
918 strbuf_release(&msg
);
921 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
923 unsigned char result_tree
[20], result_commit
[20];
924 struct commit_list
*parent
= xmalloc(sizeof(*parent
));
926 write_tree_trivial(result_tree
);
927 printf(_("Wonderful.\n"));
929 parent
->next
= xmalloc(sizeof(*parent
->next
));
930 parent
->next
->item
= remoteheads
->item
;
931 parent
->next
->next
= NULL
;
932 prepare_to_commit(remoteheads
);
933 if (commit_tree(&merge_msg
, result_tree
, parent
, result_commit
, NULL
,
935 die(_("failed to write commit object"));
936 finish(head
, remoteheads
, result_commit
, "In-index merge");
941 static int finish_automerge(struct commit
*head
,
943 struct commit_list
*common
,
944 struct commit_list
*remoteheads
,
945 unsigned char *result_tree
,
946 const char *wt_strategy
)
948 struct commit_list
*parents
= NULL
;
949 struct strbuf buf
= STRBUF_INIT
;
950 unsigned char result_commit
[20];
952 free_commit_list(common
);
953 parents
= remoteheads
;
954 if (!head_subsumed
|| !allow_fast_forward
)
955 commit_list_insert(head
, &parents
);
956 strbuf_addch(&merge_msg
, '\n');
957 prepare_to_commit(remoteheads
);
958 if (commit_tree(&merge_msg
, result_tree
, parents
, result_commit
,
960 die(_("failed to write commit object"));
961 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
962 finish(head
, remoteheads
, result_commit
, buf
.buf
);
963 strbuf_release(&buf
);
968 static int suggest_conflicts(int renormalizing
)
970 const char *filename
;
974 filename
= git_path("MERGE_MSG");
975 fp
= fopen(filename
, "a");
977 die_errno(_("Could not open '%s' for writing"), filename
);
978 fprintf(fp
, "\nConflicts:\n");
979 for (pos
= 0; pos
< active_nr
; pos
++) {
980 struct cache_entry
*ce
= active_cache
[pos
];
983 fprintf(fp
, "\t%s\n", ce
->name
);
984 while (pos
+ 1 < active_nr
&&
986 active_cache
[pos
+ 1]->name
))
991 rerere(allow_rerere_auto
);
992 printf(_("Automatic merge failed; "
993 "fix conflicts and then commit the result.\n"));
997 static struct commit
*is_old_style_invocation(int argc
, const char **argv
,
998 const unsigned char *head
)
1000 struct commit
*second_token
= NULL
;
1002 unsigned char second_sha1
[20];
1004 if (get_sha1(argv
[1], second_sha1
))
1006 second_token
= lookup_commit_reference_gently(second_sha1
, 0);
1008 die(_("'%s' is not a commit"), argv
[1]);
1009 if (hashcmp(second_token
->object
.sha1
, head
))
1012 return second_token
;
1015 static int evaluate_result(void)
1018 struct rev_info rev
;
1020 /* Check how many files differ. */
1021 init_revisions(&rev
, "");
1022 setup_revisions(0, NULL
, &rev
, NULL
);
1023 rev
.diffopt
.output_format
|=
1024 DIFF_FORMAT_CALLBACK
;
1025 rev
.diffopt
.format_callback
= count_diff_files
;
1026 rev
.diffopt
.format_callback_data
= &cnt
;
1027 run_diff_files(&rev
, 0);
1030 * Check how many unmerged entries are
1033 cnt
+= count_unmerged_entries();
1039 * Pretend as if the user told us to merge with the tracking
1040 * branch we have for the upstream of the current branch
1042 static int setup_with_upstream(const char ***argv
)
1044 struct branch
*branch
= branch_get(NULL
);
1049 die(_("No current branch."));
1050 if (!branch
->remote
)
1051 die(_("No remote for the current branch."));
1052 if (!branch
->merge_nr
)
1053 die(_("No default upstream defined for the current branch."));
1055 args
= xcalloc(branch
->merge_nr
+ 1, sizeof(char *));
1056 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1057 if (!branch
->merge
[i
]->dst
)
1058 die(_("No remote tracking branch for %s from %s"),
1059 branch
->merge
[i
]->src
, branch
->remote_name
);
1060 args
[i
] = branch
->merge
[i
]->dst
;
1067 static void write_merge_state(struct commit_list
*remoteheads
)
1069 const char *filename
;
1071 struct commit_list
*j
;
1072 struct strbuf buf
= STRBUF_INIT
;
1074 for (j
= remoteheads
; j
; j
= j
->next
) {
1075 unsigned const char *sha1
;
1076 struct commit
*c
= j
->item
;
1077 if (c
->util
&& merge_remote_util(c
)->obj
) {
1078 sha1
= merge_remote_util(c
)->obj
->sha1
;
1080 sha1
= c
->object
.sha1
;
1082 strbuf_addf(&buf
, "%s\n", sha1_to_hex(sha1
));
1084 filename
= git_path("MERGE_HEAD");
1085 fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
1087 die_errno(_("Could not open '%s' for writing"), filename
);
1088 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
)
1089 die_errno(_("Could not write to '%s'"), filename
);
1091 strbuf_addch(&merge_msg
, '\n');
1092 write_merge_msg(&merge_msg
);
1094 filename
= git_path("MERGE_MODE");
1095 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
1097 die_errno(_("Could not open '%s' for writing"), filename
);
1099 if (!allow_fast_forward
)
1100 strbuf_addf(&buf
, "no-ff");
1101 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
)
1102 die_errno(_("Could not write to '%s'"), filename
);
1106 static int default_edit_option(void)
1108 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1109 const char *e
= getenv(name
);
1110 struct stat st_stdin
, st_stdout
;
1113 /* an explicit -m msg without --[no-]edit */
1117 int v
= git_config_maybe_bool(name
, e
);
1119 die("Bad value '%s' in environment '%s'", e
, name
);
1123 /* Use editor if stdin and stdout are the same and is a tty */
1124 return (!fstat(0, &st_stdin
) &&
1125 !fstat(1, &st_stdout
) &&
1126 isatty(0) && isatty(1) &&
1127 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1128 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1129 st_stdin
.st_mode
== st_stdout
.st_mode
);
1132 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1134 int argc
, const char **argv
)
1137 struct commit_list
*remoteheads
= NULL
, *parents
, *next
;
1138 struct commit_list
**remotes
= &remoteheads
;
1141 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1142 for (i
= 0; i
< argc
; i
++) {
1143 struct commit
*commit
= get_merge_parent(argv
[i
]);
1145 die(_("%s - not something we can merge"), argv
[i
]);
1146 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1150 parents
= reduce_heads(remoteheads
);
1152 *head_subsumed
= 1; /* we will flip this to 0 when we find it */
1153 for (remoteheads
= NULL
, remotes
= &remoteheads
;
1156 struct commit
*commit
= parents
->item
;
1157 next
= parents
->next
;
1158 if (commit
== head_commit
)
1161 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1166 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1168 unsigned char result_tree
[20];
1169 unsigned char stash
[20];
1170 unsigned char head_sha1
[20];
1171 struct commit
*head_commit
;
1172 struct strbuf buf
= STRBUF_INIT
;
1173 const char *head_arg
;
1174 int flag
, i
, ret
= 0, head_subsumed
;
1175 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1176 struct commit_list
*common
= NULL
;
1177 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1178 struct commit_list
*remoteheads
, *p
;
1179 void *branch_to_free
;
1181 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1182 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1185 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1188 branch
= branch_to_free
= resolve_refdup("HEAD", head_sha1
, 0, &flag
);
1189 if (branch
&& !prefixcmp(branch
, "refs/heads/"))
1191 if (!branch
|| is_null_sha1(head_sha1
))
1194 head_commit
= lookup_commit_or_die(head_sha1
, "HEAD");
1196 git_config(git_merge_config
, NULL
);
1198 if (branch_mergeoptions
)
1199 parse_branch_merge_options(branch_mergeoptions
);
1200 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1201 builtin_merge_usage
, 0);
1202 if (shortlog_len
< 0)
1203 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1205 if (verbosity
< 0 && show_progress
== -1)
1208 if (abort_current_merge
) {
1210 const char *nargv
[] = {"reset", "--merge", NULL
};
1212 if (!file_exists(git_path("MERGE_HEAD")))
1213 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1215 /* Invoke 'git reset --merge' */
1216 ret
= cmd_reset(nargc
, nargv
, prefix
);
1220 if (read_cache_unmerged())
1221 die_resolve_conflict("merge");
1223 if (file_exists(git_path("MERGE_HEAD"))) {
1225 * There is no unmerged entry, don't advise 'git
1226 * add/rm <file>', just 'git commit'.
1228 if (advice_resolve_conflict
)
1229 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1230 "Please, commit your changes before you can merge."));
1232 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1234 if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1235 if (advice_resolve_conflict
)
1236 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1237 "Please, commit your changes before you can merge."));
1239 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1241 resolve_undo_clear();
1247 if (!allow_fast_forward
)
1248 die(_("You cannot combine --squash with --no-ff."));
1252 if (!allow_fast_forward
&& fast_forward_only
)
1253 die(_("You cannot combine --no-ff with --ff-only."));
1255 if (!abort_current_merge
) {
1257 if (default_to_upstream
)
1258 argc
= setup_with_upstream(&argv
);
1260 die(_("No commit specified and merge.defaultToUpstream not set."));
1261 } else if (argc
== 1 && !strcmp(argv
[0], "-"))
1265 usage_with_options(builtin_merge_usage
,
1266 builtin_merge_options
);
1269 * This could be traditional "merge <msg> HEAD <commit>..." and
1270 * the way we can tell it is to see if the second token is HEAD,
1271 * but some people might have misused the interface and used a
1272 * committish that is the same as HEAD there instead.
1273 * Traditional format never would have "-m" so it is an
1274 * additional safety measure to check for it.
1277 if (!have_message
&& head_commit
&&
1278 is_old_style_invocation(argc
, argv
, head_commit
->object
.sha1
)) {
1279 strbuf_addstr(&merge_msg
, argv
[0]);
1283 remoteheads
= collect_parents(head_commit
, &head_subsumed
, argc
, argv
);
1284 } else if (!head_commit
) {
1285 struct commit
*remote_head
;
1287 * If the merged head is a valid one there is no reason
1288 * to forbid "git merge" into a branch yet to be born.
1289 * We do the same for "git pull".
1292 die(_("Can merge only exactly one commit into "
1295 die(_("Squash commit into empty head not supported yet"));
1296 if (!allow_fast_forward
)
1297 die(_("Non-fast-forward commit does not make sense into "
1299 remoteheads
= collect_parents(head_commit
, &head_subsumed
, argc
, argv
);
1300 remote_head
= remoteheads
->item
;
1302 die(_("%s - not something we can merge"), argv
[0]);
1303 read_empty(remote_head
->object
.sha1
, 0);
1304 update_ref("initial pull", "HEAD", remote_head
->object
.sha1
,
1305 NULL
, 0, DIE_ON_ERR
);
1308 struct strbuf merge_names
= STRBUF_INIT
;
1310 /* We are invoked directly as the first-class UI. */
1314 * All the rest are the commits being merged; prepare
1315 * the standard merge summary message to be appended
1316 * to the given message.
1318 remoteheads
= collect_parents(head_commit
, &head_subsumed
, argc
, argv
);
1319 for (p
= remoteheads
; p
; p
= p
->next
)
1320 merge_name(merge_remote_util(p
->item
)->name
, &merge_names
);
1322 if (!have_message
|| shortlog_len
) {
1323 struct fmt_merge_msg_opts opts
;
1324 memset(&opts
, 0, sizeof(opts
));
1325 opts
.add_title
= !have_message
;
1326 opts
.shortlog_len
= shortlog_len
;
1328 fmt_merge_msg(&merge_names
, &merge_msg
, &opts
);
1330 strbuf_setlen(&merge_msg
, merge_msg
.len
- 1);
1334 if (!head_commit
|| !argc
)
1335 usage_with_options(builtin_merge_usage
,
1336 builtin_merge_options
);
1338 strbuf_addstr(&buf
, "merge");
1339 for (p
= remoteheads
; p
; p
= p
->next
)
1340 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1341 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1344 for (p
= remoteheads
; p
; p
= p
->next
) {
1345 struct commit
*commit
= p
->item
;
1346 strbuf_addf(&buf
, "GITHEAD_%s",
1347 sha1_to_hex(commit
->object
.sha1
));
1348 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1350 if (!fast_forward_only
&&
1351 merge_remote_util(commit
) &&
1352 merge_remote_util(commit
)->obj
&&
1353 merge_remote_util(commit
)->obj
->type
== OBJ_TAG
)
1354 allow_fast_forward
= 0;
1357 if (option_edit
< 0)
1358 option_edit
= default_edit_option();
1360 if (!use_strategies
) {
1362 ; /* already up-to-date */
1363 else if (!remoteheads
->next
)
1364 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1366 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1369 for (i
= 0; i
< use_strategies_nr
; i
++) {
1370 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1371 allow_fast_forward
= 0;
1372 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1377 ; /* already up-to-date */
1378 else if (!remoteheads
->next
)
1379 common
= get_merge_bases(head_commit
, remoteheads
->item
, 1);
1381 struct commit_list
*list
= remoteheads
;
1382 commit_list_insert(head_commit
, &list
);
1383 common
= get_octopus_merge_bases(list
);
1387 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit
->object
.sha1
,
1388 NULL
, 0, DIE_ON_ERR
);
1390 if (remoteheads
&& !common
)
1391 ; /* No common ancestors found. We need a real merge. */
1392 else if (!remoteheads
||
1393 (!remoteheads
->next
&& !common
->next
&&
1394 common
->item
== remoteheads
->item
)) {
1396 * If head can reach all the merge then we are up to date.
1397 * but first the most common case of merging one remote.
1399 finish_up_to_date("Already up-to-date.");
1401 } else if (allow_fast_forward
&& !remoteheads
->next
&&
1403 !hashcmp(common
->item
->object
.sha1
, head_commit
->object
.sha1
)) {
1404 /* Again the most common case of merging one remote. */
1405 struct strbuf msg
= STRBUF_INIT
;
1406 struct commit
*commit
;
1409 strcpy(hex
, find_unique_abbrev(head_commit
->object
.sha1
, DEFAULT_ABBREV
));
1412 printf(_("Updating %s..%s\n"),
1414 find_unique_abbrev(remoteheads
->item
->object
.sha1
,
1416 strbuf_addstr(&msg
, "Fast-forward");
1419 " (no commit created; -m option ignored)");
1420 commit
= remoteheads
->item
;
1426 if (checkout_fast_forward(head_commit
->object
.sha1
,
1427 commit
->object
.sha1
)) {
1432 finish(head_commit
, remoteheads
, commit
->object
.sha1
, msg
.buf
);
1435 } else if (!remoteheads
->next
&& common
->next
)
1438 * We are not doing octopus and not fast-forward. Need
1441 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1443 * We are not doing octopus, not fast-forward, and have
1446 refresh_cache(REFRESH_QUIET
);
1447 if (allow_trivial
&& !fast_forward_only
) {
1448 /* See if it is really trivial. */
1449 git_committer_info(IDENT_STRICT
);
1450 printf(_("Trying really trivial in-index merge...\n"));
1451 if (!read_tree_trivial(common
->item
->object
.sha1
,
1452 head_commit
->object
.sha1
,
1453 remoteheads
->item
->object
.sha1
)) {
1454 ret
= merge_trivial(head_commit
, remoteheads
);
1457 printf(_("Nope.\n"));
1461 * An octopus. If we can reach all the remote we are up
1465 struct commit_list
*j
;
1467 for (j
= remoteheads
; j
; j
= j
->next
) {
1468 struct commit_list
*common_one
;
1471 * Here we *have* to calculate the individual
1472 * merge_bases again, otherwise "git merge HEAD^
1473 * HEAD^^" would be missed.
1475 common_one
= get_merge_bases(head_commit
, j
->item
, 1);
1476 if (hashcmp(common_one
->item
->object
.sha1
,
1477 j
->item
->object
.sha1
)) {
1483 finish_up_to_date("Already up-to-date. Yeeah!");
1488 if (fast_forward_only
)
1489 die(_("Not possible to fast-forward, aborting."));
1491 /* We are going to make a new commit. */
1492 git_committer_info(IDENT_STRICT
);
1495 * At this point, we need a real merge. No matter what strategy
1496 * we use, it would operate on the index, possibly affecting the
1497 * working tree, and when resolved cleanly, have the desired
1498 * tree in the index -- this means that the index must be in
1499 * sync with the head commit. The strategies are responsible
1502 if (use_strategies_nr
== 1 ||
1504 * Stash away the local changes so that we can try more than one.
1507 hashcpy(stash
, null_sha1
);
1509 for (i
= 0; i
< use_strategies_nr
; i
++) {
1512 printf(_("Rewinding the tree to pristine...\n"));
1513 restore_state(head_commit
->object
.sha1
, stash
);
1515 if (use_strategies_nr
!= 1)
1516 printf(_("Trying merge strategy %s...\n"),
1517 use_strategies
[i
]->name
);
1519 * Remember which strategy left the state in the working
1522 wt_strategy
= use_strategies
[i
]->name
;
1524 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1525 common
, remoteheads
,
1526 head_commit
, head_arg
);
1527 if (!option_commit
&& !ret
) {
1530 * This is necessary here just to avoid writing
1531 * the tree, but later we will *not* exit with
1532 * status code 1 because merge_was_ok is set.
1539 * The backend exits with 1 when conflicts are
1540 * left to be resolved, with 2 when it does not
1541 * handle the given merge at all.
1544 int cnt
= evaluate_result();
1546 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1547 best_strategy
= use_strategies
[i
]->name
;
1557 /* Automerge succeeded. */
1558 write_tree_trivial(result_tree
);
1559 automerge_was_ok
= 1;
1564 * If we have a resulting tree, that means the strategy module
1565 * auto resolved the merge cleanly.
1567 if (automerge_was_ok
) {
1568 ret
= finish_automerge(head_commit
, head_subsumed
,
1569 common
, remoteheads
,
1570 result_tree
, wt_strategy
);
1575 * Pick the result from the best strategy and have the user fix
1578 if (!best_strategy
) {
1579 restore_state(head_commit
->object
.sha1
, stash
);
1580 if (use_strategies_nr
> 1)
1582 _("No merge strategy handled the merge.\n"));
1584 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1585 use_strategies
[0]->name
);
1588 } else if (best_strategy
== wt_strategy
)
1589 ; /* We already have its result in the working tree. */
1591 printf(_("Rewinding the tree to pristine...\n"));
1592 restore_state(head_commit
->object
.sha1
, stash
);
1593 printf(_("Using the %s to prepare resolving by hand.\n"),
1595 try_merge_strategy(best_strategy
, common
, remoteheads
,
1596 head_commit
, head_arg
);
1600 finish(head_commit
, remoteheads
, NULL
, NULL
);
1602 write_merge_state(remoteheads
);
1605 fprintf(stderr
, _("Automatic merge went well; "
1606 "stopped before committing as requested\n"));
1608 ret
= suggest_conflicts(option_renormalize
);
1611 free(branch_to_free
);