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