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