git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git bugreport with invalid CLI argument does not report error

From
Emily Shaffer <nasamuffin@google.com>
Date
Oct 25, 2023, 22:53 UTC
Message-ID
<ZTmcVJaY2TjFCVyw@google.com>
In-Reply-To
<7a92b537-ba88-4667-bb18-2e8c74aa9915@gmail.com>
On Thu, Oct 26, 2023 at 07:59:16AM +1100, Sheik wrote:
Show 14 quoted lines
> 
> Hi Maintainers,
> 
> 
> Running git bugreport with an invalid CLI argument in a valid Git directory
> does not report error. Expected behaviour would be that it reports an error.
> 
> 
> #Example git commands which should have reported an error but continues to
> succeed
> 
> cd $ToAnyDirectory
> 
> git bugreport diagnose

It looks like parse-options.[ch] helps us here for misspelled dashed options, like `--diaggnose`. But it doesn't complain when there are unexpected positional arguments. I think we can just notice if there are any argc left over, complain, and print usage.

I put together a quick patch; could be that we don't need to leave this error about "positional arguments" and can leave it as an exercise to the reader to compare their previous command to the usage text. I guess we could also unroll remaining argv but it was just a hair more time than I wanted to spend ;)

 - Emily
--- 8< ---
From 2031c7f55652559b8b4ec3c67ce4c4f94a355762 Mon Sep 17 00:00:00 2001
From: Emily Shaffer <nasamuffin@google.com>
Date: Wed, 25 Oct 2023 15:45:25 -0700
Subject: [PATCH] bugreport: reject positional arguments

git-bugreport already rejected unrecognized flag arguments, like `--diaggnose`, but this doesn't help if the user's mistake was to forget the `--` in front of the argument. This can result in a user's intended argument not being parsed with no indication to the user that something went wrong. Since git-bugreport presently doesn't take any positionals at all, let's reject all positionals and give the user a usage hint.

Signed-off-by: Emily Shaffer <nasamuffin@google.com>
---
 builtin/bugreport.c  | 5 +++++
 t/t0091-bugreport.sh | 6 ++++++
 2 files changed, 11 insertions(+)
diff --git a/builtin/bugreport.c b/builtin/bugreport.c
index d2ae5c305d..eb6234a50d 100644
--- a/builtin/bugreport.c
+++ b/builtin/bugreport.c
@@ -126,6 +126,11 @@ int cmd_bugreport(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, bugreport_options,
 			     bugreport_usage, 0);
 
+	if (argc) {
+		error(_("git bugreport does not take positional arguments"));
+		usage(bugreport_usage[0]);
+	}
+
 	/* Prepare the path to put the result */
 	prefixed_filename = prefix_filename(prefix,
 					    option_output ? option_output : "");
diff --git a/t/t0091-bugreport.sh b/t/t0091-bugreport.sh
index f6998269be..2061d6f386 100755
--- a/t/t0091-bugreport.sh
+++ b/t/t0091-bugreport.sh
@@ -69,6 +69,12 @@ test_expect_success 'incorrect arguments abort with usage' '
 	test_path_is_missing git-bugreport-*
 '
 
+test_expect_success 'incorrect positional arguments abort with usage' '
+	test_must_fail git bugreport false 2>output &&
+	test_i18ngrep usage output &&
+	test_path_is_missing git-bugreport-*
+'
+
 test_expect_success 'runs outside of a git dir' '
 	test_when_finished rm non-repo/git-bugreport-* &&
 	nongit git bugreport
-- 
2.42.0.758.gaed0368e0e-goog
Previous: SheikNext: Eric Sunshine
Message 2 of 28 in “git bugreport with invalid CLI argument does not report error”
  1. SheikOct 25, 2023
  2. Emily ShafferOct 25, 2023
  3. Eric SunshineOct 25, 2023
  4. bugreport: reject positional argumentsemilyshaffer@google.com, Oct 26, 2023
  5. Eric SunshineOct 26, 2023
  6. Dragan SimicOct 26, 2023
  7. Eric SunshineOct 26, 2023
  8. Dragan SimicOct 26, 2023
  9. bugreport: reject positional argumentsemilyshaffer@google.com, Oct 26, 2023
  10. Eric SunshineOct 26, 2023
  11. Phillip WoodOct 27, 2023
  12. Junio C HamanoOct 30, 2023
  13. Junio C HamanoOct 30, 2023
  14. Junio C HamanoOct 30, 2023
  15. Junio C HamanoOct 30, 2023
  16. Phillip WoodOct 30, 2023
  17. Junio C HamanoOct 30, 2023
  18. Junio C HamanoOct 31, 2023
  19. 0/2 Deprecate test_i18ngrep furtherJunio C Hamano, Oct 31, 2023
  20. 1/2 test framework: further deprecate test_i18ngrepJunio C Hamano, Oct 31, 2023
  21. 2/2 tests: teach callers of test_i18ngrep to use test_grepJunio C Hamano, Oct 31, 2023
  22. Phillip WoodNov 1, 2023
  23. Junio C HamanoNov 1, 2023
  24. 0/2 bugreport: reject positional argumentsemilyshaffer@google.com, Oct 26, 2023
  25. Eric SunshineOct 26, 2023
  26. 1/2 t0091-bugreport: stop using i18ngrepemilyshaffer@google.com, Oct 26, 2023
  27. Junio C HamanoOct 29, 2023
  28. 2/2 bugreport: reject positional argumentsemilyshaffer@google.com, Oct 26, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.