Skip to content

Commit

Permalink
Merge branch 'jk/consistent-h'
Browse files Browse the repository at this point in the history
"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
  • Loading branch information
gitster committed Jun 19, 2017
2 parents 06959fe + d691551 commit 50ad856
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 13 deletions.
4 changes: 2 additions & 2 deletions builtin/credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
3 changes: 3 additions & 0 deletions builtin/diff-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions builtin/diff-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions builtin/diff-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion builtin/remote-ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]);
}
5 changes: 4 additions & 1 deletion builtin/remote-fd.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions builtin/rev-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions builtin/submodule--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion builtin/upload-archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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.
*
Expand Down
12 changes: 12 additions & 0 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
25 changes: 20 additions & 5 deletions help.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 12 additions & 0 deletions t/t0012-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 50ad856

Please sign in to comment.