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

Alban Gruin committed Aug 10, 2018 at 18:51 UTC 2c58483a5983d1313f674e2ba32b3bac24df6911
4 files changed +40 -15
builtin/rebase--helper.c
+6 -1
@@ -18,7 +18,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
18 enum {
19 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
20 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
22 } command = 0;
23 struct option options[] = {
24 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)
28 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
29 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
30 N_("keep original branch points of cousins")),
31 + OPT__VERBOSE(&opts.verbose, N_("be verbose")),
32 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
33 CONTINUE),
34 OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -51,6 +52,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
52 OPT_CMDMODE(0, "edit-todo", &command,
53 N_("edit the todo list during an interactive rebase"),
54 EDIT_TODO),
55 + OPT_CMDMODE(0, "prepare-branch", &command,
56 + N_("prepare the branch to be rebased"), PREPARE_BRANCH),
57 OPT_END()
58 };
59
@@ -94,5 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
97 return !!append_todo_help(0, keep_empty);
98 if (command == EDIT_TODO && argc == 1)
99 return !!edit_todo_list(flags);
100 + if (command == PREPARE_BRANCH && argc == 2)
101 + return !!prepare_branch_to_be_rebased(&opts, argv[1]);
102 usage_with_options(builtin_rebase_helper_usage, options);
103 }
git-rebase--interactive.sh
+2 -14
@@ -72,6 +72,7 @@ collapse_todo_ids() {
72
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
@@ -119,19 +120,6 @@ initiate_action () {
120 esac
121 }
122
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 -
123 init_basic_state () {
124 orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
125 mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
@@ -211,7 +199,7 @@ git_rebase__interactive () {
199 return 0
200 fi
201
214 - setup_reflog_action
202 + git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
203 init_basic_state
204
205 init_revisions_and_shortrevisions
sequencer.c
+30
@@ -3139,6 +3139,36 @@ static const char *reflog_message(struct replay_opts *opts,
3139 return buf.buf;
3140 }
3141
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 +
3172 static const char rescheduled_advice[] =
3173 N_("Could not execute the todo command\n"
3174 "\n"
sequencer.h
+2
@@ -109,6 +109,8 @@ int update_head_with_reflog(const struct commit *old_head,
109 void commit_post_rewrite(const struct commit *current_head,
110 const struct object_id *new_head);
111
112 +int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
113 +
114 #define SUMMARY_INITIAL_COMMIT (1 << 0)
115 #define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
116 void print_commit_summary(const char *prefix, const struct object_id *oid,