Merge branch 'es/trace-log-progress'
[git.git] / builtin / help.c
blob299206eb573985370330fd10e619c2343209de9c
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 "column.h"
11 #include "config-list.h"
12 #include "help.h"
13 #include "alias.h"
15 #ifndef DEFAULT_HELP_FORMAT
16 #define DEFAULT_HELP_FORMAT "man"
17 #endif
19 static struct man_viewer_list {
20 struct man_viewer_list *next;
21 char name[FLEX_ARRAY];
22 } *man_viewer_list;
24 static struct man_viewer_info_list {
25 struct man_viewer_info_list *next;
26 const char *info;
27 char name[FLEX_ARRAY];
28 } *man_viewer_info_list;
30 enum help_format {
31 HELP_FORMAT_NONE,
32 HELP_FORMAT_MAN,
33 HELP_FORMAT_INFO,
34 HELP_FORMAT_WEB
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"),
54 HELP_FORMAT_WEB),
55 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
56 HELP_FORMAT_INFO),
57 OPT__VERBOSE(&verbose, N_("print command description")),
58 OPT_END(),
61 static const char * const builtin_help_usage[] = {
62 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
63 NULL
66 struct slot_expansion {
67 const char *prefix;
68 const char *placeholder;
69 void (*fn)(struct string_list *list, const char *prefix);
70 int found;
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 },
86 { NULL, NULL, NULL }
88 const char **p;
89 struct slot_expansion *e;
90 struct string_list keys = STRING_LIST_INIT_DUP;
91 int i;
93 for (p = config_name_list; *p; p++) {
94 const char *var = *p;
95 struct strbuf sb = STRBUF_INIT;
97 for (e = slot_expansions; e->prefix; e++) {
99 strbuf_reset(&sb);
100 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
101 if (!strcasecmp(var, sb.buf)) {
102 e->fn(&keys, e->prefix);
103 e->found++;
104 break;
107 strbuf_release(&sb);
108 if (!e->prefix)
109 string_list_append(&keys, var);
112 for (e = slot_expansions; e->prefix; e++)
113 if (!e->found)
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;
122 if (for_human) {
123 puts(var);
124 continue;
127 wildcard = strchr(var, '*');
128 tag = strchr(var, '<');
130 if (!wildcard && !tag) {
131 puts(var);
132 continue;
135 if (wildcard && !tag)
136 cut = wildcard;
137 else if (!wildcard && tag)
138 cut = tag;
139 else
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))
173 return viewer->info;
175 return NULL;
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 };
183 int version;
185 /* emacsclient prints its version number on stderr */
186 ec_process.argv = argv_ec;
187 ec_process.err = -1;
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);
209 if (version < 22) {
210 strbuf_release(&buffer);
211 return error(_("emacsclient version '%d' too old (< 22)."),
212 version);
215 strbuf_release(&buffer);
216 return 0;
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;
225 if (!path)
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. */
242 if (path) {
243 size_t len;
244 if (strip_suffix(path, "/konqueror", &len))
245 path = xstrfmt("%.*s/kfmclient", (int)len, path);
246 filename = basename((char *)path);
247 } else
248 path = "kfmclient";
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)
258 if (!path)
259 path = "man";
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;
277 while (*p)
278 p = &((*p)->next);
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,
290 size_t len,
291 const char *value)
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,
301 size_t len,
302 const char *value)
304 if (supported_man_viewer(name, len))
305 do_add_man_viewer_info(name, len, value);
306 else
307 warning(_("'%s': path for unsupported man viewer.\n"
308 "Please consider using 'man.<tool>.cmd' instead."),
309 name);
311 return 0;
314 static int add_man_viewer_cmd(const char *name,
315 size_t len,
316 const char *value)
318 if (supported_man_viewer(name, len))
319 warning(_("'%s': cmd for supported man viewer.\n"
320 "Please consider using 'man.<tool>.path' instead."),
321 name);
322 else
323 do_add_man_viewer_info(name, len, value);
325 return 0;
328 static int add_man_viewer_info(const char *var, const char *value)
330 const char *name, *subkey;
331 size_t namelen;
333 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
334 return 0;
336 if (!strcmp(subkey, "path")) {
337 if (!value)
338 return config_error_nonbool(var);
339 return add_man_viewer_path(name, namelen, value);
341 if (!strcmp(subkey, "cmd")) {
342 if (!value)
343 return config_error_nonbool(var);
344 return add_man_viewer_cmd(name, namelen, value);
347 return 0;
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")) {
355 if (!value)
356 return config_error_nonbool(var);
357 help_format = parse_help_format(value);
358 return 0;
360 if (!strcmp(var, "help.htmlpath")) {
361 if (!value)
362 return config_error_nonbool(var);
363 html_path = xstrdup(value);
364 return 0;
366 if (!strcmp(var, "man.viewer")) {
367 if (!value)
368 return config_error_nonbool(var);
369 add_man_viewer(value);
370 return 0;
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)
382 if (is_builtin(s))
383 return 1;
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)
392 if (!git_cmd)
393 return "git";
394 else if (starts_with(git_cmd, "git"))
395 return git_cmd;
396 else if (is_git_command(git_cmd))
397 return xstrfmt("git-%s", git_cmd);
398 else
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, ':');
414 if (old_path)
415 strbuf_addstr(&new_path, old_path);
417 free(git_man_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);
433 else if (info)
434 exec_man_cmd(info, page);
435 else
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");
445 setup_man_path();
446 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
448 exec_viewer(viewer->name, page); /* will return when unable */
450 if (fallback)
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)
466 struct stat st;
467 char *to_free = NULL;
469 if (!html_path)
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);
481 free(to_free);
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)
501 char *alias;
503 if (is_git_command(cmd))
504 return cmd;
506 alias = alias_lookup(cmd);
507 if (alias) {
508 const char **argv;
509 int count;
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
517 * definition.
519 if (!exclude_guides || alias[0] == '!') {
520 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
521 free(alias);
522 exit(0);
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);
533 if (count < 0)
534 die(_("bad alias.%s string: %s"), cmd,
535 split_cmdline_strerror(count));
536 free(argv);
537 UNLEAK(alias);
538 return alias;
541 if (exclude_guides)
542 return help_unknown_cmd(cmd);
544 return cmd;
547 int cmd_help(int argc, const char **argv, const char *prefix)
549 int nongit;
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;
556 if (show_all) {
557 git_config(git_help_config, NULL);
558 if (verbose) {
559 setup_pager();
560 list_all_cmds_help();
561 return 0;
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);
568 if (show_config) {
569 int for_human = show_config == 1;
571 if (!for_human) {
572 list_config_help(for_human);
573 return 0;
575 setup_pager();
576 list_config_help(for_human);
577 printf("\n%s\n", _("'git help config' for more information"));
578 return 0;
581 if (show_guides)
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
589 return 0;
592 if (!argv[0]) {
593 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
594 list_common_cmds_help();
595 printf("\n%s\n", _(git_more_info_string));
596 return 0;
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]);
613 break;
614 case HELP_FORMAT_INFO:
615 show_info_page(argv[0]);
616 break;
617 case HELP_FORMAT_WEB:
618 show_html_page(argv[0]);
619 break;
622 return 0;