Skip to content

Commit 28a8d0f

Browse files
drafnelgitster
authored andcommitted
rev-parse parseopt: do not search help text for flag chars
When searching for flag characters in the option spec, we should ensure the search stays within the bounds of the option spec and does not enter the help text portion of the spec. So when we find the boundary white space marking the start of the help text, let's mark it with a nul character. Then when we search for flag characters starting from the beginning of the string we'll stop at the nul and won't enter the help text. Now, the following option spec: exclame this does something! will produce this 'set' expression when --exclame is specified: set -- --exclame -- instead of this one: set -- --exclame this does something -- Mark t1502.4 and t1502.5 as fixed. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f221861 commit 28a8d0f

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

builtin/rev-parse.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
434434
/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
435435
while (strbuf_getline(&sb, stdin) != EOF) {
436436
const char *s;
437-
const char *help;
437+
char *help;
438438
struct option *o;
439439

440440
if (!sb.len)
@@ -451,8 +451,10 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
451451
continue;
452452
}
453453

454+
*help = '\0';
455+
454456
o->type = OPTION_CALLBACK;
455-
o->help = xstrdup(skipspaces(help));
457+
o->help = xstrdup(skipspaces(help+1));
456458
o->value = &parsed;
457459
o->flags = PARSE_OPT_NOARG;
458460
o->callback = &parseopt_dump;

t/t1502-rev-parse-parseopt.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ set -- --foo --bar 'ham' -b --aswitch -- 'arg'
8585
EOF
8686
"
8787

88-
test_expect_failure 'test --parseopt' '
88+
test_expect_success 'test --parseopt' '
8989
git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
9090
test_cmp expect output
9191
'
9292

93-
test_expect_failure 'test --parseopt with mixed options and arguments' '
93+
test_expect_success 'test --parseopt with mixed options and arguments' '
9494
git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
9595
test_cmp expect output
9696
'

0 commit comments

Comments
 (0)