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