8 #include "parse-options.h"
9 #include "run-command.h"
10 #include "config-list.h"
14 #ifndef DEFAULT_HELP_FORMAT
15 #define DEFAULT_HELP_FORMAT "man"
18 static struct man_viewer_list
{
19 struct man_viewer_list
*next
;
20 char name
[FLEX_ARRAY
];
23 static struct man_viewer_info_list
{
24 struct man_viewer_info_list
*next
;
26 char name
[FLEX_ARRAY
];
27 } *man_viewer_info_list
;
36 enum show_config_type
{
42 static enum help_action
{
46 HELP_ACTION_USER_INTERFACES
,
47 HELP_ACTION_DEVELOPER_INTERFACES
,
48 HELP_ACTION_CONFIG_FOR_COMPLETION
,
49 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
,
52 static const char *html_path
;
53 static int verbose
= 1;
54 static enum help_format help_format
= HELP_FORMAT_NONE
;
55 static int exclude_guides
;
56 static int show_external_commands
= -1;
57 static int show_aliases
= -1;
58 static struct option builtin_help_options
[] = {
59 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
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")),
64 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
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"),
68 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
70 OPT__VERBOSE(&verbose
, N_("print command description")),
72 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
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
),
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
),
80 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
82 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
83 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
84 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
85 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
90 static const char * const builtin_help_usage
[] = {
91 "git help [-a|--all] [--[no-]verbose]] [--[no-]external-commands] [--[no-]aliases]",
92 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
93 "git help [-g|--guides]",
94 "git help [-c|--config]",
95 "git help [--user-interfaces]",
96 "git help [--developer-interfaces]",
100 struct slot_expansion
{
102 const char *placeholder
;
103 void (*fn
)(struct string_list
*list
, const char *prefix
);
107 static void list_config_help(enum show_config_type type
)
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
},
123 struct slot_expansion
*e
;
124 struct string_list keys
= STRING_LIST_INIT_DUP
;
125 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
126 struct string_list_item
*item
;
129 for (p
= config_name_list
; *p
; p
++) {
130 const char *var
= *p
;
131 struct strbuf sb
= STRBUF_INIT
;
133 for (e
= slot_expansions
; e
->prefix
; e
++) {
136 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
137 if (!strcasecmp(var
, sb
.buf
)) {
138 e
->fn(&keys
, e
->prefix
);
145 string_list_append(&keys
, var
);
148 for (e
= slot_expansions
; e
->prefix
; e
++)
150 BUG("slot_expansion %s.%s is not used",
151 e
->prefix
, e
->placeholder
);
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
;
157 const char *dot
= NULL
;
158 struct strbuf sb
= STRBUF_INIT
;
161 case SHOW_CONFIG_HUMAN
:
164 case SHOW_CONFIG_SECTIONS
:
165 dot
= strchr(var
, '.');
167 case SHOW_CONFIG_VARS
:
170 wildcard
= strchr(var
, '*');
171 tag
= strchr(var
, '<');
173 if (!dot
&& !wildcard
&& !tag
) {
174 string_list_append(&keys_uniq
, var
);
180 else if (wildcard
&& !tag
)
182 else if (!wildcard
&& tag
)
185 cut
= wildcard
< tag
? wildcard
: tag
;
187 strbuf_add(&sb
, var
, cut
- var
);
188 string_list_append(&keys_uniq
, sb
.buf
);
192 string_list_clear(&keys
, 0);
193 string_list_remove_duplicates(&keys_uniq
, 0);
194 for_each_string_list_item(item
, &keys_uniq
)
196 string_list_clear(&keys_uniq
, 0);
199 static enum help_format
parse_help_format(const char *format
)
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
;
208 * Please update _git_config() in git-completion.bash when you
209 * add new help formats.
211 die(_("unrecognized help format '%s'"), format
);
214 static const char *get_man_viewer_info(const char *name
)
216 struct man_viewer_info_list
*viewer
;
218 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
220 if (!strcasecmp(name
, viewer
->name
))
226 static int check_emacsclient_version(void)
228 struct strbuf buffer
= STRBUF_INIT
;
229 struct child_process ec_process
= CHILD_PROCESS_INIT
;
232 /* emacsclient prints its version number on stderr */
233 strvec_pushl(&ec_process
.args
, "emacsclient", "--version", NULL
);
235 ec_process
.stdout_to_stderr
= 1;
236 if (start_command(&ec_process
))
237 return error(_("Failed to start emacsclient."));
239 strbuf_read(&buffer
, ec_process
.err
, 20);
240 close(ec_process
.err
);
243 * Don't bother checking return value, because "emacsclient --version"
244 * seems to always exits with code 1.
246 finish_command(&ec_process
);
248 if (!starts_with(buffer
.buf
, "emacsclient")) {
249 strbuf_release(&buffer
);
250 return error(_("Failed to parse emacsclient version."));
253 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
254 version
= atoi(buffer
.buf
);
257 strbuf_release(&buffer
);
258 return error(_("emacsclient version '%d' too old (< 22)."),
262 strbuf_release(&buffer
);
266 static void exec_woman_emacs(const char *path
, const char *page
)
268 if (!check_emacsclient_version()) {
269 /* This works only with emacsclient version >= 22. */
270 struct strbuf man_page
= STRBUF_INIT
;
273 path
= "emacsclient";
274 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
275 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
276 warning_errno(_("failed to exec '%s'"), path
);
277 strbuf_release(&man_page
);
281 static void exec_man_konqueror(const char *path
, const char *page
)
283 const char *display
= getenv("DISPLAY");
284 if (display
&& *display
) {
285 struct strbuf man_page
= STRBUF_INIT
;
286 const char *filename
= "kfmclient";
288 /* It's simpler to launch konqueror using kfmclient. */
291 if (strip_suffix(path
, "/konqueror", &len
))
292 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
293 filename
= basename((char *)path
);
296 strbuf_addf(&man_page
, "man:%s(1)", page
);
297 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
298 warning_errno(_("failed to exec '%s'"), path
);
299 strbuf_release(&man_page
);
303 static void exec_man_man(const char *path
, const char *page
)
307 execlp(path
, "man", page
, (char *)NULL
);
308 warning_errno(_("failed to exec '%s'"), path
);
311 static void exec_man_cmd(const char *cmd
, const char *page
)
313 struct strbuf shell_cmd
= STRBUF_INIT
;
314 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
315 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
316 warning(_("failed to exec '%s'"), cmd
);
317 strbuf_release(&shell_cmd
);
320 static void add_man_viewer(const char *name
)
322 struct man_viewer_list
**p
= &man_viewer_list
;
326 FLEX_ALLOC_STR(*p
, name
, name
);
329 static int supported_man_viewer(const char *name
, size_t len
)
331 return (!strncasecmp("man", name
, len
) ||
332 !strncasecmp("woman", name
, len
) ||
333 !strncasecmp("konqueror", name
, len
));
336 static void do_add_man_viewer_info(const char *name
,
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
;
347 static int add_man_viewer_path(const char *name
,
351 if (supported_man_viewer(name
, len
))
352 do_add_man_viewer_info(name
, len
, value
);
354 warning(_("'%s': path for unsupported man viewer.\n"
355 "Please consider using 'man.<tool>.cmd' instead."),
361 static int add_man_viewer_cmd(const char *name
,
365 if (supported_man_viewer(name
, len
))
366 warning(_("'%s': cmd for supported man viewer.\n"
367 "Please consider using 'man.<tool>.path' instead."),
370 do_add_man_viewer_info(name
, len
, value
);
375 static int add_man_viewer_info(const char *var
, const char *value
)
377 const char *name
, *subkey
;
380 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
383 if (!strcmp(subkey
, "path")) {
385 return config_error_nonbool(var
);
386 return add_man_viewer_path(name
, namelen
, value
);
388 if (!strcmp(subkey
, "cmd")) {
390 return config_error_nonbool(var
);
391 return add_man_viewer_cmd(name
, namelen
, value
);
397 static int git_help_config(const char *var
, const char *value
, void *cb
)
399 if (!strcmp(var
, "help.format")) {
401 return config_error_nonbool(var
);
402 help_format
= parse_help_format(value
);
405 if (!strcmp(var
, "help.htmlpath")) {
407 return config_error_nonbool(var
);
408 html_path
= xstrdup(value
);
411 if (!strcmp(var
, "man.viewer")) {
413 return config_error_nonbool(var
);
414 add_man_viewer(value
);
417 if (starts_with(var
, "man."))
418 return add_man_viewer_info(var
, value
);
420 return git_default_config(var
, value
, cb
);
423 static struct cmdnames main_cmds
, other_cmds
;
425 static int is_git_command(const char *s
)
430 load_command_list("git-", &main_cmds
, &other_cmds
);
431 return is_in_cmdlist(&main_cmds
, s
) ||
432 is_in_cmdlist(&other_cmds
, s
);
435 static const char *cmd_to_page(const char *git_cmd
)
439 else if (starts_with(git_cmd
, "git"))
441 else if (is_git_command(git_cmd
))
442 return xstrfmt("git-%s", git_cmd
);
443 else if (!strcmp("scalar", git_cmd
))
444 return xstrdup(git_cmd
);
446 return xstrfmt("git%s", git_cmd
);
449 static void setup_man_path(void)
451 struct strbuf new_path
= STRBUF_INIT
;
452 const char *old_path
= getenv("MANPATH");
453 char *git_man_path
= system_path(GIT_MAN_PATH
);
455 /* We should always put ':' after our path. If there is no
456 * old_path, the ':' at the end will let 'man' to try
457 * system-wide paths after ours to find the manual page. If
458 * there is old_path, we need ':' as delimiter. */
459 strbuf_addstr(&new_path
, git_man_path
);
460 strbuf_addch(&new_path
, ':');
462 strbuf_addstr(&new_path
, old_path
);
465 setenv("MANPATH", new_path
.buf
, 1);
467 strbuf_release(&new_path
);
470 static void exec_viewer(const char *name
, const char *page
)
472 const char *info
= get_man_viewer_info(name
);
474 if (!strcasecmp(name
, "man"))
475 exec_man_man(info
, page
);
476 else if (!strcasecmp(name
, "woman"))
477 exec_woman_emacs(info
, page
);
478 else if (!strcasecmp(name
, "konqueror"))
479 exec_man_konqueror(info
, page
);
481 exec_man_cmd(info
, page
);
483 warning(_("'%s': unknown man viewer."), name
);
486 static void show_man_page(const char *page
)
488 struct man_viewer_list
*viewer
;
489 const char *fallback
= getenv("GIT_MAN_VIEWER");
492 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
494 exec_viewer(viewer
->name
, page
); /* will return when unable */
497 exec_viewer(fallback
, page
);
498 exec_viewer("man", page
);
499 die(_("no man viewer handled the request"));
502 static void show_info_page(const char *page
)
504 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
505 execlp("info", "info", "gitman", page
, (char *)NULL
);
506 die(_("no info viewer handled the request"));
509 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
512 char *to_free
= NULL
;
515 html_path
= to_free
= system_path(GIT_HTML_PATH
);
518 * Check that the page we're looking for exists.
520 if (!strstr(html_path
, "://")) {
521 if (stat(mkpath("%s/%s.html", html_path
, page
), &st
)
522 || !S_ISREG(st
.st_mode
))
523 die("'%s/%s.html': documentation file not found.",
527 strbuf_init(page_path
, 0);
528 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
532 static void open_html(const char *path
)
534 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
537 static void show_html_page(const char *page
)
539 struct strbuf page_path
; /* it leaks but we exec bellow */
541 get_html_page_path(&page_path
, page
);
543 open_html(page_path
.buf
);
546 static const char *check_git_cmd(const char* cmd
)
550 if (is_git_command(cmd
))
553 alias
= alias_lookup(cmd
);
559 * handle_builtin() in git.c rewrites "git cmd --help"
560 * to "git help --exclude-guides cmd", so we can use
561 * exclude_guides to distinguish "git cmd --help" from
562 * "git help cmd". In the latter case, or if cmd is an
563 * alias for a shell command, just print the alias
566 if (!exclude_guides
|| alias
[0] == '!') {
567 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
572 * Otherwise, we pretend that the command was "git
573 * word0 --help". We use split_cmdline() to get the
574 * first word of the alias, to ensure that we use the
575 * same rules as when the alias is actually
576 * used. split_cmdline() modifies alias in-place.
578 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
579 count
= split_cmdline(alias
, &argv
);
581 die(_("bad alias.%s string: %s"), cmd
,
582 split_cmdline_strerror(count
));
589 return help_unknown_cmd(cmd
);
594 static void no_help_format(const char *opt_mode
, enum help_format fmt
)
599 case HELP_FORMAT_NONE
:
601 case HELP_FORMAT_MAN
:
604 case HELP_FORMAT_INFO
:
607 case HELP_FORMAT_WEB
:
614 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
615 builtin_help_usage
, builtin_help_options
, opt_mode
,
619 static void opt_mode_usage(int argc
, const char *opt_mode
,
620 enum help_format fmt
)
623 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
624 builtin_help_usage
, builtin_help_options
,
627 no_help_format(opt_mode
, fmt
);
630 int cmd_help(int argc
, const char **argv
, const char *prefix
)
633 enum help_format parsed_help_format
;
636 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
637 builtin_help_usage
, 0);
638 parsed_help_format
= help_format
;
640 if (cmd_mode
!= HELP_ACTION_ALL
&&
641 (show_external_commands
>= 0 ||
643 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
644 builtin_help_usage
, builtin_help_options
);
647 case HELP_ACTION_ALL
:
648 opt_mode_usage(argc
, "--all", help_format
);
651 list_all_cmds_help(show_external_commands
,
655 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
656 load_command_list("git-", &main_cmds
, &other_cmds
);
657 list_commands(&main_cmds
, &other_cmds
);
658 printf("%s\n", _(git_more_info_string
));
660 case HELP_ACTION_GUIDES
:
661 opt_mode_usage(argc
, "--guides", help_format
);
663 printf("%s\n", _(git_more_info_string
));
665 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
666 opt_mode_usage(argc
, "--config-for-completion", help_format
);
667 list_config_help(SHOW_CONFIG_VARS
);
669 case HELP_ACTION_USER_INTERFACES
:
670 opt_mode_usage(argc
, "--user-interfaces", help_format
);
671 list_user_interfaces_help();
673 case HELP_ACTION_DEVELOPER_INTERFACES
:
674 opt_mode_usage(argc
, "--developer-interfaces", help_format
);
675 list_developer_interfaces_help();
677 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
678 opt_mode_usage(argc
, "--config-sections-for-completion",
680 list_config_help(SHOW_CONFIG_SECTIONS
);
682 case HELP_ACTION_CONFIG
:
683 opt_mode_usage(argc
, "--config", help_format
);
685 list_config_help(SHOW_CONFIG_HUMAN
);
686 printf("\n%s\n", _("'git help config' for more information"));
691 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
692 list_common_cmds_help();
693 printf("\n%s\n", _(git_more_info_string
));
697 setup_git_directory_gently(&nongit
);
698 git_config(git_help_config
, NULL
);
700 if (parsed_help_format
!= HELP_FORMAT_NONE
)
701 help_format
= parsed_help_format
;
702 if (help_format
== HELP_FORMAT_NONE
)
703 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
705 argv
[0] = check_git_cmd(argv
[0]);
707 page
= cmd_to_page(argv
[0]);
708 switch (help_format
) {
709 case HELP_FORMAT_NONE
:
710 case HELP_FORMAT_MAN
:
713 case HELP_FORMAT_INFO
:
714 show_info_page(page
);
716 case HELP_FORMAT_WEB
:
717 show_html_page(page
);