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>
This commit is contained in:
Junio C Hamano
2025-01-16 13:35:53 -08:00
parent a36a822d7d
commit f66d1423f5
19 changed files with 37 additions and 32 deletions

View File

@ -64,8 +64,8 @@ int cmd_check_ref_format(int argc,
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv,
usage(builtin_check_ref_format_usage); builtin_check_ref_format_usage);
if (argc == 3 && !strcmp(argv[1], "--branch")) if (argc == 3 && !strcmp(argv[1], "--branch"))
return check_ref_format_branch(argv[2]); return check_ref_format_branch(argv[2]);

View File

@ -29,8 +29,7 @@ int cmd_diff_files(int argc,
int result; int result;
unsigned options = 0; unsigned options = 0;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, diff_files_usage);
usage(diff_files_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

View File

@ -26,8 +26,7 @@ int cmd_diff_index(int argc,
int i; int i;
int result; int result;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, diff_cache_usage);
usage(diff_cache_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

View File

@ -122,8 +122,7 @@ int cmd_diff_tree(int argc,
int read_stdin = 0; int read_stdin = 0;
int merge_base = 0; int merge_base = 0;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, diff_tree_usage);
usage(diff_tree_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */ git_config(git_diff_basic_config, NULL); /* no "diff" UI options */

View File

@ -3565,8 +3565,7 @@ int cmd_fast_import(int argc,
{ {
unsigned int i; unsigned int i;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, fast_import_usage);
usage(fast_import_usage);
reset_pack_idx_option(&pack_idx_opts); reset_pack_idx_option(&pack_idx_opts);
git_pack_config(); git_pack_config();

View File

@ -13,7 +13,7 @@ static const char builtin_get_tar_commit_id_usage[] =
#define HEADERSIZE (2 * RECORDSIZE) #define HEADERSIZE (2 * RECORDSIZE)
int cmd_get_tar_commit_id(int argc, int cmd_get_tar_commit_id(int argc,
const char **argv UNUSED, const char **argv,
const char *prefix, const char *prefix,
struct repository *repo UNUSED) struct repository *repo UNUSED)
{ {
@ -27,6 +27,8 @@ int cmd_get_tar_commit_id(int argc,
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
show_usage_if_asked(argc, argv, builtin_get_tar_commit_id_usage);
if (argc != 1) if (argc != 1)
usage(builtin_get_tar_commit_id_usage); usage(builtin_get_tar_commit_id_usage);

View File

@ -1897,8 +1897,7 @@ int cmd_index_pack(int argc,
*/ */
fetch_if_missing = 0; fetch_if_missing = 0;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, index_pack_usage);
usage(index_pack_usage);
disable_replace_refs(); disable_replace_refs();
fsck_options.walk = mark_link; fsck_options.walk = mark_link;

View File

@ -284,6 +284,8 @@ int cmd_mailsplit(int argc,
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
show_usage_if_asked(argc, argv, git_mailsplit_usage);
for (argp = argv+1; *argp; argp++) { for (argp = argv+1; *argp; argp++) {
const char *arg = *argp; const char *arg = *argp;
@ -297,8 +299,6 @@ int cmd_mailsplit(int argc,
continue; continue;
} else if ( arg[1] == 'f' ) { } else if ( arg[1] == 'f' ) {
nr = strtol(arg+2, NULL, 10); nr = strtol(arg+2, NULL, 10);
} else if ( arg[1] == 'h' ) {
usage(git_mailsplit_usage);
} else if ( arg[1] == 'b' && !arg[2] ) { } else if ( arg[1] == 'b' && !arg[2] ) {
allow_bare = 1; allow_bare = 1;
} else if (!strcmp(arg, "--keep-cr")) { } else if (!strcmp(arg, "--keep-cr")) {

View File

@ -75,6 +75,9 @@ static void merge_all(void)
} }
} }
static const char usage_string[] =
"git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])";
int cmd_merge_index(int argc, int cmd_merge_index(int argc,
const char **argv, const char **argv,
const char *prefix UNUSED, const char *prefix UNUSED,
@ -87,8 +90,10 @@ int cmd_merge_index(int argc,
*/ */
signal(SIGCHLD, SIG_DFL); signal(SIGCHLD, SIG_DFL);
show_usage_if_asked(argc, argv, usage_string);
if (argc < 3) if (argc < 3)
usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])"); usage(usage_string);
repo_read_index(the_repository); repo_read_index(the_repository);

View File

@ -23,8 +23,7 @@ int cmd_merge_ours(int argc,
const char *prefix UNUSED, const char *prefix UNUSED,
struct repository *repo UNUSED) struct repository *repo UNUSED)
{ {
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, builtin_merge_ours_usage);
usage(builtin_merge_ours_usage);
/* /*
* The contents of the current index becomes the tree we * The contents of the current index becomes the tree we

View File

@ -38,6 +38,12 @@ int cmd_merge_recursive(int argc,
if (argv[0] && ends_with(argv[0], "-subtree")) if (argv[0] && ends_with(argv[0], "-subtree"))
o.subtree_shift = ""; o.subtree_shift = "";
if (argc == 2 && !strcmp(argv[1], "-h")) {
struct strbuf msg = STRBUF_INIT;
strbuf_addf(&msg, builtin_merge_recursive_usage, argv[0]);
show_usage_if_asked(argc, argv, msg.buf);
}
if (argc < 4) if (argc < 4)
usagef(builtin_merge_recursive_usage, argv[0]); usagef(builtin_merge_recursive_usage, argv[0]);

View File

@ -595,8 +595,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
struct strbuf idx_name = STRBUF_INIT; struct strbuf idx_name = STRBUF_INIT;
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */ char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, pack_redundant_usage);
usage(pack_redundant_usage);
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
const char *arg = argv[i]; const char *arg = argv[i];

View File

@ -202,6 +202,8 @@ int cmd_remote_ext(int argc,
{ {
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
show_usage_if_asked(argc, argv, usage_msg);
if (argc != 3) if (argc != 3)
usage(usage_msg); usage(usage_msg);

View File

@ -64,6 +64,7 @@ int cmd_remote_fd(int argc,
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
show_usage_if_asked(argc, argv, usage_msg);
if (argc != 3) if (argc != 3)
usage(usage_msg); usage(usage_msg);

View File

@ -542,8 +542,7 @@ int cmd_rev_list(int argc,
const char *show_progress = NULL; const char *show_progress = NULL;
int ret = 0; int ret = 0;
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, rev_list_usage);
usage(rev_list_usage);
git_config(git_default_config, NULL); git_config(git_default_config, NULL);
repo_init_revisions(the_repository, &revs, prefix); repo_init_revisions(the_repository, &revs, prefix);

View File

@ -713,6 +713,8 @@ int cmd_rev_parse(int argc,
int seen_end_of_options = 0; int seen_end_of_options = 0;
enum format_type format = FORMAT_DEFAULT; enum format_type format = FORMAT_DEFAULT;
show_usage_if_asked(argc, argv, builtin_rev_parse_usage);
if (argc > 1 && !strcmp("--parseopt", argv[1])) if (argc > 1 && !strcmp("--parseopt", argv[1]))
return cmd_parseopt(argc - 1, argv + 1, prefix); return cmd_parseopt(argc - 1, argv + 1, prefix);

View File

@ -619,6 +619,8 @@ int cmd_unpack_objects(int argc,
quiet = !isatty(2); quiet = !isatty(2);
show_usage_if_asked(argc, argv, unpack_usage);
for (i = 1 ; i < argc; i++) { for (i = 1 ; i < argc; i++) {
const char *arg = argv[i]; const char *arg = argv[i];

View File

@ -93,8 +93,7 @@ struct repository *repo UNUSED)
BUG_ON_NON_EMPTY_PREFIX(prefix); BUG_ON_NON_EMPTY_PREFIX(prefix);
if (argc == 2 && !strcmp(argv[1], "-h")) show_usage_if_asked(argc, argv, upload_archive_usage);
usage(upload_archive_usage);
/* /*
* Set up sideband subprocess. * Set up sideband subprocess.

View File

@ -257,14 +257,8 @@ do
export GIT_CEILING_DIRECTORIES && export GIT_CEILING_DIRECTORIES &&
test_expect_code 129 git -C sub $builtin -h >output 2>err test_expect_code 129 git -C sub $builtin -h >output 2>err
) && ) &&
if test -n "$GIT_TEST_HELP_MUST_BE_STDOUT" test_must_be_empty err &&
then test_grep usage output
test_must_be_empty err &&
test_grep usage output
else
test_grep usage output ||
test_grep usage err
fi
' '
done <builtins done <builtins