The reason to make it understand negative values or more specifically
"unspecified" values is to give the ability to differentiate whether
`--option` or `--no-option` was specified at all.

Many uses of COUNTUP have now been replaced with BOOL and what remains
are verbose/quiet/force. This change will not affect existing users of
COUNTUP at all, as long as they use the initial value of 0 (or more), as
there is no mechanism to decrement. The only thing the command line can
do is to reset it to zero with "--no-foo".

Verbose or quiet don't use negative values before this commit but force
uses it in a different way to handle its config and then munges the "-1"
into 0 before we get to parse_options.

There are uses of COUNTUP in cmd_hash_object() and test-parse-options.c
and they are both fine.

Helped-by: Jeff King <p...@peff.net>
Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Helped-by: Junio C Hamano <gits...@pobox.com>
Signed-off-by: Pranit Bauva <pranit.ba...@gmail.com>

---
The discussion about this patch:
[1] : http://thread.gmane.org/gmane.comp.version-control.git/289027

Previous version of the patch:
[v1] : http://thread.gmane.org/gmane.comp.version-control.git/289061

Changes introduced:
Use a different language in commit message to make the change and its
utility more clear. Also added some points as to where this patch could
break but it doesn't.
---
 Documentation/technical/api-parse-options.txt | 8 ++++++--
 parse-options.c                               | 8 +++++++-
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Documentation/technical/api-parse-options.txt 
b/Documentation/technical/api-parse-options.txt
index 5f0757d..8908bf7 100644
--- a/Documentation/technical/api-parse-options.txt
+++ b/Documentation/technical/api-parse-options.txt
@@ -144,8 +144,12 @@ There are some macros to easily define options:
 
 `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:

--
https://github.com/git/git/pull/218
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to