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