sequencer: introduce todo_list_write_to_file()

This introduces a new function to recreate the text of a todo list from its commands and write it to a file. This will be useful as the next few commits will change the use of the buffer in struct todo_list so it will no longer be a mirror of the file on disk. This functionality already exists in todo_list_transform(), but this function was made to replace the buffer of a todo list, which is not what we want here. Thus, the part of todo_list_transform() that replaces the buffer is dropped, and the function is renamed todo_list_to_strbuf(). It is called by todo_list_write_to_file() to fill the buffer to write to the disk. todo_list_write_to_file() can also take care of appending the help text to the buffer before writing it to the disk, or to write only the first n items of the list. This feature will be used by skip_unnecessary_picks(), which has to write done commands in a file. 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 616d7740cfbe65533af8ff1dabcf4e56f6baad5a
2 files changed +48 -24
sequencer.c
+41 -20
@@ -4562,26 +4562,28 @@ int sequencer_add_exec_commands(struct repository *r,
4562 return i;
4563 }
4564
4565 -void todo_list_transform(struct repository *r, struct todo_list *todo_list,
4566 - unsigned flags)
4565 +static void todo_list_to_strbuf(struct repository *r, struct todo_list *todo_list,
4566 + struct strbuf *buf, int num, unsigned flags)
4567 {
4568 - struct strbuf buf = STRBUF_INIT;
4568 struct todo_item *item;
4570 - int i;
4569 + int i, max = todo_list->nr;
4570
4572 - for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
4571 + if (num > 0 && num < max)
4572 + max = num;
4573 +
4574 + for (item = todo_list->items, i = 0; i < max; i++, item++) {
4575 /* if the item is not a command write it and continue */
4576 if (item->command >= TODO_COMMENT) {
4575 - strbuf_addf(&buf, "%.*s\n", item->arg_len,
4577 + strbuf_addf(buf, "%.*s\n", item->arg_len,
4578 todo_item_get_arg(todo_list, item));
4579 continue;
4580 }
4581
4582 /* add command to the buffer */
4583 if (flags & TODO_LIST_ABBREVIATE_CMDS)
4582 - strbuf_addch(&buf, command_to_char(item->command));
4584 + strbuf_addch(buf, command_to_char(item->command));
4585 else
4584 - strbuf_addstr(&buf, command_to_string(item->command));
4586 + strbuf_addstr(buf, command_to_string(item->command));
4587
4588 /* add commit id */
4589 if (item->commit) {
@@ -4591,28 +4593,48 @@ void todo_list_transform(struct repository *r, struct todo_list *todo_list,
4593
4594 if (item->command == TODO_MERGE) {
4595 if (item->flags & TODO_EDIT_MERGE_MSG)
4594 - strbuf_addstr(&buf, " -c");
4596 + strbuf_addstr(buf, " -c");
4597 else
4596 - strbuf_addstr(&buf, " -C");
4598 + strbuf_addstr(buf, " -C");
4599 }
4600
4599 - strbuf_addf(&buf, " %s", oid);
4601 + strbuf_addf(buf, " %s", oid);
4602 }
4603
4604 /* add all the rest */
4605 if (!item->arg_len)
4604 - strbuf_addch(&buf, '\n');
4606 + strbuf_addch(buf, '\n');
4607 else
4606 - strbuf_addf(&buf, " %.*s\n", item->arg_len,
4608 + strbuf_addf(buf, " %.*s\n", item->arg_len,
4609 todo_item_get_arg(todo_list, item));
4610 }
4611 +}
4612
4610 - strbuf_reset(&todo_list->buf);
4611 - strbuf_add(&todo_list->buf, buf.buf, buf.len);
4613 +int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
4614 + const char *file, const char *shortrevisions,
4615 + const char *shortonto, int num, unsigned flags)
4616 +{
4617 + int edit_todo = !(shortrevisions && shortonto), res;
4618 + struct strbuf buf = STRBUF_INIT;
4619 +
4620 + todo_list_to_strbuf(r, todo_list, &buf, num, flags);
4621 +
4622 + if (flags & TODO_LIST_APPEND_TODO_HELP) {
4623 + int command_count = count_commands(todo_list);
4624 + if (!edit_todo) {
4625 + strbuf_addch(&buf, '\n');
4626 + strbuf_commented_addf(&buf, Q_("Rebase %s onto %s (%d command)",
4627 + "Rebase %s onto %s (%d commands)",
4628 + command_count),
4629 + shortrevisions, shortonto, command_count);
4630 + }
4631 + append_todo_help(edit_todo, flags & TODO_LIST_KEEP_EMPTY, &buf);
4632 + }
4633 +
4634 + res = write_message(buf.buf, buf.len, file, 0);
4635 strbuf_release(&buf);
4636
4614 - if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
4615 - BUG("unusable todo list");
4637 + return res;
4638 }
4639
4640 int transform_todo_file(struct repository *r, unsigned flags)
@@ -4629,9 +4651,8 @@ int transform_todo_file(struct repository *r, unsigned flags)
4651 return error(_("unusable todo list: '%s'"), todo_file);
4652 }
4653
4632 - todo_list_transform(r, &todo_list, flags);
4633 -
4634 - res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
4654 + res = todo_list_write_to_file(r, &todo_list, todo_file,
4655 + NULL, NULL, -1, flags);
4656 todo_list_release(&todo_list);
4657
4658 if (res)
sequencer.h
+7 -4
@@ -121,8 +121,9 @@ 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);
124 +int todo_list_write_to_file(struct repository *r, struct todo_list *todo_list,
125 + const char *file, const char *shortrevisions,
126 + const char *shortonto, int num, unsigned flags);
127 void todo_list_release(struct todo_list *todo_list);
128 const char *todo_item_get_arg(struct todo_list *todo_list,
129 struct todo_item *item);
@@ -145,8 +146,10 @@ int sequencer_remove_state(struct replay_opts *opts);
146 * commits should be rebased onto the new base, this flag needs to be passed.
147 */
148 #define TODO_LIST_REBASE_COUSINS (1U << 4)
148 -int sequencer_make_script(struct repository *repo, FILE *out,
149 - int argc, const char **argv,
149 +#define TODO_LIST_APPEND_TODO_HELP (1U << 5)
150 +
151 +int sequencer_make_script(struct repository *r, FILE *out, int argc,
152 + const char **argv,
153 unsigned flags);
154
155 int sequencer_add_exec_commands(struct repository *r, const char *command);