git@vger.kernel.org mailing list mirror (one of many)
 help / Atom feed
From: Phillip Wood <phillip.wood@talktalk.net>
To: Alban Gruin <alban.gruin@gmail.com>, git@vger.kernel.org
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2 04/16] sequencer: introduce todo_list_write_to_file()
Date: Tue, 30 Oct 2018 16:28:53 +0000
Message-ID: <03475c29-5317-b105-6102-5cae3a5ae926@talktalk.net> (raw)
In-Reply-To: <20181027212930.9303-5-alban.gruin@gmail.com>

Hi Alban

I like the direction this is going, it is an improvement on re-scanning 
the list at the end of each function.

On 27/10/2018 22:29, Alban Gruin wrote:
> This introduce a new function to recreate the text of a todo list from
> its commands, and then to write it to the disk.  This will be useful in
> the future, the buffer of a todo list won’t be treated as a strict
> mirror of the todo file by some of its functions once they will be
> refactored.

I'd suggest rewording this slightly, maybe something like

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 functionnality can already be found in todo_list_transform(), but

s/functionnality/functionality/

> it is specifically made to replace the buffer of a todo list, which is
> not the desired behaviour.  Thus, the part of todo_list_transform() that
> actually creates the buffer is moved to a new function,
> todo_list_to_strbuf().  The rest is unused, and so is dropped.
> 
> todo_list_write_to_file() can also take care to append the help text to

s/care to append/care of appending/

> the buffer before writing it to the disk, or to write only the first n
> items of the list.

Why/when do we only want to write a subset of the items?

> Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
> ---
>   sequencer.c | 59 ++++++++++++++++++++++++++++++++++++-----------------
>   sequencer.h |  4 +++-
>   2 files changed, 43 insertions(+), 20 deletions(-)
> 
> diff --git a/sequencer.c b/sequencer.c
> index 07296f1f57..0dd45677b1 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -4256,24 +4256,27 @@ int sequencer_add_exec_commands(const char *commands)
>   	return i;
>   }
>   
> -void todo_list_transform(struct todo_list *todo_list, unsigned flags)
> +static void todo_list_to_strbuf(struct todo_list *todo_list, struct strbuf *buf,
> +				int num, unsigned flags)
>   {
> -	struct strbuf buf = STRBUF_INIT;
>   	struct todo_item *item;
> -	int i;
> +	int i, max = todo_list->nr;
>   
> -	for (item = todo_list->items, i = 0; i < todo_list->nr; i++, item++) {
> +	if (num > 0 && num < max)
> +		max = num;
> +
> +	for (item = todo_list->items, i = 0; i < max; i++, item++) {
>   		/* if the item is not a command write it and continue */
>   		if (item->command >= TODO_COMMENT) {
> -			strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg);
> +			strbuf_addf(buf, "%.*s\n", item->arg_len, item->arg);
>   			continue;
>   		}
>   
>   		/* add command to the buffer */
>   		if (flags & TODO_LIST_ABBREVIATE_CMDS)
> -			strbuf_addch(&buf, command_to_char(item->command));
> +			strbuf_addch(buf, command_to_char(item->command));
>   		else
> -			strbuf_addstr(&buf, command_to_string(item->command));
> +			strbuf_addstr(buf, command_to_string(item->command));
>   
>   		/* add commit id */
>   		if (item->commit) {
> @@ -4283,27 +4286,46 @@ void todo_list_transform(struct todo_list *todo_list, unsigned flags)
>   
>   			if (item->command == TODO_MERGE) {
>   				if (item->flags & TODO_EDIT_MERGE_MSG)
> -					strbuf_addstr(&buf, " -c");
> +					strbuf_addstr(buf, " -c");
>   				else
> -					strbuf_addstr(&buf, " -C");
> +					strbuf_addstr(buf, " -C");
>   			}
>   
> -			strbuf_addf(&buf, " %s", oid);
> +			strbuf_addf(buf, " %s", oid);
>   		}
>   
>   		/* add all the rest */
>   		if (!item->arg_len)
> -			strbuf_addch(&buf, '\n');
> +			strbuf_addch(buf, '\n');
>   		else
> -			strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
> +			strbuf_addf(buf, " %.*s\n", item->arg_len, item->arg);
>   	}
> +}
>   
> -	strbuf_reset(&todo_list->buf);
> -	strbuf_add(&todo_list->buf, buf.buf, buf.len);
> +int todo_list_write_to_file(struct todo_list *todo_list, const char *file,
> +			    const char *shortrevisions, const char *shortonto,
> +			    int command_count, int append_help, int num, unsigned flags)

This is a really long argument list which makes it easy for callers to 
get the parameters in the wrong order. I think append_help could 
probably be folded into the flags, I'm not sure what the command_count 
is used for but I've only read the first few patches. Maybe it would be 
better to pass a struct so we have named fields.

Best Wishes

Phillip

> +{
> +	int edit_todo = !(shortrevisions && shortonto), res;
> +	struct strbuf buf = STRBUF_INIT;
> +
> +	todo_list_to_strbuf(todo_list, &buf, num, flags);
> +
> +	if (append_help) {
> +		if (!edit_todo) {
> +			strbuf_addch(&buf, '\n');
> +			strbuf_commented_addf(&buf, Q_("Rebase %s onto %s (%d command)",
> +						       "Rebase %s onto %s (%d commands)",
> +						       command_count),
> +					      shortrevisions, shortonto, command_count);
> +		}
> +		append_todo_help(edit_todo, flags & TODO_LIST_KEEP_EMPTY, &buf);
> +	}
> +
> +	res = write_message(buf.buf, buf.len, file, 0);
>   	strbuf_release(&buf);
>   
> -	if (todo_list_parse_insn_buffer(todo_list->buf.buf, todo_list))
> -		BUG("unusable todo list");
> +	return res;
>   }
>   
>   int transform_todo_file(unsigned flags)
> @@ -4320,9 +4342,8 @@ int transform_todo_file(unsigned flags)
>   		return error(_("unusable todo list: '%s'"), todo_file);
>   	}
>   
> -	todo_list_transform(&todo_list, flags);
> -
> -	res = write_message(todo_list.buf.buf, todo_list.buf.len, todo_file, 0);
> +	res = todo_list_write_to_file(&todo_list, todo_file,
> +				      NULL, NULL, 0, 0, -1, flags);
>   	todo_list_release(&todo_list);
>   	return res;
>   }
> diff --git a/sequencer.h b/sequencer.h
> index 029d842ac5..a299c977fe 100644
> --- a/sequencer.h
> +++ b/sequencer.h
> @@ -113,7 +113,9 @@ struct todo_list {
>   #define TODO_LIST_INIT { STRBUF_INIT }
>   
>   int todo_list_parse_insn_buffer(char *buf, struct todo_list *todo_list);
> -void todo_list_transform(struct todo_list *todo_list, unsigned flags);
> +int todo_list_write_to_file(struct todo_list *todo_list, const char *file,
> +			    const char *shortrevisions, const char *shortonto,
> +			    int command_count, int append_help, int num, unsigned flags);
>   void todo_list_release(struct todo_list *todo_list);
>   
>   /* Call this to setup defaults before parsing command line options */
> 


  reply index

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-07 19:54 [PATCH 00/15] sequencer: refactor functions working on a todo_list Alban Gruin
2018-10-07 19:54 ` [PATCH 01/15] sequencer: clear the number of items of a todo_list before parsing Alban Gruin
2018-10-07 19:54 ` [PATCH 02/15] sequencer: make the todo_list structure public Alban Gruin
2018-10-07 19:54 ` [PATCH 03/15] sequencer: refactor check_todo_list() to work on a todo_list Alban Gruin
2018-10-11 11:23   ` Phillip Wood
2018-10-07 19:54 ` [PATCH 04/15] sequencer: refactor sequencer_add_exec_commands() " Alban Gruin
2018-10-11 11:25   ` Phillip Wood
2018-10-11 16:57     ` Alban Gruin
2018-10-12  9:54       ` Phillip Wood
2018-10-12 12:23         ` Alban Gruin
2018-10-07 19:54 ` [PATCH 05/15] sequencer: refactor rearrange_squash() " Alban Gruin
2018-10-07 19:54 ` [PATCH 06/15] sequencer: refactor transform_todos() " Alban Gruin
2018-10-07 19:54 ` [PATCH 07/15] sequencer: make sequencer_make_script() write its script to a strbuf Alban Gruin
2018-10-12 10:01   ` SZEDER Gábor
2018-10-19  8:16     ` Junio C Hamano
2018-10-19  9:27       ` SZEDER Gábor
2018-10-07 19:54 ` [PATCH 08/15] sequencer: change complete_action() to use the refactored functions Alban Gruin
2018-10-11 13:51   ` Phillip Wood
2018-10-11 17:06     ` Alban Gruin
2018-10-07 19:54 ` [PATCH 09/15] sequencer: refactor skip_unnecessary_picks() to work on a todo_list Alban Gruin
2018-10-07 19:54 ` [PATCH 10/15] rebase-interactive: use todo_list_transform() in edit_todo_list() Alban Gruin
2018-10-11 15:16   ` Phillip Wood
2018-10-11 19:58     ` Alban Gruin
2018-10-07 19:54 ` [PATCH 11/15] rebase-interactive: append_todo_help() changes Alban Gruin
2018-10-07 19:54 ` [PATCH 12/15] rebase-interactive: rewrite edit_todo_list() to handle the initial edit Alban Gruin
2018-10-07 19:54 ` [PATCH 13/15] sequencer: use edit_todo_list() in complete_action() Alban Gruin
2018-10-07 19:54 ` [PATCH 14/15] sequencer: fix a call to error() in transform_todo_file() Alban Gruin
2018-10-07 19:54 ` [PATCH 15/15] rebase--interactive: move transform_todo_file() to rebase--interactive.c Alban Gruin
2018-10-07 20:51 ` [PATCH 00/15] sequencer: refactor functions working on a todo_list Alban Gruin
2018-10-27 21:29 ` [PATCH v2 00/16] " Alban Gruin
2018-10-27 21:29   ` [PATCH v2 01/16] sequencer: changes in parse_insn_buffer() Alban Gruin
2018-10-27 21:29   ` [PATCH v2 02/16] sequencer: make the todo_list structure public Alban Gruin
2018-10-27 21:29   ` [PATCH v2 03/16] sequencer: refactor transform_todos() to work on a todo_list Alban Gruin
2018-10-27 21:29   ` [PATCH v2 04/16] sequencer: introduce todo_list_write_to_file() Alban Gruin
2018-10-30 16:28     ` Phillip Wood [this message]
2018-11-01 23:31       ` Alban Gruin
2018-10-27 21:29   ` [PATCH v2 05/16] sequencer: refactor check_todo_list() to work on a todo_list Alban Gruin
2018-10-27 21:29   ` [PATCH v2 06/16] sequencer: refactor sequencer_add_exec_commands() " Alban Gruin
2018-10-30 16:47     ` Phillip Wood
2018-11-01 23:31       ` Alban Gruin
2018-11-02 10:09         ` Phillip Wood
2018-11-02 16:26           ` Alban Gruin
2018-11-02 17:09             ` Phillip Wood
2018-10-27 21:29   ` [PATCH v2 07/16] sequencer: refactor rearrange_squash() " Alban Gruin
2018-10-27 21:29   ` [PATCH v2 08/16] sequencer: make sequencer_make_script() write its script to a strbuf Alban Gruin
2018-10-27 21:29   ` [PATCH v2 09/16] sequencer: change complete_action() to use the refactored functions Alban Gruin
2018-10-27 21:29   ` [PATCH v2 10/16] sequencer: refactor skip_unnecessary_picks() to work on a todo_list Alban Gruin
2018-10-27 21:29   ` [PATCH v2 11/16] rebase-interactive: use todo_list_write_to_file() in edit_todo_list() Alban Gruin
2018-10-27 21:29   ` [PATCH v2 12/16] rebase-interactive: append_todo_help() changes Alban Gruin
2018-10-27 21:29   ` [PATCH v2 13/16] rebase-interactive: rewrite edit_todo_list() to handle the initial edit Alban Gruin
2018-10-27 21:29   ` [PATCH v2 14/16] sequencer: use edit_todo_list() in complete_action() Alban Gruin
2018-10-27 21:29   ` [PATCH v2 15/16] sequencer: fix a call to error() in transform_todo_file() Alban Gruin
2018-10-27 21:29   ` [PATCH v2 16/16] rebase--interactive: move transform_todo_file() to rebase--interactive.c Alban Gruin
2018-10-29  3:05   ` [PATCH v2 00/16] sequencer: refactor functions working on a todo_list Junio C Hamano
2018-10-29 15:34     ` Alban Gruin
2018-11-09  8:07   ` [PATCH v3 " Alban Gruin
2018-11-09  8:07     ` [PATCH v3 01/16] sequencer: changes in parse_insn_buffer() Alban Gruin
2018-11-09  8:07     ` [PATCH v3 02/16] sequencer: make the todo_list structure public Alban Gruin
2018-11-09  8:07     ` [PATCH v3 03/16] sequencer: refactor transform_todos() to work on a todo_list Alban Gruin
2018-11-09  8:07     ` [PATCH v3 04/16] sequencer: introduce todo_list_write_to_file() Alban Gruin
2018-11-09  8:07     ` [PATCH v3 05/16] sequencer: refactor check_todo_list() to work on a todo_list Alban Gruin
2018-11-09  8:07     ` [PATCH v3 06/16] sequencer: refactor sequencer_add_exec_commands() " Alban Gruin
2018-11-09  8:07     ` [PATCH v3 07/16] sequencer: refactor rearrange_squash() " Alban Gruin
2018-11-09  8:07     ` [PATCH v3 08/16] sequencer: make sequencer_make_script() write its script to a strbuf Alban Gruin
2018-11-09  8:07     ` [PATCH v3 09/16] sequencer: change complete_action() to use the refactored functions Alban Gruin
2018-11-09  8:07     ` [PATCH v3 10/16] sequencer: refactor skip_unnecessary_picks() to work on a todo_list Alban Gruin
2018-11-09  8:08     ` [PATCH v3 11/16] rebase-interactive: use todo_list_write_to_file() in edit_todo_list() Alban Gruin
2018-11-09  8:08     ` [PATCH v3 12/16] rebase-interactive: append_todo_help() changes Alban Gruin
2018-11-09  8:08     ` [PATCH v3 13/16] rebase-interactive: rewrite edit_todo_list() to handle the initial edit Alban Gruin
2018-11-09  8:08     ` [PATCH v3 14/16] sequencer: use edit_todo_list() in complete_action() Alban Gruin
2018-11-09  8:08     ` [PATCH v3 15/16] sequencer: fix a call to error() in transform_todo_file() Alban Gruin
2018-11-09  8:08     ` [PATCH v3 16/16] rebase--interactive: move transform_todo_file() to rebase--interactive.c Alban Gruin

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=03475c29-5317-b105-6102-5cae3a5ae926@talktalk.net \
    --to=phillip.wood@talktalk.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=alban.gruin@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

git@vger.kernel.org mailing list mirror (one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/
       or Tor2web: https://www.tor2web.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox