Skip to content

Commit 73cbb80

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9aadc56 commit 73cbb80

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

builtin/rebase--helper.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
#include "parse-options.h"
55
#include "sequencer.h"
66
#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+
}
726

827
static const char * const builtin_rebase_helper_usage[] = {
928
N_("git rebase--helper [<options>]"),
@@ -14,7 +33,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1433
{
1534
struct replay_opts opts = REPLAY_OPTS_INIT;
1635
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;
1839
enum {
1940
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
2041
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)
5475
N_("prepare the branch to be rebased"), PREPARE_BRANCH),
5576
OPT_CMDMODE(0, "complete-action", &command,
5677
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")),
5785
OPT_END()
5886
};
5987

@@ -81,8 +109,30 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
81109
return !!sequencer_continue(&opts);
82110
if (command == ABORT && argc == 1)
83111
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+
}
86136
if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
87137
return !!transform_todos(flags);
88138
if (command == CHECK_TODO_LIST && argc == 1)

git-rebase--interactive.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ git_rebase__interactive () {
9292
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
9393
${rebase_merges:+--rebase-merges} \
9494
${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" ||
9698
die "$(gettext "Could not generate todo list")"
9799

98100
exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \

0 commit comments

Comments
 (0)