git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC PATCH 1/1] for-each-ref: do not output empty lines

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 10, 2019, 06:02 UTC
Message-ID
<xmqqpnk8heow.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20190910051705.2765-2-ericdfreese@gmail.com>
Eric Freese <ericdfreese@gmail.com> writes:
Show 5 quoted lines
> If the format string expands to an empty string for a given ref, do not
> print the empty line.
>
> This is helpful when wanting to print only certain kinds of refs that
> you can't already filter for.

We tend to prefer stating the reason why we want to do so first and then give a command to the codebase to "become like so". Here is to illustrate how you would do it:

    The custom format specifier "--format=<format>" can be used to
    tell the for-each-ref command to say nothing for certain kind of
    refs, e.g.
       --format="%(if)%(symref)%(then)%(else)%(refname)%(end)"
    may be used to show the refname only for refs that are not
    symbolic refs.  Except that the command still would show one
    blank line per each symbolic ref, which is fairly useless.
    Introduce the `--omit-empty-lines` option to squelch these
    useless lines from the output.
Show 6 quoted lines
> @@ -2395,9 +2395,10 @@ void show_ref_array_item(struct ref_array_item *info,
>  	if (format_ref_array_item(info, format, &final_buf, &error_buf))
>  		die("%s", error_buf.buf);
>  	fwrite(final_buf.buf, 1, final_buf.len, stdout);
> +	if (final_buf.len)
> +		putchar('\n');

While we are introducing a conditional, let's drop the useless fwrite of 0-byte while we are at it [*1*], i.e.

	if (final_buf.len && !omit_empty_lines) {
		fwrite(final_buf.buf, 1, final_buf.len, stdout);
		putchar('\n');
	}
Thanks.
[Footnote]
*1* "While we are at it", the existing code tempts me to drop fwrite
    and replace it with something along the lines of...
	printf("%*s\n", count, buf)
    but I refrained from doing so.  An enhancement patch like this
    is not a place to "improve" existing code (which should be done
    as a separate patch).
Previous: Eric FreeseNext: Junio C Hamano
Message 3 of 5 in “for-each-ref: do not output empty lines”
  1. 0/1 for-each-ref: do not output empty linesEric Freese, Sep 10, 2019
  2. 1/1 for-each-ref: do not output empty linesEric Freese, Sep 10, 2019
  3. Junio C HamanoSep 10, 2019
  4. Junio C HamanoSep 10, 2019
  5. Jeff KingSep 10, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.