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 struct option builtin_help_options
[] = {
55 OPT_CMDMODE('a', "all", &cmd_mode
, N_("print all available commands"),
57 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
58 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
59 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
61 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
63 OPT__VERBOSE(&verbose
, N_("print command description")),
65 OPT_CMDMODE('g', "guides", &cmd_mode
, N_("print list of useful guides"),
67 OPT_CMDMODE('c', "config", &cmd_mode
, N_("print all configuration variable names"),
69 OPT_CMDMODE_F(0, "config-for-completion", &cmd_mode
, "",
70 HELP_ACTION_CONFIG_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
71 OPT_CMDMODE_F(0, "config-sections-for-completion", &cmd_mode
, "",
72 HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
, PARSE_OPT_HIDDEN
),
77 static const char * const builtin_help_usage
[] = {
78 N_("git help [-a|--all] [--[no-]verbose]]\n"
79 " [[-i|--info] [-m|--man] [-w|--web]] [<command>]"),
80 N_("git help [-g|--guides]"),
81 N_("git help [-c|--config]"),
85 struct slot_expansion
{
87 const char *placeholder
;
88 void (*fn
)(struct string_list
*list
, const char *prefix
);
92 static void list_config_help(enum show_config_type type
)
94 struct slot_expansion slot_expansions
[] = {
95 { "advice", "*", list_config_advices
},
96 { "color.branch", "<slot>", list_config_color_branch_slots
},
97 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
98 { "color.diff", "<slot>", list_config_color_diff_slots
},
99 { "color.grep", "<slot>", list_config_color_grep_slots
},
100 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
101 { "color.remote", "<slot>", list_config_color_sideband_slots
},
102 { "color.status", "<slot>", list_config_color_status_slots
},
103 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
104 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
108 struct slot_expansion
*e
;
109 struct string_list keys
= STRING_LIST_INIT_DUP
;
110 struct string_list keys_uniq
= STRING_LIST_INIT_DUP
;
111 struct string_list_item
*item
;
114 for (p
= config_name_list
; *p
; p
++) {
115 const char *var
= *p
;
116 struct strbuf sb
= STRBUF_INIT
;
118 for (e
= slot_expansions
; e
->prefix
; e
++) {
121 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
122 if (!strcasecmp(var
, sb
.buf
)) {
123 e
->fn(&keys
, e
->prefix
);
130 string_list_append(&keys
, var
);
133 for (e
= slot_expansions
; e
->prefix
; e
++)
135 BUG("slot_expansion %s.%s is not used",
136 e
->prefix
, e
->placeholder
);
138 string_list_sort(&keys
);
139 for (i
= 0; i
< keys
.nr
; i
++) {
140 const char *var
= keys
.items
[i
].string
;
141 const char *wildcard
, *tag
, *cut
;
142 const char *dot
= NULL
;
143 struct strbuf sb
= STRBUF_INIT
;
146 case SHOW_CONFIG_HUMAN
:
149 case SHOW_CONFIG_SECTIONS
:
150 dot
= strchr(var
, '.');
152 case SHOW_CONFIG_VARS
:
155 wildcard
= strchr(var
, '*');
156 tag
= strchr(var
, '<');
158 if (!dot
&& !wildcard
&& !tag
) {
159 string_list_append(&keys_uniq
, var
);
165 else if (wildcard
&& !tag
)
167 else if (!wildcard
&& tag
)
170 cut
= wildcard
< tag
? wildcard
: tag
;
172 strbuf_add(&sb
, var
, cut
- var
);
173 string_list_append(&keys_uniq
, sb
.buf
);
177 string_list_clear(&keys
, 0);
178 string_list_remove_duplicates(&keys_uniq
, 0);
179 for_each_string_list_item(item
, &keys_uniq
)
181 string_list_clear(&keys_uniq
, 0);
184 static enum help_format
parse_help_format(const char *format
)
186 if (!strcmp(format
, "man"))
187 return HELP_FORMAT_MAN
;
188 if (!strcmp(format
, "info"))
189 return HELP_FORMAT_INFO
;
190 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
191 return HELP_FORMAT_WEB
;
193 * Please update _git_config() in git-completion.bash when you
194 * add new help formats.
196 die(_("unrecognized help format '%s'"), format
);
199 static const char *get_man_viewer_info(const char *name
)
201 struct man_viewer_info_list
*viewer
;
203 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
205 if (!strcasecmp(name
, viewer
->name
))
211 static int check_emacsclient_version(void)
213 struct strbuf buffer
= STRBUF_INIT
;
214 struct child_process ec_process
= CHILD_PROCESS_INIT
;
215 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
218 /* emacsclient prints its version number on stderr */
219 ec_process
.argv
= argv_ec
;
221 ec_process
.stdout_to_stderr
= 1;
222 if (start_command(&ec_process
))
223 return error(_("Failed to start emacsclient."));
225 strbuf_read(&buffer
, ec_process
.err
, 20);
226 close(ec_process
.err
);
229 * Don't bother checking return value, because "emacsclient --version"
230 * seems to always exits with code 1.
232 finish_command(&ec_process
);
234 if (!starts_with(buffer
.buf
, "emacsclient")) {
235 strbuf_release(&buffer
);
236 return error(_("Failed to parse emacsclient version."));
239 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
240 version
= atoi(buffer
.buf
);
243 strbuf_release(&buffer
);
244 return error(_("emacsclient version '%d' too old (< 22)."),
248 strbuf_release(&buffer
);
252 static void exec_woman_emacs(const char *path
, const char *page
)
254 if (!check_emacsclient_version()) {
255 /* This works only with emacsclient version >= 22. */
256 struct strbuf man_page
= STRBUF_INIT
;
259 path
= "emacsclient";
260 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
261 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
262 warning_errno(_("failed to exec '%s'"), path
);
263 strbuf_release(&man_page
);
267 static void exec_man_konqueror(const char *path
, const char *page
)
269 const char *display
= getenv("DISPLAY");
270 if (display
&& *display
) {
271 struct strbuf man_page
= STRBUF_INIT
;
272 const char *filename
= "kfmclient";
274 /* It's simpler to launch konqueror using kfmclient. */
277 if (strip_suffix(path
, "/konqueror", &len
))
278 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
279 filename
= basename((char *)path
);
282 strbuf_addf(&man_page
, "man:%s(1)", page
);
283 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
284 warning_errno(_("failed to exec '%s'"), path
);
285 strbuf_release(&man_page
);
289 static void exec_man_man(const char *path
, const char *page
)
293 execlp(path
, "man", page
, (char *)NULL
);
294 warning_errno(_("failed to exec '%s'"), path
);
297 static void exec_man_cmd(const char *cmd
, const char *page
)
299 struct strbuf shell_cmd
= STRBUF_INIT
;
300 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
301 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
302 warning(_("failed to exec '%s'"), cmd
);
303 strbuf_release(&shell_cmd
);
306 static void add_man_viewer(const char *name
)
308 struct man_viewer_list
**p
= &man_viewer_list
;
312 FLEX_ALLOC_STR(*p
, name
, name
);
315 static int supported_man_viewer(const char *name
, size_t len
)
317 return (!strncasecmp("man", name
, len
) ||
318 !strncasecmp("woman", name
, len
) ||
319 !strncasecmp("konqueror", name
, len
));
322 static void do_add_man_viewer_info(const char *name
,
326 struct man_viewer_info_list
*new_man_viewer
;
327 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
328 new_man_viewer
->info
= xstrdup(value
);
329 new_man_viewer
->next
= man_viewer_info_list
;
330 man_viewer_info_list
= new_man_viewer
;
333 static int add_man_viewer_path(const char *name
,
337 if (supported_man_viewer(name
, len
))
338 do_add_man_viewer_info(name
, len
, value
);
340 warning(_("'%s': path for unsupported man viewer.\n"
341 "Please consider using 'man.<tool>.cmd' instead."),
347 static int add_man_viewer_cmd(const char *name
,
351 if (supported_man_viewer(name
, len
))
352 warning(_("'%s': cmd for supported man viewer.\n"
353 "Please consider using 'man.<tool>.path' instead."),
356 do_add_man_viewer_info(name
, len
, value
);
361 static int add_man_viewer_info(const char *var
, const char *value
)
363 const char *name
, *subkey
;
366 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
369 if (!strcmp(subkey
, "path")) {
371 return config_error_nonbool(var
);
372 return add_man_viewer_path(name
, namelen
, value
);
374 if (!strcmp(subkey
, "cmd")) {
376 return config_error_nonbool(var
);
377 return add_man_viewer_cmd(name
, namelen
, value
);
383 static int git_help_config(const char *var
, const char *value
, void *cb
)
385 if (!strcmp(var
, "help.format")) {
387 return config_error_nonbool(var
);
388 help_format
= parse_help_format(value
);
391 if (!strcmp(var
, "help.htmlpath")) {
393 return config_error_nonbool(var
);
394 html_path
= xstrdup(value
);
397 if (!strcmp(var
, "man.viewer")) {
399 return config_error_nonbool(var
);
400 add_man_viewer(value
);
403 if (starts_with(var
, "man."))
404 return add_man_viewer_info(var
, value
);
406 return git_default_config(var
, value
, cb
);
409 static struct cmdnames main_cmds
, other_cmds
;
411 static int is_git_command(const char *s
)
416 load_command_list("git-", &main_cmds
, &other_cmds
);
417 return is_in_cmdlist(&main_cmds
, s
) ||
418 is_in_cmdlist(&other_cmds
, s
);
421 static const char *cmd_to_page(const char *git_cmd
)
425 else if (starts_with(git_cmd
, "git"))
427 else if (is_git_command(git_cmd
))
428 return xstrfmt("git-%s", git_cmd
);
430 return xstrfmt("git%s", git_cmd
);
433 static void setup_man_path(void)
435 struct strbuf new_path
= STRBUF_INIT
;
436 const char *old_path
= getenv("MANPATH");
437 char *git_man_path
= system_path(GIT_MAN_PATH
);
439 /* We should always put ':' after our path. If there is no
440 * old_path, the ':' at the end will let 'man' to try
441 * system-wide paths after ours to find the manual page. If
442 * there is old_path, we need ':' as delimiter. */
443 strbuf_addstr(&new_path
, git_man_path
);
444 strbuf_addch(&new_path
, ':');
446 strbuf_addstr(&new_path
, old_path
);
449 setenv("MANPATH", new_path
.buf
, 1);
451 strbuf_release(&new_path
);
454 static void exec_viewer(const char *name
, const char *page
)
456 const char *info
= get_man_viewer_info(name
);
458 if (!strcasecmp(name
, "man"))
459 exec_man_man(info
, page
);
460 else if (!strcasecmp(name
, "woman"))
461 exec_woman_emacs(info
, page
);
462 else if (!strcasecmp(name
, "konqueror"))
463 exec_man_konqueror(info
, page
);
465 exec_man_cmd(info
, page
);
467 warning(_("'%s': unknown man viewer."), name
);
470 static void show_man_page(const char *page
)
472 struct man_viewer_list
*viewer
;
473 const char *fallback
= getenv("GIT_MAN_VIEWER");
476 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
478 exec_viewer(viewer
->name
, page
); /* will return when unable */
481 exec_viewer(fallback
, page
);
482 exec_viewer("man", page
);
483 die(_("no man viewer handled the request"));
486 static void show_info_page(const char *page
)
488 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
489 execlp("info", "info", "gitman", page
, (char *)NULL
);
490 die(_("no info viewer handled the request"));
493 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
496 char *to_free
= NULL
;
499 html_path
= to_free
= system_path(GIT_HTML_PATH
);
502 * Check that the page we're looking for exists.
504 if (!strstr(html_path
, "://")) {
505 if (stat(mkpath("%s/%s.html", html_path
, page
), &st
)
506 || !S_ISREG(st
.st_mode
))
507 die("'%s/%s.html': documentation file not found.",
511 strbuf_init(page_path
, 0);
512 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
516 static void open_html(const char *path
)
518 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
521 static void show_html_page(const char *page
)
523 struct strbuf page_path
; /* it leaks but we exec bellow */
525 get_html_page_path(&page_path
, page
);
527 open_html(page_path
.buf
);
530 static const char *check_git_cmd(const char* cmd
)
534 if (is_git_command(cmd
))
537 alias
= alias_lookup(cmd
);
543 * handle_builtin() in git.c rewrites "git cmd --help"
544 * to "git help --exclude-guides cmd", so we can use
545 * exclude_guides to distinguish "git cmd --help" from
546 * "git help cmd". In the latter case, or if cmd is an
547 * alias for a shell command, just print the alias
550 if (!exclude_guides
|| alias
[0] == '!') {
551 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
556 * Otherwise, we pretend that the command was "git
557 * word0 --help". We use split_cmdline() to get the
558 * first word of the alias, to ensure that we use the
559 * same rules as when the alias is actually
560 * used. split_cmdline() modifies alias in-place.
562 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
563 count
= split_cmdline(alias
, &argv
);
565 die(_("bad alias.%s string: %s"), cmd
,
566 split_cmdline_strerror(count
));
573 return help_unknown_cmd(cmd
);
578 static void no_extra_argc(int argc
)
581 usage_msg_opt(_("this option doesn't take any other arguments"),
582 builtin_help_usage
, builtin_help_options
);
585 int cmd_help(int argc
, const char **argv
, const char *prefix
)
588 enum help_format parsed_help_format
;
591 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
592 builtin_help_usage
, 0);
593 parsed_help_format
= help_format
;
596 case HELP_ACTION_ALL
:
599 list_all_cmds_help();
602 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
603 load_command_list("git-", &main_cmds
, &other_cmds
);
604 list_commands(&main_cmds
, &other_cmds
);
605 printf("%s\n", _(git_more_info_string
));
607 case HELP_ACTION_GUIDES
:
610 printf("%s\n", _(git_more_info_string
));
612 case HELP_ACTION_CONFIG_FOR_COMPLETION
:
614 list_config_help(SHOW_CONFIG_VARS
);
616 case HELP_ACTION_CONFIG_SECTIONS_FOR_COMPLETION
:
618 list_config_help(SHOW_CONFIG_SECTIONS
);
620 case HELP_ACTION_CONFIG
:
623 list_config_help(SHOW_CONFIG_HUMAN
);
624 printf("\n%s\n", _("'git help config' for more information"));
629 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
630 list_common_cmds_help();
631 printf("\n%s\n", _(git_more_info_string
));
635 setup_git_directory_gently(&nongit
);
636 git_config(git_help_config
, NULL
);
638 if (parsed_help_format
!= HELP_FORMAT_NONE
)
639 help_format
= parsed_help_format
;
640 if (help_format
== HELP_FORMAT_NONE
)
641 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
643 argv
[0] = check_git_cmd(argv
[0]);
645 page
= cmd_to_page(argv
[0]);
646 switch (help_format
) {
647 case HELP_FORMAT_NONE
:
648 case HELP_FORMAT_MAN
:
651 case HELP_FORMAT_INFO
:
652 show_info_page(page
);
654 case HELP_FORMAT_WEB
:
655 show_html_page(page
);