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

Re: [PATCH v11 2/4] parse-options.c: make OPTION_COUNTUP respect "unspecified" values

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 31, 2016, 18:41 UTC
Message-ID
<xmqq60w2o5g9.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<01020153cd2341e2-2616ba0b-9271-4a97-87d1-b6dedbf31828-000000@eu-west-1.amazonses.com>
Pranit Bauva <pranit.bauva@gmail.com> writes:
Show 6 quoted lines
> This change will not affect existing users of COUNTUP at all, as long as
> they use the initial value of 0 (or more).
>
> Force uses the "unspecified" value in conjunction with OPT__FORCE in
> builtin/clean.c in a different way to handle its config which
> munges the "-1" into 0 before we get to parse_options.

These two paragraphs leave the readers wondering what the conclusion is. "it is OK as long as..." makes us wonder "so are there users that do not satisfy that condition?" "in a different way" makes us wonder "Does this change break builtin/clean.c because COUNTUP is used in a different way?".

	This change does not affect existing users of COUNTUP,
	because they all use the initial value of 0 (or more).
	Note that builtin/clean.c initializes the variable used with
	OPT__FORCE (which uses OPT_COUNTUP()) to a negative value,
	but it is set to either 0 or 1 by reading the configuration
	before the code calls parse_options(), i.e. as far as
	parse_options() is concerned, the initial value of the
	variable is not negative.
perhaps?
Show 32 quoted lines
>  `OPT_COUNTUP(short, long, &int_var, description)`::
>  	Introduce a count-up option.
> -	`int_var` is incremented on each use of `--option`, and
> -	reset to zero with `--no-option`.
> +	Each use of `--option` increments `int_var`, starting from zero
> +	(even if initially negative), and `--no-option` resets it to
> +	zero. To determine if `--option` or `--no-option` was set at
> +	all, set `int_var` to a negative value, and if it is still
> +	negative after parse_options(), then neither `--option` nor
> +	`--no-option` was seen.
>  
>  `OPT_BIT(short, long, &int_var, description, mask)`::
>  	Introduce a boolean option.
> diff --git a/parse-options.c b/parse-options.c
> index 47a9192..86d30bd 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -110,7 +110,13 @@ static int get_value(struct parse_opt_ctx_t *p,
>  		return 0;
>  
>  	case OPTION_COUNTUP:
> -		*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
> +		if (unset)
> +			*(int *)opt->value = 0;
> +		else {
> +			if (*(int *)opt->value < 0)
> +				*(int *)opt->value = 0;
> +			*(int *)opt->value += 1;
> +		}
>  		return 0;
>  
>  	case OPTION_SET_INT:

The new behaviour given by the patch looks very sensible, but I wonder if this is easier to reason about:

	case OPTION_COUNTUP:
+		if (*(int *)opt->value < 0)
+			*(int *)opt->value = 0;
		*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;

That is, upon hitting this case arm, we know that an explicit option was given from the command line, so the "unspecified" initial value, if it is still there, gets reset to 0, and after doing that, we just do the usual thing.

Previous: Pranit BauvaNext: Pranit Bauva
Message 25 of 35 in “parse-options.c: make OPTION__COUNTUP understand "unspecified" values”
  1. 1/3 parse-options.c: make OPTION__COUNTUP understand "unspecified" valuesPranit Bauva, Mar 26, 2016
  2. 3/3 commit: add a commit.verbose config variablePranit Bauva, Mar 26, 2016
  3. Eric SunshineMar 27, 2016
  4. Pranit BauvaMar 27, 2016
  5. Eric SunshineMar 27, 2016
  6. Pranit BauvaMar 27, 2016
  7. SZEDER GáborMar 27, 2016
  8. Pranit BauvaMar 27, 2016
  9. SZEDER GáborMar 27, 2016
  10. Pranit BauvaMar 27, 2016
  11. Eric SunshineMar 27, 2016
  12. 2/3 t7507-commit-verbose: store output of grep in a filePranit Bauva, Mar 26, 2016
  13. Eric SunshineMar 27, 2016
  14. SZEDER GáborMar 27, 2016
  15. Pranit BauvaMar 27, 2016
  16. Eric SunshineMar 27, 2016
  17. Pranit BauvaMar 27, 2016
  18. Eric SunshineMar 27, 2016
  19. Pranit BauvaMar 27, 2016
  20. 1/4 test-parse-options: print quiet as integerPranit Bauva, Mar 31, 2016
  21. 3/4 t7507-commit-verbose: improve test coverage by testing number of diffsPranit Bauva, Mar 31, 2016
  22. Junio C HamanoMar 31, 2016
  23. Pranit BauvaMar 31, 2016
  24. 2/4 parse-options.c: make OPTION_COUNTUP respect "unspecified" valuesPranit Bauva, Mar 31, 2016
  25. Junio C HamanoMar 31, 2016
  26. Pranit BauvaMar 31, 2016
  27. Junio C HamanoMar 31, 2016
  28. Pranit BauvaMar 31, 2016
  29. Junio C HamanoMar 31, 2016
  30. 4/4 commit: add a commit.verbose config variablePranit Bauva, Mar 31, 2016
  31. Junio C HamanoMar 31, 2016
  32. Pranit BauvaMar 31, 2016
  33. Eric SunshineMar 27, 2016
  34. Eric SunshineMar 27, 2016
  35. Eric SunshineMar 27, 2016

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.