rebase -i: implement the logic to initialize $revisions in C

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.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alban Gruin committed Aug 28, 2018 at 14:10 UTC 6ab54d17be3f51153444d8efebb0ae363eb9b7c9
2 files changed +56 -4
builtin/rebase--helper.c
+53 -3
@@ -4,6 +4,25 @@
4 #include "parse-options.h"
5 #include "sequencer.h"
6 #include "rebase-interactive.h"
7 +#include "argv-array.h"
8 +
9 +static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
10 +
11 +static int get_revision_ranges(const char *upstream, const char *onto,
12 + const char **head_hash,
13 + char **revisions)
14 +{
15 + const char *base_rev = upstream ? upstream : onto;
16 + struct object_id orig_head;
17 +
18 + if (get_oid("HEAD", &orig_head))
19 + return error(_("no HEAD?"));
20 +
21 + *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
22 + *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
23 +
24 + return 0;
25 +}
26
27 static const char * const builtin_rebase_helper_usage[] = {
28 N_("git rebase--helper [<options>]"),
@@ -14,7 +33,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
33 {
34 struct replay_opts opts = REPLAY_OPTS_INIT;
35 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
17 - int abbreviate_commands = 0, rebase_cousins = -1;
36 + int abbreviate_commands = 0, rebase_cousins = -1, ret;
37 + const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
38 + *squash_onto = NULL, *upstream = NULL;
39 enum {
40 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
41 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)
75 N_("prepare the branch to be rebased"), PREPARE_BRANCH),
76 OPT_CMDMODE(0, "complete-action", &command,
77 N_("complete the action"), COMPLETE_ACTION),
78 + OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
79 + OPT_STRING(0, "restrict-revision", &restrict_revision,
80 + N_("restrict-revision"), N_("restrict revision")),
81 + OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
82 + N_("squash onto")),
83 + OPT_STRING(0, "upstream", &upstream, N_("upstream"),
84 + N_("the upstream commit")),
85 OPT_END()
86 };
87
@@ -81,8 +109,30 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
109 return !!sequencer_continue(&opts);
110 if (command == ABORT && argc == 1)
111 return !!sequencer_remove_state(&opts);
84 - if (command == MAKE_SCRIPT && argc > 1)
85 - return !!sequencer_make_script(stdout, argc, argv, flags);
112 + if (command == MAKE_SCRIPT && argc == 1) {
113 + char *revisions = NULL;
114 + struct argv_array make_script_args = ARGV_ARRAY_INIT;
115 +
116 + if (!upstream && squash_onto)
117 + write_file(path_squash_onto(), "%s\n", squash_onto);
118 +
119 + ret = get_revision_ranges(upstream, onto, &head_hash, &revisions);
120 + if (ret)
121 + return ret;
122 +
123 + argv_array_pushl(&make_script_args, "", revisions, NULL);
124 + if (restrict_revision)
125 + argv_array_push(&make_script_args, restrict_revision);
126 +
127 + ret = sequencer_make_script(stdout,
128 + make_script_args.argc, make_script_args.argv,
129 + flags);
130 +
131 + free(revisions);
132 + argv_array_clear(&make_script_args);
133 +
134 + return !!ret;
135 + }
136 if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
137 return !!transform_todos(flags);
138 if (command == CHECK_TODO_LIST && argc == 1)
git-rebase--interactive.sh
+3 -1
@@ -92,7 +92,9 @@ git_rebase__interactive () {
92 git rebase--helper --make-script ${keep_empty:+--keep-empty} \
93 ${rebase_merges:+--rebase-merges} \
94 ${rebase_cousins:+--rebase-cousins} \
95 - $revisions ${restrict_revision+^$restrict_revision} >"$todo" ||
95 + ${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"} \
96 + ${squash_onto:+--squash-onto "$squash_onto"} \
97 + ${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" ||
98 die "$(gettext "Could not generate todo list")"
99
100 exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \