8 #include "parse-options.h"
9 #include "run-command.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 static const char *html_path
;
38 static int show_all
= 0;
39 static int show_guides
= 0;
40 static int show_config
;
41 static int verbose
= 1;
42 static unsigned int colopts
;
43 static enum help_format help_format
= HELP_FORMAT_NONE
;
44 static int exclude_guides
;
45 static struct option builtin_help_options
[] = {
46 OPT_BOOL('a', "all", &show_all
, N_("print all available commands")),
47 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides
, N_("exclude guides")),
48 OPT_BOOL('g', "guides", &show_guides
, N_("print list of useful guides")),
49 OPT_BOOL('c', "config", &show_config
, N_("print all configuration variable names")),
50 OPT_SET_INT_F(0, "config-for-completion", &show_config
, "", 2, PARSE_OPT_HIDDEN
),
51 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
52 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
54 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
56 OPT__VERBOSE(&verbose
, N_("print command description")),
60 static const char * const builtin_help_usage
[] = {
61 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
65 static enum help_format
parse_help_format(const char *format
)
67 if (!strcmp(format
, "man"))
68 return HELP_FORMAT_MAN
;
69 if (!strcmp(format
, "info"))
70 return HELP_FORMAT_INFO
;
71 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
72 return HELP_FORMAT_WEB
;
74 * Please update _git_config() in git-completion.bash when you
75 * add new help formats.
77 die(_("unrecognized help format '%s'"), format
);
80 static const char *get_man_viewer_info(const char *name
)
82 struct man_viewer_info_list
*viewer
;
84 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
86 if (!strcasecmp(name
, viewer
->name
))
92 static int check_emacsclient_version(void)
94 struct strbuf buffer
= STRBUF_INIT
;
95 struct child_process ec_process
= CHILD_PROCESS_INIT
;
96 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
99 /* emacsclient prints its version number on stderr */
100 ec_process
.argv
= argv_ec
;
102 ec_process
.stdout_to_stderr
= 1;
103 if (start_command(&ec_process
))
104 return error(_("Failed to start emacsclient."));
106 strbuf_read(&buffer
, ec_process
.err
, 20);
107 close(ec_process
.err
);
110 * Don't bother checking return value, because "emacsclient --version"
111 * seems to always exits with code 1.
113 finish_command(&ec_process
);
115 if (!starts_with(buffer
.buf
, "emacsclient")) {
116 strbuf_release(&buffer
);
117 return error(_("Failed to parse emacsclient version."));
120 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
121 version
= atoi(buffer
.buf
);
124 strbuf_release(&buffer
);
125 return error(_("emacsclient version '%d' too old (< 22)."),
129 strbuf_release(&buffer
);
133 static void exec_woman_emacs(const char *path
, const char *page
)
135 if (!check_emacsclient_version()) {
136 /* This works only with emacsclient version >= 22. */
137 struct strbuf man_page
= STRBUF_INIT
;
140 path
= "emacsclient";
141 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
142 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
143 warning_errno(_("failed to exec '%s'"), path
);
144 strbuf_release(&man_page
);
148 static void exec_man_konqueror(const char *path
, const char *page
)
150 const char *display
= getenv("DISPLAY");
151 if (display
&& *display
) {
152 struct strbuf man_page
= STRBUF_INIT
;
153 const char *filename
= "kfmclient";
155 /* It's simpler to launch konqueror using kfmclient. */
158 if (strip_suffix(path
, "/konqueror", &len
))
159 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
160 filename
= basename((char *)path
);
163 strbuf_addf(&man_page
, "man:%s(1)", page
);
164 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
165 warning_errno(_("failed to exec '%s'"), path
);
166 strbuf_release(&man_page
);
170 static void exec_man_man(const char *path
, const char *page
)
174 execlp(path
, "man", page
, (char *)NULL
);
175 warning_errno(_("failed to exec '%s'"), path
);
178 static void exec_man_cmd(const char *cmd
, const char *page
)
180 struct strbuf shell_cmd
= STRBUF_INIT
;
181 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
182 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
183 warning(_("failed to exec '%s'"), cmd
);
184 strbuf_release(&shell_cmd
);
187 static void add_man_viewer(const char *name
)
189 struct man_viewer_list
**p
= &man_viewer_list
;
193 FLEX_ALLOC_STR(*p
, name
, name
);
196 static int supported_man_viewer(const char *name
, size_t len
)
198 return (!strncasecmp("man", name
, len
) ||
199 !strncasecmp("woman", name
, len
) ||
200 !strncasecmp("konqueror", name
, len
));
203 static void do_add_man_viewer_info(const char *name
,
207 struct man_viewer_info_list
*new_man_viewer
;
208 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
209 new_man_viewer
->info
= xstrdup(value
);
210 new_man_viewer
->next
= man_viewer_info_list
;
211 man_viewer_info_list
= new_man_viewer
;
214 static int add_man_viewer_path(const char *name
,
218 if (supported_man_viewer(name
, len
))
219 do_add_man_viewer_info(name
, len
, value
);
221 warning(_("'%s': path for unsupported man viewer.\n"
222 "Please consider using 'man.<tool>.cmd' instead."),
228 static int add_man_viewer_cmd(const char *name
,
232 if (supported_man_viewer(name
, len
))
233 warning(_("'%s': cmd for supported man viewer.\n"
234 "Please consider using 'man.<tool>.path' instead."),
237 do_add_man_viewer_info(name
, len
, value
);
242 static int add_man_viewer_info(const char *var
, const char *value
)
244 const char *name
, *subkey
;
247 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
250 if (!strcmp(subkey
, "path")) {
252 return config_error_nonbool(var
);
253 return add_man_viewer_path(name
, namelen
, value
);
255 if (!strcmp(subkey
, "cmd")) {
257 return config_error_nonbool(var
);
258 return add_man_viewer_cmd(name
, namelen
, value
);
264 static int git_help_config(const char *var
, const char *value
, void *cb
)
266 if (starts_with(var
, "column."))
267 return git_column_config(var
, value
, "help", &colopts
);
268 if (!strcmp(var
, "help.format")) {
270 return config_error_nonbool(var
);
271 help_format
= parse_help_format(value
);
274 if (!strcmp(var
, "help.htmlpath")) {
276 return config_error_nonbool(var
);
277 html_path
= xstrdup(value
);
280 if (!strcmp(var
, "man.viewer")) {
282 return config_error_nonbool(var
);
283 add_man_viewer(value
);
286 if (starts_with(var
, "man."))
287 return add_man_viewer_info(var
, value
);
289 return git_default_config(var
, value
, cb
);
292 static struct cmdnames main_cmds
, other_cmds
;
294 static int is_git_command(const char *s
)
299 load_command_list("git-", &main_cmds
, &other_cmds
);
300 return is_in_cmdlist(&main_cmds
, s
) ||
301 is_in_cmdlist(&other_cmds
, s
);
304 static const char *cmd_to_page(const char *git_cmd
)
308 else if (starts_with(git_cmd
, "git"))
310 else if (is_git_command(git_cmd
))
311 return xstrfmt("git-%s", git_cmd
);
313 return xstrfmt("git%s", git_cmd
);
316 static void setup_man_path(void)
318 struct strbuf new_path
= STRBUF_INIT
;
319 const char *old_path
= getenv("MANPATH");
320 char *git_man_path
= system_path(GIT_MAN_PATH
);
322 /* We should always put ':' after our path. If there is no
323 * old_path, the ':' at the end will let 'man' to try
324 * system-wide paths after ours to find the manual page. If
325 * there is old_path, we need ':' as delimiter. */
326 strbuf_addstr(&new_path
, git_man_path
);
327 strbuf_addch(&new_path
, ':');
329 strbuf_addstr(&new_path
, old_path
);
332 setenv("MANPATH", new_path
.buf
, 1);
334 strbuf_release(&new_path
);
337 static void exec_viewer(const char *name
, const char *page
)
339 const char *info
= get_man_viewer_info(name
);
341 if (!strcasecmp(name
, "man"))
342 exec_man_man(info
, page
);
343 else if (!strcasecmp(name
, "woman"))
344 exec_woman_emacs(info
, page
);
345 else if (!strcasecmp(name
, "konqueror"))
346 exec_man_konqueror(info
, page
);
348 exec_man_cmd(info
, page
);
350 warning(_("'%s': unknown man viewer."), name
);
353 static void show_man_page(const char *git_cmd
)
355 struct man_viewer_list
*viewer
;
356 const char *page
= cmd_to_page(git_cmd
);
357 const char *fallback
= getenv("GIT_MAN_VIEWER");
360 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
362 exec_viewer(viewer
->name
, page
); /* will return when unable */
365 exec_viewer(fallback
, page
);
366 exec_viewer("man", page
);
367 die(_("no man viewer handled the request"));
370 static void show_info_page(const char *git_cmd
)
372 const char *page
= cmd_to_page(git_cmd
);
373 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
374 execlp("info", "info", "gitman", page
, (char *)NULL
);
375 die(_("no info viewer handled the request"));
378 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
381 char *to_free
= NULL
;
384 html_path
= to_free
= system_path(GIT_HTML_PATH
);
386 /* Check that we have a git documentation directory. */
387 if (!strstr(html_path
, "://")) {
388 if (stat(mkpath("%s/git.html", html_path
), &st
)
389 || !S_ISREG(st
.st_mode
))
390 die("'%s': not a documentation directory.", html_path
);
393 strbuf_init(page_path
, 0);
394 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
398 static void open_html(const char *path
)
400 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
403 static void show_html_page(const char *git_cmd
)
405 const char *page
= cmd_to_page(git_cmd
);
406 struct strbuf page_path
; /* it leaks but we exec bellow */
408 get_html_page_path(&page_path
, page
);
410 open_html(page_path
.buf
);
413 static const char *check_git_cmd(const char* cmd
)
417 if (is_git_command(cmd
))
420 alias
= alias_lookup(cmd
);
426 * handle_builtin() in git.c rewrites "git cmd --help"
427 * to "git help --exclude-guides cmd", so we can use
428 * exclude_guides to distinguish "git cmd --help" from
429 * "git help cmd". In the latter case, or if cmd is an
430 * alias for a shell command, just print the alias
433 if (!exclude_guides
|| alias
[0] == '!') {
434 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
439 * Otherwise, we pretend that the command was "git
440 * word0 --help". We use split_cmdline() to get the
441 * first word of the alias, to ensure that we use the
442 * same rules as when the alias is actually
443 * used. split_cmdline() modifies alias in-place.
445 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"), cmd
, alias
);
446 count
= split_cmdline(alias
, &argv
);
448 die(_("bad alias.%s string: %s"), cmd
,
449 split_cmdline_strerror(count
));
456 return help_unknown_cmd(cmd
);
461 int cmd_help(int argc
, const char **argv
, const char *prefix
)
464 enum help_format parsed_help_format
;
466 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
467 builtin_help_usage
, 0);
468 parsed_help_format
= help_format
;
471 git_config(git_help_config
, NULL
);
474 list_all_cmds_help();
477 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
478 load_command_list("git-", &main_cmds
, &other_cmds
);
479 list_commands(colopts
, &main_cmds
, &other_cmds
);
483 int for_human
= show_config
== 1;
486 list_config_help(for_human
);
490 list_config_help(for_human
);
491 printf("\n%s\n", _("'git help config' for more information"));
496 list_common_guides_help();
498 if (show_all
|| show_guides
) {
499 printf("%s\n", _(git_more_info_string
));
501 * We're done. Ignore any remaining args
507 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
508 list_common_cmds_help();
509 printf("\n%s\n", _(git_more_info_string
));
513 setup_git_directory_gently(&nongit
);
514 git_config(git_help_config
, NULL
);
516 if (parsed_help_format
!= HELP_FORMAT_NONE
)
517 help_format
= parsed_help_format
;
518 if (help_format
== HELP_FORMAT_NONE
)
519 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
521 argv
[0] = check_git_cmd(argv
[0]);
523 switch (help_format
) {
524 case HELP_FORMAT_NONE
:
525 case HELP_FORMAT_MAN
:
526 show_man_page(argv
[0]);
528 case HELP_FORMAT_INFO
:
529 show_info_page(argv
[0]);
531 case HELP_FORMAT_WEB
:
532 show_html_page(argv
[0]);