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

[PATCH v6 07/10] cat-file: fix remaining usage bugs

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 28, 2021, 13:28 UTC
Message-ID
<patch-v6-07.10-bf24dd063c9-20211228T132637Z-avarab@gmail.com>
In-Reply-To
<cover-v6-00.10-00000000000-20211228T132637Z-avarab@gmail.com>

With the migration of --batch-all-objects to OPT_CMDMODE() in the preceding commit one bug with combining it and other OPT_CMDMODE() options was solved, but we were still left with e.g. --buffer silently being discarded when not in batch mode.

Fix all those bugs, and in addition emit errors telling the user specifically what options can't be combined with what other options, before this we'd usually just emit the cryptic usage text and leave the users to work it out by themselves.

This change is rather large, because to do so we need to untangle the options processing so that we can not only error out, but emit sensible errors, and e.g. emit errors about options before errors about stray argc elements (as they might become valid if the option were removed).

Some of the output changes ("error:" to "fatal:" with usage_msg_opt[f]()), but none of the exit codes change, except in those cases where we silently accepted bad option combinations before, now we'll error out.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/cat-file.c  | 95 ++++++++++++++++++++++++++++++---------------
 t/t1006-cat-file.sh | 41 +++++++++----------
 2 files changed, 84 insertions(+), 52 deletions(-)
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 87356208134..895292074ae 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -648,6 +648,8 @@ static int batch_option_callback(const struct option *opt,
 int cmd_cat_file(int argc, const char **argv, const char *prefix)
 {
 	int opt = 0;
+	int opt_cw = 0;
+	int opt_epts = 0;
 	const char *exp_type = NULL, *obj_name = NULL;
 	struct batch_options batch = {0};
 	int unknown_type = 0;
@@ -701,45 +703,74 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
 	batch.buffer_output = -1;
 
 	argc = parse_options(argc, argv, prefix, options, usage, 0);
-	if (argc && batch.enabled)
-		usage_with_options(usage, options);
-	if (opt == 'b') {
-		batch.all_objects = 1;
-	} else if (opt) {
-		if (batch.enabled && (opt == 'c' || opt == 'w'))
-			batch.cmdmode = opt;
-		else if (argc == 1)
-			obj_name = argv[0];
-		else
-			usage_with_options(usage, options);
-	} else if (!opt && !batch.enabled) {
-		if (argc == 2) {
-			exp_type = argv[0];
-			obj_name = argv[1];
-		} else
-			usage_with_options(usage, options);
-	} else if (batch.enabled && batch.cmdmode != opt)
-		usage_with_options(usage, options);
+	opt_cw = (opt == 'c' || opt == 'w');
+	opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
 
-	if ((batch.follow_symlinks || batch.all_objects) && !batch.enabled) {
-		usage_with_options(usage, options);
-	}
-
-	if (force_path && opt != 'c' && opt != 'w') {
-		error("--path=<path> needs --textconv or --filters");
-		usage_with_options(usage, options);
-	}
+	/* --batch-all-objects? */
+	if (opt == 'b')
+		batch.all_objects = 1;
 
-	if (force_path && batch.enabled) {
-		error("--path=<path> incompatible with --batch");
-		usage_with_options(usage, options);
-	}
+	/* Option compatibility */
+	if (force_path && !opt_cw)
+		usage_msg_optf(_("'%s=<%s>' needs '%s' or '%s'"),
+			       usage, options,
+			       "--path", _("path|tree-ish"), "--filters",
+			       "--textconv");
 
+	/* Option compatibility with batch mode */
+	if (batch.enabled)
+		;
+	else if (batch.follow_symlinks)
+		usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
+			       "--follow_symlinks");
+	else if (batch.buffer_output >= 0)
+		usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
+			       "--buffer");
+	else if (batch.all_objects)
+		usage_msg_optf(_("'%s' requires a batch mode"), usage, options,
+			       "--batch-all-objects");
+
+	/* Batch defaults */
 	if (batch.buffer_output < 0)
 		batch.buffer_output = batch.all_objects;
 
-	if (batch.enabled)
+	/* Return early if we're in batch mode? */
+	if (batch.enabled) {
+		if (opt_cw)
+			batch.cmdmode = opt;
+		else if (opt && opt != 'b')
+			usage_msg_optf(_("'-%c' is incompatible with batch mode"),
+				       usage, options, opt);
+		else if (argc)
+			usage_msg_opt(_("batch modes take no arguments"), usage,
+				      options);
+
 		return batch_objects(&batch);
+	}
+
+	if (opt) {
+		if (!argc && opt == 'c')
+			usage_msg_optf(_("<rev> required with '%s'"),
+				       usage, options, "--textconv");
+		else if (!argc && opt == 'w')
+			usage_msg_optf(_("<rev> required with '%s'"),
+				       usage, options, "--filters");
+		else if (!argc && opt_epts)
+			usage_msg_optf(_("<object> required with '-%c'"),
+				       usage, options, opt);
+		else if (argc == 1)
+			obj_name = argv[0];
+		else
+			usage_msg_opt(_("too many arguments"), usage, options);
+	} else if (!argc) {
+		usage_with_options(usage, options);
+	} else if (argc != 2) {
+		usage_msg_optf(_("only two arguments allowed in <type> <object> mode, not %d"),
+			      usage, options, argc);
+	} else if (argc) {
+		exp_type = argv[0];
+		obj_name = argv[1];
+	}
 
 	if (unknown_type && opt != 't' && opt != 's')
 		die("git cat-file --allow-unknown-type: use with -s or -t");
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index 2ce5c8b1824..964b3bc0b40 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -24,7 +24,7 @@ done
 
 test_incompatible_usage () {
 	test_expect_code 129 "$@" 2>err &&
-	grep -E "^error:.**needs" err
+	grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err
 }
 
 for opt in --batch --batch-check
@@ -34,48 +34,54 @@ do
 	'
 done
 
+test_missing_usage () {
+	test_expect_code 129 "$@" 2>err &&
+	grep -E "^fatal:.*required" err
+}
+
 short_modes="-e -p -t -s"
 cw_modes="--textconv --filters"
 
 for opt in $cw_modes
 do
 	test_expect_success "usage: $opt requires another option" '
-		test_expect_code 129 git cat-file $opt
+		test_missing_usage git cat-file $opt
 	'
 done
 
 for opt in $short_modes
 do
 	test_expect_success "usage: $opt requires another option" '
-		test_expect_code 129 git cat-file $opt
+		test_missing_usage git cat-file $opt
 	'
 
 	for opt2 in --batch \
 		--batch-check \
-		--follow-symlinks
+		--follow-symlinks \
+		"--path=foo HEAD:some-path.txt"
 	do
-		test_expect_failure "usage: incompatible options: $opt and $opt2" '
+		test_expect_success "usage: incompatible options: $opt and $opt2" '
 			test_incompatible_usage git cat-file $opt $opt2
 		'
 	done
-
-	opt2="--path=foo HEAD:some-path.txt"
-	test_expect_success "usage: incompatible options: $opt and $opt2" '
-		test_incompatible_usage git cat-file $opt $opt2
-	'
 done
 
+test_too_many_arguments () {
+	test_expect_code 129 "$@" 2>err &&
+	grep -E "^fatal: too many arguments$" err
+}
+
 for opt in $short_modes $cw_modes
 do
 	args="one two three"
 	test_expect_success "usage: too many arguments: $opt $args" '
-		test_expect_code 129 git cat-file $opt $args
+		test_too_many_arguments git cat-file $opt $args
 	'
 
 	for opt2 in --buffer --follow-symlinks
 	do
 		test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" '
-			test_expect_code 129 git cat-file $opt $opt2
+			test_incompatible_usage git cat-file $opt $opt2
 		'
 	done
 done
@@ -84,14 +90,9 @@ for opt in --buffer \
 	--follow-symlinks \
 	--batch-all-objects
 do
-	status=success
-	if test $opt = "--buffer"
-	then
-		status=failure
-	fi
-	test_expect_$status "usage: bad option combination: $opt without batch mode" '
-		test_expect_code 129 git cat-file $opt &&
-		test_expect_code 129 git cat-file $opt commit HEAD
+	test_expect_success "usage: bad option combination: $opt without batch mode" '
+		test_incompatible_usage git cat-file $opt &&
+		test_incompatible_usage git cat-file $opt commit HEAD
 	'
 done
 
-- 
2.34.1.1257.g2af47340c7b
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 81 of 101 in “cat-file replace handling and optimization”
  1. 0/5 cat-file replace handling and optimizationJeff King, Oct 5, 2021
  2. 1/5 t1006: clean up broken objectsJeff King, Oct 5, 2021
  3. 2/5 cat-file: mention --unordered along with --batch-all-objectsJeff King, Oct 5, 2021
  4. Ævar Arnfjörð BjarmasonOct 5, 2021
  5. Jeff KingOct 5, 2021
  6. Ævar Arnfjörð BjarmasonOct 6, 2021
  7. Jeff KingOct 6, 2021
  8. Ævar Arnfjörð BjarmasonOct 7, 2021
  9. Jeff KingOct 8, 2021
  10. Ævar Arnfjörð BjarmasonOct 8, 2021
  11. Junio C HamanoOct 8, 2021
  12. Jeff KingOct 8, 2021
  13. Junio C HamanoOct 8, 2021
  14. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Nov 6, 2021
  15. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Nov 6, 2021
  16. Eric SunshineNov 7, 2021
  17. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Nov 6, 2021
  18. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Nov 6, 2021
  19. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Nov 6, 2021
  20. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Nov 6, 2021
  21. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Nov 6, 2021
  22. Eric SunshineNov 7, 2021
  23. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Nov 6, 2021
  24. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Nov 6, 2021
  25. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Nov 6, 2021
  26. Eric SunshineNov 7, 2021
  27. 10/10 cat-file: improve --(textconv|filters) disambiguationÆvar Arnfjörð Bjarmason, Nov 6, 2021
  28. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Nov 12, 2021
  29. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Nov 12, 2021
  30. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Nov 12, 2021
  31. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Nov 12, 2021
  32. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Nov 12, 2021
  33. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Nov 12, 2021
  34. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Nov 12, 2021
  35. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Nov 12, 2021
  36. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Nov 12, 2021
  37. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Nov 12, 2021
  38. 10/10 cat-file: improve --(textconv|filters) disambiguationÆvar Arnfjörð Bjarmason, Nov 12, 2021
  39. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Nov 29, 2021
  40. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Nov 29, 2021
  41. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Nov 29, 2021
  42. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Nov 29, 2021
  43. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Nov 29, 2021
  44. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Nov 29, 2021
  45. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Nov 29, 2021
  46. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Nov 29, 2021
  47. Jiang XinDec 6, 2021
  48. 10/10 cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters)Ævar Arnfjörð Bjarmason, Nov 29, 2021
  49. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Nov 29, 2021
  50. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Nov 29, 2021
  51. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Dec 8, 2021
  52. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Dec 8, 2021
  53. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Dec 8, 2021
  54. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Dec 8, 2021
  55. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Dec 8, 2021
  56. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Dec 8, 2021
  57. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Dec 8, 2021
  58. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Dec 8, 2021
  59. John CaiDec 20, 2021
  60. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Dec 8, 2021
  61. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Dec 8, 2021
  62. 10/10 cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters)Ævar Arnfjörð Bjarmason, Dec 8, 2021
  63. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Dec 22, 2021
  64. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Dec 22, 2021
  65. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Dec 22, 2021
  66. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Dec 22, 2021
  67. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Dec 22, 2021
  68. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Dec 22, 2021
  69. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Dec 22, 2021
  70. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Dec 22, 2021
  71. Junio C HamanoDec 26, 2021
  72. 10/10 cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters)Ævar Arnfjörð Bjarmason, Dec 22, 2021
  73. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Dec 22, 2021
  74. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Dec 22, 2021
  75. 00/10 cat-file: better usage UX & error messagesÆvar Arnfjörð Bjarmason, Dec 28, 2021
  76. 01/10 cat-file tests: test bad usageÆvar Arnfjörð Bjarmason, Dec 28, 2021
  77. 02/10 cat-file tests: test messaging on bad objects/pathsÆvar Arnfjörð Bjarmason, Dec 28, 2021
  78. 03/10 parse-options API: add a usage_msg_optf()Ævar Arnfjörð Bjarmason, Dec 28, 2021
  79. 04/10 cat-file docs: fix SYNOPSIS and "-h" outputÆvar Arnfjörð Bjarmason, Dec 28, 2021
  80. 05/10 cat-file: move "usage" variable to cmd_cat_file()Ævar Arnfjörð Bjarmason, Dec 28, 2021
  81. 07/10 cat-file: fix remaining usage bugsÆvar Arnfjörð Bjarmason, Dec 28, 2021
  82. 08/10 cat-file: correct and improve usage informationÆvar Arnfjörð Bjarmason, Dec 28, 2021
  83. Jiang XinJan 8, 2022
  84. 0/2 fixups for issues in next-merged ab/cat-fileÆvar Arnfjörð Bjarmason, Jan 10, 2022
  85. 1/2 cat-file: don't whitespace-pad "(...)" in SYNOPSIS and usage outputÆvar Arnfjörð Bjarmason, Jan 10, 2022
  86. 2/2 cat-file: s/_/-/ in typo'd usage_msg_optf() messageÆvar Arnfjörð Bjarmason, Jan 10, 2022
  87. Junio C HamanoJan 10, 2022
  88. Taylor BlauJan 11, 2022
  89. Junio C HamanoJan 12, 2022
  90. 09/10 object-name.c: don't have GET_OID_ONLY_TO_DIE imply *_QUIETLYÆvar Arnfjörð Bjarmason, Dec 28, 2021
  91. 10/10 cat-file: use GET_OID_ONLY_TO_DIE in --(textconv|filters)Ævar Arnfjörð Bjarmason, Dec 28, 2021
  92. 06/10 cat-file: make --batch-all-objects a CMDMODEÆvar Arnfjörð Bjarmason, Dec 28, 2021
  93. 3/5 cat-file: disable refs/replace with --batch-all-objectsJeff King, Oct 5, 2021
  94. Derrick StoleeOct 6, 2021
  95. Junio C HamanoOct 7, 2021
  96. 4/5 cat-file: split ordered/unordered batch-all-objects callbacksJeff King, Oct 5, 2021
  97. 5/5 cat-file: use packed_object_info() for --batch-all-objectsJeff King, Oct 5, 2021
  98. Junio C HamanoOct 7, 2021
  99. Jeff KingOct 8, 2021
  100. Derrick StoleeOct 6, 2021
  101. Jeff KingOct 7, 2021

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.