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