Merge branch 'nd/switch-and-restore'
[gitweb.git] / builtin / merge.c
index e9663f027ada2807e62a1ce625d2bffca0332dac..e2ccbc44e204173b09f5ad4b704a31a9e8643bb6 100644 (file)
@@ -39,6 +39,7 @@
 #include "alias.h"
 #include "branch.h"
 #include "commit-reach.h"
+#include "wt-status.h"
 
 #define DEFAULT_TWOHEAD (1<<0)
 #define DEFAULT_OCTOPUS (1<<1)
@@ -58,7 +59,7 @@ static const char * const builtin_merge_usage[] = {
 };
 
 static int show_diffstat = 1, shortlog_len = -1, squash;
-static int option_commit = 1;
+static int option_commit = -1;
 static int option_edit = -1;
 static int allow_trivial = 1, have_message, verify_signatures;
 static int overwrite_ignore = 1;
@@ -73,6 +74,7 @@ static int option_renormalize;
 static int verbosity;
 static int allow_rerere_auto;
 static int abort_current_merge;
+static int quit_current_merge;
 static int continue_current_merge;
 static int allow_unrelated_histories;
 static int show_progress = -1;
@@ -99,6 +101,9 @@ enum ff_type {
 
 static enum ff_type fast_forward = FF_ALLOW;
 
+static const char *cleanup_arg;
+static enum commit_msg_cleanup_mode cleanup_mode;
+
 static int option_parse_message(const struct option *opt,
                                const char *arg, int unset)
 {
@@ -114,12 +119,15 @@ static int option_parse_message(const struct option *opt,
        return 0;
 }
 
-static int option_read_message(struct parse_opt_ctx_t *ctx,
-                              const struct option *opt, int unset)
+static enum parse_opt_result option_read_message(struct parse_opt_ctx_t *ctx,
+                                                const struct option *opt,
+                                                const char *arg_not_used,
+                                                int unset)
 {
        struct strbuf *buf = opt->value;
        const char *arg;
 
+       BUG_ON_OPT_ARG(arg_not_used);
        if (unset)
                BUG("-F cannot be negated");
 
@@ -247,6 +255,7 @@ static struct option builtin_merge_options[] = {
                N_("perform a commit if the merge succeeds (default)")),
        OPT_BOOL('e', "edit", &option_edit,
                N_("edit message before committing")),
+       OPT_CLEANUP(&cleanup_arg),
        OPT_SET_INT(0, "ff", &fast_forward, N_("allow fast-forward (default)"), FF_ALLOW),
        OPT_SET_INT_F(0, "ff-only", &fast_forward,
                      N_("abort if fast-forward is not possible"),
@@ -263,10 +272,12 @@ static struct option builtin_merge_options[] = {
                option_parse_message),
        { OPTION_LOWLEVEL_CALLBACK, 'F', "file", &merge_msg, N_("path"),
                N_("read message from file"), PARSE_OPT_NONEG,
-               (parse_opt_cb *) option_read_message },
+               NULL, 0, option_read_message },
        OPT__VERBOSITY(&verbosity),
        OPT_BOOL(0, "abort", &abort_current_merge,
                N_("abort the current in-progress merge")),
+       OPT_BOOL(0, "quit", &quit_current_merge,
+               N_("--abort but leave index and working tree alone")),
        OPT_BOOL(0, "continue", &continue_current_merge,
                N_("continue the current in-progress merge")),
        OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
@@ -442,7 +453,7 @@ static void finish(struct commit *head_commit,
                         * We ignore errors in 'gc --auto', since the
                         * user should see them.
                         */
-                       close_all_packs(the_repository->objects);
+                       close_object_store(the_repository->objects);
                        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
                }
        }
@@ -602,6 +613,8 @@ static int git_merge_config(const char *k, const char *v, void *cb)
                return git_config_string(&pull_twohead, k, v);
        else if (!strcmp(k, "pull.octopus"))
                return git_config_string(&pull_octopus, k, v);
+       else if (!strcmp(k, "commit.cleanup"))
+               return git_config_string(&cleanup_arg, k, v);
        else if (!strcmp(k, "merge.renormalize"))
                option_renormalize = git_config_bool(k, v);
        else if (!strcmp(k, "merge.ff")) {
@@ -790,8 +803,13 @@ static void abort_commit(struct commit_list *remoteheads, const char *err_msg)
 static const char merge_editor_comment[] =
 N_("Please enter a commit message to explain why this merge is necessary,\n"
    "especially if it merges an updated upstream into a topic branch.\n"
-   "\n"
-   "Lines starting with '%c' will be ignored, and an empty message aborts\n"
+   "\n");
+
+static const char scissors_editor_comment[] =
+N_("An empty message aborts the commit.\n");
+
+static const char no_scissors_editor_comment[] =
+N_("Lines starting with '%c' will be ignored, and an empty message aborts\n"
    "the commit.\n");
 
 static void write_merge_heads(struct commit_list *);
@@ -799,11 +817,19 @@ static void prepare_to_commit(struct commit_list *remoteheads)
 {
        struct strbuf msg = STRBUF_INIT;
        strbuf_addbuf(&msg, &merge_msg);
-       strbuf_addch(&msg, '\n');
        if (squash)
                BUG("the control must not reach here under --squash");
-       if (0 < option_edit)
-               strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
+       if (0 < option_edit) {
+               strbuf_addch(&msg, '\n');
+               if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
+                       wt_status_append_cut_line(&msg);
+                       strbuf_commented_addf(&msg, "\n");
+               }
+               strbuf_commented_addf(&msg, _(merge_editor_comment));
+               strbuf_commented_addf(&msg, _(cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS ?
+                       scissors_editor_comment :
+                       no_scissors_editor_comment), comment_line_char);
+       }
        if (signoff)
                append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
        write_merge_heads(remoteheads);
@@ -822,7 +848,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
                abort_commit(remoteheads, NULL);
 
        read_merge_msg(&msg);
-       strbuf_stripspace(&msg, 0 < option_edit);
+       cleanup_message(&msg, cleanup_mode, 0);
        if (!msg.len)
                abort_commit(remoteheads, _("Empty commit message."));
        strbuf_release(&merge_msg);
@@ -866,11 +892,11 @@ static int finish_automerge(struct commit *head,
        struct strbuf buf = STRBUF_INIT;
        struct object_id result_commit;
 
+       write_tree_trivial(result_tree);
        free_commit_list(common);
        parents = remoteheads;
        if (!head_subsumed || fast_forward == FF_NO)
                commit_list_insert(head, &parents);
-       strbuf_addch(&merge_msg, '\n');
        prepare_to_commit(remoteheads);
        if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
                        &result_commit, NULL, sign_commit))
@@ -891,7 +917,15 @@ static int suggest_conflicts(void)
        filename = git_path_merge_msg(the_repository);
        fp = xfopen(filename, "a");
 
-       append_conflicts_hint(&the_index, &msgbuf);
+       /*
+        * We can't use cleanup_mode because if we're not using the editor,
+        * get_cleanup_mode will return COMMIT_MSG_CLEANUP_SPACE instead, even
+        * though the message is meant to be processed later by git-commit.
+        * Thus, we will get the cleanup mode which is returned when we _are_
+        * using an editor.
+        */
+       append_conflicts_hint(&the_index, &msgbuf,
+                             get_cleanup_mode(cleanup_arg, 1));
        fputs(msgbuf.buf, fp);
        strbuf_release(&msgbuf);
        fclose(fp);
@@ -1252,6 +1286,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                goto done;
        }
 
+       if (quit_current_merge) {
+               if (orig_argc != 2)
+                       usage_msg_opt(_("--quit expects no arguments"),
+                                     builtin_merge_usage,
+                                     builtin_merge_options);
+
+               remove_merge_branch_state(the_repository);
+               goto done;
+       }
+
        if (continue_current_merge) {
                int nargc = 1;
                const char *nargv[] = {"commit", NULL};
@@ -1291,15 +1335,30 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
        }
        resolve_undo_clear();
 
+       if (option_edit < 0)
+               option_edit = default_edit_option();
+
+       cleanup_mode = get_cleanup_mode(cleanup_arg, 0 < option_edit);
+
        if (verbosity < 0)
                show_diffstat = 0;
 
        if (squash) {
                if (fast_forward == FF_NO)
                        die(_("You cannot combine --squash with --no-ff."));
+               if (option_commit > 0)
+                       die(_("You cannot combine --squash with --commit."));
+               /*
+                * squash can now silently disable option_commit - this is not
+                * a problem as it is only overriding the default, not a user
+                * supplied option.
+                */
                option_commit = 0;
        }
 
+       if (option_commit < 0)
+               option_commit = 1;
+
        if (!argc) {
                if (default_to_upstream)
                        argc = setup_with_upstream(&argv);
@@ -1376,9 +1435,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                        fast_forward = FF_NO;
        }
 
-       if (option_edit < 0)
-               option_edit = default_edit_option();
-
        if (!use_strategies) {
                if (!remoteheads)
                        ; /* already up-to-date */
@@ -1531,8 +1587,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
            save_state(&stash))
                oidclr(&stash);
 
-       for (i = 0; i < use_strategies_nr; i++) {
-               int ret;
+       for (i = 0; !merge_was_ok && i < use_strategies_nr; i++) {
+               int ret, cnt;
                if (i) {
                        printf(_("Rewinding the tree to pristine...\n"));
                        restore_state(&head_commit->object.oid, &stash);
@@ -1549,40 +1605,26 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
                ret = try_merge_strategy(use_strategies[i]->name,
                                         common, remoteheads,
                                         head_commit);
-               if (!option_commit && !ret) {
-                       merge_was_ok = 1;
-                       /*
-                        * This is necessary here just to avoid writing
-                        * the tree, but later we will *not* exit with
-                        * status code 1 because merge_was_ok is set.
-                        */
-                       ret = 1;
-               }
-
-               if (ret) {
-                       /*
-                        * The backend exits with 1 when conflicts are
-                        * left to be resolved, with 2 when it does not
-                        * handle the given merge at all.
-                        */
-                       if (ret == 1) {
-                               int cnt = evaluate_result();
-
-                               if (best_cnt <= 0 || cnt <= best_cnt) {
-                                       best_strategy = use_strategies[i]->name;
-                                       best_cnt = cnt;
+               /*
+                * The backend exits with 1 when conflicts are
+                * left to be resolved, with 2 when it does not
+                * handle the given merge at all.
+                */
+               if (ret < 2) {
+                       if (!ret) {
+                               if (option_commit) {
+                                       /* Automerge succeeded. */
+                                       automerge_was_ok = 1;
+                                       break;
                                }
+                               merge_was_ok = 1;
+                       }
+                       cnt = evaluate_result();
+                       if (best_cnt <= 0 || cnt <= best_cnt) {
+                               best_strategy = use_strategies[i]->name;
+                               best_cnt = cnt;
                        }
-                       if (merge_was_ok)
-                               break;
-                       else
-                               continue;
                }
-
-               /* Automerge succeeded. */
-               write_tree_trivial(&result_tree);
-               automerge_was_ok = 1;
-               break;
        }
 
        /*