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 unsigned int colopts
;
40 static enum help_format help_format
= HELP_FORMAT_NONE
;
41 static struct option builtin_help_options
[] = {
42 OPT_BOOLEAN('a', "all", &show_all
, N_("print all available commands")),
43 OPT_SET_INT('m', "man", &help_format
, N_("show man page"), HELP_FORMAT_MAN
),
44 OPT_SET_INT('w', "web", &help_format
, N_("show manual in web browser"),
46 OPT_SET_INT('i', "info", &help_format
, N_("show info page"),
51 static const char * const builtin_help_usage
[] = {
52 N_("git help [--all] [--man|--web|--info] [command]"),
56 static enum help_format
parse_help_format(const char *format
)
58 if (!strcmp(format
, "man"))
59 return HELP_FORMAT_MAN
;
60 if (!strcmp(format
, "info"))
61 return HELP_FORMAT_INFO
;
62 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
63 return HELP_FORMAT_WEB
;
64 die(_("unrecognized help format '%s'"), format
);
67 static const char *get_man_viewer_info(const char *name
)
69 struct man_viewer_info_list
*viewer
;
71 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
73 if (!strcasecmp(name
, viewer
->name
))
79 static int check_emacsclient_version(void)
81 struct strbuf buffer
= STRBUF_INIT
;
82 struct child_process ec_process
;
83 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
86 /* emacsclient prints its version number on stderr */
87 memset(&ec_process
, 0, sizeof(ec_process
));
88 ec_process
.argv
= argv_ec
;
90 ec_process
.stdout_to_stderr
= 1;
91 if (start_command(&ec_process
))
92 return error(_("Failed to start emacsclient."));
94 strbuf_read(&buffer
, ec_process
.err
, 20);
95 close(ec_process
.err
);
98 * Don't bother checking return value, because "emacsclient --version"
99 * seems to always exits with code 1.
101 finish_command(&ec_process
);
103 if (prefixcmp(buffer
.buf
, "emacsclient")) {
104 strbuf_release(&buffer
);
105 return error(_("Failed to parse emacsclient version."));
108 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
109 version
= atoi(buffer
.buf
);
112 strbuf_release(&buffer
);
113 return error(_("emacsclient version '%d' too old (< 22)."),
117 strbuf_release(&buffer
);
121 static void exec_woman_emacs(const char *path
, const char *page
)
123 if (!check_emacsclient_version()) {
124 /* This works only with emacsclient version >= 22. */
125 struct strbuf man_page
= STRBUF_INIT
;
128 path
= "emacsclient";
129 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
130 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
131 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
135 static void exec_man_konqueror(const char *path
, const char *page
)
137 const char *display
= getenv("DISPLAY");
138 if (display
&& *display
) {
139 struct strbuf man_page
= STRBUF_INIT
;
140 const char *filename
= "kfmclient";
142 /* It's simpler to launch konqueror using kfmclient. */
144 const char *file
= strrchr(path
, '/');
145 if (file
&& !strcmp(file
+ 1, "konqueror")) {
146 char *new = xstrdup(path
);
147 char *dest
= strrchr(new, '/');
149 /* strlen("konqueror") == strlen("kfmclient") */
150 strcpy(dest
+ 1, "kfmclient");
157 strbuf_addf(&man_page
, "man:%s(1)", page
);
158 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
159 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
163 static void exec_man_man(const char *path
, const char *page
)
167 execlp(path
, "man", page
, (char *)NULL
);
168 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
171 static void exec_man_cmd(const char *cmd
, const char *page
)
173 struct strbuf shell_cmd
= STRBUF_INIT
;
174 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
175 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, (char *)NULL
);
176 warning(_("failed to exec '%s': %s"), cmd
, strerror(errno
));
179 static void add_man_viewer(const char *name
)
181 struct man_viewer_list
**p
= &man_viewer_list
;
182 size_t len
= strlen(name
);
186 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
187 strncpy((*p
)->name
, name
, len
);
190 static int supported_man_viewer(const char *name
, size_t len
)
192 return (!strncasecmp("man", name
, len
) ||
193 !strncasecmp("woman", name
, len
) ||
194 !strncasecmp("konqueror", name
, len
));
197 static void do_add_man_viewer_info(const char *name
,
201 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
203 strncpy(new->name
, name
, len
);
204 new->info
= xstrdup(value
);
205 new->next
= man_viewer_info_list
;
206 man_viewer_info_list
= new;
209 static int add_man_viewer_path(const char *name
,
213 if (supported_man_viewer(name
, len
))
214 do_add_man_viewer_info(name
, len
, value
);
216 warning(_("'%s': path for unsupported man viewer.\n"
217 "Please consider using 'man.<tool>.cmd' instead."),
223 static int add_man_viewer_cmd(const char *name
,
227 if (supported_man_viewer(name
, len
))
228 warning(_("'%s': cmd for supported man viewer.\n"
229 "Please consider using 'man.<tool>.path' instead."),
232 do_add_man_viewer_info(name
, len
, value
);
237 static int add_man_viewer_info(const char *var
, const char *value
)
239 const char *name
= var
+ 4;
240 const char *subkey
= strrchr(name
, '.');
245 if (!strcmp(subkey
, ".path")) {
247 return config_error_nonbool(var
);
248 return add_man_viewer_path(name
, subkey
- name
, value
);
250 if (!strcmp(subkey
, ".cmd")) {
252 return config_error_nonbool(var
);
253 return add_man_viewer_cmd(name
, subkey
- name
, value
);
259 static int git_help_config(const char *var
, const char *value
, void *cb
)
261 if (!prefixcmp(var
, "column."))
262 return git_column_config(var
, value
, "help", &colopts
);
263 if (!strcmp(var
, "help.format")) {
265 return config_error_nonbool(var
);
266 help_format
= parse_help_format(value
);
269 if (!strcmp(var
, "help.htmlpath")) {
271 return config_error_nonbool(var
);
272 html_path
= xstrdup(value
);
275 if (!strcmp(var
, "man.viewer")) {
277 return config_error_nonbool(var
);
278 add_man_viewer(value
);
281 if (!prefixcmp(var
, "man."))
282 return add_man_viewer_info(var
, value
);
284 return git_default_config(var
, value
, cb
);
287 static struct cmdnames main_cmds
, other_cmds
;
289 static int is_git_command(const char *s
)
291 return is_in_cmdlist(&main_cmds
, s
) ||
292 is_in_cmdlist(&other_cmds
, s
);
295 static const char *prepend(const char *prefix
, const char *cmd
)
297 size_t pre_len
= strlen(prefix
);
298 size_t cmd_len
= strlen(cmd
);
299 char *p
= xmalloc(pre_len
+ cmd_len
+ 1);
300 memcpy(p
, prefix
, pre_len
);
301 strcpy(p
+ pre_len
, cmd
);
305 static const char *cmd_to_page(const char *git_cmd
)
309 else if (!prefixcmp(git_cmd
, "git"))
311 else if (is_git_command(git_cmd
))
312 return prepend("git-", git_cmd
);
314 return prepend("git", git_cmd
);
317 static void setup_man_path(void)
319 struct strbuf new_path
= STRBUF_INIT
;
320 const char *old_path
= getenv("MANPATH");
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
, system_path(GIT_MAN_PATH
));
327 strbuf_addch(&new_path
, ':');
329 strbuf_addstr(&new_path
, old_path
);
331 setenv("MANPATH", new_path
.buf
, 1);
333 strbuf_release(&new_path
);
336 static void exec_viewer(const char *name
, const char *page
)
338 const char *info
= get_man_viewer_info(name
);
340 if (!strcasecmp(name
, "man"))
341 exec_man_man(info
, page
);
342 else if (!strcasecmp(name
, "woman"))
343 exec_woman_emacs(info
, page
);
344 else if (!strcasecmp(name
, "konqueror"))
345 exec_man_konqueror(info
, page
);
347 exec_man_cmd(info
, page
);
349 warning(_("'%s': unknown man viewer."), name
);
352 static void show_man_page(const char *git_cmd
)
354 struct man_viewer_list
*viewer
;
355 const char *page
= cmd_to_page(git_cmd
);
356 const char *fallback
= getenv("GIT_MAN_VIEWER");
359 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
361 exec_viewer(viewer
->name
, page
); /* will return when unable */
364 exec_viewer(fallback
, page
);
365 exec_viewer("man", page
);
366 die(_("no man viewer handled the request"));
369 static void show_info_page(const char *git_cmd
)
371 const char *page
= cmd_to_page(git_cmd
);
372 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
373 execlp("info", "info", "gitman", page
, (char *)NULL
);
374 die(_("no info viewer handled the request"));
377 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
381 html_path
= system_path(GIT_HTML_PATH
);
383 /* Check that we have a git documentation directory. */
384 if (!strstr(html_path
, "://")) {
385 if (stat(mkpath("%s/git.html", html_path
), &st
)
386 || !S_ISREG(st
.st_mode
))
387 die("'%s': not a documentation directory.", html_path
);
390 strbuf_init(page_path
, 0);
391 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
395 * If open_html is not defined in a platform-specific way (see for
396 * example compat/mingw.h), we use the script web--browse to display
400 static void open_html(const char *path
)
402 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
406 static void show_html_page(const char *git_cmd
)
408 const char *page
= cmd_to_page(git_cmd
);
409 struct strbuf page_path
; /* it leaks but we exec bellow */
411 get_html_page_path(&page_path
, page
);
413 open_html(page_path
.buf
);
416 int cmd_help(int argc
, const char **argv
, const char *prefix
)
420 enum help_format parsed_help_format
;
421 load_command_list("git-", &main_cmds
, &other_cmds
);
423 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
424 builtin_help_usage
, 0);
425 parsed_help_format
= help_format
;
428 git_config(git_help_config
, NULL
);
429 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
430 list_commands(colopts
, &main_cmds
, &other_cmds
);
431 printf("%s\n", _(git_more_info_string
));
436 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
437 list_common_cmds_help();
438 printf("\n%s\n", _(git_more_info_string
));
442 setup_git_directory_gently(&nongit
);
443 git_config(git_help_config
, NULL
);
445 if (parsed_help_format
!= HELP_FORMAT_NONE
)
446 help_format
= parsed_help_format
;
447 if (help_format
== HELP_FORMAT_NONE
)
448 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
450 alias
= alias_lookup(argv
[0]);
451 if (alias
&& !is_git_command(argv
[0])) {
452 printf_ln(_("`git %s' is aliased to `%s'"), argv
[0], alias
);
456 switch (help_format
) {
457 case HELP_FORMAT_NONE
:
458 case HELP_FORMAT_MAN
:
459 show_man_page(argv
[0]);
461 case HELP_FORMAT_INFO
:
462 show_info_page(argv
[0]);
464 case HELP_FORMAT_WEB
:
465 show_html_page(argv
[0]);