Skip to content

Commit 50ad856

Browse files
committed
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
2 parents 06959fe + d691551 commit 50ad856

File tree

12 files changed

+72
-13
lines changed

12 files changed

+72
-13
lines changed

builtin/credential.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ int cmd_credential(int argc, const char **argv, const char *prefix)
1010
const char *op;
1111
struct credential c = CREDENTIAL_INIT;
1212

13-
op = argv[1];
14-
if (!op)
13+
if (argc != 2 || !strcmp(argv[1], "-h"))
1514
usage(usage_msg);
15+
op = argv[1];
1616

1717
if (credential_read(&c, stdin) < 0)
1818
die("unable to read credential from stdin");

builtin/diff-files.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ int cmd_diff_files(int argc, const char **argv, const char *prefix)
2020
int result;
2121
unsigned options = 0;
2222

23+
if (argc == 2 && !strcmp(argv[1], "-h"))
24+
usage(diff_files_usage);
25+
2326
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
2427
init_revisions(&rev, prefix);
2528
gitmodules_config();

builtin/diff-index.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ int cmd_diff_index(int argc, const char **argv, const char *prefix)
1717
int i;
1818
int result;
1919

20+
if (argc == 2 && !strcmp(argv[1], "-h"))
21+
usage(diff_cache_usage);
22+
2023
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
2124
init_revisions(&rev, prefix);
2225
gitmodules_config();

builtin/diff-tree.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
104104
struct setup_revision_opt s_r_opt;
105105
int read_stdin = 0;
106106

107+
if (argc == 2 && !strcmp(argv[1], "-h"))
108+
usage(diff_tree_usage);
109+
107110
git_config(git_diff_basic_config, NULL); /* no "diff" UI options */
108111
init_revisions(opt, prefix);
109112
gitmodules_config();

builtin/remote-ext.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "run-command.h"
44
#include "pkt-line.h"
55

6+
static const char usage_msg[] =
7+
"git remote-ext <remote> <url>";
8+
69
/*
710
* URL syntax:
811
* 'command [arg1 [arg2 [...]]]' Invoke command with given arguments.
@@ -193,7 +196,7 @@ static int command_loop(const char *child)
193196
int cmd_remote_ext(int argc, const char **argv, const char *prefix)
194197
{
195198
if (argc != 3)
196-
die("Expected two arguments");
199+
usage(usage_msg);
197200

198201
return command_loop(argv[2]);
199202
}

builtin/remote-fd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "builtin.h"
22
#include "transport.h"
33

4+
static const char usage_msg[] =
5+
"git remote-fd <remote> <url>";
6+
47
/*
58
* URL syntax:
69
* 'fd::<inoutfd>[/<anything>]' Read/write socket pair
@@ -57,7 +60,7 @@ int cmd_remote_fd(int argc, const char **argv, const char *prefix)
5760
char *end;
5861

5962
if (argc != 3)
60-
die("Expected two arguments");
63+
usage(usage_msg);
6164

6265
input_fd = (int)strtoul(argv[2], &end, 10);
6366

builtin/rev-list.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
277277
int use_bitmap_index = 0;
278278
const char *show_progress = NULL;
279279

280+
if (argc == 2 && !strcmp(argv[1], "-h"))
281+
usage(rev_list_usage);
282+
280283
git_config(git_default_config, NULL);
281284
init_revisions(&revs, prefix);
282285
revs.abbrev = DEFAULT_ABBREV;

builtin/submodule--helper.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,8 @@ static struct cmd_struct commands[] = {
12211221
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
12221222
{
12231223
int i;
1224-
if (argc < 2)
1225-
die(_("submodule--helper subcommand must be "
1226-
"called with a subcommand"));
1224+
if (argc < 2 || !strcmp(argv[1], "-h"))
1225+
usage("git submodule--helper <command>");
12271226

12281227
for (i = 0; i < ARRAY_SIZE(commands); i++) {
12291228
if (!strcmp(argv[1], commands[i].cmd)) {

builtin/upload-archive.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
2222
struct argv_array sent_argv = ARGV_ARRAY_INIT;
2323
const char *arg_cmd = "argument ";
2424

25-
if (argc != 2)
25+
if (argc != 2 || !strcmp(argv[1], "-h"))
2626
usage(upload_archive_usage);
2727

2828
if (!enter_repo(argv[1], 0))
@@ -76,6 +76,9 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
7676
{
7777
struct child_process writer = { argv };
7878

79+
if (argc == 2 && !strcmp(argv[1], "-h"))
80+
usage(upload_archive_usage);
81+
7982
/*
8083
* Set up sideband subprocess.
8184
*

git.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ static const char *env_names[] = {
2626
static char *orig_env[4];
2727
static int save_restore_env_balance;
2828

29+
static void list_builtins(void);
30+
2931
static void save_env_before_alias(void)
3032
{
3133
int i;
@@ -232,6 +234,9 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
232234
}
233235
(*argv)++;
234236
(*argc)--;
237+
} else if (!strcmp(cmd, "--list-builtins")) {
238+
list_builtins();
239+
exit(0);
235240
} else {
236241
fprintf(stderr, "Unknown option: %s\n", cmd);
237242
usage(git_usage_string);
@@ -529,6 +534,13 @@ int is_builtin(const char *s)
529534
return !!get_builtin(s);
530535
}
531536

537+
static void list_builtins(void)
538+
{
539+
int i;
540+
for (i = 0; i < ARRAY_SIZE(commands); i++)
541+
printf("%s\n", commands[i].cmd);
542+
}
543+
532544
#ifdef STRIP_EXTENSION
533545
static void strip_extension(const char **argv)
534546
{

0 commit comments

Comments
 (0)