1
0
mirror of https://github.com/git/git.git synced 2025-02-06 09:44:30 +00:00

Merge branch 'jc/show-usage-help'

The help text from "git $cmd -h" appear on the standard output for
some $cmd and the standard error for others.  The built-in commands
have been fixed to show them on the standard output consistently.

* jc/show-usage-help:
  builtin: send usage() help text to standard output
  oddballs: send usage() help text to standard output
  builtins: send usage_with_options() help text to standard output
  usage: add show_usage_if_asked()
  parse-options: add show_usage_with_options_if_asked()
  t0012: optionally check that "-h" output goes to stdout
This commit is contained in:
Junio C Hamano 2025-01-28 13:02:22 -08:00
commit f0a371a39d
41 changed files with 121 additions and 64 deletions

View File

@ -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_with_options_if_asked(argc, argv, usage, options);
git_config(git_default_config, NULL);

View File

@ -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_with_options_if_asked(argc, argv,
builtin_branch_usage, options);
/*
* Try to set sort keys from config. If config does not set any,

View 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]);

View File

@ -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_with_options_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,

View File

@ -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_with_options_if_asked(argc, argv,
builtin_checkout_index_usage,
builtin_checkout_index_options);
git_config(git_default_config, NULL);
prefix_length = prefix ? strlen(prefix) : 0;

View File

@ -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_with_options_if_asked(argc, argv,
commit_tree_usage, options);
argc = parse_options(argc, argv, prefix, options, commit_tree_usage, 0);

View File

@ -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_with_options_if_asked(argc, argv,
builtin_status_usage, builtin_status_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;
@ -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_with_options_if_asked(argc, argv,
builtin_commit_usage, builtin_commit_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

View File

@ -18,7 +18,8 @@ int cmd_credential(int argc,
git_config(git_default_config, NULL);
if (argc != 2 || !strcmp(argv[1], "-h"))
show_usage_if_asked(argc, argv, usage_msg);
if (argc != 2)
usage(usage_msg);
op = argv[1];

View 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 */

View 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 */

View 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 */

View 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();

View File

@ -75,6 +75,8 @@ int cmd_fetch_pack(int argc,
list_objects_filter_init(&args.filter_options);
args.uploadpack = "git-upload-pack";
show_usage_if_asked(argc, argv, fetch_pack_usage);
for (i = 1; i < argc && *argv[i] == '-'; i++) {
const char *arg = argv[i];

View File

@ -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_with_options_if_asked(argc, argv,
builtin_fsmonitor__daemon_usage, options);
die(_("fsmonitor--daemon not supported on this platform"));
}

View File

@ -710,8 +710,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_with_options_if_asked(argc, argv,
builtin_gc_usage, builtin_gc_options);
strvec_pushl(&reflog, "reflog", "expire", "--all", NULL);
strvec_pushl(&repack, "repack", "-d", "-l", NULL);

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

View File

@ -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_if_asked(argc, argv, index_pack_usage);
disable_replace_refs();
fsck_options.walk = mark_link;

View File

@ -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_with_options_if_asked(argc, argv,
ls_files_usage, builtin_ls_files_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

View 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")) {

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

View 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

View 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]);

View File

@ -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_with_options_if_asked(argc, argv,
builtin_merge_usage, builtin_merge_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

View 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];

View File

@ -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_with_options_if_asked(argc, argv,
builtin_rebase_usage,
builtin_rebase_options);
prepare_repo_settings(the_repository);
the_repository->settings.command_requires_full_index = 0;

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

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

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

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

View File

@ -26,6 +26,9 @@ 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,
@ -33,8 +36,9 @@ int cmd_unpack_file(int argc,
{
struct object_id oid;
if (argc != 2 || !strcmp(argv[1], "-h"))
usage("git unpack-file <blob>");
show_usage_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]);

View File

@ -620,6 +620,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];

View File

@ -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_with_options_if_asked(argc, argv,
update_index_usage, options);
git_config(git_default_config, NULL);

View File

@ -27,7 +27,8 @@ int cmd_upload_archive_writer(int argc,
const char *arg_cmd = "argument ";
int ret;
if (argc != 2 || !strcmp(argv[1], "-h"))
show_usage_if_asked(argc, argv, upload_archive_usage);
if (argc != 2)
usage(upload_archive_usage);
if (!enter_repo(argv[1], 0))
@ -92,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.

View File

@ -221,6 +221,7 @@ int cmd_var(int argc,
const struct git_var *git_var;
char *val;
show_usage_if_asked(argc, argv, var_usage);
if (argc != 2)
usage(var_usage);

View File

@ -701,6 +701,8 @@ int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
void show_usage_if_asked(int ac, const char **av, const char *err);
#ifndef NO_OPENSSL
#ifdef APPLE_COMMON_CRYPTO
#include "compat/apple-common-crypto.h"

View File

@ -1319,6 +1319,16 @@ void NORETURN usage_with_options(const char * const *usagestr,
exit(129);
}
void show_usage_with_options_if_asked(int ac, const char **av,
const char * const *usagestr,
const struct option *opts)
{
if (ac == 2 && !strcmp(av[1], "-h")) {
usage_with_options_internal(NULL, usagestr, opts, 0, 0);
exit(129);
}
}
void NORETURN usage_msg_opt(const char *msg,
const char * const *usagestr,
const struct option *options)

View File

@ -402,6 +402,10 @@ int parse_options(int argc, const char **argv, const char *prefix,
NORETURN void usage_with_options(const char * const *usagestr,
const struct option *options);
void show_usage_with_options_if_asked(int ac, const char **av,
const char * const *usage,
const struct option *options);
NORETURN void usage_msg_opt(const char *msg,
const char * const *usagestr,
const struct option *options);

View File

@ -612,8 +612,8 @@ int cmd__simple_ipc(int argc, const char **argv)
if (argc < 2)
usage_with_options(simple_ipc_usage, options);
if (argc == 2 && !strcmp(argv[1], "-h"))
usage_with_options(simple_ipc_usage, options);
show_usage_with_options_if_asked(argc, argv,
simple_ipc_usage, options);
if (argc == 2 && !strcmp(argv[1], "SUPPORTS_SIMPLE_IPC"))
return 0;

View File

@ -255,8 +255,9 @@ do
(
GIT_CEILING_DIRECTORIES=$(pwd) &&
export GIT_CEILING_DIRECTORIES &&
test_expect_code 129 git -C sub $builtin -h >output 2>&1
test_expect_code 129 git -C sub $builtin -h >output 2>err
) &&
test_must_be_empty err &&
test_grep usage output
'
done <builtins

View File

@ -173,7 +173,7 @@ test_expect_success 'merge -h with invalid index' '
cd broken &&
git init &&
>.git/index &&
test_expect_code 129 git merge -h 2>usage
test_expect_code 129 git merge -h >usage
) &&
test_grep "[Uu]sage: git merge" broken/usage
'

27
usage.c
View File

@ -8,7 +8,7 @@
#include "gettext.h"
#include "trace2.h"
static void vreportf(const char *prefix, const char *err, va_list params)
static void vfreportf(FILE *f, const char *prefix, const char *err, va_list params)
{
char msg[4096];
char *p, *pend = msg + sizeof(msg);
@ -32,8 +32,13 @@ static void vreportf(const char *prefix, const char *err, va_list params)
}
*(p++) = '\n'; /* we no longer need a NUL */
fflush(stderr);
write_in_full(2, msg, p - msg);
fflush(f);
write_in_full(fileno(f), msg, p - msg);
}
static void vreportf(const char *prefix, const char *err, va_list params)
{
vfreportf(stderr, prefix, err, params);
}
static NORETURN void usage_builtin(const char *err, va_list params)
@ -173,6 +178,22 @@ void NORETURN usage(const char *err)
usagef("%s", err);
}
static void show_usage_if_asked_helper(const char *err, ...)
{
va_list params;
va_start(params, err);
vfreportf(stdout, _("usage: "), err, params);
va_end(params);
exit(129);
}
void show_usage_if_asked(int ac, const char **av, const char *err)
{
if (ac == 2 && !strcmp(av[1], "-h"))
show_usage_if_asked_helper(err);
}
void NORETURN die(const char *err, ...)
{
va_list params;