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_CONFIG_FOR_COMPLETION
,
47 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
,
50 static const char *html_path
;
51 static int verbose
= 1;
52 static enum help_format help_format
= HELP_FORMAT_NONE
;
53 static int exclude_guides
;
54 static int show_external_commands
= -1;
55 static int show_aliases
= -1;
56 static struct option builtin_help_options
[] = {
57 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
59 OPT_BOOL(0, "external-commands", &show_external_commands
,
60 N_("show external commands in --all")),
61 OPT_BOOL(0, "aliases", &show_aliases
, N_("show aliases in --all")),
62 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
63 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
64 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
66 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
68 OPT__VERBOSE(&verbose
, N_("print command description")),
70 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
72 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
74 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
75 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
76 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
77 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
82 static const char * const builtin_help_usage
[] = {
83 "git help [-a|--all] [--[no-]verbose]] [--[no-]external-commands] [--[no-]aliases]",
84 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
85 "git help [-g|--guides]",
86 "git help [-c|--config]",
90 struct slot_expansion
{
92 const char *placeholder
;
93 void (*fn
)(struct string_list
*list
, const char *prefix
);
97 static void list_config_help(enum show_config_type type
)
99 struct slot_expansion slot_expansions
[] = {
100 { "advice", "*", list_config_advices
},
101 { "color.branch", "<slot>", list_config_color_branch_slots
},
102 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
103 { "color.diff", "<slot>", list_config_color_diff_slots
},
104 { "color.grep", "<slot>", list_config_color_grep_slots
},
105 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
106 { "color.remote", "<slot>", list_config_color_sideband_slots
},
107 { "color.status", "<slot>", list_config_color_status_slots
},
108 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
109 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
113 struct slot_expansion
*e
;
114 struct string_list keys
= STRING_LIST_INIT_DUP
;
115 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
116 struct string_list_item
*item
;
119 for (p
= config_name_list
; *p
; p
++) {
120 const char *var
= *p
;
121 struct strbuf sb
= STRBUF_INIT
;
123 for (e
= slot_expansions
; e
->prefix
; e
++) {
126 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
127 if (!strcasecmp(var
, sb
.buf
)) {
128 e
->fn(&keys
, e
->prefix
);
135 string_list_append(&keys
, var
);
138 for (e
= slot_expansions
; e
->prefix
; e
++)
140 BUG("slot_expansion %s.%s is not used",
141 e
->prefix
, e
->placeholder
);
143 string_list_sort(&keys
);
144 for (i
= 0; i
< keys
.nr
; i
++) {
145 const char *var
= keys
.items
[i
].string
;
146 const char *wildcard
, *tag
, *cut
;
147 const char *dot
= NULL
;
148 struct strbuf sb
= STRBUF_INIT
;
151 case SHOW_CONFIG_HUMAN
:
154 case SHOW_CONFIG_SECTIONS
:
155 dot
= strchr(var
, '.');
157 case SHOW_CONFIG_VARS
:
160 wildcard
= strchr(var
, '*');
161 tag
= strchr(var
, '<');
163 if (!dot
&& !wildcard
&& !tag
) {
164 string_list_append(&keys_uniq
, var
);
170 else if (wildcard
&& !tag
)
172 else if (!wildcard
&& tag
)
175 cut
= wildcard
< tag
? wildcard
: tag
;
177 strbuf_add(&sb
, var
, cut
- var
);
178 string_list_append(&keys_uniq
, sb
.buf
);
182 string_list_clear(&keys
, 0);
183 string_list_remove_duplicates(&keys_uniq
, 0);
184 for_each_string_list_item(item
, &keys_uniq
)
186 string_list_clear(&keys_uniq
, 0);
189 static enum help_format
parse_help_format(const char *format
)
191 if (!strcmp(format
, "man"))
192 return HELP_FORMAT_MAN
;
193 if (!strcmp(format
, "info"))
194 return HELP_FORMAT_INFO
;
195 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
196 return HELP_FORMAT_WEB
;
198 * Please update _git_config() in git-completion.bash when you
199 * add new help formats.
201 die(_("unrecognized help format '%s'"), format
);
204 static const char *get_man_viewer_info(const char *name
)
206 struct man_viewer_info_list
*viewer
;
208 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
210 if (!strcasecmp(name
, viewer
->name
))
216 static int check_emacsclient_version(void)
218 struct strbuf buffer
= STRBUF_INIT
;
219 struct child_process ec_process
= CHILD_PROCESS_INIT
;
222 /* emacsclient prints its version number on stderr */
223 strvec_pushl(&ec_process
.args
, "emacsclient", "--version", NULL
);
225 ec_process
.stdout_to_stderr
= 1;
226 if (start_command(&ec_process
))
227 return error(_("Failed to start emacsclient."));
229 strbuf_read(&buffer
, ec_process
.err
, 20);
230 close(ec_process
.err
);
233 * Don't bother checking return value, because "emacsclient --version"
234 * seems to always exits with code 1.
236 finish_command(&ec_process
);
238 if (!starts_with(buffer
.buf
, "emacsclient")) {
239 strbuf_release(&buffer
);
240 return error(_("Failed to parse emacsclient version."));
243 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
244 version
= atoi(buffer
.buf
);
247 strbuf_release(&buffer
);
248 return error(_("emacsclient version '%d' too old (< 22)."),
252 strbuf_release(&buffer
);
256 static void exec_woman_emacs(const char *path
, const char *page
)
258 if (!check_emacsclient_version()) {
259 /* This works only with emacsclient version >= 22. */
260 struct strbuf man_page
= STRBUF_INIT
;
263 path
= "emacsclient";
264 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
265 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
266 warning_errno(_("failed to exec '%s'"), path
);
267 strbuf_release(&man_page
);
271 static void exec_man_konqueror(const char *path
, const char *page
)
273 const char *display
= getenv("DISPLAY");
274 if (display
&& *display
) {
275 struct strbuf man_page
= STRBUF_INIT
;
276 const char *filename
= "kfmclient";
278 /* It's simpler to launch konqueror using kfmclient. */
281 if (strip_suffix(path
, "/konqueror", &len
))
282 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
283 filename
= basename((char *)path
);
286 strbuf_addf(&man_page
, "man:%s(1)", page
);
287 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
288 warning_errno(_("failed to exec '%s'"), path
);
289 strbuf_release(&man_page
);
293 static void exec_man_man(const char *path
, const char *page
)
297 execlp(path
, "man", page
, (char *)NULL
);
298 warning_errno(_("failed to exec '%s'"), path
);
301 static void exec_man_cmd(const char *cmd
, const char *page
)
303 struct strbuf shell_cmd
= STRBUF_INIT
;
304 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
305 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
306 warning(_("failed to exec '%s'"), cmd
);
307 strbuf_release(&shell_cmd
);
310 static void add_man_viewer(const char *name
)
312 struct man_viewer_list
**p
= &man_viewer_list
;
316 FLEX_ALLOC_STR(*p
, name
, name
);
319 static int supported_man_viewer(const char *name
, size_t len
)
321 return (!strncasecmp("man", name
, len
) ||
322 !strncasecmp("woman", name
, len
) ||
323 !strncasecmp("konqueror", name
, len
));
326 static void do_add_man_viewer_info(const char *name
,
330 struct man_viewer_info_list
*new_man_viewer
;
331 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
332 new_man_viewer
->info
= xstrdup(value
);
333 new_man_viewer
->next
= man_viewer_info_list
;
334 man_viewer_info_list
= new_man_viewer
;
337 static int add_man_viewer_path(const char *name
,
341 if (supported_man_viewer(name
, len
))
342 do_add_man_viewer_info(name
, len
, value
);
344 warning(_("'%s': path for unsupported man viewer.\n"
345 "Please consider using 'man.<tool>.cmd' instead."),
351 static int add_man_viewer_cmd(const char *name
,
355 if (supported_man_viewer(name
, len
))
356 warning(_("'%s': cmd for supported man viewer.\n"
357 "Please consider using 'man.<tool>.path' instead."),
360 do_add_man_viewer_info(name
, len
, value
);
365 static int add_man_viewer_info(const char *var
, const char *value
)
367 const char *name
, *subkey
;
370 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
373 if (!strcmp(subkey
, "path")) {
375 return config_error_nonbool(var
);
376 return add_man_viewer_path(name
, namelen
, value
);
378 if (!strcmp(subkey
, "cmd")) {
380 return config_error_nonbool(var
);
381 return add_man_viewer_cmd(name
, namelen
, value
);
387 static int git_help_config(const char *var
, const char *value
, void *cb
)
389 if (!strcmp(var
, "help.format")) {
391 return config_error_nonbool(var
);
392 help_format
= parse_help_format(value
);
395 if (!strcmp(var
, "help.htmlpath")) {
397 return config_error_nonbool(var
);
398 html_path
= xstrdup(value
);
401 if (!strcmp(var
, "man.viewer")) {
403 return config_error_nonbool(var
);
404 add_man_viewer(value
);
407 if (starts_with(var
, "man."))
408 return add_man_viewer_info(var
, value
);
410 return git_default_config(var
, value
, cb
);
413 static struct cmdnames main_cmds
, other_cmds
;
415 static int is_git_command(const char *s
)
420 load_command_list("git-", &main_cmds
, &other_cmds
);
421 return is_in_cmdlist(&main_cmds
, s
) ||
422 is_in_cmdlist(&other_cmds
, s
);
425 static const char *cmd_to_page(const char *git_cmd
)
429 else if (starts_with(git_cmd
, "git"))
431 else if (is_git_command(git_cmd
))
432 return xstrfmt("git-%s", git_cmd
);
434 return xstrfmt("git%s", git_cmd
);
437 static void setup_man_path(void)
439 struct strbuf new_path
= STRBUF_INIT
;
440 const char *old_path
= getenv("MANPATH");
441 char *git_man_path
= system_path(GIT_MAN_PATH
);
443 /* We should always put ':' after our path. If there is no
444 * old_path, the ':' at the end will let 'man' to try
445 * system-wide paths after ours to find the manual page. If
446 * there is old_path, we need ':' as delimiter. */
447 strbuf_addstr(&new_path
, git_man_path
);
448 strbuf_addch(&new_path
, ':');
450 strbuf_addstr(&new_path
, old_path
);
453 setenv("MANPATH", new_path
.buf
, 1);
455 strbuf_release(&new_path
);
458 static void exec_viewer(const char *name
, const char *page
)
460 const char *info
= get_man_viewer_info(name
);
462 if (!strcasecmp(name
, "man"))
463 exec_man_man(info
, page
);
464 else if (!strcasecmp(name
, "woman"))
465 exec_woman_emacs(info
, page
);
466 else if (!strcasecmp(name
, "konqueror"))
467 exec_man_konqueror(info
, page
);
469 exec_man_cmd(info
, page
);
471 warning(_("'%s': unknown man viewer."), name
);
474 static void show_man_page(const char *page
)
476 struct man_viewer_list
*viewer
;
477 const char *fallback
= getenv("GIT_MAN_VIEWER");
480 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
482 exec_viewer(viewer
->name
, page
); /* will return when unable */
485 exec_viewer(fallback
, page
);
486 exec_viewer("man", page
);
487 die(_("no man viewer handled the request"));
490 static void show_info_page(const char *page
)
492 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
493 execlp("info", "info", "gitman", page
, (char *)NULL
);
494 die(_("no info viewer handled the request"));
497 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
500 char *to_free
= NULL
;
503 html_path
= to_free
= system_path(GIT_HTML_PATH
);
506 * Check that the page we're looking for exists.
508 if (!strstr(html_path
, "://")) {
509 if (stat(mkpath("%s/%s.html", html_path
, page
), &st
)
510 || !S_ISREG(st
.st_mode
))
511 die("'%s/%s.html': documentation file not found.",
515 strbuf_init(page_path
, 0);
516 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
520 static void open_html(const char *path
)
522 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
525 static void show_html_page(const char *page
)
527 struct strbuf page_path
; /* it leaks but we exec bellow */
529 get_html_page_path(&page_path
, page
);
531 open_html(page_path
.buf
);
534 static const char *check_git_cmd(const char* cmd
)
538 if (is_git_command(cmd
))
541 alias
= alias_lookup(cmd
);
547 * handle_builtin() in git.c rewrites "git cmd --help"
548 * to "git help --exclude-guides cmd", so we can use
549 * exclude_guides to distinguish "git cmd --help" from
550 * "git help cmd". In the latter case, or if cmd is an
551 * alias for a shell command, just print the alias
554 if (!exclude_guides
|| alias
[0] == '!') {
555 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
560 * Otherwise, we pretend that the command was "git
561 * word0 --help". We use split_cmdline() to get the
562 * first word of the alias, to ensure that we use the
563 * same rules as when the alias is actually
564 * used. split_cmdline() modifies alias in-place.
566 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
567 count
= split_cmdline(alias
, &argv
);
569 die(_("bad alias.%s string: %s"), cmd
,
570 split_cmdline_strerror(count
));
577 return help_unknown_cmd(cmd
);
582 static void no_help_format(const char *opt_mode
, enum help_format fmt
)
587 case HELP_FORMAT_NONE
:
589 case HELP_FORMAT_MAN
:
592 case HELP_FORMAT_INFO
:
595 case HELP_FORMAT_WEB
:
602 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
603 builtin_help_usage
, builtin_help_options
, opt_mode
,
607 static void opt_mode_usage(int argc
, const char *opt_mode
,
608 enum help_format fmt
)
611 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
612 builtin_help_usage
, builtin_help_options
,
615 no_help_format(opt_mode
, fmt
);
618 int cmd_help(int argc
, const char **argv
, const char *prefix
)
621 enum help_format parsed_help_format
;
624 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
625 builtin_help_usage
, 0);
626 parsed_help_format
= help_format
;
628 if (cmd_mode
!= HELP_ACTION_ALL
&&
629 (show_external_commands
>= 0 ||
631 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
632 builtin_help_usage
, builtin_help_options
);
635 case HELP_ACTION_ALL
:
636 opt_mode_usage(argc
, "--all", help_format
);
639 list_all_cmds_help(show_external_commands
,
643 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
644 load_command_list("git-", &main_cmds
, &other_cmds
);
645 list_commands(&main_cmds
, &other_cmds
);
646 printf("%s\n", _(git_more_info_string
));
648 case HELP_ACTION_GUIDES
:
649 opt_mode_usage(argc
, "--guides", help_format
);
651 printf("%s\n", _(git_more_info_string
));
653 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
654 opt_mode_usage(argc
, "--config-for-completion", help_format
);
655 list_config_help(SHOW_CONFIG_VARS
);
657 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
658 opt_mode_usage(argc
, "--config-sections-for-completion",
660 list_config_help(SHOW_CONFIG_SECTIONS
);
662 case HELP_ACTION_CONFIG
:
663 opt_mode_usage(argc
, "--config", help_format
);
665 list_config_help(SHOW_CONFIG_HUMAN
);
666 printf("\n%s\n", _("'git help config' for more information"));
671 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
672 list_common_cmds_help();
673 printf("\n%s\n", _(git_more_info_string
));
677 setup_git_directory_gently(&nongit
);
678 git_config(git_help_config
, NULL
);
680 if (parsed_help_format
!= HELP_FORMAT_NONE
)
681 help_format
= parsed_help_format
;
682 if (help_format
== HELP_FORMAT_NONE
)
683 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
685 argv
[0] = check_git_cmd(argv
[0]);
687 page
= cmd_to_page(argv
[0]);
688 switch (help_format
) {
689 case HELP_FORMAT_NONE
:
690 case HELP_FORMAT_MAN
:
693 case HELP_FORMAT_INFO
:
694 show_info_page(page
);
696 case HELP_FORMAT_WEB
:
697 show_html_page(page
);