Skip to content

Commit 2c58483

Browse files
agrngitster
authored andcommitted
rebase -i: rewrite setup_reflog_action() in C
This rewrites (the misnamed) setup_reflog_action() from shell to C. The new version is called prepare_branch_to_be_rebased(). A new command is added to rebase--helper.c, “checkout-base”, as well as a new flag, “verbose”, to avoid silencing the output of the checkout operation called by checkout_base_commit(). The function `run_git_checkout()` will also be used in the next commit, therefore its code is not part of `checkout_base_commit()`. The shell version is then stripped in favour of a call to the helper. As $GIT_REFLOG_ACTION is no longer set at the first call of checkout_onto(), a call to comment_for_reflog() is added at the beginning of this function. Signed-off-by: Alban Gruin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 34bec2c commit 2c58483

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

builtin/rebase--helper.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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
21+
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO, PREPARE_BRANCH
2222
} command = 0;
2323
struct option options[] = {
2424
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -28,6 +28,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2828
OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
2929
OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
3030
N_("keep original branch points of cousins")),
31+
OPT__VERBOSE(&opts.verbose, N_("be verbose")),
3132
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
3233
CONTINUE),
3334
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -51,6 +52,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5152
OPT_CMDMODE(0, "edit-todo", &command,
5253
N_("edit the todo list during an interactive rebase"),
5354
EDIT_TODO),
55+
OPT_CMDMODE(0, "prepare-branch", &command,
56+
N_("prepare the branch to be rebased"), PREPARE_BRANCH),
5457
OPT_END()
5558
};
5659

@@ -94,5 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
9497
return !!append_todo_help(0, keep_empty);
9598
if (command == EDIT_TODO && argc == 1)
9699
return !!edit_todo_list(flags);
100+
if (command == PREPARE_BRANCH && argc == 2)
101+
return !!prepare_branch_to_be_rebased(&opts, argv[1]);
97102
usage_with_options(builtin_rebase_helper_usage, options);
98103
}

git-rebase--interactive.sh

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ collapse_todo_ids() {
7272

7373
# Switch to the branch in $into and notify it in the reflog
7474
checkout_onto () {
75+
comment_for_reflog start
7576
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
7677
output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
7778
git update-ref ORIG_HEAD $orig_head
@@ -119,19 +120,6 @@ initiate_action () {
119120
esac
120121
}
121122

122-
setup_reflog_action () {
123-
comment_for_reflog start
124-
125-
if test ! -z "$switch_to"
126-
then
127-
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
128-
output git checkout "$switch_to" -- ||
129-
die "$(eval_gettext "Could not checkout \$switch_to")"
130-
131-
comment_for_reflog start
132-
fi
133-
}
134-
135123
init_basic_state () {
136124
orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
137125
mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
@@ -211,7 +199,7 @@ git_rebase__interactive () {
211199
return 0
212200
fi
213201

214-
setup_reflog_action
202+
git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
215203
init_basic_state
216204

217205
init_revisions_and_shortrevisions

sequencer.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,6 +3139,36 @@ static const char *reflog_message(struct replay_opts *opts,
31393139
return buf.buf;
31403140
}
31413141

3142+
static int run_git_checkout(struct replay_opts *opts, const char *commit,
3143+
const char *action)
3144+
{
3145+
struct child_process cmd = CHILD_PROCESS_INIT;
3146+
3147+
cmd.git_cmd = 1;
3148+
3149+
argv_array_push(&cmd.args, "checkout");
3150+
argv_array_push(&cmd.args, commit);
3151+
argv_array_pushf(&cmd.env_array, GIT_REFLOG_ACTION "=%s", action);
3152+
3153+
if (opts->verbose)
3154+
return run_command(&cmd);
3155+
else
3156+
return run_command_silent_on_success(&cmd);
3157+
}
3158+
3159+
int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit)
3160+
{
3161+
const char *action;
3162+
3163+
if (commit && *commit) {
3164+
action = reflog_message(opts, "start", "checkout %s", commit);
3165+
if (run_git_checkout(opts, commit, action))
3166+
return error(_("could not checkout %s"), commit);
3167+
}
3168+
3169+
return 0;
3170+
}
3171+
31423172
static const char rescheduled_advice[] =
31433173
N_("Could not execute the todo command\n"
31443174
"\n"

sequencer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ int update_head_with_reflog(const struct commit *old_head,
109109
void commit_post_rewrite(const struct commit *current_head,
110110
const struct object_id *new_head);
111111

112+
int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
113+
112114
#define SUMMARY_INITIAL_COMMIT (1 << 0)
113115
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
114116
void print_commit_summary(const char *prefix, const struct object_id *oid,

0 commit comments

Comments
 (0)