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

Alban Gruin committed Aug 28, 2018 at 14:10 UTC f22e4e1a3c2faaac59099aaca15641c18f478f77
2 files changed +38 -29
builtin/rebase--helper.c
+34 -6
@@ -10,7 +10,7 @@ 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)
13 + char **revisions, char **shortrevisions)
14 {
15 const char *base_rev = upstream ? upstream : onto;
16 struct object_id orig_head;
@@ -19,7 +19,25 @@ static int get_revision_ranges(const char *upstream, const char *onto,
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);
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 + }
41
42 return 0;
43 }
@@ -116,7 +134,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
134 if (!upstream && squash_onto)
135 write_file(path_squash_onto(), "%s\n", squash_onto);
136
119 - ret = get_revision_ranges(upstream, onto, &head_hash, &revisions);
137 + ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL);
138 if (ret)
139 return ret;
140
@@ -145,9 +163,19 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
163 return !!edit_todo_list(flags);
164 if (command == PREPARE_BRANCH && argc == 2)
165 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 + }
179
180 usage_with_options(builtin_rebase_helper_usage, options);
181 }
git-rebase--interactive.sh
+4 -23
@@ -60,23 +60,6 @@ init_basic_state () {
60 write_basic_state
61 }
62
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 -
63 git_rebase__interactive () {
64 initiate_action "$action"
65 ret=$?
@@ -87,8 +70,6 @@ git_rebase__interactive () {
70 git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
71 init_basic_state
72
90 - init_revisions_and_shortrevisions
91 -
73 git rebase--helper --make-script ${keep_empty:+--keep-empty} \
74 ${rebase_merges:+--rebase-merges} \
75 ${rebase_cousins:+--rebase-cousins} \
@@ -97,8 +78,8 @@ git_rebase__interactive () {
78 ${restrict_revision:+--restrict-revision ^"$restrict_revision"} >"$todo" ||
79 die "$(gettext "Could not generate todo list")"
80
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"}
85 }