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