revision: avoid reporting known options as unknown on error

In parse_revision_opt(), we report "unknown option" when
handle_revision_opt() returns a negative value or zero. But these are
two different conditions: a negative value indicates a malformed option
(like a missing argument) and zero indicates an unknown option.

So malformed options produce nonsense like this:

  $ git shortlog --default
  error: bad --default argument
  error: unknown option `--default'
  usage: [...]

We should treat a negative return as a logic error which has already
been reported by handle_revision_opt(), and just show the regular usage
message.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Jeff King 2026-09-25 16:35:48 -04:00 committed by Junio C Hamano
parent 9a2fb147f2
commit 253118d1d9
2 changed files with 10 additions and 1 deletions

View File

@ -2734,7 +2734,10 @@ void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
{
int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
&ctx->cpidx, ctx->out, NULL);
if (n <= 0) {
if (n < 0) {
/* handle_revision_opt() has already reported the error. */
usage_with_options(usagestr, options);
} else if (!n) {
error("unknown option `%s'", ctx->argv[0]);
usage_with_options(usagestr, options);
}

View File

@ -434,4 +434,10 @@ test_expect_success 'stdin with multiple groups reports error' '
test_must_fail git shortlog --group=author --group=committer <log
'

test_expect_success 'invalid revision options are not reported as unknown' '
test_must_fail git shortlog --default 2>err &&
test_grep "bad --default argument" err &&
test_grep ! "unknown option" err
'

test_done