8 #include "parse-options.h"
9 #include "run-command.h"
11 #include "config-list.h"
15 #ifndef DEFAULT_HELP_FORMAT
16 #define DEFAULT_HELP_FORMAT "man"
19 static struct man_viewer_list
{
20 struct man_viewer_list
*next
;
21 char name
[FLEX_ARRAY
];
24 static struct man_viewer_info_list
{
25 struct man_viewer_info_list
*next
;
27 char name
[FLEX_ARRAY
];
28 } *man_viewer_info_list
;
37 static const char *html_path
;
39 static int show_all
= 0;
40 static int show_guides
= 0;
41 static int show_config
;
42 static int verbose
= 1;
43 static unsigned int colopts
;
44 static enum help_format help_format
= HELP_FORMAT_NONE
;
45 static int exclude_guides
;
46 static struct option builtin_help_options
[] = {
47 OPT_BOOL('a', "all", &show_all
, N_("print all available commands")),
48 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
49 OPT_BOOL('g', "guides", &show_guides
, N_("print list of useful guides")),
50 OPT_BOOL('c', "config", &show_config
, N_("print all configuration variable names")),
51 OPT_SET_INT_F(0, "config-for-completion", &show_config
, "", 2, PARSE_OPT_HIDDEN
),
52 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
53 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
55 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
57 OPT__VERBOSE(&verbose
, N_("print command description")),
61 static const char * const builtin_help_usage
[] = {
62 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
66 struct slot_expansion
{
68 const char *placeholder
;
69 void (*fn
)(struct string_list
*list
, const char *prefix
);
73 static void list_config_help(int for_human
)
75 struct slot_expansion slot_expansions
[] = {
76 { "advice", "*", list_config_advices
},
77 { "color.branch", "<slot>", list_config_color_branch_slots
},
78 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
79 { "color.diff", "<slot>", list_config_color_diff_slots
},
80 { "color.grep", "<slot>", list_config_color_grep_slots
},
81 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
82 { "color.remote", "<slot>", list_config_color_sideband_slots
},
83 { "color.status", "<slot>", list_config_color_status_slots
},
84 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
85 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
89 struct slot_expansion
*e
;
90 struct string_list keys
= STRING_LIST_INIT_DUP
;
93 for (p
= config_name_list
; *p
; p
++) {
95 struct strbuf sb
= STRBUF_INIT
;
97 for (e
= slot_expansions
; e
->prefix
; e
++) {
100 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
101 if (!strcasecmp(var
, sb
.buf
)) {
102 e
->fn(&keys
, e
->prefix
);
109 string_list_append(&keys
, var
);
112 for (e
= slot_expansions
; e
->prefix
; e
++)
114 BUG("slot_expansion %s.%s is not used",
115 e
->prefix
, e
->placeholder
);
117 string_list_sort(&keys
);
118 for (i
= 0; i
< keys
.nr
; i
++) {
119 const char *var
= keys
.items
[i
].string
;
120 const char *wildcard
, *tag
, *cut
;
127 wildcard
= strchr(var
, '*');
128 tag
= strchr(var
, '<');
130 if (!wildcard
&& !tag
) {
135 if (wildcard
&& !tag
)
137 else if (!wildcard
&& tag
)
140 cut
= wildcard
< tag
? wildcard
: tag
;
143 * We may produce duplicates, but that's up to
144 * git-completion.bash to handle
146 printf("%.*s\n", (int)(cut
- var
), var
);
148 string_list_clear(&keys
, 0);
151 static enum help_format
parse_help_format(const char *format
)
153 if (!strcmp(format
, "man"))
154 return HELP_FORMAT_MAN
;
155 if (!strcmp(format
, "info"))
156 return HELP_FORMAT_INFO
;
157 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
158 return HELP_FORMAT_WEB
;
160 * Please update _git_config() in git-completion.bash when you
161 * add new help formats.
163 die(_("unrecognized help format '%s'"), format
);
166 static const char *get_man_viewer_info(const char *name
)
168 struct man_viewer_info_list
*viewer
;
170 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
172 if (!strcasecmp(name
, viewer
->name
))
178 static int check_emacsclient_version(void)
180 struct strbuf buffer
= STRBUF_INIT
;
181 struct child_process ec_process
= CHILD_PROCESS_INIT
;
182 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
185 /* emacsclient prints its version number on stderr */
186 ec_process
.argv
= argv_ec
;
188 ec_process
.stdout_to_stderr
= 1;
189 if (start_command(&ec_process
))
190 return error(_("Failed to start emacsclient."));
192 strbuf_read(&buffer
, ec_process
.err
, 20);
193 close(ec_process
.err
);
196 * Don't bother checking return value, because "emacsclient --version"
197 * seems to always exits with code 1.
199 finish_command(&ec_process
);
201 if (!starts_with(buffer
.buf
, "emacsclient")) {
202 strbuf_release(&buffer
);
203 return error(_("Failed to parse emacsclient version."));
206 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
207 version
= atoi(buffer
.buf
);
210 strbuf_release(&buffer
);
211 return error(_("emacsclient version '%d' too old (< 22)."),
215 strbuf_release(&buffer
);
219 static void exec_woman_emacs(const char *path
, const char *page
)
221 if (!check_emacsclient_version()) {
222 /* This works only with emacsclient version >= 22. */
223 struct strbuf man_page
= STRBUF_INIT
;
226 path
= "emacsclient";
227 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
228 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
229 warning_errno(_("failed to exec '%s'"), path
);
230 strbuf_release(&man_page
);
234 static void exec_man_konqueror(const char *path
, const char *page
)
236 const char *display
= getenv("DISPLAY");
237 if (display
&& *display
) {
238 struct strbuf man_page
= STRBUF_INIT
;
239 const char *filename
= "kfmclient";
241 /* It's simpler to launch konqueror using kfmclient. */
244 if (strip_suffix(path
, "/konqueror", &len
))
245 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
246 filename
= basename((char *)path
);
249 strbuf_addf(&man_page
, "man:%s(1)", page
);
250 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
251 warning_errno(_("failed to exec '%s'"), path
);
252 strbuf_release(&man_page
);
256 static void exec_man_man(const char *path
, const char *page
)
260 execlp(path
, "man", page
, (char *)NULL
);
261 warning_errno(_("failed to exec '%s'"), path
);
264 static void exec_man_cmd(const char *cmd
, const char *page
)
266 struct strbuf shell_cmd
= STRBUF_INIT
;
267 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
268 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
269 warning(_("failed to exec '%s'"), cmd
);
270 strbuf_release(&shell_cmd
);
273 static void add_man_viewer(const char *name
)
275 struct man_viewer_list
**p
= &man_viewer_list
;
279 FLEX_ALLOC_STR(*p
, name
, name
);
282 static int supported_man_viewer(const char *name
, size_t len
)
284 return (!strncasecmp("man", name
, len
) ||
285 !strncasecmp("woman", name
, len
) ||
286 !strncasecmp("konqueror", name
, len
));
289 static void do_add_man_viewer_info(const char *name
,
293 struct man_viewer_info_list
*new_man_viewer
;
294 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
295 new_man_viewer
->info
= xstrdup(value
);
296 new_man_viewer
->next
= man_viewer_info_list
;
297 man_viewer_info_list
= new_man_viewer
;
300 static int add_man_viewer_path(const char *name
,
304 if (supported_man_viewer(name
, len
))
305 do_add_man_viewer_info(name
, len
, value
);
307 warning(_("'%s': path for unsupported man viewer.\n"
308 "Please consider using 'man.<tool>.cmd' instead."),
314 static int add_man_viewer_cmd(const char *name
,
318 if (supported_man_viewer(name
, len
))
319 warning(_("'%s': cmd for supported man viewer.\n"
320 "Please consider using 'man.<tool>.path' instead."),
323 do_add_man_viewer_info(name
, len
, value
);
328 static int add_man_viewer_info(const char *var
, const char *value
)
330 const char *name
, *subkey
;
333 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
336 if (!strcmp(subkey
, "path")) {
338 return config_error_nonbool(var
);
339 return add_man_viewer_path(name
, namelen
, value
);
341 if (!strcmp(subkey
, "cmd")) {
343 return config_error_nonbool(var
);
344 return add_man_viewer_cmd(name
, namelen
, value
);
350 static int git_help_config(const char *var
, const char *value
, void *cb
)
352 if (starts_with(var
, "column."))
353 return git_column_config(var
, value
, "help", &colopts
);
354 if (!strcmp(var
, "help.format")) {
356 return config_error_nonbool(var
);
357 help_format
= parse_help_format(value
);
360 if (!strcmp(var
, "help.htmlpath")) {
362 return config_error_nonbool(var
);
363 html_path
= xstrdup(value
);
366 if (!strcmp(var
, "man.viewer")) {
368 return config_error_nonbool(var
);
369 add_man_viewer(value
);
372 if (starts_with(var
, "man."))
373 return add_man_viewer_info(var
, value
);
375 return git_default_config(var
, value
, cb
);
378 static struct cmdnames main_cmds
, other_cmds
;
380 static int is_git_command(const char *s
)
385 load_command_list("git-", &main_cmds
, &other_cmds
);
386 return is_in_cmdlist(&main_cmds
, s
) ||
387 is_in_cmdlist(&other_cmds
, s
);
390 static const char *cmd_to_page(const char *git_cmd
)
394 else if (starts_with(git_cmd
, "git"))
396 else if (is_git_command(git_cmd
))
397 return xstrfmt("git-%s", git_cmd
);
399 return xstrfmt("git%s", git_cmd
);
402 static void setup_man_path(void)
404 struct strbuf new_path
= STRBUF_INIT
;
405 const char *old_path
= getenv("MANPATH");
406 char *git_man_path
= system_path(GIT_MAN_PATH
);
408 /* We should always put ':' after our path. If there is no
409 * old_path, the ':' at the end will let 'man' to try
410 * system-wide paths after ours to find the manual page. If
411 * there is old_path, we need ':' as delimiter. */
412 strbuf_addstr(&new_path
, git_man_path
);
413 strbuf_addch(&new_path
, ':');
415 strbuf_addstr(&new_path
, old_path
);
418 setenv("MANPATH", new_path
.buf
, 1);
420 strbuf_release(&new_path
);
423 static void exec_viewer(const char *name
, const char *page
)
425 const char *info
= get_man_viewer_info(name
);
427 if (!strcasecmp(name
, "man"))
428 exec_man_man(info
, page
);
429 else if (!strcasecmp(name
, "woman"))
430 exec_woman_emacs(info
, page
);
431 else if (!strcasecmp(name
, "konqueror"))
432 exec_man_konqueror(info
, page
);
434 exec_man_cmd(info
, page
);
436 warning(_("'%s': unknown man viewer."), name
);
439 static void show_man_page(const char *page
)
441 struct man_viewer_list
*viewer
;
442 const char *fallback
= getenv("GIT_MAN_VIEWER");
445 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
447 exec_viewer(viewer
->name
, page
); /* will return when unable */
450 exec_viewer(fallback
, page
);
451 exec_viewer("man", page
);
452 die(_("no man viewer handled the request"));
455 static void show_info_page(const char *page
)
457 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
458 execlp("info", "info", "gitman", page
, (char *)NULL
);
459 die(_("no info viewer handled the request"));
462 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
465 char *to_free
= NULL
;
468 html_path
= to_free
= system_path(GIT_HTML_PATH
);
470 /* Check that we have a git documentation directory. */
471 if (!strstr(html_path
, "://")) {
472 if (stat(mkpath("%s/git.html", html_path
), &st
)
473 || !S_ISREG(st
.st_mode
))
474 die("'%s': not a documentation directory.", html_path
);
477 strbuf_init(page_path
, 0);
478 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
482 static void open_html(const char *path
)
484 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
487 static void show_html_page(const char *page
)
489 struct strbuf page_path
; /* it leaks but we exec bellow */
491 get_html_page_path(&page_path
, page
);
493 open_html(page_path
.buf
);
496 static const char *check_git_cmd(const char* cmd
)
500 if (is_git_command(cmd
))
503 alias
= alias_lookup(cmd
);
509 * handle_builtin() in git.c rewrites "git cmd --help"
510 * to "git help --exclude-guides cmd", so we can use
511 * exclude_guides to distinguish "git cmd --help" from
512 * "git help cmd". In the latter case, or if cmd is an
513 * alias for a shell command, just print the alias
516 if (!exclude_guides
|| alias
[0] == '!') {
517 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
522 * Otherwise, we pretend that the command was "git
523 * word0 --help". We use split_cmdline() to get the
524 * first word of the alias, to ensure that we use the
525 * same rules as when the alias is actually
526 * used. split_cmdline() modifies alias in-place.
528 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
529 count
= split_cmdline(alias
, &argv
);
531 die(_("bad alias.%s string: %s"), cmd
,
532 split_cmdline_strerror(count
));
539 return help_unknown_cmd(cmd
);
544 int cmd_help(int argc
, const char **argv
, const char *prefix
)
547 enum help_format parsed_help_format
;
550 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
551 builtin_help_usage
, 0);
552 parsed_help_format
= help_format
;
555 git_config(git_help_config
, NULL
);
558 list_all_cmds_help();
561 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
562 load_command_list("git-", &main_cmds
, &other_cmds
);
563 list_commands(colopts
, &main_cmds
, &other_cmds
);
567 int for_human
= show_config
== 1;
570 list_config_help(for_human
);
574 list_config_help(for_human
);
575 printf("\n%s\n", _("'git help config' for more information"));
582 if (show_all
|| show_guides
) {
583 printf("%s\n", _(git_more_info_string
));
585 * We're done. Ignore any remaining args
591 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
592 list_common_cmds_help();
593 printf("\n%s\n", _(git_more_info_string
));
597 setup_git_directory_gently(&nongit
);
598 git_config(git_help_config
, NULL
);
600 if (parsed_help_format
!= HELP_FORMAT_NONE
)
601 help_format
= parsed_help_format
;
602 if (help_format
== HELP_FORMAT_NONE
)
603 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
605 argv
[0] = check_git_cmd(argv
[0]);
607 page
= cmd_to_page(argv
[0]);
608 switch (help_format
) {
609 case HELP_FORMAT_NONE
:
610 case HELP_FORMAT_MAN
:
613 case HELP_FORMAT_INFO
:
614 show_info_page(page
);
616 case HELP_FORMAT_WEB
:
617 show_html_page(page
);