parse-options: simplify positivation handling

We accept the positive version of options whose long name starts with "no-" and are defined without the flag PARSE_OPT_NONEG. E.g. git clone has an explicitly defined --no-checkout option and also implicitly accepts --checkout to override it. parse_long_opt() handles that by restarting the option matching with the positive version when it finds that only the current option definition starts with "no-", but not the user-supplied argument. This code is located almost at the end of the matching logic. Avoid the need for a restart by moving the code up. We don't have to check the positive arg against the negative long_name at all -- the "no-" prefix of the latter makes a match impossible. Skip it and toggle OPT_UNSET right away to simplify the control flow. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 21, 2024 at 18:56 UTC 457f96252fcfd64127f71e8663e2e4b6dfd4576c
1 file changed +6 -7
parse-options.c
+6 -7
@@ -369,7 +369,11 @@ static enum parse_opt_result parse_long_opt(
369 if (!long_name)
370 continue;
371
372 -again:
372 + if (!starts_with(arg, "no-") &&
373 + !(options->flags & PARSE_OPT_NONEG) &&
374 + skip_prefix(long_name, "no-", &long_name))
375 + opt_flags |= OPT_UNSET;
376 +
377 if (!skip_prefix(arg, long_name, &rest))
378 rest = NULL;
379 if (!rest) {
@@ -403,13 +407,8 @@ is_abbreviated:
407 goto is_abbreviated;
408 }
409 /* negated? */
406 - if (!starts_with(arg, "no-")) {
407 - if (skip_prefix(long_name, "no-", &long_name)) {
408 - opt_flags |= OPT_UNSET;
409 - goto again;
410 - }
410 + if (!starts_with(arg, "no-"))
411 continue;
412 - }
412 flags |= OPT_UNSET;
413 if (!skip_prefix(arg + 3, long_name, &rest)) {
414 /* abbreviated and negated? */