]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/help.c
Merge branch 'ea/blame-use-oideq'
[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"
709df95b 11#include "config-list.h"
3d78d1f1 12#include "help.h"
65b5f948 13#include "alias.h"
3d78d1f1 14
1cc8af04
VR
15#ifndef DEFAULT_HELP_FORMAT
16#define DEFAULT_HELP_FORMAT "man"
17#endif
18
3d78d1f1
MV
19static struct man_viewer_list {
20 struct man_viewer_list *next;
21 char name[FLEX_ARRAY];
22} *man_viewer_list;
23
24static struct man_viewer_info_list {
25 struct man_viewer_info_list *next;
26 const char *info;
27 char name[FLEX_ARRAY];
28} *man_viewer_info_list;
29
30enum help_format {
3caa8239 31 HELP_FORMAT_NONE,
3d78d1f1
MV
32 HELP_FORMAT_MAN,
33 HELP_FORMAT_INFO,
4b05548f 34 HELP_FORMAT_WEB
3d78d1f1
MV
35};
36
89a852ef
CW
37static const char *html_path;
38
3d78d1f1 39static int show_all = 0;
65f98358 40static int show_guides = 0;
3ac68a93 41static int show_config;
26c7d067 42static int verbose = 1;
dbfae689 43static unsigned int colopts;
3caa8239 44static enum help_format help_format = HELP_FORMAT_NONE;
af74128f 45static int exclude_guides;
3d78d1f1 46static struct option builtin_help_options[] = {
15f7d494 47 OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
af74128f 48 OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
65f98358 49 OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
3ac68a93 50 OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
e17ca926 51 OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
68918696
NTND
52 OPT_SET_INT('m', "man", &help_format, N_("show man page"), HELP_FORMAT_MAN),
53 OPT_SET_INT('w', "web", &help_format, N_("show manual in web browser"),
3d78d1f1 54 HELP_FORMAT_WEB),
68918696 55 OPT_SET_INT('i', "info", &help_format, N_("show info page"),
3d78d1f1 56 HELP_FORMAT_INFO),
63eae83f 57 OPT__VERBOSE(&verbose, N_("print command description")),
3d78d1f1
MV
58 OPT_END(),
59};
60
61static const char * const builtin_help_usage[] = {
9c9b4f2f 62 N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
3d78d1f1
MV
63 NULL
64};
65
709df95b
ES
66struct slot_expansion {
67 const char *prefix;
68 const char *placeholder;
69 void (*fn)(struct string_list *list, const char *prefix);
70 int found;
71};
72
73static void list_config_help(int for_human)
74{
75 struct slot_expansion slot_expansions[] = {
76 { "advice", "*", list_config_advices },
77 { "color.branch", "<slot>", list_config_color_branch_slots },
78 { "color.decorate", "<slot>", list_config_color_decorate_slots },
79 { "color.diff", "<slot>", list_config_color_diff_slots },
80 { "color.grep", "<slot>", list_config_color_grep_slots },
81 { "color.interactive", "<slot>", list_config_color_interactive_slots },
82 { "color.remote", "<slot>", list_config_color_sideband_slots },
83 { "color.status", "<slot>", list_config_color_status_slots },
84 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
85 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
86 { NULL, NULL, NULL }
87 };
88 const char **p;
89 struct slot_expansion *e;
90 struct string_list keys = STRING_LIST_INIT_DUP;
91 int i;
92
93 for (p = config_name_list; *p; p++) {
94 const char *var = *p;
95 struct strbuf sb = STRBUF_INIT;
96
97 for (e = slot_expansions; e->prefix; e++) {
98
99 strbuf_reset(&sb);
100 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
101 if (!strcasecmp(var, sb.buf)) {
102 e->fn(&keys, e->prefix);
103 e->found++;
104 break;
105 }
106 }
107 strbuf_release(&sb);
108 if (!e->prefix)
109 string_list_append(&keys, var);
110 }
111
112 for (e = slot_expansions; e->prefix; e++)
113 if (!e->found)
114 BUG("slot_expansion %s.%s is not used",
115 e->prefix, e->placeholder);
116
117 string_list_sort(&keys);
118 for (i = 0; i < keys.nr; i++) {
119 const char *var = keys.items[i].string;
120 const char *wildcard, *tag, *cut;
121
122 if (for_human) {
123 puts(var);
124 continue;
125 }
126
127 wildcard = strchr(var, '*');
128 tag = strchr(var, '<');
129
130 if (!wildcard && !tag) {
131 puts(var);
132 continue;
133 }
134
135 if (wildcard && !tag)
136 cut = wildcard;
137 else if (!wildcard && tag)
138 cut = tag;
139 else
140 cut = wildcard < tag ? wildcard : tag;
141
142 /*
143 * We may produce duplicates, but that's up to
144 * git-completion.bash to handle
145 */
146 printf("%.*s\n", (int)(cut - var), var);
147 }
148 string_list_clear(&keys, 0);
149}
150
3d78d1f1
MV
151static enum help_format parse_help_format(const char *format)
152{
153 if (!strcmp(format, "man"))
154 return HELP_FORMAT_MAN;
155 if (!strcmp(format, "info"))
156 return HELP_FORMAT_INFO;
157 if (!strcmp(format, "web") || !strcmp(format, "html"))
158 return HELP_FORMAT_WEB;
5a59a230
NTND
159 /*
160 * Please update _git_config() in git-completion.bash when you
161 * add new help formats.
162 */
9665627d 163 die(_("unrecognized help format '%s'"), format);
3d78d1f1
MV
164}
165
166static const char *get_man_viewer_info(const char *name)
167{
168 struct man_viewer_info_list *viewer;
169
170 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
171 {
172 if (!strcasecmp(name, viewer->name))
173 return viewer->info;
174 }
175 return NULL;
176}
177
178static int check_emacsclient_version(void)
179{
180 struct strbuf buffer = STRBUF_INIT;
d3180279 181 struct child_process ec_process = CHILD_PROCESS_INIT;
3d78d1f1
MV
182 const char *argv_ec[] = { "emacsclient", "--version", NULL };
183 int version;
184
185 /* emacsclient prints its version number on stderr */
3d78d1f1
MV
186 ec_process.argv = argv_ec;
187 ec_process.err = -1;
188 ec_process.stdout_to_stderr = 1;
4e2715fb 189 if (start_command(&ec_process))
9665627d 190 return error(_("Failed to start emacsclient."));
4e2715fb 191
3d78d1f1
MV
192 strbuf_read(&buffer, ec_process.err, 20);
193 close(ec_process.err);
194
195 /*
196 * Don't bother checking return value, because "emacsclient --version"
197 * seems to always exits with code 1.
198 */
199 finish_command(&ec_process);
200
59556548 201 if (!starts_with(buffer.buf, "emacsclient")) {
3d78d1f1 202 strbuf_release(&buffer);
9665627d 203 return error(_("Failed to parse emacsclient version."));
3d78d1f1
MV
204 }
205
206 strbuf_remove(&buffer, 0, strlen("emacsclient"));
207 version = atoi(buffer.buf);
208
209 if (version < 22) {
3d78d1f1 210 strbuf_release(&buffer);
9665627d 211 return error(_("emacsclient version '%d' too old (< 22)."),
4e2715fb 212 version);
3d78d1f1
MV
213 }
214
215 strbuf_release(&buffer);
216 return 0;
217}
218
4b25d091 219static void exec_woman_emacs(const char *path, const char *page)
3d78d1f1
MV
220{
221 if (!check_emacsclient_version()) {
222 /* This works only with emacsclient version >= 22. */
223 struct strbuf man_page = STRBUF_INIT;
224
225 if (!path)
226 path = "emacsclient";
227 strbuf_addf(&man_page, "(woman \"%s\")", page);
5d314759 228 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
57477498 229 warning_errno(_("failed to exec '%s'"), path);
bad0e2c6 230 strbuf_release(&man_page);
3d78d1f1
MV
231 }
232}
233
4b25d091 234static void exec_man_konqueror(const char *path, const char *page)
3d78d1f1
MV
235{
236 const char *display = getenv("DISPLAY");
237 if (display && *display) {
238 struct strbuf man_page = STRBUF_INIT;
239 const char *filename = "kfmclient";
240
241 /* It's simpler to launch konqueror using kfmclient. */
242 if (path) {
4c9ac3bf
JK
243 size_t len;
244 if (strip_suffix(path, "/konqueror", &len))
245 path = xstrfmt("%.*s/kfmclient", (int)len, path);
246 filename = basename((char *)path);
3d78d1f1
MV
247 } else
248 path = "kfmclient";
249 strbuf_addf(&man_page, "man:%s(1)", page);
5d314759 250 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
57477498 251 warning_errno(_("failed to exec '%s'"), path);
a981a9f0 252 strbuf_release(&man_page);
3d78d1f1
MV
253 }
254}
255
4b25d091 256static void exec_man_man(const char *path, const char *page)
3d78d1f1
MV
257{
258 if (!path)
259 path = "man";
5d314759 260 execlp(path, "man", page, (char *)NULL);
57477498 261 warning_errno(_("failed to exec '%s'"), path);
3d78d1f1
MV
262}
263
264static void exec_man_cmd(const char *cmd, const char *page)
265{
266 struct strbuf shell_cmd = STRBUF_INIT;
267 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
b680a86a 268 execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
57477498 269 warning(_("failed to exec '%s'"), cmd);
72462186 270 strbuf_release(&shell_cmd);
3d78d1f1
MV
271}
272
273static void add_man_viewer(const char *name)
274{
275 struct man_viewer_list **p = &man_viewer_list;
3d78d1f1
MV
276
277 while (*p)
278 p = &((*p)->next);
96ffc06f 279 FLEX_ALLOC_STR(*p, name, name);
3d78d1f1
MV
280}
281
282static int supported_man_viewer(const char *name, size_t len)
283{
284 return (!strncasecmp("man", name, len) ||
285 !strncasecmp("woman", name, len) ||
286 !strncasecmp("konqueror", name, len));
287}
288
289static void do_add_man_viewer_info(const char *name,
290 size_t len,
291 const char *value)
292{
8adda462
BW
293 struct man_viewer_info_list *new_man_viewer;
294 FLEX_ALLOC_MEM(new_man_viewer, name, name, len);
295 new_man_viewer->info = xstrdup(value);
296 new_man_viewer->next = man_viewer_info_list;
297 man_viewer_info_list = new_man_viewer;
3d78d1f1
MV
298}
299
300static int add_man_viewer_path(const char *name,
301 size_t len,
302 const char *value)
303{
304 if (supported_man_viewer(name, len))
305 do_add_man_viewer_info(name, len, value);
306 else
9665627d
NTND
307 warning(_("'%s': path for unsupported man viewer.\n"
308 "Please consider using 'man.<tool>.cmd' instead."),
3d78d1f1
MV
309 name);
310
311 return 0;
312}
313
314static int add_man_viewer_cmd(const char *name,
315 size_t len,
316 const char *value)
317{
318 if (supported_man_viewer(name, len))
9665627d
NTND
319 warning(_("'%s': cmd for supported man viewer.\n"
320 "Please consider using 'man.<tool>.path' instead."),
3d78d1f1
MV
321 name);
322 else
323 do_add_man_viewer_info(name, len, value);
324
325 return 0;
326}
327
328static int add_man_viewer_info(const char *var, const char *value)
329{
4d5c6cef 330 const char *name, *subkey;
f5914f4b 331 size_t namelen;
3d78d1f1 332
4d5c6cef 333 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
178b513e 334 return 0;
3d78d1f1 335
4d5c6cef 336 if (!strcmp(subkey, "path")) {
3d78d1f1
MV
337 if (!value)
338 return config_error_nonbool(var);
4d5c6cef 339 return add_man_viewer_path(name, namelen, value);
3d78d1f1 340 }
4d5c6cef 341 if (!strcmp(subkey, "cmd")) {
3d78d1f1
MV
342 if (!value)
343 return config_error_nonbool(var);
4d5c6cef 344 return add_man_viewer_cmd(name, namelen, value);
3d78d1f1
MV
345 }
346
3d78d1f1
MV
347 return 0;
348}
349
350static int git_help_config(const char *var, const char *value, void *cb)
351{
59556548 352 if (starts_with(var, "column."))
dbfae689 353 return git_column_config(var, value, "help", &colopts);
3d78d1f1
MV
354 if (!strcmp(var, "help.format")) {
355 if (!value)
356 return config_error_nonbool(var);
357 help_format = parse_help_format(value);
358 return 0;
359 }
89a852ef
CW
360 if (!strcmp(var, "help.htmlpath")) {
361 if (!value)
362 return config_error_nonbool(var);
363 html_path = xstrdup(value);
364 return 0;
365 }
3d78d1f1
MV
366 if (!strcmp(var, "man.viewer")) {
367 if (!value)
368 return config_error_nonbool(var);
369 add_man_viewer(value);
370 return 0;
371 }
59556548 372 if (starts_with(var, "man."))
3d78d1f1
MV
373 return add_man_viewer_info(var, value);
374
375 return git_default_config(var, value, cb);
376}
377
c7371e99 378static struct cmdnames main_cmds, other_cmds;
3d78d1f1 379
3d78d1f1
MV
380static int is_git_command(const char *s)
381{
c6127fa3
SS
382 if (is_builtin(s))
383 return 1;
384
a3c52634 385 load_command_list("git-", &main_cmds, &other_cmds);
3d78d1f1
MV
386 return is_in_cmdlist(&main_cmds, s) ||
387 is_in_cmdlist(&other_cmds, s);
388}
389
3d78d1f1
MV
390static const char *cmd_to_page(const char *git_cmd)
391{
392 if (!git_cmd)
393 return "git";
59556548 394 else if (starts_with(git_cmd, "git"))
3d78d1f1
MV
395 return git_cmd;
396 else if (is_git_command(git_cmd))
acd47eec 397 return xstrfmt("git-%s", git_cmd);
3d78d1f1 398 else
acd47eec 399 return xstrfmt("git%s", git_cmd);
3d78d1f1
MV
400}
401
402static void setup_man_path(void)
403{
f285a2d7 404 struct strbuf new_path = STRBUF_INIT;
3d78d1f1 405 const char *old_path = getenv("MANPATH");
59362e56 406 char *git_man_path = system_path(GIT_MAN_PATH);
3d78d1f1 407
3d78d1f1
MV
408 /* We should always put ':' after our path. If there is no
409 * old_path, the ':' at the end will let 'man' to try
410 * system-wide paths after ours to find the manual page. If
411 * there is old_path, we need ':' as delimiter. */
59362e56 412 strbuf_addstr(&new_path, git_man_path);
3d78d1f1
MV
413 strbuf_addch(&new_path, ':');
414 if (old_path)
415 strbuf_addstr(&new_path, old_path);
416
59362e56 417 free(git_man_path);
3d78d1f1
MV
418 setenv("MANPATH", new_path.buf, 1);
419
420 strbuf_release(&new_path);
421}
422
423static void exec_viewer(const char *name, const char *page)
424{
425 const char *info = get_man_viewer_info(name);
426
427 if (!strcasecmp(name, "man"))
428 exec_man_man(info, page);
429 else if (!strcasecmp(name, "woman"))
430 exec_woman_emacs(info, page);
431 else if (!strcasecmp(name, "konqueror"))
432 exec_man_konqueror(info, page);
433 else if (info)
434 exec_man_cmd(info, page);
435 else
9665627d 436 warning(_("'%s': unknown man viewer."), name);
3d78d1f1
MV
437}
438
439static void show_man_page(const char *git_cmd)
440{
441 struct man_viewer_list *viewer;
442 const char *page = cmd_to_page(git_cmd);
5059a427 443 const char *fallback = getenv("GIT_MAN_VIEWER");
3d78d1f1
MV
444
445 setup_man_path();
446 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
447 {
448 exec_viewer(viewer->name, page); /* will return when unable */
449 }
5059a427
RF
450 if (fallback)
451 exec_viewer(fallback, page);
3d78d1f1 452 exec_viewer("man", page);
9665627d 453 die(_("no man viewer handled the request"));
3d78d1f1
MV
454}
455
456static void show_info_page(const char *git_cmd)
457{
458 const char *page = cmd_to_page(git_cmd);
026fa0d5 459 setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
5d314759 460 execlp("info", "info", "gitman", page, (char *)NULL);
9665627d 461 die(_("no info viewer handled the request"));
3d78d1f1
MV
462}
463
464static void get_html_page_path(struct strbuf *page_path, const char *page)
465{
466 struct stat st;
59362e56
JH
467 char *to_free = NULL;
468
89a852ef 469 if (!html_path)
59362e56 470 html_path = to_free = system_path(GIT_HTML_PATH);
3d78d1f1
MV
471
472 /* Check that we have a git documentation directory. */
86272b4f
CW
473 if (!strstr(html_path, "://")) {
474 if (stat(mkpath("%s/git.html", html_path), &st)
475 || !S_ISREG(st.st_mode))
476 die("'%s': not a documentation directory.", html_path);
477 }
3d78d1f1
MV
478
479 strbuf_init(page_path, 0);
480 strbuf_addf(page_path, "%s/%s.html", html_path, page);
59362e56 481 free(to_free);
3d78d1f1
MV
482}
483
2af202be 484static void open_html(const char *path)
3d78d1f1 485{
5d314759 486 execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
3d78d1f1 487}
3d78d1f1
MV
488
489static void show_html_page(const char *git_cmd)
490{
491 const char *page = cmd_to_page(git_cmd);
492 struct strbuf page_path; /* it leaks but we exec bellow */
493
494 get_html_page_path(&page_path, page);
495
496 open_html(page_path.buf);
497}
498
af74128f
RT
499static const char *check_git_cmd(const char* cmd)
500{
501 char *alias;
502
503 if (is_git_command(cmd))
504 return cmd;
505
506 alias = alias_lookup(cmd);
507 if (alias) {
e6e76baa
RV
508 const char **argv;
509 int count;
510
511 /*
512 * handle_builtin() in git.c rewrites "git cmd --help"
513 * to "git help --exclude-guides cmd", so we can use
514 * exclude_guides to distinguish "git cmd --help" from
515 * "git help cmd". In the latter case, or if cmd is an
516 * alias for a shell command, just print the alias
517 * definition.
518 */
519 if (!exclude_guides || alias[0] == '!') {
520 printf_ln(_("'%s' is aliased to '%s'"), cmd, alias);
521 free(alias);
522 exit(0);
523 }
524 /*
525 * Otherwise, we pretend that the command was "git
526 * word0 --help". We use split_cmdline() to get the
527 * first word of the alias, to ensure that we use the
528 * same rules as when the alias is actually
529 * used. split_cmdline() modifies alias in-place.
530 */
531 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), cmd, alias);
532 count = split_cmdline(alias, &argv);
533 if (count < 0)
534 die(_("bad alias.%s string: %s"), cmd,
535 split_cmdline_strerror(count));
536 free(argv);
537 UNLEAK(alias);
538 return alias;
af74128f
RT
539 }
540
541 if (exclude_guides)
542 return help_unknown_cmd(cmd);
543
544 return cmd;
545}
546
3d78d1f1
MV
547int cmd_help(int argc, const char **argv, const char *prefix)
548{
549 int nongit;
3caa8239 550 enum help_format parsed_help_format;
3d78d1f1 551
37782920 552 argc = parse_options(argc, argv, prefix, builtin_help_options,
3d78d1f1 553 builtin_help_usage, 0);
3caa8239 554 parsed_help_format = help_format;
3d78d1f1
MV
555
556 if (show_all) {
dbfae689 557 git_config(git_help_config, NULL);
63eae83f
NTND
558 if (verbose) {
559 setup_pager();
560 list_all_cmds_help();
561 return 0;
562 }
9665627d 563 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
a3c52634 564 load_command_list("git-", &main_cmds, &other_cmds);
f4ed0af6 565 list_commands(colopts, &main_cmds, &other_cmds);
15f7d494
PO
566 }
567
3ac68a93 568 if (show_config) {
e17ca926
NTND
569 int for_human = show_config == 1;
570
571 if (!for_human) {
572 list_config_help(for_human);
573 return 0;
574 }
3ac68a93 575 setup_pager();
e17ca926 576 list_config_help(for_human);
3ac68a93
NTND
577 printf("\n%s\n", _("'git help config' for more information"));
578 return 0;
579 }
580
002b726a 581 if (show_guides)
0371a764 582 list_guides_help();
65f98358
PO
583
584 if (show_all || show_guides) {
9665627d 585 printf("%s\n", _(git_more_info_string));
15f7d494
PO
586 /*
587 * We're done. Ignore any remaining args
588 */
3d78d1f1
MV
589 return 0;
590 }
591
592 if (!argv[0]) {
9665627d 593 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
3d78d1f1 594 list_common_cmds_help();
9665627d 595 printf("\n%s\n", _(git_more_info_string));
3d78d1f1
MV
596 return 0;
597 }
598
af6fbf9f
DA
599 setup_git_directory_gently(&nongit);
600 git_config(git_help_config, NULL);
601
d0408c0c 602 if (parsed_help_format != HELP_FORMAT_NONE)
3caa8239 603 help_format = parsed_help_format;
d0408c0c
PT
604 if (help_format == HELP_FORMAT_NONE)
605 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
3caa8239 606
af74128f 607 argv[0] = check_git_cmd(argv[0]);
3d78d1f1
MV
608
609 switch (help_format) {
3caa8239 610 case HELP_FORMAT_NONE:
3d78d1f1
MV
611 case HELP_FORMAT_MAN:
612 show_man_page(argv[0]);
613 break;
614 case HELP_FORMAT_INFO:
615 show_info_page(argv[0]);
616 break;
617 case HELP_FORMAT_WEB:
618 show_html_page(argv[0]);
619 break;
620 }
621
622 return 0;
623}