]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/help.c
scalar: include in standard Git build & installation
[thirdparty/git.git] / builtin / help.c
CommitLineData
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
18static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21} *man_viewer_list;
22
23static 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
29enum 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
36enum show_config_type {
37 SHOW_CONFIG_HUMAN,
38 SHOW_CONFIG_VARS,
39 SHOW_CONFIG_SECTIONS,
40};
41
d35d03cf
ÆAB
42static 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 52static const char *html_path;
26c7d067 53static int verbose = 1;
3caa8239 54static enum help_format help_format = HELP_FORMAT_NONE;
af74128f 55static int exclude_guides;
1ce59013
ÆAB
56static int show_external_commands = -1;
57static int show_aliases = -1;
3d78d1f1 58static 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
90static 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
100struct 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 107static 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
199static 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
214static 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
226static 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 266static 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 281static 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 303static 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
311static 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
320static 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
329static 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
336static 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
347static 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
361static 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
375static 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
397static 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 423static struct cmdnames main_cmds, other_cmds;
3d78d1f1 424
3d78d1f1
MV
425static 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
435static 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);
3d78d1f1 443 else
acd47eec 444 return xstrfmt("git%s", git_cmd);
3d78d1f1
MV
445}
446
447static void setup_man_path(void)
448{
f285a2d7 449 struct strbuf new_path = STRBUF_INIT;
3d78d1f1 450 const char *old_path = getenv("MANPATH");
59362e56 451 char *git_man_path = system_path(GIT_MAN_PATH);
3d78d1f1 452
3d78d1f1
MV
453 /* We should always put ':' after our path. If there is no
454 * old_path, the ':' at the end will let 'man' to try
455 * system-wide paths after ours to find the manual page. If
456 * there is old_path, we need ':' as delimiter. */
59362e56 457 strbuf_addstr(&new_path, git_man_path);
3d78d1f1
MV
458 strbuf_addch(&new_path, ':');
459 if (old_path)
460 strbuf_addstr(&new_path, old_path);
461
59362e56 462 free(git_man_path);
3d78d1f1
MV
463 setenv("MANPATH", new_path.buf, 1);
464
465 strbuf_release(&new_path);
466}
467
468static void exec_viewer(const char *name, const char *page)
469{
470 const char *info = get_man_viewer_info(name);
471
472 if (!strcasecmp(name, "man"))
473 exec_man_man(info, page);
474 else if (!strcasecmp(name, "woman"))
475 exec_woman_emacs(info, page);
476 else if (!strcasecmp(name, "konqueror"))
477 exec_man_konqueror(info, page);
478 else if (info)
479 exec_man_cmd(info, page);
480 else
9665627d 481 warning(_("'%s': unknown man viewer."), name);
3d78d1f1
MV
482}
483
d5659f85 484static void show_man_page(const char *page)
3d78d1f1
MV
485{
486 struct man_viewer_list *viewer;
5059a427 487 const char *fallback = getenv("GIT_MAN_VIEWER");
3d78d1f1
MV
488
489 setup_man_path();
490 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
491 {
492 exec_viewer(viewer->name, page); /* will return when unable */
493 }
5059a427
RF
494 if (fallback)
495 exec_viewer(fallback, page);
3d78d1f1 496 exec_viewer("man", page);
9665627d 497 die(_("no man viewer handled the request"));
3d78d1f1
MV
498}
499
d5659f85 500static void show_info_page(const char *page)
3d78d1f1 501{
026fa0d5 502 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
5d314759 503 execlp("info", "info", "gitman", page, (char *)NULL);
9665627d 504 die(_("no info viewer handled the request"));
3d78d1f1
MV
505}
506
507static void get_html_page_path(struct strbuf *page_path, const char *page)
508{
509 struct stat st;
59362e56
JH
510 char *to_free = NULL;
511
89a852ef 512 if (!html_path)
59362e56 513 html_path = to_free = system_path(GIT_HTML_PATH);
3d78d1f1 514
a3952f8e
MA
515 /*
516 * Check that the page we're looking for exists.
517 */
86272b4f 518 if (!strstr(html_path, "://")) {
a3952f8e 519 if (stat(mkpath("%s/%s.html", html_path, page), &st)
86272b4f 520 || !S_ISREG(st.st_mode))
a3952f8e
MA
521 die("'%s/%s.html': documentation file not found.",
522 html_path, page);
86272b4f 523 }
3d78d1f1
MV
524
525 strbuf_init(page_path, 0);
526 strbuf_addf(page_path, "%s/%s.html", html_path, page);
59362e56 527 free(to_free);
3d78d1f1
MV
528}
529
2af202be 530static void open_html(const char *path)
3d78d1f1 531{
5d314759 532 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
3d78d1f1 533}
3d78d1f1 534
d5659f85 535static void show_html_page(const char *page)
3d78d1f1 536{
3d78d1f1
MV
537 struct strbuf page_path; /* it leaks but we exec bellow */
538
539 get_html_page_path(&page_path, page);
540
541 open_html(page_path.buf);
542}
543
af74128f
RT
544static const char *check_git_cmd(const char* cmd)
545{
546 char *alias;
547
548 if (is_git_command(cmd))
549 return cmd;
550
551 alias = alias_lookup(cmd);
552 if (alias) {
e6e76baa
RV
553 const char **argv;
554 int count;
555
556 /*
557 * handle_builtin() in git.c rewrites "git cmd --help"
558 * to "git help --exclude-guides cmd", so we can use
559 * exclude_guides to distinguish "git cmd --help" from
560 * "git help cmd". In the latter case, or if cmd is an
561 * alias for a shell command, just print the alias
562 * definition.
563 */
564 if (!exclude_guides || alias[0] == '!') {
565 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
566 free(alias);
567 exit(0);
568 }
569 /*
570 * Otherwise, we pretend that the command was "git
571 * word0 --help". We use split_cmdline() to get the
572 * first word of the alias, to ensure that we use the
573 * same rules as when the alias is actually
574 * used. split_cmdline() modifies alias in-place.
575 */
576 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
577 count = split_cmdline(alias, &argv);
578 if (count < 0)
579 die(_("bad alias.%s string: %s"), cmd,
580 split_cmdline_strerror(count));
581 free(argv);
582 UNLEAK(alias);
583 return alias;
af74128f
RT
584 }
585
586 if (exclude_guides)
587 return help_unknown_cmd(cmd);
588
589 return cmd;
590}
591
503cddac
ÆAB
592static void no_help_format(const char *opt_mode, enum help_format fmt)
593{
594 const char *opt_fmt;
595
596 switch (fmt) {
597 case HELP_FORMAT_NONE:
598 return;
599 case HELP_FORMAT_MAN:
600 opt_fmt = "--man";
601 break;
602 case HELP_FORMAT_INFO:
603 opt_fmt = "--info";
604 break;
605 case HELP_FORMAT_WEB:
606 opt_fmt = "--web";
607 break;
608 default:
609 BUG("unreachable");
610 }
611
612 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
613 builtin_help_usage, builtin_help_options, opt_mode,
614 opt_fmt);
615}
616
617static void opt_mode_usage(int argc, const char *opt_mode,
618 enum help_format fmt)
d35d03cf
ÆAB
619{
620 if (argc)
d7f817d3
ÆAB
621 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
622 builtin_help_usage, builtin_help_options,
623 opt_mode);
503cddac
ÆAB
624
625 no_help_format(opt_mode, fmt);
d35d03cf
ÆAB
626}
627
3d78d1f1
MV
628int cmd_help(int argc, const char **argv, const char *prefix)
629{
630 int nongit;
3caa8239 631 enum help_format parsed_help_format;
d5659f85 632 const char *page;
3d78d1f1 633
37782920 634 argc = parse_options(argc, argv, prefix, builtin_help_options,
3d78d1f1 635 builtin_help_usage, 0);
3caa8239 636 parsed_help_format = help_format;
3d78d1f1 637
1ce59013
ÆAB
638 if (cmd_mode != HELP_ACTION_ALL &&
639 (show_external_commands >= 0 ||
640 show_aliases >= 0))
641 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
642 builtin_help_usage, builtin_help_options);
643
d35d03cf
ÆAB
644 switch (cmd_mode) {
645 case HELP_ACTION_ALL:
503cddac 646 opt_mode_usage(argc, "--all", help_format);
63eae83f
NTND
647 if (verbose) {
648 setup_pager();
1ce59013
ÆAB
649 list_all_cmds_help(show_external_commands,
650 show_aliases);
63eae83f
NTND
651 return 0;
652 }
9665627d 653 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
a3c52634 654 load_command_list("git-", &main_cmds, &other_cmds);
06fa4db3 655 list_commands(&main_cmds, &other_cmds);
d35d03cf
ÆAB
656 printf("%s\n", _(git_more_info_string));
657 break;
658 case HELP_ACTION_GUIDES:
503cddac 659 opt_mode_usage(argc, "--guides", help_format);
0371a764 660 list_guides_help();
9665627d 661 printf("%s\n", _(git_more_info_string));
1ed4bef6 662 return 0;
d35d03cf 663 case HELP_ACTION_CONFIG_FOR_COMPLETION:
503cddac 664 opt_mode_usage(argc, "--config-for-completion", help_format);
a9baccca
ÆAB
665 list_config_help(SHOW_CONFIG_VARS);
666 return 0;
d976c510
ÆAB
667 case HELP_ACTION_USER_INTERFACES:
668 opt_mode_usage(argc, "--user-interfaces", help_format);
669 list_user_interfaces_help();
670 return 0;
844739ba
ÆAB
671 case HELP_ACTION_DEVELOPER_INTERFACES:
672 opt_mode_usage(argc, "--developer-interfaces", help_format);
673 list_developer_interfaces_help();
674 return 0;
a9baccca 675 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION:
503cddac
ÆAB
676 opt_mode_usage(argc, "--config-sections-for-completion",
677 help_format);
a9baccca 678 list_config_help(SHOW_CONFIG_SECTIONS);
d35d03cf
ÆAB
679 return 0;
680 case HELP_ACTION_CONFIG:
503cddac 681 opt_mode_usage(argc, "--config", help_format);
3ac68a93 682 setup_pager();
a9baccca 683 list_config_help(SHOW_CONFIG_HUMAN);
3ac68a93 684 printf("\n%s\n", _("'git help config' for more information"));
3d78d1f1
MV
685 return 0;
686 }
687
688 if (!argv[0]) {
9665627d 689 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
3d78d1f1 690 list_common_cmds_help();
9665627d 691 printf("\n%s\n", _(git_more_info_string));
3d78d1f1
MV
692 return 0;
693 }
694
af6fbf9f
DA
695 setup_git_directory_gently(&nongit);
696 git_config(git_help_config, NULL);
697
d0408c0c 698 if (parsed_help_format != HELP_FORMAT_NONE)
3caa8239 699 help_format = parsed_help_format;
d0408c0c
PT
700 if (help_format == HELP_FORMAT_NONE)
701 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
3caa8239 702
af74128f 703 argv[0] = check_git_cmd(argv[0]);
3d78d1f1 704
d5659f85 705 page = cmd_to_page(argv[0]);
3d78d1f1 706 switch (help_format) {
3caa8239 707 case HELP_FORMAT_NONE:
3d78d1f1 708 case HELP_FORMAT_MAN:
d5659f85 709 show_man_page(page);
3d78d1f1
MV
710 break;
711 case HELP_FORMAT_INFO:
d5659f85 712 show_info_page(page);
3d78d1f1
MV
713 break;
714 case HELP_FORMAT_WEB:
d5659f85 715 show_html_page(page);
3d78d1f1
MV
716 break;
717 }
718
719 return 0;
720}