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