Skip to content

Commit 33e7512

Browse files
drafnelgitster
authored andcommitted
rev-parse parseopt: interpret any whitespace as start of help text
Currently, rev-parse only interprets a space ' ' character as the delimiter between the option spec and the help text. So if a tab character is placed between the option spec and the help text, it will be interpreted as part of the long option name or as part of the arg hint. If it is interpreted as part of the long option name, then rev-parse will produce what will be interpreted as multiple arguments on the command line. For example, the following option spec (note: there is a <tab> between "frotz" and "enable"): frotz enable frotzing will produce the following set expression when --frotz is used: set -- --frotz -- instead of this: set -- --frotz enable -- Mark t1502.2 as fixed. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28a8d0f commit 33e7512

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

builtin/rev-parse.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,14 @@ static const char *skipspaces(const char *s)
387387
return s;
388388
}
389389

390+
static char *findspace(const char *s)
391+
{
392+
for (; *s; s++)
393+
if (isspace(*s))
394+
return (char*)s;
395+
return NULL;
396+
}
397+
390398
static int cmd_parseopt(int argc, const char **argv, const char *prefix)
391399
{
392400
static int keep_dashdash = 0, stop_at_non_option = 0;
@@ -444,8 +452,8 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
444452
memset(opts + onb, 0, sizeof(opts[onb]));
445453

446454
o = &opts[onb++];
447-
help = strchr(sb.buf, ' ');
448-
if (!help || *sb.buf == ' ') {
455+
help = findspace(sb.buf);
456+
if (!help || sb.buf == help) {
449457
o->type = OPTION_GROUP;
450458
o->help = xstrdup(skipspaces(sb.buf));
451459
continue;

t/t1502-rev-parse-parseopt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test_expect_success 'setup optionspec' '
3838
EOF
3939
'
4040

41-
test_expect_failure 'test --parseopt help output' '
41+
test_expect_success 'test --parseopt help output' '
4242
sed -e "s/^|//" >expect <<\END_EXPECT &&
4343
|cat <<\EOF
4444
|usage: some-command [options] <args>...

0 commit comments

Comments
 (0)