Git fork

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>

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