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