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

[PATCH 4/6] parse-options: detect ambiguous self-negation

From
René Scharfe <l.s.r@web.de>
Date
Feb 24, 2024, 21:29 UTC
Message-ID
<20240224212953.44026-5-l.s.r@web.de>
In-Reply-To
<20240224212953.44026-1-l.s.r@web.de>

Git currently does not detect the ambiguity of an option that starts with "no" like --notes and its negated form if given just --n or --no. All Git commands with such options have other negatable options, and we detect the ambiguity with them, so that's currently only a potential problem for scripts that use git rev-parse --parseopt.

Let's fix it nevertheless, as there's no need for that confusion. To detect the ambiguity we have to loosen the check in register_abbrev(), as an option is considered an alias of itself. Add non-matching negation flags as a criterion to recognize an option being ambiguous with its negated form.

And we need to keep going after finding a non-negated option as an
abbreviated candidate and perform the negation checks in the same
loop.
---
 parse-options.c               |  3 +--
 t/t1502-rev-parse-parseopt.sh | 11 +++++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/parse-options.c b/parse-options.c
index 398ebaef14..008c0f32cf 100644
--- a/parse-options.c
+++ b/parse-options.c
@@ -363,7 +363,7 @@ static void register_abbrev(struct parse_opt_ctx_t *p,
 	if (p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT)
 		return;
 	if (abbrev->option &&
-	    !is_alias(p, abbrev->option, option)) {
+	    !(abbrev->flags == flags && is_alias(p, abbrev->option, option))) {
 		/*
 		 * If this is abbreviated, it is
 		 * ambiguous. So when there is no
@@ -406,7 +406,6 @@ static enum parse_opt_result parse_long_opt(
 			if (!strncmp(long_name, arg, arg_end - arg)) {
 				register_abbrev(p, options, flags ^ opt_flags,
 						&abbrev, &ambiguous);
-				continue;
 			}
 			/* negation allowed? */
 			if (options->flags & PARSE_OPT_NONEG)
diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh
index f0737593c3..b754b9fd74 100755
--- a/t/t1502-rev-parse-parseopt.sh
+++ b/t/t1502-rev-parse-parseopt.sh
@@ -322,4 +322,15 @@ check_invalid_long_option optionspec-neg --no-positive-only
 check_invalid_long_option optionspec-neg --negative
 check_invalid_long_option optionspec-neg --no-no-negative

+test_expect_success 'ambiguous: --no matches both --noble and --no-noble' '
+	cat >spec <<-\EOF &&
+	some-command [options]
+	--
+	noble The feudal switch.
+	EOF
+	test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+	git rev-parse --parseopt -- <spec 2>err --no &&
+	grep "error: ambiguous option: no (could be --noble or --no-noble)" err
+'
+
 test_done
--
2.44.0
Previous: René ScharfeNext: René Scharfe
Message 8 of 23 in “parse-options: long option parsing fixes and cleanup”
  1. 0/6 parse-options: long option parsing fixes and cleanupRené Scharfe, Feb 24, 2024
  2. 2/6 parse-options: set arg of abbreviated option lazilyRené Scharfe, Feb 24, 2024
  3. 3/6 parse-options: factor out register_abbrev() and struct parsed_optionRené Scharfe, Feb 24, 2024
  4. Junio C HamanoFeb 24, 2024
  5. 1/6 parse-options: recognize abbreviated negated option with argRené Scharfe, Feb 24, 2024
  6. 6/6 parse-options: rearrange long_name matching codeRené Scharfe, Feb 24, 2024
  7. 5/6 parse-options: normalize arg and long_name before comparisonRené Scharfe, Feb 24, 2024
  8. 4/6 parse-options: detect ambiguous self-negationRené Scharfe, Feb 24, 2024
  9. René ScharfeFeb 24, 2024
  10. 0/6 parse-options: long option parsing fixes and cleanupRené Scharfe, Mar 3, 2024
  11. 6/6 parse-options: rearrange long_name matching codeRené Scharfe, Mar 3, 2024
  12. Josh SteadmonMar 12, 2024
  13. 5/6 parse-options: normalize arg and long_name before comparisonRené Scharfe, Mar 3, 2024
  14. Josh SteadmonMar 12, 2024
  15. 2/6 parse-options: set arg of abbreviated option lazilyRené Scharfe, Mar 3, 2024
  16. 1/6 parse-options: recognize abbreviated negated option with argRené Scharfe, Mar 3, 2024
  17. Josh SteadmonMar 12, 2024
  18. 4/6 parse-options: detect ambiguous self-negationRené Scharfe, Mar 3, 2024
  19. 3/6 parse-options: factor out register_abbrev() and struct parsed_optionRené Scharfe, Mar 3, 2024
  20. Josh SteadmonMar 12, 2024
  21. Junio C HamanoMar 13, 2024
  22. René ScharfeMar 13, 2024
  23. Josh SteadmonMar 12, 2024

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.