format-patch: tie file-opening logic to output_directory
[git] / builtin / help.c
1 /*
2  * Builtin help command
3  */
4 #include "cache.h"
5 #include "config.h"
6 #include "builtin.h"
7 #include "exec-cmd.h"
8 #include "parse-options.h"
9 #include "run-command.h"
10 #include "column.h"
11 #include "config-list.h"
12 #include "help.h"
13 #include "alias.h"
14
15 #ifndef DEFAULT_HELP_FORMAT
16 #define DEFAULT_HELP_FORMAT "man"
17 #endif
18
19 static struct man_viewer_list {
20         struct man_viewer_list *next;
21         char name[FLEX_ARRAY];
22 } *man_viewer_list;
23
24 static 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
30 enum help_format {
31         HELP_FORMAT_NONE,
32         HELP_FORMAT_MAN,
33         HELP_FORMAT_INFO,
34         HELP_FORMAT_WEB
35 };
36
37 static const char *html_path;
38
39 static int show_all = 0;
40 static int show_guides = 0;
41 static int show_config;
42 static int verbose = 1;
43 static unsigned int colopts;
44 static enum help_format help_format = HELP_FORMAT_NONE;
45 static int exclude_guides;
46 static struct option builtin_help_options[] = {
47         OPT_BOOL('a', "all", &show_all, N_("print all available commands")),
48         OPT_HIDDEN_BOOL(0, "exclude-guides", &exclude_guides, N_("exclude guides")),
49         OPT_BOOL('g', "guides", &show_guides, N_("print list of useful guides")),
50         OPT_BOOL('c', "config", &show_config, N_("print all configuration variable names")),
51         OPT_SET_INT_F(0, "config-for-completion", &show_config, "", 2, PARSE_OPT_HIDDEN),
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"),
54                         HELP_FORMAT_WEB),
55         OPT_SET_INT('i', "info", &help_format, N_("show info page"),
56                         HELP_FORMAT_INFO),
57         OPT__VERBOSE(&verbose, N_("print command description")),
58         OPT_END(),
59 };
60
61 static const char * const builtin_help_usage[] = {
62         N_("git help [--all] [--guides] [--man | --web | --info] [<command>]"),
63         NULL
64 };
65
66 struct 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
73 static 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
151 static 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;
159         /*
160          * Please update _git_config() in git-completion.bash when you
161          * add new help formats.
162          */
163         die(_("unrecognized help format '%s'"), format);
164 }
165
166 static 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
178 static int check_emacsclient_version(void)
179 {
180         struct strbuf buffer = STRBUF_INIT;
181         struct child_process ec_process = CHILD_PROCESS_INIT;
182         const char *argv_ec[] = { "emacsclient", "--version", NULL };
183         int version;
184
185         /* emacsclient prints its version number on stderr */
186         ec_process.argv = argv_ec;
187         ec_process.err = -1;
188         ec_process.stdout_to_stderr = 1;
189         if (start_command(&ec_process))
190                 return error(_("Failed to start emacsclient."));
191
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
201         if (!starts_with(buffer.buf, "emacsclient")) {
202                 strbuf_release(&buffer);
203                 return error(_("Failed to parse emacsclient version."));
204         }
205
206         strbuf_remove(&buffer, 0, strlen("emacsclient"));
207         version = atoi(buffer.buf);
208
209         if (version < 22) {
210                 strbuf_release(&buffer);
211                 return error(_("emacsclient version '%d' too old (< 22)."),
212                         version);
213         }
214
215         strbuf_release(&buffer);
216         return 0;
217 }
218
219 static void exec_woman_emacs(const char *path, const char *page)
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);
228                 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
229                 warning_errno(_("failed to exec '%s'"), path);
230                 strbuf_release(&man_page);
231         }
232 }
233
234 static void exec_man_konqueror(const char *path, const char *page)
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) {
243                         size_t len;
244                         if (strip_suffix(path, "/konqueror", &len))
245                                 path = xstrfmt("%.*s/kfmclient", (int)len, path);
246                         filename = basename((char *)path);
247                 } else
248                         path = "kfmclient";
249                 strbuf_addf(&man_page, "man:%s(1)", page);
250                 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
251                 warning_errno(_("failed to exec '%s'"), path);
252                 strbuf_release(&man_page);
253         }
254 }
255
256 static void exec_man_man(const char *path, const char *page)
257 {
258         if (!path)
259                 path = "man";
260         execlp(path, "man", page, (char *)NULL);
261         warning_errno(_("failed to exec '%s'"), path);
262 }
263
264 static 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);
268         execl(SHELL_PATH, SHELL_PATH, "-c", shell_cmd.buf, (char *)NULL);
269         warning(_("failed to exec '%s'"), cmd);
270         strbuf_release(&shell_cmd);
271 }
272
273 static void add_man_viewer(const char *name)
274 {
275         struct man_viewer_list **p = &man_viewer_list;
276
277         while (*p)
278                 p = &((*p)->next);
279         FLEX_ALLOC_STR(*p, name, name);
280 }
281
282 static 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
289 static void do_add_man_viewer_info(const char *name,
290                                    size_t len,
291                                    const char *value)
292 {
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;
298 }
299
300 static 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
307                 warning(_("'%s': path for unsupported man viewer.\n"
308                           "Please consider using 'man.<tool>.cmd' instead."),
309                         name);
310
311         return 0;
312 }
313
314 static int add_man_viewer_cmd(const char *name,
315                               size_t len,
316                               const char *value)
317 {
318         if (supported_man_viewer(name, len))
319                 warning(_("'%s': cmd for supported man viewer.\n"
320                           "Please consider using 'man.<tool>.path' instead."),
321                         name);
322         else
323                 do_add_man_viewer_info(name, len, value);
324
325         return 0;
326 }
327
328 static int add_man_viewer_info(const char *var, const char *value)
329 {
330         const char *name, *subkey;
331         size_t namelen;
332
333         if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
334                 return 0;
335
336         if (!strcmp(subkey, "path")) {
337                 if (!value)
338                         return config_error_nonbool(var);
339                 return add_man_viewer_path(name, namelen, value);
340         }
341         if (!strcmp(subkey, "cmd")) {
342                 if (!value)
343                         return config_error_nonbool(var);
344                 return add_man_viewer_cmd(name, namelen, value);
345         }
346
347         return 0;
348 }
349
350 static int git_help_config(const char *var, const char *value, void *cb)
351 {
352         if (starts_with(var, "column."))
353                 return git_column_config(var, value, "help", &colopts);
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         }
360         if (!strcmp(var, "help.htmlpath")) {
361                 if (!value)
362                         return config_error_nonbool(var);
363                 html_path = xstrdup(value);
364                 return 0;
365         }
366         if (!strcmp(var, "man.viewer")) {
367                 if (!value)
368                         return config_error_nonbool(var);
369                 add_man_viewer(value);
370                 return 0;
371         }
372         if (starts_with(var, "man."))
373                 return add_man_viewer_info(var, value);
374
375         return git_default_config(var, value, cb);
376 }
377
378 static struct cmdnames main_cmds, other_cmds;
379
380 static int is_git_command(const char *s)
381 {
382         if (is_builtin(s))
383                 return 1;
384
385         load_command_list("git-", &main_cmds, &other_cmds);
386         return is_in_cmdlist(&main_cmds, s) ||
387                 is_in_cmdlist(&other_cmds, s);
388 }
389
390 static const char *cmd_to_page(const char *git_cmd)
391 {
392         if (!git_cmd)
393                 return "git";
394         else if (starts_with(git_cmd, "git"))
395                 return git_cmd;
396         else if (is_git_command(git_cmd))
397                 return xstrfmt("git-%s", git_cmd);
398         else
399                 return xstrfmt("git%s", git_cmd);
400 }
401
402 static void setup_man_path(void)
403 {
404         struct strbuf new_path = STRBUF_INIT;
405         const char *old_path = getenv("MANPATH");
406         char *git_man_path = system_path(GIT_MAN_PATH);
407
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. */
412         strbuf_addstr(&new_path, git_man_path);
413         strbuf_addch(&new_path, ':');
414         if (old_path)
415                 strbuf_addstr(&new_path, old_path);
416
417         free(git_man_path);
418         setenv("MANPATH", new_path.buf, 1);
419
420         strbuf_release(&new_path);
421 }
422
423 static 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
436                 warning(_("'%s': unknown man viewer."), name);
437 }
438
439 static void show_man_page(const char *git_cmd)
440 {
441         struct man_viewer_list *viewer;
442         const char *page = cmd_to_page(git_cmd);
443         const char *fallback = getenv("GIT_MAN_VIEWER");
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         }
450         if (fallback)
451                 exec_viewer(fallback, page);
452         exec_viewer("man", page);
453         die(_("no man viewer handled the request"));
454 }
455
456 static void show_info_page(const char *git_cmd)
457 {
458         const char *page = cmd_to_page(git_cmd);
459         setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
460         execlp("info", "info", "gitman", page, (char *)NULL);
461         die(_("no info viewer handled the request"));
462 }
463
464 static void get_html_page_path(struct strbuf *page_path, const char *page)
465 {
466         struct stat st;
467         char *to_free = NULL;
468
469         if (!html_path)
470                 html_path = to_free = system_path(GIT_HTML_PATH);
471
472         /* Check that we have a git documentation directory. */
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         }
478
479         strbuf_init(page_path, 0);
480         strbuf_addf(page_path, "%s/%s.html", html_path, page);
481         free(to_free);
482 }
483
484 static void open_html(const char *path)
485 {
486         execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
487 }
488
489 static 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
499 static 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) {
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;
539         }
540
541         if (exclude_guides)
542                 return help_unknown_cmd(cmd);
543
544         return cmd;
545 }
546
547 int cmd_help(int argc, const char **argv, const char *prefix)
548 {
549         int nongit;
550         enum help_format parsed_help_format;
551
552         argc = parse_options(argc, argv, prefix, builtin_help_options,
553                         builtin_help_usage, 0);
554         parsed_help_format = help_format;
555
556         if (show_all) {
557                 git_config(git_help_config, NULL);
558                 if (verbose) {
559                         setup_pager();
560                         list_all_cmds_help();
561                         return 0;
562                 }
563                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
564                 load_command_list("git-", &main_cmds, &other_cmds);
565                 list_commands(colopts, &main_cmds, &other_cmds);
566         }
567
568         if (show_config) {
569                 int for_human = show_config == 1;
570
571                 if (!for_human) {
572                         list_config_help(for_human);
573                         return 0;
574                 }
575                 setup_pager();
576                 list_config_help(for_human);
577                 printf("\n%s\n", _("'git help config' for more information"));
578                 return 0;
579         }
580
581         if (show_guides)
582                 list_guides_help();
583
584         if (show_all || show_guides) {
585                 printf("%s\n", _(git_more_info_string));
586                 /*
587                 * We're done. Ignore any remaining args
588                 */
589                 return 0;
590         }
591
592         if (!argv[0]) {
593                 printf(_("usage: %s%s"), _(git_usage_string), "\n\n");
594                 list_common_cmds_help();
595                 printf("\n%s\n", _(git_more_info_string));
596                 return 0;
597         }
598
599         setup_git_directory_gently(&nongit);
600         git_config(git_help_config, NULL);
601
602         if (parsed_help_format != HELP_FORMAT_NONE)
603                 help_format = parsed_help_format;
604         if (help_format == HELP_FORMAT_NONE)
605                 help_format = parse_help_format(DEFAULT_HELP_FORMAT);
606
607         argv[0] = check_git_cmd(argv[0]);
608
609         switch (help_format) {
610         case HELP_FORMAT_NONE:
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 }