8 #include "parse-options.h"
9 #include "run-command.h"
11 #include "config-list.h"
15 #ifndef DEFAULT_HELP_FORMAT
16 #define DEFAULT_HELP_FORMAT "man"
19 static struct man_viewer_list {
20 struct man_viewer_list *next;
21 char name[FLEX_ARRAY];
24 static struct man_viewer_info_list {
25 struct man_viewer_info_list *next;
27 char name[FLEX_ARRAY];
28 } *man_viewer_info_list;
37 static const char *html_path;
39 static int show_all = 0;
40 static int show_guides = 0;
41 static int show_config;
42 static int verbose = 1;
43 static unsigned int colopts;
44 static enum help_format help_format = HELP_FORMAT_NONE;
45 static int exclude_guides;
46 static struct option builtin_help_options[] = {
47 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
48 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
49 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
50 OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
51 OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
52 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
53 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
55 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
57 OPT__VERBOSE(&verbose, N_("print command description")),
61 static const char * const builtin_help_usage[] = {
62 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
66 struct slot_expansion {
68 const char *placeholder;
69 void (*fn)(struct string_list *list, const char *prefix);
73 static void list_config_help(int for_human)
75 struct slot_expansion slot_expansions[] = {
76 { "advice", "*", list_config_advices },
77 { "color.branch", "<slot>", list_config_color_branch_slots },
78 { "color.decorate", "<slot>", list_config_color_decorate_slots },
79 { "color.diff", "<slot>", list_config_color_diff_slots },
80 { "color.grep", "<slot>", list_config_color_grep_slots },
81 { "color.interactive", "<slot>", list_config_color_interactive_slots },
82 { "color.remote", "<slot>", list_config_color_sideband_slots },
83 { "color.status", "<slot>", list_config_color_status_slots },
84 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
85 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
89 struct slot_expansion *e;
90 struct string_list keys = STRING_LIST_INIT_DUP;
93 for (p = config_name_list; *p; p++) {
95 struct strbuf sb = STRBUF_INIT;
97 for (e = slot_expansions; e->prefix; e++) {
100 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
101 if (!strcasecmp(var, sb.buf)) {
102 e->fn(&keys, e->prefix);
109 string_list_append(&keys, var);
112 for (e = slot_expansions; e->prefix; e++)
114 BUG("slot_expansion %s.%s is not used",
115 e->prefix, e->placeholder);
117 string_list_sort(&keys);
118 for (i = 0; i < keys.nr; i++) {
119 const char *var = keys.items[i].string;
120 const char *wildcard, *tag, *cut;
127 wildcard = strchr(var, '*');
128 tag = strchr(var, '<');
130 if (!wildcard && !tag) {
135 if (wildcard && !tag)
137 else if (!wildcard && tag)
140 cut = wildcard < tag ? wildcard : tag;
143 * We may produce duplicates, but that's up to
144 * git-completion.bash to handle
146 printf("%.*s\n", (int)(cut - var), var);
148 string_list_clear(&keys, 0);
151 static enum help_format parse_help_format(const char *format)
153 if (!strcmp(format, "man"))
154 return HELP_FORMAT_MAN;
155 if (!strcmp(format, "info"))
156 return HELP_FORMAT_INFO;
157 if (!strcmp(format, "web") || !strcmp(format, "html"))
158 return HELP_FORMAT_WEB;
160 * Please update _git_config() in git-completion.bash when you
161 * add new help formats.
163 die(_("unrecognized help format '%s'"), format);
166 static const char *get_man_viewer_info(const char *name)
168 struct man_viewer_info_list *viewer;
170 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
172 if (!strcasecmp(name, viewer->name))
178 static int check_emacsclient_version(void)
180 struct strbuf buffer = STRBUF_INIT;
181 struct child_process ec_process = CHILD_PROCESS_INIT;
182 const char *argv_ec[] = { "emacsclient", "--version", NULL };
185 /* emacsclient prints its version number on stderr */
186 ec_process.argv = argv_ec;
188 ec_process.stdout_to_stderr = 1;
189 if (start_command(&ec_process))
190 return error(_("Failed to start emacsclient."));
192 strbuf_read(&buffer, ec_process.err, 20);
193 close(ec_process.err);
196 * Don't bother checking return value, because "emacsclient --version"
197 * seems to always exits with code 1.
199 finish_command(&ec_process);
201 if (!starts_with(buffer.buf, "emacsclient")) {
202 strbuf_release(&buffer);
203 return error(_("Failed to parse emacsclient version."));
206 strbuf_remove(&buffer, 0, strlen("emacsclient"));
207 version = atoi(buffer.buf);
210 strbuf_release(&buffer);
211 return error(_("emacsclient version '%d' too old (< 22)."),
215 strbuf_release(&buffer);
219 static void exec_woman_emacs(const char *path, const char *page)
221 if (!check_emacsclient_version()) {
222 /* This works only with emacsclient version >= 22. */
223 struct strbuf man_page = STRBUF_INIT;
226 path = "emacsclient";
227 strbuf_addf(&man_page, "(woman \"%s\")", page);
228 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
229 warning_errno(_("failed to exec '%s'"), path);
230 strbuf_release(&man_page);
234 static void exec_man_konqueror(const char *path, const char *page)
236 const char *display = getenv("DISPLAY");
237 if (display && *display) {
238 struct strbuf man_page = STRBUF_INIT;
239 const char *filename = "kfmclient";
241 /* It's simpler to launch konqueror using kfmclient. */
244 if (strip_suffix(path, "/konqueror", &len))
245 path = xstrfmt("%.*s/kfmclient", (int)len, path);
246 filename = basename((char *)path);
249 strbuf_addf(&man_page, "man:%s(1)", page);
250 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
251 warning_errno(_("failed to exec '%s'"), path);
252 strbuf_release(&man_page);
256 static void exec_man_man(const char *path, const char *page)
260 execlp(path, "man", page, (char *)NULL);
261 warning_errno(_("failed to exec '%s'"), path);
264 static void exec_man_cmd(const char *cmd, const char *page)
266 struct strbuf shell_cmd = STRBUF_INIT;
267 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
268 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
269 warning(_("failed to exec '%s'"), cmd);
270 strbuf_release(&shell_cmd);
273 static void add_man_viewer(const char *name)
275 struct man_viewer_list **p = &man_viewer_list;
279 FLEX_ALLOC_STR(*p, name, name);
282 static int supported_man_viewer(const char *name, size_t len)
284 return (!strncasecmp("man", name, len) ||
285 !strncasecmp("woman", name, len) ||
286 !strncasecmp("konqueror", name, len));
289 static void do_add_man_viewer_info(const char *name,
293 struct man_viewer_info_list *new_man_viewer;
294 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
295 new_man_viewer->info = xstrdup(value);
296 new_man_viewer->next = man_viewer_info_list;
297 man_viewer_info_list = new_man_viewer;
300 static int add_man_viewer_path(const char *name,
304 if (supported_man_viewer(name, len))
305 do_add_man_viewer_info(name, len, value);
307 warning(_("'%s': path for unsupported man viewer.\n"
308 "Please consider using 'man.<tool>.cmd' instead."),
314 static int add_man_viewer_cmd(const char *name,
318 if (supported_man_viewer(name, len))
319 warning(_("'%s': cmd for supported man viewer.\n"
320 "Please consider using 'man.<tool>.path' instead."),
323 do_add_man_viewer_info(name, len, value);
328 static int add_man_viewer_info(const char *var, const char *value)
330 const char *name, *subkey;
333 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
336 if (!strcmp(subkey, "path")) {
338 return config_error_nonbool(var);
339 return add_man_viewer_path(name, namelen, value);
341 if (!strcmp(subkey, "cmd")) {
343 return config_error_nonbool(var);
344 return add_man_viewer_cmd(name, namelen, value);
350 static int git_help_config(const char *var, const char *value, void *cb)
352 if (starts_with(var, "column."))
353 return git_column_config(var, value, "help", &colopts);
354 if (!strcmp(var, "help.format")) {
356 return config_error_nonbool(var);
357 help_format = parse_help_format(value);
360 if (!strcmp(var, "help.htmlpath")) {
362 return config_error_nonbool(var);
363 html_path = xstrdup(value);
366 if (!strcmp(var, "man.viewer")) {
368 return config_error_nonbool(var);
369 add_man_viewer(value);
372 if (starts_with(var, "man."))
373 return add_man_viewer_info(var, value);
375 return git_default_config(var, value, cb);
378 static struct cmdnames main_cmds, other_cmds;
380 static int is_git_command(const char *s)
385 load_command_list("git-", &main_cmds, &other_cmds);
386 return is_in_cmdlist(&main_cmds, s) ||
387 is_in_cmdlist(&other_cmds, s);
390 static const char *cmd_to_page(const char *git_cmd)
394 else if (starts_with(git_cmd, "git"))
396 else if (is_git_command(git_cmd))
397 return xstrfmt("git-%s", git_cmd);
399 return xstrfmt("git%s", git_cmd);
402 static void setup_man_path(void)
404 struct strbuf new_path = STRBUF_INIT;
405 const char *old_path = getenv("MANPATH");
406 char *git_man_path = system_path(GIT_MAN_PATH);
408 /* We should always put ':' after our path. If there is no
409 * old_path, the ':' at the end will let 'man' to try
410 * system-wide paths after ours to find the manual page. If
411 * there is old_path, we need ':' as delimiter. */
412 strbuf_addstr(&new_path, git_man_path);
413 strbuf_addch(&new_path, ':');
415 strbuf_addstr(&new_path, old_path);
418 setenv("MANPATH", new_path.buf, 1);
420 strbuf_release(&new_path);
423 static void exec_viewer(const char *name, const char *page)
425 const char *info = get_man_viewer_info(name);
427 if (!strcasecmp(name, "man"))
428 exec_man_man(info, page);
429 else if (!strcasecmp(name, "woman"))
430 exec_woman_emacs(info, page);
431 else if (!strcasecmp(name, "konqueror"))
432 exec_man_konqueror(info, page);
434 exec_man_cmd(info, page);
436 warning(_("'%s': unknown man viewer."), name);
439 static void show_man_page(const char *git_cmd)
441 struct man_viewer_list *viewer;
442 const char *page = cmd_to_page(git_cmd);
443 const char *fallback = getenv("GIT_MAN_VIEWER");
446 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
448 exec_viewer(viewer->name, page); /* will return when unable */
451 exec_viewer(fallback, page);
452 exec_viewer("man", page);
453 die(_("no man viewer handled the request"));
456 static void show_info_page(const char *git_cmd)
458 const char *page = cmd_to_page(git_cmd);
459 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
460 execlp("info", "info", "gitman", page, (char *)NULL);
461 die(_("no info viewer handled the request"));
464 static void get_html_page_path(struct strbuf *page_path, const char *page)
467 char *to_free = NULL;
470 html_path = to_free = system_path(GIT_HTML_PATH);
472 /* Check that we have a git documentation directory. */
473 if (!strstr(html_path, "://")) {
474 if (stat(mkpath("%s/git.html", html_path), &st)
475 || !S_ISREG(st.st_mode))
476 die("'%s': not a documentation directory.", html_path);
479 strbuf_init(page_path, 0);
480 strbuf_addf(page_path, "%s/%s.html", html_path, page);
484 static void open_html(const char *path)
486 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
489 static void show_html_page(const char *git_cmd)
491 const char *page = cmd_to_page(git_cmd);
492 struct strbuf page_path; /* it leaks but we exec bellow */
494 get_html_page_path(&page_path, page);
496 open_html(page_path.buf);
499 static const char *check_git_cmd(const char* cmd)
503 if (is_git_command(cmd))
506 alias = alias_lookup(cmd);
512 * handle_builtin() in git.c rewrites "git cmd --help"
513 * to "git help --exclude-guides cmd", so we can use
514 * exclude_guides to distinguish "git cmd --help" from
515 * "git help cmd". In the latter case, or if cmd is an
516 * alias for a shell command, just print the alias
519 if (!exclude_guides || alias[0] == '!') {
520 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
525 * Otherwise, we pretend that the command was "git
526 * word0 --help". We use split_cmdline() to get the
527 * first word of the alias, to ensure that we use the
528 * same rules as when the alias is actually
529 * used. split_cmdline() modifies alias in-place.
531 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
532 count = split_cmdline(alias, &argv);
534 die(_("bad alias.%s string: %s"), cmd,
535 split_cmdline_strerror(count));
542 return help_unknown_cmd(cmd);
547 int cmd_help(int argc, const char **argv, const char *prefix)
550 enum help_format parsed_help_format;
552 argc = parse_options(argc, argv, prefix, builtin_help_options,
553 builtin_help_usage, 0);
554 parsed_help_format = help_format;
557 git_config(git_help_config, NULL);
560 list_all_cmds_help();
563 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
564 load_command_list("git-", &main_cmds, &other_cmds);
565 list_commands(colopts, &main_cmds, &other_cmds);
569 int for_human = show_config == 1;
572 list_config_help(for_human);
576 list_config_help(for_human);
577 printf("\n%s\n", _("'git help config' for more information"));
582 list_common_guides_help();
584 if (show_all || show_guides) {
585 printf("%s\n", _(git_more_info_string));
587 * We're done. Ignore any remaining args
593 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
594 list_common_cmds_help();
595 printf("\n%s\n", _(git_more_info_string));
599 setup_git_directory_gently(&nongit);
600 git_config(git_help_config, NULL);
602 if (parsed_help_format != HELP_FORMAT_NONE)
603 help_format = parsed_help_format;
604 if (help_format == HELP_FORMAT_NONE)
605 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
607 argv[0] = check_git_cmd(argv[0]);
609 switch (help_format) {
610 case HELP_FORMAT_NONE:
611 case HELP_FORMAT_MAN:
612 show_man_page(argv[0]);
614 case HELP_FORMAT_INFO:
615 show_info_page(argv[0]);
617 case HELP_FORMAT_WEB:
618 show_html_page(argv[0]);