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 void (*exec)(const char *);
15 struct man_viewer_list *next;
24 static int show_all = 0;
25 static enum help_format help_format = HELP_FORMAT_MAN;
26 static 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",
31 OPT_SET_INT('i', "info", &help_format, "show info page",
36 static const char * const builtin_help_usage[] = {
37 "git-help [--all] [--man|--web|--info] [command]",
41 static enum help_format parse_help_format(const char *format)
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;
49 die("unrecognized help format '%s'", format);
52 static int check_emacsclient_version(void)
54 struct strbuf buffer = STRBUF_INIT;
55 struct child_process ec_process;
56 const char *argv_ec[] = { "emacsclient", "--version", NULL };
59 /* emacsclient prints its version number on stderr */
60 memset(&ec_process, 0, sizeof(ec_process));
61 ec_process.argv = argv_ec;
63 ec_process.stdout_to_stderr = 1;
64 if (start_command(&ec_process)) {
65 fprintf(stderr, "Failed to start emacsclient.\n");
68 strbuf_read(&buffer, ec_process.err, 20);
69 close(ec_process.err);
72 * Don't bother checking return value, because "emacsclient --version"
73 * seems to always exits with code 1.
75 finish_command(&ec_process);
77 if (prefixcmp(buffer.buf, "emacsclient")) {
78 fprintf(stderr, "Failed to parse emacsclient version.\n");
79 strbuf_release(&buffer);
83 strbuf_remove(&buffer, 0, strlen("emacsclient"));
84 version = atoi(buffer.buf);
88 "emacsclient version '%d' too old (< 22).\n",
90 strbuf_release(&buffer);
94 strbuf_release(&buffer);
98 static void exec_woman_emacs(const char *page)
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);
108 static void exec_man_konqueror(const char *page)
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);
118 static void exec_man_man(const char *page)
120 execlp("man", "man", page, NULL);
123 static void do_add_man_viewer(void (*exec)(const char *))
125 struct man_viewer_list **p = &man_viewer_list;
129 *p = xmalloc(sizeof(**p));
134 static int add_man_viewer(const char *value)
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);
143 warning("'%s': unsupported man viewer.", value);
148 static int git_help_config(const char *var, const char *value)
150 if (!strcmp(var, "help.format")) {
152 return config_error_nonbool(var);
153 help_format = parse_help_format(value);
156 if (!strcmp(var, "man.viewer")) {
158 return config_error_nonbool(var);
159 return add_man_viewer(value);
161 return git_default_config(var, value);
164 /* most GUI terminals set COLUMNS (although some don't export it) */
165 static int term_columns(void)
167 char *col_string = getenv("COLUMNS");
170 if (col_string && (n_cols = atoi(col_string)) > 0)
176 if (!ioctl(1, TIOCGWINSZ, &ws)) {
186 static inline void mput_char(char c, unsigned int num)
192 static struct cmdnames {
199 } main_cmds, other_cmds;
201 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
203 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
206 memcpy(ent->name, name, len);
209 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
210 cmds->names[cmds->cnt++] = ent;
213 static int cmdname_compare(const void *a_, const void *b_)
215 struct cmdname *a = *(struct cmdname **)a_;
216 struct cmdname *b = *(struct cmdname **)b_;
217 return strcmp(a->name, b->name);
220 static void uniq(struct cmdnames *cmds)
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];
234 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
240 while (ci < cmds->cnt && ei < excludes->cnt) {
241 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
243 cmds->names[cj++] = cmds->names[ci++];
250 while (ci < cmds->cnt)
251 cmds->names[cj++] = cmds->names[ci++];
256 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
259 int space = longest + 1; /* min 1 SP between words */
260 int max_cols = term_columns() - 1; /* don't print *on* the edge */
263 if (space < max_cols)
264 cols = max_cols / space;
265 rows = (cmds->cnt + cols - 1) / cols;
267 for (i = 0; i < rows; i++) {
270 for (j = 0; j < cols; j++) {
271 int n = j * rows + i;
275 if (j == cols-1 || n + rows >= cmds->cnt)
277 printf("%-*s", size, cmds->names[n]->name);
283 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
286 unsigned int longest = 0;
287 const char *prefix = "git-";
288 int prefix_len = strlen(prefix);
289 DIR *dir = opendir(path);
292 if (!dir || chdir(path))
295 while ((de = readdir(dir)) != NULL) {
299 if (prefixcmp(de->d_name, prefix))
302 if (stat(de->d_name, &st) || /* stat, not lstat */
303 !S_ISREG(st.st_mode) ||
304 !(st.st_mode & S_IXUSR))
307 entlen = strlen(de->d_name) - prefix_len;
308 if (has_extension(de->d_name, ".exe"))
311 if (longest < entlen)
314 add_cmdname(cmds, de->d_name + prefix_len, entlen);
321 static unsigned int load_command_list(void)
323 unsigned int longest = 0;
325 const char *env_path = getenv("PATH");
326 char *paths, *path, *colon;
327 const char *exec_path = git_exec_path();
330 longest = list_commands_in_dir(&main_cmds, exec_path);
333 fprintf(stderr, "PATH not set\n");
337 path = paths = xstrdup(env_path);
339 if ((colon = strchr(path, ':')))
342 len = list_commands_in_dir(&other_cmds, path);
352 qsort(main_cmds.names, main_cmds.cnt,
353 sizeof(*main_cmds.names), cmdname_compare);
356 qsort(other_cmds.names, other_cmds.cnt,
357 sizeof(*other_cmds.names), cmdname_compare);
359 exclude_cmds(&other_cmds, &main_cmds);
364 static void list_commands(void)
366 unsigned int longest = load_command_list();
367 const char *exec_path = git_exec_path();
370 printf("available git commands in '%s'\n", exec_path);
371 printf("----------------------------");
372 mput_char('-', strlen(exec_path));
374 pretty_print_string_list(&main_cmds, longest);
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);
386 void list_common_cmds_help(void)
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);
395 puts("The most commonly used git commands are:");
396 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
397 printf(" %s ", common_cmds[i].name);
398 mput_char(' ', longest - strlen(common_cmds[i].name));
399 puts(common_cmds[i].help);
403 static int is_in_cmdlist(struct cmdnames *c, const char *s)
406 for (i = 0; i < c->cnt; i++)
407 if (!strcmp(s, c->names[i]->name))
412 static int is_git_command(const char *s)
415 return is_in_cmdlist(&main_cmds, s) ||
416 is_in_cmdlist(&other_cmds, s);
419 static const char *cmd_to_page(const char *git_cmd)
423 else if (!prefixcmp(git_cmd, "git"))
426 int page_len = strlen(git_cmd) + 4;
427 char *p = xmalloc(page_len + 1);
429 strcpy(p + 4, git_cmd);
435 static void setup_man_path(void)
437 struct strbuf new_path;
438 const char *old_path = getenv("MANPATH");
440 strbuf_init(&new_path, 0);
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, ':');
449 strbuf_addstr(&new_path, old_path);
451 setenv("MANPATH", new_path.buf, 1);
453 strbuf_release(&new_path);
456 static void show_man_page(const char *git_cmd)
458 struct man_viewer_list *viewer;
459 const char *page = cmd_to_page(git_cmd);
462 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
464 viewer->exec(page); /* will return when unable */
467 die("no man viewer handled the request");
470 static void show_info_page(const char *git_cmd)
472 const char *page = cmd_to_page(git_cmd);
473 setenv("INFOPATH", GIT_INFO_PATH, 1);
474 execlp("info", "info", "gitman", page, NULL);
477 static void get_html_page_path(struct strbuf *page_path, const char *page)
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);
485 strbuf_init(page_path, 0);
486 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
489 static void show_html_page(const char *git_cmd)
491 const char *page = cmd_to_page(git_cmd);
492 struct strbuf page_path; /* it leaks but we exec bellow */
494 get_html_page_path(&page_path, page);
496 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
499 void help_unknown_cmd(const char *cmd)
501 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
505 int cmd_version(int argc, const char **argv, const char *prefix)
507 printf("git version %s\n", git_version_string);
511 int cmd_help(int argc, const char **argv, const char *prefix)
516 setup_git_directory_gently(&nongit);
517 git_config(git_help_config);
519 argc = parse_options(argc, argv, builtin_help_options,
520 builtin_help_usage, 0);
523 printf("usage: %s\n\n", git_usage_string);
529 printf("usage: %s\n\n", git_usage_string);
530 list_common_cmds_help();
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);
540 switch (help_format) {
541 case HELP_FORMAT_MAN:
542 show_man_page(argv[0]);
544 case HELP_FORMAT_INFO:
545 show_info_page(argv[0]);
547 case HELP_FORMAT_WEB:
548 show_html_page(argv[0]);