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