Re: [PATCH v3 06/16] sequencer: refactor sequencer_add_exec_commands() to work on a todo_list
- From
Johannes Schindelin <johannes.schindelin@gmx.de>
- Date
- Nov 30, 2018, 19:06 UTC
- Message-ID
- <nycvar.QRO.7.76.6.1811302002340.41@tvgsbejvaqbjf.bet>
- In-Reply-To
- <0e00acd9-5b09-b712-2e21-550e348ea0df@talktalk.net>
Hi,
On Fri, 30 Nov 2018, Phillip Wood wrote:
Show 37 quoted lines
> > diff --git a/sequencer.c b/sequencer.c
> > index 900899ef20..11692d0b98 100644
> > --- a/sequencer.c
> > +++ b/sequencer.c
> > @@ -4394,24 +4394,29 @@ int sequencer_make_script(FILE *out, int argc, const
> > char **argv,
> > return 0;
> > }
> >
> > -/*
> > - * Add commands after pick and (series of) squash/fixup commands
> > - * in the todo list.
> > - */
> > -int sequencer_add_exec_commands(const char *commands)
> > +static void todo_list_add_exec_commands(struct todo_list *todo_list,
> > + struct string_list *commands)
> > {
> > - const char *todo_file = rebase_path_todo();
> > - struct todo_list todo_list = TODO_LIST_INIT;
> > - struct strbuf *buf = &todo_list.buf;
> > - size_t offset = 0, commands_len = strlen(commands);
> > - int i, insert;
> > + struct strbuf *buf = &todo_list->buf;
> > + const char *old_buf = buf->buf;
> > + size_t base_length = buf->len;
> > + int i, insert, nr = 0, alloc = 0;
> > + struct todo_item *items = NULL, *base_items = NULL;
> >
> > - if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
> > - return error(_("could not read '%s'."), todo_file);
> > + for (i = 0; i < commands->nr; ++i) {
> > + strbuf_addstr(buf, commands->items[i].string);
> > + strbuf_addch(buf, '\n');
> > + }
>
> This could cause buf->buf to be reallocated in which case all the
> todo_list_item.arg pointers will be invalidated.I guess it is a good time for me to admit that the `arg` idea was not my best. Maybe it would be good to convert that from a pointer to an offset, e.g. `size_t arg_offset_in_buf;`? Or maybe we should just drop the `_in_buf` suffix and clarify in a comment next to the declaration of the fields that they are offsets in the strbuf?
Ciao, Dscho