]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin: send usage() help text to standard output
authorJunio C Hamano <gitster@pobox.com>
Thu, 16 Jan 2025 21:35:53 +0000 (13:35 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 17 Jan 2025 21:30:03 +0000 (13:30 -0800)
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>
19 files changed:
builtin/check-ref-format.c
builtin/diff-files.c
builtin/diff-index.c
builtin/diff-tree.c
builtin/fast-import.c
builtin/get-tar-commit-id.c
builtin/index-pack.c
builtin/mailsplit.c
builtin/merge-index.c
builtin/merge-ours.c
builtin/merge-recursive.c
builtin/pack-redundant.c
builtin/remote-ext.c
builtin/remote-fd.c
builtin/rev-list.c
builtin/rev-parse.c
builtin/unpack-objects.c
builtin/upload-archive.c
t/t0012-help.sh

index cef1ffe3ceb7199875d4b98cafb28d2d1d703ee9..5d80afeec05e3d75e5dad203e70c403bd1b10072 100644 (file)
@@ -64,8 +64,8 @@ int cmd_check_ref_format(int argc,
 
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(builtin_check_ref_format_usage);
+       show_usage_if_asked(argc, argv,
+                           builtin_check_ref_format_usage);
 
        if (argc == 3 && !strcmp(argv[1], "--branch"))
                return check_ref_format_branch(argv[2]);
index 604b04bb2c8694716fb3f6ba630d955959f6ceb9..99b1749723bc674beb52b8b1bf2b7c03e01d6e31 100644 (file)
@@ -29,8 +29,7 @@ int cmd_diff_files(int argc,
        int result;
        unsigned options = 0;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(diff_files_usage);
+       show_usage_if_asked(argc, argv, diff_files_usage);
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
index ebc824602e02e67cadfffa8022afef314d367bfa..81c0bc8ed7c6bedb7138b21e7c2027aee8ff60e2 100644 (file)
@@ -26,8 +26,7 @@ int cmd_diff_index(int argc,
        int i;
        int result;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(diff_cache_usage);
+       show_usage_if_asked(argc, argv, diff_cache_usage);
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
index 40804e7b48e319f6618cd22ac3695ce510b5f51a..e31cc797fe1e6e5f1269ad7a37492141708fe11c 100644 (file)
@@ -122,8 +122,7 @@ int cmd_diff_tree(int argc,
        int read_stdin = 0;
        int merge_base = 0;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(diff_tree_usage);
+       show_usage_if_asked(argc, argv, diff_tree_usage);
 
        git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
 
index 0f86392761abbe6acb217fef7f4fe7c3ff5ac1fa..2da46fecdcbe0a69fd5bbc13c65c9cba1c66b5bd 100644 (file)
@@ -3565,8 +3565,7 @@ int cmd_fast_import(int argc,
 {
        unsigned int i;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(fast_import_usage);
+       show_usage_if_asked(argc, argv, fast_import_usage);
 
        reset_pack_idx_option(&pack_idx_opts);
        git_pack_config();
index 6bec0d1854c03ecbda0c364a7ff8313b10ca3069..e4cd1627b44bccb743ed19952a6b1c52ca742de4 100644 (file)
@@ -13,7 +13,7 @@ static const char builtin_get_tar_commit_id_usage[] =
 #define HEADERSIZE (2 * RECORDSIZE)
 
 int cmd_get_tar_commit_id(int argc,
-                         const char **argv UNUSED,
+                         const char **argv,
                          const char *prefix,
                          struct repository *repo UNUSED)
 {
@@ -27,6 +27,8 @@ int cmd_get_tar_commit_id(int argc,
 
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
+       show_usage_if_asked(argc, argv, builtin_get_tar_commit_id_usage);
+
        if (argc != 1)
                usage(builtin_get_tar_commit_id_usage);
 
index 0b62b2589f10f97aacab4614db158666850b78f6..d41b126ec04ef362f73c404d3dbe528ea2bd9d31 100644 (file)
@@ -1897,8 +1897,7 @@ int cmd_index_pack(int argc,
         */
        fetch_if_missing = 0;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(index_pack_usage);
+       show_usage_if_asked(argc, argv, index_pack_usage);
 
        disable_replace_refs();
        fsck_options.walk = mark_link;
index 41dd3047313e6cab6dcf7fe65d2f5529e70e4aa2..264df6259a04118f78a3adee6f2574e2332c2502 100644 (file)
@@ -284,6 +284,8 @@ int cmd_mailsplit(int argc,
 
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
+       show_usage_if_asked(argc, argv, git_mailsplit_usage);
+
        for (argp = argv+1; *argp; argp++) {
                const char *arg = *argp;
 
@@ -297,8 +299,6 @@ int cmd_mailsplit(int argc,
                        continue;
                } else if ( arg[1] == 'f' ) {
                        nr = strtol(arg+2, NULL, 10);
-               } else if ( arg[1] == 'h' ) {
-                       usage(git_mailsplit_usage);
                } else if ( arg[1] == 'b' && !arg[2] ) {
                        allow_bare = 1;
                } else if (!strcmp(arg, "--keep-cr")) {
index 342699edb77c97cd36eb2ac00d2495b3d64c6481..3314fb13361d644e3514c709c8d8de40994fc1ac 100644 (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,
                    const char **argv,
                    const char *prefix UNUSED,
@@ -87,8 +90,10 @@ int cmd_merge_index(int argc,
         */
        signal(SIGCHLD, SIG_DFL);
 
+       show_usage_if_asked(argc, argv, usage_string);
+
        if (argc < 3)
-               usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
+               usage(usage_string);
 
        repo_read_index(the_repository);
 
index 3ecd9172f18466823ad867ca28958c12a3266b85..97b8a792c77eb7c2b0447d532cc19342a974ac47 100644 (file)
@@ -23,8 +23,7 @@ int cmd_merge_ours(int argc,
                   const char *prefix UNUSED,
                   struct repository *repo UNUSED)
 {
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(builtin_merge_ours_usage);
+       show_usage_if_asked(argc, argv, builtin_merge_ours_usage);
 
        /*
         * The contents of the current index becomes the tree we
index 1dd295558b207d495eb0e4bf4523f7a78f9eb070..abfc060e28a863f260ce855b1b0fda28a4b6bdaa 100644 (file)
@@ -38,6 +38,12 @@ int cmd_merge_recursive(int argc,
        if (argv[0] && ends_with(argv[0], "-subtree"))
                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)
                usagef(builtin_merge_recursive_usage, argv[0]);
 
index e046575871956b12dd3b2e2dfed52a016e043803..3febe732f8e1c75c54248e1fdff9442051f312ea 100644 (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;
        char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(pack_redundant_usage);
+       show_usage_if_asked(argc, argv, pack_redundant_usage);
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
index 33c8ae0fc7d638461e11b01b1c31d291efdacba9..bd2037f27da0c44ce628eeedb889b7df50ba19a6 100644 (file)
@@ -202,6 +202,8 @@ int cmd_remote_ext(int argc,
 {
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
+       show_usage_if_asked(argc, argv, usage_msg);
+
        if (argc != 3)
                usage(usage_msg);
 
index ae896eda57b86d110d6cb2f3aec1cc8e69ddb508..39908546ba0128dedf6ae4fae657cdc7bf9b07e9 100644 (file)
@@ -64,6 +64,7 @@ int cmd_remote_fd(int argc,
 
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
+       show_usage_if_asked(argc, argv, usage_msg);
        if (argc != 3)
                usage(usage_msg);
 
index 3196da7b2d23e33fafde57a1af1c42a5c985f0b0..28f148049f0db6d40301ad0aee59ec97922593bc 100644 (file)
@@ -542,8 +542,7 @@ int cmd_rev_list(int argc,
        const char *show_progress = NULL;
        int ret = 0;
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(rev_list_usage);
+       show_usage_if_asked(argc, argv, rev_list_usage);
 
        git_config(git_default_config, NULL);
        repo_init_revisions(the_repository, &revs, prefix);
index 949747a6b6c7f8ee854dd6027fd4bc7cee7f82be..428c866c05f4a47cef88ebcb1296facc46cbb143 100644 (file)
@@ -713,6 +713,8 @@ int cmd_rev_parse(int argc,
        int seen_end_of_options = 0;
        enum format_type format = FORMAT_DEFAULT;
 
+       show_usage_if_asked(argc, argv, builtin_rev_parse_usage);
+
        if (argc > 1 && !strcmp("--parseopt", argv[1]))
                return cmd_parseopt(argc - 1, argv + 1, prefix);
 
index 2197d6d9332117636f31abadb5aff70424ba305d..8faa6024b26dd08f511bb9fbe66b38da7a9f1ddf 100644 (file)
@@ -619,6 +619,8 @@ int cmd_unpack_objects(int argc,
 
        quiet = !isatty(2);
 
+       show_usage_if_asked(argc, argv, unpack_usage);
+
        for (i = 1 ; i < argc; i++) {
                const char *arg = argv[i];
 
index 9d76a31c8f5d9ac968c5e160e6dcc54f998eca8a..97d7c9522f98684c9f749351798839f7935dcee8 100644 (file)
@@ -93,8 +93,7 @@ struct repository *repo UNUSED)
 
        BUG_ON_NON_EMPTY_PREFIX(prefix);
 
-       if (argc == 2 && !strcmp(argv[1], "-h"))
-               usage(upload_archive_usage);
+       show_usage_if_asked(argc, argv, upload_archive_usage);
 
        /*
         * Set up sideband subprocess.
index 9c7ae9fd36dfd96e3b919d6bb8193c5cf508f458..d3a0967e9d075254d0a2fbecb6d30b00e0e2010f 100755 (executable)
@@ -257,14 +257,8 @@ do
                        export GIT_CEILING_DIRECTORIES &&
                        test_expect_code 129 git -C sub $builtin -h >output 2>err
                ) &&
-               if test -n "$GIT_TEST_HELP_MUST_BE_STDOUT"
-               then
-                       test_must_be_empty err &&
-                       test_grep usage output
-               else
-                       test_grep usage output ||
-                       test_grep usage err
-               fi
+               test_must_be_empty err &&
+               test_grep usage output
        '
 done <builtins