Skip to content

Commit 5825268

Browse files
rscharfegitster
authored andcommitted
parse-options: fully disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN
baa4adc (parse-options: disable option abbreviation with PARSE_OPT_KEEP_UNKNOWN, 2019-01-27) turned off support for abbreviated options when the flag PARSE_OPT_KEEP_UNKNOWN is given, as any shortened option could also be an abbreviation for one of the unknown options. The code for handling abbreviated options is guarded by an if, but it can also be reached via goto. baa4adc only blocked the first way. Add the condition to the other ones as well. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 5825268

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

parse-options.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ static enum parse_opt_result parse_long_opt(
358358
const char *arg_end = strchrnul(arg, '=');
359359
const struct option *abbrev_option = NULL, *ambiguous_option = NULL;
360360
enum opt_parsed abbrev_flags = OPT_LONG, ambiguous_flags = OPT_LONG;
361+
int allow_abbrev = !(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT);
361362

362363
for (; options->type != OPTION_END; options++) {
363364
const char *rest, *long_name = options->long_name;
@@ -373,7 +374,7 @@ static enum parse_opt_result parse_long_opt(
373374
rest = NULL;
374375
if (!rest) {
375376
/* abbreviated? */
376-
if (!(p->flags & PARSE_OPT_KEEP_UNKNOWN_OPT) &&
377+
if (allow_abbrev &&
377378
!strncmp(long_name, arg, arg_end - arg)) {
378379
is_abbreviated:
379380
if (abbrev_option &&
@@ -397,7 +398,7 @@ static enum parse_opt_result parse_long_opt(
397398
if (options->flags & PARSE_OPT_NONEG)
398399
continue;
399400
/* negated and abbreviated very much? */
400-
if (starts_with("no-", arg)) {
401+
if (allow_abbrev && starts_with("no-", arg)) {
401402
flags |= OPT_UNSET;
402403
goto is_abbreviated;
403404
}
@@ -412,7 +413,8 @@ static enum parse_opt_result parse_long_opt(
412413
flags |= OPT_UNSET;
413414
if (!skip_prefix(arg + 3, long_name, &rest)) {
414415
/* abbreviated and negated? */
415-
if (starts_with(long_name, arg + 3))
416+
if (allow_abbrev &&
417+
starts_with(long_name, arg + 3))
416418
goto is_abbreviated;
417419
else
418420
continue;

t/t4013-diff-various.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,4 +666,10 @@ test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
666666
check_prefix actual a/file0 b/file0
667667
'
668668

669+
test_expect_success 'diff --no-renames cannot be abbreviated' '
670+
test_expect_code 129 git diff --no-rename >actual 2>error &&
671+
test_must_be_empty actual &&
672+
grep "invalid option: --no-rename" error
673+
'
674+
669675
test_done

0 commit comments

Comments
 (0)