|
7 | 7 | #include "builtin.h" |
8 | 8 | #include "exec_cmd.h" |
9 | 9 | #include "common-cmds.h" |
10 | | - |
11 | | -static const char *help_default_format; |
12 | | - |
13 | | -static enum help_format { |
14 | | - man_format, |
15 | | - info_format, |
16 | | - web_format, |
17 | | -} help_format = man_format; |
18 | | - |
19 | | -static void parse_help_format(const char *format) |
| 10 | +#include "parse-options.h" |
| 11 | + |
| 12 | +enum help_format { |
| 13 | + HELP_FORMAT_MAN, |
| 14 | + HELP_FORMAT_INFO, |
| 15 | + HELP_FORMAT_WEB, |
| 16 | +}; |
| 17 | + |
| 18 | +static int show_all = 0; |
| 19 | +static enum help_format help_format = HELP_FORMAT_MAN; |
| 20 | +static struct option builtin_help_options[] = { |
| 21 | + OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), |
| 22 | + OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), |
| 23 | + OPT_SET_INT('w', "web", &help_format, "show manual in web browser", |
| 24 | + HELP_FORMAT_WEB), |
| 25 | + OPT_SET_INT('i', "info", &help_format, "show info page", |
| 26 | + HELP_FORMAT_INFO), |
| 27 | +}; |
| 28 | + |
| 29 | +static const char * const builtin_help_usage[] = { |
| 30 | + "git-help [--all] [--man|--web|--info] [command]", |
| 31 | + NULL |
| 32 | +}; |
| 33 | + |
| 34 | +static enum help_format parse_help_format(const char *format) |
20 | 35 | { |
21 | | - if (!format) { |
22 | | - help_format = man_format; |
23 | | - return; |
24 | | - } |
25 | | - if (!strcmp(format, "man")) { |
26 | | - help_format = man_format; |
27 | | - return; |
28 | | - } |
29 | | - if (!strcmp(format, "info")) { |
30 | | - help_format = info_format; |
31 | | - return; |
32 | | - } |
33 | | - if (!strcmp(format, "web") || !strcmp(format, "html")) { |
34 | | - help_format = web_format; |
35 | | - return; |
36 | | - } |
| 36 | + if (!strcmp(format, "man")) |
| 37 | + return HELP_FORMAT_MAN; |
| 38 | + if (!strcmp(format, "info")) |
| 39 | + return HELP_FORMAT_INFO; |
| 40 | + if (!strcmp(format, "web") || !strcmp(format, "html")) |
| 41 | + return HELP_FORMAT_WEB; |
37 | 42 | die("unrecognized help format '%s'", format); |
38 | 43 | } |
39 | 44 |
|
40 | 45 | static int git_help_config(const char *var, const char *value) |
41 | 46 | { |
42 | | - if (!strcmp(var, "help.format")) |
43 | | - return git_config_string(&help_default_format, var, value); |
| 47 | + if (!strcmp(var, "help.format")) { |
| 48 | + if (!value) |
| 49 | + return config_error_nonbool(var); |
| 50 | + help_format = parse_help_format(value); |
| 51 | + return 0; |
| 52 | + } |
44 | 53 | return git_default_config(var, value); |
45 | 54 | } |
46 | 55 |
|
@@ -201,7 +210,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds, |
201 | 210 | return longest; |
202 | 211 | } |
203 | 212 |
|
204 | | -static void list_commands(void) |
| 213 | +static unsigned int load_command_list(void) |
205 | 214 | { |
206 | 215 | unsigned int longest = 0; |
207 | 216 | unsigned int len; |
@@ -241,6 +250,14 @@ static void list_commands(void) |
241 | 250 | uniq(&other_cmds); |
242 | 251 | exclude_cmds(&other_cmds, &main_cmds); |
243 | 252 |
|
| 253 | + return longest; |
| 254 | +} |
| 255 | + |
| 256 | +static void list_commands(void) |
| 257 | +{ |
| 258 | + unsigned int longest = load_command_list(); |
| 259 | + const char *exec_path = git_exec_path(); |
| 260 | + |
244 | 261 | if (main_cmds.cnt) { |
245 | 262 | printf("available git commands in '%s'\n", exec_path); |
246 | 263 | printf("----------------------------"); |
@@ -275,6 +292,22 @@ void list_common_cmds_help(void) |
275 | 292 | } |
276 | 293 | } |
277 | 294 |
|
| 295 | +static int is_in_cmdlist(struct cmdnames *c, const char *s) |
| 296 | +{ |
| 297 | + int i; |
| 298 | + for (i = 0; i < c->cnt; i++) |
| 299 | + if (!strcmp(s, c->names[i]->name)) |
| 300 | + return 1; |
| 301 | + return 0; |
| 302 | +} |
| 303 | + |
| 304 | +static int is_git_command(const char *s) |
| 305 | +{ |
| 306 | + load_command_list(); |
| 307 | + return is_in_cmdlist(&main_cmds, s) || |
| 308 | + is_in_cmdlist(&other_cmds, s); |
| 309 | +} |
| 310 | + |
278 | 311 | static const char *cmd_to_page(const char *git_cmd) |
279 | 312 | { |
280 | 313 | if (!git_cmd) |
@@ -362,50 +395,43 @@ int cmd_version(int argc, const char **argv, const char *prefix) |
362 | 395 |
|
363 | 396 | int cmd_help(int argc, const char **argv, const char *prefix) |
364 | 397 | { |
365 | | - const char *help_cmd = argv[1]; |
| 398 | + int nongit; |
| 399 | + const char *alias; |
366 | 400 |
|
367 | | - if (argc < 2) { |
368 | | - printf("usage: %s\n\n", git_usage_string); |
369 | | - list_common_cmds_help(); |
370 | | - exit(0); |
371 | | - } |
| 401 | + setup_git_directory_gently(&nongit); |
| 402 | + git_config(git_help_config); |
| 403 | + |
| 404 | + argc = parse_options(argc, argv, builtin_help_options, |
| 405 | + builtin_help_usage, 0); |
372 | 406 |
|
373 | | - if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) { |
| 407 | + if (show_all) { |
374 | 408 | printf("usage: %s\n\n", git_usage_string); |
375 | 409 | list_commands(); |
| 410 | + return 0; |
376 | 411 | } |
377 | 412 |
|
378 | | - else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) { |
379 | | - show_html_page(argc > 2 ? argv[2] : NULL); |
380 | | - } |
381 | | - |
382 | | - else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) { |
383 | | - show_info_page(argc > 2 ? argv[2] : NULL); |
| 413 | + if (!argv[0]) { |
| 414 | + printf("usage: %s\n\n", git_usage_string); |
| 415 | + list_common_cmds_help(); |
| 416 | + return 0; |
384 | 417 | } |
385 | 418 |
|
386 | | - else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) { |
387 | | - show_man_page(argc > 2 ? argv[2] : NULL); |
| 419 | + alias = alias_lookup(argv[0]); |
| 420 | + if (alias && !is_git_command(argv[0])) { |
| 421 | + printf("`git %s' is aliased to `%s'\n", argv[0], alias); |
| 422 | + return 0; |
388 | 423 | } |
389 | 424 |
|
390 | | - else { |
391 | | - int nongit; |
392 | | - |
393 | | - setup_git_directory_gently(&nongit); |
394 | | - git_config(git_help_config); |
395 | | - if (help_default_format) |
396 | | - parse_help_format(help_default_format); |
397 | | - |
398 | | - switch (help_format) { |
399 | | - case man_format: |
400 | | - show_man_page(help_cmd); |
401 | | - break; |
402 | | - case info_format: |
403 | | - show_info_page(help_cmd); |
404 | | - break; |
405 | | - case web_format: |
406 | | - show_html_page(help_cmd); |
407 | | - break; |
408 | | - } |
| 425 | + switch (help_format) { |
| 426 | + case HELP_FORMAT_MAN: |
| 427 | + show_man_page(argv[0]); |
| 428 | + break; |
| 429 | + case HELP_FORMAT_INFO: |
| 430 | + show_info_page(argv[0]); |
| 431 | + break; |
| 432 | + case HELP_FORMAT_WEB: |
| 433 | + show_html_page(argv[0]); |
| 434 | + break; |
409 | 435 | } |
410 | 436 |
|
411 | 437 | return 0; |
|
0 commit comments