Git fork

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>

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