8 #include "parse-options.h"
9 #include "run-command.h"
13 #ifndef DEFAULT_HELP_FORMAT
14 #define DEFAULT_HELP_FORMAT "man"
17 static struct man_viewer_list {
18 struct man_viewer_list *next;
19 char name[FLEX_ARRAY];
22 static struct man_viewer_info_list {
23 struct man_viewer_info_list *next;
25 char name[FLEX_ARRAY];
26 } *man_viewer_info_list;
35 static const char *html_path;
37 static int show_all = 0;
38 static int show_guides = 0;
39 static unsigned int colopts;
40 static enum help_format help_format = HELP_FORMAT_NONE;
41 static int exclude_guides;
42 static struct option builtin_help_options[] = {
43 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
44 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
45 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
46 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
47 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
49 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
54 static const char * const builtin_help_usage[] = {
55 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
59 static enum help_format parse_help_format(const char *format)
61 if (!strcmp(format, "man"))
62 return HELP_FORMAT_MAN;
63 if (!strcmp(format, "info"))
64 return HELP_FORMAT_INFO;
65 if (!strcmp(format, "web") || !strcmp(format, "html"))
66 return HELP_FORMAT_WEB;
67 die(_("unrecognized help format '%s'"), format);
70 static const char *get_man_viewer_info(const char *name)
72 struct man_viewer_info_list *viewer;
74 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
76 if (!strcasecmp(name, viewer->name))
82 static int check_emacsclient_version(void)
84 struct strbuf buffer = STRBUF_INIT;
85 struct child_process ec_process = CHILD_PROCESS_INIT;
86 const char *argv_ec[] = { "emacsclient", "--version", NULL };
89 /* emacsclient prints its version number on stderr */
90 ec_process.argv = argv_ec;
92 ec_process.stdout_to_stderr = 1;
93 if (start_command(&ec_process))
94 return error(_("Failed to start emacsclient."));
96 strbuf_read(&buffer, ec_process.err, 20);
97 close(ec_process.err);
100 * Don't bother checking return value, because "emacsclient --version"
101 * seems to always exits with code 1.
103 finish_command(&ec_process);
105 if (!starts_with(buffer.buf, "emacsclient")) {
106 strbuf_release(&buffer);
107 return error(_("Failed to parse emacsclient version."));
110 strbuf_remove(&buffer, 0, strlen("emacsclient"));
111 version = atoi(buffer.buf);
114 strbuf_release(&buffer);
115 return error(_("emacsclient version '%d' too old (< 22)."),
119 strbuf_release(&buffer);
123 static void exec_woman_emacs(const char *path, const char *page)
125 if (!check_emacsclient_version()) {
126 /* This works only with emacsclient version >= 22. */
127 struct strbuf man_page = STRBUF_INIT;
130 path = "emacsclient";
131 strbuf_addf(&man_page, "(woman \"%s\")", page);
132 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
133 warning_errno(_("failed to exec '%s'"), path);
137 static void exec_man_konqueror(const char *path, const char *page)
139 const char *display = getenv("DISPLAY");
140 if (display && *display) {
141 struct strbuf man_page = STRBUF_INIT;
142 const char *filename = "kfmclient";
144 /* It's simpler to launch konqueror using kfmclient. */
147 if (strip_suffix(path, "/konqueror", &len))
148 path = xstrfmt("%.*s/kfmclient", (int)len, path);
149 filename = basename((char *)path);
152 strbuf_addf(&man_page, "man:%s(1)", page);
153 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
154 warning_errno(_("failed to exec '%s'"), path);
158 static void exec_man_man(const char *path, const char *page)
162 execlp(path, "man", page, (char *)NULL);
163 warning_errno(_("failed to exec '%s'"), path);
166 static void exec_man_cmd(const char *cmd, const char *page)
168 struct strbuf shell_cmd = STRBUF_INIT;
169 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
170 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
171 warning(_("failed to exec '%s'"), cmd);
174 static void add_man_viewer(const char *name)
176 struct man_viewer_list **p = &man_viewer_list;
180 FLEX_ALLOC_STR(*p, name, name);
183 static int supported_man_viewer(const char *name, size_t len)
185 return (!strncasecmp("man", name, len) ||
186 !strncasecmp("woman", name, len) ||
187 !strncasecmp("konqueror", name, len));
190 static void do_add_man_viewer_info(const char *name,
194 struct man_viewer_info_list *new;
195 FLEX_ALLOC_MEM(new, name, name, len);
196 new->info = xstrdup(value);
197 new->next = man_viewer_info_list;
198 man_viewer_info_list = new;
201 static int add_man_viewer_path(const char *name,
205 if (supported_man_viewer(name, len))
206 do_add_man_viewer_info(name, len, value);
208 warning(_("'%s': path for unsupported man viewer.\n"
209 "Please consider using 'man.<tool>.cmd' instead."),
215 static int add_man_viewer_cmd(const char *name,
219 if (supported_man_viewer(name, len))
220 warning(_("'%s': cmd for supported man viewer.\n"
221 "Please consider using 'man.<tool>.path' instead."),
224 do_add_man_viewer_info(name, len, value);
229 static int add_man_viewer_info(const char *var, const char *value)
231 const char *name, *subkey;
234 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
237 if (!strcmp(subkey, "path")) {
239 return config_error_nonbool(var);
240 return add_man_viewer_path(name, namelen, value);
242 if (!strcmp(subkey, "cmd")) {
244 return config_error_nonbool(var);
245 return add_man_viewer_cmd(name, namelen, value);
251 static int git_help_config(const char *var, const char *value, void *cb)
253 if (starts_with(var, "column."))
254 return git_column_config(var, value, "help", &colopts);
255 if (!strcmp(var, "help.format")) {
257 return config_error_nonbool(var);
258 help_format = parse_help_format(value);
261 if (!strcmp(var, "help.htmlpath")) {
263 return config_error_nonbool(var);
264 html_path = xstrdup(value);
267 if (!strcmp(var, "man.viewer")) {
269 return config_error_nonbool(var);
270 add_man_viewer(value);
273 if (starts_with(var, "man."))
274 return add_man_viewer_info(var, value);
276 return git_default_config(var, value, cb);
279 static struct cmdnames main_cmds, other_cmds;
281 static int is_git_command(const char *s)
286 load_command_list("git-", &main_cmds, &other_cmds);
287 return is_in_cmdlist(&main_cmds, s) ||
288 is_in_cmdlist(&other_cmds, s);
291 static const char *cmd_to_page(const char *git_cmd)
295 else if (starts_with(git_cmd, "git"))
297 else if (is_git_command(git_cmd))
298 return xstrfmt("git-%s", git_cmd);
300 return xstrfmt("git%s", git_cmd);
303 static void setup_man_path(void)
305 struct strbuf new_path = STRBUF_INIT;
306 const char *old_path = getenv("MANPATH");
307 char *git_man_path = system_path(GIT_MAN_PATH);
309 /* We should always put ':' after our path. If there is no
310 * old_path, the ':' at the end will let 'man' to try
311 * system-wide paths after ours to find the manual page. If
312 * there is old_path, we need ':' as delimiter. */
313 strbuf_addstr(&new_path, git_man_path);
314 strbuf_addch(&new_path, ':');
316 strbuf_addstr(&new_path, old_path);
319 setenv("MANPATH", new_path.buf, 1);
321 strbuf_release(&new_path);
324 static void exec_viewer(const char *name, const char *page)
326 const char *info = get_man_viewer_info(name);
328 if (!strcasecmp(name, "man"))
329 exec_man_man(info, page);
330 else if (!strcasecmp(name, "woman"))
331 exec_woman_emacs(info, page);
332 else if (!strcasecmp(name, "konqueror"))
333 exec_man_konqueror(info, page);
335 exec_man_cmd(info, page);
337 warning(_("'%s': unknown man viewer."), name);
340 static void show_man_page(const char *git_cmd)
342 struct man_viewer_list *viewer;
343 const char *page = cmd_to_page(git_cmd);
344 const char *fallback = getenv("GIT_MAN_VIEWER");
347 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
349 exec_viewer(viewer->name, page); /* will return when unable */
352 exec_viewer(fallback, page);
353 exec_viewer("man", page);
354 die(_("no man viewer handled the request"));
357 static void show_info_page(const char *git_cmd)
359 const char *page = cmd_to_page(git_cmd);
360 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
361 execlp("info", "info", "gitman", page, (char *)NULL);
362 die(_("no info viewer handled the request"));
365 static void get_html_page_path(struct strbuf *page_path, const char *page)
368 char *to_free = NULL;
371 html_path = to_free = system_path(GIT_HTML_PATH);
373 /* Check that we have a git documentation directory. */
374 if (!strstr(html_path, "://")) {
375 if (stat(mkpath("%s/git.html", html_path), &st)
376 || !S_ISREG(st.st_mode))
377 die("'%s': not a documentation directory.", html_path);
380 strbuf_init(page_path, 0);
381 strbuf_addf(page_path, "%s/%s.html", html_path, page);
385 static void open_html(const char *path)
387 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
390 static void show_html_page(const char *git_cmd)
392 const char *page = cmd_to_page(git_cmd);
393 struct strbuf page_path; /* it leaks but we exec bellow */
395 get_html_page_path(&page_path, page);
397 open_html(page_path.buf);
403 } common_guides[] = {
404 { "attributes", N_("Defining attributes per path") },
405 { "everyday", N_("Everyday Git With 20 Commands Or So") },
406 { "glossary", N_("A Git glossary") },
407 { "ignore", N_("Specifies intentionally untracked files to ignore") },
408 { "modules", N_("Defining submodule properties") },
409 { "revisions", N_("Specifying revisions and ranges for Git") },
410 { "tutorial", N_("A tutorial introduction to Git (for version 1.5.1 or newer)") },
411 { "workflows", N_("An overview of recommended workflows with Git") },
414 static void list_common_guides_help(void)
418 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
419 if (longest < strlen(common_guides[i].name))
420 longest = strlen(common_guides[i].name);
423 puts(_("The common Git guides are:\n"));
424 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
425 printf(" %s ", common_guides[i].name);
426 mput_char(' ', longest - strlen(common_guides[i].name));
427 puts(_(common_guides[i].help));
432 static const char *check_git_cmd(const char* cmd)
436 if (is_git_command(cmd))
439 alias = alias_lookup(cmd);
441 printf_ln(_("`git %s' is aliased to `%s'"), cmd, alias);
447 return help_unknown_cmd(cmd);
452 int cmd_help(int argc, const char **argv, const char *prefix)
455 enum help_format parsed_help_format;
457 argc = parse_options(argc, argv, prefix, builtin_help_options,
458 builtin_help_usage, 0);
459 parsed_help_format = help_format;
462 git_config(git_help_config, NULL);
463 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
464 load_command_list("git-", &main_cmds, &other_cmds);
465 list_commands(colopts, &main_cmds, &other_cmds);
469 list_common_guides_help();
471 if (show_all || show_guides) {
472 printf("%s\n", _(git_more_info_string));
474 * We're done. Ignore any remaining args
480 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
481 list_common_cmds_help();
482 printf("\n%s\n", _(git_more_info_string));
486 setup_git_directory_gently(&nongit);
487 git_config(git_help_config, NULL);
489 if (parsed_help_format != HELP_FORMAT_NONE)
490 help_format = parsed_help_format;
491 if (help_format == HELP_FORMAT_NONE)
492 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
494 argv[0] = check_git_cmd(argv[0]);
496 switch (help_format) {
497 case HELP_FORMAT_NONE:
498 case HELP_FORMAT_MAN:
499 show_man_page(argv[0]);
501 case HELP_FORMAT_INFO:
502 show_info_page(argv[0]);
504 case HELP_FORMAT_WEB:
505 show_html_page(argv[0]);