10 #include "parse-options.h"
11 #include "run-command.h"
12 #include "config-list.h"
17 #ifndef DEFAULT_HELP_FORMAT
18 #define DEFAULT_HELP_FORMAT "man"
21 static struct man_viewer_list
{
22 struct man_viewer_list
*next
;
23 char name
[FLEX_ARRAY
];
26 static struct man_viewer_info_list
{
27 struct man_viewer_info_list
*next
;
29 char name
[FLEX_ARRAY
];
30 } *man_viewer_info_list
;
39 enum show_config_type
{
45 static enum help_action
{
49 HELP_ACTION_USER_INTERFACES
,
50 HELP_ACTION_DEVELOPER_INTERFACES
,
51 HELP_ACTION_CONFIG_FOR_COMPLETION
,
52 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
,
55 static const char *html_path
;
56 static int verbose
= 1;
57 static enum help_format help_format
= HELP_FORMAT_NONE
;
58 static int exclude_guides
;
59 static int show_external_commands
= -1;
60 static int show_aliases
= -1;
61 static struct option builtin_help_options
[] = {
62 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
64 OPT_BOOL(0, "external-commands", &show_external_commands
,
65 N_("show external commands in --all")),
66 OPT_BOOL(0, "aliases", &show_aliases
, N_("show aliases in --all")),
67 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
68 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
69 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
71 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
73 OPT__VERBOSE(&verbose
, N_("print command description")),
75 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
77 OPT_CMDMODE(0, "user-interfaces", &cmd_mode
,
78 N_("print list of user-facing repository, command and file interfaces"),
79 HELP_ACTION_USER_INTERFACES
),
80 OPT_CMDMODE(0, "developer-interfaces", &cmd_mode
,
81 N_("print list of file formats, protocols and other developer interfaces"),
82 HELP_ACTION_DEVELOPER_INTERFACES
),
83 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
85 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
86 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
87 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
88 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
93 static const char * const builtin_help_usage
[] = {
94 "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]",
95 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
96 "git help [-g|--guides]",
97 "git help [-c|--config]",
98 "git help [--user-interfaces]",
99 "git help [--developer-interfaces]",
103 struct slot_expansion
{
105 const char *placeholder
;
106 void (*fn
)(struct string_list
*list
, const char *prefix
);
110 static void list_config_help(enum show_config_type type
)
112 struct slot_expansion slot_expansions
[] = {
113 { "advice", "*", list_config_advices
},
114 { "color.branch", "<slot>", list_config_color_branch_slots
},
115 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
116 { "color.diff", "<slot>", list_config_color_diff_slots
},
117 { "color.grep", "<slot>", list_config_color_grep_slots
},
118 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
119 { "color.remote", "<slot>", list_config_color_sideband_slots
},
120 { "color.status", "<slot>", list_config_color_status_slots
},
121 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
122 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
126 struct slot_expansion
*e
;
127 struct string_list keys
= STRING_LIST_INIT_DUP
;
128 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
129 struct string_list_item
*item
;
132 for (p
= config_name_list
; *p
; p
++) {
133 const char *var
= *p
;
134 struct strbuf sb
= STRBUF_INIT
;
136 for (e
= slot_expansions
; e
->prefix
; e
++) {
139 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
140 if (!strcasecmp(var
, sb
.buf
)) {
141 e
->fn(&keys
, e
->prefix
);
148 string_list_append(&keys
, var
);
151 for (e
= slot_expansions
; e
->prefix
; e
++)
153 BUG("slot_expansion %s.%s is not used",
154 e
->prefix
, e
->placeholder
);
156 string_list_sort(&keys
);
157 for (i
= 0; i
< keys
.nr
; i
++) {
158 const char *var
= keys
.items
[i
].string
;
159 const char *wildcard
, *tag
, *cut
;
160 const char *dot
= NULL
;
161 struct strbuf sb
= STRBUF_INIT
;
164 case SHOW_CONFIG_HUMAN
:
167 case SHOW_CONFIG_SECTIONS
:
168 dot
= strchr(var
, '.');
170 case SHOW_CONFIG_VARS
:
173 wildcard
= strchr(var
, '*');
174 tag
= strchr(var
, '<');
176 if (!dot
&& !wildcard
&& !tag
) {
177 string_list_append(&keys_uniq
, var
);
183 else if (wildcard
&& !tag
)
185 else if (!wildcard
&& tag
)
188 cut
= wildcard
< tag
? wildcard
: tag
;
190 strbuf_add(&sb
, var
, cut
- var
);
191 string_list_append(&keys_uniq
, sb
.buf
);
195 string_list_clear(&keys
, 0);
196 string_list_remove_duplicates(&keys_uniq
, 0);
197 for_each_string_list_item(item
, &keys_uniq
)
199 string_list_clear(&keys_uniq
, 0);
202 static enum help_format
parse_help_format(const char *format
)
204 if (!strcmp(format
, "man"))
205 return HELP_FORMAT_MAN
;
206 if (!strcmp(format
, "info"))
207 return HELP_FORMAT_INFO
;
208 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
209 return HELP_FORMAT_WEB
;
211 * Please update _git_config() in git-completion.bash when you
212 * add new help formats.
214 die(_("unrecognized help format '%s'"), format
);
217 static const char *get_man_viewer_info(const char *name
)
219 struct man_viewer_info_list
*viewer
;
221 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
223 if (!strcasecmp(name
, viewer
->name
))
229 static int check_emacsclient_version(void)
231 struct strbuf buffer
= STRBUF_INIT
;
232 struct child_process ec_process
= CHILD_PROCESS_INIT
;
235 /* emacsclient prints its version number on stderr */
236 strvec_pushl(&ec_process
.args
, "emacsclient", "--version", NULL
);
238 ec_process
.stdout_to_stderr
= 1;
239 if (start_command(&ec_process
))
240 return error(_("Failed to start emacsclient."));
242 strbuf_read(&buffer
, ec_process
.err
, 20);
243 close(ec_process
.err
);
246 * Don't bother checking return value, because "emacsclient --version"
247 * seems to always exits with code 1.
249 finish_command(&ec_process
);
251 if (!starts_with(buffer
.buf
, "emacsclient")) {
252 strbuf_release(&buffer
);
253 return error(_("Failed to parse emacsclient version."));
256 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
257 version
= atoi(buffer
.buf
);
260 strbuf_release(&buffer
);
261 return error(_("emacsclient version '%d' too old (< 22)."),
265 strbuf_release(&buffer
);
269 static void exec_woman_emacs(const char *path
, const char *page
)
271 if (!check_emacsclient_version()) {
272 /* This works only with emacsclient version >= 22. */
273 struct strbuf man_page
= STRBUF_INIT
;
276 path
= "emacsclient";
277 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
278 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
279 warning_errno(_("failed to exec '%s'"), path
);
280 strbuf_release(&man_page
);
284 static void exec_man_konqueror(const char *path
, const char *page
)
286 const char *display
= getenv("DISPLAY");
287 if (display
&& *display
) {
288 struct strbuf man_page
= STRBUF_INIT
;
289 const char *filename
= "kfmclient";
291 /* It's simpler to launch konqueror using kfmclient. */
294 if (strip_suffix(path
, "/konqueror", &len
))
295 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
296 filename
= basename((char *)path
);
299 strbuf_addf(&man_page
, "man:%s(1)", page
);
300 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
301 warning_errno(_("failed to exec '%s'"), path
);
302 strbuf_release(&man_page
);
306 static void exec_man_man(const char *path
, const char *page
)
310 execlp(path
, "man", page
, (char *)NULL
);
311 warning_errno(_("failed to exec '%s'"), path
);
314 static void exec_man_cmd(const char *cmd
, const char *page
)
316 struct strbuf shell_cmd
= STRBUF_INIT
;
317 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
318 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
319 warning(_("failed to exec '%s'"), cmd
);
320 strbuf_release(&shell_cmd
);
323 static void add_man_viewer(const char *name
)
325 struct man_viewer_list
**p
= &man_viewer_list
;
329 FLEX_ALLOC_STR(*p
, name
, name
);
332 static int supported_man_viewer(const char *name
, size_t len
)
334 return (!strncasecmp("man", name
, len
) ||
335 !strncasecmp("woman", name
, len
) ||
336 !strncasecmp("konqueror", name
, len
));
339 static void do_add_man_viewer_info(const char *name
,
343 struct man_viewer_info_list
*new_man_viewer
;
344 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
345 new_man_viewer
->info
= xstrdup(value
);
346 new_man_viewer
->next
= man_viewer_info_list
;
347 man_viewer_info_list
= new_man_viewer
;
350 static int add_man_viewer_path(const char *name
,
354 if (supported_man_viewer(name
, len
))
355 do_add_man_viewer_info(name
, len
, value
);
357 warning(_("'%s': path for unsupported man viewer.\n"
358 "Please consider using 'man.<tool>.cmd' instead."),
364 static int add_man_viewer_cmd(const char *name
,
368 if (supported_man_viewer(name
, len
))
369 warning(_("'%s': cmd for supported man viewer.\n"
370 "Please consider using 'man.<tool>.path' instead."),
373 do_add_man_viewer_info(name
, len
, value
);
378 static int add_man_viewer_info(const char *var
, const char *value
)
380 const char *name
, *subkey
;
383 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
386 if (!strcmp(subkey
, "path")) {
388 return config_error_nonbool(var
);
389 return add_man_viewer_path(name
, namelen
, value
);
391 if (!strcmp(subkey
, "cmd")) {
393 return config_error_nonbool(var
);
394 return add_man_viewer_cmd(name
, namelen
, value
);
400 static int git_help_config(const char *var
, const char *value
, void *cb
)
402 if (!strcmp(var
, "help.format")) {
404 return config_error_nonbool(var
);
405 help_format
= parse_help_format(value
);
408 if (!strcmp(var
, "help.htmlpath")) {
410 return config_error_nonbool(var
);
411 html_path
= xstrdup(value
);
414 if (!strcmp(var
, "man.viewer")) {
416 return config_error_nonbool(var
);
417 add_man_viewer(value
);
420 if (starts_with(var
, "man."))
421 return add_man_viewer_info(var
, value
);
423 return git_default_config(var
, value
, cb
);
426 static struct cmdnames main_cmds
, other_cmds
;
428 static int is_git_command(const char *s
)
433 load_command_list("git-", &main_cmds
, &other_cmds
);
434 return is_in_cmdlist(&main_cmds
, s
) ||
435 is_in_cmdlist(&other_cmds
, s
);
438 static const char *cmd_to_page(const char *git_cmd
)
442 else if (starts_with(git_cmd
, "git"))
444 else if (is_git_command(git_cmd
))
445 return xstrfmt("git-%s", git_cmd
);
446 else if (!strcmp("scalar", git_cmd
))
447 return xstrdup(git_cmd
);
449 return xstrfmt("git%s", git_cmd
);
452 static void setup_man_path(void)
454 struct strbuf new_path
= STRBUF_INIT
;
455 const char *old_path
= getenv("MANPATH");
456 char *git_man_path
= system_path(GIT_MAN_PATH
);
458 /* We should always put ':' after our path. If there is no
459 * old_path, the ':' at the end will let 'man' to try
460 * system-wide paths after ours to find the manual page. If
461 * there is old_path, we need ':' as delimiter. */
462 strbuf_addstr(&new_path
, git_man_path
);
463 strbuf_addch(&new_path
, ':');
465 strbuf_addstr(&new_path
, old_path
);
468 setenv("MANPATH", new_path
.buf
, 1);
470 strbuf_release(&new_path
);
473 static void exec_viewer(const char *name
, const char *page
)
475 const char *info
= get_man_viewer_info(name
);
477 if (!strcasecmp(name
, "man"))
478 exec_man_man(info
, page
);
479 else if (!strcasecmp(name
, "woman"))
480 exec_woman_emacs(info
, page
);
481 else if (!strcasecmp(name
, "konqueror"))
482 exec_man_konqueror(info
, page
);
484 exec_man_cmd(info
, page
);
486 warning(_("'%s': unknown man viewer."), name
);
489 static void show_man_page(const char *page
)
491 struct man_viewer_list
*viewer
;
492 const char *fallback
= getenv("GIT_MAN_VIEWER");
495 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
497 exec_viewer(viewer
->name
, page
); /* will return when unable */
500 exec_viewer(fallback
, page
);
501 exec_viewer("man", page
);
502 die(_("no man viewer handled the request"));
505 static void show_info_page(const char *page
)
507 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
508 execlp("info", "info", "gitman", page
, (char *)NULL
);
509 die(_("no info viewer handled the request"));
512 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
515 char *to_free
= NULL
;
518 html_path
= to_free
= system_path(GIT_HTML_PATH
);
521 * Check that the page we're looking for exists.
523 if (!strstr(html_path
, "://")) {
524 if (stat(mkpath("%s/%s.html", html_path
, page
), &st
)
525 || !S_ISREG(st
.st_mode
))
526 die("'%s/%s.html': documentation file not found.",
530 strbuf_init(page_path
, 0);
531 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
535 static void open_html(const char *path
)
537 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
540 static void show_html_page(const char *page
)
542 struct strbuf page_path
; /* it leaks but we exec bellow */
544 get_html_page_path(&page_path
, page
);
546 open_html(page_path
.buf
);
549 static const char *check_git_cmd(const char* cmd
)
553 if (is_git_command(cmd
))
556 alias
= alias_lookup(cmd
);
562 * handle_builtin() in git.c rewrites "git cmd --help"
563 * to "git help --exclude-guides cmd", so we can use
564 * exclude_guides to distinguish "git cmd --help" from
565 * "git help cmd". In the latter case, or if cmd is an
566 * alias for a shell command, just print the alias
569 if (!exclude_guides
|| alias
[0] == '!') {
570 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
575 * Otherwise, we pretend that the command was "git
576 * word0 --help". We use split_cmdline() to get the
577 * first word of the alias, to ensure that we use the
578 * same rules as when the alias is actually
579 * used. split_cmdline() modifies alias in-place.
581 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
582 count
= split_cmdline(alias
, &argv
);
584 die(_("bad alias.%s string: %s"), cmd
,
585 split_cmdline_strerror(count
));
592 return help_unknown_cmd(cmd
);
597 static void no_help_format(const char *opt_mode
, enum help_format fmt
)
602 case HELP_FORMAT_NONE
:
604 case HELP_FORMAT_MAN
:
607 case HELP_FORMAT_INFO
:
610 case HELP_FORMAT_WEB
:
617 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
618 builtin_help_usage
, builtin_help_options
, opt_mode
,
622 static void opt_mode_usage(int argc
, const char *opt_mode
,
623 enum help_format fmt
)
626 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
627 builtin_help_usage
, builtin_help_options
,
630 no_help_format(opt_mode
, fmt
);
633 int cmd_help(int argc
, const char **argv
, const char *prefix
)
636 enum help_format parsed_help_format
;
639 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
640 builtin_help_usage
, 0);
641 parsed_help_format
= help_format
;
643 if (cmd_mode
!= HELP_ACTION_ALL
&&
644 (show_external_commands
>= 0 ||
646 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
647 builtin_help_usage
, builtin_help_options
);
650 case HELP_ACTION_ALL
:
651 opt_mode_usage(argc
, "--all", help_format
);
654 list_all_cmds_help(show_external_commands
,
658 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
659 load_command_list("git-", &main_cmds
, &other_cmds
);
660 list_commands(&main_cmds
, &other_cmds
);
661 printf("%s\n", _(git_more_info_string
));
663 case HELP_ACTION_GUIDES
:
664 opt_mode_usage(argc
, "--guides", help_format
);
666 printf("%s\n", _(git_more_info_string
));
668 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
669 opt_mode_usage(argc
, "--config-for-completion", help_format
);
670 list_config_help(SHOW_CONFIG_VARS
);
672 case HELP_ACTION_USER_INTERFACES
:
673 opt_mode_usage(argc
, "--user-interfaces", help_format
);
674 list_user_interfaces_help();
676 case HELP_ACTION_DEVELOPER_INTERFACES
:
677 opt_mode_usage(argc
, "--developer-interfaces", help_format
);
678 list_developer_interfaces_help();
680 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
681 opt_mode_usage(argc
, "--config-sections-for-completion",
683 list_config_help(SHOW_CONFIG_SECTIONS
);
685 case HELP_ACTION_CONFIG
:
686 opt_mode_usage(argc
, "--config", help_format
);
688 list_config_help(SHOW_CONFIG_HUMAN
);
689 printf("\n%s\n", _("'git help config' for more information"));
694 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
695 list_common_cmds_help();
696 printf("\n%s\n", _(git_more_info_string
));
700 setup_git_directory_gently(&nongit
);
701 git_config(git_help_config
, NULL
);
703 if (parsed_help_format
!= HELP_FORMAT_NONE
)
704 help_format
= parsed_help_format
;
705 if (help_format
== HELP_FORMAT_NONE
)
706 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
708 argv
[0] = check_git_cmd(argv
[0]);
710 page
= cmd_to_page(argv
[0]);
711 switch (help_format
) {
712 case HELP_FORMAT_NONE
:
713 case HELP_FORMAT_MAN
:
716 case HELP_FORMAT_INFO
:
717 show_info_page(page
);
719 case HELP_FORMAT_WEB
:
720 show_html_page(page
);