Skip to content

Commit 616d774

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbef27d commit 616d774

File tree

2 files changed

+48
-24
lines changed

2 files changed

+48
-24
lines changed

sequencer.c

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4562,26 +4562,28 @@ int sequencer_add_exec_commands(struct repository *r,
45624562
return i;
45634563
}
45644564

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)
45674567
{
4568-
struct strbuf buf = STRBUF_INIT;
45694568
struct todo_item *item;
4570-
int i;
4569+
int i, max = todo_list->nr;
45714570

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++) {
45734575
/* if the item is not a command write it and continue */
45744576
if (item->command >= TODO_COMMENT) {
4575-
strbuf_addf(&buf, "%.*s\n", item->arg_len,
4577+
strbuf_addf(buf, "%.*s\n", item->arg_len,
45764578
todo_item_get_arg(todo_list, item));
45774579
continue;
45784580
}
45794581

45804582
/* add command to the buffer */
45814583
if (flags & TODO_LIST_ABBREVIATE_CMDS)
4582-
strbuf_addch(&buf, command_to_char(item->command));
4584+
strbuf_addch(buf, command_to_char(item->command));
45834585
else
4584-
strbuf_addstr(&buf, command_to_string(item->command));
4586+
strbuf_addstr(buf, command_to_string(item->command));
45854587

45864588
/* add commit id */
45874589
if (item->commit) {
@@ -4591,28 +4593,48 @@ void todo_list_transform(struct repository *r, struct todo_list *todo_list,
45914593

45924594
if (item->command == TODO_MERGE) {
45934595
if (item->flags & TODO_EDIT_MERGE_MSG)
4594-
strbuf_addstr(&buf, " -c");
4596+
strbuf_addstr(buf, " -c");
45954597
else
4596-
strbuf_addstr(&buf, " -C");
4598+
strbuf_addstr(buf, " -C");
45974599
}
45984600

4599-
strbuf_addf(&buf, " %s", oid);
4601+
strbuf_addf(buf, " %s", oid);
46004602
}
46014603

46024604
/* add all the rest */
46034605
if (!item->arg_len)
4604-
strbuf_addch(&buf, '\n');
4606+
strbuf_addch(buf, '\n');
46054607
else
4606-
strbuf_addf(&buf, " %.*s\n", item->arg_len,
4608+
strbuf_addf(buf, " %.*s\n", item->arg_len,
46074609
todo_item_get_arg(todo_list, item));
46084610
}
4611+
}
46094612

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);
46124635
strbuf_release(&buf);
46134636

4614-
if (todo_list_parse_insn_buffer(r, todo_list->buf.buf, todo_list))
4615-
BUG("unusable todo list");
4637+
return res;
46164638
}
46174639

46184640
int transform_todo_file(struct repository *r, unsigned flags)
@@ -4629,9 +4651,8 @@ int transform_todo_file(struct repository *r, unsigned flags)
46294651
return error(_("unusable todo list: '%s'"), todo_file);
46304652
}
46314653

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);
46354656
todo_list_release(&todo_list);
46364657

46374658
if (res)

sequencer.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ struct todo_list {
121121

122122
int todo_list_parse_insn_buffer(struct repository *r, char *buf,
123123
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);
126127
void todo_list_release(struct todo_list *todo_list);
127128
const char *todo_item_get_arg(struct todo_list *todo_list,
128129
struct todo_item *item);
@@ -145,8 +146,10 @@ int sequencer_remove_state(struct replay_opts *opts);
145146
* commits should be rebased onto the new base, this flag needs to be passed.
146147
*/
147148
#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,
150153
unsigned flags);
151154

152155
int sequencer_add_exec_commands(struct repository *r, const char *command);

0 commit comments

Comments
 (0)