rebase -i: rewrite init_basic_state() in C
[gitweb.git] / builtin / rebase--helper.c
index 0716bbfd786cbfbfbec37565e499a5837c5ed0f9..f8128037d3fb3eb7eabfab3747150d0fd82332ac 100644 (file)
@@ -5,8 +5,13 @@
 #include "sequencer.h"
 #include "rebase-interactive.h"
 #include "argv-array.h"
+#include "refs.h"
+#include "rerere.h"
+#include "alias.h"
 
+static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
+static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
 
 static int get_revision_ranges(const char *upstream, const char *onto,
                               const char **head_hash,
@@ -42,6 +47,24 @@ static int get_revision_ranges(const char *upstream, const char *onto,
        return 0;
 }
 
+static int init_basic_state(struct replay_opts *opts, const char *head_name,
+                           const char *onto, const char *orig_head)
+{
+       FILE *interactive;
+
+       if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
+               return error_errno(_("could not create temporary %s"), path_state_dir());
+
+       delete_reflog("REBASE_HEAD");
+
+       interactive = fopen(path_interactive(), "w");
+       if (!interactive)
+               return error_errno(_("could not mark as interactive"));
+       fclose(interactive);
+
+       return write_basic_state(opts, head_name, onto, orig_head);
+}
+
 static const char * const builtin_rebase_helper_usage[] = {
        N_("git rebase--helper [<options>]"),
        NULL
@@ -53,11 +76,12 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
        unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
        int abbreviate_commands = 0, rebase_cousins = -1, ret;
        const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
-               *squash_onto = NULL, *upstream = NULL;
+               *squash_onto = NULL, *upstream = NULL, *head_name = NULL;
+       char *raw_strategies = NULL;
        enum {
                CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
                CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
-               COMPLETE_ACTION
+               COMPLETE_ACTION, INIT_BASIC_STATE
        } command = 0;
        struct option options[] = {
                OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -69,6 +93,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                         N_("keep original branch points of cousins")),
                OPT_BOOL(0, "autosquash", &autosquash,
                         N_("move commits that begin with squash!/fixup!")),
+               OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
                OPT__VERBOSE(&opts.verbose, N_("be verbose")),
                OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
                                CONTINUE),
@@ -93,6 +118,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                            N_("prepare the branch to be rebased"), PREPARE_BRANCH),
                OPT_CMDMODE(0, "complete-action", &command,
                            N_("complete the action"), COMPLETE_ACTION),
+               OPT_CMDMODE(0, "init-basic-state", &command,
+                           N_("initialise the rebase state"), INIT_BASIC_STATE),
                OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
                OPT_STRING(0, "restrict-revision", &restrict_revision,
                           N_("restrict-revision"), N_("restrict revision")),
@@ -100,6 +127,14 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                           N_("squash onto")),
                OPT_STRING(0, "upstream", &upstream, N_("upstream"),
                           N_("the upstream commit")),
+               OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
+               OPT_STRING('S', "gpg-sign", &opts.gpg_sign, N_("gpg-sign"),
+                          N_("GPG-sign commits")),
+               OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
+                          N_("rebase strategy")),
+               OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
+                          N_("strategy options")),
+               OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
                OPT_END()
        };
 
@@ -176,6 +211,16 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                free(shortrevisions);
                return !!ret;
        }
+       if (command == INIT_BASIC_STATE) {
+               if (raw_strategies)
+                       parse_strategy_opts(&opts, raw_strategies);
+
+               ret = get_revision_ranges(upstream, onto, &head_hash, NULL, NULL);
+               if (ret)
+                       return ret;
+
+               return !!init_basic_state(&opts, head_name, onto, head_hash);
+       }
 
        usage_with_options(builtin_rebase_helper_usage, options);
 }