Merge branch 'ho/shared'
[git/git.git] / help.c
CommitLineData
70827b15
LT
1/*
2 * builtin-help.c
3 *
4 * Builtin help-related commands (help, usage, version)
5 */
6#include "cache.h"
7#include "builtin.h"
8#include "exec_cmd.h"
9#include "common-cmds.h"
41eb33bd 10#include "parse-options.h"
64949984
CC
11#include "run-command.h"
12
69099d6b
CC
13static struct man_viewer_list {
14 void (*exec)(const char *);
15 struct man_viewer_list *next;
16} *man_viewer_list;
41eb33bd
JK
17
18enum help_format {
19 HELP_FORMAT_MAN,
20 HELP_FORMAT_INFO,
21 HELP_FORMAT_WEB,
22};
23
24static int show_all = 0;
25static enum help_format help_format = HELP_FORMAT_MAN;
26static struct option builtin_help_options[] = {
27 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
28 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
29 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
30 HELP_FORMAT_WEB),
31 OPT_SET_INT('i', "info", &help_format, "show info page",
32 HELP_FORMAT_INFO),
4637e47a 33 OPT_END(),
41eb33bd
JK
34};
35
36static const char * const builtin_help_usage[] = {
37 "git-help [--all] [--man|--web|--info] [command]",
38 NULL
39};
40
41static enum help_format parse_help_format(const char *format)
70087cdb 42{
41eb33bd
JK
43 if (!strcmp(format, "man"))
44 return HELP_FORMAT_MAN;
45 if (!strcmp(format, "info"))
46 return HELP_FORMAT_INFO;
47 if (!strcmp(format, "web") || !strcmp(format, "html"))
48 return HELP_FORMAT_WEB;
70087cdb
CC
49 die("unrecognized help format '%s'", format);
50}
51
69099d6b
CC
52static int check_emacsclient_version(void)
53{
54 struct strbuf buffer = STRBUF_INIT;
55 struct child_process ec_process;
56 const char *argv_ec[] = { "emacsclient", "--version", NULL };
57 int version;
58
59 /* emacsclient prints its version number on stderr */
60 memset(&ec_process, 0, sizeof(ec_process));
61 ec_process.argv = argv_ec;
62 ec_process.err = -1;
63 ec_process.stdout_to_stderr = 1;
64 if (start_command(&ec_process)) {
65 fprintf(stderr, "Failed to start emacsclient.\n");
66 return -1;
67 }
68 strbuf_read(&buffer, ec_process.err, 20);
69 close(ec_process.err);
70
71 /*
72 * Don't bother checking return value, because "emacsclient --version"
73 * seems to always exits with code 1.
74 */
75 finish_command(&ec_process);
76
77 if (prefixcmp(buffer.buf, "emacsclient")) {
78 fprintf(stderr, "Failed to parse emacsclient version.\n");
79 strbuf_release(&buffer);
80 return -1;
81 }
82
83 strbuf_remove(&buffer, 0, strlen("emacsclient"));
84 version = atoi(buffer.buf);
85
86 if (version < 22) {
87 fprintf(stderr,
88 "emacsclient version '%d' too old (< 22).\n",
89 version);
90 strbuf_release(&buffer);
91 return -1;
92 }
93
94 strbuf_release(&buffer);
95 return 0;
96}
97
98static void exec_woman_emacs(const char *page)
99{
100 if (!check_emacsclient_version()) {
101 /* This works only with emacsclient version >= 22. */
102 struct strbuf man_page = STRBUF_INIT;
103 strbuf_addf(&man_page, "(woman \"%s\")", page);
104 execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
105 }
106}
107
108static void exec_man_konqueror(const char *page)
109{
110 const char *display = getenv("DISPLAY");
111 if (display && *display) {
112 struct strbuf man_page = STRBUF_INIT;
113 strbuf_addf(&man_page, "man:%s(1)", page);
114 execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
115 }
116}
117
118static void exec_man_man(const char *page)
119{
120 execlp("man", "man", page, NULL);
121}
122
123static void do_add_man_viewer(void (*exec)(const char *))
124{
125 struct man_viewer_list **p = &man_viewer_list;
126
127 while (*p)
128 p = &((*p)->next);
129 *p = xmalloc(sizeof(**p));
130 (*p)->next = NULL;
131 (*p)->exec = exec;
132}
133
134static int add_man_viewer(const char *value)
135{
136 if (!strcasecmp(value, "man"))
137 do_add_man_viewer(exec_man_man);
138 else if (!strcasecmp(value, "woman"))
139 do_add_man_viewer(exec_woman_emacs);
140 else if (!strcasecmp(value, "konqueror"))
141 do_add_man_viewer(exec_man_konqueror);
142 else
5fb0b3e0 143 warning("'%s': unsupported man viewer.", value);
69099d6b
CC
144
145 return 0;
146}
147
70087cdb
CC
148static int git_help_config(const char *var, const char *value)
149{
41eb33bd
JK
150 if (!strcmp(var, "help.format")) {
151 if (!value)
152 return config_error_nonbool(var);
153 help_format = parse_help_format(value);
154 return 0;
155 }
69099d6b
CC
156 if (!strcmp(var, "man.viewer")) {
157 if (!value)
158 return config_error_nonbool(var);
159 return add_man_viewer(value);
160 }
70087cdb
CC
161 return git_default_config(var, value);
162}
163
82e5a82f 164/* most GUI terminals set COLUMNS (although some don't export it) */
70827b15
LT
165static int term_columns(void)
166{
167 char *col_string = getenv("COLUMNS");
96f1e58f 168 int n_cols;
70827b15
LT
169
170 if (col_string && (n_cols = atoi(col_string)) > 0)
171 return n_cols;
172
173#ifdef TIOCGWINSZ
174 {
175 struct winsize ws;
176 if (!ioctl(1, TIOCGWINSZ, &ws)) {
177 if (ws.ws_col)
178 return ws.ws_col;
179 }
180 }
181#endif
182
183 return 80;
184}
185
70827b15
LT
186static inline void mput_char(char c, unsigned int num)
187{
188 while(num--)
189 putchar(c);
190}
191
1eb05690
SP
192static struct cmdnames {
193 int alloc;
194 int cnt;
195 struct cmdname {
196 size_t len;
197 char name[1];
198 } **names;
199} main_cmds, other_cmds;
200
201static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 202{
1eb05690
SP
203 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
204
70827b15
LT
205 ent->len = len;
206 memcpy(ent->name, name, len);
207 ent->name[len] = 0;
1eb05690
SP
208
209 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
210 cmds->names[cmds->cnt++] = ent;
70827b15
LT
211}
212
213static int cmdname_compare(const void *a_, const void *b_)
214{
215 struct cmdname *a = *(struct cmdname **)a_;
216 struct cmdname *b = *(struct cmdname **)b_;
217 return strcmp(a->name, b->name);
218}
219
1eb05690
SP
220static void uniq(struct cmdnames *cmds)
221{
222 int i, j;
223
224 if (!cmds->cnt)
225 return;
226
227 for (i = j = 1; i < cmds->cnt; i++)
228 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
229 cmds->names[j++] = cmds->names[i];
230
231 cmds->cnt = j;
232}
233
f3fa1838
JH
234static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
235{
1eb05690
SP
236 int ci, cj, ei;
237 int cmp;
238
239 ci = cj = ei = 0;
240 while (ci < cmds->cnt && ei < excludes->cnt) {
241 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
242 if (cmp < 0)
243 cmds->names[cj++] = cmds->names[ci++];
244 else if (cmp == 0)
245 ci++, ei++;
246 else if (cmp > 0)
247 ei++;
248 }
249
250 while (ci < cmds->cnt)
251 cmds->names[cj++] = cmds->names[ci++];
252
253 cmds->cnt = cj;
254}
255
256static void pretty_print_string_list(struct cmdnames *cmds, int longest)
70827b15
LT
257{
258 int cols = 1, rows;
259 int space = longest + 1; /* min 1 SP between words */
260 int max_cols = term_columns() - 1; /* don't print *on* the edge */
261 int i, j;
262
263 if (space < max_cols)
264 cols = max_cols / space;
1eb05690 265 rows = (cmds->cnt + cols - 1) / cols;
70827b15
LT
266
267 for (i = 0; i < rows; i++) {
268 printf(" ");
269
270 for (j = 0; j < cols; j++) {
271 int n = j * rows + i;
272 int size = space;
1eb05690 273 if (n >= cmds->cnt)
70827b15 274 break;
1eb05690 275 if (j == cols-1 || n + rows >= cmds->cnt)
70827b15 276 size = 1;
1eb05690 277 printf("%-*s", size, cmds->names[n]->name);
70827b15
LT
278 }
279 putchar('\n');
280 }
281}
282
1eb05690
SP
283static unsigned int list_commands_in_dir(struct cmdnames *cmds,
284 const char *path)
70827b15
LT
285{
286 unsigned int longest = 0;
edb6ddc5
SP
287 const char *prefix = "git-";
288 int prefix_len = strlen(prefix);
1eb05690 289 DIR *dir = opendir(path);
70827b15
LT
290 struct dirent *de;
291
1eb05690
SP
292 if (!dir || chdir(path))
293 return 0;
70827b15 294
70827b15
LT
295 while ((de = readdir(dir)) != NULL) {
296 struct stat st;
297 int entlen;
298
edb6ddc5 299 if (prefixcmp(de->d_name, prefix))
70827b15 300 continue;
0966003c
SP
301
302 if (stat(de->d_name, &st) || /* stat, not lstat */
70827b15
LT
303 !S_ISREG(st.st_mode) ||
304 !(st.st_mode & S_IXUSR))
305 continue;
306
edb6ddc5 307 entlen = strlen(de->d_name) - prefix_len;
5bb1cda5 308 if (has_extension(de->d_name, ".exe"))
70827b15
LT
309 entlen -= 4;
310
311 if (longest < entlen)
312 longest = entlen;
313
1eb05690 314 add_cmdname(cmds, de->d_name + prefix_len, entlen);
70827b15
LT
315 }
316 closedir(dir);
317
1eb05690
SP
318 return longest;
319}
320
2156435f 321static unsigned int load_command_list(void)
1eb05690
SP
322{
323 unsigned int longest = 0;
324 unsigned int len;
325 const char *env_path = getenv("PATH");
326 char *paths, *path, *colon;
327 const char *exec_path = git_exec_path();
328
329 if (exec_path)
330 longest = list_commands_in_dir(&main_cmds, exec_path);
331
332 if (!env_path) {
333 fprintf(stderr, "PATH not set\n");
334 exit(1);
335 }
336
337 path = paths = xstrdup(env_path);
338 while (1) {
339 if ((colon = strchr(path, ':')))
340 *colon = 0;
341
342 len = list_commands_in_dir(&other_cmds, path);
343 if (len > longest)
344 longest = len;
345
346 if (!colon)
347 break;
348 path = colon + 1;
349 }
350 free(paths);
351
352 qsort(main_cmds.names, main_cmds.cnt,
353 sizeof(*main_cmds.names), cmdname_compare);
354 uniq(&main_cmds);
355
356 qsort(other_cmds.names, other_cmds.cnt,
357 sizeof(*other_cmds.names), cmdname_compare);
358 uniq(&other_cmds);
359 exclude_cmds(&other_cmds, &main_cmds);
360
2156435f
JK
361 return longest;
362}
363
364static void list_commands(void)
365{
366 unsigned int longest = load_command_list();
367 const char *exec_path = git_exec_path();
368
1eb05690
SP
369 if (main_cmds.cnt) {
370 printf("available git commands in '%s'\n", exec_path);
371 printf("----------------------------");
372 mput_char('-', strlen(exec_path));
373 putchar('\n');
374 pretty_print_string_list(&main_cmds, longest);
375 putchar('\n');
376 }
377
378 if (other_cmds.cnt) {
379 printf("git commands available from elsewhere on your $PATH\n");
380 printf("---------------------------------------------------\n");
381 pretty_print_string_list(&other_cmds, longest);
382 putchar('\n');
383 }
70827b15
LT
384}
385
3d7e2d85 386void list_common_cmds_help(void)
70827b15
LT
387{
388 int i, longest = 0;
389
390 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
391 if (longest < strlen(common_cmds[i].name))
392 longest = strlen(common_cmds[i].name);
393 }
394
395 puts("The most commonly used git commands are:");
396 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
4f75b115
RS
397 printf(" %s ", common_cmds[i].name);
398 mput_char(' ', longest - strlen(common_cmds[i].name));
70827b15
LT
399 puts(common_cmds[i].help);
400 }
70827b15
LT
401}
402
2156435f
JK
403static int is_in_cmdlist(struct cmdnames *c, const char *s)
404{
405 int i;
406 for (i = 0; i < c->cnt; i++)
407 if (!strcmp(s, c->names[i]->name))
408 return 1;
409 return 0;
410}
411
412static int is_git_command(const char *s)
413{
414 load_command_list();
415 return is_in_cmdlist(&main_cmds, s) ||
416 is_in_cmdlist(&other_cmds, s);
417}
418
df55c9cb 419static const char *cmd_to_page(const char *git_cmd)
70827b15 420{
5d6491c7
CC
421 if (!git_cmd)
422 return "git";
423 else if (!prefixcmp(git_cmd, "git"))
df55c9cb 424 return git_cmd;
70827b15
LT
425 else {
426 int page_len = strlen(git_cmd) + 4;
2d7320d0 427 char *p = xmalloc(page_len + 1);
70827b15
LT
428 strcpy(p, "git-");
429 strcpy(p + 4, git_cmd);
430 p[page_len] = 0;
df55c9cb 431 return p;
70827b15 432 }
df55c9cb 433}
70827b15 434
8e566f24
SO
435static void setup_man_path(void)
436{
437 struct strbuf new_path;
438 const char *old_path = getenv("MANPATH");
439
440 strbuf_init(&new_path, 0);
441
442 /* We should always put ':' after our path. If there is no
443 * old_path, the ':' at the end will let 'man' to try
444 * system-wide paths after ours to find the manual page. If
445 * there is old_path, we need ':' as delimiter. */
446 strbuf_addstr(&new_path, GIT_MAN_PATH);
447 strbuf_addch(&new_path, ':');
448 if (old_path)
449 strbuf_addstr(&new_path, old_path);
450
451 setenv("MANPATH", new_path.buf, 1);
452
453 strbuf_release(&new_path);
454}
455
df55c9cb
CC
456static void show_man_page(const char *git_cmd)
457{
69099d6b 458 struct man_viewer_list *viewer;
df55c9cb 459 const char *page = cmd_to_page(git_cmd);
69099d6b 460
8e566f24 461 setup_man_path();
69099d6b
CC
462 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
463 {
464 viewer->exec(page); /* will return when unable */
465 }
466 exec_man_man(page);
467 die("no man viewer handled the request");
70827b15
LT
468}
469
df55c9cb
CC
470static void show_info_page(const char *git_cmd)
471{
472 const char *page = cmd_to_page(git_cmd);
a149a1a4 473 setenv("INFOPATH", GIT_INFO_PATH, 1);
78d39f98 474 execlp("info", "info", "gitman", page, NULL);
df55c9cb
CC
475}
476
482cce82
CC
477static void get_html_page_path(struct strbuf *page_path, const char *page)
478{
479 struct stat st;
480
481 /* Check that we have a git documentation directory. */
482 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
483 die("'%s': not a documentation directory.", GIT_HTML_PATH);
484
485 strbuf_init(page_path, 0);
486 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
487}
488
5d6491c7
CC
489static void show_html_page(const char *git_cmd)
490{
491 const char *page = cmd_to_page(git_cmd);
482cce82
CC
492 struct strbuf page_path; /* it leaks but we exec bellow */
493
494 get_html_page_path(&page_path, page);
495
5884f1fe 496 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
5d6491c7
CC
497}
498
822a7d50
RJ
499void help_unknown_cmd(const char *cmd)
500{
a238917b 501 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
822a7d50
RJ
502 exit(1);
503}
504
a633fca0 505int cmd_version(int argc, const char **argv, const char *prefix)
70827b15
LT
506{
507 printf("git version %s\n", git_version_string);
508 return 0;
509}
510
5d6491c7 511int cmd_help(int argc, const char **argv, const char *prefix)
70827b15 512{
41eb33bd 513 int nongit;
2156435f 514 const char *alias;
5d6491c7 515
41eb33bd
JK
516 setup_git_directory_gently(&nongit);
517 git_config(git_help_config);
822a7d50 518
41eb33bd
JK
519 argc = parse_options(argc, argv, builtin_help_options,
520 builtin_help_usage, 0);
521
522 if (show_all) {
822a7d50 523 printf("usage: %s\n\n", git_usage_string);
1eb05690 524 list_commands();
41eb33bd 525 return 0;
822a7d50 526 }
df55c9cb 527
41eb33bd
JK
528 if (!argv[0]) {
529 printf("usage: %s\n\n", git_usage_string);
530 list_common_cmds_help();
531 return 0;
70087cdb
CC
532 }
533
2156435f
JK
534 alias = alias_lookup(argv[0]);
535 if (alias && !is_git_command(argv[0])) {
536 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
537 return 0;
538 }
539
41eb33bd
JK
540 switch (help_format) {
541 case HELP_FORMAT_MAN:
542 show_man_page(argv[0]);
543 break;
544 case HELP_FORMAT_INFO:
545 show_info_page(argv[0]);
546 break;
547 case HELP_FORMAT_WEB:
548 show_html_page(argv[0]);
549 break;
70087cdb 550 }
822a7d50 551
70827b15
LT
552 return 0;
553}