Skip to content

Commit a6651eb

Browse files
committed
rebase -i: rewrite checkout_onto() in C
This rewrites checkout_onto() from shell to C. A new command (“checkout-onto”) is added to rebase--helper.c. The shell version is then stripped. Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
1 parent 47885d9 commit a6651eb

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

builtin/rebase--helper.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1717
enum {
1818
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
1919
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
20-
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, CHECKOUT_BASE
20+
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, CHECKOUT_BASE,
21+
CHECKOUT_ONTO
2122
} command = 0;
2223
struct option options[] = {
2324
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -53,6 +54,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5354
EDIT_TODO),
5455
OPT_CMDMODE(0, "checkout-base", &command,
5556
N_("checkout the base commit"), CHECKOUT_BASE),
57+
OPT_CMDMODE(0, "checkout-onto", &command,
58+
N_("checkout a commit"), CHECKOUT_ONTO),
5659
OPT_END()
5760
};
5861

@@ -98,5 +101,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
98101
return !!edit_todo_list(flags);
99102
if (command == CHECKOUT_BASE && argc == 2)
100103
return !!checkout_base_commit(&opts, argv[1], verbose);
104+
if (command == CHECKOUT_ONTO && argc == 4)
105+
return !!checkout_onto(&opts, argv[1], argv[2], argv[3], verbose);
101106
usage_with_options(builtin_rebase_helper_usage, options);
102107
}

git-rebase--interactive.sh

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ case "$comment_char" in
2828
;;
2929
esac
3030

31-
orig_reflog_action="$GIT_REFLOG_ACTION"
32-
33-
comment_for_reflog () {
34-
case "$orig_reflog_action" in
35-
''|rebase*)
36-
GIT_REFLOG_ACTION="rebase -i ($1)"
37-
export GIT_REFLOG_ACTION
38-
;;
39-
esac
40-
}
41-
4231
die_abort () {
4332
apply_autostash
4433
rm -rf "$state_dir"
@@ -70,14 +59,6 @@ collapse_todo_ids() {
7059
git rebase--helper --shorten-ids
7160
}
7261

73-
# Switch to the branch in $into and notify it in the reflog
74-
checkout_onto () {
75-
comment_for_reflog start
76-
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
77-
output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
78-
git update-ref ORIG_HEAD $orig_head
79-
}
80-
8162
get_missing_commit_check_level () {
8263
check_level=$(git config --get rebase.missingCommitsCheck)
8364
check_level=${check_level:-ignore}
@@ -176,7 +157,8 @@ EOF
176157

177158
git rebase--helper --check-todo-list || {
178159
ret=$?
179-
checkout_onto
160+
git rebase--helper --checkout-onto "$onto_name" "$onto" \
161+
"$orig_head" ${verbose:+--verbose}
180162
exit $ret
181163
}
182164

@@ -186,7 +168,8 @@ EOF
186168
onto="$(git rebase--helper --skip-unnecessary-picks)" ||
187169
die "Could not skip unnecessary pick commands"
188170

189-
checkout_onto
171+
git rebase--helper --checkout-onto "$onto_name" "$onto" "$orig_head" \
172+
${verbose:+--verbose}
190173
require_clean_work_tree "rebase"
191174
exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
192175
--continue

sequencer.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,6 +3161,25 @@ int checkout_base_commit(struct replay_opts *opts, const char *commit,
31613161
return 0;
31623162
}
31633163

3164+
int checkout_onto(struct replay_opts *opts,
3165+
const char *onto_name, const char *onto,
3166+
const char *orig_head, unsigned verbose)
3167+
{
3168+
struct object_id oid;
3169+
const char *action = reflog_message(opts, "start", "checkout %s", onto_name);
3170+
3171+
if (get_oid(orig_head, &oid))
3172+
return error(_("%s: not a valid OID"), orig_head);
3173+
3174+
if (run_git_checkout(opts, onto, verbose, action)) {
3175+
apply_autostash(opts);
3176+
sequencer_remove_state(opts);
3177+
return error(_("could not detach HEAD"));
3178+
}
3179+
3180+
return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
3181+
}
3182+
31643183
static const char rescheduled_advice[] =
31653184
N_("Could not execute the todo command\n"
31663185
"\n"

sequencer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ void commit_post_rewrite(const struct commit *current_head,
102102

103103
int checkout_base_commit(struct replay_opts *opts, const char *commit,
104104
int verbose);
105+
int checkout_onto(struct replay_opts *opts,
106+
const char *onto_name, const char *onto,
107+
const char *orig_head, unsigned verbose);
105108

106109
#define SUMMARY_INITIAL_COMMIT (1 << 0)
107110
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)

0 commit comments

Comments
 (0)