9 #include "parse-options.h"
10 #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 unsigned int colopts
;
41 static enum help_format help_format
= HELP_FORMAT_NONE
;
42 static struct option builtin_help_options
[] = {
43 OPT_BOOL('a', "all", &show_all
, N_("print all available commands")),
44 OPT_BOOL('g', "guides", &show_guides
, N_("print list of useful guides")),
45 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
46 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
48 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
53 static const char * const builtin_help_usage
[] = {
54 N_("git help [--all] [--guides] [--man|--web|--info] [command]"),
58 static enum help_format
parse_help_format(const char *format
)
60 if (!strcmp(format
, "man"))
61 return HELP_FORMAT_MAN
;
62 if (!strcmp(format
, "info"))
63 return HELP_FORMAT_INFO
;
64 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
65 return HELP_FORMAT_WEB
;
66 die(_("unrecognized help format '%s'"), format
);
69 static const char *get_man_viewer_info(const char *name
)
71 struct man_viewer_info_list
*viewer
;
73 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
75 if (!strcasecmp(name
, viewer
->name
))
81 static int check_emacsclient_version(void)
83 struct strbuf buffer
= STRBUF_INIT
;
84 struct child_process ec_process
;
85 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
88 /* emacsclient prints its version number on stderr */
89 memset(&ec_process
, 0, sizeof(ec_process
));
90 ec_process
.argv
= argv_ec
;
92 ec_process
.stdout_to_stderr
= 1;
93 if (start_command(&ec_process
))
94 return error(_("Failed to start emacsclient."));
96 strbuf_read(&buffer
, ec_process
.err
, 20);
97 close(ec_process
.err
);
100 * Don't bother checking return value, because "emacsclient --version"
101 * seems to always exits with code 1.
103 finish_command(&ec_process
);
105 if (prefixcmp(buffer
.buf
, "emacsclient")) {
106 strbuf_release(&buffer
);
107 return error(_("Failed to parse emacsclient version."));
110 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
111 version
= atoi(buffer
.buf
);
114 strbuf_release(&buffer
);
115 return error(_("emacsclient version '%d' too old (< 22)."),
119 strbuf_release(&buffer
);
123 static void exec_woman_emacs(const char *path
, const char *page
)
125 if (!check_emacsclient_version()) {
126 /* This works only with emacsclient version >= 22. */
127 struct strbuf man_page
= STRBUF_INIT
;
130 path
= "emacsclient";
131 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
132 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
133 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
137 static void exec_man_konqueror(const char *path
, const char *page
)
139 const char *display
= getenv("DISPLAY");
140 if (display
&& *display
) {
141 struct strbuf man_page
= STRBUF_INIT
;
142 const char *filename
= "kfmclient";
144 /* It's simpler to launch konqueror using kfmclient. */
146 const char *file
= strrchr(path
, '/');
147 if (file
&& !strcmp(file
+ 1, "konqueror")) {
148 char *new = xstrdup(path
);
149 char *dest
= strrchr(new, '/');
151 /* strlen("konqueror") == strlen("kfmclient") */
152 strcpy(dest
+ 1, "kfmclient");
159 strbuf_addf(&man_page
, "man:%s(1)", page
);
160 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
161 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
165 static void exec_man_man(const char *path
, const char *page
)
169 execlp(path
, "man", page
, (char *)NULL
);
170 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
173 static void exec_man_cmd(const char *cmd
, const char *page
)
175 struct strbuf shell_cmd
= STRBUF_INIT
;
176 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
177 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, (char *)NULL
);
178 warning(_("failed to exec '%s': %s"), cmd
, strerror(errno
));
181 static void add_man_viewer(const char *name
)
183 struct man_viewer_list
**p
= &man_viewer_list
;
184 size_t len
= strlen(name
);
188 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
189 strncpy((*p
)->name
, name
, len
);
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 = xcalloc(1, sizeof(*new) + len
+ 1);
205 strncpy(new->name
, name
, len
);
206 new->info
= xstrdup(value
);
207 new->next
= man_viewer_info_list
;
208 man_viewer_info_list
= new;
211 static int add_man_viewer_path(const char *name
,
215 if (supported_man_viewer(name
, len
))
216 do_add_man_viewer_info(name
, len
, value
);
218 warning(_("'%s': path for unsupported man viewer.\n"
219 "Please consider using 'man.<tool>.cmd' instead."),
225 static int add_man_viewer_cmd(const char *name
,
229 if (supported_man_viewer(name
, len
))
230 warning(_("'%s': cmd for supported man viewer.\n"
231 "Please consider using 'man.<tool>.path' instead."),
234 do_add_man_viewer_info(name
, len
, value
);
239 static int add_man_viewer_info(const char *var
, const char *value
)
241 const char *name
, *subkey
;
244 if (parse_config_key(var
, "man", &name
, &namelen
, &subkey
) < 0 || !name
)
247 if (!strcmp(subkey
, "path")) {
249 return config_error_nonbool(var
);
250 return add_man_viewer_path(name
, namelen
, value
);
252 if (!strcmp(subkey
, "cmd")) {
254 return config_error_nonbool(var
);
255 return add_man_viewer_cmd(name
, namelen
, value
);
261 static int git_help_config(const char *var
, const char *value
, void *cb
)
263 if (!prefixcmp(var
, "column."))
264 return git_column_config(var
, value
, "help", &colopts
);
265 if (!strcmp(var
, "help.format")) {
267 return config_error_nonbool(var
);
268 help_format
= parse_help_format(value
);
271 if (!strcmp(var
, "help.htmlpath")) {
273 return config_error_nonbool(var
);
274 html_path
= xstrdup(value
);
277 if (!strcmp(var
, "man.viewer")) {
279 return config_error_nonbool(var
);
280 add_man_viewer(value
);
283 if (!prefixcmp(var
, "man."))
284 return add_man_viewer_info(var
, value
);
286 return git_default_config(var
, value
, cb
);
289 static struct cmdnames main_cmds
, other_cmds
;
291 static int is_git_command(const char *s
)
293 return is_in_cmdlist(&main_cmds
, s
) ||
294 is_in_cmdlist(&other_cmds
, s
);
297 static const char *prepend(const char *prefix
, const char *cmd
)
299 size_t pre_len
= strlen(prefix
);
300 size_t cmd_len
= strlen(cmd
);
301 char *p
= xmalloc(pre_len
+ cmd_len
+ 1);
302 memcpy(p
, prefix
, pre_len
);
303 strcpy(p
+ pre_len
, cmd
);
307 static const char *cmd_to_page(const char *git_cmd
)
311 else if (!prefixcmp(git_cmd
, "git"))
313 else if (is_git_command(git_cmd
))
314 return prepend("git-", git_cmd
);
316 return prepend("git", git_cmd
);
319 static void setup_man_path(void)
321 struct strbuf new_path
= STRBUF_INIT
;
322 const char *old_path
= getenv("MANPATH");
324 /* We should always put ':' after our path. If there is no
325 * old_path, the ':' at the end will let 'man' to try
326 * system-wide paths after ours to find the manual page. If
327 * there is old_path, we need ':' as delimiter. */
328 strbuf_addstr(&new_path
, system_path(GIT_MAN_PATH
));
329 strbuf_addch(&new_path
, ':');
331 strbuf_addstr(&new_path
, old_path
);
333 setenv("MANPATH", new_path
.buf
, 1);
335 strbuf_release(&new_path
);
338 static void exec_viewer(const char *name
, const char *page
)
340 const char *info
= get_man_viewer_info(name
);
342 if (!strcasecmp(name
, "man"))
343 exec_man_man(info
, page
);
344 else if (!strcasecmp(name
, "woman"))
345 exec_woman_emacs(info
, page
);
346 else if (!strcasecmp(name
, "konqueror"))
347 exec_man_konqueror(info
, page
);
349 exec_man_cmd(info
, page
);
351 warning(_("'%s': unknown man viewer."), name
);
354 static void show_man_page(const char *git_cmd
)
356 struct man_viewer_list
*viewer
;
357 const char *page
= cmd_to_page(git_cmd
);
358 const char *fallback
= getenv("GIT_MAN_VIEWER");
361 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
363 exec_viewer(viewer
->name
, page
); /* will return when unable */
366 exec_viewer(fallback
, page
);
367 exec_viewer("man", page
);
368 die(_("no man viewer handled the request"));
371 static void show_info_page(const char *git_cmd
)
373 const char *page
= cmd_to_page(git_cmd
);
374 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
375 execlp("info", "info", "gitman", page
, (char *)NULL
);
376 die(_("no info viewer handled the request"));
379 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
383 html_path
= system_path(GIT_HTML_PATH
);
385 /* Check that we have a git documentation directory. */
386 if (!strstr(html_path
, "://")) {
387 if (stat(mkpath("%s/git.html", html_path
), &st
)
388 || !S_ISREG(st
.st_mode
))
389 die("'%s': not a documentation directory.", html_path
);
392 strbuf_init(page_path
, 0);
393 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
397 * If open_html is not defined in a platform-specific way (see for
398 * example compat/mingw.h), we use the script web--browse to display
402 static void open_html(const char *path
)
404 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
408 static void show_html_page(const char *git_cmd
)
410 const char *page
= cmd_to_page(git_cmd
);
411 struct strbuf page_path
; /* it leaks but we exec bellow */
413 get_html_page_path(&page_path
, page
);
415 open_html(page_path
.buf
);
421 } common_guides
[] = {
422 { "attributes", N_("Defining attributes per path") },
423 { "glossary", N_("A Git glossary") },
424 { "ignore", N_("Specifies intentionally untracked files to ignore") },
425 { "modules", N_("Defining submodule properties") },
426 { "revisions", N_("Specifying revisions and ranges for Git") },
427 { "tutorial", N_("A tutorial introduction to Git (for version 1.5.1 or newer)") },
428 { "workflows", N_("An overview of recommended workflows with Git") },
431 static void list_common_guides_help(void)
435 for (i
= 0; i
< ARRAY_SIZE(common_guides
); i
++) {
436 if (longest
< strlen(common_guides
[i
].name
))
437 longest
= strlen(common_guides
[i
].name
);
440 puts(_("The common Git guides are:\n"));
441 for (i
= 0; i
< ARRAY_SIZE(common_guides
); i
++) {
442 printf(" %s ", common_guides
[i
].name
);
443 mput_char(' ', longest
- strlen(common_guides
[i
].name
));
444 puts(_(common_guides
[i
].help
));
449 int cmd_help(int argc
, const char **argv
, const char *prefix
)
453 enum help_format parsed_help_format
;
454 load_command_list("git-", &main_cmds
, &other_cmds
);
456 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
457 builtin_help_usage
, 0);
458 parsed_help_format
= help_format
;
461 git_config(git_help_config
, NULL
);
462 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
463 list_commands(colopts
, &main_cmds
, &other_cmds
);
467 list_common_guides_help();
469 if (show_all
|| show_guides
) {
470 printf("%s\n", _(git_more_info_string
));
472 * We're done. Ignore any remaining args
478 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
479 list_common_cmds_help();
480 printf("\n%s\n", _(git_more_info_string
));
484 setup_git_directory_gently(&nongit
);
485 git_config(git_help_config
, NULL
);
487 if (parsed_help_format
!= HELP_FORMAT_NONE
)
488 help_format
= parsed_help_format
;
489 if (help_format
== HELP_FORMAT_NONE
)
490 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
492 alias
= alias_lookup(argv
[0]);
493 if (alias
&& !is_git_command(argv
[0])) {
494 printf_ln(_("`git %s' is aliased to `%s'"), argv
[0], alias
);
498 switch (help_format
) {
499 case HELP_FORMAT_NONE
:
500 case HELP_FORMAT_MAN
:
501 show_man_page(argv
[0]);
503 case HELP_FORMAT_INFO
:
504 show_info_page(argv
[0]);
506 case HELP_FORMAT_WEB
:
507 show_html_page(argv
[0]);