sequencer: refactor transform_todos() to work on a todo_list

This refactors transform_todos() to work on a todo_list.  The function
is renamed todo_list_transform().

As rebase -p still need to check the todo list from the disk, a new
function is introduced, transform_todo_file().  It is still used by
complete_action() and edit_todo_list() for now, but they will be
replaced in a future commit.

todo_list_transform() is not a static function, because it will be used
by edit_todo_list() from rebase-interactive.c in a future commit.

Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Alban Gruin 2019-01-29 16:01:47 +01:00 committed by Junio C Hamano
parent 6ad656db9b
commit cbef27d61c
4 changed files with 42 additions and 22 deletions

View File

@ -253,7 +253,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
} }
case SHORTEN_OIDS: case SHORTEN_OIDS:
case EXPAND_OIDS: case EXPAND_OIDS:
ret = transform_todos(the_repository, flags); ret = transform_todo_file(the_repository, flags);
break; break;
case CHECK_TODO_LIST: case CHECK_TODO_LIST:
ret = check_todo_list(the_repository); ret = check_todo_list(the_repository);

View File

@ -69,7 +69,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
strbuf_release(&buf); strbuf_release(&buf);
transform_todos(r, flags | TODO_LIST_SHORTEN_IDS); transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS);
if (strbuf_read_file(&buf, todo_file, 0) < 0) if (strbuf_read_file(&buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file); return error_errno(_("could not read '%s'."), todo_file);
@ -85,7 +85,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
if (launch_sequence_editor(todo_file, NULL, NULL)) if (launch_sequence_editor(todo_file, NULL, NULL))
return -1; return -1;
transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS)); transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS));
return 0; return 0;
} }

View File

@ -4562,27 +4562,18 @@ int sequencer_add_exec_commands(struct repository *r,
return i; return i;
} }
int transform_todos(struct repository *r, unsigned flags) void todo_list_transform(struct repository *r, struct todo_list *todo_list,
unsigned flags)
{ {
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct todo_item *item; struct todo_item *item;
int i; int i;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
return error(_("could not read '%s'."), todo_file);
if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
todo_list_release(&todo_list);
return error(_("unusable todo list: '%s'"), todo_file);
}
for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
/* if the item is not a command write it and continue */ /* if the item is not a command write it and continue */
if (item->command >= TODO_COMMENT) { if (item->command >= TODO_COMMENT) {
strbuf_addf(&buf, "%.*s\n", item->arg_len, strbuf_addf(&buf, "%.*s\n", item->arg_len,
todo_item_get_arg(&todo_list, item)); todo_item_get_arg(todo_list, item));
continue; continue;
} }
@ -4613,12 +4604,39 @@ int transform_todos(struct repository *r, unsigned flags)
strbuf_addch(&buf, '\n'); strbuf_addch(&buf, '\n');
else else
strbuf_addf(&buf, " %.*s\n", item->arg_len, strbuf_addf(&buf, " %.*s\n", item->arg_len,
todo_item_get_arg(&todo_list, item)); todo_item_get_arg(todo_list, item));
} }
i = write_message(buf.buf, buf.len, todo_file, 0); strbuf_reset(&todo_list->buf);
strbuf_add(&todo_list->buf, buf.buf, buf.len);
strbuf_release(&buf);
if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
BUG("unusable todo list");
}
int transform_todo_file(struct repository *r, unsigned flags)
{
const char *todo_file = rebase_path_todo();
struct todo_list todo_list = TODO_LIST_INIT;
int res;
if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
todo_list_release(&todo_list);
return error(_("unusable todo list: '%s'"), todo_file);
}
todo_list_transform(r, &todo_list, flags);
res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
todo_list_release(&todo_list); todo_list_release(&todo_list);
return i;
if (res)
return error_errno(_("could not write '%s'."), todo_file);
return 0;
} }
enum missing_commit_check_level get_missing_commit_check_level(void) enum missing_commit_check_level get_missing_commit_check_level(void)
@ -4880,7 +4898,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
return error(_("could not copy '%s' to '%s'."), todo_file, return error(_("could not copy '%s' to '%s'."), todo_file,
rebase_path_todo_backup()); rebase_path_todo_backup());
if (transform_todos(r, flags | TODO_LIST_SHORTEN_IDS)) if (transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS))
return error(_("could not transform the todo list")); return error(_("could not transform the todo list"));
strbuf_reset(buf); strbuf_reset(buf);
@ -4909,7 +4927,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
return -1; return -1;
} }
if (transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS))) if (transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
return error(_("could not transform the todo list")); return error(_("could not transform the todo list"));
if (opts->allow_ff && skip_unnecessary_picks(r, &oid)) if (opts->allow_ff && skip_unnecessary_picks(r, &oid))

View File

@ -121,6 +121,8 @@ struct todo_list {
int todo_list_parse_insn_buffer(struct repository *r, char *buf, int todo_list_parse_insn_buffer(struct repository *r, char *buf,
struct todo_list *todo_list); struct todo_list *todo_list);
void todo_list_transform(struct repository *r, struct todo_list *todo_list,
unsigned flags);
void todo_list_release(struct todo_list *todo_list); void todo_list_release(struct todo_list *todo_list);
const char *todo_item_get_arg(struct todo_list *todo_list, const char *todo_item_get_arg(struct todo_list *todo_list,
struct todo_item *item); struct todo_item *item);
@ -148,7 +150,7 @@ int sequencer_make_script(struct repository *repo, FILE *out,
unsigned flags); unsigned flags);
int sequencer_add_exec_commands(struct repository *r, const char *command); int sequencer_add_exec_commands(struct repository *r, const char *command);
int transform_todos(struct repository *r, unsigned flags); int transform_todo_file(struct repository *r, unsigned flags);
enum missing_commit_check_level get_missing_commit_check_level(void); enum missing_commit_check_level get_missing_commit_check_level(void);
int check_todo_list(struct repository *r); int check_todo_list(struct repository *r);
int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags, int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,