Skip to content

Commit cd327be

Browse files
committed
rebase--interactive: rewrite append_todo_help() in C
This rewrites append_todo_help() from shell to C. It also incorporates some parts of initiate_action() and complete_action() that also write help texts to the todo file. Two flags are added to rebase--helper.c: one to call append_todo_help() (`--append-todo-help`), and another one to tell append_todo_help() to write the help text suited for the edit-todo mode (`--write-edit-todo`). Finally, append_todo_help() is removed from git-rebase--interactive.sh to use `rebase--helper --append-todo-help` instead. Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
1 parent dde1554 commit cd327be

File tree

4 files changed

+71
-52
lines changed

4 files changed

+71
-52
lines changed

builtin/rebase--helper.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ static const char * const builtin_rebase_helper_usage[] = {
1212
int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1313
{
1414
struct replay_opts opts = REPLAY_OPTS_INIT;
15-
unsigned flags = 0, keep_empty = 0, rebase_merges = 0;
15+
unsigned flags = 0, keep_empty = 0, rebase_merges = 0, write_edit_todo = 0;
1616
int abbreviate_commands = 0, rebase_cousins = -1;
1717
enum {
1818
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
1919
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
20-
ADD_EXEC
20+
ADD_EXEC, APPEND_TODO_HELP
2121
} command = 0;
2222
struct option options[] = {
2323
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -27,6 +27,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
2727
OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
2828
OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
2929
N_("keep original branch points of cousins")),
30+
OPT_BOOL(0, "write-edit-todo", &write_edit_todo,
31+
N_("append the edit-todo message to the todo-list")),
3032
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
3133
CONTINUE),
3234
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -45,6 +47,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
4547
N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
4648
OPT_CMDMODE(0, "add-exec-commands", &command,
4749
N_("insert exec commands in todo list"), ADD_EXEC),
50+
OPT_CMDMODE(0, "append-todo-help", &command,
51+
N_("insert the help in the todo list"), APPEND_TODO_HELP),
4852
OPT_END()
4953
};
5054

@@ -84,5 +88,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
8488
return !!rearrange_squash();
8589
if (command == ADD_EXEC && argc == 2)
8690
return !!sequencer_add_exec_commands(argv[1]);
91+
if (command == APPEND_TODO_HELP && argc == 1)
92+
return !!append_todo_help(write_edit_todo, keep_empty);
8793
usage_with_options(builtin_rebase_helper_usage, options);
8894
}

git-rebase--interactive.sh

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,6 @@ comment_for_reflog () {
3939
esac
4040
}
4141

42-
append_todo_help () {
43-
gettext "
44-
Commands:
45-
p, pick <commit> = use commit
46-
r, reword <commit> = use commit, but edit the commit message
47-
e, edit <commit> = use commit, but stop for amending
48-
s, squash <commit> = use commit, but meld into previous commit
49-
f, fixup <commit> = like \"squash\", but discard this commit's log message
50-
x, exec <command> = run command (the rest of the line) using shell
51-
d, drop <commit> = remove commit
52-
l, label <label> = label current HEAD with a name
53-
t, reset <label> = reset HEAD to a label
54-
m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
55-
. create a merge commit using the original merge commit's
56-
. message (or the oneline, if no original merge commit was
57-
. specified). Use -c <commit> to reword the commit message.
58-
59-
These lines can be re-ordered; they are executed from top to bottom.
60-
" | git stripspace --comment-lines >>"$todo"
61-
62-
if test $(get_missing_commit_check_level) = error
63-
then
64-
gettext "
65-
Do not remove any line. Use 'drop' explicitly to remove a commit.
66-
" | git stripspace --comment-lines >>"$todo"
67-
else
68-
gettext "
69-
If you remove a line here THAT COMMIT WILL BE LOST.
70-
" | git stripspace --comment-lines >>"$todo"
71-
fi
72-
}
73-
7442
die_abort () {
7543
apply_autostash
7644
rm -rf "$state_dir"
@@ -143,13 +111,7 @@ initiate_action () {
143111
git stripspace --strip-comments <"$todo" >"$todo".new
144112
mv -f "$todo".new "$todo"
145113
collapse_todo_ids
146-
append_todo_help
147-
gettext "
148-
You are editing the todo file of an ongoing interactive rebase.
149-
To continue rebase after editing, run:
150-
git rebase --continue
151-
152-
" | git stripspace --comment-lines >>"$todo"
114+
git rebase--helper --append-todo-help --write-edit-todo
153115

154116
git_sequence_editor "$todo" ||
155117
die "$(gettext "Could not execute editor")"
@@ -220,17 +182,7 @@ $comment_char $(eval_ngettext \
220182
"Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
221183
"$todocount")
222184
EOF
223-
append_todo_help
224-
gettext "
225-
However, if you remove everything, the rebase will be aborted.
226-
227-
" | git stripspace --comment-lines >>"$todo"
228-
229-
if test -z "$keep_empty"
230-
then
231-
printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
232-
fi
233-
185+
git rebase--helper --append-todo-help ${keep_empty:+--keep-empty}
234186

235187
has_action "$todo" ||
236188
return 2

sequencer.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,66 @@ int check_todo_list(void)
43264326
return res;
43274327
}
43284328

4329+
int append_todo_help(unsigned edit_todo, unsigned keep_empty)
4330+
{
4331+
struct strbuf buf = STRBUF_INIT;
4332+
FILE *todo;
4333+
int ret;
4334+
const char *msg = _("\nCommands:\n"
4335+
"p, pick <commit> = use commit\n"
4336+
"r, reword <commit> = use commit, but edit the commit message\n"
4337+
"e, edit <commit> = use commit, but stop for amending\n"
4338+
"s, squash <commit> = use commit, but meld into previous commit\n"
4339+
"f, fixup <commit> = like \"squash\", but discard this commit's log message\n"
4340+
"x, exec <command> = run command (the rest of the line) using shell\n"
4341+
"d, drop <commit> = remove commit\n"
4342+
"l, label <label> = label current HEAD with a name\n"
4343+
"t, reset <label> = reset HEAD to a label\n"
4344+
"m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]\n"
4345+
". create a merge commit using the original merge commit's\n"
4346+
". message (or the oneline, if no original merge commit was\n"
4347+
". specified). Use -c <commit> to reword the commit message.\n"
4348+
"\n"
4349+
"These lines can be re-ordered; they are executed from top to bottom.\n");
4350+
4351+
todo = fopen_or_warn(rebase_path_todo(), "a");
4352+
if (!todo)
4353+
return 1;
4354+
4355+
strbuf_add_commented_lines(&buf, msg, strlen(msg));
4356+
4357+
if (get_missing_commit_check_level() == CHECK_ERROR)
4358+
msg = _("\nDo not remove any line. Use 'drop' "
4359+
"explicitly to remove a commit.\n");
4360+
else
4361+
msg = _("\nIf you remove a line here "
4362+
"THAT COMMIT WILL BE LOST.\n");
4363+
4364+
strbuf_add_commented_lines(&buf, msg, strlen(msg));
4365+
4366+
if (edit_todo)
4367+
msg = _("\nYou are editing the todo file "
4368+
"of an ongoing interactive rebase.\n"
4369+
"To continue rebase after editing, run:\n"
4370+
" git rebase --continue\n\n");
4371+
else
4372+
msg = _("\nHowever, if you remove everything, "
4373+
"the rebase will be aborted.\n\n");
4374+
4375+
strbuf_add_commented_lines(&buf, msg, strlen(msg));
4376+
4377+
if (!keep_empty) {
4378+
msg = _("Note that empty commits are commented out");
4379+
strbuf_add_commented_lines(&buf, msg, strlen(msg));
4380+
}
4381+
4382+
ret = fputs(buf.buf, todo);
4383+
fclose(todo);
4384+
strbuf_release(&buf);
4385+
4386+
return ret;
4387+
}
4388+
43294389
static int rewrite_file(const char *path, const char *buf, size_t len)
43304390
{
43314391
int rc = 0;

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ int sequencer_make_script(FILE *out, int argc, const char **argv,
8080
int sequencer_add_exec_commands(const char *command);
8181
int transform_todos(unsigned flags);
8282
int check_todo_list(void);
83+
int append_todo_help(unsigned edit_todo, unsigned keep_empty);
8384
int skip_unnecessary_picks(void);
8485
int rearrange_squash(void);
8586

0 commit comments

Comments
 (0)