5 #define USE_THE_REPOSITORY_VARIABLE
11 #include "parse-options.h"
13 #include "run-command.h"
14 #include "config-list.h"
19 #ifndef DEFAULT_HELP_FORMAT
20 #define DEFAULT_HELP_FORMAT "man"
23 static struct man_viewer_list
{
24 struct man_viewer_list
*next
;
25 char name
[FLEX_ARRAY
];
28 static struct man_viewer_info_list
{
29 struct man_viewer_info_list
*next
;
31 char name
[FLEX_ARRAY
];
32 } *man_viewer_info_list
;
41 enum show_config_type
{
47 static enum help_action
{
51 HELP_ACTION_USER_INTERFACES
,
52 HELP_ACTION_DEVELOPER_INTERFACES
,
53 HELP_ACTION_CONFIG_FOR_COMPLETION
,
54 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
,
57 static char *html_path
;
58 static int verbose
= 1;
59 static enum help_format help_format
= HELP_FORMAT_NONE
;
60 static int exclude_guides
;
61 static int show_external_commands
= -1;
62 static int show_aliases
= -1;
63 static struct option builtin_help_options
[] = {
64 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
66 OPT_BOOL(0, "external-commands", &show_external_commands
,
67 N_("show external commands in --all")),
68 OPT_BOOL(0, "aliases", &show_aliases
, N_("show aliases in --all")),
69 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
70 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
71 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
73 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
75 OPT__VERBOSE(&verbose
, N_("print command description")),
77 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
79 OPT_CMDMODE(0, "user-interfaces", &cmd_mode
,
80 N_("print list of user-facing repository, command and file interfaces"),
81 HELP_ACTION_USER_INTERFACES
),
82 OPT_CMDMODE(0, "developer-interfaces", &cmd_mode
,
83 N_("print list of file formats, protocols and other developer interfaces"),
84 HELP_ACTION_DEVELOPER_INTERFACES
),
85 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
87 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
88 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
89 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
90 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
95 static const char * const builtin_help_usage
[] = {
96 "git help [-a|--all] [--[no-]verbose] [--[no-]external-commands] [--[no-]aliases]",
97 N_("git help [[-i|--info] [-m|--man] [-w|--web]] [<command>|<doc>]"),
98 "git help [-g|--guides]",
99 "git help [-c|--config]",
100 "git help [--user-interfaces]",
101 "git help [--developer-interfaces]",
105 struct slot_expansion
{
107 const char *placeholder
;
108 void (*fn
)(struct string_list
*list
, const char *prefix
);
112 static void list_config_help(enum show_config_type type
)
114 struct slot_expansion slot_expansions
[] = {
115 { "advice", "*", list_config_advices
},
116 { "color.branch", "<slot>", list_config_color_branch_slots
},
117 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
118 { "color.diff", "<slot>", list_config_color_diff_slots
},
119 { "color.grep", "<slot>", list_config_color_grep_slots
},
120 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
121 { "color.remote", "<slot>", list_config_color_sideband_slots
},
122 { "color.status", "<slot>", list_config_color_status_slots
},
123 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
124 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
128 struct slot_expansion
*e
;
129 struct string_list keys
= STRING_LIST_INIT_DUP
;
130 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
131 struct string_list_item
*item
;
134 for (p
= config_name_list
; *p
; p
++) {
135 const char *var
= *p
;
136 struct strbuf sb
= STRBUF_INIT
;
138 for (e
= slot_expansions
; e
->prefix
; e
++) {
141 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
142 if (!strcasecmp(var
, sb
.buf
)) {
143 e
->fn(&keys
, e
->prefix
);
150 string_list_append(&keys
, var
);
153 for (e
= slot_expansions
; e
->prefix
; e
++)
155 BUG("slot_expansion %s.%s is not used",
156 e
->prefix
, e
->placeholder
);
158 string_list_sort(&keys
);
159 for (i
= 0; i
< keys
.nr
; i
++) {
160 const char *var
= keys
.items
[i
].string
;
161 const char *wildcard
, *tag
, *cut
;
162 const char *dot
= NULL
;
163 struct strbuf sb
= STRBUF_INIT
;
166 case SHOW_CONFIG_HUMAN
:
169 case SHOW_CONFIG_SECTIONS
:
170 dot
= strchr(var
, '.');
172 case SHOW_CONFIG_VARS
:
175 wildcard
= strchr(var
, '*');
176 tag
= strchr(var
, '<');
178 if (!dot
&& !wildcard
&& !tag
) {
179 string_list_append(&keys_uniq
, var
);
185 else if (wildcard
&& !tag
)
187 else if (!wildcard
&& tag
)
190 cut
= wildcard
< tag
? wildcard
: tag
;
192 strbuf_add(&sb
, var
, cut
- var
);
193 string_list_append(&keys_uniq
, sb
.buf
);
197 string_list_clear(&keys
, 0);
198 string_list_remove_duplicates(&keys_uniq
, 0);
199 for_each_string_list_item(item
, &keys_uniq
)
201 string_list_clear(&keys_uniq
, 0);
204 static enum help_format
parse_help_format(const char *format
)
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
;
213 * Please update _git_config() in git-completion.bash when you
214 * add new help formats.
216 die(_("unrecognized help format '%s'"), format
);
219 static const char *get_man_viewer_info(const char *name
)
221 struct man_viewer_info_list
*viewer
;
223 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
225 if (!strcasecmp(name
, viewer
->name
))
231 static int check_emacsclient_version(void)
233 struct strbuf buffer
= STRBUF_INIT
;
234 struct child_process ec_process
= CHILD_PROCESS_INIT
;
237 /* emacsclient prints its version number on stderr */
238 strvec_pushl(&ec_process
.args
, "emacsclient", "--version", NULL
);
240 ec_process
.stdout_to_stderr
= 1;
241 if (start_command(&ec_process
))
242 return error(_("Failed to start emacsclient."));
244 strbuf_read(&buffer
, ec_process
.err
, 20);
245 close(ec_process
.err
);
248 * Don't bother checking return value, because "emacsclient --version"
249 * seems to always exits with code 1.
251 finish_command(&ec_process
);
253 if (!starts_with(buffer
.buf
, "emacsclient")) {
254 strbuf_release(&buffer
);
255 return error(_("Failed to parse emacsclient version."));
258 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
259 version
= atoi(buffer
.buf
);
262 strbuf_release(&buffer
);
263 return error(_("emacsclient version '%d' too old (< 22)."),
267 strbuf_release(&buffer
);
271 static void exec_woman_emacs(const char *path
, const char *page
)
273 if (!check_emacsclient_version()) {
274 /* This works only with emacsclient version >= 22. */
275 struct strbuf man_page
= STRBUF_INIT
;
278 path
= "emacsclient";
279 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
280 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
281 warning_errno(_("failed to exec '%s'"), path
);
282 strbuf_release(&man_page
);
286 static void exec_man_konqueror(const char *path
, const char *page
)
288 const char *display
= getenv("DISPLAY");
289 if (display
&& *display
) {
290 struct strbuf man_page
= STRBUF_INIT
;
291 const char *filename
= "kfmclient";
293 /* It's simpler to launch konqueror using kfmclient. */
296 if (strip_suffix(path
, "/konqueror", &len
))
297 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
298 filename
= basename((char *)path
);
301 strbuf_addf(&man_page
, "man:%s(1)", page
);
302 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
303 warning_errno(_("failed to exec '%s'"), path
);
304 strbuf_release(&man_page
);
308 static void exec_man_man(const char *path
, const char *page
)
312 execlp(path
, "man", page
, (char *)NULL
);
313 warning_errno(_("failed to exec '%s'"), path
);
316 static void exec_man_cmd(const char *cmd
, const char *page
)
318 struct strbuf shell_cmd
= STRBUF_INIT
;
319 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
320 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
321 warning(_("failed to exec '%s'"), cmd
);
322 strbuf_release(&shell_cmd
);
325 static void add_man_viewer(const char *name
)
327 struct man_viewer_list
**p
= &man_viewer_list
;
331 FLEX_ALLOC_STR(*p
, name
, name
);
334 static int supported_man_viewer(const char *name
, size_t len
)
336 return (!strncasecmp("man", name
, len
) ||
337 !strncasecmp("woman", name
, len
) ||
338 !strncasecmp("konqueror", name
, len
));
341 static void do_add_man_viewer_info(const char *name
,
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
;
352 static int add_man_viewer_path(const char *name
,
356 if (supported_man_viewer(name
, len
))
357 do_add_man_viewer_info(name
, len
, value
);
359 warning(_("'%s': path for unsupported man viewer.\n"
360 "Please consider using 'man.<tool>.cmd' instead."),
366 static int add_man_viewer_cmd(const char *name
,
370 if (supported_man_viewer(name
, len
))
371 warning(_("'%s': cmd for supported man viewer.\n"
372 "Please consider using 'man.<tool>.path' instead."),
375 do_add_man_viewer_info(name
, len
, value
);
380 static int add_man_viewer_info(const char *var
, const char *value
)
382 const char *name
, *subkey
;
385 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
388 if (!strcmp(subkey
, "path")) {
390 return config_error_nonbool(var
);
391 return add_man_viewer_path(name
, namelen
, value
);
393 if (!strcmp(subkey
, "cmd")) {
395 return config_error_nonbool(var
);
396 return add_man_viewer_cmd(name
, namelen
, value
);
402 static int git_help_config(const char *var
, const char *value
,
403 const struct config_context
*ctx
, void *cb
)
405 if (!strcmp(var
, "help.format")) {
407 return config_error_nonbool(var
);
408 help_format
= parse_help_format(value
);
411 if (!strcmp(var
, "help.htmlpath")) {
413 return config_error_nonbool(var
);
415 html_path
= xstrdup(value
);
418 if (!strcmp(var
, "man.viewer")) {
420 return config_error_nonbool(var
);
421 add_man_viewer(value
);
424 if (starts_with(var
, "man."))
425 return add_man_viewer_info(var
, value
);
427 return git_default_config(var
, value
, ctx
, cb
);
430 static struct cmdnames main_cmds
, other_cmds
;
432 static int is_git_command(const char *s
)
437 load_command_list("git-", &main_cmds
, &other_cmds
);
438 return is_in_cmdlist(&main_cmds
, s
) ||
439 is_in_cmdlist(&other_cmds
, s
);
442 static const char *cmd_to_page(const char *git_cmd
)
446 else if (starts_with(git_cmd
, "git"))
448 else if (is_git_command(git_cmd
))
449 return xstrfmt("git-%s", git_cmd
);
450 else if (!strcmp("scalar", git_cmd
))
451 return xstrdup(git_cmd
);
453 return xstrfmt("git%s", git_cmd
);
456 static void setup_man_path(void)
458 struct strbuf new_path
= STRBUF_INIT
;
459 const char *old_path
= getenv("MANPATH");
460 char *git_man_path
= system_path(GIT_MAN_PATH
);
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. */
466 strbuf_addstr(&new_path
, git_man_path
);
467 strbuf_addch(&new_path
, ':');
469 strbuf_addstr(&new_path
, old_path
);
472 setenv("MANPATH", new_path
.buf
, 1);
474 strbuf_release(&new_path
);
477 static void exec_viewer(const char *name
, const char *page
)
479 const char *info
= get_man_viewer_info(name
);
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
);
488 exec_man_cmd(info
, page
);
490 warning(_("'%s': unknown man viewer."), name
);
493 static void show_man_page(const char *page
)
495 struct man_viewer_list
*viewer
;
496 const char *fallback
= getenv("GIT_MAN_VIEWER");
499 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
501 exec_viewer(viewer
->name
, page
); /* will return when unable */
504 exec_viewer(fallback
, page
);
505 exec_viewer("man", page
);
506 die(_("no man viewer handled the request"));
509 static void show_info_page(const char *page
)
511 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
512 execlp("info", "info", "gitman", page
, (char *)NULL
);
513 die(_("no info viewer handled the request"));
516 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
519 const char *path
= html_path
;
520 char *to_free
= NULL
;
523 path
= to_free
= system_path(GIT_HTML_PATH
);
526 * Check that the page we're looking for exists.
528 if (!strstr(path
, "://")) {
529 if (stat(mkpath("%s/%s.html", path
, page
), &st
)
530 || !S_ISREG(st
.st_mode
))
531 die("'%s/%s.html': documentation file not found.",
535 strbuf_init(page_path
, 0);
536 strbuf_addf(page_path
, "%s/%s.html", path
, page
);
540 static void open_html(const char *path
)
542 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
545 static void show_html_page(const char *page
)
547 struct strbuf page_path
; /* it leaks but we exec below */
549 get_html_page_path(&page_path
, page
);
551 open_html(page_path
.buf
);
554 static const char *check_git_cmd(const char* cmd
)
558 if (is_git_command(cmd
))
561 alias
= alias_lookup(cmd
);
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
574 if (!exclude_guides
|| alias
[0] == '!') {
575 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
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.
586 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
587 count
= split_cmdline(alias
, &argv
);
589 die(_("bad alias.%s string: %s"), cmd
,
590 split_cmdline_strerror(count
));
597 return help_unknown_cmd(cmd
);
602 static void no_help_format(const char *opt_mode
, enum help_format fmt
)
607 case HELP_FORMAT_NONE
:
609 case HELP_FORMAT_MAN
:
612 case HELP_FORMAT_INFO
:
615 case HELP_FORMAT_WEB
:
622 usage_msg_optf(_("options '%s' and '%s' cannot be used together"),
623 builtin_help_usage
, builtin_help_options
, opt_mode
,
627 static void opt_mode_usage(int argc
, const char *opt_mode
,
628 enum help_format fmt
)
631 usage_msg_optf(_("the '%s' option doesn't take any non-option arguments"),
632 builtin_help_usage
, builtin_help_options
,
635 no_help_format(opt_mode
, fmt
);
638 int cmd_help(int argc
,
641 struct repository
*repo UNUSED
)
644 enum help_format parsed_help_format
;
647 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
648 builtin_help_usage
, 0);
649 parsed_help_format
= help_format
;
651 if (cmd_mode
!= HELP_ACTION_ALL
&&
652 (show_external_commands
>= 0 ||
654 usage_msg_opt(_("the '--no-[external-commands|aliases]' options can only be used with '--all'"),
655 builtin_help_usage
, builtin_help_options
);
658 case HELP_ACTION_ALL
:
659 opt_mode_usage(argc
, "--all", help_format
);
662 list_all_cmds_help(show_external_commands
,
666 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
667 load_command_list("git-", &main_cmds
, &other_cmds
);
668 list_commands(&main_cmds
, &other_cmds
);
669 printf("%s\n", _(git_more_info_string
));
671 case HELP_ACTION_GUIDES
:
672 opt_mode_usage(argc
, "--guides", help_format
);
674 printf("%s\n", _(git_more_info_string
));
676 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
677 opt_mode_usage(argc
, "--config-for-completion", help_format
);
678 list_config_help(SHOW_CONFIG_VARS
);
680 case HELP_ACTION_USER_INTERFACES
:
681 opt_mode_usage(argc
, "--user-interfaces", help_format
);
682 list_user_interfaces_help();
684 case HELP_ACTION_DEVELOPER_INTERFACES
:
685 opt_mode_usage(argc
, "--developer-interfaces", help_format
);
686 list_developer_interfaces_help();
688 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
689 opt_mode_usage(argc
, "--config-sections-for-completion",
691 list_config_help(SHOW_CONFIG_SECTIONS
);
693 case HELP_ACTION_CONFIG
:
694 opt_mode_usage(argc
, "--config", help_format
);
696 list_config_help(SHOW_CONFIG_HUMAN
);
697 printf("\n%s\n", _("'git help config' for more information"));
702 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
703 list_common_cmds_help();
704 printf("\n%s\n", _(git_more_info_string
));
708 setup_git_directory_gently(&nongit
);
709 git_config(git_help_config
, NULL
);
711 if (parsed_help_format
!= HELP_FORMAT_NONE
)
712 help_format
= parsed_help_format
;
713 if (help_format
== HELP_FORMAT_NONE
)
714 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
716 argv
[0] = check_git_cmd(argv
[0]);
718 page
= cmd_to_page(argv
[0]);
719 switch (help_format
) {
720 case HELP_FORMAT_NONE
:
721 case HELP_FORMAT_MAN
:
724 case HELP_FORMAT_INFO
:
725 show_info_page(page
);
727 case HELP_FORMAT_WEB
:
728 show_html_page(page
);