4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
10 #include "parse-options.h"
18 static int show_all
= 0;
19 static enum help_format help_format
= HELP_FORMAT_MAN
;
20 static struct option builtin_help_options
[] = {
21 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
22 OPT_SET_INT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
23 OPT_SET_INT('w', "web", &help_format
, "show manual in web browser",
25 OPT_SET_INT('i', "info", &help_format
, "show info page",
29 static const char * const builtin_help_usage
[] = {
30 "git-help [--all] [--man|--web|--info] [command]",
34 static enum help_format
parse_help_format(const char *format
)
36 if (!strcmp(format
, "man"))
37 return HELP_FORMAT_MAN
;
38 if (!strcmp(format
, "info"))
39 return HELP_FORMAT_INFO
;
40 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
41 return HELP_FORMAT_WEB
;
42 die("unrecognized help format '%s'", format
);
45 static int git_help_config(const char *var
, const char *value
)
47 if (!strcmp(var
, "help.format")) {
49 return config_error_nonbool(var
);
50 help_format
= parse_help_format(value
);
53 return git_default_config(var
, value
);
56 /* most GUI terminals set COLUMNS (although some don't export it) */
57 static int term_columns(void)
59 char *col_string
= getenv("COLUMNS");
62 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
68 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
78 static inline void mput_char(char c
, unsigned int num
)
84 static struct cmdnames
{
91 } main_cmds
, other_cmds
;
93 static void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
95 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
);
98 memcpy(ent
->name
, name
, len
);
101 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
102 cmds
->names
[cmds
->cnt
++] = ent
;
105 static int cmdname_compare(const void *a_
, const void *b_
)
107 struct cmdname
*a
= *(struct cmdname
**)a_
;
108 struct cmdname
*b
= *(struct cmdname
**)b_
;
109 return strcmp(a
->name
, b
->name
);
112 static void uniq(struct cmdnames
*cmds
)
119 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
120 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
121 cmds
->names
[j
++] = cmds
->names
[i
];
126 static void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
132 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
133 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
135 cmds
->names
[cj
++] = cmds
->names
[ci
++];
142 while (ci
< cmds
->cnt
)
143 cmds
->names
[cj
++] = cmds
->names
[ci
++];
148 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
151 int space
= longest
+ 1; /* min 1 SP between words */
152 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
155 if (space
< max_cols
)
156 cols
= max_cols
/ space
;
157 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
159 for (i
= 0; i
< rows
; i
++) {
162 for (j
= 0; j
< cols
; j
++) {
163 int n
= j
* rows
+ i
;
167 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
169 printf("%-*s", size
, cmds
->names
[n
]->name
);
175 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
178 unsigned int longest
= 0;
179 const char *prefix
= "git-";
180 int prefix_len
= strlen(prefix
);
181 DIR *dir
= opendir(path
);
184 if (!dir
|| chdir(path
))
187 while ((de
= readdir(dir
)) != NULL
) {
191 if (prefixcmp(de
->d_name
, prefix
))
194 if (stat(de
->d_name
, &st
) || /* stat, not lstat */
195 !S_ISREG(st
.st_mode
) ||
196 !(st
.st_mode
& S_IXUSR
))
199 entlen
= strlen(de
->d_name
) - prefix_len
;
200 if (has_extension(de
->d_name
, ".exe"))
203 if (longest
< entlen
)
206 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
213 static unsigned int load_command_list(void)
215 unsigned int longest
= 0;
217 const char *env_path
= getenv("PATH");
218 char *paths
, *path
, *colon
;
219 const char *exec_path
= git_exec_path();
222 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
225 fprintf(stderr
, "PATH not set\n");
229 path
= paths
= xstrdup(env_path
);
231 if ((colon
= strchr(path
, ':')))
234 len
= list_commands_in_dir(&other_cmds
, path
);
244 qsort(main_cmds
.names
, main_cmds
.cnt
,
245 sizeof(*main_cmds
.names
), cmdname_compare
);
248 qsort(other_cmds
.names
, other_cmds
.cnt
,
249 sizeof(*other_cmds
.names
), cmdname_compare
);
251 exclude_cmds(&other_cmds
, &main_cmds
);
256 static void list_commands(void)
258 unsigned int longest
= load_command_list();
259 const char *exec_path
= git_exec_path();
262 printf("available git commands in '%s'\n", exec_path
);
263 printf("----------------------------");
264 mput_char('-', strlen(exec_path
));
266 pretty_print_string_list(&main_cmds
, longest
);
270 if (other_cmds
.cnt
) {
271 printf("git commands available from elsewhere on your $PATH\n");
272 printf("---------------------------------------------------\n");
273 pretty_print_string_list(&other_cmds
, longest
);
278 void list_common_cmds_help(void)
282 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
283 if (longest
< strlen(common_cmds
[i
].name
))
284 longest
= strlen(common_cmds
[i
].name
);
287 puts("The most commonly used git commands are:");
288 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
289 printf(" %s ", common_cmds
[i
].name
);
290 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
291 puts(common_cmds
[i
].help
);
295 static int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
298 for (i
= 0; i
< c
->cnt
; i
++)
299 if (!strcmp(s
, c
->names
[i
]->name
))
304 static int is_git_command(const char *s
)
307 return is_in_cmdlist(&main_cmds
, s
) ||
308 is_in_cmdlist(&other_cmds
, s
);
311 static const char *cmd_to_page(const char *git_cmd
)
315 else if (!prefixcmp(git_cmd
, "git"))
318 int page_len
= strlen(git_cmd
) + 4;
319 char *p
= xmalloc(page_len
+ 1);
321 strcpy(p
+ 4, git_cmd
);
327 static void setup_man_path(void)
329 struct strbuf new_path
;
330 const char *old_path
= getenv("MANPATH");
332 strbuf_init(&new_path
, 0);
334 /* We should always put ':' after our path. If there is no
335 * old_path, the ':' at the end will let 'man' to try
336 * system-wide paths after ours to find the manual page. If
337 * there is old_path, we need ':' as delimiter. */
338 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
339 strbuf_addch(&new_path
, ':');
341 strbuf_addstr(&new_path
, old_path
);
343 setenv("MANPATH", new_path
.buf
, 1);
345 strbuf_release(&new_path
);
348 static void show_man_page(const char *git_cmd
)
350 const char *page
= cmd_to_page(git_cmd
);
352 execlp("man", "man", page
, NULL
);
355 static void show_info_page(const char *git_cmd
)
357 const char *page
= cmd_to_page(git_cmd
);
358 setenv("INFOPATH", GIT_INFO_PATH
, 1);
359 execlp("info", "info", "gitman", page
, NULL
);
362 static void get_html_page_path(struct strbuf
*page_path
, const char *page
)
366 /* Check that we have a git documentation directory. */
367 if (stat(GIT_HTML_PATH
"/git.html", &st
) || !S_ISREG(st
.st_mode
))
368 die("'%s': not a documentation directory.", GIT_HTML_PATH
);
370 strbuf_init(page_path
, 0);
371 strbuf_addf(page_path
, GIT_HTML_PATH
"/%s.html", page
);
374 static void show_html_page(const char *git_cmd
)
376 const char *page
= cmd_to_page(git_cmd
);
377 struct strbuf page_path
; /* it leaks but we exec bellow */
379 get_html_page_path(&page_path
, page
);
381 execl_git_cmd("web--browse", "-c", "help.browser", page_path
.buf
, NULL
);
384 void help_unknown_cmd(const char *cmd
)
386 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
390 int cmd_version(int argc
, const char **argv
, const char *prefix
)
392 printf("git version %s\n", git_version_string
);
396 int cmd_help(int argc
, const char **argv
, const char *prefix
)
401 setup_git_directory_gently(&nongit
);
402 git_config(git_help_config
);
404 argc
= parse_options(argc
, argv
, builtin_help_options
,
405 builtin_help_usage
, 0);
408 printf("usage: %s\n\n", git_usage_string
);
414 printf("usage: %s\n\n", git_usage_string
);
415 list_common_cmds_help();
419 alias
= alias_lookup(argv
[0]);
420 if (alias
&& !is_git_command(argv
[0])) {
421 printf("`git %s' is aliased to `%s'\n", argv
[0], alias
);
425 switch (help_format
) {
426 case HELP_FORMAT_MAN
:
427 show_man_page(argv
[0]);
429 case HELP_FORMAT_INFO
:
430 show_info_page(argv
[0]);
432 case HELP_FORMAT_WEB
:
433 show_html_page(argv
[0]);