builtin: send usage() help text to standard output

Using the show_usage_and_exit_if_asked() helper we introduced earlier, fix callers of usage() that want to show the help text when explicitly asked by the end-user. The help text now goes to the standard output stream for them. These are the bog standard "if we got only '-h', then that is a request for help" callers. Their if (argc == 2 && !strcmp(argv[1], "-h")) usage(message); are simply replaced with show_usage_and_exit_if_asked(argc, argv, message); With this, the built-ins tested by t0012 all send their help text to their standard output stream, so the check in t0012 that was half tightened earlier is now fully tightened to insist on standard error stream being empty. Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 16, 2025 at 13:35 UTC f66d1423f528403a33e8984f765801deb1b9cb97
19 files changed +37 -32
builtin/check-ref-format.c
+2 -2
@@ -64,8 +64,8 @@ int cmd_check_ref_format(int argc,
64
65 BUG_ON_NON_EMPTY_PREFIX(prefix);
66
67 - if (argc == 2 && !strcmp(argv[1], "-h"))
68 - usage(builtin_check_ref_format_usage);
67 + show_usage_if_asked(argc, argv,
68 + builtin_check_ref_format_usage);
69
70 if (argc == 3 && !strcmp(argv[1], "--branch"))
71 return check_ref_format_branch(argv[2]);
builtin/diff-files.c
+1 -2
@@ -29,8 +29,7 @@ int cmd_diff_files(int argc,
29 int result;
30 unsigned options = 0;
31
32 - if (argc == 2 && !strcmp(argv[1], "-h"))
33 - usage(diff_files_usage);
32 + show_usage_if_asked(argc, argv, diff_files_usage);
33
34 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
35
builtin/diff-index.c
+1 -2
@@ -26,8 +26,7 @@ int cmd_diff_index(int argc,
26 int i;
27 int result;
28
29 - if (argc == 2 && !strcmp(argv[1], "-h"))
30 - usage(diff_cache_usage);
29 + show_usage_if_asked(argc, argv, diff_cache_usage);
30
31 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
32
builtin/diff-tree.c
+1 -2
@@ -122,8 +122,7 @@ int cmd_diff_tree(int argc,
122 int read_stdin = 0;
123 int merge_base = 0;
124
125 - if (argc == 2 && !strcmp(argv[1], "-h"))
126 - usage(diff_tree_usage);
125 + show_usage_if_asked(argc, argv, diff_tree_usage);
126
127 git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
128
builtin/fast-import.c
+1 -2
@@ -3565,8 +3565,7 @@ int cmd_fast_import(int argc,
3565 {
3566 unsigned int i;
3567
3568 - if (argc == 2 && !strcmp(argv[1], "-h"))
3569 - usage(fast_import_usage);
3568 + show_usage_if_asked(argc, argv, fast_import_usage);
3569
3570 reset_pack_idx_option(&pack_idx_opts);
3571 git_pack_config();
builtin/get-tar-commit-id.c
+3 -1
@@ -13,7 +13,7 @@ static const char builtin_get_tar_commit_id_usage[] =
13 #define HEADERSIZE (2 * RECORDSIZE)
14
15 int cmd_get_tar_commit_id(int argc,
16 - const char **argv UNUSED,
16 + const char **argv,
17 const char *prefix,
18 struct repository *repo UNUSED)
19 {
@@ -27,6 +27,8 @@ int cmd_get_tar_commit_id(int argc,
27
28 BUG_ON_NON_EMPTY_PREFIX(prefix);
29
30 + show_usage_if_asked(argc, argv, builtin_get_tar_commit_id_usage);
31 +
32 if (argc != 1)
33 usage(builtin_get_tar_commit_id_usage);
34
builtin/index-pack.c
+1 -2
@@ -1897,8 +1897,7 @@ int cmd_index_pack(int argc,
1897 */
1898 fetch_if_missing = 0;
1899
1900 - if (argc == 2 && !strcmp(argv[1], "-h"))
1901 - usage(index_pack_usage);
1900 + show_usage_if_asked(argc, argv, index_pack_usage);
1901
1902 disable_replace_refs();
1903 fsck_options.walk = mark_link;
builtin/mailsplit.c
+2 -2
@@ -284,6 +284,8 @@ int cmd_mailsplit(int argc,
284
285 BUG_ON_NON_EMPTY_PREFIX(prefix);
286
287 + show_usage_if_asked(argc, argv, git_mailsplit_usage);
288 +
289 for (argp = argv+1; *argp; argp++) {
290 const char *arg = *argp;
291
@@ -297,8 +299,6 @@ int cmd_mailsplit(int argc,
299 continue;
300 } else if ( arg[1] == 'f' ) {
301 nr = strtol(arg+2, NULL, 10);
300 - } else if ( arg[1] == 'h' ) {
301 - usage(git_mailsplit_usage);
302 } else if ( arg[1] == 'b' && !arg[2] ) {
303 allow_bare = 1;
304 } else if (!strcmp(arg, "--keep-cr")) {
builtin/merge-index.c
+6 -1
@@ -75,6 +75,9 @@ static void merge_all(void)
75 }
76 }
77
78 +static const char usage_string[] =
79 +"git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])";
80 +
81 int cmd_merge_index(int argc,
82 const char **argv,
83 const char *prefix UNUSED,
@@ -87,8 +90,10 @@ int cmd_merge_index(int argc,
90 */
91 signal(SIGCHLD, SIG_DFL);
92
93 + show_usage_if_asked(argc, argv, usage_string);
94 +
95 if (argc < 3)
91 - usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
96 + usage(usage_string);
97
98 repo_read_index(the_repository);
99
builtin/merge-ours.c
+1 -2
@@ -23,8 +23,7 @@ int cmd_merge_ours(int argc,
23 const char *prefix UNUSED,
24 struct repository *repo UNUSED)
25 {
26 - if (argc == 2 && !strcmp(argv[1], "-h"))
27 - usage(builtin_merge_ours_usage);
26 + show_usage_if_asked(argc, argv, builtin_merge_ours_usage);
27
28 /*
29 * The contents of the current index becomes the tree we
builtin/merge-recursive.c
+6
@@ -38,6 +38,12 @@ int cmd_merge_recursive(int argc,
38 if (argv[0] && ends_with(argv[0], "-subtree"))
39 o.subtree_shift = "";
40
41 + if (argc == 2 && !strcmp(argv[1], "-h")) {
42 + struct strbuf msg = STRBUF_INIT;
43 + strbuf_addf(&msg, builtin_merge_recursive_usage, argv[0]);
44 + show_usage_if_asked(argc, argv, msg.buf);
45 + }
46 +
47 if (argc < 4)
48 usagef(builtin_merge_recursive_usage, argv[0]);
49
builtin/pack-redundant.c
+1 -2
@@ -595,8 +595,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
595 struct strbuf idx_name = STRBUF_INIT;
596 char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
597
598 - if (argc == 2 && !strcmp(argv[1], "-h"))
599 - usage(pack_redundant_usage);
598 + show_usage_if_asked(argc, argv, pack_redundant_usage);
599
600 for (i = 1; i < argc; i++) {
601 const char *arg = argv[i];
builtin/remote-ext.c
+2
@@ -202,6 +202,8 @@ int cmd_remote_ext(int argc,
202 {
203 BUG_ON_NON_EMPTY_PREFIX(prefix);
204
205 + show_usage_if_asked(argc, argv, usage_msg);
206 +
207 if (argc != 3)
208 usage(usage_msg);
209
builtin/remote-fd.c
+1
@@ -64,6 +64,7 @@ int cmd_remote_fd(int argc,
64
65 BUG_ON_NON_EMPTY_PREFIX(prefix);
66
67 + show_usage_if_asked(argc, argv, usage_msg);
68 if (argc != 3)
69 usage(usage_msg);
70
builtin/rev-list.c
+1 -2
@@ -542,8 +542,7 @@ int cmd_rev_list(int argc,
542 const char *show_progress = NULL;
543 int ret = 0;
544
545 - if (argc == 2 && !strcmp(argv[1], "-h"))
546 - usage(rev_list_usage);
545 + show_usage_if_asked(argc, argv, rev_list_usage);
546
547 git_config(git_default_config, NULL);
548 repo_init_revisions(the_repository, &revs, prefix);
builtin/rev-parse.c
+2
@@ -713,6 +713,8 @@ int cmd_rev_parse(int argc,
713 int seen_end_of_options = 0;
714 enum format_type format = FORMAT_DEFAULT;
715
716 + show_usage_if_asked(argc, argv, builtin_rev_parse_usage);
717 +
718 if (argc > 1 && !strcmp("--parseopt", argv[1]))
719 return cmd_parseopt(argc - 1, argv + 1, prefix);
720
builtin/unpack-objects.c
+2
@@ -619,6 +619,8 @@ int cmd_unpack_objects(int argc,
619
620 quiet = !isatty(2);
621
622 + show_usage_if_asked(argc, argv, unpack_usage);
623 +
624 for (i = 1 ; i < argc; i++) {
625 const char *arg = argv[i];
626
builtin/upload-archive.c
+1 -2
@@ -93,8 +93,7 @@ struct repository *repo UNUSED)
93
94 BUG_ON_NON_EMPTY_PREFIX(prefix);
95
96 - if (argc == 2 && !strcmp(argv[1], "-h"))
97 - usage(upload_archive_usage);
96 + show_usage_if_asked(argc, argv, upload_archive_usage);
97
98 /*
99 * Set up sideband subprocess.
t/t0012-help.sh
+2 -8
@@ -257,14 +257,8 @@ do
257 export GIT_CEILING_DIRECTORIES &&
258 test_expect_code 129 git -C sub $builtin -h >output 2>err
259 ) &&
260 - if test -n "$GIT_TEST_HELP_MUST_BE_STDOUT"
261 - then
262 - test_must_be_empty err &&
263 - test_grep usage output
264 - else
265 - test_grep usage output ||
266 - test_grep usage err
267 - fi
260 + test_must_be_empty err &&
261 + test_grep usage output
262 '
263 done <builtins
264