1
0
mirror of https://github.com/git/git synced 2024-07-05 00:58:49 +00:00

Merge branch 'jk/consistent-h'

"git $cmd -h" for builtin commands calls the implementation of the
command (i.e. cmd_$cmd() function) without doing any repository
set-up, and the commands that expect RUN_SETUP is done by the Git
potty needs to be prepared to show the help text without barfing.

* jk/consistent-h:
  t0012: test "-h" with builtins
  git: add hidden --list-builtins option
  version: convert to parse-options
  diff- and log- family: handle "git cmd -h" early
  submodule--helper: show usage for "-h"
  remote-{ext,fd}: print usage message on invalid arguments
  upload-archive: handle "-h" option early
  credential: handle invalid arguments earlier
This commit is contained in:
Junio C Hamano 2017-06-19 12:38:45 -07:00
commit 50ad8561de
12 changed files with 72 additions and 13 deletions

View File

@ -10,9 +10,9 @@ int cmd_credential(int argc, const char **argv, const char *prefix)
const char *op;
struct credential c = CREDENTIAL_INIT;
op = argv[1];
if (!op)
if (argc != 2 || !strcmp(argv[1], "-h"))
usage(usage_msg);
op = argv[1];
if (credential_read(&c, stdin) < 0)
die("unable to read credential from stdin");

View File

@ -20,6 +20,9 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
int result;
unsigned options = 0;
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(diff_files_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
init_revisions(&rev, prefix);
gitmodules_config();

View File

@ -17,6 +17,9 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
int i;
int result;
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(diff_cache_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
init_revisions(&rev, prefix);
gitmodules_config();

View File

@ -104,6 +104,9 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
struct setup_revision_opt s_r_opt;
int read_stdin = 0;
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(diff_tree_usage);
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
init_revisions(opt, prefix);
gitmodules_config();

View File

@ -3,6 +3,9 @@
#include "run-command.h"
#include "pkt-line.h"
static const char usage_msg[] =
"git remote-ext <remote> <url>";
/*
* URL syntax:
* 'command [arg1 [arg2 [...]]]' Invoke command with given arguments.
@ -193,7 +196,7 @@ static int command_loop(const char *child)
int cmd_remote_ext(int argc, const char **argv, const char *prefix)
{
if (argc != 3)
die("Expected two arguments");
usage(usage_msg);
return command_loop(argv[2]);
}

View File

@ -1,6 +1,9 @@
#include "builtin.h"
#include "transport.h"
static const char usage_msg[] =
"git remote-fd <remote> <url>";
/*
* URL syntax:
* 'fd::<inoutfd>[/<anything>]' Read/write socket pair
@ -57,7 +60,7 @@ int cmd_remote_fd(int argc, const char **argv, const char *prefix)
char *end;
if (argc != 3)
die("Expected two arguments");
usage(usage_msg);
input_fd = (int)strtoul(argv[2], &end, 10);

View File

@ -277,6 +277,9 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
int use_bitmap_index = 0;
const char *show_progress = NULL;
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(rev_list_usage);
git_config(git_default_config, NULL);
init_revisions(&revs, prefix);
revs.abbrev = DEFAULT_ABBREV;

View File

@ -1221,9 +1221,8 @@ static struct cmd_struct commands[] = {
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
{
int i;
if (argc < 2)
die(_("submodule--helper subcommand must be "
"called with a subcommand"));
if (argc < 2 || !strcmp(argv[1], "-h"))
usage("git submodule--helper <command>");
for (i = 0; i < ARRAY_SIZE(commands); i++) {
if (!strcmp(argv[1], commands[i].cmd)) {

View File

@ -22,7 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
struct argv_array sent_argv = ARGV_ARRAY_INIT;
const char *arg_cmd = "argument ";
if (argc != 2)
if (argc != 2 || !strcmp(argv[1], "-h"))
usage(upload_archive_usage);
if (!enter_repo(argv[1], 0))
@ -76,6 +76,9 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
{
struct child_process writer = { argv };
if (argc == 2 && !strcmp(argv[1], "-h"))
usage(upload_archive_usage);
/*
* Set up sideband subprocess.
*

12
git.c
View File

@ -26,6 +26,8 @@ static const char *env_names[] = {
static char *orig_env[4];
static int save_restore_env_balance;
static void list_builtins(void);
static void save_env_before_alias(void)
{
int i;
@ -232,6 +234,9 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
}
(*argv)++;
(*argc)--;
} else if (!strcmp(cmd, "--list-builtins")) {
list_builtins();
exit(0);
} else {
fprintf(stderr, "Unknown option: %s\n", cmd);
usage(git_usage_string);
@ -529,6 +534,13 @@ int is_builtin(const char *s)
return !!get_builtin(s);
}
static void list_builtins(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(commands); i++)
printf("%s\n", commands[i].cmd);
}
#ifdef STRIP_EXTENSION
static void strip_extension(const char **argv)
{

25
help.c
View File

@ -9,6 +9,7 @@
#include "column.h"
#include "version.h"
#include "refs.h"
#include "parse-options.h"
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
{
@ -383,16 +384,30 @@ const char *help_unknown_cmd(const char *cmd)
int cmd_version(int argc, const char **argv, const char *prefix)
{
int build_options = 0;
const char * const usage[] = {
N_("git version [<options>]"),
NULL
};
struct option options[] = {
OPT_BOOL(0, "build-options", &build_options,
"also print build options"),
OPT_END()
};
argc = parse_options(argc, argv, prefix, options, usage, 0);
/*
* The format of this string should be kept stable for compatibility
* with external projects that rely on the output of "git version".
*
* Always show the version, even if other options are given.
*/
printf("git version %s\n", git_version_string);
while (*++argv) {
if (!strcmp(*argv, "--build-options")) {
printf("sizeof-long: %d\n", (int)sizeof(long));
/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
}
if (build_options) {
printf("sizeof-long: %d\n", (int)sizeof(long));
/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
}
return 0;
}

View File

@ -49,4 +49,16 @@ test_expect_success "--help does not work for guides" "
test_i18ncmp expect actual
"
test_expect_success 'generate builtin list' '
git --list-builtins >builtins
'
while read builtin
do
test_expect_success "$builtin can handle -h" '
test_expect_code 129 git $builtin -h >output 2>&1 &&
test_i18ngrep usage output
'
done <builtins
test_done