Skip to content

Commit e372252

Browse files
agrngitster
authored andcommitted
rebase -i: rewrite the rest of init_revisions_and_shortrevisions() in C
This rewrites the part of init_revisions_and_shortrevisions() needed by `--complete-action` (which initialize $shortrevisions) from shell to C. When `upstream` is empty, it means that the user launched a `rebase --root`, and `onto` contains the ID of an empty commit. As a range between an empty commit and `head` is not really meaningful, `onto` is not used to initialize `shortrevisions` in this case. The corresponding arguments passed to `--complete-action` are then dropped, and init_revisions_and_shortrevisions() is stripped from git-rebase--interactive.sh Signed-off-by: Alban Gruin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73cbb80 commit e372252

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

builtin/rebase--helper.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
1010

1111
static int get_revision_ranges(const char *upstream, const char *onto,
1212
const char **head_hash,
13-
char **revisions)
13+
char **revisions, char **shortrevisions)
1414
{
1515
const char *base_rev = upstream ? upstream : onto;
1616
struct object_id orig_head;
@@ -19,7 +19,25 @@ static int get_revision_ranges(const char *upstream, const char *onto,
1919
return error(_("no HEAD?"));
2020

2121
*head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
22-
*revisions = xstrfmt("%s...%s", base_rev, *head_hash);
22+
23+
if (revisions)
24+
*revisions = xstrfmt("%s...%s", base_rev, *head_hash);
25+
if (shortrevisions) {
26+
const char *shorthead;
27+
28+
shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
29+
30+
if (upstream) {
31+
const char *shortrev;
32+
struct object_id rev_oid;
33+
34+
get_oid(base_rev, &rev_oid);
35+
shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
36+
37+
*shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
38+
} else
39+
*shortrevisions = xstrdup(shorthead);
40+
}
2341

2442
return 0;
2543
}
@@ -116,7 +134,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
116134
if (!upstream && squash_onto)
117135
write_file(path_squash_onto(), "%s\n", squash_onto);
118136

119-
ret = get_revision_ranges(upstream, onto, &head_hash, &revisions);
137+
ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL);
120138
if (ret)
121139
return ret;
122140

@@ -145,9 +163,19 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
145163
return !!edit_todo_list(flags);
146164
if (command == PREPARE_BRANCH && argc == 2)
147165
return !!prepare_branch_to_be_rebased(&opts, argv[1]);
148-
if (command == COMPLETE_ACTION && argc == 6)
149-
return !!complete_action(&opts, flags, argv[1], argv[2], argv[3],
150-
argv[4], argv[5], autosquash);
166+
if (command == COMPLETE_ACTION && argc == 3) {
167+
char *shortrevisions = NULL;
168+
169+
ret = get_revision_ranges(upstream, onto, &head_hash, NULL, &shortrevisions);
170+
if (ret)
171+
return ret;
172+
173+
ret = complete_action(&opts, flags, shortrevisions, argv[1], onto,
174+
head_hash, argv[2], autosquash);
175+
176+
free(shortrevisions);
177+
return !!ret;
178+
}
151179

152180
usage_with_options(builtin_rebase_helper_usage, options);
153181
}

git-rebase--interactive.sh

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,6 @@ init_basic_state () {
6060
write_basic_state
6161
}
6262

63-
init_revisions_and_shortrevisions () {
64-
shorthead=$(git rev-parse --short $orig_head)
65-
shortonto=$(git rev-parse --short $onto)
66-
if test -z "$rebase_root"
67-
# this is now equivalent to ! -z "$upstream"
68-
then
69-
shortupstream=$(git rev-parse --short $upstream)
70-
revisions=$upstream...$orig_head
71-
shortrevisions=$shortupstream..$shorthead
72-
else
73-
revisions=$onto...$orig_head
74-
shortrevisions=$shorthead
75-
test -z "$squash_onto" ||
76-
echo "$squash_onto" >"$state_dir"/squash-onto
77-
fi
78-
}
79-
8063
git_rebase__interactive () {
8164
initiate_action "$action"
8265
ret=$?
@@ -87,8 +70,6 @@ git_rebase__interactive () {
8770
git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
8871
init_basic_state
8972

90-
init_revisions_and_shortrevisions
91-
9273
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
9374
${rebase_merges:+--rebase-merges} \
9475
${rebase_cousins:+--rebase-cousins} \
@@ -97,8 +78,8 @@ git_rebase__interactive () {
9778
${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" ||
9879
die "$(gettext "Could not generate todo list")"
9980

100-
exec git rebase--helper --complete-action "$shortrevisions" "$onto_name" \
101-
"$shortonto" "$orig_head" "$cmd" $allow_empty_message \
102-
${autosquash:+--autosquash} ${keep_empty:+--keep-empty} \
103-
${verbose:+--verbose} ${force_rebase:+--no-ff}
81+
exec git rebase--helper --complete-action "$onto_name" "$cmd" \
82+
$allow_empty_message ${autosquash:+--autosquash} ${verbose:+--verbose} \
83+
${keep_empty:+--keep-empty} ${force_rebase:+--no-ff} \
84+
${upstream:+--upstream "$upstream"} ${onto:+--onto "$onto"}
10485
}

0 commit comments

Comments
 (0)