Skip to content

Commit f221861

Browse files
drafnelgitster
authored andcommitted
t1502: demonstrate rev-parse --parseopt option mis-parsing
Since commit 2d893df rev-parse will scan forward from the beginning of the option string looking for a flag character. If there are no flag characters then the scan will spill over into the help text and will interpret the characters preceding the "flag" as part of the option-spec i.e. the long option name. For example, the following option spec: exclame this does something! will produce this 'set' expression when --exclame is specified: set -- --exclame this does something -- which will be interpreted as four separate parameters by the shell. And will produce a help string that looks like: --exclame this does something this does something! git-rebase.sh has such an option (--autosquash), and so will add extra parameters to the 'set' expression when --autosquash is used. git-rebase continues to work correctly though because when it parses the arguments, it ignores ones that it does not recognize. Also, rev-parse --parseopt does not currently interpret a tab character as a delimiter between the option spec and the help text. If a tab is used at the end of the option spec, before the help text, and before a space has been specified, then rev-parse will interpret the tab as part of the preceding component (either the long name or the arg hint). For example, the following option spec (note: there is a <tab> between "frotz" and "enable"): frotz enable frotzing will produce this 'set' expression when --frotz is specified: set -- --frotz enable -- which will be interpreted as 2 separate arguments by the shell. git-rebase.sh has one of these too (--keep-empty). In this case the tab is immediately followed by spaces so there are no additional parameters produced on the command line. The only side-effect is misalignment in the help text. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94c9fd2 commit f221861

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

t/t1502-rev-parse-parseopt.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ test_expect_success 'setup optionspec' '
2828
|g,fluf?path short and long option optional argument
2929
|longest=very-long-argument-hint a very long argument hint
3030
|pair=key=value with an equals sign in the hint
31+
|aswitch help te=t contains? fl*g characters!`
32+
|bswitch=hint hint has trailing tab character
33+
|cswitch switch has trailing tab character
3134
|short-hint=a with a one symbol hint
3235
|
3336
|Extras
3437
|extra1 line above used to cause a segfault but no longer does
3538
EOF
3639
'
3740

38-
test_expect_success 'test --parseopt help output' '
41+
test_expect_failure 'test --parseopt help output' '
3942
sed -e "s/^|//" >expect <<\END_EXPECT &&
4043
|cat <<\EOF
4144
|usage: some-command [options] <args>...
@@ -62,6 +65,9 @@ test_expect_success 'test --parseopt help output' '
6265
| --longest <very-long-argument-hint>
6366
| a very long argument hint
6467
| --pair <key=value> with an equals sign in the hint
68+
| --aswitch help te=t contains? fl*g characters!`
69+
| --bswitch <hint> hint has trailing tab character
70+
| --cswitch switch has trailing tab character
6571
| --short-hint <a> with a one symbol hint
6672
|
6773
|Extras
@@ -75,17 +81,17 @@ END_EXPECT
7581

7682
test_expect_success 'setup expect.1' "
7783
cat > expect <<EOF
78-
set -- --foo --bar 'ham' -b -- 'arg'
84+
set -- --foo --bar 'ham' -b --aswitch -- 'arg'
7985
EOF
8086
"
8187

82-
test_expect_success 'test --parseopt' '
83-
git rev-parse --parseopt -- --foo --bar=ham --baz arg < optionspec > output &&
88+
test_expect_failure 'test --parseopt' '
89+
git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
8490
test_cmp expect output
8591
'
8692

87-
test_expect_success 'test --parseopt with mixed options and arguments' '
88-
git rev-parse --parseopt -- --foo arg --bar=ham --baz < optionspec > output &&
93+
test_expect_failure 'test --parseopt with mixed options and arguments' '
94+
git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
8995
test_cmp expect output
9096
'
9197

0 commit comments

Comments
 (0)