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",
35 static const char * const builtin_help_usage[] = {
36 "git-help [--all] [--man|--web|--info] [command]",
40 static enum help_format parse_help_format(const char *format)
42 if (!strcmp(format, "man"))
43 return HELP_FORMAT_MAN;
44 if (!strcmp(format, "info"))
45 return HELP_FORMAT_INFO;
46 if (!strcmp(format, "web") || !strcmp(format, "html"))
47 return HELP_FORMAT_WEB;
48 die("unrecognized help format '%s'", format);
51 static int check_emacsclient_version(void)
53 struct strbuf buffer = STRBUF_INIT;
54 struct child_process ec_process;
55 const char *argv_ec[] = { "emacsclient", "--version", NULL };
58 /* emacsclient prints its version number on stderr */
59 memset(&ec_process, 0, sizeof(ec_process));
60 ec_process.argv = argv_ec;
62 ec_process.stdout_to_stderr = 1;
63 if (start_command(&ec_process)) {
64 fprintf(stderr, "Failed to start emacsclient.\n");
67 strbuf_read(&buffer, ec_process.err, 20);
68 close(ec_process.err);
71 * Don't bother checking return value, because "emacsclient --version"
72 * seems to always exits with code 1.
74 finish_command(&ec_process);
76 if (prefixcmp(buffer.buf, "emacsclient")) {
77 fprintf(stderr, "Failed to parse emacsclient version.\n");
78 strbuf_release(&buffer);
82 strbuf_remove(&buffer, 0, strlen("emacsclient"));
83 version = atoi(buffer.buf);
87 "emacsclient version '%d' too old (< 22).\n",
89 strbuf_release(&buffer);
93 strbuf_release(&buffer);
97 static void exec_woman_emacs(const char *page)
99 if (!check_emacsclient_version()) {
100 /* This works only with emacsclient version >= 22. */
101 struct strbuf man_page = STRBUF_INIT;
102 strbuf_addf(&man_page, "(woman \"%s\")", page);
103 execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
107 static void exec_man_konqueror(const char *page)
109 const char *display = getenv("DISPLAY");
110 if (display && *display) {
111 struct strbuf man_page = STRBUF_INIT;
112 strbuf_addf(&man_page, "man:%s(1)", page);
113 execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
117 static void exec_man_man(const char *page)
119 execlp("man", "man", page, NULL);
122 static void do_add_man_viewer(void (*exec)(const char *))
124 struct man_viewer_list **p = &man_viewer_list;
128 *p = xmalloc(sizeof(**p));
133 static int add_man_viewer(const char *value)
135 if (!strcasecmp(value, "man"))
136 do_add_man_viewer(exec_man_man);
137 else if (!strcasecmp(value, "woman"))
138 do_add_man_viewer(exec_woman_emacs);
139 else if (!strcasecmp(value, "konqueror"))
140 do_add_man_viewer(exec_man_konqueror);
142 warning("'%s': unsupported man viewer.", value);
147 static int git_help_config(const char *var, const char *value)
149 if (!strcmp(var, "help.format")) {
151 return config_error_nonbool(var);
152 help_format = parse_help_format(value);
155 if (!strcmp(var, "man.viewer")) {
157 return config_error_nonbool(var);
158 return add_man_viewer(value);
160 return git_default_config(var, value);
163 /* most GUI terminals set COLUMNS (although some don't export it) */
164 static int term_columns(void)
166 char *col_string = getenv("COLUMNS");
169 if (col_string && (n_cols = atoi(col_string)) > 0)
175 if (!ioctl(1, TIOCGWINSZ, &ws)) {
185 static inline void mput_char(char c, unsigned int num)
191 static struct cmdnames {
198 } main_cmds, other_cmds;
200 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
202 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
205 memcpy(ent->name, name, len);
208 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
209 cmds->names[cmds->cnt++] = ent;
212 static int cmdname_compare(const void *a_, const void *b_)
214 struct cmdname *a = *(struct cmdname **)a_;
215 struct cmdname *b = *(struct cmdname **)b_;
216 return strcmp(a->name, b->name);
219 static void uniq(struct cmdnames *cmds)
226 for (i = j = 1; i < cmds->cnt; i++)
227 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
228 cmds->names[j++] = cmds->names[i];
233 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
239 while (ci < cmds->cnt && ei < excludes->cnt) {
240 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
242 cmds->names[cj++] = cmds->names[ci++];
249 while (ci < cmds->cnt)
250 cmds->names[cj++] = cmds->names[ci++];
255 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
258 int space = longest + 1; /* min 1 SP between words */
259 int max_cols = term_columns() - 1; /* don't print *on* the edge */
262 if (space < max_cols)
263 cols = max_cols / space;
264 rows = (cmds->cnt + cols - 1) / cols;
266 for (i = 0; i < rows; i++) {
269 for (j = 0; j < cols; j++) {
270 int n = j * rows + i;
274 if (j == cols-1 || n + rows >= cmds->cnt)
276 printf("%-*s", size, cmds->names[n]->name);
282 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
285 unsigned int longest = 0;
286 const char *prefix = "git-";
287 int prefix_len = strlen(prefix);
288 DIR *dir = opendir(path);
291 if (!dir || chdir(path))
294 while ((de = readdir(dir)) != NULL) {
298 if (prefixcmp(de->d_name, prefix))
301 if (stat(de->d_name, &st) || /* stat, not lstat */
302 !S_ISREG(st.st_mode) ||
303 !(st.st_mode & S_IXUSR))
306 entlen = strlen(de->d_name) - prefix_len;
307 if (has_extension(de->d_name, ".exe"))
310 if (longest < entlen)
313 add_cmdname(cmds, de->d_name + prefix_len, entlen);
320 static unsigned int load_command_list(void)
322 unsigned int longest = 0;
324 const char *env_path = getenv("PATH");
325 char *paths, *path, *colon;
326 const char *exec_path = git_exec_path();
329 longest = list_commands_in_dir(&main_cmds, exec_path);
332 fprintf(stderr, "PATH not set\n");
336 path = paths = xstrdup(env_path);
338 if ((colon = strchr(path, ':')))
341 len = list_commands_in_dir(&other_cmds, path);
351 qsort(main_cmds.names, main_cmds.cnt,
352 sizeof(*main_cmds.names), cmdname_compare);
355 qsort(other_cmds.names, other_cmds.cnt,
356 sizeof(*other_cmds.names), cmdname_compare);
358 exclude_cmds(&other_cmds, &main_cmds);
363 static void list_commands(void)
365 unsigned int longest = load_command_list();
366 const char *exec_path = git_exec_path();
369 printf("available git commands in '%s'\n", exec_path);
370 printf("----------------------------");
371 mput_char('-', strlen(exec_path));
373 pretty_print_string_list(&main_cmds, longest);
377 if (other_cmds.cnt) {
378 printf("git commands available from elsewhere on your $PATH\n");
379 printf("---------------------------------------------------\n");
380 pretty_print_string_list(&other_cmds, longest);
385 void list_common_cmds_help(void)
389 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
390 if (longest < strlen(common_cmds[i].name))
391 longest = strlen(common_cmds[i].name);
394 puts("The most commonly used git commands are:");
395 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
396 printf(" %s ", common_cmds[i].name);
397 mput_char(' ', longest - strlen(common_cmds[i].name));
398 puts(common_cmds[i].help);
402 static int is_in_cmdlist(struct cmdnames *c, const char *s)
405 for (i = 0; i < c->cnt; i++)
406 if (!strcmp(s, c->names[i]->name))
411 static int is_git_command(const char *s)
414 return is_in_cmdlist(&main_cmds, s) ||
415 is_in_cmdlist(&other_cmds, s);
418 static const char *cmd_to_page(const char *git_cmd)
422 else if (!prefixcmp(git_cmd, "git"))
425 int page_len = strlen(git_cmd) + 4;
426 char *p = xmalloc(page_len + 1);
428 strcpy(p + 4, git_cmd);
434 static void setup_man_path(void)
436 struct strbuf new_path;
437 const char *old_path = getenv("MANPATH");
439 strbuf_init(&new_path, 0);
441 /* We should always put ':' after our path. If there is no
442 * old_path, the ':' at the end will let 'man' to try
443 * system-wide paths after ours to find the manual page. If
444 * there is old_path, we need ':' as delimiter. */
445 strbuf_addstr(&new_path, GIT_MAN_PATH);
446 strbuf_addch(&new_path, ':');
448 strbuf_addstr(&new_path, old_path);
450 setenv("MANPATH", new_path.buf, 1);
452 strbuf_release(&new_path);
455 static void show_man_page(const char *git_cmd)
457 struct man_viewer_list *viewer;
458 const char *page = cmd_to_page(git_cmd);
461 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
463 viewer->exec(page); /* will return when unable */
466 die("no man viewer handled the request");
469 static void show_info_page(const char *git_cmd)
471 const char *page = cmd_to_page(git_cmd);
472 setenv("INFOPATH", GIT_INFO_PATH, 1);
473 execlp("info", "info", "gitman", page, NULL);
476 static void get_html_page_path(struct strbuf *page_path, const char *page)
480 /* Check that we have a git documentation directory. */
481 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
482 die("'%s': not a documentation directory.", GIT_HTML_PATH);
484 strbuf_init(page_path, 0);
485 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
488 static void show_html_page(const char *git_cmd)
490 const char *page = cmd_to_page(git_cmd);
491 struct strbuf page_path; /* it leaks but we exec bellow */
493 get_html_page_path(&page_path, page);
495 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
498 void help_unknown_cmd(const char *cmd)
500 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
504 int cmd_version(int argc, const char **argv, const char *prefix)
506 printf("git version %s\n", git_version_string);
510 int cmd_help(int argc, const char **argv, const char *prefix)
515 setup_git_directory_gently(&nongit);
516 git_config(git_help_config);
518 argc = parse_options(argc, argv, builtin_help_options,
519 builtin_help_usage, 0);
522 printf("usage: %s\n\n", git_usage_string);
528 printf("usage: %s\n\n", git_usage_string);
529 list_common_cmds_help();
533 alias = alias_lookup(argv[0]);
534 if (alias && !is_git_command(argv[0])) {
535 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
539 switch (help_format) {
540 case HELP_FORMAT_MAN:
541 show_man_page(argv[0]);
543 case HELP_FORMAT_INFO:
544 show_info_page(argv[0]);
546 case HELP_FORMAT_WEB:
547 show_html_page(argv[0]);