Skip to content

Commit 4df66c4

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c58483 commit 4df66c4

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
@@ -18,7 +18,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1818
enum {
1919
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
2020
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
21-
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH
21+
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH,
22+
CHECKOUT_ONTO
2223
} command = 0;
2324
struct option options[] = {
2425
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -54,6 +55,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5455
EDIT_TODO),
5556
OPT_CMDMODE(0, "prepare-branch", &command,
5657
N_("prepare the branch to be rebased"), PREPARE_BRANCH),
58+
OPT_CMDMODE(0, "checkout-onto", &command,
59+
N_("checkout a commit"), CHECKOUT_ONTO),
5760
OPT_END()
5861
};
5962

@@ -99,5 +102,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
99102
return !!edit_todo_list(flags);
100103
if (command == PREPARE_BRANCH && argc == 2)
101104
return !!prepare_branch_to_be_rebased(&opts, argv[1]);
105+
if (command == CHECKOUT_ONTO && argc == 4)
106+
return !!checkout_onto(&opts, argv[1], argv[2], argv[3]);
102107
usage_with_options(builtin_rebase_helper_usage, options);
103108
}

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
@@ -3169,6 +3169,25 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit)
31693169
return 0;
31703170
}
31713171

3172+
int checkout_onto(struct replay_opts *opts,
3173+
const char *onto_name, const char *onto,
3174+
const char *orig_head)
3175+
{
3176+
struct object_id oid;
3177+
const char *action = reflog_message(opts, "start", "checkout %s", onto_name);
3178+
3179+
if (get_oid(orig_head, &oid))
3180+
return error(_("%s: not a valid OID"), orig_head);
3181+
3182+
if (run_git_checkout(opts, onto, action)) {
3183+
apply_autostash(opts);
3184+
sequencer_remove_state(opts);
3185+
return error(_("could not detach HEAD"));
3186+
}
3187+
3188+
return update_ref(NULL, "ORIG_HEAD", &oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR);
3189+
}
3190+
31723191
static const char rescheduled_advice[] =
31733192
N_("Could not execute the todo command\n"
31743193
"\n"

sequencer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ void commit_post_rewrite(const struct commit *current_head,
110110
const struct object_id *new_head);
111111

112112
int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
113+
int checkout_onto(struct replay_opts *opts,
114+
const char *onto_name, const char *onto,
115+
const char *orig_head);
113116

114117
#define SUMMARY_INITIAL_COMMIT (1 << 0)
115118
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)

0 commit comments

Comments
 (0)