4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
10 #include "parse-options.h"
11 #include "run-command.h"
14 static struct man_viewer_list
{
15 struct man_viewer_list
*next
;
16 char name
[FLEX_ARRAY
];
19 static struct man_viewer_info_list
{
20 struct man_viewer_info_list
*next
;
22 char name
[FLEX_ARRAY
];
23 } *man_viewer_info_list
;
31 static int show_all
= 0;
32 static enum help_format help_format
= HELP_FORMAT_MAN
;
33 static struct option builtin_help_options
[] = {
34 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
35 OPT_SET_INT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
36 OPT_SET_INT('w', "web", &help_format
, "show manual in web browser",
38 OPT_SET_INT('i', "info", &help_format
, "show info page",
43 static const char * const builtin_help_usage
[] = {
44 "git help [--all] [--man|--web|--info] [command]",
48 static enum help_format
parse_help_format(const char *format
)
50 if (!strcmp(format
, "man"))
51 return HELP_FORMAT_MAN
;
52 if (!strcmp(format
, "info"))
53 return HELP_FORMAT_INFO
;
54 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
55 return HELP_FORMAT_WEB
;
56 die("unrecognized help format '%s'", format
);
59 static const char *get_man_viewer_info(const char *name
)
61 struct man_viewer_info_list
*viewer
;
63 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
65 if (!strcasecmp(name
, viewer
->name
))
71 static int check_emacsclient_version(void)
73 struct strbuf buffer
= STRBUF_INIT
;
74 struct child_process ec_process
;
75 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
78 /* emacsclient prints its version number on stderr */
79 memset(&ec_process
, 0, sizeof(ec_process
));
80 ec_process
.argv
= argv_ec
;
82 ec_process
.stdout_to_stderr
= 1;
83 if (start_command(&ec_process
)) {
84 fprintf(stderr
, "Failed to start emacsclient.\n");
87 strbuf_read(&buffer
, ec_process
.err
, 20);
88 close(ec_process
.err
);
91 * Don't bother checking return value, because "emacsclient --version"
92 * seems to always exits with code 1.
94 finish_command(&ec_process
);
96 if (prefixcmp(buffer
.buf
, "emacsclient")) {
97 fprintf(stderr
, "Failed to parse emacsclient version.\n");
98 strbuf_release(&buffer
);
102 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
103 version
= atoi(buffer
.buf
);
107 "emacsclient version '%d' too old (< 22).\n",
109 strbuf_release(&buffer
);
113 strbuf_release(&buffer
);
117 static void exec_woman_emacs(const char* path
, const char *page
)
119 if (!check_emacsclient_version()) {
120 /* This works only with emacsclient version >= 22. */
121 struct strbuf man_page
= STRBUF_INIT
;
124 path
= "emacsclient";
125 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
126 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
127 warning("failed to exec '%s': %s", path
, strerror(errno
));
131 static void exec_man_konqueror(const char* path
, const char *page
)
133 const char *display
= getenv("DISPLAY");
134 if (display
&& *display
) {
135 struct strbuf man_page
= STRBUF_INIT
;
136 const char *filename
= "kfmclient";
138 /* It's simpler to launch konqueror using kfmclient. */
140 const char *file
= strrchr(path
, '/');
141 if (file
&& !strcmp(file
+ 1, "konqueror")) {
142 char *new = xstrdup(path
);
143 char *dest
= strrchr(new, '/');
145 /* strlen("konqueror") == strlen("kfmclient") */
146 strcpy(dest
+ 1, "kfmclient");
153 strbuf_addf(&man_page
, "man:%s(1)", page
);
154 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
155 warning("failed to exec '%s': %s", path
, strerror(errno
));
159 static void exec_man_man(const char* path
, const char *page
)
163 execlp(path
, "man", page
, NULL
);
164 warning("failed to exec '%s': %s", path
, strerror(errno
));
167 static void exec_man_cmd(const char *cmd
, const char *page
)
169 struct strbuf shell_cmd
= STRBUF_INIT
;
170 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
171 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, NULL
);
172 warning("failed to exec '%s': %s", cmd
, strerror(errno
));
175 static void add_man_viewer(const char *name
)
177 struct man_viewer_list
**p
= &man_viewer_list
;
178 size_t len
= strlen(name
);
182 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
183 strncpy((*p
)->name
, name
, len
);
186 static int supported_man_viewer(const char *name
, size_t len
)
188 return (!strncasecmp("man", name
, len
) ||
189 !strncasecmp("woman", name
, len
) ||
190 !strncasecmp("konqueror", name
, len
));
193 static void do_add_man_viewer_info(const char *name
,
197 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
199 strncpy(new->name
, name
, len
);
200 new->info
= xstrdup(value
);
201 new->next
= man_viewer_info_list
;
202 man_viewer_info_list
= new;
205 static int add_man_viewer_path(const char *name
,
209 if (supported_man_viewer(name
, len
))
210 do_add_man_viewer_info(name
, len
, value
);
212 warning("'%s': path for unsupported man viewer.\n"
213 "Please consider using 'man.<tool>.cmd' instead.",
219 static int add_man_viewer_cmd(const char *name
,
223 if (supported_man_viewer(name
, len
))
224 warning("'%s': cmd for supported man viewer.\n"
225 "Please consider using 'man.<tool>.path' instead.",
228 do_add_man_viewer_info(name
, len
, value
);
233 static int add_man_viewer_info(const char *var
, const char *value
)
235 const char *name
= var
+ 4;
236 const char *subkey
= strrchr(name
, '.');
239 return error("Config with no key for man viewer: %s", name
);
241 if (!strcmp(subkey
, ".path")) {
243 return config_error_nonbool(var
);
244 return add_man_viewer_path(name
, subkey
- name
, value
);
246 if (!strcmp(subkey
, ".cmd")) {
248 return config_error_nonbool(var
);
249 return add_man_viewer_cmd(name
, subkey
- name
, value
);
252 warning("'%s': unsupported man viewer sub key.", subkey
);
256 static int git_help_config(const char *var
, const char *value
, void *cb
)
258 if (!strcmp(var
, "help.format")) {
260 return config_error_nonbool(var
);
261 help_format
= parse_help_format(value
);
264 if (!strcmp(var
, "man.viewer")) {
266 return config_error_nonbool(var
);
267 add_man_viewer(value
);
270 if (!prefixcmp(var
, "man."))
271 return add_man_viewer_info(var
, value
);
273 return git_default_config(var
, value
, cb
);
276 /* most GUI terminals set COLUMNS (although some don't export it) */
277 static int term_columns(void)
279 char *col_string
= getenv("COLUMNS");
282 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
288 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
298 static inline void mput_char(char c
, unsigned int num
)
304 struct cmdnames main_cmds
, other_cmds
;
306 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
308 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
+ 1);
311 memcpy(ent
->name
, name
, len
);
314 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
315 cmds
->names
[cmds
->cnt
++] = ent
;
318 static int cmdname_compare(const void *a_
, const void *b_
)
320 struct cmdname
*a
= *(struct cmdname
**)a_
;
321 struct cmdname
*b
= *(struct cmdname
**)b_
;
322 return strcmp(a
->name
, b
->name
);
325 static void uniq(struct cmdnames
*cmds
)
332 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
333 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
334 cmds
->names
[j
++] = cmds
->names
[i
];
339 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
345 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
346 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
348 cmds
->names
[cj
++] = cmds
->names
[ci
++];
355 while (ci
< cmds
->cnt
)
356 cmds
->names
[cj
++] = cmds
->names
[ci
++];
361 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
364 int space
= longest
+ 1; /* min 1 SP between words */
365 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
368 if (space
< max_cols
)
369 cols
= max_cols
/ space
;
370 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
372 for (i
= 0; i
< rows
; i
++) {
375 for (j
= 0; j
< cols
; j
++) {
376 int n
= j
* rows
+ i
;
380 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
382 printf("%-*s", size
, cmds
->names
[n
]->name
);
388 static int is_executable(const char *name
)
392 if (stat(name
, &st
) || /* stat, not lstat */
393 !S_ISREG(st
.st_mode
))
397 /* cannot trust the executable bit, peek into the file instead */
400 int fd
= open(name
, O_RDONLY
);
401 st
.st_mode
&= ~S_IXUSR
;
403 n
= read(fd
, buf
, 2);
405 /* DOS executables start with "MZ" */
406 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
407 st
.st_mode
|= S_IXUSR
;
411 return st
.st_mode
& S_IXUSR
;
414 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
418 unsigned int longest
= 0;
420 DIR *dir
= opendir(path
);
422 struct strbuf buf
= STRBUF_INIT
;
429 prefix_len
= strlen(prefix
);
431 strbuf_addf(&buf
, "%s/", path
);
434 while ((de
= readdir(dir
)) != NULL
) {
437 if (prefixcmp(de
->d_name
, prefix
))
440 strbuf_setlen(&buf
, len
);
441 strbuf_addstr(&buf
, de
->d_name
);
442 if (!is_executable(buf
.buf
))
445 entlen
= strlen(de
->d_name
) - prefix_len
;
446 if (has_extension(de
->d_name
, ".exe"))
449 if (longest
< entlen
)
452 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
455 strbuf_release(&buf
);
460 unsigned int load_command_list(const char *prefix
,
461 struct cmdnames
*main_cmds
,
462 struct cmdnames
*other_cmds
)
464 unsigned int longest
= 0;
466 const char *env_path
= getenv("PATH");
467 char *paths
, *path
, *colon
;
468 const char *exec_path
= git_exec_path();
471 longest
= list_commands_in_dir(main_cmds
, exec_path
, prefix
);
474 fprintf(stderr
, "PATH not set\n");
478 path
= paths
= xstrdup(env_path
);
480 if ((colon
= strchr(path
, PATH_SEP
)))
483 len
= list_commands_in_dir(other_cmds
, path
, prefix
);
493 qsort(main_cmds
->names
, main_cmds
->cnt
,
494 sizeof(*main_cmds
->names
), cmdname_compare
);
497 qsort(other_cmds
->names
, other_cmds
->cnt
,
498 sizeof(*other_cmds
->names
), cmdname_compare
);
500 exclude_cmds(other_cmds
, main_cmds
);
505 void list_commands(const char *title
, unsigned int longest
,
506 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
508 const char *exec_path
= git_exec_path();
510 if (main_cmds
->cnt
) {
511 printf("available %s in '%s'\n", title
, exec_path
);
512 printf("----------------");
513 mput_char('-', strlen(title
) + strlen(exec_path
));
515 pretty_print_string_list(main_cmds
, longest
);
519 if (other_cmds
->cnt
) {
520 printf("%s available from elsewhere on your $PATH\n", title
);
521 printf("---------------------------------------");
522 mput_char('-', strlen(title
));
524 pretty_print_string_list(other_cmds
, longest
);
529 void list_common_cmds_help(void)
533 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
534 if (longest
< strlen(common_cmds
[i
].name
))
535 longest
= strlen(common_cmds
[i
].name
);
538 puts("The most commonly used git commands are:");
539 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
540 printf(" %s ", common_cmds
[i
].name
);
541 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
542 puts(common_cmds
[i
].help
);
546 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
549 for (i
= 0; i
< c
->cnt
; i
++)
550 if (!strcmp(s
, c
->names
[i
]->name
))
555 static int is_git_command(const char *s
)
557 return is_in_cmdlist(&main_cmds
, s
) ||
558 is_in_cmdlist(&other_cmds
, s
);
561 static const char *prepend(const char *prefix
, const char *cmd
)
563 size_t pre_len
= strlen(prefix
);
564 size_t cmd_len
= strlen(cmd
);
565 char *p
= xmalloc(pre_len
+ cmd_len
+ 1);
566 memcpy(p
, prefix
, pre_len
);
567 strcpy(p
+ pre_len
, cmd
);
571 static const char *cmd_to_page(const char *git_cmd
)
575 else if (!prefixcmp(git_cmd
, "git"))
577 else if (is_git_command(git_cmd
))
578 return prepend("git-", git_cmd
);
580 return prepend("git", git_cmd
);
583 static void setup_man_path(void)
585 struct strbuf new_path
;
586 const char *old_path
= getenv("MANPATH");
588 strbuf_init(&new_path
, 0);
590 /* We should always put ':' after our path. If there is no
591 * old_path, the ':' at the end will let 'man' to try
592 * system-wide paths after ours to find the manual page. If
593 * there is old_path, we need ':' as delimiter. */
594 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
595 strbuf_addch(&new_path
, ':');
597 strbuf_addstr(&new_path
, old_path
);
599 setenv("MANPATH", new_path
.buf
, 1);
601 strbuf_release(&new_path
);
604 static void exec_viewer(const char *name
, const char *page
)
606 const char *info
= get_man_viewer_info(name
);
608 if (!strcasecmp(name
, "man"))
609 exec_man_man(info
, page
);
610 else if (!strcasecmp(name
, "woman"))
611 exec_woman_emacs(info
, page
);
612 else if (!strcasecmp(name
, "konqueror"))
613 exec_man_konqueror(info
, page
);
615 exec_man_cmd(info
, page
);
617 warning("'%s': unknown man viewer.", name
);
620 static void show_man_page(const char *git_cmd
)
622 struct man_viewer_list
*viewer
;
623 const char *page
= cmd_to_page(git_cmd
);
626 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
628 exec_viewer(viewer
->name
, page
); /* will return when unable */
630 exec_viewer("man", page
);
631 die("no man viewer handled the request");
634 static void show_info_page(const char *git_cmd
)
636 const char *page
= cmd_to_page(git_cmd
);
637 setenv("INFOPATH", GIT_INFO_PATH
, 1);
638 execlp("info", "info", "gitman", page
, NULL
);
641 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
644 const char *html_path
= system_path(GIT_HTML_PATH
);
646 /* Check that we have a git documentation directory. */
647 if (stat(mkpath("%s/git.html", html_path
), &st
)
648 || !S_ISREG(st
.st_mode
))
649 die("'%s': not a documentation directory.", html_path
);
651 strbuf_init(page_path
, 0);
652 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
656 * If open_html is not defined in a platform-specific way (see for
657 * example compat/mingw.h), we use the script web--browse to display
661 void open_html(const char *path
)
663 execl_git_cmd("web--browse", "-c", "help.browser", path
, NULL
);
667 static void show_html_page(const char *git_cmd
)
669 const char *page
= cmd_to_page(git_cmd
);
670 struct strbuf page_path
; /* it leaks but we exec bellow */
672 get_html_page_path(&page_path
, page
);
674 open_html(page_path
.buf
);
677 void help_unknown_cmd(const char *cmd
)
679 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
683 int cmd_version(int argc
, const char **argv
, const char *prefix
)
685 printf("git version %s\n", git_version_string
);
689 int cmd_help(int argc
, const char **argv
, const char *prefix
)
693 unsigned int longest
= load_command_list("git-", &main_cmds
, &other_cmds
);
695 setup_git_directory_gently(&nongit
);
696 git_config(git_help_config
, NULL
);
698 argc
= parse_options(argc
, argv
, builtin_help_options
,
699 builtin_help_usage
, 0);
702 printf("usage: %s\n\n", git_usage_string
);
703 list_commands("git commands", longest
, &main_cmds
, &other_cmds
);
704 printf("%s\n", git_more_info_string
);
709 printf("usage: %s\n\n", git_usage_string
);
710 list_common_cmds_help();
711 printf("\n%s\n", git_more_info_string
);
715 alias
= alias_lookup(argv
[0]);
716 if (alias
&& !is_git_command(argv
[0])) {
717 printf("`git %s' is aliased to `%s'\n", argv
[0], alias
);
721 switch (help_format
) {
722 case HELP_FORMAT_MAN
:
723 show_man_page(argv
[0]);
725 case HELP_FORMAT_INFO
:
726 show_info_page(argv
[0]);
728 case HELP_FORMAT_WEB
:
729 show_html_page(argv
[0]);