parse-options: rearrange long_name matching code

Move the code for handling a full match of long_name first and get rid of negations. Reduce the indent of the code for matching abbreviations and remove unnecessary curly braces. Combine the checks for whether negation is allowed and whether arg is "n", "no" or "no-" because they belong together and avoid a continue statement. The result is shorter, more readable code. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Mar 3, 2024 at 13:19 UTC 28a92478b825a4a2c7c2c0c6b725ce92cd0b83e0
1 file changed +15 -22
parse-options.c
+15 -22
@@ -413,30 +413,23 @@ static enum parse_opt_result parse_long_opt(
413 if (((flags ^ opt_flags) & OPT_UNSET) && !allow_unset)
414 continue;
415
416 - if (!skip_prefix(arg_start, long_name, &rest))
417 - rest = NULL;
418 - if (!rest) {
419 - /* abbreviated? */
420 - if (!strncmp(long_name, arg_start, arg_end - arg_start)) {
421 - register_abbrev(p, options, flags ^ opt_flags,
422 - &abbrev, &ambiguous);
423 - }
424 - /* negation allowed? */
425 - if (options->flags & PARSE_OPT_NONEG)
416 + if (skip_prefix(arg_start, long_name, &rest)) {
417 + if (*rest == '=')
418 + p->opt = rest + 1;
419 + else if (*rest)
420 continue;
427 - /* negated and abbreviated very much? */
428 - if (starts_with("no-", arg)) {
429 - register_abbrev(p, options, OPT_UNSET ^ opt_flags,
430 - &abbrev, &ambiguous);
431 - }
432 - continue;
421 + return get_value(p, options, flags ^ opt_flags);
422 }
434 - if (*rest) {
435 - if (*rest != '=')
436 - continue;
437 - p->opt = rest + 1;
438 - }
439 - return get_value(p, options, flags ^ opt_flags);
423 +
424 + /* abbreviated? */
425 + if (!strncmp(long_name, arg_start, arg_end - arg_start))
426 + register_abbrev(p, options, flags ^ opt_flags,
427 + &abbrev, &ambiguous);
428 +
429 + /* negated and abbreviated very much? */
430 + if (allow_unset && starts_with("no-", arg))
431 + register_abbrev(p, options, OPT_UNSET ^ opt_flags,
432 + &abbrev, &ambiguous);
433 }
434
435 if (disallow_abbreviated_options && (ambiguous.option || abbrev.option))