t1507: add tests to document @{upstream} behaviour
[git] / builtin / help.c
1 /*
2  * builtin-help.c
3  *
4  * Builtin help command
5  */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "common-cmds.h"
10 #include "parse-options.h"
11 #include "run-command.h"
12 #include "help.h"
13
14 static struct man_viewer_list {
15         struct man_viewer_list *next;
16         char name[FLEX_ARRAY];
17 } *man_viewer_list;
18
19 static struct man_viewer_info_list {
20         struct man_viewer_info_list *next;
21         const char *info;
22         char name[FLEX_ARRAY];
23 } *man_viewer_info_list;
24
25 enum help_format {
26         HELP_FORMAT_NONE,
27         HELP_FORMAT_MAN,
28         HELP_FORMAT_INFO,
29         HELP_FORMAT_WEB
30 };
31
32 static int show_all = 0;
33 static enum help_format help_format = HELP_FORMAT_NONE;
34 static struct option builtin_help_options[] = {
35         OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
36         OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
37         OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
38                         HELP_FORMAT_WEB),
39         OPT_SET_INT('i', "info", &help_format, "show info page",
40                         HELP_FORMAT_INFO),
41         OPT_END(),
42 };
43
44 static const char * const builtin_help_usage[] = {
45         "git help [--all] [--man|--web|--info] [command]",
46         NULL
47 };
48
49 static enum help_format parse_help_format(const char *format)
50 {
51         if (!strcmp(format, "man"))
52                 return HELP_FORMAT_MAN;
53         if (!strcmp(format, "info"))
54                 return HELP_FORMAT_INFO;
55         if (!strcmp(format, "web") || !strcmp(format, "html"))
56                 return HELP_FORMAT_WEB;
57         die("unrecognized help format '%s'", format);
58 }
59
60 static const char *get_man_viewer_info(const char *name)
61 {
62         struct man_viewer_info_list *viewer;
63
64         for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
65         {
66                 if (!strcasecmp(name, viewer->name))
67                         return viewer->info;
68         }
69         return NULL;
70 }
71
72 static int check_emacsclient_version(void)
73 {
74         struct strbuf buffer = STRBUF_INIT;
75         struct child_process ec_process;
76         const char *argv_ec[] = { "emacsclient", "--version", NULL };
77         int version;
78
79         /* emacsclient prints its version number on stderr */
80         memset(&ec_process, 0, sizeof(ec_process));
81         ec_process.argv = argv_ec;
82         ec_process.err = -1;
83         ec_process.stdout_to_stderr = 1;
84         if (start_command(&ec_process))
85                 return error("Failed to start emacsclient.");
86
87         strbuf_read(&buffer, ec_process.err, 20);
88         close(ec_process.err);
89
90         /*
91          * Don't bother checking return value, because "emacsclient --version"
92          * seems to always exits with code 1.
93          */
94         finish_command(&ec_process);
95
96         if (prefixcmp(buffer.buf, "emacsclient")) {
97                 strbuf_release(&buffer);
98                 return error("Failed to parse emacsclient version.");
99         }
100
101         strbuf_remove(&buffer, 0, strlen("emacsclient"));
102         version = atoi(buffer.buf);
103
104         if (version < 22) {
105                 strbuf_release(&buffer);
106                 return error("emacsclient version '%d' too old (< 22).",
107                         version);
108         }
109
110         strbuf_release(&buffer);
111         return 0;
112 }
113
114 static void exec_woman_emacs(const char *path, const char *page)
115 {
116         if (!check_emacsclient_version()) {
117                 /* This works only with emacsclient version >= 22. */
118                 struct strbuf man_page = STRBUF_INIT;
119
120                 if (!path)
121                         path = "emacsclient";
122                 strbuf_addf(&man_page, "(woman \"%s\")", page);
123                 execlp(path, "emacsclient", "-e", man_page.buf, (char *)NULL);
124                 warning("failed to exec '%s': %s", path, strerror(errno));
125         }
126 }
127
128 static void exec_man_konqueror(const char *path, const char *page)
129 {
130         const char *display = getenv("DISPLAY");
131         if (display && *display) {
132                 struct strbuf man_page = STRBUF_INIT;
133                 const char *filename = "kfmclient";
134
135                 /* It's simpler to launch konqueror using kfmclient. */
136                 if (path) {
137                         const char *file = strrchr(path, '/');
138                         if (file && !strcmp(file + 1, "konqueror")) {
139                                 char *new = xstrdup(path);
140                                 char *dest = strrchr(new, '/');
141
142                                 /* strlen("konqueror") == strlen("kfmclient") */
143                                 strcpy(dest + 1, "kfmclient");
144                                 path = new;
145                         }
146                         if (file)
147                                 filename = file;
148                 } else
149                         path = "kfmclient";
150                 strbuf_addf(&man_page, "man:%s(1)", page);
151                 execlp(path, filename, "newTab", man_page.buf, (char *)NULL);
152                 warning("failed to exec '%s': %s", path, strerror(errno));
153         }
154 }
155
156 static void exec_man_man(const char *path, const char *page)
157 {
158         if (!path)
159                 path = "man";
160         execlp(path, "man", page, (char *)NULL);
161         warning("failed to exec '%s': %s", path, strerror(errno));
162 }
163
164 static void exec_man_cmd(const char *cmd, const char *page)
165 {
166         struct strbuf shell_cmd = STRBUF_INIT;
167         strbuf_addf(&shell_cmd, "%s %s", cmd, page);
168         execl("/bin/sh", "sh", "-c", shell_cmd.buf, (char *)NULL);
169         warning("failed to exec '%s': %s", cmd, strerror(errno));
170 }
171
172 static void add_man_viewer(const char *name)
173 {
174         struct man_viewer_list **p = &man_viewer_list;
175         size_t len = strlen(name);
176
177         while (*p)
178                 p = &((*p)->next);
179         *p = xcalloc(1, (sizeof(**p) + len + 1));
180         strncpy((*p)->name, name, len);
181 }
182
183 static int supported_man_viewer(const char *name, size_t len)
184 {
185         return (!strncasecmp("man", name, len) ||
186                 !strncasecmp("woman", name, len) ||
187                 !strncasecmp("konqueror", name, len));
188 }
189
190 static void do_add_man_viewer_info(const char *name,
191                                    size_t len,
192                                    const char *value)
193 {
194         struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
195
196         strncpy(new->name, name, len);
197         new->info = xstrdup(value);
198         new->next = man_viewer_info_list;
199         man_viewer_info_list = new;
200 }
201
202 static int add_man_viewer_path(const char *name,
203                                size_t len,
204                                const char *value)
205 {
206         if (supported_man_viewer(name, len))
207                 do_add_man_viewer_info(name, len, value);
208         else
209                 warning("'%s': path for unsupported man viewer.\n"
210                         "Please consider using 'man.<tool>.cmd' instead.",
211                         name);
212
213         return 0;
214 }
215
216 static int add_man_viewer_cmd(const char *name,
217                               size_t len,
218                               const char *value)
219 {
220         if (supported_man_viewer(name, len))
221                 warning("'%s': cmd for supported man viewer.\n"
222                         "Please consider using 'man.<tool>.path' instead.",
223                         name);
224         else
225                 do_add_man_viewer_info(name, len, value);
226
227         return 0;
228 }
229
230 static int add_man_viewer_info(const char *var, const char *value)
231 {
232         const char *name = var + 4;
233         const char *subkey = strrchr(name, '.');
234
235         if (!subkey)
236                 return 0;
237
238         if (!strcmp(subkey, ".path")) {
239                 if (!value)
240                         return config_error_nonbool(var);
241                 return add_man_viewer_path(name, subkey - name, value);
242         }
243         if (!strcmp(subkey, ".cmd")) {
244                 if (!value)
245                         return config_error_nonbool(var);
246                 return add_man_viewer_cmd(name, subkey - name, value);
247         }
248
249         return 0;
250 }
251
252 static int git_help_config(const char *var, const char *value, void *cb)
253 {
254         if (!strcmp(var, "help.format")) {
255                 if (!value)
256                         return config_error_nonbool(var);
257                 help_format = parse_help_format(value);
258                 return 0;
259         }
260         if (!strcmp(var, "man.viewer")) {
261                 if (!value)
262                         return config_error_nonbool(var);
263                 add_man_viewer(value);
264                 return 0;
265         }
266         if (!prefixcmp(var, "man."))
267                 return add_man_viewer_info(var, value);
268
269         return git_default_config(var, value, cb);
270 }
271
272 static struct cmdnames main_cmds, other_cmds;
273
274 void list_common_cmds_help(void)
275 {
276         int i, longest = 0;
277
278         for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
279                 if (longest < strlen(common_cmds[i].name))
280                         longest = strlen(common_cmds[i].name);
281         }
282
283         puts("The most commonly used git commands are:");
284         for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
285                 printf("   %s   ", common_cmds[i].name);
286                 mput_char(' ', longest - strlen(common_cmds[i].name));
287                 puts(common_cmds[i].help);
288         }
289 }
290
291 static 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
297 static 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
307 static 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
319 static void setup_man_path(void)
320 {
321         struct strbuf new_path = STRBUF_INIT;
322         const char *old_path = getenv("MANPATH");
323
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. */
328         strbuf_addstr(&new_path, system_path(GIT_MAN_PATH));
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
338 static 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
351                 warning("'%s': unknown man viewer.", name);
352 }
353
354 static void show_man_page(const char *git_cmd)
355 {
356         struct man_viewer_list *viewer;
357         const char *page = cmd_to_page(git_cmd);
358         const char *fallback = getenv("GIT_MAN_VIEWER");
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         }
365         if (fallback)
366                 exec_viewer(fallback, page);
367         exec_viewer("man", page);
368         die("no man viewer handled the request");
369 }
370
371 static void show_info_page(const char *git_cmd)
372 {
373         const char *page = cmd_to_page(git_cmd);
374         setenv("INFOPATH", system_path(GIT_INFO_PATH), 1);
375         execlp("info", "info", "gitman", page, (char *)NULL);
376         die("no info viewer handled the request");
377 }
378
379 static void get_html_page_path(struct strbuf *page_path, const char *page)
380 {
381         struct stat st;
382         const char *html_path = system_path(GIT_HTML_PATH);
383
384         /* Check that we have a git documentation directory. */
385         if (stat(mkpath("%s/git.html", html_path), &st)
386             || !S_ISREG(st.st_mode))
387                 die("'%s': not a documentation directory.", html_path);
388
389         strbuf_init(page_path, 0);
390         strbuf_addf(page_path, "%s/%s.html", html_path, page);
391 }
392
393 /*
394  * If open_html is not defined in a platform-specific way (see for
395  * example compat/mingw.h), we use the script web--browse to display
396  * HTML.
397  */
398 #ifndef open_html
399 static void open_html(const char *path)
400 {
401         execl_git_cmd("web--browse", "-c", "help.browser", path, (char *)NULL);
402 }
403 #endif
404
405 static void show_html_page(const char *git_cmd)
406 {
407         const char *page = cmd_to_page(git_cmd);
408         struct strbuf page_path; /* it leaks but we exec bellow */
409
410         get_html_page_path(&page_path, page);
411
412         open_html(page_path.buf);
413 }
414
415 int cmd_help(int argc, const char **argv, const char *prefix)
416 {
417         int nongit;
418         const char *alias;
419         enum help_format parsed_help_format;
420         load_command_list("git-", &main_cmds, &other_cmds);
421
422         argc = parse_options(argc, argv, prefix, builtin_help_options,
423                         builtin_help_usage, 0);
424         parsed_help_format = help_format;
425
426         if (show_all) {
427                 printf("usage: %s\n\n", git_usage_string);
428                 list_commands("git commands", &main_cmds, &other_cmds);
429                 printf("%s\n", git_more_info_string);
430                 return 0;
431         }
432
433         if (!argv[0]) {
434                 printf("usage: %s\n\n", git_usage_string);
435                 list_common_cmds_help();
436                 printf("\n%s\n", git_more_info_string);
437                 return 0;
438         }
439
440         setup_git_directory_gently(&nongit);
441         git_config(git_help_config, NULL);
442
443         if (parsed_help_format != HELP_FORMAT_NONE)
444                 help_format = parsed_help_format;
445
446         alias = alias_lookup(argv[0]);
447         if (alias && !is_git_command(argv[0])) {
448                 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
449                 return 0;
450         }
451
452         switch (help_format) {
453         case HELP_FORMAT_NONE:
454         case HELP_FORMAT_MAN:
455                 show_man_page(argv[0]);
456                 break;
457         case HELP_FORMAT_INFO:
458                 show_info_page(argv[0]);
459                 break;
460         case HELP_FORMAT_WEB:
461                 show_html_page(argv[0]);
462                 break;
463         }
464
465         return 0;
466 }