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 int is_executable(const char *name
)
287 if (stat(name
, &st
) || /* stat, not lstat */
288 !S_ISREG(st
.st_mode
))
292 /* cannot trust the executable bit, peek into the file instead */
295 int fd
= open(name
, O_RDONLY
);
296 st
.st_mode
&= ~S_IXUSR
;
298 n
= read(fd
, buf
, 2);
300 /* DOS executables start with "MZ" */
301 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
302 st
.st_mode
|= S_IXUSR
;
306 return st
.st_mode
& S_IXUSR
;
309 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
312 unsigned int longest
= 0;
313 const char *prefix
= "git-";
314 int prefix_len
= strlen(prefix
);
315 DIR *dir
= opendir(path
);
318 if (!dir
|| chdir(path
))
321 while ((de
= readdir(dir
)) != NULL
) {
324 if (prefixcmp(de
->d_name
, prefix
))
327 if (!is_executable(de
->d_name
))
330 entlen
= strlen(de
->d_name
) - prefix_len
;
331 if (has_extension(de
->d_name
, ".exe"))
334 if (longest
< entlen
)
337 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
344 static unsigned int load_command_list(void)
346 unsigned int longest
= 0;
348 const char *env_path
= getenv("PATH");
349 char *paths
, *path
, *colon
;
350 const char *exec_path
= git_exec_path();
353 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
356 fprintf(stderr
, "PATH not set\n");
360 path
= paths
= xstrdup(env_path
);
362 if ((colon
= strchr(path
, PATH_SEP
)))
365 len
= list_commands_in_dir(&other_cmds
, path
);
375 qsort(main_cmds
.names
, main_cmds
.cnt
,
376 sizeof(*main_cmds
.names
), cmdname_compare
);
379 qsort(other_cmds
.names
, other_cmds
.cnt
,
380 sizeof(*other_cmds
.names
), cmdname_compare
);
382 exclude_cmds(&other_cmds
, &main_cmds
);
387 static void list_commands(void)
389 unsigned int longest
= load_command_list();
390 const char *exec_path
= git_exec_path();
393 printf("available git commands in '%s'\n", exec_path
);
394 printf("----------------------------");
395 mput_char('-', strlen(exec_path
));
397 pretty_print_string_list(&main_cmds
, longest
);
401 if (other_cmds
.cnt
) {
402 printf("git commands available from elsewhere on your $PATH\n");
403 printf("---------------------------------------------------\n");
404 pretty_print_string_list(&other_cmds
, longest
);
409 void list_common_cmds_help(void)
413 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
414 if (longest
< strlen(common_cmds
[i
].name
))
415 longest
= strlen(common_cmds
[i
].name
);
418 puts("The most commonly used git commands are:");
419 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
420 printf(" %s ", common_cmds
[i
].name
);
421 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
422 puts(common_cmds
[i
].help
);
426 static int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
429 for (i
= 0; i
< c
->cnt
; i
++)
430 if (!strcmp(s
, c
->names
[i
]->name
))
435 static int is_git_command(const char *s
)
438 return is_in_cmdlist(&main_cmds
, s
) ||
439 is_in_cmdlist(&other_cmds
, s
);
442 static const char *cmd_to_page(const char *git_cmd
)
446 else if (!prefixcmp(git_cmd
, "git"))
449 int page_len
= strlen(git_cmd
) + 4;
450 char *p
= xmalloc(page_len
+ 1);
452 strcpy(p
+ 4, git_cmd
);
458 static void setup_man_path(void)
460 struct strbuf new_path
;
461 const char *old_path
= getenv("MANPATH");
463 strbuf_init(&new_path
, 0);
465 /* We should always put ':' after our path. If there is no
466 * old_path, the ':' at the end will let 'man' to try
467 * system-wide paths after ours to find the manual page. If
468 * there is old_path, we need ':' as delimiter. */
469 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
470 strbuf_addch(&new_path
, ':');
472 strbuf_addstr(&new_path
, old_path
);
474 setenv("MANPATH", new_path
.buf
, 1);
476 strbuf_release(&new_path
);
479 static void show_man_page(const char *git_cmd
)
481 struct man_viewer_list
*viewer
;
482 const char *page
= cmd_to_page(git_cmd
);
485 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
487 viewer
->exec(page
); /* will return when unable */
490 die("no man viewer handled the request");
493 static void show_info_page(const char *git_cmd
)
495 const char *page
= cmd_to_page(git_cmd
);
496 setenv("INFOPATH", GIT_INFO_PATH
, 1);
497 execlp("info", "info", "gitman", page
, NULL
);
500 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
504 /* Check that we have a git documentation directory. */
505 if (stat(GIT_HTML_PATH
"/git.html", &st
) || !S_ISREG(st
.st_mode
))
506 die("'%s': not a documentation directory.", GIT_HTML_PATH
);
508 strbuf_init(page_path
, 0);
509 strbuf_addf(page_path
, GIT_HTML_PATH
"/%s.html", page
);
512 static void show_html_page(const char *git_cmd
)
514 const char *page
= cmd_to_page(git_cmd
);
515 struct strbuf page_path
; /* it leaks but we exec bellow */
517 get_html_page_path(&page_path
, page
);
519 execl_git_cmd("web--browse", "-c", "help.browser", page_path
.buf
, NULL
);
522 void help_unknown_cmd(const char *cmd
)
524 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
528 int cmd_version(int argc
, const char **argv
, const char *prefix
)
530 printf("git version %s\n", git_version_string
);
534 int cmd_help(int argc
, const char **argv
, const char *prefix
)
539 setup_git_directory_gently(&nongit
);
540 git_config(git_help_config
);
542 argc
= parse_options(argc
, argv
, builtin_help_options
,
543 builtin_help_usage
, 0);
546 printf("usage: %s\n\n", git_usage_string
);
552 printf("usage: %s\n\n", git_usage_string
);
553 list_common_cmds_help();
557 alias
= alias_lookup(argv
[0]);
558 if (alias
&& !is_git_command(argv
[0])) {
559 printf("`git %s' is aliased to `%s'\n", argv
[0], alias
);
563 switch (help_format
) {
564 case HELP_FORMAT_MAN
:
565 show_man_page(argv
[0]);
567 case HELP_FORMAT_INFO
:
568 show_info_page(argv
[0]);
570 case HELP_FORMAT_WEB
:
571 show_html_page(argv
[0]);