Skip to content

Commit

Permalink
Merge branch 'jc/show-usage-help' into seen
Browse files Browse the repository at this point in the history
* jc/show-usage-help:
  builtin: send usage() help text to standard output
  oddballs: send usage() help text to standard output
  usage: add show_usage_and_exit_if_asked()
  builtins: send usage_with_options() help text to standard output
  t0012: optionally check that "-h" output goes to stdout
  parse-options: add show_usage_help_and_exit_if_asked()
  • Loading branch information
gitster committed Jan 16, 2025
2 parents 6293018 + 2d4967c commit 5c2c4e0
Show file tree
Hide file tree
Showing 42 changed files with 125 additions and 66 deletions.
3 changes: 1 addition & 2 deletions builtin/am.c
Original file line number Diff line number Diff line change
Expand Up @@ -2427,8 +2427,7 @@ int cmd_am(int argc,
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(usage, options);
show_usage_help_and_exit_if_asked(argc, argv, usage, options);

git_config(git_default_config, NULL);

Expand Down
4 changes: 2 additions & 2 deletions builtin/backfill.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ int cmd_backfill(int argc, const char **argv, const char *prefix, struct reposit
OPT_END(),
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_backfill_usage, options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_backfill_usage, options);

argc = parse_options(argc, argv, prefix, options, builtin_backfill_usage,
0);
Expand Down
4 changes: 2 additions & 2 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,8 @@ int cmd_branch(int argc,
filter.kind = FILTER_REFS_BRANCHES;
filter.abbrev = -1;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_branch_usage, options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_branch_usage, options);

/*
* Try to set sort keys from config. If config does not set any,
Expand Down
4 changes: 2 additions & 2 deletions builtin/check-ref-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv,
builtin_check_ref_format_usage);

if (argc == 3 && !strcmp(argv[1], "--branch"))
return check_ref_format_branch(argv[2]);
Expand Down
6 changes: 3 additions & 3 deletions builtin/checkout--worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ int cmd_checkout__worker(int argc,
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(checkout_worker_usage,
checkout_worker_options);
show_usage_help_and_exit_if_asked(argc, argv,
checkout_worker_usage,
checkout_worker_options);

git_config(git_default_config, NULL);
argc = parse_options(argc, argv, prefix, checkout_worker_options,
Expand Down
6 changes: 3 additions & 3 deletions builtin/checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ int cmd_checkout_index(int argc,
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_checkout_index_usage,
builtin_checkout_index_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_checkout_index_usage,
builtin_checkout_index_options);
git_config(git_default_config, NULL);
prefix_length = prefix ? strlen(prefix) : 0;

Expand Down
4 changes: 2 additions & 2 deletions builtin/commit-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ int cmd_commit_tree(int argc,

git_config(git_default_config, NULL);

if (argc < 2 || !strcmp(argv[1], "-h"))
usage_with_options(commit_tree_usage, options);
show_usage_help_and_exit_if_asked(argc, argv,
commit_tree_usage, options);

argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);

Expand Down
8 changes: 4 additions & 4 deletions builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1559,8 +1559,8 @@ struct repository *repo UNUSED)
OPT_END(),
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_status_usage, builtin_status_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_status_usage, builtin_status_options);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
Expand Down Expand Up @@ -1736,8 +1736,8 @@ int cmd_commit(int argc,
struct strbuf err = STRBUF_INIT;
int ret = 0;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_commit_usage, builtin_commit_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_commit_usage, builtin_commit_options);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
Expand Down
3 changes: 2 additions & 1 deletion builtin/credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ int cmd_credential(int argc,

git_config(git_default_config, NULL);

if (argc != 2 || !strcmp(argv[1], "-h"))
show_usage_and_exit_if_asked(argc, argv, usage_msg);
if (argc != 2)
usage(usage_msg);
op = argv[1];

Expand Down
3 changes: 1 addition & 2 deletions builtin/diff-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, diff_files_usage);

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

Expand Down
3 changes: 1 addition & 2 deletions builtin/diff-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, diff_cache_usage);

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

Expand Down
3 changes: 1 addition & 2 deletions builtin/diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, diff_tree_usage);

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

Expand Down
3 changes: 1 addition & 2 deletions builtin/fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, fast_import_usage);

reset_pack_idx_option(&pack_idx_opts);
git_pack_config();
Expand Down
3 changes: 3 additions & 0 deletions builtin/fetch-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ int cmd_fetch_pack(int argc,
list_objects_filter_set_no_filter(&args.filter_options);
continue;
}

if (!strcmp(arg, "-h"))
show_usage_and_exit_if_asked(2, &arg - 1, fetch_pack_usage);

Check failure on line 181 in builtin/fetch-pack.c

View workflow job for this annotation

GitHub Actions / linux-breaking-changes (ubuntu:rolling)

builtin/fetch-pack.c:181:25: array subscript -1 is outside array bounds of ‘const char[8]’ [-Werror=array-bounds=]

Check failure on line 181 in builtin/fetch-pack.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora:latest)

builtin/fetch-pack.c:181:25: array subscript -1 is outside array bounds of 'const char[8]' [-Werror=array-bounds=]

Check failure on line 181 in builtin/fetch-pack.c

View workflow job for this annotation

GitHub Actions / win build

builtin/fetch-pack.c:181:25: array subscript -1 is outside array bounds of 'const char[8]' [-Werror=array-bounds=]
usage(fetch_pack_usage);
}
if (deepen_not.nr)
Expand Down
4 changes: 2 additions & 2 deletions builtin/fsmonitor--daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1598,8 +1598,8 @@ int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix UNUSED
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_fsmonitor__daemon_usage, options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_fsmonitor__daemon_usage, options);

die(_("fsmonitor--daemon not supported on this platform"));
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ struct repository *repo UNUSED)
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_gc_usage, builtin_gc_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_gc_usage, builtin_gc_options);

strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
strvec_pushl(&repack, "repack", "-d", "-l", NULL);
Expand Down
4 changes: 3 additions & 1 deletion builtin/get-tar-commit-id.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -27,6 +27,8 @@ int cmd_get_tar_commit_id(int argc,

BUG_ON_NON_EMPTY_PREFIX(prefix);

show_usage_and_exit_if_asked(argc, argv, builtin_get_tar_commit_id_usage);

if (argc != 1)
usage(builtin_get_tar_commit_id_usage);

Expand Down
3 changes: 1 addition & 2 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1900,8 +1900,7 @@ int cmd_index_pack(int argc,
*/
fetch_if_missing = 0;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage(index_pack_usage);
show_usage_and_exit_if_asked(argc, argv, index_pack_usage);

disable_replace_refs();
fsck_options.walk = mark_link;
Expand Down
4 changes: 2 additions & 2 deletions builtin/ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ int cmd_ls_files(int argc,
};
int ret = 0;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(ls_files_usage, builtin_ls_files_options);
show_usage_help_and_exit_if_asked(argc, argv,
ls_files_usage, builtin_ls_files_options);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
Expand Down
4 changes: 2 additions & 2 deletions builtin/mailsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ int cmd_mailsplit(int argc,

BUG_ON_NON_EMPTY_PREFIX(prefix);

show_usage_and_exit_if_asked(argc, argv, git_mailsplit_usage);

for (argp = argv+1; *argp; argp++) {
const char *arg = *argp;

Expand All @@ -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")) {
Expand Down
7 changes: 6 additions & 1 deletion builtin/merge-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -87,8 +90,10 @@ int cmd_merge_index(int argc,
*/
signal(SIGCHLD, SIG_DFL);

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

Expand Down
3 changes: 1 addition & 2 deletions builtin/merge-ours.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, builtin_merge_ours_usage);

/*
* The contents of the current index becomes the tree we
Expand Down
6 changes: 6 additions & 0 deletions builtin/merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, msg.buf);
}

if (argc < 4)
usagef(builtin_merge_recursive_usage, argv[0]);

Expand Down
4 changes: 2 additions & 2 deletions builtin/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,8 +1300,8 @@ int cmd_merge(int argc,
void *branch_to_free;
int orig_argc = argc;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_merge_usage, builtin_merge_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_merge_usage, builtin_merge_options);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
Expand Down
3 changes: 1 addition & 2 deletions builtin/pack-redundant.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, pack_redundant_usage);

for (i = 1; i < argc; i++) {
const char *arg = argv[i];
Expand Down
6 changes: 3 additions & 3 deletions builtin/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,9 +1223,9 @@ int cmd_rebase(int argc,
};
int i;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(builtin_rebase_usage,
builtin_rebase_options);
show_usage_help_and_exit_if_asked(argc, argv,
builtin_rebase_usage,
builtin_rebase_options);

prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
Expand Down
2 changes: 2 additions & 0 deletions builtin/remote-ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ int cmd_remote_ext(int argc,
{
BUG_ON_NON_EMPTY_PREFIX(prefix);

show_usage_and_exit_if_asked(argc, argv, usage_msg);

if (argc != 3)
usage(usage_msg);

Expand Down
1 change: 1 addition & 0 deletions builtin/remote-fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int cmd_remote_fd(int argc,

BUG_ON_NON_EMPTY_PREFIX(prefix);

show_usage_and_exit_if_asked(argc, argv, usage_msg);
if (argc != 3)
usage(usage_msg);

Expand Down
3 changes: 1 addition & 2 deletions builtin/rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_and_exit_if_asked(argc, argv, rev_list_usage);

git_config(git_default_config, NULL);
repo_init_revisions(the_repository, &revs, prefix);
Expand Down
2 changes: 2 additions & 0 deletions builtin/rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ int cmd_rev_parse(int argc,
int seen_end_of_options = 0;
enum format_type format = FORMAT_DEFAULT;

show_usage_and_exit_if_asked(argc, argv, builtin_rev_parse_usage);

if (argc > 1 && !strcmp("--parseopt", argv[1]))
return cmd_parseopt(argc - 1, argv + 1, prefix);

Expand Down
8 changes: 6 additions & 2 deletions builtin/unpack-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ static char *create_temp_file(struct object_id *oid)
return path;
}

static const char usage_msg[] =
"git unpack-file <blob>";

int cmd_unpack_file(int argc,
const char **argv,
const char *prefix UNUSED,
struct repository *repo UNUSED)
{
struct object_id oid;

if (argc != 2 || !strcmp(argv[1], "-h"))
usage("git unpack-file <blob>");
show_usage_and_exit_if_asked(argc, argv, usage_msg);
if (argc != 2)
usage(usage_msg);
if (repo_get_oid(the_repository, argv[1], &oid))
die("Not a valid object name %s", argv[1]);

Expand Down
2 changes: 2 additions & 0 deletions builtin/unpack-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ int cmd_unpack_objects(int argc,

quiet = !isatty(2);

show_usage_and_exit_if_asked(argc, argv, unpack_usage);

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

Expand Down
4 changes: 2 additions & 2 deletions builtin/update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,8 @@ int cmd_update_index(int argc,
OPT_END()
};

if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(update_index_usage, options);
show_usage_help_and_exit_if_asked(argc, argv,
update_index_usage, options);

git_config(git_default_config, NULL);

Expand Down
Loading

0 comments on commit 5c2c4e0

Please sign in to comment.