4 * Copyright (c) 2008 Miklos Vajna <vmiklos@frugalware.org>
6 * Based on git-merge.sh by Junio C Hamano.
9 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "parse-options.h"
15 #include "run-command.h"
18 #include "diff-merges.h"
24 #include "unpack-trees.h"
25 #include "cache-tree.h"
32 #include "merge-recursive.h"
33 #include "merge-ort-wrappers.h"
34 #include "resolve-undo.h"
36 #include "fmt-merge-msg.h"
37 #include "gpg-interface.h"
38 #include "sequencer.h"
39 #include "string-list.h"
44 #include "commit-reach.h"
45 #include "wt-status.h"
46 #include "commit-graph.h"
48 #define DEFAULT_TWOHEAD (1<<0)
49 #define DEFAULT_OCTOPUS (1<<1)
50 #define NO_FAST_FORWARD (1<<2)
51 #define NO_TRIVIAL (1<<3)
58 static const char * const builtin_merge_usage
[] = {
59 N_("git merge [<options>] [<commit>...]"),
61 "git merge --continue",
65 static int show_diffstat
= 1, shortlog_len
= -1, squash
;
66 static int option_commit
= -1;
67 static int option_edit
= -1;
68 static int allow_trivial
= 1, have_message
, verify_signatures
;
69 static int check_trust_level
= 1;
70 static int overwrite_ignore
= 1;
71 static struct strbuf merge_msg
= STRBUF_INIT
;
72 static struct strategy
**use_strategies
;
73 static size_t use_strategies_nr
, use_strategies_alloc
;
74 static const char **xopts
;
75 static size_t xopts_nr
, xopts_alloc
;
76 static const char *branch
;
77 static char *branch_mergeoptions
;
79 static int allow_rerere_auto
;
80 static int abort_current_merge
;
81 static int quit_current_merge
;
82 static int continue_current_merge
;
83 static int allow_unrelated_histories
;
84 static int show_progress
= -1;
85 static int default_to_upstream
= 1;
87 static const char *sign_commit
;
91 static struct strategy all_strategy
[] = {
92 { "recursive", NO_TRIVIAL
},
93 { "octopus", DEFAULT_OCTOPUS
},
94 { "ort", DEFAULT_TWOHEAD
| NO_TRIVIAL
},
96 { "ours", NO_FAST_FORWARD
| NO_TRIVIAL
},
97 { "subtree", NO_FAST_FORWARD
| NO_TRIVIAL
},
100 static const char *pull_twohead
, *pull_octopus
;
108 static enum ff_type fast_forward
= FF_ALLOW
;
110 static const char *cleanup_arg
;
111 static enum commit_msg_cleanup_mode cleanup_mode
;
113 static int option_parse_message(const struct option
*opt
,
114 const char *arg
, int unset
)
116 struct strbuf
*buf
= opt
->value
;
119 strbuf_setlen(buf
, 0);
121 strbuf_addf(buf
, "%s%s", buf
->len
? "\n\n" : "", arg
);
124 return error(_("switch `m' requires a value"));
128 static enum parse_opt_result
option_read_message(struct parse_opt_ctx_t
*ctx
,
129 const struct option
*opt
,
130 const char *arg_not_used
,
133 struct strbuf
*buf
= opt
->value
;
136 BUG_ON_OPT_ARG(arg_not_used
);
138 BUG("-F cannot be negated");
143 } else if (ctx
->argc
> 1) {
147 return error(_("option `%s' requires a value"), opt
->long_name
);
150 strbuf_addch(buf
, '\n');
151 if (ctx
->prefix
&& !is_absolute_path(arg
))
152 arg
= prefix_filename(ctx
->prefix
, arg
);
153 if (strbuf_read_file(buf
, arg
, 0) < 0)
154 return error(_("could not read file '%s'"), arg
);
160 static struct strategy
*get_strategy(const char *name
)
163 struct strategy
*ret
;
164 static struct cmdnames main_cmds
, other_cmds
;
166 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
171 if (default_strategy
&&
172 !strcmp(default_strategy
, "ort") &&
173 !strcmp(name
, "recursive")) {
177 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
178 if (!strcmp(name
, all_strategy
[i
].name
))
179 return &all_strategy
[i
];
182 struct cmdnames not_strategies
;
185 memset(¬_strategies
, 0, sizeof(struct cmdnames
));
186 load_command_list("git-merge-", &main_cmds
, &other_cmds
);
187 for (i
= 0; i
< main_cmds
.cnt
; i
++) {
189 struct cmdname
*ent
= main_cmds
.names
[i
];
190 for (j
= 0; j
< ARRAY_SIZE(all_strategy
); j
++)
191 if (!strncmp(ent
->name
, all_strategy
[j
].name
, ent
->len
)
192 && !all_strategy
[j
].name
[ent
->len
])
195 add_cmdname(¬_strategies
, ent
->name
, ent
->len
);
197 exclude_cmds(&main_cmds
, ¬_strategies
);
199 if (!is_in_cmdlist(&main_cmds
, name
) && !is_in_cmdlist(&other_cmds
, name
)) {
200 fprintf(stderr
, _("Could not find merge strategy '%s'.\n"), name
);
201 fprintf(stderr
, _("Available strategies are:"));
202 for (i
= 0; i
< main_cmds
.cnt
; i
++)
203 fprintf(stderr
, " %s", main_cmds
.names
[i
]->name
);
204 fprintf(stderr
, ".\n");
205 if (other_cmds
.cnt
) {
206 fprintf(stderr
, _("Available custom strategies are:"));
207 for (i
= 0; i
< other_cmds
.cnt
; i
++)
208 fprintf(stderr
, " %s", other_cmds
.names
[i
]->name
);
209 fprintf(stderr
, ".\n");
214 CALLOC_ARRAY(ret
, 1);
215 ret
->name
= xstrdup(name
);
216 ret
->attr
= NO_TRIVIAL
;
220 static void append_strategy(struct strategy
*s
)
222 ALLOC_GROW(use_strategies
, use_strategies_nr
+ 1, use_strategies_alloc
);
223 use_strategies
[use_strategies_nr
++] = s
;
226 static int option_parse_strategy(const struct option
*opt
,
227 const char *name
, int unset
)
232 append_strategy(get_strategy(name
));
236 static int option_parse_x(const struct option
*opt
,
237 const char *arg
, int unset
)
242 ALLOC_GROW(xopts
, xopts_nr
+ 1, xopts_alloc
);
243 xopts
[xopts_nr
++] = xstrdup(arg
);
247 static int option_parse_n(const struct option
*opt
,
248 const char *arg
, int unset
)
251 show_diffstat
= unset
;
255 static struct option builtin_merge_options
[] = {
256 OPT_CALLBACK_F('n', NULL
, NULL
, NULL
,
257 N_("do not show a diffstat at the end of the merge"),
258 PARSE_OPT_NOARG
, option_parse_n
),
259 OPT_BOOL(0, "stat", &show_diffstat
,
260 N_("show a diffstat at the end of the merge")),
261 OPT_BOOL(0, "summary", &show_diffstat
, N_("(synonym to --stat)")),
262 { OPTION_INTEGER
, 0, "log", &shortlog_len
, N_("n"),
263 N_("add (at most <n>) entries from shortlog to merge commit message"),
264 PARSE_OPT_OPTARG
, NULL
, DEFAULT_MERGE_LOG_LEN
},
265 OPT_BOOL(0, "squash", &squash
,
266 N_("create a single commit instead of doing a merge")),
267 OPT_BOOL(0, "commit", &option_commit
,
268 N_("perform a commit if the merge succeeds (default)")),
269 OPT_BOOL('e', "edit", &option_edit
,
270 N_("edit message before committing")),
271 OPT_CLEANUP(&cleanup_arg
),
272 OPT_SET_INT(0, "ff", &fast_forward
, N_("allow fast-forward (default)"), FF_ALLOW
),
273 OPT_SET_INT_F(0, "ff-only", &fast_forward
,
274 N_("abort if fast-forward is not possible"),
275 FF_ONLY
, PARSE_OPT_NONEG
),
276 OPT_RERERE_AUTOUPDATE(&allow_rerere_auto
),
277 OPT_BOOL(0, "verify-signatures", &verify_signatures
,
278 N_("verify that the named commit has a valid GPG signature")),
279 OPT_CALLBACK('s', "strategy", &use_strategies
, N_("strategy"),
280 N_("merge strategy to use"), option_parse_strategy
),
281 OPT_CALLBACK('X', "strategy-option", &xopts
, N_("option=value"),
282 N_("option for selected merge strategy"), option_parse_x
),
283 OPT_CALLBACK('m', "message", &merge_msg
, N_("message"),
284 N_("merge commit message (for a non-fast-forward merge)"),
285 option_parse_message
),
286 { OPTION_LOWLEVEL_CALLBACK
, 'F', "file", &merge_msg
, N_("path"),
287 N_("read message from file"), PARSE_OPT_NONEG
,
288 NULL
, 0, option_read_message
},
289 OPT__VERBOSITY(&verbosity
),
290 OPT_BOOL(0, "abort", &abort_current_merge
,
291 N_("abort the current in-progress merge")),
292 OPT_BOOL(0, "quit", &quit_current_merge
,
293 N_("--abort but leave index and working tree alone")),
294 OPT_BOOL(0, "continue", &continue_current_merge
,
295 N_("continue the current in-progress merge")),
296 OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories
,
297 N_("allow merging unrelated histories")),
298 OPT_SET_INT(0, "progress", &show_progress
, N_("force progress reporting"), 1),
299 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
300 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
301 OPT_AUTOSTASH(&autostash
),
302 OPT_BOOL(0, "overwrite-ignore", &overwrite_ignore
, N_("update ignored files (default)")),
303 OPT_BOOL(0, "signoff", &signoff
, N_("add a Signed-off-by trailer")),
304 OPT_BOOL(0, "no-verify", &no_verify
, N_("bypass pre-merge-commit and commit-msg hooks")),
308 static int save_state(struct object_id
*stash
)
311 struct child_process cp
= CHILD_PROCESS_INIT
;
312 struct strbuf buffer
= STRBUF_INIT
;
315 strvec_pushl(&cp
.args
, "stash", "create", NULL
);
319 if (start_command(&cp
))
320 die(_("could not run stash."));
321 len
= strbuf_read(&buffer
, cp
.out
, 1024);
324 if (finish_command(&cp
) || len
< 0)
325 die(_("stash failed"));
326 else if (!len
) /* no changes */
328 strbuf_setlen(&buffer
, buffer
.len
-1);
329 if (get_oid(buffer
.buf
, stash
))
330 die(_("not a valid object: %s"), buffer
.buf
);
333 strbuf_release(&buffer
);
337 static void read_empty(const struct object_id
*oid
, int verbose
)
342 args
[i
++] = "read-tree";
347 args
[i
++] = empty_tree_oid_hex();
348 args
[i
++] = oid_to_hex(oid
);
351 if (run_command_v_opt(args
, RUN_GIT_CMD
))
352 die(_("read-tree failed"));
355 static void reset_hard(const struct object_id
*oid
, int verbose
)
360 args
[i
++] = "read-tree";
363 args
[i
++] = "--reset";
365 args
[i
++] = oid_to_hex(oid
);
368 if (run_command_v_opt(args
, RUN_GIT_CMD
))
369 die(_("read-tree failed"));
372 static void restore_state(const struct object_id
*head
,
373 const struct object_id
*stash
)
375 struct strbuf sb
= STRBUF_INIT
;
376 const char *args
[] = { "stash", "apply", NULL
, NULL
};
378 if (is_null_oid(stash
))
383 args
[2] = oid_to_hex(stash
);
386 * It is OK to ignore error here, for example when there was
387 * nothing to restore.
389 run_command_v_opt(args
, RUN_GIT_CMD
);
392 refresh_cache(REFRESH_QUIET
);
395 /* This is called when no merge was necessary. */
396 static void finish_up_to_date(void)
398 if (verbosity
>= 0) {
400 puts(_("Already up to date. (nothing to squash)"));
402 puts(_("Already up to date."));
404 remove_merge_branch_state(the_repository
);
407 static void squash_message(struct commit
*commit
, struct commit_list
*remoteheads
)
410 struct strbuf out
= STRBUF_INIT
;
411 struct commit_list
*j
;
412 struct pretty_print_context ctx
= {0};
414 printf(_("Squash commit -- not updating HEAD\n"));
416 repo_init_revisions(the_repository
, &rev
, NULL
);
417 diff_merges_suppress(&rev
);
418 rev
.commit_format
= CMIT_FMT_MEDIUM
;
420 commit
->object
.flags
|= UNINTERESTING
;
421 add_pending_object(&rev
, &commit
->object
, NULL
);
423 for (j
= remoteheads
; j
; j
= j
->next
)
424 add_pending_object(&rev
, &j
->item
->object
, NULL
);
426 setup_revisions(0, NULL
, &rev
, NULL
);
427 if (prepare_revision_walk(&rev
))
428 die(_("revision walk setup failed"));
430 ctx
.abbrev
= rev
.abbrev
;
431 ctx
.date_mode
= rev
.date_mode
;
432 ctx
.fmt
= rev
.commit_format
;
434 strbuf_addstr(&out
, "Squashed commit of the following:\n");
435 while ((commit
= get_revision(&rev
)) != NULL
) {
436 strbuf_addch(&out
, '\n');
437 strbuf_addf(&out
, "commit %s\n",
438 oid_to_hex(&commit
->object
.oid
));
439 pretty_print_commit(&ctx
, commit
, &out
);
441 write_file_buf(git_path_squash_msg(the_repository
), out
.buf
, out
.len
);
442 strbuf_release(&out
);
445 static void finish(struct commit
*head_commit
,
446 struct commit_list
*remoteheads
,
447 const struct object_id
*new_head
, const char *msg
)
449 struct strbuf reflog_message
= STRBUF_INIT
;
450 const struct object_id
*head
= &head_commit
->object
.oid
;
453 strbuf_addstr(&reflog_message
, getenv("GIT_REFLOG_ACTION"));
457 strbuf_addf(&reflog_message
, "%s: %s",
458 getenv("GIT_REFLOG_ACTION"), msg
);
461 squash_message(head_commit
, remoteheads
);
463 if (verbosity
>= 0 && !merge_msg
.len
)
464 printf(_("No merge message -- not updating HEAD\n"));
466 update_ref(reflog_message
.buf
, "HEAD", new_head
, head
,
467 0, UPDATE_REFS_DIE_ON_ERR
);
469 * We ignore errors in 'gc --auto', since the
470 * user should see them.
472 run_auto_maintenance(verbosity
< 0);
475 if (new_head
&& show_diffstat
) {
476 struct diff_options opts
;
477 repo_diff_setup(the_repository
, &opts
);
478 opts
.stat_width
= -1; /* use full terminal width */
479 opts
.stat_graph_width
= -1; /* respect statGraphWidth config */
480 opts
.output_format
|=
481 DIFF_FORMAT_SUMMARY
| DIFF_FORMAT_DIFFSTAT
;
482 opts
.detect_rename
= DIFF_DETECT_RENAME
;
483 diff_setup_done(&opts
);
484 diff_tree_oid(head
, new_head
, "", &opts
);
489 /* Run a post-merge hook */
490 run_hook_le(NULL
, "post-merge", squash
? "1" : "0", NULL
);
492 apply_autostash(git_path_merge_autostash(the_repository
));
493 strbuf_release(&reflog_message
);
496 /* Get the name for the merge commit's message. */
497 static void merge_name(const char *remote
, struct strbuf
*msg
)
499 struct commit
*remote_head
;
500 struct object_id branch_head
;
501 struct strbuf buf
= STRBUF_INIT
;
502 struct strbuf bname
= STRBUF_INIT
;
503 struct merge_remote_desc
*desc
;
505 char *found_ref
= NULL
;
508 strbuf_branchname(&bname
, remote
, 0);
511 oidclr(&branch_head
);
512 remote_head
= get_merge_parent(remote
);
514 die(_("'%s' does not point to a commit"), remote
);
516 if (dwim_ref(remote
, strlen(remote
), &branch_head
, &found_ref
, 0) > 0) {
517 if (starts_with(found_ref
, "refs/heads/")) {
518 strbuf_addf(msg
, "%s\t\tbranch '%s' of .\n",
519 oid_to_hex(&branch_head
), remote
);
522 if (starts_with(found_ref
, "refs/tags/")) {
523 strbuf_addf(msg
, "%s\t\ttag '%s' of .\n",
524 oid_to_hex(&branch_head
), remote
);
527 if (starts_with(found_ref
, "refs/remotes/")) {
528 strbuf_addf(msg
, "%s\t\tremote-tracking branch '%s' of .\n",
529 oid_to_hex(&branch_head
), remote
);
534 /* See if remote matches <name>^^^.. or <name>~<number> */
535 for (len
= 0, ptr
= remote
+ strlen(remote
);
536 remote
< ptr
&& ptr
[-1] == '^';
543 ptr
= strrchr(remote
, '~');
545 int seen_nonzero
= 0;
548 while (*++ptr
&& isdigit(*ptr
)) {
549 seen_nonzero
|= (*ptr
!= '0');
553 len
= 0; /* not ...~<number> */
554 else if (seen_nonzero
)
557 early
= 1; /* "name~" is "name~1"! */
561 struct strbuf truname
= STRBUF_INIT
;
562 strbuf_addf(&truname
, "refs/heads/%s", remote
);
563 strbuf_setlen(&truname
, truname
.len
- len
);
564 if (ref_exists(truname
.buf
)) {
566 "%s\t\tbranch '%s'%s of .\n",
567 oid_to_hex(&remote_head
->object
.oid
),
569 (early
? " (early part)" : ""));
570 strbuf_release(&truname
);
573 strbuf_release(&truname
);
576 desc
= merge_remote_util(remote_head
);
577 if (desc
&& desc
->obj
&& desc
->obj
->type
== OBJ_TAG
) {
578 strbuf_addf(msg
, "%s\t\t%s '%s'\n",
579 oid_to_hex(&desc
->obj
->oid
),
580 type_name(desc
->obj
->type
),
585 strbuf_addf(msg
, "%s\t\tcommit '%s'\n",
586 oid_to_hex(&remote_head
->object
.oid
), remote
);
589 strbuf_release(&buf
);
590 strbuf_release(&bname
);
593 static void parse_branch_merge_options(char *bmo
)
600 argc
= split_cmdline(bmo
, &argv
);
602 die(_("Bad branch.%s.mergeoptions string: %s"), branch
,
603 _(split_cmdline_strerror(argc
)));
604 REALLOC_ARRAY(argv
, argc
+ 2);
605 MOVE_ARRAY(argv
+ 1, argv
, argc
+ 1);
607 argv
[0] = "branch.*.mergeoptions";
608 parse_options(argc
, argv
, NULL
, builtin_merge_options
,
609 builtin_merge_usage
, 0);
613 static int git_merge_config(const char *k
, const char *v
, void *cb
)
619 skip_prefix(k
, "branch.", &str
) &&
620 skip_prefix(str
, branch
, &str
) &&
621 !strcmp(str
, ".mergeoptions")) {
622 free(branch_mergeoptions
);
623 branch_mergeoptions
= xstrdup(v
);
627 if (!strcmp(k
, "merge.diffstat") || !strcmp(k
, "merge.stat"))
628 show_diffstat
= git_config_bool(k
, v
);
629 else if (!strcmp(k
, "merge.verifysignatures"))
630 verify_signatures
= git_config_bool(k
, v
);
631 else if (!strcmp(k
, "pull.twohead"))
632 return git_config_string(&pull_twohead
, k
, v
);
633 else if (!strcmp(k
, "pull.octopus"))
634 return git_config_string(&pull_octopus
, k
, v
);
635 else if (!strcmp(k
, "commit.cleanup"))
636 return git_config_string(&cleanup_arg
, k
, v
);
637 else if (!strcmp(k
, "merge.ff")) {
638 int boolval
= git_parse_maybe_bool(v
);
640 fast_forward
= boolval
? FF_ALLOW
: FF_NO
;
641 } else if (v
&& !strcmp(v
, "only")) {
642 fast_forward
= FF_ONLY
;
643 } /* do not barf on values from future versions of git */
645 } else if (!strcmp(k
, "merge.defaulttoupstream")) {
646 default_to_upstream
= git_config_bool(k
, v
);
648 } else if (!strcmp(k
, "commit.gpgsign")) {
649 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
651 } else if (!strcmp(k
, "gpg.mintrustlevel")) {
652 check_trust_level
= 0;
653 } else if (!strcmp(k
, "merge.autostash")) {
654 autostash
= git_config_bool(k
, v
);
658 status
= fmt_merge_msg_config(k
, v
, cb
);
661 status
= git_gpg_config(k
, v
, NULL
);
664 return git_diff_ui_config(k
, v
, cb
);
667 static int read_tree_trivial(struct object_id
*common
, struct object_id
*head
,
668 struct object_id
*one
)
671 struct tree
*trees
[MAX_UNPACK_TREES
];
672 struct tree_desc t
[MAX_UNPACK_TREES
];
673 struct unpack_trees_options opts
;
675 memset(&opts
, 0, sizeof(opts
));
677 opts
.src_index
= &the_index
;
678 opts
.dst_index
= &the_index
;
680 opts
.verbose_update
= 1;
681 opts
.trivial_merges_only
= 1;
683 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
684 trees
[nr_trees
] = parse_tree_indirect(common
);
685 if (!trees
[nr_trees
++])
687 trees
[nr_trees
] = parse_tree_indirect(head
);
688 if (!trees
[nr_trees
++])
690 trees
[nr_trees
] = parse_tree_indirect(one
);
691 if (!trees
[nr_trees
++])
693 opts
.fn
= threeway_merge
;
694 cache_tree_free(&active_cache_tree
);
695 for (i
= 0; i
< nr_trees
; i
++) {
696 parse_tree(trees
[i
]);
697 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
699 if (unpack_trees(nr_trees
, t
, &opts
))
704 static void write_tree_trivial(struct object_id
*oid
)
706 if (write_cache_as_tree(oid
, 0, NULL
))
707 die(_("git write-tree failed to write a tree"));
710 static int try_merge_strategy(const char *strategy
, struct commit_list
*common
,
711 struct commit_list
*remoteheads
,
714 const char *head_arg
= "HEAD";
716 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
717 return error(_("Unable to write index."));
719 if (!strcmp(strategy
, "recursive") || !strcmp(strategy
, "subtree") ||
720 !strcmp(strategy
, "ort")) {
721 struct lock_file lock
= LOCK_INIT
;
723 struct commit
*result
;
724 struct commit_list
*reversed
= NULL
;
725 struct merge_options o
;
726 struct commit_list
*j
;
728 if (remoteheads
->next
) {
729 error(_("Not handling anything other than two heads merge."));
733 init_merge_options(&o
, the_repository
);
734 if (!strcmp(strategy
, "subtree"))
735 o
.subtree_shift
= "";
737 o
.show_rename_progress
=
738 show_progress
== -1 ? isatty(2) : show_progress
;
740 for (x
= 0; x
< xopts_nr
; x
++)
741 if (parse_merge_opt(&o
, xopts
[x
]))
742 die(_("unknown strategy option: -X%s"), xopts
[x
]);
744 o
.branch1
= head_arg
;
745 o
.branch2
= merge_remote_util(remoteheads
->item
)->name
;
747 for (j
= common
; j
; j
= j
->next
)
748 commit_list_insert(j
->item
, &reversed
);
750 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
751 if (!strcmp(strategy
, "ort"))
752 clean
= merge_ort_recursive(&o
, head
, remoteheads
->item
,
755 clean
= merge_recursive(&o
, head
, remoteheads
->item
,
759 if (write_locked_index(&the_index
, &lock
,
760 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
761 die(_("unable to write %s"), get_index_file());
762 return clean
? 0 : 1;
764 return try_merge_command(the_repository
,
765 strategy
, xopts_nr
, xopts
,
766 common
, head_arg
, remoteheads
);
770 static void count_diff_files(struct diff_queue_struct
*q
,
771 struct diff_options
*opt
, void *data
)
778 static int count_unmerged_entries(void)
782 for (i
= 0; i
< active_nr
; i
++)
783 if (ce_stage(active_cache
[i
]))
789 static void add_strategies(const char *string
, unsigned attr
)
794 struct string_list list
= STRING_LIST_INIT_DUP
;
795 struct string_list_item
*item
;
796 string_list_split(&list
, string
, ' ', -1);
797 for_each_string_list_item(item
, &list
)
798 append_strategy(get_strategy(item
->string
));
799 string_list_clear(&list
, 0);
802 for (i
= 0; i
< ARRAY_SIZE(all_strategy
); i
++)
803 if (all_strategy
[i
].attr
& attr
)
804 append_strategy(&all_strategy
[i
]);
808 static void read_merge_msg(struct strbuf
*msg
)
810 const char *filename
= git_path_merge_msg(the_repository
);
812 if (strbuf_read_file(msg
, filename
, 0) < 0)
813 die_errno(_("Could not read from '%s'"), filename
);
816 static void write_merge_state(struct commit_list
*);
817 static void abort_commit(struct commit_list
*remoteheads
, const char *err_msg
)
820 error("%s", err_msg
);
822 _("Not committing merge; use 'git commit' to complete the merge.\n"));
823 write_merge_state(remoteheads
);
827 static const char merge_editor_comment
[] =
828 N_("Please enter a commit message to explain why this merge is necessary,\n"
829 "especially if it merges an updated upstream into a topic branch.\n"
832 static const char scissors_editor_comment
[] =
833 N_("An empty message aborts the commit.\n");
835 static const char no_scissors_editor_comment
[] =
836 N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
839 static void write_merge_heads(struct commit_list
*);
840 static void prepare_to_commit(struct commit_list
*remoteheads
)
842 struct strbuf msg
= STRBUF_INIT
;
843 const char *index_file
= get_index_file();
845 if (!no_verify
&& run_commit_hook(0 < option_edit
, index_file
, "pre-merge-commit", NULL
))
846 abort_commit(remoteheads
, NULL
);
848 * Re-read the index as pre-merge-commit hook could have updated it,
849 * and write it out as a tree. We must do this before we invoke
850 * the editor and after we invoke run_status above.
852 if (hook_exists("pre-merge-commit"))
854 read_cache_from(index_file
);
855 strbuf_addbuf(&msg
, &merge_msg
);
857 BUG("the control must not reach here under --squash");
858 if (0 < option_edit
) {
859 strbuf_addch(&msg
, '\n');
860 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
861 wt_status_append_cut_line(&msg
);
862 strbuf_commented_addf(&msg
, "\n");
864 strbuf_commented_addf(&msg
, _(merge_editor_comment
));
865 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
866 strbuf_commented_addf(&msg
, _(scissors_editor_comment
));
868 strbuf_commented_addf(&msg
,
869 _(no_scissors_editor_comment
), comment_line_char
);
872 append_signoff(&msg
, ignore_non_trailer(msg
.buf
, msg
.len
), 0);
873 write_merge_heads(remoteheads
);
874 write_file_buf(git_path_merge_msg(the_repository
), msg
.buf
, msg
.len
);
875 if (run_commit_hook(0 < option_edit
, get_index_file(), "prepare-commit-msg",
876 git_path_merge_msg(the_repository
), "merge", NULL
))
877 abort_commit(remoteheads
, NULL
);
878 if (0 < option_edit
) {
879 if (launch_editor(git_path_merge_msg(the_repository
), NULL
, NULL
))
880 abort_commit(remoteheads
, NULL
);
883 if (!no_verify
&& run_commit_hook(0 < option_edit
, get_index_file(),
885 git_path_merge_msg(the_repository
), NULL
))
886 abort_commit(remoteheads
, NULL
);
888 read_merge_msg(&msg
);
889 cleanup_message(&msg
, cleanup_mode
, 0);
891 abort_commit(remoteheads
, _("Empty commit message."));
892 strbuf_release(&merge_msg
);
893 strbuf_addbuf(&merge_msg
, &msg
);
894 strbuf_release(&msg
);
897 static int merge_trivial(struct commit
*head
, struct commit_list
*remoteheads
)
899 struct object_id result_tree
, result_commit
;
900 struct commit_list
*parents
, **pptr
= &parents
;
902 if (refresh_and_write_cache(REFRESH_QUIET
, SKIP_IF_UNCHANGED
, 0) < 0)
903 return error(_("Unable to write index."));
905 write_tree_trivial(&result_tree
);
906 printf(_("Wonderful.\n"));
907 pptr
= commit_list_append(head
, pptr
);
908 pptr
= commit_list_append(remoteheads
->item
, pptr
);
909 prepare_to_commit(remoteheads
);
910 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, &result_tree
, parents
,
911 &result_commit
, NULL
, sign_commit
))
912 die(_("failed to write commit object"));
913 finish(head
, remoteheads
, &result_commit
, "In-index merge");
914 remove_merge_branch_state(the_repository
);
918 static int finish_automerge(struct commit
*head
,
920 struct commit_list
*common
,
921 struct commit_list
*remoteheads
,
922 struct object_id
*result_tree
,
923 const char *wt_strategy
)
925 struct commit_list
*parents
= NULL
;
926 struct strbuf buf
= STRBUF_INIT
;
927 struct object_id result_commit
;
929 write_tree_trivial(result_tree
);
930 free_commit_list(common
);
931 parents
= remoteheads
;
932 if (!head_subsumed
|| fast_forward
== FF_NO
)
933 commit_list_insert(head
, &parents
);
934 prepare_to_commit(remoteheads
);
935 if (commit_tree(merge_msg
.buf
, merge_msg
.len
, result_tree
, parents
,
936 &result_commit
, NULL
, sign_commit
))
937 die(_("failed to write commit object"));
938 strbuf_addf(&buf
, "Merge made by the '%s' strategy.", wt_strategy
);
939 finish(head
, remoteheads
, &result_commit
, buf
.buf
);
940 strbuf_release(&buf
);
941 remove_merge_branch_state(the_repository
);
945 static int suggest_conflicts(void)
947 const char *filename
;
949 struct strbuf msgbuf
= STRBUF_INIT
;
951 filename
= git_path_merge_msg(the_repository
);
952 fp
= xfopen(filename
, "a");
955 * We can't use cleanup_mode because if we're not using the editor,
956 * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
957 * though the message is meant to be processed later by git-commit.
958 * Thus, we will get the cleanup mode which is returned when we _are_
961 append_conflicts_hint(&the_index
, &msgbuf
,
962 get_cleanup_mode(cleanup_arg
, 1));
963 fputs(msgbuf
.buf
, fp
);
964 strbuf_release(&msgbuf
);
966 repo_rerere(the_repository
, allow_rerere_auto
);
967 printf(_("Automatic merge failed; "
968 "fix conflicts and then commit the result.\n"));
972 static int evaluate_result(void)
977 /* Check how many files differ. */
978 repo_init_revisions(the_repository
, &rev
, "");
979 setup_revisions(0, NULL
, &rev
, NULL
);
980 rev
.diffopt
.output_format
|=
981 DIFF_FORMAT_CALLBACK
;
982 rev
.diffopt
.format_callback
= count_diff_files
;
983 rev
.diffopt
.format_callback_data
= &cnt
;
984 run_diff_files(&rev
, 0);
987 * Check how many unmerged entries are
990 cnt
+= count_unmerged_entries();
996 * Pretend as if the user told us to merge with the remote-tracking
997 * branch we have for the upstream of the current branch
999 static int setup_with_upstream(const char ***argv
)
1001 struct branch
*branch
= branch_get(NULL
);
1006 die(_("No current branch."));
1007 if (!branch
->remote_name
)
1008 die(_("No remote for the current branch."));
1009 if (!branch
->merge_nr
)
1010 die(_("No default upstream defined for the current branch."));
1012 args
= xcalloc(st_add(branch
->merge_nr
, 1), sizeof(char *));
1013 for (i
= 0; i
< branch
->merge_nr
; i
++) {
1014 if (!branch
->merge
[i
]->dst
)
1015 die(_("No remote-tracking branch for %s from %s"),
1016 branch
->merge
[i
]->src
, branch
->remote_name
);
1017 args
[i
] = branch
->merge
[i
]->dst
;
1024 static void write_merge_heads(struct commit_list
*remoteheads
)
1026 struct commit_list
*j
;
1027 struct strbuf buf
= STRBUF_INIT
;
1029 for (j
= remoteheads
; j
; j
= j
->next
) {
1030 struct object_id
*oid
;
1031 struct commit
*c
= j
->item
;
1032 struct merge_remote_desc
*desc
;
1034 desc
= merge_remote_util(c
);
1035 if (desc
&& desc
->obj
) {
1036 oid
= &desc
->obj
->oid
;
1038 oid
= &c
->object
.oid
;
1040 strbuf_addf(&buf
, "%s\n", oid_to_hex(oid
));
1042 write_file_buf(git_path_merge_head(the_repository
), buf
.buf
, buf
.len
);
1045 if (fast_forward
== FF_NO
)
1046 strbuf_addstr(&buf
, "no-ff");
1047 write_file_buf(git_path_merge_mode(the_repository
), buf
.buf
, buf
.len
);
1048 strbuf_release(&buf
);
1051 static void write_merge_state(struct commit_list
*remoteheads
)
1053 write_merge_heads(remoteheads
);
1054 strbuf_addch(&merge_msg
, '\n');
1055 write_file_buf(git_path_merge_msg(the_repository
), merge_msg
.buf
,
1059 static int default_edit_option(void)
1061 static const char name
[] = "GIT_MERGE_AUTOEDIT";
1062 const char *e
= getenv(name
);
1063 struct stat st_stdin
, st_stdout
;
1066 /* an explicit -m msg without --[no-]edit */
1070 int v
= git_parse_maybe_bool(e
);
1072 die(_("Bad value '%s' in environment '%s'"), e
, name
);
1076 /* Use editor if stdin and stdout are the same and is a tty */
1077 return (!fstat(0, &st_stdin
) &&
1078 !fstat(1, &st_stdout
) &&
1079 isatty(0) && isatty(1) &&
1080 st_stdin
.st_dev
== st_stdout
.st_dev
&&
1081 st_stdin
.st_ino
== st_stdout
.st_ino
&&
1082 st_stdin
.st_mode
== st_stdout
.st_mode
);
1085 static struct commit_list
*reduce_parents(struct commit
*head_commit
,
1087 struct commit_list
*remoteheads
)
1089 struct commit_list
*parents
, **remotes
;
1092 * Is the current HEAD reachable from another commit being
1093 * merged? If so we do not want to record it as a parent of
1094 * the resulting merge, unless --no-ff is given. We will flip
1095 * this variable to 0 when we find HEAD among the independent
1096 * tips being merged.
1100 /* Find what parents to record by checking independent ones. */
1101 parents
= reduce_heads(remoteheads
);
1102 free_commit_list(remoteheads
);
1105 remotes
= &remoteheads
;
1107 struct commit
*commit
= pop_commit(&parents
);
1108 if (commit
== head_commit
)
1111 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1116 static void prepare_merge_message(struct strbuf
*merge_names
, struct strbuf
*merge_msg
)
1118 struct fmt_merge_msg_opts opts
;
1120 memset(&opts
, 0, sizeof(opts
));
1121 opts
.add_title
= !have_message
;
1122 opts
.shortlog_len
= shortlog_len
;
1123 opts
.credit_people
= (0 < option_edit
);
1125 fmt_merge_msg(merge_names
, merge_msg
, &opts
);
1127 strbuf_setlen(merge_msg
, merge_msg
->len
- 1);
1130 static void handle_fetch_head(struct commit_list
**remotes
, struct strbuf
*merge_names
)
1132 const char *filename
;
1134 struct strbuf fetch_head_file
= STRBUF_INIT
;
1135 const unsigned hexsz
= the_hash_algo
->hexsz
;
1138 merge_names
= &fetch_head_file
;
1140 filename
= git_path_fetch_head(the_repository
);
1141 fd
= xopen(filename
, O_RDONLY
);
1143 if (strbuf_read(merge_names
, fd
, 0) < 0)
1144 die_errno(_("could not read '%s'"), filename
);
1146 die_errno(_("could not close '%s'"), filename
);
1148 for (pos
= 0; pos
< merge_names
->len
; pos
= npos
) {
1149 struct object_id oid
;
1151 struct commit
*commit
;
1153 ptr
= strchr(merge_names
->buf
+ pos
, '\n');
1155 npos
= ptr
- merge_names
->buf
+ 1;
1157 npos
= merge_names
->len
;
1159 if (npos
- pos
< hexsz
+ 2 ||
1160 get_oid_hex(merge_names
->buf
+ pos
, &oid
))
1161 commit
= NULL
; /* bad */
1162 else if (memcmp(merge_names
->buf
+ pos
+ hexsz
, "\t\t", 2))
1163 continue; /* not-for-merge */
1165 char saved
= merge_names
->buf
[pos
+ hexsz
];
1166 merge_names
->buf
[pos
+ hexsz
] = '\0';
1167 commit
= get_merge_parent(merge_names
->buf
+ pos
);
1168 merge_names
->buf
[pos
+ hexsz
] = saved
;
1173 die(_("not something we can merge in %s: %s"),
1174 filename
, merge_names
->buf
+ pos
);
1176 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1179 if (merge_names
== &fetch_head_file
)
1180 strbuf_release(&fetch_head_file
);
1183 static struct commit_list
*collect_parents(struct commit
*head_commit
,
1185 int argc
, const char **argv
,
1186 struct strbuf
*merge_msg
)
1189 struct commit_list
*remoteheads
= NULL
;
1190 struct commit_list
**remotes
= &remoteheads
;
1191 struct strbuf merge_names
= STRBUF_INIT
, *autogen
= NULL
;
1193 if (merge_msg
&& (!have_message
|| shortlog_len
))
1194 autogen
= &merge_names
;
1197 remotes
= &commit_list_insert(head_commit
, remotes
)->next
;
1199 if (argc
== 1 && !strcmp(argv
[0], "FETCH_HEAD")) {
1200 handle_fetch_head(remotes
, autogen
);
1201 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1203 for (i
= 0; i
< argc
; i
++) {
1204 struct commit
*commit
= get_merge_parent(argv
[i
]);
1206 help_unknown_ref(argv
[i
], "merge",
1207 _("not something we can merge"));
1208 remotes
= &commit_list_insert(commit
, remotes
)->next
;
1210 remoteheads
= reduce_parents(head_commit
, head_subsumed
, remoteheads
);
1212 struct commit_list
*p
;
1213 for (p
= remoteheads
; p
; p
= p
->next
)
1214 merge_name(merge_remote_util(p
->item
)->name
, autogen
);
1219 prepare_merge_message(autogen
, merge_msg
);
1220 strbuf_release(autogen
);
1226 static int merging_a_throwaway_tag(struct commit
*commit
)
1229 struct object_id oid
;
1230 int is_throwaway_tag
= 0;
1232 /* Are we merging a tag? */
1233 if (!merge_remote_util(commit
) ||
1234 !merge_remote_util(commit
)->obj
||
1235 merge_remote_util(commit
)->obj
->type
!= OBJ_TAG
)
1236 return is_throwaway_tag
;
1239 * Now we know we are merging a tag object. Are we downstream
1240 * and following the tags from upstream? If so, we must have
1241 * the tag object pointed at by "refs/tags/$T" where $T is the
1242 * tagname recorded in the tag object. We want to allow such
1243 * a "just to catch up" merge to fast-forward.
1245 * Otherwise, we are playing an integrator's role, making a
1246 * merge with a throw-away tag from a contributor with
1247 * something like "git pull $contributor $signed_tag".
1248 * We want to forbid such a merge from fast-forwarding
1249 * by default; otherwise we would not keep the signature
1252 tag_ref
= xstrfmt("refs/tags/%s",
1253 ((struct tag
*)merge_remote_util(commit
)->obj
)->tag
);
1254 if (!read_ref(tag_ref
, &oid
) &&
1255 oideq(&oid
, &merge_remote_util(commit
)->obj
->oid
))
1256 is_throwaway_tag
= 0;
1258 is_throwaway_tag
= 1;
1260 return is_throwaway_tag
;
1263 int cmd_merge(int argc
, const char **argv
, const char *prefix
)
1265 struct object_id result_tree
, stash
, head_oid
;
1266 struct commit
*head_commit
;
1267 struct strbuf buf
= STRBUF_INIT
;
1268 int i
, ret
= 0, head_subsumed
;
1269 int best_cnt
= -1, merge_was_ok
= 0, automerge_was_ok
= 0;
1270 struct commit_list
*common
= NULL
;
1271 const char *best_strategy
= NULL
, *wt_strategy
= NULL
;
1272 struct commit_list
*remoteheads
, *p
;
1273 void *branch_to_free
;
1274 int orig_argc
= argc
;
1276 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1277 usage_with_options(builtin_merge_usage
, builtin_merge_options
);
1279 prepare_repo_settings(the_repository
);
1280 the_repository
->settings
.command_requires_full_index
= 0;
1283 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1286 branch
= branch_to_free
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1288 skip_prefix(branch
, "refs/heads/", &branch
);
1290 if (!pull_twohead
) {
1291 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1292 if (default_strategy
&& !strcmp(default_strategy
, "ort"))
1293 pull_twohead
= "ort";
1296 init_diff_ui_defaults();
1297 git_config(git_merge_config
, NULL
);
1299 if (!branch
|| is_null_oid(&head_oid
))
1302 head_commit
= lookup_commit_or_die(&head_oid
, "HEAD");
1304 if (branch_mergeoptions
)
1305 parse_branch_merge_options(branch_mergeoptions
);
1306 argc
= parse_options(argc
, argv
, prefix
, builtin_merge_options
,
1307 builtin_merge_usage
, 0);
1308 if (shortlog_len
< 0)
1309 shortlog_len
= (merge_log_config
> 0) ? merge_log_config
: 0;
1311 if (verbosity
< 0 && show_progress
== -1)
1314 if (abort_current_merge
) {
1316 const char *nargv
[] = {"reset", "--merge", NULL
};
1317 struct strbuf stash_oid
= STRBUF_INIT
;
1320 usage_msg_opt(_("--abort expects no arguments"),
1321 builtin_merge_usage
, builtin_merge_options
);
1323 if (!file_exists(git_path_merge_head(the_repository
)))
1324 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1326 if (read_oneliner(&stash_oid
, git_path_merge_autostash(the_repository
),
1327 READ_ONELINER_SKIP_IF_EMPTY
))
1328 unlink(git_path_merge_autostash(the_repository
));
1330 /* Invoke 'git reset --merge' */
1331 ret
= cmd_reset(nargc
, nargv
, prefix
);
1334 apply_autostash_oid(stash_oid
.buf
);
1336 strbuf_release(&stash_oid
);
1340 if (quit_current_merge
) {
1342 usage_msg_opt(_("--quit expects no arguments"),
1343 builtin_merge_usage
,
1344 builtin_merge_options
);
1346 remove_merge_branch_state(the_repository
);
1350 if (continue_current_merge
) {
1352 const char *nargv
[] = {"commit", NULL
};
1355 usage_msg_opt(_("--continue expects no arguments"),
1356 builtin_merge_usage
, builtin_merge_options
);
1358 if (!file_exists(git_path_merge_head(the_repository
)))
1359 die(_("There is no merge in progress (MERGE_HEAD missing)."));
1361 /* Invoke 'git commit' */
1362 ret
= cmd_commit(nargc
, nargv
, prefix
);
1366 if (read_cache_unmerged())
1367 die_resolve_conflict("merge");
1369 if (file_exists(git_path_merge_head(the_repository
))) {
1371 * There is no unmerged entry, don't advise 'git
1372 * add/rm <file>', just 'git commit'.
1374 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1375 die(_("You have not concluded your merge (MERGE_HEAD exists).\n"
1376 "Please, commit your changes before you merge."));
1378 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1380 if (ref_exists("CHERRY_PICK_HEAD")) {
1381 if (advice_enabled(ADVICE_RESOLVE_CONFLICT
))
1382 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1383 "Please, commit your changes before you merge."));
1385 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists)."));
1387 resolve_undo_clear();
1389 if (option_edit
< 0)
1390 option_edit
= default_edit_option();
1392 cleanup_mode
= get_cleanup_mode(cleanup_arg
, 0 < option_edit
);
1398 if (fast_forward
== FF_NO
)
1399 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--no-ff.");
1400 if (option_commit
> 0)
1401 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--commit.");
1403 * squash can now silently disable option_commit - this is not
1404 * a problem as it is only overriding the default, not a user
1410 if (option_commit
< 0)
1414 if (default_to_upstream
)
1415 argc
= setup_with_upstream(&argv
);
1417 die(_("No commit specified and merge.defaultToUpstream not set."));
1418 } else if (argc
== 1 && !strcmp(argv
[0], "-")) {
1423 usage_with_options(builtin_merge_usage
,
1424 builtin_merge_options
);
1428 * If the merged head is a valid one there is no reason
1429 * to forbid "git merge" into a branch yet to be born.
1430 * We do the same for "git pull".
1432 struct object_id
*remote_head_oid
;
1434 die(_("Squash commit into empty head not supported yet"));
1435 if (fast_forward
== FF_NO
)
1436 die(_("Non-fast-forward commit does not make sense into "
1438 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1441 die(_("%s - not something we can merge"), argv
[0]);
1442 if (remoteheads
->next
)
1443 die(_("Can merge only exactly one commit into empty head"));
1445 if (verify_signatures
)
1446 verify_merge_signature(remoteheads
->item
, verbosity
,
1449 remote_head_oid
= &remoteheads
->item
->object
.oid
;
1450 read_empty(remote_head_oid
, 0);
1451 update_ref("initial pull", "HEAD", remote_head_oid
, NULL
, 0,
1452 UPDATE_REFS_DIE_ON_ERR
);
1457 * All the rest are the commits being merged; prepare
1458 * the standard merge summary message to be appended
1459 * to the given message.
1461 remoteheads
= collect_parents(head_commit
, &head_subsumed
,
1462 argc
, argv
, &merge_msg
);
1464 if (!head_commit
|| !argc
)
1465 usage_with_options(builtin_merge_usage
,
1466 builtin_merge_options
);
1468 if (verify_signatures
) {
1469 for (p
= remoteheads
; p
; p
= p
->next
) {
1470 verify_merge_signature(p
->item
, verbosity
,
1475 strbuf_addstr(&buf
, "merge");
1476 for (p
= remoteheads
; p
; p
= p
->next
)
1477 strbuf_addf(&buf
, " %s", merge_remote_util(p
->item
)->name
);
1478 setenv("GIT_REFLOG_ACTION", buf
.buf
, 0);
1481 for (p
= remoteheads
; p
; p
= p
->next
) {
1482 struct commit
*commit
= p
->item
;
1483 strbuf_addf(&buf
, "GITHEAD_%s",
1484 oid_to_hex(&commit
->object
.oid
));
1485 setenv(buf
.buf
, merge_remote_util(commit
)->name
, 1);
1487 if (fast_forward
!= FF_ONLY
&& merging_a_throwaway_tag(commit
))
1488 fast_forward
= FF_NO
;
1491 if (!use_strategies
&& !pull_twohead
&&
1492 remoteheads
&& !remoteheads
->next
) {
1493 char *default_strategy
= getenv("GIT_TEST_MERGE_ALGORITHM");
1494 if (default_strategy
)
1495 append_strategy(get_strategy(default_strategy
));
1497 if (!use_strategies
) {
1499 ; /* already up-to-date */
1500 else if (!remoteheads
->next
)
1501 add_strategies(pull_twohead
, DEFAULT_TWOHEAD
);
1503 add_strategies(pull_octopus
, DEFAULT_OCTOPUS
);
1506 for (i
= 0; i
< use_strategies_nr
; i
++) {
1507 if (use_strategies
[i
]->attr
& NO_FAST_FORWARD
)
1508 fast_forward
= FF_NO
;
1509 if (use_strategies
[i
]->attr
& NO_TRIVIAL
)
1514 ; /* already up-to-date */
1515 else if (!remoteheads
->next
)
1516 common
= get_merge_bases(head_commit
, remoteheads
->item
);
1518 struct commit_list
*list
= remoteheads
;
1519 commit_list_insert(head_commit
, &list
);
1520 common
= get_octopus_merge_bases(list
);
1524 update_ref("updating ORIG_HEAD", "ORIG_HEAD",
1525 &head_commit
->object
.oid
, NULL
, 0, UPDATE_REFS_DIE_ON_ERR
);
1527 if (remoteheads
&& !common
) {
1528 /* No common ancestors found. */
1529 if (!allow_unrelated_histories
)
1530 die(_("refusing to merge unrelated histories"));
1531 /* otherwise, we need a real merge. */
1532 } else if (!remoteheads
||
1533 (!remoteheads
->next
&& !common
->next
&&
1534 common
->item
== remoteheads
->item
)) {
1536 * If head can reach all the merge then we are up to date.
1537 * but first the most common case of merging one remote.
1539 finish_up_to_date();
1541 } else if (fast_forward
!= FF_NO
&& !remoteheads
->next
&&
1543 oideq(&common
->item
->object
.oid
, &head_commit
->object
.oid
)) {
1544 /* Again the most common case of merging one remote. */
1545 struct strbuf msg
= STRBUF_INIT
;
1546 struct commit
*commit
;
1548 if (verbosity
>= 0) {
1549 printf(_("Updating %s..%s\n"),
1550 find_unique_abbrev(&head_commit
->object
.oid
,
1552 find_unique_abbrev(&remoteheads
->item
->object
.oid
,
1555 strbuf_addstr(&msg
, "Fast-forward");
1558 " (no commit created; -m option ignored)");
1559 commit
= remoteheads
->item
;
1566 create_autostash(the_repository
,
1567 git_path_merge_autostash(the_repository
),
1569 if (checkout_fast_forward(the_repository
,
1570 &head_commit
->object
.oid
,
1571 &commit
->object
.oid
,
1572 overwrite_ignore
)) {
1573 apply_autostash(git_path_merge_autostash(the_repository
));
1578 finish(head_commit
, remoteheads
, &commit
->object
.oid
, msg
.buf
);
1579 remove_merge_branch_state(the_repository
);
1580 strbuf_release(&msg
);
1582 } else if (!remoteheads
->next
&& common
->next
)
1585 * We are not doing octopus and not fast-forward. Need
1588 else if (!remoteheads
->next
&& !common
->next
&& option_commit
) {
1590 * We are not doing octopus, not fast-forward, and have
1593 refresh_cache(REFRESH_QUIET
);
1594 if (allow_trivial
&& fast_forward
!= FF_ONLY
) {
1595 /* See if it is really trivial. */
1596 git_committer_info(IDENT_STRICT
);
1597 printf(_("Trying really trivial in-index merge...\n"));
1598 if (!read_tree_trivial(&common
->item
->object
.oid
,
1599 &head_commit
->object
.oid
,
1600 &remoteheads
->item
->object
.oid
)) {
1601 ret
= merge_trivial(head_commit
, remoteheads
);
1604 printf(_("Nope.\n"));
1608 * An octopus. If we can reach all the remote we are up
1612 struct commit_list
*j
;
1614 for (j
= remoteheads
; j
; j
= j
->next
) {
1615 struct commit_list
*common_one
;
1618 * Here we *have* to calculate the individual
1619 * merge_bases again, otherwise "git merge HEAD^
1620 * HEAD^^" would be missed.
1622 common_one
= get_merge_bases(head_commit
, j
->item
);
1623 if (!oideq(&common_one
->item
->object
.oid
, &j
->item
->object
.oid
)) {
1629 finish_up_to_date();
1634 if (fast_forward
== FF_ONLY
)
1635 die_ff_impossible();
1638 create_autostash(the_repository
,
1639 git_path_merge_autostash(the_repository
),
1642 /* We are going to make a new commit. */
1643 git_committer_info(IDENT_STRICT
);
1646 * At this point, we need a real merge. No matter what strategy
1647 * we use, it would operate on the index, possibly affecting the
1648 * working tree, and when resolved cleanly, have the desired
1649 * tree in the index -- this means that the index must be in
1650 * sync with the head commit. The strategies are responsible
1653 if (use_strategies_nr
== 1 ||
1655 * Stash away the local changes so that we can try more than one.
1660 for (i
= 0; !merge_was_ok
&& i
< use_strategies_nr
; i
++) {
1663 printf(_("Rewinding the tree to pristine...\n"));
1664 restore_state(&head_commit
->object
.oid
, &stash
);
1666 if (use_strategies_nr
!= 1)
1667 printf(_("Trying merge strategy %s...\n"),
1668 use_strategies
[i
]->name
);
1670 * Remember which strategy left the state in the working
1673 wt_strategy
= use_strategies
[i
]->name
;
1675 ret
= try_merge_strategy(use_strategies
[i
]->name
,
1676 common
, remoteheads
,
1679 * The backend exits with 1 when conflicts are
1680 * left to be resolved, with 2 when it does not
1681 * handle the given merge at all.
1685 if (option_commit
) {
1686 /* Automerge succeeded. */
1687 automerge_was_ok
= 1;
1692 cnt
= (use_strategies_nr
> 1) ? evaluate_result() : 0;
1693 if (best_cnt
<= 0 || cnt
<= best_cnt
) {
1694 best_strategy
= use_strategies
[i
]->name
;
1701 * If we have a resulting tree, that means the strategy module
1702 * auto resolved the merge cleanly.
1704 if (automerge_was_ok
) {
1705 ret
= finish_automerge(head_commit
, head_subsumed
,
1706 common
, remoteheads
,
1707 &result_tree
, wt_strategy
);
1712 * Pick the result from the best strategy and have the user fix
1715 if (!best_strategy
) {
1716 restore_state(&head_commit
->object
.oid
, &stash
);
1717 if (use_strategies_nr
> 1)
1719 _("No merge strategy handled the merge.\n"));
1721 fprintf(stderr
, _("Merge with strategy %s failed.\n"),
1722 use_strategies
[0]->name
);
1723 apply_autostash(git_path_merge_autostash(the_repository
));
1726 } else if (best_strategy
== wt_strategy
)
1727 ; /* We already have its result in the working tree. */
1729 printf(_("Rewinding the tree to pristine...\n"));
1730 restore_state(&head_commit
->object
.oid
, &stash
);
1731 printf(_("Using the %s strategy to prepare resolving by hand.\n"),
1733 try_merge_strategy(best_strategy
, common
, remoteheads
,
1738 finish(head_commit
, remoteheads
, NULL
, NULL
);
1740 git_test_write_commit_graph_or_die();
1742 write_merge_state(remoteheads
);
1745 fprintf(stderr
, _("Automatic merge went well; "
1746 "stopped before committing as requested\n"));
1748 ret
= suggest_conflicts();
1751 strbuf_release(&buf
);
1752 free(branch_to_free
);