Skip to content

Commit a36a822

Browse files
committed
oddballs: send usage() help text to standard output
Using the show_usage_if_asked() helper we introduced earlier, fix callers of usage() that want to show the help text when explicitly asked by the end-user. The help text now goes to the standard output stream for them. The callers in this step are oddballs in that their invocations of usage() are *not* guarded by if (argc == 2 && !strcmp(argv[1], "-h") usage(...); There are (unnecessarily) being clever ones that do things like if (argc != 2 || !strcmp(argv[1], "-h") usage(...); to say "I know I take only one argument, so argc != 2 is always an error regardless of what is in argv[]. Ah, by the way, even if argc is 2, "-h" is a request for usage text, so we do the same". Some like "git var -h" just do not treat "-h" any specially, and let it take the same error code paths as a parameter error. Now we cannot do the same, so these callers are rewrittin to do the show_usage_and_exit_if_asked() first and then handle the usage error the way they used to. Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b821c99 commit a36a822

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

builtin/credential.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ int cmd_credential(int argc,
1818

1919
git_config(git_default_config, NULL);
2020

21-
if (argc != 2 || !strcmp(argv[1], "-h"))
21+
show_usage_if_asked(argc, argv, usage_msg);
22+
if (argc != 2)
2223
usage(usage_msg);
2324
op = argv[1];
2425

builtin/fetch-pack.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ int cmd_fetch_pack(int argc,
7575
list_objects_filter_init(&args.filter_options);
7676
args.uploadpack = "git-upload-pack";
7777

78+
show_usage_if_asked(argc, argv, fetch_pack_usage);
79+
7880
for (i = 1; i < argc && *argv[i] == '-'; i++) {
7981
const char *arg = argv[i];
8082

builtin/unpack-file.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ static char *create_temp_file(struct object_id *oid)
2626
return path;
2727
}
2828

29+
static const char usage_msg[] =
30+
"git unpack-file <blob>";
31+
2932
int cmd_unpack_file(int argc,
3033
const char **argv,
3134
const char *prefix UNUSED,
3235
struct repository *repo UNUSED)
3336
{
3437
struct object_id oid;
3538

36-
if (argc != 2 || !strcmp(argv[1], "-h"))
37-
usage("git unpack-file <blob>");
39+
show_usage_if_asked(argc, argv, usage_msg);
40+
if (argc != 2)
41+
usage(usage_msg);
3842
if (repo_get_oid(the_repository, argv[1], &oid))
3943
die("Not a valid object name %s", argv[1]);
4044

builtin/upload-archive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ int cmd_upload_archive_writer(int argc,
2727
const char *arg_cmd = "argument ";
2828
int ret;
2929

30-
if (argc != 2 || !strcmp(argv[1], "-h"))
30+
show_usage_if_asked(argc, argv, upload_archive_usage);
31+
if (argc != 2)
3132
usage(upload_archive_usage);
3233

3334
if (!enter_repo(argv[1], 0))

builtin/var.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ int cmd_var(int argc,
221221
const struct git_var *git_var;
222222
char *val;
223223

224+
show_usage_if_asked(argc, argv, var_usage);
224225
if (argc != 2)
225226
usage(var_usage);
226227

0 commit comments

Comments
 (0)