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