9 #include "common-cmds.h"
10 #include "parse-options.h"
11 #include "run-command.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 int show_all
= 0;
38 static unsigned int colopts
;
39 static enum help_format help_format
= HELP_FORMAT_NONE
;
40 static struct option builtin_help_options
[] = {
41 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
42 OPT_SET_INT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
43 OPT_SET_INT('w', "web", &help_format
, "show manual in web browser",
45 OPT_SET_INT('i', "info", &help_format
, "show info page",
50 static const char * const builtin_help_usage
[] = {
51 "git help [--all] [--man|--web|--info] [command]",
55 static enum help_format
parse_help_format(const char *format
)
57 if (!strcmp(format
, "man"))
58 return HELP_FORMAT_MAN
;
59 if (!strcmp(format
, "info"))
60 return HELP_FORMAT_INFO
;
61 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
62 return HELP_FORMAT_WEB
;
63 die(_("unrecognized help format '%s'"), format
);
66 static const char *get_man_viewer_info(const char *name
)
68 struct man_viewer_info_list
*viewer
;
70 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
72 if (!strcasecmp(name
, viewer
->name
))
78 static int check_emacsclient_version(void)
80 struct strbuf buffer
= STRBUF_INIT
;
81 struct child_process ec_process
;
82 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
85 /* emacsclient prints its version number on stderr */
86 memset(&ec_process
, 0, sizeof(ec_process
));
87 ec_process
.argv
= argv_ec
;
89 ec_process
.stdout_to_stderr
= 1;
90 if (start_command(&ec_process
))
91 return error(_("Failed to start emacsclient."));
93 strbuf_read(&buffer
, ec_process
.err
, 20);
94 close(ec_process
.err
);
97 * Don't bother checking return value, because "emacsclient --version"
98 * seems to always exits with code 1.
100 finish_command(&ec_process
);
102 if (prefixcmp(buffer
.buf
, "emacsclient")) {
103 strbuf_release(&buffer
);
104 return error(_("Failed to parse emacsclient version."));
107 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
108 version
= atoi(buffer
.buf
);
111 strbuf_release(&buffer
);
112 return error(_("emacsclient version '%d' too old (< 22)."),
116 strbuf_release(&buffer
);
120 static void exec_woman_emacs(const char *path
, const char *page
)
122 if (!check_emacsclient_version()) {
123 /* This works only with emacsclient version >= 22. */
124 struct strbuf man_page
= STRBUF_INIT
;
127 path
= "emacsclient";
128 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
129 execlp(path
, "emacsclient", "-e", man_page
.buf
, (char *)NULL
);
130 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
134 static void exec_man_konqueror(const char *path
, const char *page
)
136 const char *display
= getenv("DISPLAY");
137 if (display
&& *display
) {
138 struct strbuf man_page
= STRBUF_INIT
;
139 const char *filename
= "kfmclient";
141 /* It's simpler to launch konqueror using kfmclient. */
143 const char *file
= strrchr(path
, '/');
144 if (file
&& !strcmp(file
+ 1, "konqueror")) {
145 char *new = xstrdup(path
);
146 char *dest
= strrchr(new, '/');
148 /* strlen("konqueror") == strlen("kfmclient") */
149 strcpy(dest
+ 1, "kfmclient");
156 strbuf_addf(&man_page
, "man:%s(1)", page
);
157 execlp(path
, filename
, "newTab", man_page
.buf
, (char *)NULL
);
158 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
162 static void exec_man_man(const char *path
, const char *page
)
166 execlp(path
, "man", page
, (char *)NULL
);
167 warning(_("failed to exec '%s': %s"), path
, strerror(errno
));
170 static void exec_man_cmd(const char *cmd
, const char *page
)
172 struct strbuf shell_cmd
= STRBUF_INIT
;
173 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
174 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, (char *)NULL
);
175 warning(_("failed to exec '%s': %s"), cmd
, strerror(errno
));
178 static void add_man_viewer(const char *name
)
180 struct man_viewer_list
**p
= &man_viewer_list
;
181 size_t len
= strlen(name
);
185 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
186 strncpy((*p
)->name
, name
, len
);
189 static int supported_man_viewer(const char *name
, size_t len
)
191 return (!strncasecmp("man", name
, len
) ||
192 !strncasecmp("woman", name
, len
) ||
193 !strncasecmp("konqueror", name
, len
));
196 static void do_add_man_viewer_info(const char *name
,
200 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
202 strncpy(new->name
, name
, len
);
203 new->info
= xstrdup(value
);
204 new->next
= man_viewer_info_list
;
205 man_viewer_info_list
= new;
208 static int add_man_viewer_path(const char *name
,
212 if (supported_man_viewer(name
, len
))
213 do_add_man_viewer_info(name
, len
, value
);
215 warning(_("'%s': path for unsupported man viewer.\n"
216 "Please consider using 'man.<tool>.cmd' instead."),
222 static int add_man_viewer_cmd(const char *name
,
226 if (supported_man_viewer(name
, len
))
227 warning(_("'%s': cmd for supported man viewer.\n"
228 "Please consider using 'man.<tool>.path' instead."),
231 do_add_man_viewer_info(name
, len
, value
);
236 static int add_man_viewer_info(const char *var
, const char *value
)
238 const char *name
= var
+ 4;
239 const char *subkey
= strrchr(name
, '.');
244 if (!strcmp(subkey
, ".path")) {
246 return config_error_nonbool(var
);
247 return add_man_viewer_path(name
, subkey
- name
, value
);
249 if (!strcmp(subkey
, ".cmd")) {
251 return config_error_nonbool(var
);
252 return add_man_viewer_cmd(name
, subkey
- name
, value
);
258 static int git_help_config(const char *var
, const char *value
, void *cb
)
260 if (!prefixcmp(var
, "column."))
261 return git_column_config(var
, value
, "help", &colopts
);
262 if (!strcmp(var
, "help.format")) {
264 return config_error_nonbool(var
);
265 help_format
= parse_help_format(value
);
268 if (!strcmp(var
, "man.viewer")) {
270 return config_error_nonbool(var
);
271 add_man_viewer(value
);
274 if (!prefixcmp(var
, "man."))
275 return add_man_viewer_info(var
, value
);
277 return git_default_config(var
, value
, cb
);
280 static struct cmdnames main_cmds
, other_cmds
;
282 void list_common_cmds_help(void)
286 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
287 if (longest
< strlen(common_cmds
[i
].name
))
288 longest
= strlen(common_cmds
[i
].name
);
291 puts(_("The most commonly used git commands are:"));
292 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
293 printf(" %s ", common_cmds
[i
].name
);
294 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
295 puts(_(common_cmds
[i
].help
));
299 static int is_git_command(const char *s
)
301 return is_in_cmdlist(&main_cmds
, s
) ||
302 is_in_cmdlist(&other_cmds
, s
);
305 static const char *prepend(const char *prefix
, const char *cmd
)
307 size_t pre_len
= strlen(prefix
);
308 size_t cmd_len
= strlen(cmd
);
309 char *p
= xmalloc(pre_len
+ cmd_len
+ 1);
310 memcpy(p
, prefix
, pre_len
);
311 strcpy(p
+ pre_len
, cmd
);
315 static const char *cmd_to_page(const char *git_cmd
)
319 else if (!prefixcmp(git_cmd
, "git"))
321 else if (is_git_command(git_cmd
))
322 return prepend("git-", git_cmd
);
324 return prepend("git", git_cmd
);
327 static void setup_man_path(void)
329 struct strbuf new_path
= STRBUF_INIT
;
330 const char *old_path
= getenv("MANPATH");
332 /* We should always put ':' after our path. If there is no
333 * old_path, the ':' at the end will let 'man' to try
334 * system-wide paths after ours to find the manual page. If
335 * there is old_path, we need ':' as delimiter. */
336 strbuf_addstr(&new_path
, system_path(GIT_MAN_PATH
));
337 strbuf_addch(&new_path
, ':');
339 strbuf_addstr(&new_path
, old_path
);
341 setenv("MANPATH", new_path
.buf
, 1);
343 strbuf_release(&new_path
);
346 static void exec_viewer(const char *name
, const char *page
)
348 const char *info
= get_man_viewer_info(name
);
350 if (!strcasecmp(name
, "man"))
351 exec_man_man(info
, page
);
352 else if (!strcasecmp(name
, "woman"))
353 exec_woman_emacs(info
, page
);
354 else if (!strcasecmp(name
, "konqueror"))
355 exec_man_konqueror(info
, page
);
357 exec_man_cmd(info
, page
);
359 warning(_("'%s': unknown man viewer."), name
);
362 static void show_man_page(const char *git_cmd
)
364 struct man_viewer_list
*viewer
;
365 const char *page
= cmd_to_page(git_cmd
);
366 const char *fallback
= getenv("GIT_MAN_VIEWER");
369 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
371 exec_viewer(viewer
->name
, page
); /* will return when unable */
374 exec_viewer(fallback
, page
);
375 exec_viewer("man", page
);
376 die(_("no man viewer handled the request"));
379 static void show_info_page(const char *git_cmd
)
381 const char *page
= cmd_to_page(git_cmd
);
382 setenv("INFOPATH", system_path(GIT_INFO_PATH
), 1);
383 execlp("info", "info", "gitman", page
, (char *)NULL
);
384 die(_("no info viewer handled the request"));
387 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
390 const char *html_path
= system_path(GIT_HTML_PATH
);
392 /* Check that we have a git documentation directory. */
393 if (stat(mkpath("%s/git.html", html_path
), &st
)
394 || !S_ISREG(st
.st_mode
))
395 die(_("'%s': not a documentation directory."), html_path
);
397 strbuf_init(page_path
, 0);
398 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
402 * If open_html is not defined in a platform-specific way (see for
403 * example compat/mingw.h), we use the script web--browse to display
407 static void open_html(const char *path
)
409 execl_git_cmd("web--browse", "-c", "help.browser", path
, (char *)NULL
);
413 static void show_html_page(const char *git_cmd
)
415 const char *page
= cmd_to_page(git_cmd
);
416 struct strbuf page_path
; /* it leaks but we exec bellow */
418 get_html_page_path(&page_path
, page
);
420 open_html(page_path
.buf
);
423 int cmd_help(int argc
, const char **argv
, const char *prefix
)
427 enum help_format parsed_help_format
;
428 load_command_list("git-", &main_cmds
, &other_cmds
);
430 argc
= parse_options(argc
, argv
, prefix
, builtin_help_options
,
431 builtin_help_usage
, 0);
432 parsed_help_format
= help_format
;
435 git_config(git_help_config
, NULL
);
436 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
437 list_commands(colopts
, &main_cmds
, &other_cmds
);
438 printf("%s\n", _(git_more_info_string
));
443 printf(_("usage: %s%s"), _(git_usage_string
), "\n\n");
444 list_common_cmds_help();
445 printf("\n%s\n", _(git_more_info_string
));
449 setup_git_directory_gently(&nongit
);
450 git_config(git_help_config
, NULL
);
452 if (parsed_help_format
!= HELP_FORMAT_NONE
)
453 help_format
= parsed_help_format
;
454 if (help_format
== HELP_FORMAT_NONE
)
455 help_format
= parse_help_format(DEFAULT_HELP_FORMAT
);
457 alias
= alias_lookup(argv
[0]);
458 if (alias
&& !is_git_command(argv
[0])) {
459 printf_ln(_("`git %s' is aliased to `%s'"), argv
[0], alias
);
463 switch (help_format
) {
464 case HELP_FORMAT_NONE
:
465 case HELP_FORMAT_MAN
:
466 show_man_page(argv
[0]);
468 case HELP_FORMAT_INFO
:
469 show_info_page(argv
[0]);
471 case HELP_FORMAT_WEB
:
472 show_html_page(argv
[0]);