Skip to content

Commit 76dfbe4

Browse files
committed
rebase-interactive: rewrite the edit-todo functionality in C
This rewrites the edit-todo functionality from shell to C. To achieve that, a new command mode, `edit-todo`, is added, and the `write-edit-todo` flag is removed, as the shell script does not need to write the edit todo help message to the todo list anymore. The shell version is then stripped in favour of a call to the helper. Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
1 parent 40fec3f commit 76dfbe4

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

builtin/rebase--helper.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ static const char * const builtin_rebase_helper_usage[] = {
1313
int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
1414
{
1515
struct replay_opts opts = REPLAY_OPTS_INIT;
16-
unsigned flags = 0, keep_empty = 0, rebase_merges = 0, write_edit_todo = 0;
16+
unsigned flags = 0, keep_empty = 0, rebase_merges = 0;
1717
int abbreviate_commands = 0, rebase_cousins = -1;
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
21+
ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO
2222
} command = 0;
2323
struct option options[] = {
2424
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -28,8 +28,6 @@ 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_BOOL(0, "write-edit-todo", &write_edit_todo,
32-
N_("append the edit-todo message to the todo-list")),
3331
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
3432
CONTINUE),
3533
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -50,6 +48,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5048
N_("insert exec commands in todo list"), ADD_EXEC),
5149
OPT_CMDMODE(0, "append-todo-help", &command,
5250
N_("insert the help in the todo list"), APPEND_TODO_HELP),
51+
OPT_CMDMODE(0, "edit-todo", &command,
52+
N_("edit the todo list during an interactive rebase"),
53+
EDIT_TODO),
5354
OPT_END()
5455
};
5556

@@ -90,6 +91,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
9091
if (command == ADD_EXEC && argc == 2)
9192
return !!sequencer_add_exec_commands(argv[1]);
9293
if (command == APPEND_TODO_HELP && argc == 1)
93-
return !!append_todo_help(write_edit_todo, keep_empty);
94+
return !!append_todo_help(0, keep_empty);
95+
if (command == EDIT_TODO && argc == 1)
96+
return !!edit_todo_list(flags);
9497
usage_with_options(builtin_rebase_helper_usage, options);
9598
}

git-rebase--interactive.sh

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,7 @@ initiate_action () {
108108
--continue
109109
;;
110110
edit-todo)
111-
git stripspace --strip-comments <"$todo" >"$todo".new
112-
mv -f "$todo".new "$todo"
113-
collapse_todo_ids
114-
git rebase--helper --append-todo-help --write-edit-todo
115-
116-
git_sequence_editor "$todo" ||
117-
die "$(gettext "Could not execute editor")"
118-
expand_todo_ids
119-
120-
exit
111+
exec git rebase--helper --edit-todo
121112
;;
122113
show-current-patch)
123114
exec git show REBASE_HEAD --

rebase-interactive.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,34 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
6666

6767
return ret;
6868
}
69+
70+
int edit_todo_list(unsigned flags)
71+
{
72+
struct strbuf buf = STRBUF_INIT;
73+
const char *todo_file = rebase_path_todo();
74+
FILE *todo;
75+
76+
if (strbuf_read_file(&buf, todo_file, 0) < 0)
77+
return error_errno(_("could not read '%s'."), todo_file);
78+
79+
strbuf_stripspace(&buf, 1);
80+
todo = fopen_or_warn(todo_file, "w");
81+
if (!todo) {
82+
strbuf_release(&buf);
83+
return 1;
84+
}
85+
86+
strbuf_write(&buf, todo);
87+
fclose(todo);
88+
strbuf_release(&buf);
89+
90+
transform_todos(flags | TODO_LIST_SHORTEN_IDS);
91+
append_todo_help(1, 0);
92+
93+
if (launch_sequence_editor(todo_file, NULL, NULL))
94+
return 1;
95+
96+
transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS));
97+
98+
return 0;
99+
}

rebase-interactive.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
#define REBASE_INTERACTIVE_H
33

44
int append_todo_help(unsigned edit_todo, unsigned keep_empty);
5+
int edit_todo_list(unsigned flags);
56

67
#endif

0 commit comments

Comments
 (0)