rev-list-options.txt: update --all about HEAD
[git/raj.git] / builtin / help.c
blob49f7a07f85db1e36d959749adf2eb1962940fb19
1 /*
2 * Builtin help command
3 */
4 #include "cache.h"
5 #include "builtin.h"
6 #include "exec_cmd.h"
7 #include "parse-options.h"
8 #include "run-command.h"
9 #include "column.h"
10 #include "help.h"
12 #ifndef DEFAULT_HELP_FORMAT
13 #define DEFAULT_HELP_FORMAT "man"
14 #endif
16 static struct man_viewer_list {
17 struct man_viewer_list *next;
18 char name[FLEX_ARRAY];
19 } *man_viewer_list;
21 static struct man_viewer_info_list {
22 struct man_viewer_info_list *next;
23 const char *info;
24 char name[FLEX_ARRAY];
25 } *man_viewer_info_list;
27 enum help_format {
28 HELP_FORMAT_NONE,
29 HELP_FORMAT_MAN,
30 HELP_FORMAT_INFO,
31 HELP_FORMAT_WEB
34 static const char *html_path;
36 static int show_all = 0;
37 static int show_guides = 0;
38 static unsigned int colopts;
39 static enum help_format help_format = HELP_FORMAT_NONE;
40 static int exclude_guides;
41 static struct option builtin_help_options[] = {
42 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
43 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
44 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
45 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
46 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
47 HELP_FORMAT_WEB),
48 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
49 HELP_FORMAT_INFO),
50 OPT_END(),
53 static const char * const builtin_help_usage[] = {
54 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
55 NULL
58 static enum help_format parse_help_format(const char *format)
60 if (!strcmp(format, "man"))
61 return HELP_FORMAT_MAN;
62 if (!strcmp(format, "info"))
63 return HELP_FORMAT_INFO;
64 if (!strcmp(format, "web") || !strcmp(format, "html"))
65 return HELP_FORMAT_WEB;
66 die(_("unrecognized help format '%s'"), format);
69 static const char *get_man_viewer_info(const char *name)
71 struct man_viewer_info_list *viewer;
73 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
75 if (!strcasecmp(name, viewer->name))
76 return viewer->info;
78 return NULL;
81 static int check_emacsclient_version(void)
83 struct strbuf buffer = STRBUF_INIT;
84 struct child_process ec_process = CHILD_PROCESS_INIT;
85 const char *argv_ec[] = { "emacsclient", "--version", NULL };
86 int version;
88 /* emacsclient prints its version number on stderr */
89 ec_process.argv = argv_ec;
90 ec_process.err = -1;
91 ec_process.stdout_to_stderr = 1;
92 if (start_command(&ec_process))
93 return error(_("Failed to start emacsclient."));
95 strbuf_read(&buffer, ec_process.err, 20);
96 close(ec_process.err);
99 * Don't bother checking return value, because "emacsclient --version"
100 * seems to always exits with code 1.
102 finish_command(&ec_process);
104 if (!starts_with(buffer.buf, "emacsclient")) {
105 strbuf_release(&buffer);
106 return error(_("Failed to parse emacsclient version."));
109 strbuf_remove(&buffer, 0, strlen("emacsclient"));
110 version = atoi(buffer.buf);
112 if (version < 22) {
113 strbuf_release(&buffer);
114 return error(_("emacsclient version '%d' too old (< 22)."),
115 version);
118 strbuf_release(&buffer);
119 return 0;
122 static void exec_woman_emacs(const char *path, const char *page)
124 if (!check_emacsclient_version()) {
125 /* This works only with emacsclient version >= 22. */
126 struct strbuf man_page = STRBUF_INIT;
128 if (!path)
129 path = "emacsclient";
130 strbuf_addf(&man_page, "(woman \"%s\")", page);
131 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
132 warning_errno(_("failed to exec '%s'"), path);
136 static void exec_man_konqueror(const char *path, const char *page)
138 const char *display = getenv("DISPLAY");
139 if (display && *display) {
140 struct strbuf man_page = STRBUF_INIT;
141 const char *filename = "kfmclient";
143 /* It's simpler to launch konqueror using kfmclient. */
144 if (path) {
145 size_t len;
146 if (strip_suffix(path, "/konqueror", &len))
147 path = xstrfmt("%.*s/kfmclient", (int)len, path);
148 filename = basename((char *)path);
149 } else
150 path = "kfmclient";
151 strbuf_addf(&man_page, "man:%s(1)", page);
152 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
153 warning_errno(_("failed to exec '%s'"), path);
157 static void exec_man_man(const char *path, const char *page)
159 if (!path)
160 path = "man";
161 execlp(path, "man", page, (char *)NULL);
162 warning_errno(_("failed to exec '%s'"), path);
165 static void exec_man_cmd(const char *cmd, const char *page)
167 struct strbuf shell_cmd = STRBUF_INIT;
168 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
169 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
170 warning(_("failed to exec '%s'"), cmd);
173 static void add_man_viewer(const char *name)
175 struct man_viewer_list **p = &man_viewer_list;
177 while (*p)
178 p = &((*p)->next);
179 FLEX_ALLOC_STR(*p, name, name);
182 static int supported_man_viewer(const char *name, size_t len)
184 return (!strncasecmp("man", name, len) ||
185 !strncasecmp("woman", name, len) ||
186 !strncasecmp("konqueror", name, len));
189 static void do_add_man_viewer_info(const char *name,
190 size_t len,
191 const char *value)
193 struct man_viewer_info_list *new;
194 FLEX_ALLOC_MEM(new, name, name, len);
195 new->info = xstrdup(value);
196 new->next = man_viewer_info_list;
197 man_viewer_info_list = new;
200 static int add_man_viewer_path(const char *name,
201 size_t len,
202 const char *value)
204 if (supported_man_viewer(name, len))
205 do_add_man_viewer_info(name, len, value);
206 else
207 warning(_("'%s': path for unsupported man viewer.\n"
208 "Please consider using 'man.<tool>.cmd' instead."),
209 name);
211 return 0;
214 static int add_man_viewer_cmd(const char *name,
215 size_t len,
216 const char *value)
218 if (supported_man_viewer(name, len))
219 warning(_("'%s': cmd for supported man viewer.\n"
220 "Please consider using 'man.<tool>.path' instead."),
221 name);
222 else
223 do_add_man_viewer_info(name, len, value);
225 return 0;
228 static int add_man_viewer_info(const char *var, const char *value)
230 const char *name, *subkey;
231 int namelen;
233 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
234 return 0;
236 if (!strcmp(subkey, "path")) {
237 if (!value)
238 return config_error_nonbool(var);
239 return add_man_viewer_path(name, namelen, value);
241 if (!strcmp(subkey, "cmd")) {
242 if (!value)
243 return config_error_nonbool(var);
244 return add_man_viewer_cmd(name, namelen, value);
247 return 0;
250 static int git_help_config(const char *var, const char *value, void *cb)
252 if (starts_with(var, "column."))
253 return git_column_config(var, value, "help", &colopts);
254 if (!strcmp(var, "help.format")) {
255 if (!value)
256 return config_error_nonbool(var);
257 help_format = parse_help_format(value);
258 return 0;
260 if (!strcmp(var, "help.htmlpath")) {
261 if (!value)
262 return config_error_nonbool(var);
263 html_path = xstrdup(value);
264 return 0;
266 if (!strcmp(var, "man.viewer")) {
267 if (!value)
268 return config_error_nonbool(var);
269 add_man_viewer(value);
270 return 0;
272 if (starts_with(var, "man."))
273 return add_man_viewer_info(var, value);
275 return git_default_config(var, value, cb);
278 static struct cmdnames main_cmds, other_cmds;
280 static int is_git_command(const char *s)
282 if (is_builtin(s))
283 return 1;
285 load_command_list("git-", &main_cmds, &other_cmds);
286 return is_in_cmdlist(&main_cmds, s) ||
287 is_in_cmdlist(&other_cmds, s);
290 static const char *cmd_to_page(const char *git_cmd)
292 if (!git_cmd)
293 return "git";
294 else if (starts_with(git_cmd, "git"))
295 return git_cmd;
296 else if (is_git_command(git_cmd))
297 return xstrfmt("git-%s", git_cmd);
298 else
299 return xstrfmt("git%s", git_cmd);
302 static void setup_man_path(void)
304 struct strbuf new_path = STRBUF_INIT;
305 const char *old_path = getenv("MANPATH");
306 char *git_man_path = system_path(GIT_MAN_PATH);
308 /* We should always put ':' after our path. If there is no
309 * old_path, the ':' at the end will let 'man' to try
310 * system-wide paths after ours to find the manual page. If
311 * there is old_path, we need ':' as delimiter. */
312 strbuf_addstr(&new_path, git_man_path);
313 strbuf_addch(&new_path, ':');
314 if (old_path)
315 strbuf_addstr(&new_path, old_path);
317 free(git_man_path);
318 setenv("MANPATH", new_path.buf, 1);
320 strbuf_release(&new_path);
323 static void exec_viewer(const char *name, const char *page)
325 const char *info = get_man_viewer_info(name);
327 if (!strcasecmp(name, "man"))
328 exec_man_man(info, page);
329 else if (!strcasecmp(name, "woman"))
330 exec_woman_emacs(info, page);
331 else if (!strcasecmp(name, "konqueror"))
332 exec_man_konqueror(info, page);
333 else if (info)
334 exec_man_cmd(info, page);
335 else
336 warning(_("'%s': unknown man viewer."), name);
339 static void show_man_page(const char *git_cmd)
341 struct man_viewer_list *viewer;
342 const char *page = cmd_to_page(git_cmd);
343 const char *fallback = getenv("GIT_MAN_VIEWER");
345 setup_man_path();
346 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
348 exec_viewer(viewer->name, page); /* will return when unable */
350 if (fallback)
351 exec_viewer(fallback, page);
352 exec_viewer("man", page);
353 die(_("no man viewer handled the request"));
356 static void show_info_page(const char *git_cmd)
358 const char *page = cmd_to_page(git_cmd);
359 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
360 execlp("info", "info", "gitman", page, (char *)NULL);
361 die(_("no info viewer handled the request"));
364 static void get_html_page_path(struct strbuf *page_path, const char *page)
366 struct stat st;
367 char *to_free = NULL;
369 if (!html_path)
370 html_path = to_free = system_path(GIT_HTML_PATH);
372 /* Check that we have a git documentation directory. */
373 if (!strstr(html_path, "://")) {
374 if (stat(mkpath("%s/git.html", html_path), &st)
375 || !S_ISREG(st.st_mode))
376 die("'%s': not a documentation directory.", html_path);
379 strbuf_init(page_path, 0);
380 strbuf_addf(page_path, "%s/%s.html", html_path, page);
381 free(to_free);
384 static void open_html(const char *path)
386 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
389 static void show_html_page(const char *git_cmd)
391 const char *page = cmd_to_page(git_cmd);
392 struct strbuf page_path; /* it leaks but we exec bellow */
394 get_html_page_path(&page_path, page);
396 open_html(page_path.buf);
399 static struct {
400 const char *name;
401 const char *help;
402 } common_guides[] = {
403 { "attributes", N_("Defining attributes per path") },
404 { "everyday", N_("Everyday Git With 20 Commands Or So") },
405 { "glossary", N_("A Git glossary") },
406 { "ignore", N_("Specifies intentionally untracked files to ignore") },
407 { "modules", N_("Defining submodule properties") },
408 { "revisions", N_("Specifying revisions and ranges for Git") },
409 { "tutorial", N_("A tutorial introduction to Git (for version 1.5.1 or newer)") },
410 { "workflows", N_("An overview of recommended workflows with Git") },
413 static void list_common_guides_help(void)
415 int i, longest = 0;
417 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
418 if (longest < strlen(common_guides[i].name))
419 longest = strlen(common_guides[i].name);
422 puts(_("The common Git guides are:\n"));
423 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
424 printf(" %s ", common_guides[i].name);
425 mput_char(' ', longest - strlen(common_guides[i].name));
426 puts(_(common_guides[i].help));
428 putchar('\n');
431 static const char *check_git_cmd(const char* cmd)
433 char *alias;
435 if (is_git_command(cmd))
436 return cmd;
438 alias = alias_lookup(cmd);
439 if (alias) {
440 printf_ln(_("`git %s' is aliased to `%s'"), cmd, alias);
441 free(alias);
442 exit(0);
445 if (exclude_guides)
446 return help_unknown_cmd(cmd);
448 return cmd;
451 int cmd_help(int argc, const char **argv, const char *prefix)
453 int nongit;
454 enum help_format parsed_help_format;
456 argc = parse_options(argc, argv, prefix, builtin_help_options,
457 builtin_help_usage, 0);
458 parsed_help_format = help_format;
460 if (show_all) {
461 git_config(git_help_config, NULL);
462 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
463 load_command_list("git-", &main_cmds, &other_cmds);
464 list_commands(colopts, &main_cmds, &other_cmds);
467 if (show_guides)
468 list_common_guides_help();
470 if (show_all || show_guides) {
471 printf("%s\n", _(git_more_info_string));
473 * We're done. Ignore any remaining args
475 return 0;
478 if (!argv[0]) {
479 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
480 list_common_cmds_help();
481 printf("\n%s\n", _(git_more_info_string));
482 return 0;
485 setup_git_directory_gently(&nongit);
486 git_config(git_help_config, NULL);
488 if (parsed_help_format != HELP_FORMAT_NONE)
489 help_format = parsed_help_format;
490 if (help_format == HELP_FORMAT_NONE)
491 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
493 argv[0] = check_git_cmd(argv[0]);
495 switch (help_format) {
496 case HELP_FORMAT_NONE:
497 case HELP_FORMAT_MAN:
498 show_man_page(argv[0]);
499 break;
500 case HELP_FORMAT_INFO:
501 show_info_page(argv[0]);
502 break;
503 case HELP_FORMAT_WEB:
504 show_html_page(argv[0]);
505 break;
508 return 0;