git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC PATCH 0/1] for-each-ref: do not output empty lines
@ 2019-09-10  5:17 Eric Freese
  2019-09-10  5:17 ` [RFC PATCH 1/1] " Eric Freese
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Freese @ 2019-09-10  5:17 UTC (permalink / raw)
  To: git; +Cc: Eric Freese

Hi,

This is a follow-up patch based on conversation from the thread: "[RFC
PATCH 1/1] for-each-ref: add '--no-symbolic' option"

I had proposed a `--no-symbolic` option to git-for-each-ref that would
filter out symbolic refs from the output. It was discovered that the
behavior was already possible using an `%(if)` atom, but with the small
caveat that empty lines would be output for each non-symbolic ref.

This patch changes the output behavior of git-for-each-ref such that it
only prints lines for refs for which the format string expands to a
non-empty string.

If this is deemed to be too disruptive of a change, a new command option
could be added to opt in to the new behavior.

Cheers

Eric Freese (1):
  for-each-ref: do not output empty lines

 ref-filter.c            | 3 ++-
 t/t6300-for-each-ref.sh | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

-- 
2.23.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 1/1] for-each-ref: do not output empty lines
  2019-09-10  5:17 [RFC PATCH 0/1] for-each-ref: do not output empty lines Eric Freese
@ 2019-09-10  5:17 ` Eric Freese
  2019-09-10  6:02   ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Freese @ 2019-09-10  5:17 UTC (permalink / raw)
  To: git; +Cc: Eric Freese

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.

For example, to exclude symbolic refs, use format string:
  "%(if)%(symref)%(then)%(else)%(refname)%(end)"

Or to include only symbolic refs, use:
  "%(if)%(symref)%(then)%(refname)%(end)"

Signed-off-by: Eric Freese <ericdfreese@gmail.com>
---
 ref-filter.c            | 3 ++-
 t/t6300-for-each-ref.sh | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index 7338cfc671..b5b3c4ddf6 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -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');
 	strbuf_release(&error_buf);
 	strbuf_release(&final_buf);
-	putchar('\n');
 }
 
 void pretty_print_ref(const char *name, const struct object_id *oid,
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index 9c910ce746..1f070e63e2 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -42,7 +42,7 @@ test_atom() {
 		 sym) ref=refs/heads/sym ;;
 		   *) ref=$1 ;;
 	esac
-	printf '%s\n' "$3" >expected
+	{ test -n "$3" && printf '%s\n' "$3"; } >expected
 	test_expect_${4:-success} $PREREQ "basic atom: $1 $2" "
 		git for-each-ref --format='%($2)' $ref >actual &&
 		sanitize_pgp <actual >actual.clean &&
@@ -313,7 +313,7 @@ test_expect_success 'Check format of strftime-local date fields' '
 '
 
 test_expect_success 'exercise strftime with odd fields' '
-	echo >expected &&
+	>expected &&
 	git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
 	test_cmp expected actual &&
 	long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] for-each-ref: do not output empty lines
  2019-09-10  5:17 ` [RFC PATCH 1/1] " Eric Freese
@ 2019-09-10  6:02   ` Junio C Hamano
  2019-09-10  6:14     ` Junio C Hamano
  2019-09-10 16:35     ` Jeff King
  0 siblings, 2 replies; 5+ messages in thread
From: Junio C Hamano @ 2019-09-10  6:02 UTC (permalink / raw)
  To: Eric Freese; +Cc: git

Eric Freese <ericdfreese@gmail.com> writes:

> 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.


> @@ -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).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] for-each-ref: do not output empty lines
  2019-09-10  6:02   ` Junio C Hamano
@ 2019-09-10  6:14     ` Junio C Hamano
  2019-09-10 16:35     ` Jeff King
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2019-09-10  6:14 UTC (permalink / raw)
  To: Eric Freese; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

>>  	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) {

Of course, that's a typo for "||"; if it is not empty, we'd emit no
matter what, and if omit_empty is not given, we'd emit whether it is
empty or not.

> 		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).

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH 1/1] for-each-ref: do not output empty lines
  2019-09-10  6:02   ` Junio C Hamano
  2019-09-10  6:14     ` Junio C Hamano
@ 2019-09-10 16:35     ` Jeff King
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff King @ 2019-09-10 16:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Eric Freese, git

On Mon, Sep 09, 2019 at 11:02:07PM -0700, Junio C Hamano wrote:

> Eric Freese <ericdfreese@gmail.com> writes:
> 
> > 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.

Your proposed commit message (and the suggested code below) have a
command-line option, but Eric's patch does it by default. Which do we
want? :)

I'm inclined to say that this new behavior should be the default; even
though it's technically a compatibility change, I find it hard to
imagine people would see it as a regression.

But we could also introduce "--omit-empty-lines", and then flip the
default. That gives an escape hatch of "--no-omit-empty-lines", at the
minor cost of having to carry an option that we assume nobody would ever
use.

-Peff

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-09-10 16:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-10  5:17 [RFC PATCH 0/1] for-each-ref: do not output empty lines Eric Freese
2019-09-10  5:17 ` [RFC PATCH 1/1] " Eric Freese
2019-09-10  6:02   ` Junio C Hamano
2019-09-10  6:14     ` Junio C Hamano
2019-09-10 16:35     ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).