]>
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" | |
709df95b | 10 | #include "config-list.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 | ||
a9baccca ÆAB |
36 | enum show_config_type { |
37 | SHOW_CONFIG_HUMAN, | |
38 | SHOW_CONFIG_VARS, | |
39 | SHOW_CONFIG_SECTIONS, | |
40 | }; | |
41 | ||
d35d03cf ÆAB |
42 | static enum help_action { |
43 | HELP_ACTION_ALL = 1, | |
44 | HELP_ACTION_GUIDES, | |
45 | HELP_ACTION_CONFIG, | |
d976c510 | 46 | HELP_ACTION_USER_INTERFACES, |
844739ba | 47 | HELP_ACTION_DEVELOPER_INTERFACES, |
d35d03cf | 48 | HELP_ACTION_CONFIG_FOR_COMPLETION, |
a9baccca | 49 | HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, |
d35d03cf | 50 | } cmd_mode; |
89a852ef | 51 | |
d35d03cf | 52 | static const char *html_path; |
26c7d067 | 53 | static int verbose = 1; |
3caa8239 | 54 | static enum help_format help_format = HELP_FORMAT_NONE; |
af74128f | 55 | static int exclude_guides; |
1ce59013 ÆAB |
56 | static int show_external_commands = -1; |
57 | static int show_aliases = -1; | |
3d78d1f1 | 58 | static struct option builtin_help_options[] = { |
d35d03cf ÆAB |
59 | OPT_CMDMODE('a', "all", &cmd_mode, N_("print all available commands"), |
60 | HELP_ACTION_ALL), | |
1ce59013 ÆAB |
61 | OPT_BOOL(0, "external-commands", &show_external_commands, |
62 | N_("show external commands in --all")), | |
63 | OPT_BOOL(0, "aliases", &show_aliases, N_("show aliases in --all")), | |
af74128f | 64 | OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")), |
68918696 NTND |
65 | OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN), |
66 | OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"), | |
3d78d1f1 | 67 | HELP_FORMAT_WEB), |
68918696 | 68 | OPT_SET_INT('i', "info", &help_format, N_("show info page"), |
3d78d1f1 | 69 | HELP_FORMAT_INFO), |
63eae83f | 70 | OPT__VERBOSE(&verbose, N_("print command description")), |
d35d03cf ÆAB |
71 | |
72 | OPT_CMDMODE('g', "guides", &cmd_mode, N_("print list of useful guides"), | |
73 | HELP_ACTION_GUIDES), | |
d976c510 ÆAB |
74 | OPT_CMDMODE(0, "user-interfaces", &cmd_mode, |
75 | N_("print list of user-facing repository, command and file interfaces"), | |
76 | HELP_ACTION_USER_INTERFACES), | |
844739ba ÆAB |
77 | OPT_CMDMODE(0, "developer-interfaces", &cmd_mode, |
78 | N_("print list of file formats, protocols and other developer interfaces"), | |
79 | HELP_ACTION_DEVELOPER_INTERFACES), | |
d35d03cf ÆAB |
80 | OPT_CMDMODE('c', "config", &cmd_mode, N_("print all configuration variable names"), |
81 | HELP_ACTION_CONFIG), | |
82 | OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode, "", | |
83 | HELP_ACTION_CONFIG_FOR_COMPLETION, PARSE_OPT_HIDDEN), | |
a9baccca ÆAB |
84 | OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode, "", |
85 | HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION, PARSE_OPT_HIDDEN), | |
d35d03cf | 86 | |
3d78d1f1 MV |
87 | OPT_END(), |
88 | }; | |
89 | ||
90 | static const char * const builtin_help_usage[] = { | |
1f3c5f39 | 91 | "git help [-a|--all] [--[no-]verbose]] [--[no-]external-commands] [--[no-]aliases]", |
dba1e539 | 92 | N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"), |
959d670d JNA |
93 | "git help [-g|--guides]", |
94 | "git help [-c|--config]", | |
d976c510 | 95 | "git help [--user-interfaces]", |
844739ba | 96 | "git help [--developer-interfaces]", |
3d78d1f1 MV |
97 | NULL |
98 | }; | |
99 | ||
709df95b ES |
100 | struct slot_expansion { |
101 | const char *prefix; | |
102 | const char *placeholder; | |
103 | void (*fn)(struct string_list *list, const char *prefix); | |
104 | int found; | |
105 | }; | |
106 | ||
a9baccca | 107 | static void list_config_help(enum show_config_type type) |
709df95b ES |
108 | { |
109 | struct slot_expansion slot_expansions[] = { | |
110 | { "advice", "*", list_config_advices }, | |
111 | { "color.branch", "<slot>", list_config_color_branch_slots }, | |
112 | { "color.decorate", "<slot>", list_config_color_decorate_slots }, | |
113 | { "color.diff", "<slot>", list_config_color_diff_slots }, | |
114 | { "color.grep", "<slot>", list_config_color_grep_slots }, | |
115 | { "color.interactive", "<slot>", list_config_color_interactive_slots }, | |
116 | { "color.remote", "<slot>", list_config_color_sideband_slots }, | |
117 | { "color.status", "<slot>", list_config_color_status_slots }, | |
118 | { "fsck", "<msg-id>", list_config_fsck_msg_ids }, | |
119 | { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids }, | |
120 | { NULL, NULL, NULL } | |
121 | }; | |
122 | const char **p; | |
123 | struct slot_expansion *e; | |
124 | struct string_list keys = STRING_LIST_INIT_DUP; | |
a9baccca ÆAB |
125 | struct string_list keys_uniq = STRING_LIST_INIT_DUP; |
126 | struct string_list_item *item; | |
709df95b ES |
127 | int i; |
128 | ||
129 | for (p = config_name_list; *p; p++) { | |
130 | const char *var = *p; | |
131 | struct strbuf sb = STRBUF_INIT; | |
132 | ||
133 | for (e = slot_expansions; e->prefix; e++) { | |
134 | ||
135 | strbuf_reset(&sb); | |
136 | strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder); | |
137 | if (!strcasecmp(var, sb.buf)) { | |
138 | e->fn(&keys, e->prefix); | |
139 | e->found++; | |
140 | break; | |
141 | } | |
142 | } | |
143 | strbuf_release(&sb); | |
144 | if (!e->prefix) | |
145 | string_list_append(&keys, var); | |
146 | } | |
147 | ||
148 | for (e = slot_expansions; e->prefix; e++) | |
149 | if (!e->found) | |
150 | BUG("slot_expansion %s.%s is not used", | |
151 | e->prefix, e->placeholder); | |
152 | ||
153 | string_list_sort(&keys); | |
154 | for (i = 0; i < keys.nr; i++) { | |
155 | const char *var = keys.items[i].string; | |
156 | const char *wildcard, *tag, *cut; | |
a9baccca ÆAB |
157 | const char *dot = NULL; |
158 | struct strbuf sb = STRBUF_INIT; | |
709df95b | 159 | |
a9baccca ÆAB |
160 | switch (type) { |
161 | case SHOW_CONFIG_HUMAN: | |
709df95b ES |
162 | puts(var); |
163 | continue; | |
a9baccca ÆAB |
164 | case SHOW_CONFIG_SECTIONS: |
165 | dot = strchr(var, '.'); | |
166 | break; | |
167 | case SHOW_CONFIG_VARS: | |
168 | break; | |
709df95b | 169 | } |
709df95b ES |
170 | wildcard = strchr(var, '*'); |
171 | tag = strchr(var, '<'); | |
172 | ||
a9baccca ÆAB |
173 | if (!dot && !wildcard && !tag) { |
174 | string_list_append(&keys_uniq, var); | |
709df95b ES |
175 | continue; |
176 | } | |
177 | ||
a9baccca ÆAB |
178 | if (dot) |
179 | cut = dot; | |
180 | else if (wildcard && !tag) | |
709df95b ES |
181 | cut = wildcard; |
182 | else if (!wildcard && tag) | |
183 | cut = tag; | |
184 | else | |
185 | cut = wildcard < tag ? wildcard : tag; | |
186 | ||
a9baccca ÆAB |
187 | strbuf_add(&sb, var, cut - var); |
188 | string_list_append(&keys_uniq, sb.buf); | |
189 | strbuf_release(&sb); | |
190 | ||
709df95b ES |
191 | } |
192 | string_list_clear(&keys, 0); | |
a9baccca ÆAB |
193 | string_list_remove_duplicates(&keys_uniq, 0); |
194 | for_each_string_list_item(item, &keys_uniq) | |
195 | puts(item->string); | |
196 | string_list_clear(&keys_uniq, 0); | |
709df95b ES |
197 | } |
198 | ||
3d78d1f1 MV |
199 | static enum help_format parse_help_format(const char *format) |
200 | { | |
201 | if (!strcmp(format, "man")) | |
202 | return HELP_FORMAT_MAN; | |
203 | if (!strcmp(format, "info")) | |
204 | return HELP_FORMAT_INFO; | |
205 | if (!strcmp(format, "web") || !strcmp(format, "html")) | |
206 | return HELP_FORMAT_WEB; | |
5a59a230 NTND |
207 | /* |
208 | * Please update _git_config() in git-completion.bash when you | |
209 | * add new help formats. | |
210 | */ | |
9665627d | 211 | die(_("unrecognized help format '%s'"), format); |
3d78d1f1 MV |
212 | } |
213 | ||
214 | static const char *get_man_viewer_info(const char *name) | |
215 | { | |
216 | struct man_viewer_info_list *viewer; | |
217 | ||
218 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) | |
219 | { | |
220 | if (!strcasecmp(name, viewer->name)) | |
221 | return viewer->info; | |
222 | } | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | static int check_emacsclient_version(void) | |
227 | { | |
228 | struct strbuf buffer = STRBUF_INIT; | |
d3180279 | 229 | struct child_process ec_process = CHILD_PROCESS_INIT; |
3d78d1f1 MV |
230 | int version; |
231 | ||
232 | /* emacsclient prints its version number on stderr */ | |
2b709893 | 233 | strvec_pushl(&ec_process.args, "emacsclient", "--version", NULL); |
3d78d1f1 MV |
234 | ec_process.err = -1; |
235 | ec_process.stdout_to_stderr = 1; | |
4e2715fb | 236 | if (start_command(&ec_process)) |
9665627d | 237 | return error(_("Failed to start emacsclient.")); |
4e2715fb | 238 | |
3d78d1f1 MV |
239 | strbuf_read(&buffer, ec_process.err, 20); |
240 | close(ec_process.err); | |
241 | ||
242 | /* | |
243 | * Don't bother checking return value, because "emacsclient --version" | |
244 | * seems to always exits with code 1. | |
245 | */ | |
246 | finish_command(&ec_process); | |
247 | ||
59556548 | 248 | if (!starts_with(buffer.buf, "emacsclient")) { |
3d78d1f1 | 249 | strbuf_release(&buffer); |
9665627d | 250 | return error(_("Failed to parse emacsclient version.")); |
3d78d1f1 MV |
251 | } |
252 | ||
253 | strbuf_remove(&buffer, 0, strlen("emacsclient")); | |
254 | version = atoi(buffer.buf); | |
255 | ||
256 | if (version < 22) { | |
3d78d1f1 | 257 | strbuf_release(&buffer); |
9665627d | 258 | return error(_("emacsclient version '%d' too old (< 22)."), |
4e2715fb | 259 | version); |
3d78d1f1 MV |
260 | } |
261 | ||
262 | strbuf_release(&buffer); | |
263 | return 0; | |
264 | } | |
265 | ||
4b25d091 | 266 | static void exec_woman_emacs(const char *path, const char *page) |
3d78d1f1 MV |
267 | { |
268 | if (!check_emacsclient_version()) { | |
269 | /* This works only with emacsclient version >= 22. */ | |
270 | struct strbuf man_page = STRBUF_INIT; | |
271 | ||
272 | if (!path) | |
273 | path = "emacsclient"; | |
274 | strbuf_addf(&man_page, "(woman \"%s\")", page); | |
5d314759 | 275 | execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL); |
57477498 | 276 | warning_errno(_("failed to exec '%s'"), path); |
bad0e2c6 | 277 | strbuf_release(&man_page); |
3d78d1f1 MV |
278 | } |
279 | } | |
280 | ||
4b25d091 | 281 | static void exec_man_konqueror(const char *path, const char *page) |
3d78d1f1 MV |
282 | { |
283 | const char *display = getenv("DISPLAY"); | |
284 | if (display && *display) { | |
285 | struct strbuf man_page = STRBUF_INIT; | |
286 | const char *filename = "kfmclient"; | |
287 | ||
288 | /* It's simpler to launch konqueror using kfmclient. */ | |
289 | if (path) { | |
4c9ac3bf JK |
290 | size_t len; |
291 | if (strip_suffix(path, "/konqueror", &len)) | |
292 | path = xstrfmt("%.*s/kfmclient", (int)len, path); | |
293 | filename = basename((char *)path); | |
3d78d1f1 MV |
294 | } else |
295 | path = "kfmclient"; | |
296 | strbuf_addf(&man_page, "man:%s(1)", page); | |
5d314759 | 297 | execlp(path, filename, "newTab", man_page.buf, (char *)NULL); |
57477498 | 298 | warning_errno(_("failed to exec '%s'"), path); |
a981a9f0 | 299 | strbuf_release(&man_page); |
3d78d1f1 MV |
300 | } |
301 | } | |
302 | ||
4b25d091 | 303 | static void exec_man_man(const char *path, const char *page) |
3d78d1f1 MV |
304 | { |
305 | if (!path) | |
306 | path = "man"; | |
5d314759 | 307 | execlp(path, "man", page, (char *)NULL); |
57477498 | 308 | warning_errno(_("failed to exec '%s'"), path); |
3d78d1f1 MV |
309 | } |
310 | ||
311 | static void exec_man_cmd(const char *cmd, const char *page) | |
312 | { | |
313 | struct strbuf shell_cmd = STRBUF_INIT; | |
314 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); | |
b680a86a | 315 | execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL); |
57477498 | 316 | warning(_("failed to exec '%s'"), cmd); |
72462186 | 317 | strbuf_release(&shell_cmd); |
3d78d1f1 MV |
318 | } |
319 | ||
320 | static void add_man_viewer(const char *name) | |
321 | { | |
322 | struct man_viewer_list **p = &man_viewer_list; | |
3d78d1f1 MV |
323 | |
324 | while (*p) | |
325 | p = &((*p)->next); | |
96ffc06f | 326 | FLEX_ALLOC_STR(*p, name, name); |
3d78d1f1 MV |
327 | } |
328 | ||
329 | static int supported_man_viewer(const char *name, size_t len) | |
330 | { | |
331 | return (!strncasecmp("man", name, len) || | |
332 | !strncasecmp("woman", name, len) || | |
333 | !strncasecmp("konqueror", name, len)); | |
334 | } | |
335 | ||
336 | static void do_add_man_viewer_info(const char *name, | |
337 | size_t len, | |
338 | const char *value) | |
339 | { | |
8adda462 BW |
340 | struct man_viewer_info_list *new_man_viewer; |
341 | FLEX_ALLOC_MEM(new_man_viewer, name, name, len); | |
342 | new_man_viewer->info = xstrdup(value); | |
343 | new_man_viewer->next = man_viewer_info_list; | |
344 | man_viewer_info_list = new_man_viewer; | |
3d78d1f1 MV |
345 | } |
346 | ||
347 | static int add_man_viewer_path(const char *name, | |
348 | size_t len, | |
349 | const char *value) | |
350 | { | |
351 | if (supported_man_viewer(name, len)) | |
352 | do_add_man_viewer_info(name, len, value); | |
353 | else | |
9665627d NTND |
354 | warning(_("'%s': path for unsupported man viewer.\n" |
355 | "Please consider using 'man.<tool>.cmd' instead."), | |
3d78d1f1 MV |
356 | name); |
357 | ||
358 | return 0; | |
359 | } | |
360 | ||
361 | static int add_man_viewer_cmd(const char *name, | |
362 | size_t len, | |
363 | const char *value) | |
364 | { | |
365 | if (supported_man_viewer(name, len)) | |
9665627d NTND |
366 | warning(_("'%s': cmd for supported man viewer.\n" |
367 | "Please consider using 'man.<tool>.path' instead."), | |
3d78d1f1 MV |
368 | name); |
369 | else | |
370 | do_add_man_viewer_info(name, len, value); | |
371 | ||
372 | return 0; | |
373 | } | |
374 | ||
375 | static int add_man_viewer_info(const char *var, const char *value) | |
376 | { | |
4d5c6cef | 377 | const char *name, *subkey; |
f5914f4b | 378 | size_t namelen; |
3d78d1f1 | 379 | |
4d5c6cef | 380 | if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name) |
178b513e | 381 | return 0; |
3d78d1f1 | 382 | |
4d5c6cef | 383 | if (!strcmp(subkey, "path")) { |
3d78d1f1 MV |
384 | if (!value) |
385 | return config_error_nonbool(var); | |
4d5c6cef | 386 | return add_man_viewer_path(name, namelen, value); |
3d78d1f1 | 387 | } |
4d5c6cef | 388 | if (!strcmp(subkey, "cmd")) { |
3d78d1f1 MV |
389 | if (!value) |
390 | return config_error_nonbool(var); | |
4d5c6cef | 391 | return add_man_viewer_cmd(name, namelen, value); |
3d78d1f1 MV |
392 | } |
393 | ||
3d78d1f1 MV |
394 | return 0; |
395 | } | |
396 | ||
397 | static int git_help_config(const char *var, const char *value, void *cb) | |
398 | { | |
399 | if (!strcmp(var, "help.format")) { | |
400 | if (!value) | |
401 | return config_error_nonbool(var); | |
402 | help_format = parse_help_format(value); | |
403 | return 0; | |
404 | } | |
89a852ef CW |
405 | if (!strcmp(var, "help.htmlpath")) { |
406 | if (!value) | |
407 | return config_error_nonbool(var); | |
408 | html_path = xstrdup(value); | |
409 | return 0; | |
410 | } | |
3d78d1f1 MV |
411 | if (!strcmp(var, "man.viewer")) { |
412 | if (!value) | |
413 | return config_error_nonbool(var); | |
414 | add_man_viewer(value); | |
415 | return 0; | |
416 | } | |
59556548 | 417 | if (starts_with(var, "man.")) |
3d78d1f1 MV |
418 | return add_man_viewer_info(var, value); |
419 | ||
420 | return git_default_config(var, value, cb); | |
421 | } | |
422 | ||
c7371e99 | 423 | static struct cmdnames main_cmds, other_cmds; |
3d78d1f1 | 424 | |
3d78d1f1 MV |
425 | static int is_git_command(const char *s) |
426 | { | |
c6127fa3 SS |
427 | if (is_builtin(s)) |
428 | return 1; | |
429 | ||
a3c52634 | 430 | load_command_list("git-", &main_cmds, &other_cmds); |
3d78d1f1 MV |
431 | return is_in_cmdlist(&main_cmds, s) || |
432 | is_in_cmdlist(&other_cmds, s); | |
433 | } | |
434 | ||
3d78d1f1 MV |
435 | static const char *cmd_to_page(const char *git_cmd) |
436 | { | |
437 | if (!git_cmd) | |
438 | return "git"; | |
59556548 | 439 | else if (starts_with(git_cmd, "git")) |
3d78d1f1 MV |
440 | return git_cmd; |
441 | else if (is_git_command(git_cmd)) | |
acd47eec | 442 | return xstrfmt("git-%s", git_cmd); |
dd9603e2 JS |
443 | else if (!strcmp("scalar", git_cmd)) |
444 | return xstrdup(git_cmd); | |
3d78d1f1 | 445 | else |
acd47eec | 446 | return xstrfmt("git%s", git_cmd); |
3d78d1f1 MV |
447 | } |
448 | ||
449 | static void setup_man_path(void) | |
450 | { | |
f285a2d7 | 451 | struct strbuf new_path = STRBUF_INIT; |
3d78d1f1 | 452 | const char *old_path = getenv("MANPATH"); |
59362e56 | 453 | char *git_man_path = system_path(GIT_MAN_PATH); |
3d78d1f1 | 454 | |
3d78d1f1 MV |
455 | /* We should always put ':' after our path. If there is no |
456 | * old_path, the ':' at the end will let 'man' to try | |
457 | * system-wide paths after ours to find the manual page. If | |
458 | * there is old_path, we need ':' as delimiter. */ | |
59362e56 | 459 | strbuf_addstr(&new_path, git_man_path); |
3d78d1f1 MV |
460 | strbuf_addch(&new_path, ':'); |
461 | if (old_path) | |
462 | strbuf_addstr(&new_path, old_path); | |
463 | ||
59362e56 | 464 | free(git_man_path); |
3d78d1f1 MV |
465 | setenv("MANPATH", new_path.buf, 1); |
466 | ||
467 | strbuf_release(&new_path); | |
468 | } | |
469 | ||
470 | static void exec_viewer(const char *name, const char *page) | |
471 | { | |
472 | const char *info = get_man_viewer_info(name); | |
473 | ||
474 | if (!strcasecmp(name, "man")) | |
475 | exec_man_man(info, page); | |
476 | else if (!strcasecmp(name, "woman")) | |
477 | exec_woman_emacs(info, page); | |
478 | else if (!strcasecmp(name, "konqueror")) | |
479 | exec_man_konqueror(info, page); | |
480 | else if (info) | |
481 | exec_man_cmd(info, page); | |
482 | else | |
9665627d | 483 | warning(_("'%s': unknown man viewer."), name); |
3d78d1f1 MV |
484 | } |
485 | ||
d5659f85 | 486 | static void show_man_page(const char *page) |
3d78d1f1 MV |
487 | { |
488 | struct man_viewer_list *viewer; | |
5059a427 | 489 | const char *fallback = getenv("GIT_MAN_VIEWER"); |
3d78d1f1 MV |
490 | |
491 | setup_man_path(); | |
492 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) | |
493 | { | |
494 | exec_viewer(viewer->name, page); /* will return when unable */ | |
495 | } | |
5059a427 RF |
496 | if (fallback) |
497 | exec_viewer(fallback, page); | |
3d78d1f1 | 498 | exec_viewer("man", page); |
9665627d | 499 | die(_("no man viewer handled the request")); |
3d78d1f1 MV |
500 | } |
501 | ||
d5659f85 | 502 | static void show_info_page(const char *page) |
3d78d1f1 | 503 | { |
026fa0d5 | 504 | setenv("INFOPATH", system_path(GIT_INFO_PATH), 1); |
5d314759 | 505 | execlp("info", "info", "gitman", page, (char *)NULL); |
9665627d | 506 | die(_("no info viewer handled the request")); |
3d78d1f1 MV |
507 | } |
508 | ||
509 | static void get_html_page_path(struct strbuf *page_path, const char *page) | |
510 | { | |
511 | struct stat st; | |
59362e56 JH |
512 | char *to_free = NULL; |
513 | ||
89a852ef | 514 | if (!html_path) |
59362e56 | 515 | html_path = to_free = system_path(GIT_HTML_PATH); |
3d78d1f1 | 516 | |
a3952f8e MA |
517 | /* |
518 | * Check that the page we're looking for exists. | |
519 | */ | |
86272b4f | 520 | if (!strstr(html_path, "://")) { |
a3952f8e | 521 | if (stat(mkpath("%s/%s.html", html_path, page), &st) |
86272b4f | 522 | || !S_ISREG(st.st_mode)) |
a3952f8e MA |
523 | die("'%s/%s.html': documentation file not found.", |
524 | html_path, page); | |
86272b4f | 525 | } |
3d78d1f1 MV |
526 | |
527 | strbuf_init(page_path, 0); | |
528 | strbuf_addf(page_path, "%s/%s.html", html_path, page); | |
59362e56 | 529 | free(to_free); |
3d78d1f1 MV |
530 | } |
531 | ||
2af202be | 532 | static void open_html(const char *path) |
3d78d1f1 | 533 | { |
5d314759 | 534 | execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL); |
3d78d1f1 | 535 | } |
3d78d1f1 | 536 | |
d5659f85 | 537 | static void show_html_page(const char *page) |
3d78d1f1 | 538 | { |
3d78d1f1 MV |
539 | struct strbuf page_path; /* it leaks but we exec bellow */ |
540 | ||
541 | get_html_page_path(&page_path, page); | |
542 | ||
543 | open_html(page_path.buf); | |
544 | } | |
545 | ||
af74128f RT |
546 | static const char *check_git_cmd(const char* cmd) |
547 | { | |
548 | char *alias; | |
549 | ||
550 | if (is_git_command(cmd)) | |
551 | return cmd; | |
552 | ||
553 | alias = alias_lookup(cmd); | |
554 | if (alias) { | |
e6e76baa RV |
555 | const char **argv; |
556 | int count; | |
557 | ||
558 | /* | |
559 | * handle_builtin() in git.c rewrites "git cmd --help" | |
560 | * to "git help --exclude-guides cmd", so we can use | |
561 | * exclude_guides to distinguish "git cmd --help" from | |
562 | * "git help cmd". In the latter case, or if cmd is an | |
563 | * alias for a shell command, just print the alias | |
564 | * definition. | |
565 | */ | |
566 | if (!exclude_guides || alias[0] == '!') { | |
567 | printf_ln(_("'%s' is aliased to '%s'"), cmd, alias); | |
568 | free(alias); | |
569 | exit(0); | |
570 | } | |
571 | /* | |
572 | * Otherwise, we pretend that the command was "git | |
573 | * word0 --help". We use split_cmdline() to get the | |
574 | * first word of the alias, to ensure that we use the | |
575 | * same rules as when the alias is actually | |
576 | * used. split_cmdline() modifies alias in-place. | |
577 | */ | |
578 | fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias); | |
579 | count = split_cmdline(alias, &argv); | |
580 | if (count < 0) | |
581 | die(_("bad alias.%s string: %s"), cmd, | |
582 | split_cmdline_strerror(count)); | |
583 | free(argv); | |
584 | UNLEAK(alias); | |
585 | return alias; | |
af74128f RT |
586 | } |
587 | ||
588 | if (exclude_guides) | |
589 | return help_unknown_cmd(cmd); | |
590 | ||
591 | return cmd; | |
592 | } | |
593 | ||
503cddac ÆAB |
594 | static void no_help_format(const char *opt_mode, enum help_format fmt) |
595 | { | |
596 | const char *opt_fmt; | |
597 | ||
598 | switch (fmt) { | |
599 | case HELP_FORMAT_NONE: | |
600 | return; | |
601 | case HELP_FORMAT_MAN: | |
602 | opt_fmt = "--man"; | |
603 | break; | |
604 | case HELP_FORMAT_INFO: | |
605 | opt_fmt = "--info"; | |
606 | break; | |
607 | case HELP_FORMAT_WEB: | |
608 | opt_fmt = "--web"; | |
609 | break; | |
610 | default: | |
611 | BUG("unreachable"); | |
612 | } | |
613 | ||
614 | usage_msg_optf(_("options '%s' and '%s' cannot be used together"), | |
615 | builtin_help_usage, builtin_help_options, opt_mode, | |
616 | opt_fmt); | |
617 | } | |
618 | ||
619 | static void opt_mode_usage(int argc, const char *opt_mode, | |
620 | enum help_format fmt) | |
d35d03cf ÆAB |
621 | { |
622 | if (argc) | |
d7f817d3 ÆAB |
623 | usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"), |
624 | builtin_help_usage, builtin_help_options, | |
625 | opt_mode); | |
503cddac ÆAB |
626 | |
627 | no_help_format(opt_mode, fmt); | |
d35d03cf ÆAB |
628 | } |
629 | ||
3d78d1f1 MV |
630 | int cmd_help(int argc, const char **argv, const char *prefix) |
631 | { | |
632 | int nongit; | |
3caa8239 | 633 | enum help_format parsed_help_format; |
d5659f85 | 634 | const char *page; |
3d78d1f1 | 635 | |
37782920 | 636 | argc = parse_options(argc, argv, prefix, builtin_help_options, |
3d78d1f1 | 637 | builtin_help_usage, 0); |
3caa8239 | 638 | parsed_help_format = help_format; |
3d78d1f1 | 639 | |
1ce59013 ÆAB |
640 | if (cmd_mode != HELP_ACTION_ALL && |
641 | (show_external_commands >= 0 || | |
642 | show_aliases >= 0)) | |
643 | usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"), | |
644 | builtin_help_usage, builtin_help_options); | |
645 | ||
d35d03cf ÆAB |
646 | switch (cmd_mode) { |
647 | case HELP_ACTION_ALL: | |
503cddac | 648 | opt_mode_usage(argc, "--all", help_format); |
63eae83f NTND |
649 | if (verbose) { |
650 | setup_pager(); | |
1ce59013 ÆAB |
651 | list_all_cmds_help(show_external_commands, |
652 | show_aliases); | |
63eae83f NTND |
653 | return 0; |
654 | } | |
9665627d | 655 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
a3c52634 | 656 | load_command_list("git-", &main_cmds, &other_cmds); |
06fa4db3 | 657 | list_commands(&main_cmds, &other_cmds); |
d35d03cf ÆAB |
658 | printf("%s\n", _(git_more_info_string)); |
659 | break; | |
660 | case HELP_ACTION_GUIDES: | |
503cddac | 661 | opt_mode_usage(argc, "--guides", help_format); |
0371a764 | 662 | list_guides_help(); |
9665627d | 663 | printf("%s\n", _(git_more_info_string)); |
1ed4bef6 | 664 | return 0; |
d35d03cf | 665 | case HELP_ACTION_CONFIG_FOR_COMPLETION: |
503cddac | 666 | opt_mode_usage(argc, "--config-for-completion", help_format); |
a9baccca ÆAB |
667 | list_config_help(SHOW_CONFIG_VARS); |
668 | return 0; | |
d976c510 ÆAB |
669 | case HELP_ACTION_USER_INTERFACES: |
670 | opt_mode_usage(argc, "--user-interfaces", help_format); | |
671 | list_user_interfaces_help(); | |
672 | return 0; | |
844739ba ÆAB |
673 | case HELP_ACTION_DEVELOPER_INTERFACES: |
674 | opt_mode_usage(argc, "--developer-interfaces", help_format); | |
675 | list_developer_interfaces_help(); | |
676 | return 0; | |
a9baccca | 677 | case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION: |
503cddac ÆAB |
678 | opt_mode_usage(argc, "--config-sections-for-completion", |
679 | help_format); | |
a9baccca | 680 | list_config_help(SHOW_CONFIG_SECTIONS); |
d35d03cf ÆAB |
681 | return 0; |
682 | case HELP_ACTION_CONFIG: | |
503cddac | 683 | opt_mode_usage(argc, "--config", help_format); |
3ac68a93 | 684 | setup_pager(); |
a9baccca | 685 | list_config_help(SHOW_CONFIG_HUMAN); |
3ac68a93 | 686 | printf("\n%s\n", _("'git help config' for more information")); |
3d78d1f1 MV |
687 | return 0; |
688 | } | |
689 | ||
690 | if (!argv[0]) { | |
9665627d | 691 | printf(_("usage: %s%s"), _(git_usage_string), "\n\n"); |
3d78d1f1 | 692 | list_common_cmds_help(); |
9665627d | 693 | printf("\n%s\n", _(git_more_info_string)); |
3d78d1f1 MV |
694 | return 0; |
695 | } | |
696 | ||
af6fbf9f DA |
697 | setup_git_directory_gently(&nongit); |
698 | git_config(git_help_config, NULL); | |
699 | ||
d0408c0c | 700 | if (parsed_help_format != HELP_FORMAT_NONE) |
3caa8239 | 701 | help_format = parsed_help_format; |
d0408c0c PT |
702 | if (help_format == HELP_FORMAT_NONE) |
703 | help_format = parse_help_format(DEFAULT_HELP_FORMAT); | |
3caa8239 | 704 | |
af74128f | 705 | argv[0] = check_git_cmd(argv[0]); |
3d78d1f1 | 706 | |
d5659f85 | 707 | page = cmd_to_page(argv[0]); |
3d78d1f1 | 708 | switch (help_format) { |
3caa8239 | 709 | case HELP_FORMAT_NONE: |
3d78d1f1 | 710 | case HELP_FORMAT_MAN: |
d5659f85 | 711 | show_man_page(page); |
3d78d1f1 MV |
712 | break; |
713 | case HELP_FORMAT_INFO: | |
d5659f85 | 714 | show_info_page(page); |
3d78d1f1 MV |
715 | break; |
716 | case HELP_FORMAT_WEB: | |
d5659f85 | 717 | show_html_page(page); |
3d78d1f1 MV |
718 | break; |
719 | } | |
720 | ||
721 | return 0; | |
722 | } |