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

Re: [PATCH 2/2] builtin/show-ref.c: limit output with `--count`

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Jun 7, 2022, 08:07 UTC
Message-ID
<220607.86r140vqc4.gmgdl@evledraar.gmail.com>
In-Reply-To
<3fcf1f555715e925385d37712ffe880bb869741e.1654552560.git.me@ttaylorr.com>
On Mon, Jun 06 2022, Taylor Blau wrote:
Show 10 quoted lines
> diff --git a/Documentation/git-show-ref.txt b/Documentation/git-show-ref.txt
> index ab4d271925..28256c04dd 100644
> --- a/Documentation/git-show-ref.txt
> +++ b/Documentation/git-show-ref.txt
> @@ -10,7 +10,7 @@ SYNOPSIS
>  [verse]
>  'git show-ref' [-q|--quiet] [--verify] [--head] [-d|--dereference]
>  	     [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags]
> -	     [--heads] [--] [<pattern>...]
> +	     [--heads] [--count=<n>] [--] [<pattern>...]

In addition to what Junio noted, the SYNOPSIS is now inaccurate per your documentation. I.e. if this option is incompatible with --verify and --exclude-existing we should use "|" to indicate that, e.g.:

	[ [--verify] [--exclude-existing] | --count=<n> ]
Show 16 quoted lines
> +	if (max_count) {
> +		int compatible = 0;
> +
> +		if (max_count < 0)
> +			error(_("invalid --count argument: (`%d' < 0)"),
> +			      max_count);
> +		else if (verify)
> +			error(_("--count is incompatible with %s"), "--verify");
> +		else if (exclude_arg)
> +			error(_("--count is incompatible with %s"),
> +			      "--exclude-existing");
> +		else
> +			compatible = 1;
> +
> +		if (!compatible)
> +			usage_with_options(show_ref_usage, show_ref_options);
Instead of this "int compatible" and if/else-if" just use usage_msg_optf().

That or die_for_incompatible_opt4(), at least the new _() messages should make use of the same translations. I.e. we recently made these parameterized.

Show 21 quoted lines
> +	}
> +
>  	if (exclude_arg)
>  		return exclude_existing(exclude_existing_arg);
>  
> diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh
> index 9252a581ab..b79e114c1e 100755
> --- a/t/t1403-show-ref.sh
> +++ b/t/t1403-show-ref.sh
> @@ -196,4 +196,25 @@ test_expect_success 'show-ref --verify with dangling ref' '
>  	)
>  '
>  
> +test_expect_success 'show-ref --count limits relevant output' '
> +	git show-ref --heads --count=1 >out &&
> +	test_line_count = 1 out
> +'
> +
> +test_expect_success 'show-ref --count rejects invalid input' '
> +	test_must_fail git show-ref --count=-1 2>err &&
> +	grep "invalid ..count argument: (.-1. < 0)" err
The use of .. here seems odd...
Show 5 quoted lines
> +'
> +
> +test_expect_success 'show-ref --count incompatible with --verify' '
> +	test_must_fail git show-ref --count=1 --verify HEAD 2>err &&
> +	grep "..count is incompatible with ..verify" err

...i.e. this looks like a way to avoid "--" at the beginning, but then why use it in the middle of the regex?

> +test_expect_success 'show-ref --count incompatible with --exclude-existing' '
> +	echo "refs/heads/main" >in &&
> +	test_must_fail git show-ref --count=1 --exclude-existing <in 2>err &&
> +	grep "..count is incompatible with ..exclude.existing" err

Seems like you could avoid it entirely by escaping it, e.g. "[-]-" at the beginning, or in this case I think "test_expect_code 129" would be more than sufficient, we use that to test "had usage spewed at us" elsewhere.

Previous: Junio C HamanoNext: Taylor Blau
Message 5 of 14 in “builtin/show-ref.c: support `--count` for limiting output”
  1. 0/2 builtin/show-ref.c: support `--count` for limiting outputTaylor Blau, Jun 6, 2022
  2. 1/2 builtin/show-ref.c: rename `found_match` to `matches_nr`Taylor Blau, Jun 6, 2022
  3. 2/2 builtin/show-ref.c: limit output with `--count`Taylor Blau, Jun 6, 2022
  4. Junio C HamanoJun 6, 2022
  5. Ævar Arnfjörð BjarmasonJun 7, 2022
  6. Taylor BlauJun 7, 2022
  7. Ævar Arnfjörð BjarmasonJun 7, 2022
  8. Junio C HamanoJun 8, 2022
  9. Junio C HamanoJun 6, 2022
  10. Taylor BlauJun 6, 2022
  11. Junio C HamanoJun 6, 2022
  12. Derrick StoleeJun 7, 2022
  13. Ævar Arnfjörð BjarmasonJun 7, 2022
  14. Taylor BlauJun 7, 2022

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.