All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Karthik Nayak <karthik.188@gmail.com>
Cc: git@vger.kernel.org, christian.couder@gmail.com,
	Matthieu.Moy@grenoble-inp.fr
Subject: Re: [PATCH v13 04/12] ref-filter: implement an `align` atom
Date: Mon, 24 Aug 2015 15:13:25 -0700	[thread overview]
Message-ID: <xmqqmvxg2v3u.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1440214788-1309-5-git-send-email-Karthik.188@gmail.com> (Karthik Nayak's message of "Sat, 22 Aug 2015 09:09:40 +0530")

Karthik Nayak <karthik.188@gmail.com> writes:

> +static void perform_quote_formatting(struct strbuf *s, const char *str, int quote_style);
> +
> +static void end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state)
> +{
> +	struct ref_formatting_stack *current = state->stack;
> +	struct strbuf s = STRBUF_INIT;
> +
> +	if (!current->at_end)
> +		die(_("format: `end` atom used without a supporting atom"));
> +	current->at_end(current);
> +	/*
> +	 * Whenever we have more than one stack element that means we
> +	 * are using a certain modifier atom. In that case we need to
> +	 * perform quote formatting.
> +	 */
> +	if (!state->stack->prev->prev) {

The comment and the condition seem to be saying opposite things.
The code says "If the stack only has one prev that is the very
initial one, then we do the quoting, i.e. the result of expanding
the enclosed string in %(start-something)...%(end) is quoted only
when that appears at the top level", which feels more correct than
the comment that says "if we are about to pop after seeing the
first %(end) in %(start)...%(another)...%(end)...%(end) sequence,
we quote what is between %(another)...%(end)".

> +		perform_quote_formatting(&s, current->output.buf, state->quote_style);
> +		strbuf_reset(&current->output);
> +		strbuf_addbuf(&current->output, &s);
> +	}
> +	strbuf_release(&s);
> +	pop_stack_element(&state->stack);
> +}
> +

> @@ -1228,29 +1315,33 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
>  	qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
>  }
>  
> -static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
> +static void perform_quote_formatting(struct strbuf *s, const char *str, int quote_style)
>  {
> -	struct strbuf *s = &state->stack->output;
> -
> -	switch (state->quote_style) {
> +	switch (quote_style) {
>  	case QUOTE_NONE:
> -		strbuf_addstr(s, v->s);
> +		strbuf_addstr(s, str);
>  		break;
>  	case QUOTE_SHELL:
> -		sq_quote_buf(s, v->s);
> +		sq_quote_buf(s, str);
>  		break;
>  	case QUOTE_PERL:
> -		perl_quote_buf(s, v->s);
> +		perl_quote_buf(s, str);
>  		break;
>  	case QUOTE_PYTHON:
> -		python_quote_buf(s, v->s);
> +		python_quote_buf(s, str);
>  		break;
>  	case QUOTE_TCL:
> -		tcl_quote_buf(s, v->s);
> +		tcl_quote_buf(s, str);
>  		break;
>  	}
>  }
>  
> +static void append_atom(struct atom_value *v, struct ref_formatting_state *state)
> +{
> +	struct strbuf *s = &state->stack->output;
> +	perform_quote_formatting(s, v->s, state->quote_style);

Hmmm, do we want to unconditionally do the quote here, or only when
we are not being captured by upcoming %(end) to be consistent with
the behaviour of end_atom_handler() above?

> @@ -1307,7 +1398,18 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
>  		if (cp < sp)
>  			append_literal(cp, sp, &state);
>  		get_ref_atom_value(info, parse_ref_filter_atom(sp + 2, ep), &atomv);
> -		append_atom(atomv, &state);
> +		/*
> +		 * If the atom is a modifier atom, then call the handler function.
> +		 * Else, if this is the first element on the stack, then we need to
> +		 * format the atom as per the given quote. Else we just add the atom value
> +		 * to the current stack element and handle quote formatting at the end.
> +		 */
> +		if (atomv->handler)
> +			atomv->handler(atomv, &state);
> +		else if (!state.stack->prev)
> +			append_atom(atomv, &state);
> +		else
> +			strbuf_addstr(&state.stack->output, atomv->s);

Ahh, this explains why you are not doing it above, but I do not
think if this is a good division of labor.

You can see that I expected that "if !state.stack->prev" check to be
inside append_atom(), and I would imagine future readers would have
the same expectation when reading this code.  I.e.

	append_atom(struct atom_value *v, struct ref_f_s *state)
        {
		if (state->stack->prev)
			strbuf_addstr(&state->stack->output, v->s);
		else
                	quote_format(&state->stack->output, v->s, state->quote_style);
	}

The end result may be the same, but I do think "append_atom is to
always quote, so we do an unquoted appending by hand" is a bad way
to do this.

Moreover, notice that the function signature of append_atom() is
exactly the same as atomv->handler's.  I wonder if it would be
easier to understand if you made append_atom() the handler for a
non-magic atoms, which would let you do the above without any if/else
and just a single unconditional

	atomv->handler(atomv, &state);

  reply	other threads:[~2015-08-24 22:13 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-22  3:39 [PATCH v13 00/12] port tag.c to use ref-filter APIs Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 01/12] ref-filter: move `struct atom_value` to ref-filter.c Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 02/12] ref-filter: introduce ref_formatting_state and ref_formatting_stack Karthik Nayak
2015-08-24 21:56   ` Junio C Hamano
2015-08-25 10:26     ` Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 03/12] utf8: add function to align a string into given strbuf Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 04/12] ref-filter: implement an `align` atom Karthik Nayak
2015-08-24 22:13   ` Junio C Hamano [this message]
2015-08-24 22:15     ` Junio C Hamano
2015-08-25 13:28       ` Karthik Nayak
2015-08-25  6:47     ` Matthieu Moy
2015-08-25 13:30       ` Karthik Nayak
2015-08-25 17:56         ` Junio C Hamano
2015-08-26  6:41           ` Karthik Nayak
2015-08-25 17:52       ` Junio C Hamano
2015-08-25 13:28     ` Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 05/12] ref-filter: add option to filter out tags, branches and remotes Karthik Nayak
2015-08-24 22:24   ` Junio C Hamano
2015-08-25 13:07     ` Karthik Nayak
2015-08-26 16:10   ` Michael Haggerty
2015-08-27 12:42     ` Karthik Nayak
2015-08-27 15:24       ` Michael Haggerty
2015-08-27 15:35         ` Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 06/12] ref-filter: support printing N lines from tag annotation Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 07/12] ref-filter: add support to sort by version Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 08/12] ref-filter: add option to match literal pattern Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 09/12] tag.c: use 'ref-filter' data structures Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 10/12] tag.c: use 'ref-filter' APIs Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 11/12] tag.c: implement '--format' option Karthik Nayak
2015-08-23 19:56   ` Matthieu Moy
2015-08-24 15:07     ` Karthik Nayak
2015-08-24 15:14       ` Matthieu Moy
2015-08-24 15:21         ` Karthik Nayak
2015-08-22  3:39 ` [PATCH v13 12/12] tag.c: implement '--merged' and '--no-merged' options Karthik Nayak
2015-08-23 20:00 ` [PATCH v13 00/12] port tag.c to use ref-filter APIs Matthieu Moy
2015-08-24 15:09   ` Karthik Nayak
2015-08-24 15:16     ` Matthieu Moy
2015-08-24 15:22       ` Karthik Nayak
2015-08-24 22:34   ` Junio C Hamano
2015-08-24 22:58     ` Junio C Hamano
2015-08-25 13:26       ` Karthik Nayak
2015-08-25 19:16         ` Junio C Hamano
2015-08-25 19:43           ` Junio C Hamano
2015-08-26  5:56           ` Karthik Nayak
2015-08-26 14:37             ` Junio C Hamano
2015-08-26 19:14               ` Karthik Nayak
2015-08-26 20:19                 ` Junio C Hamano
2015-08-27 18:00                   ` Karthik Nayak
2015-08-25 13:23     ` Karthik Nayak
2015-08-24 17:27 ` Junio C Hamano
2015-08-24 18:09   ` Karthik Nayak
2015-08-24 18:53     ` Junio C Hamano
2015-08-24 22:35       ` Junio C Hamano
2015-08-26 10:07         ` Karthik Nayak
2015-08-26 11:54           ` Matthieu Moy
2015-08-26 15:44             ` Junio C Hamano
2015-08-26 15:46               ` Junio C Hamano
2015-08-26 15:48                 ` Matthieu Moy
2015-08-26 19:11                   ` Karthik Nayak
2015-08-25 13:25       ` Karthik Nayak

Reply instructions:

You may reply publicly 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

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

  git send-email \
    --in-reply-to=xmqqmvxg2v3u.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=karthik.188@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.