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

Re: [PATCH 2/2] branch, for-each-ref: add option to omit empty lines

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 30, 2023, 15:54 UTC
Message-ID
<xmqqilei1bgk.fsf@gitster.g>
In-Reply-To
<20230330112133.4437-3-oystwa@gmail.com>
Øystein Walle <oystwa@gmail.com> writes:
Show 12 quoted lines
> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
> index 6da899c629..0f4fa98b18 100644
> --- a/Documentation/git-for-each-ref.txt
> +++ b/Documentation/git-for-each-ref.txt
> @@ -93,6 +93,11 @@ OPTIONS
>  --ignore-case::
>  	Sorting and filtering refs are case insensitive.
>  
> +-n::
> +--omit-empty-lines::
> +	Do not print a newline after formatted refs where the format expands
> +	to the empty string.

While I can see the utility of the new feature, it is unclear if its merit is so clear that it deserves a short-and-sweet single letter option from the get go. Especially, don't we want to give this to "git branch" and "git tag" in their listing modes for consistency, but it means stealing "-n" also from them.

Show 5 quoted lines
> @@ -757,7 +761,13 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
>  	}
>  
>  	if (list)
> +	{
Move that opening brace at the end of the previous line, i.e.

- if (list) + if (list) {

> +		if (omit_empty_lines && !format.format) {
> +			error("--omit-empty-lines without --format does not make sense");
> +			usage_with_options(builtin_branch_usage, options);
> +		}

Does it not make sense? With the default format, it may happen that there will be no empty line so there is nothing to omit, but I do not see a strong reason to forbid the request like this.

Show 35 quoted lines
>  		setup_auto_pager("branch", 1);
> +	}
>  
>  	if (delete) {
>  		if (!argc)
> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index 6f62f40d12..349c4d4ef8 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -19,7 +19,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
>  	int i;
>  	struct ref_sorting *sorting;
>  	struct string_list sorting_options = STRING_LIST_INIT_DUP;
> -	int maxcount = 0, icase = 0;
> +	int maxcount = 0, icase = 0, omit_empty_lines = 0;
>  	struct ref_array array;
>  	struct ref_filter filter;
>  	struct ref_format format = REF_FORMAT_INIT;
> @@ -35,6 +35,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
>  			N_("quote placeholders suitably for python"), QUOTE_PYTHON),
>  		OPT_BIT(0 , "tcl",  &format.quote_style,
>  			N_("quote placeholders suitably for Tcl"), QUOTE_TCL),
> +		OPT_BOOL('n' , "omit-empty-lines",  &omit_empty_lines,
> +			N_("do not output a newline after empty formatted refs")),
>  
>  		OPT_GROUP(""),
>  		OPT_INTEGER( 0 , "count", &maxcount, N_("show only <n> matched refs")),
> @@ -55,8 +57,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
>  	memset(&array, 0, sizeof(array));
>  	memset(&filter, 0, sizeof(filter));
>  
> -	format.format = "%(objectname) %(objecttype)\t%(refname)";
> -
>  	git_config(git_default_config, NULL);
>  	parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);

This smells fishy. We establish the hardcoded built-in default, let the config machinery override, and then finally let command line options to further override. You may be able to reach the same end result by leaving the value unset, fill with the configured value, override with the command line, and then if the value is still unset, fall back to a hardcoded built-in default, but I do not see why such a change logically belongs to a patch to add "--omit-empty" feature.

Show 8 quoted lines
> @@ -68,6 +68,12 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
>  		error("more than one quoting style?");
>  		usage_with_options(for_each_ref_usage, opts);
>  	}
> +	if (omit_empty_lines && !format.format) {
> +		error("--omit-empty-lines without --format does not make sense");
> +		usage_with_options(for_each_ref_usage, opts);
> +	}
I wouldn't do this, for the same reason as for "git branch".
> +	if (!format.format)
> +		format.format = "%(objectname) %(objecttype)\t%(refname)";

This is the other half of the earlier change I called "fishy". It may be benign, but it is distracting, especially when done without explanation, in a change to add a feature that is not related.

Show 8 quoted lines
> @@ -88,7 +94,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
>  		if (format_ref_array_item(array.items[i], &format, &output, &err))
>  			die("%s", err.buf);
>  		fwrite(output.buf, 1, output.len, stdout);
> -		putchar('\n');
> +		if (!omit_empty_lines || output.len > 0)
> +			putchar('\n');
>  	}
OK, but two points.
 * do not compare output.len with 0; it is sufficient to just write
	if (!omit_empty || output.len)
 * now we care if output is empty anyway, perhaps we can optimize
   out fwrite() too, perhaps with something like
	if (output.len || !omit_empty)
		printf("%.*s\n", output.len, output.buf);
   perhaps?

I am not sure about the latter, but we tend to use "%.*s" liberally when we could use fwrite() in our codebase for brevity, so ...

Show 12 quoted lines
> +test_expect_success 'git branch --format --omit-empty-lines' '
> +	cat >expect <<-\EOF &&
> +	Refname is (HEAD detached from fromtag)
> +	Refname is refs/heads/ambiguous
> +	Refname is refs/heads/branch-one
> +	Refname is refs/heads/branch-two
> +	EOF
> +	echo >>expect &&
> +	cat >>expect <<-\EOF &&
> +	Refname is refs/heads/ref-to-branch
> +	Refname is refs/heads/ref-to-remote
> +	EOF

It is hard to see that there is an empty line expected when the expectation is prepared like this. Why not something like

	cat >expect.full <<-\EOF &&
	one
	two
	four (three is missing)
	EOF
	sed -e "/^$/d" expect.full >expect.stripped &&
	git branch $args >actual &&
	test_cmp expect.full actual &&
	git branch --omit-empty $args >actual &&
	test_cmp expect.stripped actual &&

that highlights the fact that there is a missing line for one expectation, and that the only difference in two expectations is the lack of empty line(s)?

Show 11 quoted lines
> diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
> index c466fd989f..eec9d45513 100755
> --- a/t/t6300-for-each-ref.sh
> +++ b/t/t6300-for-each-ref.sh
> @@ -1374,6 +1374,14 @@ test_expect_success 'for-each-ref --ignore-case ignores case' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'for-each-ref --omit-empty-lines works' '
> +	git for-each-ref --format="%(refname)" > actual &&
> +	test_line_count -gt 1 actual &&

The next test depends on that a branch 'main' exists, so perhaps that should be tested here, at least? And then if there is no other branches and tags, we cannot tell if seeing only the 'main' branch is due to --omit-empty correctly working, or due to the repository having only that branch, so it is also good to check if there is branches or tags other than 'main' in the output.

> +	git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty-lines > actual &&
> +	echo refs/heads/main > expect &&
> +	test_cmp expect actual
> +'

By the way, lose SP between redirection operator '>' and its target, i.e. write them like so:

	echo refs/heads/main >expect

This feature makes %(if)...%(else)...%(end) construct complete and is a very good addition.

Thanks for working on it.
Previous: Øystein WalleNext: Jeff King
Message 18 of 30 in “branch, for-each-ref: add option to omit empty lines”
  1. 0/2 branch, for-each-ref: add option to omit empty linesØystein Walle, Mar 30, 2023
  2. 1/2 ref-filter: remove unused ref_format memberØystein Walle, Mar 30, 2023
  3. Junio C HamanoMar 30, 2023
  4. Junio C HamanoMar 30, 2023
  5. Øystein WalleMar 31, 2023
  6. ZheNing HuMar 31, 2023
  7. Junio C HamanoMar 31, 2023
  8. 0/2 branch, for-each-ref: add option to omit empty linesØystein Walle, Apr 6, 2023
  9. 1/2 ref-filter: remove unused ref_format memberØystein Walle, Apr 6, 2023
  10. 2/2 branch, for-each-ref: add option to omit empty linesØystein Walle, Apr 6, 2023
  11. Junio C HamanoApr 6, 2023
  12. branch, for-each-ref, tag: add option to omit empty linesØystein Walle, Apr 7, 2023
  13. Junio C HamanoApr 7, 2023
  14. Andrei RybakApr 12, 2023
  15. Øystein WalleApr 13, 2023
  16. Junio C HamanoApr 13, 2023
  17. 2/2 branch, for-each-ref: add option to omit empty linesØystein Walle, Mar 30, 2023
  18. Junio C HamanoMar 30, 2023
  19. Jeff KingMar 30, 2023
  20. Junio C HamanoMar 30, 2023
  21. Øystein WalleMar 31, 2023
  22. Junio C HamanoMar 31, 2023
  23. Junio C HamanoMar 30, 2023
  24. Phillip WoodMar 31, 2023
  25. Junio C HamanoMar 31, 2023
  26. Øystein WalleApr 6, 2023
  27. Jeff KingApr 6, 2023
  28. Junio C HamanoApr 6, 2023
  29. Jeff KingApr 10, 2023
  30. Junio C HamanoApr 6, 2023

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.