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
;
73 die(_("unrecognized help format '%s'"), format
);
76 static const char *get_man_viewer_info(const char *name
)
78 struct man_viewer_info_list
*viewer
;
80 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
82 if (!strcasecmp(name
, viewer
->name
))
88 static int check_emacsclient_version(void)
90 struct strbuf buffer
= STRBUF_INIT
;
91 struct child_process ec_process
= CHILD_PROCESS_INIT
;
92 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
95 /* emacsclient prints its version number on stderr */
96 ec_process
.argv
= argv_ec
;
98 ec_process
.stdout_to_stderr
= 1;
99 if (start_command(&ec_process
))
100 return error(_("Failed to start emacsclient."));
102 strbuf_read(&buffer
, ec_process
.err
, 20);
103 close(ec_process
.err
);
106 * Don't bother checking return value, because "emacsclient --version"
107 * seems to always exits with code 1.
109 finish_command(&ec_process
);
111 if (!starts_with(buffer
.buf
, "emacsclient")) {
112 strbuf_release(&buffer
);
113 return error(_("Failed to parse emacsclient version."));
116 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
117 version
= atoi(buffer
.buf
);
120 strbuf_release(&buffer
);
121 return error(_("emacsclient version '%d' too old (< 22)."),
125 strbuf_release(&buffer
);
129 static void exec_woman_emacs(const char *path
, const char *page
)
131 if (!check_emacsclient_version()) {
132 /* This works only with emacsclient version >= 22. */
133 struct strbuf man_page
= STRBUF_INIT
;
136 path
= "emacsclient";
137 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
138 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
139 warning_errno(_("failed to exec '%s'"), path
);
140 strbuf_release(&man_page
);
144 static void exec_man_konqueror(const char *path
, const char *page
)
146 const char *display
= getenv("DISPLAY");
147 if (display
&& *display
) {
148 struct strbuf man_page
= STRBUF_INIT
;
149 const char *filename
= "kfmclient";
151 /* It's simpler to launch konqueror using kfmclient. */
154 if (strip_suffix(path
, "/konqueror", &len
))
155 path
= xstrfmt("%.*s/kfmclient", (int)len
, path
);
156 filename
= basename((char *)path
);
159 strbuf_addf(&man_page
, "man:%s(1)", page
);
160 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
161 warning_errno(_("failed to exec '%s'"), path
);
162 strbuf_release(&man_page
);
166 static void exec_man_man(const char *path
, const char *page
)
170 execlp(path
, "man", page
, (char *)NULL
);
171 warning_errno(_("failed to exec '%s'"), path
);
174 static void exec_man_cmd(const char *cmd
, const char *page
)
176 struct strbuf shell_cmd
= STRBUF_INIT
;
177 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
178 execl(SHELL_PATH
, SHELL_PATH
, "-c", shell_cmd
.buf
, (char *)NULL
);
179 warning(_("failed to exec '%s'"), cmd
);
180 strbuf_release(&shell_cmd
);
183 static void add_man_viewer(const char *name
)
185 struct man_viewer_list
**p
= &man_viewer_list
;
189 FLEX_ALLOC_STR(*p
, name
, name
);
192 static int supported_man_viewer(const char *name
, size_t len
)
194 return (!strncasecmp("man", name
, len
) ||
195 !strncasecmp("woman", name
, len
) ||
196 !strncasecmp("konqueror", name
, len
));
199 static void do_add_man_viewer_info(const char *name
,
203 struct man_viewer_info_list
*new_man_viewer
;
204 FLEX_ALLOC_MEM(new_man_viewer
, name
, name
, len
);
205 new_man_viewer
->info
= xstrdup(value
);
206 new_man_viewer
->next
= man_viewer_info_list
;
207 man_viewer_info_list
= new_man_viewer
;
210 static int add_man_viewer_path(const char *name
,
214 if (supported_man_viewer(name
, len
))
215 do_add_man_viewer_info(name
, len
, value
);
217 warning(_("'%s': path for unsupported man viewer.\n"
218 "Please consider using 'man.<tool>.cmd' instead."),
224 static int add_man_viewer_cmd(const char *name
,
228 if (supported_man_viewer(name
, len
))
229 warning(_("'%s': cmd for supported man viewer.\n"
230 "Please consider using 'man.<tool>.path' instead."),
233 do_add_man_viewer_info(name
, len
, value
);
238 static int add_man_viewer_info(const char *var
, const char *value
)
240 const char *name
, *subkey
;
243 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
246 if (!strcmp(subkey
, "path")) {
248 return config_error_nonbool(var
);
249 return add_man_viewer_path(name
, namelen
, value
);
251 if (!strcmp(subkey
, "cmd")) {
253 return config_error_nonbool(var
);
254 return add_man_viewer_cmd(name
, namelen
, value
);
260 static int git_help_config(const char *var
, const char *value
, void *cb
)
262 if (starts_with(var
, "column."))
263 return git_column_config(var
, value
, "help", &colopts
);
264 if (!strcmp(var
, "help.format")) {
266 return config_error_nonbool(var
);
267 help_format
= parse_help_format(value
);
270 if (!strcmp(var
, "help.htmlpath")) {
272 return config_error_nonbool(var
);
273 html_path
= xstrdup(value
);
276 if (!strcmp(var
, "man.viewer")) {
278 return config_error_nonbool(var
);
279 add_man_viewer(value
);
282 if (starts_with(var
, "man."))
283 return add_man_viewer_info(var
, value
);
285 return git_default_config(var
, value
, cb
);
288 static struct cmdnames main_cmds
, other_cmds
;
290 static int is_git_command(const char *s
)
295 load_command_list("git-", &main_cmds
, &other_cmds
);
296 return is_in_cmdlist(&main_cmds
, s
) ||
297 is_in_cmdlist(&other_cmds
, s
);
300 static const char *cmd_to_page(const char *git_cmd
)
304 else if (starts_with(git_cmd
, "git"))
306 else if (is_git_command(git_cmd
))
307 return xstrfmt("git-%s", git_cmd
);
309 return xstrfmt("git%s", git_cmd
);
312 static void setup_man_path(void)
314 struct strbuf new_path
= STRBUF_INIT
;
315 const char *old_path
= getenv("MANPATH");
316 char *git_man_path
= system_path(GIT_MAN_PATH
);
318 /* We should always put ':' after our path. If there is no
319 * old_path, the ':' at the end will let 'man' to try
320 * system-wide paths after ours to find the manual page. If
321 * there is old_path, we need ':' as delimiter. */
322 strbuf_addstr(&new_path
, git_man_path
);
323 strbuf_addch(&new_path
, ':');
325 strbuf_addstr(&new_path
, old_path
);
328 setenv("MANPATH", new_path
.buf
, 1);
330 strbuf_release(&new_path
);
333 static void exec_viewer(const char *name
, const char *page
)
335 const char *info
= get_man_viewer_info(name
);
337 if (!strcasecmp(name
, "man"))
338 exec_man_man(info
, page
);
339 else if (!strcasecmp(name
, "woman"))
340 exec_woman_emacs(info
, page
);
341 else if (!strcasecmp(name
, "konqueror"))
342 exec_man_konqueror(info
, page
);
344 exec_man_cmd(info
, page
);
346 warning(_("'%s': unknown man viewer."), name
);
349 static void show_man_page(const char *git_cmd
)
351 struct man_viewer_list
*viewer
;
352 const char *page
= cmd_to_page(git_cmd
);
353 const char *fallback
= getenv("GIT_MAN_VIEWER");
356 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
358 exec_viewer(viewer
->name
, page
); /* will return when unable */
361 exec_viewer(fallback
, page
);
362 exec_viewer("man", page
);
363 die(_("no man viewer handled the request"));
366 static void show_info_page(const char *git_cmd
)
368 const char *page
= cmd_to_page(git_cmd
);
369 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
370 execlp("info", "info", "gitman", page
, (char *)NULL
);
371 die(_("no info viewer handled the request"));
374 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
377 char *to_free
= NULL
;
380 html_path
= to_free
= system_path(GIT_HTML_PATH
);
382 /* Check that we have a git documentation directory. */
383 if (!strstr(html_path
, "://")) {
384 if (stat(mkpath("%s/git.html", html_path
), &st
)
385 || !S_ISREG(st
.st_mode
))
386 die("'%s': not a documentation directory.", html_path
);
389 strbuf_init(page_path
, 0);
390 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
394 static void open_html(const char *path
)
396 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
399 static void show_html_page(const char *git_cmd
)
401 const char *page
= cmd_to_page(git_cmd
);
402 struct strbuf page_path
; /* it leaks but we exec bellow */
404 get_html_page_path(&page_path
, page
);
406 open_html(page_path
.buf
);
409 static const char *check_git_cmd(const char* cmd
)
413 if (is_git_command(cmd
))
416 alias
= alias_lookup(cmd
);
418 printf_ln(_("'%s' is aliased to '%s'"), cmd
, alias
);
424 return help_unknown_cmd(cmd
);
429 int cmd_help(int argc
, const char **argv
, const char *prefix
)
432 enum help_format parsed_help_format
;
434 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
435 builtin_help_usage
, 0);
436 parsed_help_format
= help_format
;
439 git_config(git_help_config
, NULL
);
442 list_all_cmds_help();
445 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
446 load_command_list("git-", &main_cmds
, &other_cmds
);
447 list_commands(colopts
, &main_cmds
, &other_cmds
);
451 int for_human
= show_config
== 1;
454 list_config_help(for_human
);
458 list_config_help(for_human
);
459 printf("\n%s\n", _("'git help config' for more information"));
464 list_common_guides_help();
466 if (show_all
|| show_guides
) {
467 printf("%s\n", _(git_more_info_string
));
469 * We're done. Ignore any remaining args
475 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
476 list_common_cmds_help();
477 printf("\n%s\n", _(git_more_info_string
));
481 setup_git_directory_gently(&nongit
);
482 git_config(git_help_config
, NULL
);
484 if (parsed_help_format
!= HELP_FORMAT_NONE
)
485 help_format
= parsed_help_format
;
486 if (help_format
== HELP_FORMAT_NONE
)
487 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
489 argv
[0] = check_git_cmd(argv
[0]);
491 switch (help_format
) {
492 case HELP_FORMAT_NONE
:
493 case HELP_FORMAT_MAN
:
494 show_man_page(argv
[0]);
496 case HELP_FORMAT_INFO
:
497 show_info_page(argv
[0]);
499 case HELP_FORMAT_WEB
:
500 show_html_page(argv
[0]);