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 struct man_viewer_list
*next
;
15 char name
[FLEX_ARRAY
];
18 static struct man_viewer_info_list
{
19 struct man_viewer_info_list
*next
;
21 char name
[FLEX_ARRAY
];
22 } *man_viewer_info_list
;
30 static int show_all
= 0;
31 static enum help_format help_format
= HELP_FORMAT_MAN
;
32 static struct option builtin_help_options
[] = {
33 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
34 OPT_SET_INT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
35 OPT_SET_INT('w', "web", &help_format
, "show manual in web browser",
37 OPT_SET_INT('i', "info", &help_format
, "show info page",
42 static const char * const builtin_help_usage
[] = {
43 "git-help [--all] [--man|--web|--info] [command]",
47 static enum help_format
parse_help_format(const char *format
)
49 if (!strcmp(format
, "man"))
50 return HELP_FORMAT_MAN
;
51 if (!strcmp(format
, "info"))
52 return HELP_FORMAT_INFO
;
53 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
54 return HELP_FORMAT_WEB
;
55 die("unrecognized help format '%s'", format
);
58 static const char *get_man_viewer_info(const char *name
)
60 struct man_viewer_info_list
*viewer
;
62 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
)
64 if (!strcasecmp(name
, viewer
->name
))
70 static int check_emacsclient_version(void)
72 struct strbuf buffer
= STRBUF_INIT
;
73 struct child_process ec_process
;
74 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
77 /* emacsclient prints its version number on stderr */
78 memset(&ec_process
, 0, sizeof(ec_process
));
79 ec_process
.argv
= argv_ec
;
81 ec_process
.stdout_to_stderr
= 1;
82 if (start_command(&ec_process
)) {
83 fprintf(stderr
, "Failed to start emacsclient.\n");
86 strbuf_read(&buffer
, ec_process
.err
, 20);
87 close(ec_process
.err
);
90 * Don't bother checking return value, because "emacsclient --version"
91 * seems to always exits with code 1.
93 finish_command(&ec_process
);
95 if (prefixcmp(buffer
.buf
, "emacsclient")) {
96 fprintf(stderr
, "Failed to parse emacsclient version.\n");
97 strbuf_release(&buffer
);
101 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
102 version
= atoi(buffer
.buf
);
106 "emacsclient version '%d' too old (< 22).\n",
108 strbuf_release(&buffer
);
112 strbuf_release(&buffer
);
116 static void exec_woman_emacs(const char* path
, const char *page
)
118 if (!check_emacsclient_version()) {
119 /* This works only with emacsclient version >= 22. */
120 struct strbuf man_page
= STRBUF_INIT
;
123 path
= "emacsclient";
124 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
125 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
126 warning("failed to exec '%s': %s", path
, strerror(errno
));
130 static void exec_man_konqueror(const char* path
, const char *page
)
132 const char *display
= getenv("DISPLAY");
133 if (display
&& *display
) {
134 struct strbuf man_page
= STRBUF_INIT
;
135 const char *filename
= "kfmclient";
137 /* It's simpler to launch konqueror using kfmclient. */
139 const char *file
= strrchr(path
, '/');
140 if (file
&& !strcmp(file
+ 1, "konqueror")) {
141 char *new = xstrdup(path
);
142 char *dest
= strrchr(new, '/');
144 /* strlen("konqueror") == strlen("kfmclient") */
145 strcpy(dest
+ 1, "kfmclient");
152 strbuf_addf(&man_page
, "man:%s(1)", page
);
153 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
154 warning("failed to exec '%s': %s", path
, strerror(errno
));
158 static void exec_man_man(const char* path
, const char *page
)
162 execlp(path
, "man", page
, NULL
);
163 warning("failed to exec '%s': %s", path
, strerror(errno
));
166 static void do_add_man_viewer(const char *name
)
168 struct man_viewer_list
**p
= &man_viewer_list
;
169 size_t len
= strlen(name
);
173 *p
= xcalloc(1, (sizeof(**p
) + len
+ 1));
174 strncpy((*p
)->name
, name
, len
);
177 static int supported_man_viewer(const char *name
, size_t len
)
179 return (!strncasecmp("man", name
, len
) ||
180 !strncasecmp("woman", name
, len
) ||
181 !strncasecmp("konqueror", name
, len
));
184 static int add_man_viewer(const char *value
)
186 if (supported_man_viewer(value
, strlen(value
)))
187 do_add_man_viewer(value
);
189 warning("'%s': unsupported man viewer.", value
);
194 static void do_add_man_viewer_info(const char *name
,
198 struct man_viewer_info_list
*new = xcalloc(1, sizeof(*new) + len
+ 1);
200 strncpy(new->name
, name
, len
);
201 new->info
= xstrdup(value
);
202 new->next
= man_viewer_info_list
;
203 man_viewer_info_list
= new;
206 static int add_man_viewer_path(const char *name
,
210 if (supported_man_viewer(name
, len
))
211 do_add_man_viewer_info(name
, len
, value
);
213 warning("'%s': path for unsupported man viewer.", name
);
218 static int add_man_viewer_info(const char *var
, const char *value
)
220 const char *name
= var
+ 4;
221 const char *subkey
= strrchr(name
, '.');
224 return error("Config with no key for man viewer: %s", name
);
226 if (!strcmp(subkey
, ".path")) {
228 return config_error_nonbool(var
);
229 return add_man_viewer_path(name
, subkey
- name
, value
);
232 warning("'%s': unsupported man viewer sub key.", subkey
);
236 static int git_help_config(const char *var
, const char *value
)
238 if (!strcmp(var
, "help.format")) {
240 return config_error_nonbool(var
);
241 help_format
= parse_help_format(value
);
244 if (!strcmp(var
, "man.viewer")) {
246 return config_error_nonbool(var
);
247 return add_man_viewer(value
);
249 if (!prefixcmp(var
, "man."))
250 return add_man_viewer_info(var
, value
);
252 return git_default_config(var
, value
);
255 /* most GUI terminals set COLUMNS (although some don't export it) */
256 static int term_columns(void)
258 char *col_string
= getenv("COLUMNS");
261 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
267 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
277 static inline void mput_char(char c
, unsigned int num
)
283 static struct cmdnames
{
290 } main_cmds
, other_cmds
;
292 static void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
294 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
);
297 memcpy(ent
->name
, name
, len
);
300 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
301 cmds
->names
[cmds
->cnt
++] = ent
;
304 static int cmdname_compare(const void *a_
, const void *b_
)
306 struct cmdname
*a
= *(struct cmdname
**)a_
;
307 struct cmdname
*b
= *(struct cmdname
**)b_
;
308 return strcmp(a
->name
, b
->name
);
311 static void uniq(struct cmdnames
*cmds
)
318 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
319 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
320 cmds
->names
[j
++] = cmds
->names
[i
];
325 static void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
331 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
332 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
334 cmds
->names
[cj
++] = cmds
->names
[ci
++];
341 while (ci
< cmds
->cnt
)
342 cmds
->names
[cj
++] = cmds
->names
[ci
++];
347 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
350 int space
= longest
+ 1; /* min 1 SP between words */
351 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
354 if (space
< max_cols
)
355 cols
= max_cols
/ space
;
356 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
358 for (i
= 0; i
< rows
; i
++) {
361 for (j
= 0; j
< cols
; j
++) {
362 int n
= j
* rows
+ i
;
366 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
368 printf("%-*s", size
, cmds
->names
[n
]->name
);
374 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
377 unsigned int longest
= 0;
378 const char *prefix
= "git-";
379 int prefix_len
= strlen(prefix
);
380 DIR *dir
= opendir(path
);
383 if (!dir
|| chdir(path
))
386 while ((de
= readdir(dir
)) != NULL
) {
390 if (prefixcmp(de
->d_name
, prefix
))
393 if (stat(de
->d_name
, &st
) || /* stat, not lstat */
394 !S_ISREG(st
.st_mode
) ||
395 !(st
.st_mode
& S_IXUSR
))
398 entlen
= strlen(de
->d_name
) - prefix_len
;
399 if (has_extension(de
->d_name
, ".exe"))
402 if (longest
< entlen
)
405 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
412 static unsigned int load_command_list(void)
414 unsigned int longest
= 0;
416 const char *env_path
= getenv("PATH");
417 char *paths
, *path
, *colon
;
418 const char *exec_path
= git_exec_path();
421 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
424 fprintf(stderr
, "PATH not set\n");
428 path
= paths
= xstrdup(env_path
);
430 if ((colon
= strchr(path
, ':')))
433 len
= list_commands_in_dir(&other_cmds
, path
);
443 qsort(main_cmds
.names
, main_cmds
.cnt
,
444 sizeof(*main_cmds
.names
), cmdname_compare
);
447 qsort(other_cmds
.names
, other_cmds
.cnt
,
448 sizeof(*other_cmds
.names
), cmdname_compare
);
450 exclude_cmds(&other_cmds
, &main_cmds
);
455 static void list_commands(void)
457 unsigned int longest
= load_command_list();
458 const char *exec_path
= git_exec_path();
461 printf("available git commands in '%s'\n", exec_path
);
462 printf("----------------------------");
463 mput_char('-', strlen(exec_path
));
465 pretty_print_string_list(&main_cmds
, longest
);
469 if (other_cmds
.cnt
) {
470 printf("git commands available from elsewhere on your $PATH\n");
471 printf("---------------------------------------------------\n");
472 pretty_print_string_list(&other_cmds
, longest
);
477 void list_common_cmds_help(void)
481 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
482 if (longest
< strlen(common_cmds
[i
].name
))
483 longest
= strlen(common_cmds
[i
].name
);
486 puts("The most commonly used git commands are:");
487 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
488 printf(" %s ", common_cmds
[i
].name
);
489 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
490 puts(common_cmds
[i
].help
);
494 static int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
497 for (i
= 0; i
< c
->cnt
; i
++)
498 if (!strcmp(s
, c
->names
[i
]->name
))
503 static int is_git_command(const char *s
)
506 return is_in_cmdlist(&main_cmds
, s
) ||
507 is_in_cmdlist(&other_cmds
, s
);
510 static const char *cmd_to_page(const char *git_cmd
)
514 else if (!prefixcmp(git_cmd
, "git"))
517 int page_len
= strlen(git_cmd
) + 4;
518 char *p
= xmalloc(page_len
+ 1);
520 strcpy(p
+ 4, git_cmd
);
526 static void setup_man_path(void)
528 struct strbuf new_path
;
529 const char *old_path
= getenv("MANPATH");
531 strbuf_init(&new_path
, 0);
533 /* We should always put ':' after our path. If there is no
534 * old_path, the ':' at the end will let 'man' to try
535 * system-wide paths after ours to find the manual page. If
536 * there is old_path, we need ':' as delimiter. */
537 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
538 strbuf_addch(&new_path
, ':');
540 strbuf_addstr(&new_path
, old_path
);
542 setenv("MANPATH", new_path
.buf
, 1);
544 strbuf_release(&new_path
);
547 static void exec_viewer(const char *name
, const char *page
)
549 const char *path
= get_man_viewer_info(name
);
551 if (!strcasecmp(name
, "man"))
552 exec_man_man(path
, page
);
553 else if (!strcasecmp(name
, "woman"))
554 exec_woman_emacs(path
, page
);
555 else if (!strcasecmp(name
, "konqueror"))
556 exec_man_konqueror(path
, page
);
558 warning("'%s': unsupported man viewer.", name
);
561 static void show_man_page(const char *git_cmd
)
563 struct man_viewer_list
*viewer
;
564 const char *page
= cmd_to_page(git_cmd
);
567 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
569 exec_viewer(viewer
->name
, page
); /* will return when unable */
571 exec_viewer("man", page
);
572 die("no man viewer handled the request");
575 static void show_info_page(const char *git_cmd
)
577 const char *page
= cmd_to_page(git_cmd
);
578 setenv("INFOPATH", GIT_INFO_PATH
, 1);
579 execlp("info", "info", "gitman", page
, NULL
);
582 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
586 /* Check that we have a git documentation directory. */
587 if (stat(GIT_HTML_PATH
"/git.html", &st
) || !S_ISREG(st
.st_mode
))
588 die("'%s': not a documentation directory.", GIT_HTML_PATH
);
590 strbuf_init(page_path
, 0);
591 strbuf_addf(page_path
, GIT_HTML_PATH
"/%s.html", page
);
594 static void show_html_page(const char *git_cmd
)
596 const char *page
= cmd_to_page(git_cmd
);
597 struct strbuf page_path
; /* it leaks but we exec bellow */
599 get_html_page_path(&page_path
, page
);
601 execl_git_cmd("web--browse", "-c", "help.browser", page_path
.buf
, NULL
);
604 void help_unknown_cmd(const char *cmd
)
606 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
610 int cmd_version(int argc
, const char **argv
, const char *prefix
)
612 printf("git version %s\n", git_version_string
);
616 int cmd_help(int argc
, const char **argv
, const char *prefix
)
621 setup_git_directory_gently(&nongit
);
622 git_config(git_help_config
);
624 argc
= parse_options(argc
, argv
, builtin_help_options
,
625 builtin_help_usage
, 0);
628 printf("usage: %s\n\n", git_usage_string
);
634 printf("usage: %s\n\n", git_usage_string
);
635 list_common_cmds_help();
639 alias
= alias_lookup(argv
[0]);
640 if (alias
&& !is_git_command(argv
[0])) {
641 printf("`git %s' is aliased to `%s'\n", argv
[0], alias
);
645 switch (help_format
) {
646 case HELP_FORMAT_MAN
:
647 show_man_page(argv
[0]);
649 case HELP_FORMAT_INFO
:
650 show_info_page(argv
[0]);
652 case HELP_FORMAT_WEB
:
653 show_html_page(argv
[0]);