fba8c01e242debd08dc450fa1ed4db587acfdb09
[git/git.git] / builtin / help.c
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"
11
12 #ifndef DEFAULT_HELP_FORMAT
13 #define DEFAULT_HELP_FORMAT "man"
14 #endif
15
16 static struct man_viewer_list {
17 struct man_viewer_list *next;
18 char name[FLEX_ARRAY];
19 } *man_viewer_list;
20
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;
26
27 enum help_format {
28 HELP_FORMAT_NONE,
29 HELP_FORMAT_MAN,
30 HELP_FORMAT_INFO,
31 HELP_FORMAT_WEB
32 };
33
34 static const char *html_path;
35
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 struct option builtin_help_options[] = {
41 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
42 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
43 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
44 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
45 HELP_FORMAT_WEB),
46 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
47 HELP_FORMAT_INFO),
48 OPT_END(),
49 };
50
51 static const char * const builtin_help_usage[] = {
52 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
53 NULL
54 };
55
56 static enum help_format parse_help_format(const char *format)
57 {
58 if (!strcmp(format, "man"))
59 return HELP_FORMAT_MAN;
60 if (!strcmp(format, "info"))
61 return HELP_FORMAT_INFO;
62 if (!strcmp(format, "web") || !strcmp(format, "html"))
63 return HELP_FORMAT_WEB;
64 die(_("unrecognized help format '%s'"), format);
65 }
66
67 static const char *get_man_viewer_info(const char *name)
68 {
69 struct man_viewer_info_list *viewer;
70
71 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
72 {
73 if (!strcasecmp(name, viewer->name))
74 return viewer->info;
75 }
76 return NULL;
77 }
78
79 static int check_emacsclient_version(void)
80 {
81 struct strbuf buffer = STRBUF_INIT;
82 struct child_process ec_process = CHILD_PROCESS_INIT;
83 const char *argv_ec[] = { "emacsclient", "--version", NULL };
84 int version;
85
86 /* emacsclient prints its version number on stderr */
87 ec_process.argv = argv_ec;
88 ec_process.err = -1;
89 ec_process.stdout_to_stderr = 1;
90 if (start_command(&ec_process))
91 return error(_("Failed to start emacsclient."));
92
93 strbuf_read(&buffer, ec_process.err, 20);
94 close(ec_process.err);
95
96 /*
97 * Don't bother checking return value, because "emacsclient --version"
98 * seems to always exits with code 1.
99 */
100 finish_command(&ec_process);
101
102 if (!starts_with(buffer.buf, "emacsclient")) {
103 strbuf_release(&buffer);
104 return error(_("Failed to parse emacsclient version."));
105 }
106
107 strbuf_remove(&buffer, 0, strlen("emacsclient"));
108 version = atoi(buffer.buf);
109
110 if (version < 22) {
111 strbuf_release(&buffer);
112 return error(_("emacsclient version '%d' too old (< 22)."),
113 version);
114 }
115
116 strbuf_release(&buffer);
117 return 0;
118 }
119
120 static void exec_woman_emacs(const char *path, const char *page)
121 {
122 if (!check_emacsclient_version()) {
123 /* This works only with emacsclient version >= 22. */
124 struct strbuf man_page = STRBUF_INIT;
125
126 if (!path)
127 path = "emacsclient";
128 strbuf_addf(&man_page, "(woman \"%s\")", page);
129 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
130 warning(_("failed to exec '%s': %s"), path, strerror(errno));
131 }
132 }
133
134 static void exec_man_konqueror(const char *path, const char *page)
135 {
136 const char *display = getenv("DISPLAY");
137 if (display && *display) {
138 struct strbuf man_page = STRBUF_INIT;
139 const char *filename = "kfmclient";
140
141 /* It's simpler to launch konqueror using kfmclient. */
142 if (path) {
143 const char *file = strrchr(path, '/');
144 if (file && !strcmp(file + 1, "konqueror")) {
145 char *new = xstrdup(path);
146 char *dest = strrchr(new, '/');
147
148 /* strlen("konqueror") == strlen("kfmclient") */
149 strcpy(dest + 1, "kfmclient");
150 path = new;
151 }
152 if (file)
153 filename = file;
154 } else
155 path = "kfmclient";
156 strbuf_addf(&man_page, "man:%s(1)", page);
157 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
158 warning(_("failed to exec '%s': %s"), path, strerror(errno));
159 }
160 }
161
162 static void exec_man_man(const char *path, const char *page)
163 {
164 if (!path)
165 path = "man";
166 execlp(path, "man", page, (char *)NULL);
167 warning(_("failed to exec '%s': %s"), path, strerror(errno));
168 }
169
170 static void exec_man_cmd(const char *cmd, const char *page)
171 {
172 struct strbuf shell_cmd = STRBUF_INIT;
173 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
174 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
175 warning(_("failed to exec '%s': %s"), cmd, strerror(errno));
176 }
177
178 static void add_man_viewer(const char *name)
179 {
180 struct man_viewer_list **p = &man_viewer_list;
181 size_t len = strlen(name);
182
183 while (*p)
184 p = &((*p)->next);
185 *p = xcalloc(1, (sizeof(**p) + len + 1));
186 strncpy((*p)->name, name, len);
187 }
188
189 static int supported_man_viewer(const char *name, size_t len)
190 {
191 return (!strncasecmp("man", name, len) ||
192 !strncasecmp("woman", name, len) ||
193 !strncasecmp("konqueror", name, len));
194 }
195
196 static void do_add_man_viewer_info(const char *name,
197 size_t len,
198 const char *value)
199 {
200 struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
201
202 strncpy(new->name, name, len);
203 new->info = xstrdup(value);
204 new->next = man_viewer_info_list;
205 man_viewer_info_list = new;
206 }
207
208 static int add_man_viewer_path(const char *name,
209 size_t len,
210 const char *value)
211 {
212 if (supported_man_viewer(name, len))
213 do_add_man_viewer_info(name, len, value);
214 else
215 warning(_("'%s': path for unsupported man viewer.\n"
216 "Please consider using 'man.<tool>.cmd' instead."),
217 name);
218
219 return 0;
220 }
221
222 static int add_man_viewer_cmd(const char *name,
223 size_t len,
224 const char *value)
225 {
226 if (supported_man_viewer(name, len))
227 warning(_("'%s': cmd for supported man viewer.\n"
228 "Please consider using 'man.<tool>.path' instead."),
229 name);
230 else
231 do_add_man_viewer_info(name, len, value);
232
233 return 0;
234 }
235
236 static int add_man_viewer_info(const char *var, const char *value)
237 {
238 const char *name, *subkey;
239 int namelen;
240
241 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
242 return 0;
243
244 if (!strcmp(subkey, "path")) {
245 if (!value)
246 return config_error_nonbool(var);
247 return add_man_viewer_path(name, namelen, value);
248 }
249 if (!strcmp(subkey, "cmd")) {
250 if (!value)
251 return config_error_nonbool(var);
252 return add_man_viewer_cmd(name, namelen, value);
253 }
254
255 return 0;
256 }
257
258 static int git_help_config(const char *var, const char *value, void *cb)
259 {
260 if (starts_with(var, "column."))
261 return git_column_config(var, value, "help", &colopts);
262 if (!strcmp(var, "help.format")) {
263 if (!value)
264 return config_error_nonbool(var);
265 help_format = parse_help_format(value);
266 return 0;
267 }
268 if (!strcmp(var, "help.htmlpath")) {
269 if (!value)
270 return config_error_nonbool(var);
271 html_path = xstrdup(value);
272 return 0;
273 }
274 if (!strcmp(var, "man.viewer")) {
275 if (!value)
276 return config_error_nonbool(var);
277 add_man_viewer(value);
278 return 0;
279 }
280 if (starts_with(var, "man."))
281 return add_man_viewer_info(var, value);
282
283 return git_default_config(var, value, cb);
284 }
285
286 static struct cmdnames main_cmds, other_cmds;
287
288 static int is_git_command(const char *s)
289 {
290 if (is_builtin(s))
291 return 1;
292
293 load_command_list("git-", &main_cmds, &other_cmds);
294 return is_in_cmdlist(&main_cmds, s) ||
295 is_in_cmdlist(&other_cmds, s);
296 }
297
298 static const char *cmd_to_page(const char *git_cmd)
299 {
300 if (!git_cmd)
301 return "git";
302 else if (starts_with(git_cmd, "git"))
303 return git_cmd;
304 else if (is_git_command(git_cmd))
305 return xstrfmt("git-%s", git_cmd);
306 else
307 return xstrfmt("git%s", git_cmd);
308 }
309
310 static void setup_man_path(void)
311 {
312 struct strbuf new_path = STRBUF_INIT;
313 const char *old_path = getenv("MANPATH");
314 char *git_man_path = system_path(GIT_MAN_PATH);
315
316 /* We should always put ':' after our path. If there is no
317 * old_path, the ':' at the end will let 'man' to try
318 * system-wide paths after ours to find the manual page. If
319 * there is old_path, we need ':' as delimiter. */
320 strbuf_addstr(&new_path, git_man_path);
321 strbuf_addch(&new_path, ':');
322 if (old_path)
323 strbuf_addstr(&new_path, old_path);
324
325 free(git_man_path);
326 setenv("MANPATH", new_path.buf, 1);
327
328 strbuf_release(&new_path);
329 }
330
331 static void exec_viewer(const char *name, const char *page)
332 {
333 const char *info = get_man_viewer_info(name);
334
335 if (!strcasecmp(name, "man"))
336 exec_man_man(info, page);
337 else if (!strcasecmp(name, "woman"))
338 exec_woman_emacs(info, page);
339 else if (!strcasecmp(name, "konqueror"))
340 exec_man_konqueror(info, page);
341 else if (info)
342 exec_man_cmd(info, page);
343 else
344 warning(_("'%s': unknown man viewer."), name);
345 }
346
347 static void show_man_page(const char *git_cmd)
348 {
349 struct man_viewer_list *viewer;
350 const char *page = cmd_to_page(git_cmd);
351 const char *fallback = getenv("GIT_MAN_VIEWER");
352
353 setup_man_path();
354 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
355 {
356 exec_viewer(viewer->name, page); /* will return when unable */
357 }
358 if (fallback)
359 exec_viewer(fallback, page);
360 exec_viewer("man", page);
361 die(_("no man viewer handled the request"));
362 }
363
364 static void show_info_page(const char *git_cmd)
365 {
366 const char *page = cmd_to_page(git_cmd);
367 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
368 execlp("info", "info", "gitman", page, (char *)NULL);
369 die(_("no info viewer handled the request"));
370 }
371
372 static void get_html_page_path(struct strbuf *page_path, const char *page)
373 {
374 struct stat st;
375 char *to_free = NULL;
376
377 if (!html_path)
378 html_path = to_free = system_path(GIT_HTML_PATH);
379
380 /* Check that we have a git documentation directory. */
381 if (!strstr(html_path, "://")) {
382 if (stat(mkpath("%s/git.html", html_path), &st)
383 || !S_ISREG(st.st_mode))
384 die("'%s': not a documentation directory.", html_path);
385 }
386
387 strbuf_init(page_path, 0);
388 strbuf_addf(page_path, "%s/%s.html", html_path, page);
389 free(to_free);
390 }
391
392 /*
393 * If open_html is not defined in a platform-specific way (see for
394 * example compat/mingw.h), we use the script web--browse to display
395 * HTML.
396 */
397 #ifndef open_html
398 static void open_html(const char *path)
399 {
400 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
401 }
402 #endif
403
404 static void show_html_page(const char *git_cmd)
405 {
406 const char *page = cmd_to_page(git_cmd);
407 struct strbuf page_path; /* it leaks but we exec bellow */
408
409 get_html_page_path(&page_path, page);
410
411 open_html(page_path.buf);
412 }
413
414 static struct {
415 const char *name;
416 const char *help;
417 } common_guides[] = {
418 { "attributes", N_("Defining attributes per path") },
419 { "everyday", N_("Everyday Git With 20 Commands Or So") },
420 { "glossary", N_("A Git glossary") },
421 { "ignore", N_("Specifies intentionally untracked files to ignore") },
422 { "modules", N_("Defining submodule properties") },
423 { "revisions", N_("Specifying revisions and ranges for Git") },
424 { "tutorial", N_("A tutorial introduction to Git (for version 1.5.1 or newer)") },
425 { "workflows", N_("An overview of recommended workflows with Git") },
426 };
427
428 static void list_common_guides_help(void)
429 {
430 int i, longest = 0;
431
432 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
433 if (longest < strlen(common_guides[i].name))
434 longest = strlen(common_guides[i].name);
435 }
436
437 puts(_("The common Git guides are:\n"));
438 for (i = 0; i < ARRAY_SIZE(common_guides); i++) {
439 printf(" %s ", common_guides[i].name);
440 mput_char(' ', longest - strlen(common_guides[i].name));
441 puts(_(common_guides[i].help));
442 }
443 putchar('\n');
444 }
445
446 int cmd_help(int argc, const char **argv, const char *prefix)
447 {
448 int nongit;
449 char *alias;
450 enum help_format parsed_help_format;
451
452 argc = parse_options(argc, argv, prefix, builtin_help_options,
453 builtin_help_usage, 0);
454 parsed_help_format = help_format;
455
456 if (show_all) {
457 git_config(git_help_config, NULL);
458 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
459 load_command_list("git-", &main_cmds, &other_cmds);
460 list_commands(colopts, &main_cmds, &other_cmds);
461 }
462
463 if (show_guides)
464 list_common_guides_help();
465
466 if (show_all || show_guides) {
467 printf("%s\n", _(git_more_info_string));
468 /*
469 * We're done. Ignore any remaining args
470 */
471 return 0;
472 }
473
474 if (!argv[0]) {
475 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
476 list_common_cmds_help();
477 printf("\n%s\n", _(git_more_info_string));
478 return 0;
479 }
480
481 setup_git_directory_gently(&nongit);
482 git_config(git_help_config, NULL);
483
484 if (parsed_help_format != HELP_FORMAT_NONE)
485 help_format = parsed_help_format;
486 if (help_format == HELP_FORMAT_NONE)
487 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
488
489 alias = alias_lookup(argv[0]);
490 if (alias && !is_git_command(argv[0])) {
491 printf_ln(_("`git %s' is aliased to `%s'"), argv[0], alias);
492 free(alias);
493 return 0;
494 }
495
496 switch (help_format) {
497 case HELP_FORMAT_NONE:
498 case HELP_FORMAT_MAN:
499 show_man_page(argv[0]);
500 break;
501 case HELP_FORMAT_INFO:
502 show_info_page(argv[0]);
503 break;
504 case HELP_FORMAT_WEB:
505 show_html_page(argv[0]);
506 break;
507 }
508
509 return 0;
510 }