rebase -i: 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alban Gruin committed
Aug 10, 2018 at 18:51 UTC
64a43cbd5da23718dee242d6ad4d5823128bf564
4 files changed
+37
-15
builtin/rebase--helper.c
+8
-5
@@ -13,12 +13,12 @@ static const char * const builtin_rebase_helper_usage[] = {
13
int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
14
{
15
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;
17
int abbreviate_commands = 0, rebase_cousins = -1;
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
21
+ ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO
22
} command = 0;
23
struct option options[] = {
24
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)
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_BOOL(0, "write-edit-todo", &write_edit_todo,
32
- N_("append the edit-todo message to the todo-list")),
31
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
32
CONTINUE),
33
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@@ -50,6 +48,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
48
N_("insert exec commands in todo list"), ADD_EXEC),
49
OPT_CMDMODE(0, "append-todo-help", &command,
50
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),
54
OPT_END()
55
};
56
@@ -90,6 +91,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
91
if (command == ADD_EXEC && argc == 2)
92
return !!sequencer_add_exec_commands(argv[1]);
93
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);
97
usage_with_options(builtin_rebase_helper_usage, options);
98
}
git-rebase--interactive.sh
+1
-10
@@ -108,16 +108,7 @@ initiate_action () {
108
--continue
109
;;
110
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
112
;;
113
show-current-patch)
114
exec git show REBASE_HEAD --
rebase-interactive.c
+27
@@ -66,3 +66,30 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
66
67
return ret;
68
}
69
+
70
+int edit_todo_list(unsigned flags)
71
+{
72
+ struct strbuf buf = STRBUF_INIT;
73
+ const char *todo_file = rebase_path_todo();
74
+
75
+ if (strbuf_read_file(&buf, todo_file, 0) < 0)
76
+ return error_errno(_("could not read '%s'."), todo_file);
77
+
78
+ strbuf_stripspace(&buf, 1);
79
+ if (write_message(buf.buf, buf.len, todo_file, 0)) {
80
+ strbuf_release(&buf);
81
+ return -1;
82
+ }
83
+
84
+ strbuf_release(&buf);
85
+
86
+ transform_todos(flags | TODO_LIST_SHORTEN_IDS);
87
+ append_todo_help(1, 0);
88
+
89
+ if (launch_sequence_editor(todo_file, NULL, NULL))
90
+ return -1;
91
+
92
+ transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS));
93
+
94
+ return 0;
95
+}
rebase-interactive.h
+1
@@ -2,5 +2,6 @@
2
#define REBASE_INTERACTIVE_H
3
4
int append_todo_help(unsigned edit_todo, unsigned keep_empty);
5
+int edit_todo_list(unsigned flags);
6
7
#endif