]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/help.c
builtin/help.c: add --guide option
[thirdparty/git.git] / builtin / help.c
CommitLineData
3d78d1f1
MV
1/*
2 * builtin-help.c
3 *
4 * Builtin help command
5 */
6#include "cache.h"
7#include "builtin.h"
8#include "exec_cmd.h"
3d78d1f1
MV
9#include "parse-options.h"
10#include "run-command.h"
dbfae689 11#include "column.h"
3d78d1f1
MV
12#include "help.h"
13
1cc8af04
VR
14#ifndef DEFAULT_HELP_FORMAT
15#define DEFAULT_HELP_FORMAT "man"
16#endif
17
3d78d1f1
MV
18static struct man_viewer_list {
19 struct man_viewer_list *next;
20 char name[FLEX_ARRAY];
21} *man_viewer_list;
22
23static struct man_viewer_info_list {
24 struct man_viewer_info_list *next;
25 const char *info;
26 char name[FLEX_ARRAY];
27} *man_viewer_info_list;
28
29enum help_format {
3caa8239 30 HELP_FORMAT_NONE,
3d78d1f1
MV
31 HELP_FORMAT_MAN,
32 HELP_FORMAT_INFO,
4b05548f 33 HELP_FORMAT_WEB
3d78d1f1
MV
34};
35
89a852ef
CW
36static const char *html_path;
37
3d78d1f1 38static int show_all = 0;
65f98358 39static int show_guides = 0;
dbfae689 40static unsigned int colopts;
3caa8239 41static enum help_format help_format = HELP_FORMAT_NONE;
3d78d1f1 42static struct option builtin_help_options[] = {
15f7d494 43 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
65f98358 44 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
68918696
NTND
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"),
3d78d1f1 47 HELP_FORMAT_WEB),
68918696 48 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
3d78d1f1
MV
49 HELP_FORMAT_INFO),
50 OPT_END(),
51};
52
53static const char * const builtin_help_usage[] = {
65f98358 54 N_("git help [--all] [--guides] [--man|--web|--info] [command]"),
3d78d1f1
MV
55 NULL
56};
57
58static enum help_format parse_help_format(const char *format)
59{
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;
9665627d 66 die(_("unrecognized help format '%s'"), format);
3d78d1f1
MV
67}
68
69static const char *get_man_viewer_info(const char *name)
70{
71 struct man_viewer_info_list *viewer;
72
73 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
74 {
75 if (!strcasecmp(name, viewer->name))
76 return viewer->info;
77 }
78 return NULL;
79}
80
81static int check_emacsclient_version(void)
82{
83 struct strbuf buffer = STRBUF_INIT;
84 struct child_process ec_process;
85 const char *argv_ec[] = { "emacsclient", "--version", NULL };
86 int version;
87
88 /* emacsclient prints its version number on stderr */
89 memset(&ec_process, 0, sizeof(ec_process));
90 ec_process.argv = argv_ec;
91 ec_process.err = -1;
92 ec_process.stdout_to_stderr = 1;
4e2715fb 93 if (start_command(&ec_process))
9665627d 94 return error(_("Failed to start emacsclient."));
4e2715fb 95
3d78d1f1
MV
96 strbuf_read(&buffer, ec_process.err, 20);
97 close(ec_process.err);
98
99 /*
100 * Don't bother checking return value, because "emacsclient --version"
101 * seems to always exits with code 1.
102 */
103 finish_command(&ec_process);
104
105 if (prefixcmp(buffer.buf, "emacsclient")) {
3d78d1f1 106 strbuf_release(&buffer);
9665627d 107 return error(_("Failed to parse emacsclient version."));
3d78d1f1
MV
108 }
109
110 strbuf_remove(&buffer, 0, strlen("emacsclient"));
111 version = atoi(buffer.buf);
112
113 if (version < 22) {
3d78d1f1 114 strbuf_release(&buffer);
9665627d 115 return error(_("emacsclient version '%d' too old (< 22)."),
4e2715fb 116 version);
3d78d1f1
MV
117 }
118
119 strbuf_release(&buffer);
120 return 0;
121}
122
4b25d091 123static void exec_woman_emacs(const char *path, const char *page)
3d78d1f1
MV
124{
125 if (!check_emacsclient_version()) {
126 /* This works only with emacsclient version >= 22. */
127 struct strbuf man_page = STRBUF_INIT;
128
129 if (!path)
130 path = "emacsclient";
131 strbuf_addf(&man_page, "(woman \"%s\")", page);
5d314759 132 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
9665627d 133 warning(_("failed to exec '%s': %s"), path, strerror(errno));
3d78d1f1
MV
134 }
135}
136
4b25d091 137static void exec_man_konqueror(const char *path, const char *page)
3d78d1f1
MV
138{
139 const char *display = getenv("DISPLAY");
140 if (display && *display) {
141 struct strbuf man_page = STRBUF_INIT;
142 const char *filename = "kfmclient";
143
144 /* It's simpler to launch konqueror using kfmclient. */
145 if (path) {
146 const char *file = strrchr(path, '/');
147 if (file && !strcmp(file + 1, "konqueror")) {
148 char *new = xstrdup(path);
149 char *dest = strrchr(new, '/');
150
151 /* strlen("konqueror") == strlen("kfmclient") */
152 strcpy(dest + 1, "kfmclient");
153 path = new;
154 }
155 if (file)
156 filename = file;
157 } else
158 path = "kfmclient";
159 strbuf_addf(&man_page, "man:%s(1)", page);
5d314759 160 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
9665627d 161 warning(_("failed to exec '%s': %s"), path, strerror(errno));
3d78d1f1
MV
162 }
163}
164
4b25d091 165static void exec_man_man(const char *path, const char *page)
3d78d1f1
MV
166{
167 if (!path)
168 path = "man";
5d314759 169 execlp(path, "man", page, (char *)NULL);
9665627d 170 warning(_("failed to exec '%s': %s"), path, strerror(errno));
3d78d1f1
MV
171}
172
173static void exec_man_cmd(const char *cmd, const char *page)
174{
175 struct strbuf shell_cmd = STRBUF_INIT;
176 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
5d314759 177 execl("/bin/sh", "sh", "-c", shell_cmd.buf, (char *)NULL);
9665627d 178 warning(_("failed to exec '%s': %s"), cmd, strerror(errno));
3d78d1f1
MV
179}
180
181static void add_man_viewer(const char *name)
182{
183 struct man_viewer_list **p = &man_viewer_list;
184 size_t len = strlen(name);
185
186 while (*p)
187 p = &((*p)->next);
188 *p = xcalloc(1, (sizeof(**p) + len + 1));
189 strncpy((*p)->name, name, len);
190}
191
192static int supported_man_viewer(const char *name, size_t len)
193{
194 return (!strncasecmp("man", name, len) ||
195 !strncasecmp("woman", name, len) ||
196 !strncasecmp("konqueror", name, len));
197}
198
199static void do_add_man_viewer_info(const char *name,
200 size_t len,
201 const char *value)
202{
203 struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
204
205 strncpy(new->name, name, len);
206 new->info = xstrdup(value);
207 new->next = man_viewer_info_list;
208 man_viewer_info_list = new;
209}
210
211static int add_man_viewer_path(const char *name,
212 size_t len,
213 const char *value)
214{
215 if (supported_man_viewer(name, len))
216 do_add_man_viewer_info(name, len, value);
217 else
9665627d
NTND
218 warning(_("'%s': path for unsupported man viewer.\n"
219 "Please consider using 'man.<tool>.cmd' instead."),
3d78d1f1
MV
220 name);
221
222 return 0;
223}
224
225static int add_man_viewer_cmd(const char *name,
226 size_t len,
227 const char *value)
228{
229 if (supported_man_viewer(name, len))
9665627d
NTND
230 warning(_("'%s': cmd for supported man viewer.\n"
231 "Please consider using 'man.<tool>.path' instead."),
3d78d1f1
MV
232 name);
233 else
234 do_add_man_viewer_info(name, len, value);
235
236 return 0;
237}
238
239static int add_man_viewer_info(const char *var, const char *value)
240{
4d5c6cef
JK
241 const char *name, *subkey;
242 int namelen;
3d78d1f1 243
4d5c6cef 244 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
178b513e 245 return 0;
3d78d1f1 246
4d5c6cef 247 if (!strcmp(subkey, "path")) {
3d78d1f1
MV
248 if (!value)
249 return config_error_nonbool(var);
4d5c6cef 250 return add_man_viewer_path(name, namelen, value);
3d78d1f1 251 }
4d5c6cef 252 if (!strcmp(subkey, "cmd")) {
3d78d1f1
MV
253 if (!value)
254 return config_error_nonbool(var);
4d5c6cef 255 return add_man_viewer_cmd(name, namelen, value);
3d78d1f1
MV
256 }
257
3d78d1f1
MV
258 return 0;
259}
260
261static int git_help_config(const char *var, const char *value, void *cb)
262{
dbfae689
NTND
263 if (!prefixcmp(var, "column."))
264 return git_column_config(var, value, "help", &colopts);
3d78d1f1
MV
265 if (!strcmp(var, "help.format")) {
266 if (!value)
267 return config_error_nonbool(var);
268 help_format = parse_help_format(value);
269 return 0;
270 }
89a852ef
CW
271 if (!strcmp(var, "help.htmlpath")) {
272 if (!value)
273 return config_error_nonbool(var);
274 html_path = xstrdup(value);
275 return 0;
276 }
3d78d1f1
MV
277 if (!strcmp(var, "man.viewer")) {
278 if (!value)
279 return config_error_nonbool(var);
280 add_man_viewer(value);
281 return 0;
282 }
283 if (!prefixcmp(var, "man."))
284 return add_man_viewer_info(var, value);
285
286 return git_default_config(var, value, cb);
287}
288
c7371e99 289static struct cmdnames main_cmds, other_cmds;
3d78d1f1 290
3d78d1f1
MV
291static int is_git_command(const char *s)
292{
293 return is_in_cmdlist(&main_cmds, s) ||
294 is_in_cmdlist(&other_cmds, s);
295}
296
297static const char *prepend(const char *prefix, const char *cmd)
298{
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);
304 return p;
305}
306
307static const char *cmd_to_page(const char *git_cmd)
308{
309 if (!git_cmd)
310 return "git";
311 else if (!prefixcmp(git_cmd, "git"))
312 return git_cmd;
313 else if (is_git_command(git_cmd))
314 return prepend("git-", git_cmd);
315 else
316 return prepend("git", git_cmd);
317}
318
319static void setup_man_path(void)
320{
f285a2d7 321 struct strbuf new_path = STRBUF_INIT;
3d78d1f1
MV
322 const char *old_path = getenv("MANPATH");
323
3d78d1f1
MV
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. */
026fa0d5 328 strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
3d78d1f1
MV
329 strbuf_addch(&new_path, ':');
330 if (old_path)
331 strbuf_addstr(&new_path, old_path);
332
333 setenv("MANPATH", new_path.buf, 1);
334
335 strbuf_release(&new_path);
336}
337
338static void exec_viewer(const char *name, const char *page)
339{
340 const char *info = get_man_viewer_info(name);
341
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);
348 else if (info)
349 exec_man_cmd(info, page);
350 else
9665627d 351 warning(_("'%s': unknown man viewer."), name);
3d78d1f1
MV
352}
353
354static void show_man_page(const char *git_cmd)
355{
356 struct man_viewer_list *viewer;
357 const char *page = cmd_to_page(git_cmd);
5059a427 358 const char *fallback = getenv("GIT_MAN_VIEWER");
3d78d1f1
MV
359
360 setup_man_path();
361 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
362 {
363 exec_viewer(viewer->name, page); /* will return when unable */
364 }
5059a427
RF
365 if (fallback)
366 exec_viewer(fallback, page);
3d78d1f1 367 exec_viewer("man", page);
9665627d 368 die(_("no man viewer handled the request"));
3d78d1f1
MV
369}
370
371static void show_info_page(const char *git_cmd)
372{
373 const char *page = cmd_to_page(git_cmd);
026fa0d5 374 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
5d314759 375 execlp("info", "info", "gitman", page, (char *)NULL);
9665627d 376 die(_("no info viewer handled the request"));
3d78d1f1
MV
377}
378
379static void get_html_page_path(struct strbuf *page_path, const char *page)
380{
381 struct stat st;
89a852ef
CW
382 if (!html_path)
383 html_path = system_path(GIT_HTML_PATH);
3d78d1f1
MV
384
385 /* Check that we have a git documentation directory. */
86272b4f
CW
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);
390 }
3d78d1f1
MV
391
392 strbuf_init(page_path, 0);
393 strbuf_addf(page_path, "%s/%s.html", html_path, page);
394}
395
396/*
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
399 * HTML.
400 */
401#ifndef open_html
2af202be 402static void open_html(const char *path)
3d78d1f1 403{
5d314759 404 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
3d78d1f1
MV
405}
406#endif
407
408static void show_html_page(const char *git_cmd)
409{
410 const char *page = cmd_to_page(git_cmd);
411 struct strbuf page_path; /* it leaks but we exec bellow */
412
413 get_html_page_path(&page_path, page);
414
415 open_html(page_path.buf);
416}
417
418int cmd_help(int argc, const char **argv, const char *prefix)
419{
420 int nongit;
421 const char *alias;
3caa8239 422 enum help_format parsed_help_format;
e321180e 423 load_command_list("git-", &main_cmds, &other_cmds);
3d78d1f1 424
37782920 425 argc = parse_options(argc, argv, prefix, builtin_help_options,
3d78d1f1 426 builtin_help_usage, 0);
3caa8239 427 parsed_help_format = help_format;
3d78d1f1
MV
428
429 if (show_all) {
dbfae689 430 git_config(git_help_config, NULL);
9665627d 431 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
f4ed0af6 432 list_commands(colopts, &main_cmds, &other_cmds);
15f7d494
PO
433 }
434
65f98358
PO
435 if (show_guides) {
436 /* do action - next patch */
437 }
438
439 if (show_all || show_guides) {
9665627d 440 printf("%s\n", _(git_more_info_string));
15f7d494
PO
441 /*
442 * We're done. Ignore any remaining args
443 */
3d78d1f1
MV
444 return 0;
445 }
446
447 if (!argv[0]) {
9665627d 448 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
3d78d1f1 449 list_common_cmds_help();
9665627d 450 printf("\n%s\n", _(git_more_info_string));
3d78d1f1
MV
451 return 0;
452 }
453
af6fbf9f
DA
454 setup_git_directory_gently(&nongit);
455 git_config(git_help_config, NULL);
456
d0408c0c 457 if (parsed_help_format != HELP_FORMAT_NONE)
3caa8239 458 help_format = parsed_help_format;
d0408c0c
PT
459 if (help_format == HELP_FORMAT_NONE)
460 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
3caa8239 461
3d78d1f1
MV
462 alias = alias_lookup(argv[0]);
463 if (alias && !is_git_command(argv[0])) {
9665627d 464 printf_ln(_("`git %s' is aliased to `%s'"), argv[0], alias);
3d78d1f1
MV
465 return 0;
466 }
467
468 switch (help_format) {
3caa8239 469 case HELP_FORMAT_NONE:
3d78d1f1
MV
470 case HELP_FORMAT_MAN:
471 show_man_page(argv[0]);
472 break;
473 case HELP_FORMAT_INFO:
474 show_info_page(argv[0]);
475 break;
476 case HELP_FORMAT_WEB:
477 show_html_page(argv[0]);
478 break;
479 }
480
481 return 0;
482}