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;
33 static enum help_format help_format
= HELP_FORMAT_WEB
;
35 static enum help_format help_format
= HELP_FORMAT_MAN
;
37 static struct option builtin_help_options
[] = {
38 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
39 OPT_SET_INT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
40 OPT_SET_INT('w', "web", &help_format
, "show manual in web browser",
42 OPT_SET_INT('i', "info", &help_format
, "show info page",
47 static const char * const builtin_help_usage
[] = {
48 "git-help [--all] [--man|--web|--info] [command]",
52 static enum help_format
parse_help_format(const char *format
)
54 if (!strcmp(format
, "man"))
55 return HELP_FORMAT_MAN
;
56 if (!strcmp(format
, "info"))
57 return HELP_FORMAT_INFO
;
58 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
59 return HELP_FORMAT_WEB
;
60 die("unrecognized help format '%s'", format
);
63 static const char *get_man_viewer_info(const char *name
)
65 struct man_viewer_info_list
*viewer
;
67 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
69 if (!strcasecmp(name
, viewer
->name
))
75 static int check_emacsclient_version(void)
77 struct strbuf buffer
= STRBUF_INIT
;
78 struct child_process ec_process
;
79 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
82 /* emacsclient prints its version number on stderr */
83 memset(&ec_process
, 0, sizeof(ec_process
));
84 ec_process
.argv
= argv_ec
;
86 ec_process
.stdout_to_stderr
= 1;
87 if (start_command(&ec_process
)) {
88 fprintf(stderr
, "Failed to start emacsclient.\n");
91 strbuf_read(&buffer
, ec_process
.err
, 20);
92 close(ec_process
.err
);
95 * Don't bother checking return value, because "emacsclient --version"
96 * seems to always exits with code 1.
98 finish_command(&ec_process
);
100 if (prefixcmp(buffer
.buf
, "emacsclient")) {
101 fprintf(stderr
, "Failed to parse emacsclient version.\n");
102 strbuf_release(&buffer
);
106 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
107 version
= atoi(buffer
.buf
);
111 "emacsclient version '%d' too old (< 22).\n",
113 strbuf_release(&buffer
);
117 strbuf_release(&buffer
);
121 static void exec_woman_emacs(const char* path
, const char *page
)
123 if (!check_emacsclient_version()) {
124 /* This works only with emacsclient version >= 22. */
125 struct strbuf man_page
= STRBUF_INIT
;
128 path
= "emacsclient";
129 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
130 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
131 warning("failed to exec '%s': %s", path
, strerror(errno
));
135 static void exec_man_konqueror(const char* path
, const char *page
)
137 const char *display
= getenv("DISPLAY");
138 if (display
&& *display
) {
139 struct strbuf man_page
= STRBUF_INIT
;
140 const char *filename
= "kfmclient";
142 /* It's simpler to launch konqueror using kfmclient. */
144 const char *file
= strrchr(path
, '/');
145 if (file
&& !strcmp(file
+ 1, "konqueror")) {
146 char *new = xstrdup(path
);
147 char *dest
= strrchr(new, '/');
149 /* strlen("konqueror") == strlen("kfmclient") */
150 strcpy(dest
+ 1, "kfmclient");
157 strbuf_addf(&man_page
, "man:%s(1)", page
);
158 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
159 warning("failed to exec '%s': %s", path
, strerror(errno
));
163 static void exec_man_man(const char* path
, const char *page
)
167 execlp(path
, "man", page
, NULL
);
168 warning("failed to exec '%s': %s", path
, strerror(errno
));
171 static void exec_man_cmd(const char *cmd
, const char *page
)
173 struct strbuf shell_cmd
= STRBUF_INIT
;
174 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
175 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, NULL
);
176 warning("failed to exec '%s': %s", cmd
, strerror(errno
));
179 static void add_man_viewer(const char *name
)
181 struct man_viewer_list
**p
= &man_viewer_list
;
182 size_t len
= strlen(name
);
186 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
187 strncpy((*p
)->name
, name
, len
);
190 static int supported_man_viewer(const char *name
, size_t len
)
192 return (!strncasecmp("man", name
, len
) ||
193 !strncasecmp("woman", name
, len
) ||
194 !strncasecmp("konqueror", name
, len
));
197 static void do_add_man_viewer_info(const char *name
,
201 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
203 strncpy(new->name
, name
, len
);
204 new->info
= xstrdup(value
);
205 new->next
= man_viewer_info_list
;
206 man_viewer_info_list
= new;
209 static int add_man_viewer_path(const char *name
,
213 if (supported_man_viewer(name
, len
))
214 do_add_man_viewer_info(name
, len
, value
);
216 warning("'%s': path for unsupported man viewer.\n"
217 "Please consider using 'man.<tool>.cmd' instead.",
223 static int add_man_viewer_cmd(const char *name
,
227 if (supported_man_viewer(name
, len
))
228 warning("'%s': cmd for supported man viewer.\n"
229 "Please consider using 'man.<tool>.path' instead.",
232 do_add_man_viewer_info(name
, len
, value
);
237 static int add_man_viewer_info(const char *var
, const char *value
)
239 const char *name
= var
+ 4;
240 const char *subkey
= strrchr(name
, '.');
243 return error("Config with no key for man viewer: %s", name
);
245 if (!strcmp(subkey
, ".path")) {
247 return config_error_nonbool(var
);
248 return add_man_viewer_path(name
, subkey
- name
, value
);
250 if (!strcmp(subkey
, ".cmd")) {
252 return config_error_nonbool(var
);
253 return add_man_viewer_cmd(name
, subkey
- name
, value
);
256 warning("'%s': unsupported man viewer sub key.", subkey
);
260 static int git_help_config(const char *var
, const char *value
, void *cb
)
262 if (!strcmp(var
, "help.format")) {
264 return config_error_nonbool(var
);
265 help_format
= parse_help_format(value
);
268 if (!strcmp(var
, "man.viewer")) {
270 return config_error_nonbool(var
);
271 add_man_viewer(value
);
274 if (!prefixcmp(var
, "man."))
275 return add_man_viewer_info(var
, value
);
277 return git_default_config(var
, value
, cb
);
280 /* most GUI terminals set COLUMNS (although some don't export it) */
281 static int term_columns(void)
283 char *col_string
= getenv("COLUMNS");
286 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
292 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
302 static inline void mput_char(char c
, unsigned int num
)
308 static struct cmdnames
{
315 } main_cmds
, other_cmds
;
317 static void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
319 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
);
322 memcpy(ent
->name
, name
, len
);
325 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
326 cmds
->names
[cmds
->cnt
++] = ent
;
329 static int cmdname_compare(const void *a_
, const void *b_
)
331 struct cmdname
*a
= *(struct cmdname
**)a_
;
332 struct cmdname
*b
= *(struct cmdname
**)b_
;
333 return strcmp(a
->name
, b
->name
);
336 static void uniq(struct cmdnames
*cmds
)
343 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
344 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
345 cmds
->names
[j
++] = cmds
->names
[i
];
350 static void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
356 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
357 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
359 cmds
->names
[cj
++] = cmds
->names
[ci
++];
366 while (ci
< cmds
->cnt
)
367 cmds
->names
[cj
++] = cmds
->names
[ci
++];
372 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
375 int space
= longest
+ 1; /* min 1 SP between words */
376 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
379 if (space
< max_cols
)
380 cols
= max_cols
/ space
;
381 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
383 for (i
= 0; i
< rows
; i
++) {
386 for (j
= 0; j
< cols
; j
++) {
387 int n
= j
* rows
+ i
;
391 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
393 printf("%-*s", size
, cmds
->names
[n
]->name
);
399 static int is_executable(const char *name
)
403 if (stat(name
, &st
) || /* stat, not lstat */
404 !S_ISREG(st
.st_mode
))
408 /* cannot trust the executable bit, peek into the file instead */
411 int fd
= open(name
, O_RDONLY
);
412 st
.st_mode
&= ~S_IXUSR
;
414 n
= read(fd
, buf
, 2);
416 /* DOS executables start with "MZ" */
417 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
418 st
.st_mode
|= S_IXUSR
;
422 return st
.st_mode
& S_IXUSR
;
425 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
428 unsigned int longest
= 0;
429 const char *prefix
= "git-";
430 int prefix_len
= strlen(prefix
);
431 DIR *dir
= opendir(path
);
434 if (!dir
|| chdir(path
))
437 while ((de
= readdir(dir
)) != NULL
) {
440 if (prefixcmp(de
->d_name
, prefix
))
443 if (!is_executable(de
->d_name
))
446 entlen
= strlen(de
->d_name
) - prefix_len
;
447 if (has_extension(de
->d_name
, ".exe"))
450 if (longest
< entlen
)
453 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
460 static unsigned int load_command_list(void)
462 unsigned int longest
= 0;
464 const char *env_path
= getenv("PATH");
465 char *paths
, *path
, *colon
;
466 const char *exec_path
= git_exec_path();
469 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
472 fprintf(stderr
, "PATH not set\n");
476 path
= paths
= xstrdup(env_path
);
478 if ((colon
= strchr(path
, PATH_SEP
)))
481 len
= list_commands_in_dir(&other_cmds
, path
);
491 qsort(main_cmds
.names
, main_cmds
.cnt
,
492 sizeof(*main_cmds
.names
), cmdname_compare
);
495 qsort(other_cmds
.names
, other_cmds
.cnt
,
496 sizeof(*other_cmds
.names
), cmdname_compare
);
498 exclude_cmds(&other_cmds
, &main_cmds
);
503 static void list_commands(void)
505 unsigned int longest
= load_command_list();
506 const char *exec_path
= git_exec_path();
509 printf("available git commands in '%s'\n", exec_path
);
510 printf("----------------------------");
511 mput_char('-', strlen(exec_path
));
513 pretty_print_string_list(&main_cmds
, longest
);
517 if (other_cmds
.cnt
) {
518 printf("git commands available from elsewhere on your $PATH\n");
519 printf("---------------------------------------------------\n");
520 pretty_print_string_list(&other_cmds
, longest
);
525 void list_common_cmds_help(void)
529 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
530 if (longest
< strlen(common_cmds
[i
].name
))
531 longest
= strlen(common_cmds
[i
].name
);
534 puts("The most commonly used git commands are:");
535 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
536 printf(" %s ", common_cmds
[i
].name
);
537 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
538 puts(common_cmds
[i
].help
);
542 static int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
545 for (i
= 0; i
< c
->cnt
; i
++)
546 if (!strcmp(s
, c
->names
[i
]->name
))
551 static int is_git_command(const char *s
)
554 return is_in_cmdlist(&main_cmds
, s
) ||
555 is_in_cmdlist(&other_cmds
, s
);
558 static const char *cmd_to_page(const char *git_cmd
)
562 else if (!prefixcmp(git_cmd
, "git"))
565 int page_len
= strlen(git_cmd
) + 4;
566 char *p
= xmalloc(page_len
+ 1);
568 strcpy(p
+ 4, git_cmd
);
574 static void setup_man_path(void)
576 struct strbuf new_path
;
577 const char *old_path
= getenv("MANPATH");
579 strbuf_init(&new_path
, 0);
581 /* We should always put ':' after our path. If there is no
582 * old_path, the ':' at the end will let 'man' to try
583 * system-wide paths after ours to find the manual page. If
584 * there is old_path, we need ':' as delimiter. */
585 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
586 strbuf_addch(&new_path
, ':');
588 strbuf_addstr(&new_path
, old_path
);
590 setenv("MANPATH", new_path
.buf
, 1);
592 strbuf_release(&new_path
);
595 static void exec_viewer(const char *name
, const char *page
)
597 const char *info
= get_man_viewer_info(name
);
599 if (!strcasecmp(name
, "man"))
600 exec_man_man(info
, page
);
601 else if (!strcasecmp(name
, "woman"))
602 exec_woman_emacs(info
, page
);
603 else if (!strcasecmp(name
, "konqueror"))
604 exec_man_konqueror(info
, page
);
606 exec_man_cmd(info
, page
);
608 warning("'%s': unknown man viewer.", name
);
611 static void show_man_page(const char *git_cmd
)
613 struct man_viewer_list
*viewer
;
614 const char *page
= cmd_to_page(git_cmd
);
617 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
619 exec_viewer(viewer
->name
, page
); /* will return when unable */
621 exec_viewer("man", page
);
622 die("no man viewer handled the request");
625 static void show_info_page(const char *git_cmd
)
627 const char *page
= cmd_to_page(git_cmd
);
628 setenv("INFOPATH", GIT_INFO_PATH
, 1);
629 execlp("info", "info", "gitman", page
, NULL
);
632 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
636 /* Check that we have a git documentation directory. */
637 if (stat(GIT_HTML_PATH
"/git.html", &st
) || !S_ISREG(st
.st_mode
))
638 die("'%s': not a documentation directory.", GIT_HTML_PATH
);
640 strbuf_init(page_path
, 0);
641 strbuf_addf(page_path
, GIT_HTML_PATH
"/%s.html", page
);
644 static void show_html_page(const char *git_cmd
)
647 const char* exec_path
= git_exec_path();
648 char *htmlpath
= make_native_separator(
649 mkpath("%s/../doc/git/html/%s.html"
653 if (!file_exists(htmlpath
)) {
654 htmlpath
= make_native_separator(
655 mkpath("%s/../doc/git/html/git-%s.html"
659 if (!file_exists(htmlpath
)) {
660 fprintf(stderr
, "Can't find HTML help for '%s'.\n"
665 printf("Launching default browser to display HTML help ...\n");
666 ShellExecute(NULL
, "open", htmlpath
, NULL
, "\\", 0);
668 const char *page
= cmd_to_page(git_cmd
);
669 struct strbuf page_path
; /* it leaks but we exec bellow */
671 get_html_page_path(&page_path
, page
);
673 execl_git_cmd("web--browse", "-c", "help.browser", page_path
.buf
, NULL
);
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
)
694 setup_git_directory_gently(&nongit
);
695 git_config(git_help_config
, NULL
);
697 argc
= parse_options(argc
, argv
, builtin_help_options
,
698 builtin_help_usage
, 0);
701 printf("usage: %s\n\n", git_usage_string
);
703 printf("%s\n", git_more_info_string
);
708 printf("usage: %s\n\n", git_usage_string
);
709 list_common_cmds_help();
710 printf("\n%s\n", git_more_info_string
);
714 alias
= alias_lookup(argv
[0]);
715 if (alias
&& !is_git_command(argv
[0])) {
716 printf("`git %s' is aliased to `%s'\n", argv
[0], alias
);
720 switch (help_format
) {
721 case HELP_FORMAT_MAN
:
722 show_man_page(argv
[0]);
724 case HELP_FORMAT_INFO
:
725 show_info_page(argv
[0]);
727 case HELP_FORMAT_WEB
:
728 show_html_page(argv
[0]);