Skip to content

Commit f66d142

Browse files
committed
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>
1 parent a36a822 commit f66d142

19 files changed

+37
-32
lines changed

builtin/check-ref-format.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ int cmd_check_ref_format(int argc,
6464

6565
BUG_ON_NON_EMPTY_PREFIX(prefix);
6666

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);
6969

7070
if (argc == 3 && !strcmp(argv[1], "--branch"))
7171
return check_ref_format_branch(argv[2]);

builtin/diff-files.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ int cmd_diff_files(int argc,
2929
int result;
3030
unsigned options = 0;
3131

32-
if (argc == 2 && !strcmp(argv[1], "-h"))
33-
usage(diff_files_usage);
32+
show_usage_if_asked(argc, argv, diff_files_usage);
3433

3534
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
3635

builtin/diff-index.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ int cmd_diff_index(int argc,
2626
int i;
2727
int result;
2828

29-
if (argc == 2 && !strcmp(argv[1], "-h"))
30-
usage(diff_cache_usage);
29+
show_usage_if_asked(argc, argv, diff_cache_usage);
3130

3231
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
3332

builtin/diff-tree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ int cmd_diff_tree(int argc,
122122
int read_stdin = 0;
123123
int merge_base = 0;
124124

125-
if (argc == 2 && !strcmp(argv[1], "-h"))
126-
usage(diff_tree_usage);
125+
show_usage_if_asked(argc, argv, diff_tree_usage);
127126

128127
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
129128

builtin/fast-import.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3565,8 +3565,7 @@ int cmd_fast_import(int argc,
35653565
{
35663566
unsigned int i;
35673567

3568-
if (argc == 2 && !strcmp(argv[1], "-h"))
3569-
usage(fast_import_usage);
3568+
show_usage_if_asked(argc, argv, fast_import_usage);
35703569

35713570
reset_pack_idx_option(&pack_idx_opts);
35723571
git_pack_config();

builtin/get-tar-commit-id.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static const char builtin_get_tar_commit_id_usage[] =
1313
#define HEADERSIZE (2 * RECORDSIZE)
1414

1515
int cmd_get_tar_commit_id(int argc,
16-
const char **argv UNUSED,
16+
const char **argv,
1717
const char *prefix,
1818
struct repository *repo UNUSED)
1919
{
@@ -27,6 +27,8 @@ int cmd_get_tar_commit_id(int argc,
2727

2828
BUG_ON_NON_EMPTY_PREFIX(prefix);
2929

30+
show_usage_if_asked(argc, argv, builtin_get_tar_commit_id_usage);
31+
3032
if (argc != 1)
3133
usage(builtin_get_tar_commit_id_usage);
3234

builtin/index-pack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,8 +1897,7 @@ int cmd_index_pack(int argc,
18971897
*/
18981898
fetch_if_missing = 0;
18991899

1900-
if (argc == 2 && !strcmp(argv[1], "-h"))
1901-
usage(index_pack_usage);
1900+
show_usage_if_asked(argc, argv, index_pack_usage);
19021901

19031902
disable_replace_refs();
19041903
fsck_options.walk = mark_link;

builtin/mailsplit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ int cmd_mailsplit(int argc,
284284

285285
BUG_ON_NON_EMPTY_PREFIX(prefix);
286286

287+
show_usage_if_asked(argc, argv, git_mailsplit_usage);
288+
287289
for (argp = argv+1; *argp; argp++) {
288290
const char *arg = *argp;
289291

@@ -297,8 +299,6 @@ int cmd_mailsplit(int argc,
297299
continue;
298300
} else if ( arg[1] == 'f' ) {
299301
nr = strtol(arg+2, NULL, 10);
300-
} else if ( arg[1] == 'h' ) {
301-
usage(git_mailsplit_usage);
302302
} else if ( arg[1] == 'b' && !arg[2] ) {
303303
allow_bare = 1;
304304
} else if (!strcmp(arg, "--keep-cr")) {

builtin/merge-index.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ static void merge_all(void)
7575
}
7676
}
7777

78+
static const char usage_string[] =
79+
"git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])";
80+
7881
int cmd_merge_index(int argc,
7982
const char **argv,
8083
const char *prefix UNUSED,
@@ -87,8 +90,10 @@ int cmd_merge_index(int argc,
8790
*/
8891
signal(SIGCHLD, SIG_DFL);
8992

93+
show_usage_if_asked(argc, argv, usage_string);
94+
9095
if (argc < 3)
91-
usage("git merge-index [-o] [-q] <merge-program> (-a | [--] [<filename>...])");
96+
usage(usage_string);
9297

9398
repo_read_index(the_repository);
9499

builtin/merge-ours.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ int cmd_merge_ours(int argc,
2323
const char *prefix UNUSED,
2424
struct repository *repo UNUSED)
2525
{
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);
2827

2928
/*
3029
* The contents of the current index becomes the tree we

builtin/merge-recursive.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ int cmd_merge_recursive(int argc,
3838
if (argv[0] && ends_with(argv[0], "-subtree"))
3939
o.subtree_shift = "";
4040

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+
4147
if (argc < 4)
4248
usagef(builtin_merge_recursive_usage, argv[0]);
4349

builtin/pack-redundant.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,7 @@ int cmd_pack_redundant(int argc, const char **argv, const char *prefix UNUSED, s
595595
struct strbuf idx_name = STRBUF_INIT;
596596
char buf[GIT_MAX_HEXSZ + 2]; /* hex hash + \n + \0 */
597597

598-
if (argc == 2 && !strcmp(argv[1], "-h"))
599-
usage(pack_redundant_usage);
598+
show_usage_if_asked(argc, argv, pack_redundant_usage);
600599

601600
for (i = 1; i < argc; i++) {
602601
const char *arg = argv[i];

builtin/remote-ext.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ int cmd_remote_ext(int argc,
202202
{
203203
BUG_ON_NON_EMPTY_PREFIX(prefix);
204204

205+
show_usage_if_asked(argc, argv, usage_msg);
206+
205207
if (argc != 3)
206208
usage(usage_msg);
207209

builtin/remote-fd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ int cmd_remote_fd(int argc,
6464

6565
BUG_ON_NON_EMPTY_PREFIX(prefix);
6666

67+
show_usage_if_asked(argc, argv, usage_msg);
6768
if (argc != 3)
6869
usage(usage_msg);
6970

builtin/rev-list.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,7 @@ int cmd_rev_list(int argc,
542542
const char *show_progress = NULL;
543543
int ret = 0;
544544

545-
if (argc == 2 && !strcmp(argv[1], "-h"))
546-
usage(rev_list_usage);
545+
show_usage_if_asked(argc, argv, rev_list_usage);
547546

548547
git_config(git_default_config, NULL);
549548
repo_init_revisions(the_repository, &revs, prefix);

builtin/rev-parse.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,8 @@ int cmd_rev_parse(int argc,
713713
int seen_end_of_options = 0;
714714
enum format_type format = FORMAT_DEFAULT;
715715

716+
show_usage_if_asked(argc, argv, builtin_rev_parse_usage);
717+
716718
if (argc > 1 && !strcmp("--parseopt", argv[1]))
717719
return cmd_parseopt(argc - 1, argv + 1, prefix);
718720

builtin/unpack-objects.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ int cmd_unpack_objects(int argc,
619619

620620
quiet = !isatty(2);
621621

622+
show_usage_if_asked(argc, argv, unpack_usage);
623+
622624
for (i = 1 ; i < argc; i++) {
623625
const char *arg = argv[i];
624626

builtin/upload-archive.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ struct repository *repo UNUSED)
9393

9494
BUG_ON_NON_EMPTY_PREFIX(prefix);
9595

96-
if (argc == 2 && !strcmp(argv[1], "-h"))
97-
usage(upload_archive_usage);
96+
show_usage_if_asked(argc, argv, upload_archive_usage);
9897

9998
/*
10099
* Set up sideband subprocess.

t/t0012-help.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,8 @@ do
257257
export GIT_CEILING_DIRECTORIES &&
258258
test_expect_code 129 git -C sub $builtin -h >output 2>err
259259
) &&
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
268262
'
269263
done <builtins
270264

0 commit comments

Comments
 (0)