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>

Alban Gruin committed Jan 29, 2019 at 16:01 UTC cbef27d61cd6ae4f1ecae054eb9df06e898148d8
4 files changed +42 -22
builtin/rebase--interactive.c
+1 -1
@@ -253,7 +253,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
253 }
254 case SHORTEN_OIDS:
255 case EXPAND_OIDS:
256 - ret = transform_todos(the_repository, flags);
256 + ret = transform_todo_file(the_repository, flags);
257 break;
258 case CHECK_TODO_LIST:
259 ret = check_todo_list(the_repository);
rebase-interactive.c
+2 -2
@@ -69,7 +69,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
69
70 strbuf_release(&buf);
71
72 - transform_todos(r, flags | TODO_LIST_SHORTEN_IDS);
72 + transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS);
73
74 if (strbuf_read_file(&buf, todo_file, 0) < 0)
75 return error_errno(_("could not read '%s'."), todo_file);
@@ -85,7 +85,7 @@ int edit_todo_list(struct repository *r, unsigned flags)
85 if (launch_sequence_editor(todo_file, NULL, NULL))
86 return -1;
87
88 - transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS));
88 + transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS));
89
90 return 0;
91 }
sequencer.c
+36 -18
@@ -4562,27 +4562,18 @@ int sequencer_add_exec_commands(struct repository *r,
4562 return i;
4563 }
4564
4565 -int transform_todos(struct repository *r, unsigned flags)
4565 +void todo_list_transform(struct repository *r, struct todo_list *todo_list,
4566 + unsigned flags)
4567 {
4567 - const char *todo_file = rebase_path_todo();
4568 - struct todo_list todo_list = TODO_LIST_INIT;
4568 struct strbuf buf = STRBUF_INIT;
4569 struct todo_item *item;
4570 int i;
4571
4573 - if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
4574 - return error(_("could not read '%s'."), todo_file);
4575 -
4576 - if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
4577 - todo_list_release(&todo_list);
4578 - return error(_("unusable todo list: '%s'"), todo_file);
4579 - }
4580 -
4581 - for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
4572 + for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
4573 /* if the item is not a command write it and continue */
4574 if (item->command >= TODO_COMMENT) {
4575 strbuf_addf(&buf, "%.*s\n", item->arg_len,
4585 - todo_item_get_arg(&todo_list, item));
4576 + todo_item_get_arg(todo_list, item));
4577 continue;
4578 }
4579
@@ -4613,12 +4604,39 @@ int transform_todos(struct repository *r, unsigned flags)
4604 strbuf_addch(&buf, '\n');
4605 else
4606 strbuf_addf(&buf, " %.*s\n", item->arg_len,
4616 - todo_item_get_arg(&todo_list, item));
4607 + todo_item_get_arg(todo_list, item));
4608 }
4609
4619 - i = write_message(buf.buf, buf.len, todo_file, 0);
4610 + strbuf_reset(&todo_list->buf);
4611 + strbuf_add(&todo_list->buf, buf.buf, buf.len);
4612 + strbuf_release(&buf);
4613 +
4614 + if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
4615 + BUG("unusable todo list");
4616 +}
4617 +
4618 +int transform_todo_file(struct repository *r, unsigned flags)
4619 +{
4620 + const char *todo_file = rebase_path_todo();
4621 + struct todo_list todo_list = TODO_LIST_INIT;
4622 + int res;
4623 +
4624 + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
4625 + return error_errno(_("could not read '%s'."), todo_file);
4626 +
4627 + if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list)) {
4628 + todo_list_release(&todo_list);
4629 + return error(_("unusable todo list: '%s'"), todo_file);
4630 + }
4631 +
4632 + todo_list_transform(r, &todo_list, flags);
4633 +
4634 + res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
4635 todo_list_release(&todo_list);
4621 - return i;
4636 +
4637 + if (res)
4638 + return error_errno(_("could not write '%s'."), todo_file);
4639 + return 0;
4640 }
4641
4642 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
4898 return error(_("could not copy '%s' to '%s'."), todo_file,
4899 rebase_path_todo_backup());
4900
4883 - if (transform_todos(r, flags | TODO_LIST_SHORTEN_IDS))
4901 + if (transform_todo_file(r, flags | TODO_LIST_SHORTEN_IDS))
4902 return error(_("could not transform the todo list"));
4903
4904 strbuf_reset(buf);
@@ -4909,7 +4927,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
4927 return -1;
4928 }
4929
4912 - if (transform_todos(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
4930 + if (transform_todo_file(r, flags & ~(TODO_LIST_SHORTEN_IDS)))
4931 return error(_("could not transform the todo list"));
4932
4933 if (opts->allow_ff && skip_unnecessary_picks(r, &oid))
sequencer.h
+3 -1
@@ -121,6 +121,8 @@ struct todo_list {
121
122 int todo_list_parse_insn_buffer(struct repository *r, char *buf,
123 struct todo_list *todo_list);
124 +void todo_list_transform(struct repository *r, struct todo_list *todo_list,
125 + unsigned flags);
126 void todo_list_release(struct todo_list *todo_list);
127 const char *todo_item_get_arg(struct todo_list *todo_list,
128 struct todo_item *item);
@@ -148,7 +150,7 @@ int sequencer_make_script(struct repository *repo, FILE *out,
150 unsigned flags);
151
152 int sequencer_add_exec_commands(struct repository *r, const char *command);
151 -int transform_todos(struct repository *r, unsigned flags);
153 +int transform_todo_file(struct repository *r, unsigned flags);
154 enum missing_commit_check_level get_missing_commit_check_level(void);
155 int check_todo_list(struct repository *r);
156 int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,