Skip to content

Commit becacd2

Browse files
committed
cat-file: avoid "too many arguments"
Running "git cat-file -e a b c d e f g" would fail and say "too many arguments". By reading that message, you cannot tell if the command could have worked if you limited the list of objects to 5 items instead of 7, or the command is prepared to take only a single item. Let's report that "b" is an unexpected argument instead. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb78702 commit becacd2

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

builtin/cat-file.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,10 +1071,13 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
10711071
else if (!argc && opt_epts)
10721072
usage_msg_optf(_("<object> required with '-%c'"),
10731073
usage, options, opt);
1074+
else if (!argc)
1075+
BUG("argc==0 with opt=%c", opt);
10741076
else if (argc == 1)
10751077
obj_name = argv[0];
10761078
else
1077-
usage_msg_opt(_("too many arguments"), usage, options);
1079+
usage_msg_optf(_("unexpected argument: '%s'"),
1080+
usage, options, argv[1]);
10781081
} else if (!argc) {
10791082
usage_with_options(usage, options);
10801083
} else if (argc != 2) {

t/t1006-cat-file.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,18 @@ do
6666
done
6767
done
6868

69-
test_too_many_arguments () {
69+
test_unexpected_arg () {
70+
unexpected=$1
71+
shift
7072
test_expect_code 129 "$@" 2>err &&
71-
grep -E "^fatal: too many arguments$" err
73+
grep -E "^fatal: unexpected argument: '$unexpected'" err
7274
}
7375

7476
for opt in $short_modes $cw_modes
7577
do
7678
args="one two three"
7779
test_expect_success "usage: too many arguments: $opt $args" '
78-
test_too_many_arguments git cat-file $opt $args
80+
test_unexpected_arg two git cat-file $opt $args
7981
'
8082

8183
for opt2 in --buffer --follow-symlinks

0 commit comments

Comments
 (0)