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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 16, 2025 at 13:35 UTC a36a822d7d42f36baf0b3d7a0af5691ce692ce20
5 files changed +13 -4
builtin/credential.c
+2 -1
@@ -18,7 +18,8 @@ int cmd_credential(int argc,
18
19 git_config(git_default_config, NULL);
20
21 - if (argc != 2 || !strcmp(argv[1], "-h"))
21 + show_usage_if_asked(argc, argv, usage_msg);
22 + if (argc != 2)
23 usage(usage_msg);
24 op = argv[1];
25
builtin/fetch-pack.c
+2
@@ -75,6 +75,8 @@ int cmd_fetch_pack(int argc,
75 list_objects_filter_init(&args.filter_options);
76 args.uploadpack = "git-upload-pack";
77
78 + show_usage_if_asked(argc, argv, fetch_pack_usage);
79 +
80 for (i = 1; i < argc && *argv[i] == '-'; i++) {
81 const char *arg = argv[i];
82
builtin/unpack-file.c
+6 -2
@@ -26,6 +26,9 @@ static char *create_temp_file(struct object_id *oid)
26 return path;
27 }
28
29 +static const char usage_msg[] =
30 +"git unpack-file <blob>";
31 +
32 int cmd_unpack_file(int argc,
33 const char **argv,
34 const char *prefix UNUSED,
@@ -33,8 +36,9 @@ int cmd_unpack_file(int argc,
36 {
37 struct object_id oid;
38
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);
42 if (repo_get_oid(the_repository, argv[1], &oid))
43 die("Not a valid object name %s", argv[1]);
44
builtin/upload-archive.c
+2 -1
@@ -27,7 +27,8 @@ int cmd_upload_archive_writer(int argc,
27 const char *arg_cmd = "argument ";
28 int ret;
29
30 - if (argc != 2 || !strcmp(argv[1], "-h"))
30 + show_usage_if_asked(argc, argv, upload_archive_usage);
31 + if (argc != 2)
32 usage(upload_archive_usage);
33
34 if (!enter_repo(argv[1], 0))
builtin/var.c
+1
@@ -221,6 +221,7 @@ int cmd_var(int argc,
221 const struct git_var *git_var;
222 char *val;
223
224 + show_usage_if_asked(argc, argv, var_usage);
225 if (argc != 2)
226 usage(var_usage);
227