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