Re: [PATCH v3 06/16] sequencer: refactor sequencer_add_exec_commands() to work on a todo_list
- From
Alban Gruin <alban.gruin@gmail.com>
- Date
- Dec 28, 2018, 19:42 UTC
- Message-ID
- <58d2a1e9-e798-acb6-d8af-3e67e18df057@gmail.com>
- In-Reply-To
- <a1a828e5-6faa-b1f2-20c3-a53b1a3d779a@talktalk.net>
Hi Phillip and Johannes,
many thanks for your suggestions and feedback, I really appreciate it.
Le 10/12/2018 à 15:33, Phillip Wood a écrit :
Show 53 quoted lines
> On 30/11/2018 19:06, Johannes Schindelin wrote:
>> Hi,
>>
>> On Fri, 30 Nov 2018, Phillip Wood wrote:
>>
>>>> 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?
>
> I think that sounds sensible (though I haven't looked at how much work
> it would be), having a comment and calling it arg_offset would be my
> preference.
> It’s not a lot of work, actually. Most usages of 'arg' are concentrated in two functions (parse_insn_line() and pick_commits()). Some of the subsequent patches of this series also use 'arg', and adapting them is trivial.
In the end, most of the work went into todo_list_add_exec_commands(), and the result is pretty clean.
Cheers, Alban