Browse Source

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>
maint
Alban Gruin 7 years ago committed by Junio C Hamano
parent
commit
64a43cbd5d
  1. 13
      builtin/rebase--helper.c
  2. 11
      git-rebase--interactive.sh
  3. 27
      rebase-interactive.c
  4. 1
      rebase-interactive.h

13
builtin/rebase--helper.c

@ -13,12 +13,12 @@ static const char * const builtin_rebase_helper_usage[] = {
int cmd_rebase__helper(int argc, const char **argv, const char *prefix) int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
{ {
struct replay_opts opts = REPLAY_OPTS_INIT; struct replay_opts opts = REPLAY_OPTS_INIT;
unsigned flags = 0, keep_empty = 0, rebase_merges = 0, write_edit_todo = 0; unsigned flags = 0, keep_empty = 0, rebase_merges = 0;
int abbreviate_commands = 0, rebase_cousins = -1; int abbreviate_commands = 0, rebase_cousins = -1;
enum { enum {
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS, CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH, CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
ADD_EXEC, APPEND_TODO_HELP ADD_EXEC, APPEND_TODO_HELP, EDIT_TODO
} command = 0; } command = 0;
struct option options[] = { struct option options[] = {
OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")), 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)
OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")), OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
OPT_BOOL(0, "rebase-cousins", &rebase_cousins, OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
N_("keep original branch points of cousins")), N_("keep original branch points of cousins")),
OPT_BOOL(0, "write-edit-todo", &write_edit_todo,
N_("append the edit-todo message to the todo-list")),
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
CONTINUE), CONTINUE),
OPT_CMDMODE(0, "abort", &command, N_("abort rebase"), OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
@ -50,6 +48,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
N_("insert exec commands in todo list"), ADD_EXEC), N_("insert exec commands in todo list"), ADD_EXEC),
OPT_CMDMODE(0, "append-todo-help", &command, OPT_CMDMODE(0, "append-todo-help", &command,
N_("insert the help in the todo list"), APPEND_TODO_HELP), N_("insert the help in the todo list"), APPEND_TODO_HELP),
OPT_CMDMODE(0, "edit-todo", &command,
N_("edit the todo list during an interactive rebase"),
EDIT_TODO),
OPT_END() OPT_END()
}; };


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

11
git-rebase--interactive.sh

@ -108,16 +108,7 @@ initiate_action () {
--continue --continue
;; ;;
edit-todo) edit-todo)
git stripspace --strip-comments <"$todo" >"$todo".new exec git rebase--helper --edit-todo
mv -f "$todo".new "$todo"
collapse_todo_ids
git rebase--helper --append-todo-help --write-edit-todo

git_sequence_editor "$todo" ||
die "$(gettext "Could not execute editor")"
expand_todo_ids

exit
;; ;;
show-current-patch) show-current-patch)
exec git show REBASE_HEAD -- exec git show REBASE_HEAD --

27
rebase-interactive.c

@ -66,3 +66,30 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)


return ret; return ret;
} }

int edit_todo_list(unsigned flags)
{
struct strbuf buf = STRBUF_INIT;
const char *todo_file = rebase_path_todo();

if (strbuf_read_file(&buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);

strbuf_stripspace(&buf, 1);
if (write_message(buf.buf, buf.len, todo_file, 0)) {
strbuf_release(&buf);
return -1;
}

strbuf_release(&buf);

transform_todos(flags | TODO_LIST_SHORTEN_IDS);
append_todo_help(1, 0);

if (launch_sequence_editor(todo_file, NULL, NULL))
return -1;

transform_todos(flags & ~(TODO_LIST_SHORTEN_IDS));

return 0;
}

1
rebase-interactive.h

@ -2,5 +2,6 @@
#define REBASE_INTERACTIVE_H #define REBASE_INTERACTIVE_H


int append_todo_help(unsigned edit_todo, unsigned keep_empty); int append_todo_help(unsigned edit_todo, unsigned keep_empty);
int edit_todo_list(unsigned flags);


#endif #endif

Loading…
Cancel
Save