submodules: submodule paths must not contain symlinks
[git.git] / builtin / help.c
blob53f2812dfb1c935fed6b25570f99f13fcfa67f15
1 /*
2 * Builtin help command
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "builtin.h"
7 #include "exec-cmd.h"
8 #include "parse-options.h"
9 #include "run-command.h"
10 #include "config-list.h"
11 #include "help.h"
12 #include "alias.h"
14 #ifndef DEFAULT_HELP_FORMAT
15 #define DEFAULT_HELP_FORMAT "man"
16 #endif
18 static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21 } *man_viewer_list;
23 static struct man_viewer_info_list {
24 struct man_viewer_info_list *next;
25 const char *info;
26 char name[FLEX_ARRAY];
27 } *man_viewer_info_list;
29 enum help_format {
30 HELP_FORMAT_NONE,
31 HELP_FORMAT_MAN,
32 HELP_FORMAT_INFO,
33 HELP_FORMAT_WEB
36 enum show_config_type {
37 SHOW_CONFIG_HUMAN,
38 SHOW_CONFIG_VARS,
39 SHOW_CONFIG_SECTIONS,
42 static enum help_action {
43 HELP_ACTION_ALL = 1,
44 HELP_ACTION_GUIDES,
45 HELP_ACTION_CONFIG,
46 HELP_ACTION_USER_INTERFACES,
47 HELP_ACTION_DEVELOPER_INTERFACES,
48 HELP_ACTION_CONFIG_FOR_COMPLETION,
49 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION,
50 } cmd_mode;
52 static const char *html_path;
53 static int verbose = 1;
54 static enum help_format help_format = HELP_FORMAT_NONE;
55 static int exclude_guides;
56 static int show_external_commands = -1;
57 static int show_aliases = -1;
58 static struct option builtin_help_options[] = {
59 OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"),
60 HELP_ACTION_ALL),
61 OPT_BOOL(0, "external-commands", &show_external_commands,
62 N_("show external commands in --all")),
63 OPT_BOOL(0, "aliases", &show_aliases, N_("show aliases in --all")),
64 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
65 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
66 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
67 HELP_FORMAT_WEB),
68 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
69 HELP_FORMAT_INFO),
70 OPT__VERBOSE(&verbose, N_("print command description")),
72 OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"),
73 HELP_ACTION_GUIDES),
74 OPT_CMDMODE(0, "user-interfaces", &cmd_mode,
75 N_("print list of user-facing repository, command and file interfaces"),
76 HELP_ACTION_USER_INTERFACES),
77 OPT_CMDMODE(0, "developer-interfaces", &cmd_mode,
78 N_("print list of file formats, protocols and other developer interfaces"),
79 HELP_ACTION_DEVELOPER_INTERFACES),
80 OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"),
81 HELP_ACTION_CONFIG),
82 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "",
83 HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN),
84 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode, "",
85 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, PARSE_OPT_HIDDEN),
87 OPT_END(),
90 static const char * const builtin_help_usage[] = {
91 "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]",
92 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
93 "git help [-g|--guides]",
94 "git help [-c|--config]",
95 "git help [--user-interfaces]",
96 "git help [--developer-interfaces]",
97 NULL
100 struct slot_expansion {
101 const char *prefix;
102 const char *placeholder;
103 void (*fn)(struct string_list *list, const char *prefix);
104 int found;
107 static void list_config_help(enum show_config_type type)
109 struct slot_expansion slot_expansions[] = {
110 { "advice", "*", list_config_advices },
111 { "color.branch", "<slot>", list_config_color_branch_slots },
112 { "color.decorate", "<slot>", list_config_color_decorate_slots },
113 { "color.diff", "<slot>", list_config_color_diff_slots },
114 { "color.grep", "<slot>", list_config_color_grep_slots },
115 { "color.interactive", "<slot>", list_config_color_interactive_slots },
116 { "color.remote", "<slot>", list_config_color_sideband_slots },
117 { "color.status", "<slot>", list_config_color_status_slots },
118 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
119 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
120 { NULL, NULL, NULL }
122 const char **p;
123 struct slot_expansion *e;
124 struct string_list keys = STRING_LIST_INIT_DUP;
125 struct string_list keys_uniq = STRING_LIST_INIT_DUP;
126 struct string_list_item *item;
127 int i;
129 for (p = config_name_list; *p; p++) {
130 const char *var = *p;
131 struct strbuf sb = STRBUF_INIT;
133 for (e = slot_expansions; e->prefix; e++) {
135 strbuf_reset(&sb);
136 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
137 if (!strcasecmp(var, sb.buf)) {
138 e->fn(&keys, e->prefix);
139 e->found++;
140 break;
143 strbuf_release(&sb);
144 if (!e->prefix)
145 string_list_append(&keys, var);
148 for (e = slot_expansions; e->prefix; e++)
149 if (!e->found)
150 BUG("slot_expansion %s.%s is not used",
151 e->prefix, e->placeholder);
153 string_list_sort(&keys);
154 for (i = 0; i < keys.nr; i++) {
155 const char *var = keys.items[i].string;
156 const char *wildcard, *tag, *cut;
157 const char *dot = NULL;
158 struct strbuf sb = STRBUF_INIT;
160 switch (type) {
161 case SHOW_CONFIG_HUMAN:
162 puts(var);
163 continue;
164 case SHOW_CONFIG_SECTIONS:
165 dot = strchr(var, '.');
166 break;
167 case SHOW_CONFIG_VARS:
168 break;
170 wildcard = strchr(var, '*');
171 tag = strchr(var, '<');
173 if (!dot && !wildcard && !tag) {
174 string_list_append(&keys_uniq, var);
175 continue;
178 if (dot)
179 cut = dot;
180 else if (wildcard && !tag)
181 cut = wildcard;
182 else if (!wildcard && tag)
183 cut = tag;
184 else
185 cut = wildcard < tag ? wildcard : tag;
187 strbuf_add(&sb, var, cut - var);
188 string_list_append(&keys_uniq, sb.buf);
189 strbuf_release(&sb);
192 string_list_clear(&keys, 0);
193 string_list_remove_duplicates(&keys_uniq, 0);
194 for_each_string_list_item(item, &keys_uniq)
195 puts(item->string);
196 string_list_clear(&keys_uniq, 0);
199 static enum help_format parse_help_format(const char *format)
201 if (!strcmp(format, "man"))
202 return HELP_FORMAT_MAN;
203 if (!strcmp(format, "info"))
204 return HELP_FORMAT_INFO;
205 if (!strcmp(format, "web") || !strcmp(format, "html"))
206 return HELP_FORMAT_WEB;
208 * Please update _git_config() in git-completion.bash when you
209 * add new help formats.
211 die(_("unrecognized help format '%s'"), format);
214 static const char *get_man_viewer_info(const char *name)
216 struct man_viewer_info_list *viewer;
218 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
220 if (!strcasecmp(name, viewer->name))
221 return viewer->info;
223 return NULL;
226 static int check_emacsclient_version(void)
228 struct strbuf buffer = STRBUF_INIT;
229 struct child_process ec_process = CHILD_PROCESS_INIT;
230 int version;
232 /* emacsclient prints its version number on stderr */
233 strvec_pushl(&ec_process.args, "emacsclient", "--version", NULL);
234 ec_process.err = -1;
235 ec_process.stdout_to_stderr = 1;
236 if (start_command(&ec_process))
237 return error(_("Failed to start emacsclient."));
239 strbuf_read(&buffer, ec_process.err, 20);
240 close(ec_process.err);
243 * Don't bother checking return value, because "emacsclient --version"
244 * seems to always exits with code 1.
246 finish_command(&ec_process);
248 if (!starts_with(buffer.buf, "emacsclient")) {
249 strbuf_release(&buffer);
250 return error(_("Failed to parse emacsclient version."));
253 strbuf_remove(&buffer, 0, strlen("emacsclient"));
254 version = atoi(buffer.buf);
256 if (version < 22) {
257 strbuf_release(&buffer);
258 return error(_("emacsclient version '%d' too old (< 22)."),
259 version);
262 strbuf_release(&buffer);
263 return 0;
266 static void exec_woman_emacs(const char *path, const char *page)
268 if (!check_emacsclient_version()) {
269 /* This works only with emacsclient version >= 22. */
270 struct strbuf man_page = STRBUF_INIT;
272 if (!path)
273 path = "emacsclient";
274 strbuf_addf(&man_page, "(woman \"%s\")", page);
275 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
276 warning_errno(_("failed to exec '%s'"), path);
277 strbuf_release(&man_page);
281 static void exec_man_konqueror(const char *path, const char *page)
283 const char *display = getenv("DISPLAY");
284 if (display && *display) {
285 struct strbuf man_page = STRBUF_INIT;
286 const char *filename = "kfmclient";
288 /* It's simpler to launch konqueror using kfmclient. */
289 if (path) {
290 size_t len;
291 if (strip_suffix(path, "/konqueror", &len))
292 path = xstrfmt("%.*s/kfmclient", (int)len, path);
293 filename = basename((char *)path);
294 } else
295 path = "kfmclient";
296 strbuf_addf(&man_page, "man:%s(1)", page);
297 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
298 warning_errno(_("failed to exec '%s'"), path);
299 strbuf_release(&man_page);
303 static void exec_man_man(const char *path, const char *page)
305 if (!path)
306 path = "man";
307 execlp(path, "man", page, (char *)NULL);
308 warning_errno(_("failed to exec '%s'"), path);
311 static void exec_man_cmd(const char *cmd, const char *page)
313 struct strbuf shell_cmd = STRBUF_INIT;
314 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
315 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
316 warning(_("failed to exec '%s'"), cmd);
317 strbuf_release(&shell_cmd);
320 static void add_man_viewer(const char *name)
322 struct man_viewer_list **p = &man_viewer_list;
324 while (*p)
325 p = &((*p)->next);
326 FLEX_ALLOC_STR(*p, name, name);
329 static int supported_man_viewer(const char *name, size_t len)
331 return (!strncasecmp("man", name, len) ||
332 !strncasecmp("woman", name, len) ||
333 !strncasecmp("konqueror", name, len));
336 static void do_add_man_viewer_info(const char *name,
337 size_t len,
338 const char *value)
340 struct man_viewer_info_list *new_man_viewer;
341 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
342 new_man_viewer->info = xstrdup(value);
343 new_man_viewer->next = man_viewer_info_list;
344 man_viewer_info_list = new_man_viewer;
347 static int add_man_viewer_path(const char *name,
348 size_t len,
349 const char *value)
351 if (supported_man_viewer(name, len))
352 do_add_man_viewer_info(name, len, value);
353 else
354 warning(_("'%s': path for unsupported man viewer.\n"
355 "Please consider using 'man.<tool>.cmd' instead."),
356 name);
358 return 0;
361 static int add_man_viewer_cmd(const char *name,
362 size_t len,
363 const char *value)
365 if (supported_man_viewer(name, len))
366 warning(_("'%s': cmd for supported man viewer.\n"
367 "Please consider using 'man.<tool>.path' instead."),
368 name);
369 else
370 do_add_man_viewer_info(name, len, value);
372 return 0;
375 static int add_man_viewer_info(const char *var, const char *value)
377 const char *name, *subkey;
378 size_t namelen;
380 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
381 return 0;
383 if (!strcmp(subkey, "path")) {
384 if (!value)
385 return config_error_nonbool(var);
386 return add_man_viewer_path(name, namelen, value);
388 if (!strcmp(subkey, "cmd")) {
389 if (!value)
390 return config_error_nonbool(var);
391 return add_man_viewer_cmd(name, namelen, value);
394 return 0;
397 static int git_help_config(const char *var, const char *value, void *cb)
399 if (!strcmp(var, "help.format")) {
400 if (!value)
401 return config_error_nonbool(var);
402 help_format = parse_help_format(value);
403 return 0;
405 if (!strcmp(var, "help.htmlpath")) {
406 if (!value)
407 return config_error_nonbool(var);
408 html_path = xstrdup(value);
409 return 0;
411 if (!strcmp(var, "man.viewer")) {
412 if (!value)
413 return config_error_nonbool(var);
414 add_man_viewer(value);
415 return 0;
417 if (starts_with(var, "man."))
418 return add_man_viewer_info(var, value);
420 return git_default_config(var, value, cb);
423 static struct cmdnames main_cmds, other_cmds;
425 static int is_git_command(const char *s)
427 if (is_builtin(s))
428 return 1;
430 load_command_list("git-", &main_cmds, &other_cmds);
431 return is_in_cmdlist(&main_cmds, s) ||
432 is_in_cmdlist(&other_cmds, s);
435 static const char *cmd_to_page(const char *git_cmd)
437 if (!git_cmd)
438 return "git";
439 else if (starts_with(git_cmd, "git"))
440 return git_cmd;
441 else if (is_git_command(git_cmd))
442 return xstrfmt("git-%s", git_cmd);
443 else if (!strcmp("scalar", git_cmd))
444 return xstrdup(git_cmd);
445 else
446 return xstrfmt("git%s", git_cmd);
449 static void setup_man_path(void)
451 struct strbuf new_path = STRBUF_INIT;
452 const char *old_path = getenv("MANPATH");
453 char *git_man_path = system_path(GIT_MAN_PATH);
455 /* We should always put ':' after our path. If there is no
456 * old_path, the ':' at the end will let 'man' to try
457 * system-wide paths after ours to find the manual page. If
458 * there is old_path, we need ':' as delimiter. */
459 strbuf_addstr(&new_path, git_man_path);
460 strbuf_addch(&new_path, ':');
461 if (old_path)
462 strbuf_addstr(&new_path, old_path);
464 free(git_man_path);
465 setenv("MANPATH", new_path.buf, 1);
467 strbuf_release(&new_path);
470 static void exec_viewer(const char *name, const char *page)
472 const char *info = get_man_viewer_info(name);
474 if (!strcasecmp(name, "man"))
475 exec_man_man(info, page);
476 else if (!strcasecmp(name, "woman"))
477 exec_woman_emacs(info, page);
478 else if (!strcasecmp(name, "konqueror"))
479 exec_man_konqueror(info, page);
480 else if (info)
481 exec_man_cmd(info, page);
482 else
483 warning(_("'%s': unknown man viewer."), name);
486 static void show_man_page(const char *page)
488 struct man_viewer_list *viewer;
489 const char *fallback = getenv("GIT_MAN_VIEWER");
491 setup_man_path();
492 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
494 exec_viewer(viewer->name, page); /* will return when unable */
496 if (fallback)
497 exec_viewer(fallback, page);
498 exec_viewer("man", page);
499 die(_("no man viewer handled the request"));
502 static void show_info_page(const char *page)
504 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
505 execlp("info", "info", "gitman", page, (char *)NULL);
506 die(_("no info viewer handled the request"));
509 static void get_html_page_path(struct strbuf *page_path, const char *page)
511 struct stat st;
512 char *to_free = NULL;
514 if (!html_path)
515 html_path = to_free = system_path(GIT_HTML_PATH);
518 * Check that the page we're looking for exists.
520 if (!strstr(html_path, "://")) {
521 if (stat(mkpath("%s/%s.html", html_path, page), &st)
522 || !S_ISREG(st.st_mode))
523 die("'%s/%s.html': documentation file not found.",
524 html_path, page);
527 strbuf_init(page_path, 0);
528 strbuf_addf(page_path, "%s/%s.html", html_path, page);
529 free(to_free);
532 static void open_html(const char *path)
534 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
537 static void show_html_page(const char *page)
539 struct strbuf page_path; /* it leaks but we exec bellow */
541 get_html_page_path(&page_path, page);
543 open_html(page_path.buf);
546 static const char *check_git_cmd(const char* cmd)
548 char *alias;
550 if (is_git_command(cmd))
551 return cmd;
553 alias = alias_lookup(cmd);
554 if (alias) {
555 const char **argv;
556 int count;
559 * handle_builtin() in git.c rewrites "git cmd --help"
560 * to "git help --exclude-guides cmd", so we can use
561 * exclude_guides to distinguish "git cmd --help" from
562 * "git help cmd". In the latter case, or if cmd is an
563 * alias for a shell command, just print the alias
564 * definition.
566 if (!exclude_guides || alias[0] == '!') {
567 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
568 free(alias);
569 exit(0);
572 * Otherwise, we pretend that the command was "git
573 * word0 --help". We use split_cmdline() to get the
574 * first word of the alias, to ensure that we use the
575 * same rules as when the alias is actually
576 * used. split_cmdline() modifies alias in-place.
578 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
579 count = split_cmdline(alias, &argv);
580 if (count < 0)
581 die(_("bad alias.%s string: %s"), cmd,
582 split_cmdline_strerror(count));
583 free(argv);
584 UNLEAK(alias);
585 return alias;
588 if (exclude_guides)
589 return help_unknown_cmd(cmd);
591 return cmd;
594 static void no_help_format(const char *opt_mode, enum help_format fmt)
596 const char *opt_fmt;
598 switch (fmt) {
599 case HELP_FORMAT_NONE:
600 return;
601 case HELP_FORMAT_MAN:
602 opt_fmt = "--man";
603 break;
604 case HELP_FORMAT_INFO:
605 opt_fmt = "--info";
606 break;
607 case HELP_FORMAT_WEB:
608 opt_fmt = "--web";
609 break;
610 default:
611 BUG("unreachable");
614 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
615 builtin_help_usage, builtin_help_options, opt_mode,
616 opt_fmt);
619 static void opt_mode_usage(int argc, const char *opt_mode,
620 enum help_format fmt)
622 if (argc)
623 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
624 builtin_help_usage, builtin_help_options,
625 opt_mode);
627 no_help_format(opt_mode, fmt);
630 int cmd_help(int argc, const char **argv, const char *prefix)
632 int nongit;
633 enum help_format parsed_help_format;
634 const char *page;
636 argc = parse_options(argc, argv, prefix, builtin_help_options,
637 builtin_help_usage, 0);
638 parsed_help_format = help_format;
640 if (cmd_mode != HELP_ACTION_ALL &&
641 (show_external_commands >= 0 ||
642 show_aliases >= 0))
643 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
644 builtin_help_usage, builtin_help_options);
646 switch (cmd_mode) {
647 case HELP_ACTION_ALL:
648 opt_mode_usage(argc, "--all", help_format);
649 if (verbose) {
650 setup_pager();
651 list_all_cmds_help(show_external_commands,
652 show_aliases);
653 return 0;
655 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
656 load_command_list("git-", &main_cmds, &other_cmds);
657 list_commands(&main_cmds, &other_cmds);
658 printf("%s\n", _(git_more_info_string));
659 break;
660 case HELP_ACTION_GUIDES:
661 opt_mode_usage(argc, "--guides", help_format);
662 list_guides_help();
663 printf("%s\n", _(git_more_info_string));
664 return 0;
665 case HELP_ACTION_CONFIG_FOR_COMPLETION:
666 opt_mode_usage(argc, "--config-for-completion", help_format);
667 list_config_help(SHOW_CONFIG_VARS);
668 return 0;
669 case HELP_ACTION_USER_INTERFACES:
670 opt_mode_usage(argc, "--user-interfaces", help_format);
671 list_user_interfaces_help();
672 return 0;
673 case HELP_ACTION_DEVELOPER_INTERFACES:
674 opt_mode_usage(argc, "--developer-interfaces", help_format);
675 list_developer_interfaces_help();
676 return 0;
677 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
678 opt_mode_usage(argc, "--config-sections-for-completion",
679 help_format);
680 list_config_help(SHOW_CONFIG_SECTIONS);
681 return 0;
682 case HELP_ACTION_CONFIG:
683 opt_mode_usage(argc, "--config", help_format);
684 setup_pager();
685 list_config_help(SHOW_CONFIG_HUMAN);
686 printf("\n%s\n", _("'git help config' for more information"));
687 return 0;
690 if (!argv[0]) {
691 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
692 list_common_cmds_help();
693 printf("\n%s\n", _(git_more_info_string));
694 return 0;
697 setup_git_directory_gently(&nongit);
698 git_config(git_help_config, NULL);
700 if (parsed_help_format != HELP_FORMAT_NONE)
701 help_format = parsed_help_format;
702 if (help_format == HELP_FORMAT_NONE)
703 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
705 argv[0] = check_git_cmd(argv[0]);
707 page = cmd_to_page(argv[0]);
708 switch (help_format) {
709 case HELP_FORMAT_NONE:
710 case HELP_FORMAT_MAN:
711 show_man_page(page);
712 break;
713 case HELP_FORMAT_INFO:
714 show_info_page(page);
715 break;
716 case HELP_FORMAT_WEB:
717 show_html_page(page);
718 break;
721 return 0;