9 #include "parse-options.h"
10 #include "run-command.h"
11 #include "config-list.h"
16 #ifndef DEFAULT_HELP_FORMAT
17 #define DEFAULT_HELP_FORMAT "man"
20 static struct man_viewer_list
{
21 struct man_viewer_list
*next
;
22 char name
[FLEX_ARRAY
];
25 static struct man_viewer_info_list
{
26 struct man_viewer_info_list
*next
;
28 char name
[FLEX_ARRAY
];
29 } *man_viewer_info_list
;
38 enum show_config_type
{
44 static enum help_action
{
48 HELP_ACTION_USER_INTERFACES
,
49 HELP_ACTION_DEVELOPER_INTERFACES
,
50 HELP_ACTION_CONFIG_FOR_COMPLETION
,
51 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
,
54 static const char *html_path
;
55 static int verbose
= 1;
56 static enum help_format help_format
= HELP_FORMAT_NONE
;
57 static int exclude_guides
;
58 static int show_external_commands
= -1;
59 static int show_aliases
= -1;
60 static struct option builtin_help_options
[] = {
61 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
63 OPT_BOOL(0, "external-commands", &show_external_commands
,
64 N_("show external commands in --all")),
65 OPT_BOOL(0, "aliases", &show_aliases
, N_("show aliases in --all")),
66 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
67 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
68 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
70 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
72 OPT__VERBOSE(&verbose
, N_("print command description")),
74 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
76 OPT_CMDMODE(0, "user-interfaces", &cmd_mode
,
77 N_("print list of user-facing repository, command and file interfaces"),
78 HELP_ACTION_USER_INTERFACES
),
79 OPT_CMDMODE(0, "developer-interfaces", &cmd_mode
,
80 N_("print list of file formats, protocols and other developer interfaces"),
81 HELP_ACTION_DEVELOPER_INTERFACES
),
82 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
84 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
85 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
86 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
87 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
92 static const char * const builtin_help_usage
[] = {
93 "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]",
94 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
95 "git help [-g|--guides]",
96 "git help [-c|--config]",
97 "git help [--user-interfaces]",
98 "git help [--developer-interfaces]",
102 struct slot_expansion
{
104 const char *placeholder
;
105 void (*fn
)(struct string_list
*list
, const char *prefix
);
109 static void list_config_help(enum show_config_type type
)
111 struct slot_expansion slot_expansions
[] = {
112 { "advice", "*", list_config_advices
},
113 { "color.branch", "<slot>", list_config_color_branch_slots
},
114 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
115 { "color.diff", "<slot>", list_config_color_diff_slots
},
116 { "color.grep", "<slot>", list_config_color_grep_slots
},
117 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
118 { "color.remote", "<slot>", list_config_color_sideband_slots
},
119 { "color.status", "<slot>", list_config_color_status_slots
},
120 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
121 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
125 struct slot_expansion
*e
;
126 struct string_list keys
= STRING_LIST_INIT_DUP
;
127 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
128 struct string_list_item
*item
;
131 for (p
= config_name_list
; *p
; p
++) {
132 const char *var
= *p
;
133 struct strbuf sb
= STRBUF_INIT
;
135 for (e
= slot_expansions
; e
->prefix
; e
++) {
138 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
139 if (!strcasecmp(var
, sb
.buf
)) {
140 e
->fn(&keys
, e
->prefix
);
147 string_list_append(&keys
, var
);
150 for (e
= slot_expansions
; e
->prefix
; e
++)
152 BUG("slot_expansion %s.%s is not used",
153 e
->prefix
, e
->placeholder
);
155 string_list_sort(&keys
);
156 for (i
= 0; i
< keys
.nr
; i
++) {
157 const char *var
= keys
.items
[i
].string
;
158 const char *wildcard
, *tag
, *cut
;
159 const char *dot
= NULL
;
160 struct strbuf sb
= STRBUF_INIT
;
163 case SHOW_CONFIG_HUMAN
:
166 case SHOW_CONFIG_SECTIONS
:
167 dot
= strchr(var
, '.');
169 case SHOW_CONFIG_VARS
:
172 wildcard
= strchr(var
, '*');
173 tag
= strchr(var
, '<');
175 if (!dot
&& !wildcard
&& !tag
) {
176 string_list_append(&keys_uniq
, var
);
182 else if (wildcard
&& !tag
)
184 else if (!wildcard
&& tag
)
187 cut
= wildcard
< tag
? wildcard
: tag
;
189 strbuf_add(&sb
, var
, cut
- var
);
190 string_list_append(&keys_uniq
, sb
.buf
);
194 string_list_clear(&keys
, 0);
195 string_list_remove_duplicates(&keys_uniq
, 0);
196 for_each_string_list_item(item
, &keys_uniq
)
198 string_list_clear(&keys_uniq
, 0);
201 static enum help_format
parse_help_format(const char *format
)
203 if (!strcmp(format
, "man"))
204 return HELP_FORMAT_MAN
;
205 if (!strcmp(format
, "info"))
206 return HELP_FORMAT_INFO
;
207 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
208 return HELP_FORMAT_WEB
;
210 * Please update _git_config() in git-completion.bash when you
211 * add new help formats.
213 die(_("unrecognized help format '%s'"), format
);
216 static const char *get_man_viewer_info(const char *name
)
218 struct man_viewer_info_list
*viewer
;
220 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
222 if (!strcasecmp(name
, viewer
->name
))
228 static int check_emacsclient_version(void)
230 struct strbuf buffer
= STRBUF_INIT
;
231 struct child_process ec_process
= CHILD_PROCESS_INIT
;
234 /* emacsclient prints its version number on stderr */
235 strvec_pushl(&ec_process
.args
, "emacsclient", "--version", NULL
);
237 ec_process
.stdout_to_stderr
= 1;
238 if (start_command(&ec_process
))
239 return error(_("Failed to start emacsclient."));
241 strbuf_read(&buffer
, ec_process
.err
, 20);
242 close(ec_process
.err
);
245 * Don't bother checking return value, because "emacsclient --version"
246 * seems to always exits with code 1.
248 finish_command(&ec_process
);
250 if (!starts_with(buffer
.buf
, "emacsclient")) {
251 strbuf_release(&buffer
);
252 return error(_("Failed to parse emacsclient version."));
255 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
256 version
= atoi(buffer
.buf
);
259 strbuf_release(&buffer
);
260 return error(_("emacsclient version '%d' too old (< 22)."),
264 strbuf_release(&buffer
);
268 static void exec_woman_emacs(const char *path
, const char *page
)
270 if (!check_emacsclient_version()) {
271 /* This works only with emacsclient version >= 22. */
272 struct strbuf man_page
= STRBUF_INIT
;
275 path
= "emacsclient";
276 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
277 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
278 warning_errno(_("failed to exec '%s'"), path
);
279 strbuf_release(&man_page
);
283 static void exec_man_konqueror(const char *path
, const char *page
)
285 const char *display
= getenv("DISPLAY");
286 if (display
&& *display
) {
287 struct strbuf man_page
= STRBUF_INIT
;
288 const char *filename
= "kfmclient";
290 /* It's simpler to launch konqueror using kfmclient. */
293 if (strip_suffix(path
, "/konqueror", &len
))
294 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
295 filename
= basename((char *)path
);
298 strbuf_addf(&man_page
, "man:%s(1)", page
);
299 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
300 warning_errno(_("failed to exec '%s'"), path
);
301 strbuf_release(&man_page
);
305 static void exec_man_man(const char *path
, const char *page
)
309 execlp(path
, "man", page
, (char *)NULL
);
310 warning_errno(_("failed to exec '%s'"), path
);
313 static void exec_man_cmd(const char *cmd
, const char *page
)
315 struct strbuf shell_cmd
= STRBUF_INIT
;
316 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
317 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
318 warning(_("failed to exec '%s'"), cmd
);
319 strbuf_release(&shell_cmd
);
322 static void add_man_viewer(const char *name
)
324 struct man_viewer_list
**p
= &man_viewer_list
;
328 FLEX_ALLOC_STR(*p
, name
, name
);
331 static int supported_man_viewer(const char *name
, size_t len
)
333 return (!strncasecmp("man", name
, len
) ||
334 !strncasecmp("woman", name
, len
) ||
335 !strncasecmp("konqueror", name
, len
));
338 static void do_add_man_viewer_info(const char *name
,
342 struct man_viewer_info_list
*new_man_viewer
;
343 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
344 new_man_viewer
->info
= xstrdup(value
);
345 new_man_viewer
->next
= man_viewer_info_list
;
346 man_viewer_info_list
= new_man_viewer
;
349 static int add_man_viewer_path(const char *name
,
353 if (supported_man_viewer(name
, len
))
354 do_add_man_viewer_info(name
, len
, value
);
356 warning(_("'%s': path for unsupported man viewer.\n"
357 "Please consider using 'man.<tool>.cmd' instead."),
363 static int add_man_viewer_cmd(const char *name
,
367 if (supported_man_viewer(name
, len
))
368 warning(_("'%s': cmd for supported man viewer.\n"
369 "Please consider using 'man.<tool>.path' instead."),
372 do_add_man_viewer_info(name
, len
, value
);
377 static int add_man_viewer_info(const char *var
, const char *value
)
379 const char *name
, *subkey
;
382 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
385 if (!strcmp(subkey
, "path")) {
387 return config_error_nonbool(var
);
388 return add_man_viewer_path(name
, namelen
, value
);
390 if (!strcmp(subkey
, "cmd")) {
392 return config_error_nonbool(var
);
393 return add_man_viewer_cmd(name
, namelen
, value
);
399 static int git_help_config(const char *var
, const char *value
, void *cb
)
401 if (!strcmp(var
, "help.format")) {
403 return config_error_nonbool(var
);
404 help_format
= parse_help_format(value
);
407 if (!strcmp(var
, "help.htmlpath")) {
409 return config_error_nonbool(var
);
410 html_path
= xstrdup(value
);
413 if (!strcmp(var
, "man.viewer")) {
415 return config_error_nonbool(var
);
416 add_man_viewer(value
);
419 if (starts_with(var
, "man."))
420 return add_man_viewer_info(var
, value
);
422 return git_default_config(var
, value
, cb
);
425 static struct cmdnames main_cmds
, other_cmds
;
427 static int is_git_command(const char *s
)
432 load_command_list("git-", &main_cmds
, &other_cmds
);
433 return is_in_cmdlist(&main_cmds
, s
) ||
434 is_in_cmdlist(&other_cmds
, s
);
437 static const char *cmd_to_page(const char *git_cmd
)
441 else if (starts_with(git_cmd
, "git"))
443 else if (is_git_command(git_cmd
))
444 return xstrfmt("git-%s", git_cmd
);
445 else if (!strcmp("scalar", git_cmd
))
446 return xstrdup(git_cmd
);
448 return xstrfmt("git%s", git_cmd
);
451 static void setup_man_path(void)
453 struct strbuf new_path
= STRBUF_INIT
;
454 const char *old_path
= getenv("MANPATH");
455 char *git_man_path
= system_path(GIT_MAN_PATH
);
457 /* We should always put ':' after our path. If there is no
458 * old_path, the ':' at the end will let 'man' to try
459 * system-wide paths after ours to find the manual page. If
460 * there is old_path, we need ':' as delimiter. */
461 strbuf_addstr(&new_path
, git_man_path
);
462 strbuf_addch(&new_path
, ':');
464 strbuf_addstr(&new_path
, old_path
);
467 setenv("MANPATH", new_path
.buf
, 1);
469 strbuf_release(&new_path
);
472 static void exec_viewer(const char *name
, const char *page
)
474 const char *info
= get_man_viewer_info(name
);
476 if (!strcasecmp(name
, "man"))
477 exec_man_man(info
, page
);
478 else if (!strcasecmp(name
, "woman"))
479 exec_woman_emacs(info
, page
);
480 else if (!strcasecmp(name
, "konqueror"))
481 exec_man_konqueror(info
, page
);
483 exec_man_cmd(info
, page
);
485 warning(_("'%s': unknown man viewer."), name
);
488 static void show_man_page(const char *page
)
490 struct man_viewer_list
*viewer
;
491 const char *fallback
= getenv("GIT_MAN_VIEWER");
494 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
496 exec_viewer(viewer
->name
, page
); /* will return when unable */
499 exec_viewer(fallback
, page
);
500 exec_viewer("man", page
);
501 die(_("no man viewer handled the request"));
504 static void show_info_page(const char *page
)
506 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
507 execlp("info", "info", "gitman", page
, (char *)NULL
);
508 die(_("no info viewer handled the request"));
511 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
514 char *to_free
= NULL
;
517 html_path
= to_free
= system_path(GIT_HTML_PATH
);
520 * Check that the page we're looking for exists.
522 if (!strstr(html_path
, "://")) {
523 if (stat(mkpath("%s/%s.html", html_path
, page
), &st
)
524 || !S_ISREG(st
.st_mode
))
525 die("'%s/%s.html': documentation file not found.",
529 strbuf_init(page_path
, 0);
530 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
534 static void open_html(const char *path
)
536 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
539 static void show_html_page(const char *page
)
541 struct strbuf page_path
; /* it leaks but we exec bellow */
543 get_html_page_path(&page_path
, page
);
545 open_html(page_path
.buf
);
548 static const char *check_git_cmd(const char* cmd
)
552 if (is_git_command(cmd
))
555 alias
= alias_lookup(cmd
);
561 * handle_builtin() in git.c rewrites "git cmd --help"
562 * to "git help --exclude-guides cmd", so we can use
563 * exclude_guides to distinguish "git cmd --help" from
564 * "git help cmd". In the latter case, or if cmd is an
565 * alias for a shell command, just print the alias
568 if (!exclude_guides
|| alias
[0] == '!') {
569 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
574 * Otherwise, we pretend that the command was "git
575 * word0 --help". We use split_cmdline() to get the
576 * first word of the alias, to ensure that we use the
577 * same rules as when the alias is actually
578 * used. split_cmdline() modifies alias in-place.
580 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
581 count
= split_cmdline(alias
, &argv
);
583 die(_("bad alias.%s string: %s"), cmd
,
584 split_cmdline_strerror(count
));
591 return help_unknown_cmd(cmd
);
596 static void no_help_format(const char *opt_mode
, enum help_format fmt
)
601 case HELP_FORMAT_NONE
:
603 case HELP_FORMAT_MAN
:
606 case HELP_FORMAT_INFO
:
609 case HELP_FORMAT_WEB
:
616 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
617 builtin_help_usage
, builtin_help_options
, opt_mode
,
621 static void opt_mode_usage(int argc
, const char *opt_mode
,
622 enum help_format fmt
)
625 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
626 builtin_help_usage
, builtin_help_options
,
629 no_help_format(opt_mode
, fmt
);
632 int cmd_help(int argc
, const char **argv
, const char *prefix
)
635 enum help_format parsed_help_format
;
638 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
639 builtin_help_usage
, 0);
640 parsed_help_format
= help_format
;
642 if (cmd_mode
!= HELP_ACTION_ALL
&&
643 (show_external_commands
>= 0 ||
645 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
646 builtin_help_usage
, builtin_help_options
);
649 case HELP_ACTION_ALL
:
650 opt_mode_usage(argc
, "--all", help_format
);
653 list_all_cmds_help(show_external_commands
,
657 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
658 load_command_list("git-", &main_cmds
, &other_cmds
);
659 list_commands(&main_cmds
, &other_cmds
);
660 printf("%s\n", _(git_more_info_string
));
662 case HELP_ACTION_GUIDES
:
663 opt_mode_usage(argc
, "--guides", help_format
);
665 printf("%s\n", _(git_more_info_string
));
667 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
668 opt_mode_usage(argc
, "--config-for-completion", help_format
);
669 list_config_help(SHOW_CONFIG_VARS
);
671 case HELP_ACTION_USER_INTERFACES
:
672 opt_mode_usage(argc
, "--user-interfaces", help_format
);
673 list_user_interfaces_help();
675 case HELP_ACTION_DEVELOPER_INTERFACES
:
676 opt_mode_usage(argc
, "--developer-interfaces", help_format
);
677 list_developer_interfaces_help();
679 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
680 opt_mode_usage(argc
, "--config-sections-for-completion",
682 list_config_help(SHOW_CONFIG_SECTIONS
);
684 case HELP_ACTION_CONFIG
:
685 opt_mode_usage(argc
, "--config", help_format
);
687 list_config_help(SHOW_CONFIG_HUMAN
);
688 printf("\n%s\n", _("'git help config' for more information"));
693 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
694 list_common_cmds_help();
695 printf("\n%s\n", _(git_more_info_string
));
699 setup_git_directory_gently(&nongit
);
700 git_config(git_help_config
, NULL
);
702 if (parsed_help_format
!= HELP_FORMAT_NONE
)
703 help_format
= parsed_help_format
;
704 if (help_format
== HELP_FORMAT_NONE
)
705 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
707 argv
[0] = check_git_cmd(argv
[0]);
709 page
= cmd_to_page(argv
[0]);
710 switch (help_format
) {
711 case HELP_FORMAT_NONE
:
712 case HELP_FORMAT_MAN
:
715 case HELP_FORMAT_INFO
:
716 show_info_page(page
);
718 case HELP_FORMAT_WEB
:
719 show_html_page(page
);