4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
10 #include "parse-options.h"
11 #include "run-command.h"
13 static struct man_viewer_list {
14 struct man_viewer_list *next;
15 char name[FLEX_ARRAY];
18 static struct man_viewer_info_list {
19 struct man_viewer_info_list *next;
21 char name[FLEX_ARRAY];
22 } *man_viewer_info_list;
30 static int show_all = 0;
31 static enum help_format help_format = HELP_FORMAT_MAN;
32 static struct option builtin_help_options[] = {
33 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
34 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
35 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
37 OPT_SET_INT('i', "info", &help_format, "show info page",
42 static const char * const builtin_help_usage[] = {
43 "git-help [--all] [--man|--web|--info] [command]",
47 static enum help_format parse_help_format(const char *format)
49 if (!strcmp(format, "man"))
50 return HELP_FORMAT_MAN;
51 if (!strcmp(format, "info"))
52 return HELP_FORMAT_INFO;
53 if (!strcmp(format, "web") || !strcmp(format, "html"))
54 return HELP_FORMAT_WEB;
55 die("unrecognized help format '%s'", format);
58 static const char *get_man_viewer_info(const char *name)
60 struct man_viewer_info_list *viewer;
62 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
64 if (!strcasecmp(name, viewer->name))
70 static int check_emacsclient_version(void)
72 struct strbuf buffer = STRBUF_INIT;
73 struct child_process ec_process;
74 const char *argv_ec[] = { "emacsclient", "--version", NULL };
77 /* emacsclient prints its version number on stderr */
78 memset(&ec_process, 0, sizeof(ec_process));
79 ec_process.argv = argv_ec;
81 ec_process.stdout_to_stderr = 1;
82 if (start_command(&ec_process)) {
83 fprintf(stderr, "Failed to start emacsclient.\n");
86 strbuf_read(&buffer, ec_process.err, 20);
87 close(ec_process.err);
90 * Don't bother checking return value, because "emacsclient --version"
91 * seems to always exits with code 1.
93 finish_command(&ec_process);
95 if (prefixcmp(buffer.buf, "emacsclient")) {
96 fprintf(stderr, "Failed to parse emacsclient version.\n");
97 strbuf_release(&buffer);
101 strbuf_remove(&buffer, 0, strlen("emacsclient"));
102 version = atoi(buffer.buf);
106 "emacsclient version '%d' too old (< 22).\n",
108 strbuf_release(&buffer);
112 strbuf_release(&buffer);
116 static void exec_woman_emacs(const char* path, const char *page)
118 if (!check_emacsclient_version()) {
119 /* This works only with emacsclient version >= 22. */
120 struct strbuf man_page = STRBUF_INIT;
123 path = "emacsclient";
124 strbuf_addf(&man_page, "(woman \"%s\")", page);
125 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
126 warning("failed to exec '%s': %s", path, strerror(errno));
130 static void exec_man_konqueror(const char* path, const char *page)
132 const char *display = getenv("DISPLAY");
133 if (display && *display) {
134 struct strbuf man_page = STRBUF_INIT;
135 const char *filename = "kfmclient";
137 /* It's simpler to launch konqueror using kfmclient. */
139 const char *file = strrchr(path, '/');
140 if (file && !strcmp(file + 1, "konqueror")) {
141 char *new = xstrdup(path);
142 char *dest = strrchr(new, '/');
144 /* strlen("konqueror") == strlen("kfmclient") */
145 strcpy(dest + 1, "kfmclient");
152 strbuf_addf(&man_page, "man:%s(1)", page);
153 execlp(path, filename, "newTab", man_page.buf, NULL);
154 warning("failed to exec '%s': %s", path, strerror(errno));
158 static void exec_man_man(const char* path, const char *page)
162 execlp(path, "man", page, NULL);
163 warning("failed to exec '%s': %s", path, strerror(errno));
166 static void exec_man_cmd(const char *cmd, const char *page)
168 struct strbuf shell_cmd = STRBUF_INIT;
169 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
170 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
171 warning("failed to exec '%s': %s", cmd, strerror(errno));
174 static void add_man_viewer(const char *name)
176 struct man_viewer_list **p = &man_viewer_list;
177 size_t len = strlen(name);
181 *p = xcalloc(1, (sizeof(**p) + len + 1));
182 strncpy((*p)->name, name, len);
185 static int supported_man_viewer(const char *name, size_t len)
187 return (!strncasecmp("man", name, len) ||
188 !strncasecmp("woman", name, len) ||
189 !strncasecmp("konqueror", name, len));
192 static void do_add_man_viewer_info(const char *name,
196 struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
198 strncpy(new->name, name, len);
199 new->info = xstrdup(value);
200 new->next = man_viewer_info_list;
201 man_viewer_info_list = new;
204 static int add_man_viewer_path(const char *name,
208 if (supported_man_viewer(name, len))
209 do_add_man_viewer_info(name, len, value);
211 warning("'%s': path for unsupported man viewer.\n"
212 "Please consider using 'man.<tool>.cmd' instead.",
218 static int add_man_viewer_cmd(const char *name,
222 if (supported_man_viewer(name, len))
223 warning("'%s': cmd for supported man viewer.\n"
224 "Please consider using 'man.<tool>.path' instead.",
227 do_add_man_viewer_info(name, len, value);
232 static int add_man_viewer_info(const char *var, const char *value)
234 const char *name = var + 4;
235 const char *subkey = strrchr(name, '.');
238 return error("Config with no key for man viewer: %s", name);
240 if (!strcmp(subkey, ".path")) {
242 return config_error_nonbool(var);
243 return add_man_viewer_path(name, subkey - name, value);
245 if (!strcmp(subkey, ".cmd")) {
247 return config_error_nonbool(var);
248 return add_man_viewer_cmd(name, subkey - name, value);
251 warning("'%s': unsupported man viewer sub key.", subkey);
255 static int git_help_config(const char *var, const char *value)
257 if (!strcmp(var, "help.format")) {
259 return config_error_nonbool(var);
260 help_format = parse_help_format(value);
263 if (!strcmp(var, "man.viewer")) {
265 return config_error_nonbool(var);
266 add_man_viewer(value);
269 if (!prefixcmp(var, "man."))
270 return add_man_viewer_info(var, value);
272 return git_default_config(var, value);
275 /* most GUI terminals set COLUMNS (although some don't export it) */
276 static int term_columns(void)
278 char *col_string = getenv("COLUMNS");
281 if (col_string && (n_cols = atoi(col_string)) > 0)
287 if (!ioctl(1, TIOCGWINSZ, &ws)) {
297 static inline void mput_char(char c, unsigned int num)
303 static struct cmdnames {
310 } main_cmds, other_cmds;
312 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
314 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
317 memcpy(ent->name, name, len);
320 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
321 cmds->names[cmds->cnt++] = ent;
324 static int cmdname_compare(const void *a_, const void *b_)
326 struct cmdname *a = *(struct cmdname **)a_;
327 struct cmdname *b = *(struct cmdname **)b_;
328 return strcmp(a->name, b->name);
331 static void uniq(struct cmdnames *cmds)
338 for (i = j = 1; i < cmds->cnt; i++)
339 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
340 cmds->names[j++] = cmds->names[i];
345 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
351 while (ci < cmds->cnt && ei < excludes->cnt) {
352 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
354 cmds->names[cj++] = cmds->names[ci++];
361 while (ci < cmds->cnt)
362 cmds->names[cj++] = cmds->names[ci++];
367 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
370 int space = longest + 1; /* min 1 SP between words */
371 int max_cols = term_columns() - 1; /* don't print *on* the edge */
374 if (space < max_cols)
375 cols = max_cols / space;
376 rows = (cmds->cnt + cols - 1) / cols;
378 for (i = 0; i < rows; i++) {
381 for (j = 0; j < cols; j++) {
382 int n = j * rows + i;
386 if (j == cols-1 || n + rows >= cmds->cnt)
388 printf("%-*s", size, cmds->names[n]->name);
394 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
397 unsigned int longest = 0;
398 const char *prefix = "git-";
399 int prefix_len = strlen(prefix);
400 DIR *dir = opendir(path);
403 if (!dir || chdir(path))
406 while ((de = readdir(dir)) != NULL) {
410 if (prefixcmp(de->d_name, prefix))
413 if (stat(de->d_name, &st) || /* stat, not lstat */
414 !S_ISREG(st.st_mode) ||
415 !(st.st_mode & S_IXUSR))
418 entlen = strlen(de->d_name) - prefix_len;
419 if (has_extension(de->d_name, ".exe"))
422 if (longest < entlen)
425 add_cmdname(cmds, de->d_name + prefix_len, entlen);
432 static unsigned int load_command_list(void)
434 unsigned int longest = 0;
436 const char *env_path = getenv("PATH");
437 char *paths, *path, *colon;
438 const char *exec_path = git_exec_path();
441 longest = list_commands_in_dir(&main_cmds, exec_path);
444 fprintf(stderr, "PATH not set\n");
448 path = paths = xstrdup(env_path);
450 if ((colon = strchr(path, ':')))
453 len = list_commands_in_dir(&other_cmds, path);
463 qsort(main_cmds.names, main_cmds.cnt,
464 sizeof(*main_cmds.names), cmdname_compare);
467 qsort(other_cmds.names, other_cmds.cnt,
468 sizeof(*other_cmds.names), cmdname_compare);
470 exclude_cmds(&other_cmds, &main_cmds);
475 static void list_commands(void)
477 unsigned int longest = load_command_list();
478 const char *exec_path = git_exec_path();
481 printf("available git commands in '%s'\n", exec_path);
482 printf("----------------------------");
483 mput_char('-', strlen(exec_path));
485 pretty_print_string_list(&main_cmds, longest);
489 if (other_cmds.cnt) {
490 printf("git commands available from elsewhere on your $PATH\n");
491 printf("---------------------------------------------------\n");
492 pretty_print_string_list(&other_cmds, longest);
497 void list_common_cmds_help(void)
501 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
502 if (longest < strlen(common_cmds[i].name))
503 longest = strlen(common_cmds[i].name);
506 puts("The most commonly used git commands are:");
507 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
508 printf(" %s ", common_cmds[i].name);
509 mput_char(' ', longest - strlen(common_cmds[i].name));
510 puts(common_cmds[i].help);
514 static int is_in_cmdlist(struct cmdnames *c, const char *s)
517 for (i = 0; i < c->cnt; i++)
518 if (!strcmp(s, c->names[i]->name))
523 static int is_git_command(const char *s)
526 return is_in_cmdlist(&main_cmds, s) ||
527 is_in_cmdlist(&other_cmds, s);
530 static const char *cmd_to_page(const char *git_cmd)
534 else if (!prefixcmp(git_cmd, "git"))
537 int page_len = strlen(git_cmd) + 4;
538 char *p = xmalloc(page_len + 1);
540 strcpy(p + 4, git_cmd);
546 static void setup_man_path(void)
548 struct strbuf new_path;
549 const char *old_path = getenv("MANPATH");
551 strbuf_init(&new_path, 0);
553 /* We should always put ':' after our path. If there is no
554 * old_path, the ':' at the end will let 'man' to try
555 * system-wide paths after ours to find the manual page. If
556 * there is old_path, we need ':' as delimiter. */
557 strbuf_addstr(&new_path, GIT_MAN_PATH);
558 strbuf_addch(&new_path, ':');
560 strbuf_addstr(&new_path, old_path);
562 setenv("MANPATH", new_path.buf, 1);
564 strbuf_release(&new_path);
567 static void exec_viewer(const char *name, const char *page)
569 const char *info = get_man_viewer_info(name);
571 if (!strcasecmp(name, "man"))
572 exec_man_man(info, page);
573 else if (!strcasecmp(name, "woman"))
574 exec_woman_emacs(info, page);
575 else if (!strcasecmp(name, "konqueror"))
576 exec_man_konqueror(info, page);
578 exec_man_cmd(info, page);
580 warning("'%s': unknown man viewer.", name);
583 static void show_man_page(const char *git_cmd)
585 struct man_viewer_list *viewer;
586 const char *page = cmd_to_page(git_cmd);
589 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
591 exec_viewer(viewer->name, page); /* will return when unable */
593 exec_viewer("man", page);
594 die("no man viewer handled the request");
597 static void show_info_page(const char *git_cmd)
599 const char *page = cmd_to_page(git_cmd);
600 setenv("INFOPATH", GIT_INFO_PATH, 1);
601 execlp("info", "info", "gitman", page, NULL);
604 static void get_html_page_path(struct strbuf *page_path, const char *page)
608 /* Check that we have a git documentation directory. */
609 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
610 die("'%s': not a documentation directory.", GIT_HTML_PATH);
612 strbuf_init(page_path, 0);
613 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
616 static void show_html_page(const char *git_cmd)
618 const char *page = cmd_to_page(git_cmd);
619 struct strbuf page_path; /* it leaks but we exec bellow */
621 get_html_page_path(&page_path, page);
623 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
626 void help_unknown_cmd(const char *cmd)
628 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
632 int cmd_version(int argc, const char **argv, const char *prefix)
634 printf("git version %s\n", git_version_string);
638 int cmd_help(int argc, const char **argv, const char *prefix)
643 setup_git_directory_gently(&nongit);
644 git_config(git_help_config);
646 argc = parse_options(argc, argv, builtin_help_options,
647 builtin_help_usage, 0);
650 printf("usage: %s\n\n", git_usage_string);
656 printf("usage: %s\n\n", git_usage_string);
657 list_common_cmds_help();
661 alias = alias_lookup(argv[0]);
662 if (alias && !is_git_command(argv[0])) {
663 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
667 switch (help_format) {
668 case HELP_FORMAT_MAN:
669 show_man_page(argv[0]);
671 case HELP_FORMAT_INFO:
672 show_info_page(argv[0]);
674 case HELP_FORMAT_WEB:
675 show_html_page(argv[0]);