This rewrites the part of init_revisions_and_shortrevisions() needed by
`--make-script` from shell to C.  The new version is called
get_revision_ranges(), and is a static function inside of
rebase--helper.c.  As this does not initialize $shortrevision, the
original shell version is not yet stripped.

Unlike init_revisions_and_shortrevisions(), get_revision_ranges()
doesn’t write $squash_onto to the state directory, it’s done by the
handler of `--make-script` instead.

Finally, this drops the $revision argument passed to `--make-script` in
git-rebase--interactive.sh, and rebase--helper is changed accordingly.

Signed-off-by: Alban Gruin <alban.gr...@gmail.com>
---
No changes since v7.

 builtin/rebase--helper.c   | 56 ++++++++++++++++++++++++++++++++++++--
 git-rebase--interactive.sh |  4 ++-
 2 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c
index e1460136f5..acc71a6f99 100644
--- a/builtin/rebase--helper.c
+++ b/builtin/rebase--helper.c
@@ -4,6 +4,25 @@
 #include "parse-options.h"
 #include "sequencer.h"
 #include "rebase-interactive.h"
+#include "argv-array.h"
+
+static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
+
+static int get_revision_ranges(const char *upstream, const char *onto,
+                              const char **head_hash,
+                              char **revisions)
+{
+       const char *base_rev = upstream ? upstream : onto;
+       struct object_id orig_head;
+
+       if (get_oid("HEAD", &orig_head))
+               return error(_("no HEAD?"));
+
+       *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
+       *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
+
+       return 0;
+}
 
 static const char * const builtin_rebase_helper_usage[] = {
        N_("git rebase--helper [<options>]"),
@@ -14,7 +33,9 @@ int cmd_rebase__helper(int argc, const char **argv, const 
char *prefix)
 {
        struct replay_opts opts = REPLAY_OPTS_INIT;
        unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
-       int abbreviate_commands = 0, rebase_cousins = -1;
+       int abbreviate_commands = 0, rebase_cousins = -1, ret;
+       const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
+               *squash_onto = NULL, *upstream = NULL;
        enum {
                CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
                CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, 
PREPARE_BRANCH,
@@ -54,6 +75,13 @@ 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_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
+               OPT_STRING(0, "restrict-revision", &restrict_revision,
+                          N_("restrict-revision"), N_("restrict revision")),
+               OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
+                          N_("squash onto")),
+               OPT_STRING(0, "upstream", &upstream, N_("upstream"),
+                          N_("the upstream commit")),
                OPT_END()
        };
 
@@ -81,8 +109,30 @@ int cmd_rebase__helper(int argc, const char **argv, const 
char *prefix)
                return !!sequencer_continue(&opts);
        if (command == ABORT && argc == 1)
                return !!sequencer_remove_state(&opts);
-       if (command == MAKE_SCRIPT && argc > 1)
-               return !!sequencer_make_script(stdout, argc, argv, flags);
+       if (command == MAKE_SCRIPT && argc == 1) {
+               char *revisions = NULL;
+               struct argv_array make_script_args = ARGV_ARRAY_INIT;
+
+               if (!upstream && squash_onto)
+                       write_file(path_squash_onto(), "%s\n", squash_onto);
+
+               ret = get_revision_ranges(upstream, onto, &head_hash, 
&revisions);
+               if (ret)
+                       return ret;
+
+               argv_array_pushl(&make_script_args, "", revisions, NULL);
+               if (restrict_revision)
+                       argv_array_push(&make_script_args, restrict_revision);
+
+               ret = sequencer_make_script(stdout,
+                                           make_script_args.argc, 
make_script_args.argv,
+                                           flags);
+
+               free(revisions);
+               argv_array_clear(&make_script_args);
+
+               return !!ret;
+       }
        if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
                return !!transform_todos(flags);
        if (command == CHECK_TODO_LIST && argc == 1)
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 0d66c0f8b8..4ca47aed1e 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -92,7 +92,9 @@ git_rebase__interactive () {
        git rebase--helper --make-script ${keep_empty:+--keep-empty} \
                ${rebase_merges:+--rebase-merges} \
                ${rebase_cousins:+--rebase-cousins} \
-               $revisions ${restrict_revision+^$restrict_revision} >"$todo" ||
+               ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} \
+               ${squash_onto:+--squash-onto "$squash_onto"} \
+               ${restrict_revision:+--restrict-revision ^"$restrict_revision"} 
>"$todo" ||
        die "$(gettext "Could not generate todo list")"
 
        exec git rebase--helper --complete-action "$shortrevisions" 
"$onto_name" \
-- 
2.19.0

Reply via email to