rebase -i: rewrite init_basic_state() in C
authorAlban Gruin <alban.gruin@gmail.com>
Tue, 28 Aug 2018 12:10:41 +0000 (14:10 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 29 Aug 2018 20:38:18 +0000 (13:38 -0700)
This rewrites init_basic_state() from shell to C. The call to
write_basic_state() in cmd_rebase__helper() is replaced by a call to the
new function.

The shell version is then stripped from git-rebase--interactive.sh.

Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase--helper.c
git-rebase--interactive.sh
index 63c5086e423199214dbdd38049731a2a23afa54b..f8128037d3fb3eb7eabfab3747150d0fd82332ac 100644 (file)
@@ -5,10 +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,
@@ -44,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
@@ -198,7 +219,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
                if (ret)
                        return ret;
 
-               return !!write_basic_state(&opts, head_name, onto, head_hash);
+               return !!init_basic_state(&opts, head_name, onto, head_hash);
        }
 
        usage_with_options(builtin_rebase_helper_usage, options);
index 6367da66e2dd05a2b0ab0091bd20ad742eceedec..761be95ed1a9e622ce7815d2cdb9d12333f5ce46 100644 (file)
@@ -51,14 +51,6 @@ initiate_action () {
        esac
 }
 
-init_basic_state () {
-       orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
-       mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
-       rm -f "$(git rev-parse --git-path REBASE_HEAD)"
-
-       : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
-}
-
 git_rebase__interactive () {
        initiate_action "$action"
        ret=$?
@@ -67,7 +59,6 @@ git_rebase__interactive () {
        fi
 
        git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
-       init_basic_state
 
        git rebase--helper --init-basic-state ${upstream:+--upstream "$upstream"} \
                ${onto:+--onto "$onto"} ${head_name:+--head-name "$head_name"} \