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