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