4 * Builtin help-related commands (help, usage, version)
9 #include "common-cmds.h"
11 static const char *help_default_format
;
13 static enum help_format
{
17 } help_format
= man_format
;
19 static void parse_help_format(const char *format
)
22 help_format
= man_format
;
25 if (!strcmp(format
, "man")) {
26 help_format
= man_format
;
29 if (!strcmp(format
, "info")) {
30 help_format
= info_format
;
33 if (!strcmp(format
, "web") || !strcmp(format
, "html")) {
34 help_format
= web_format
;
37 die("unrecognized help format '%s'", format
);
40 static int git_help_config(const char *var
, const char *value
)
42 if (!strcmp(var
, "help.format")) {
43 help_default_format
= xstrdup(value
);
46 return git_default_config(var
, value
);
49 /* most GUI terminals set COLUMNS (although some don't export it) */
50 static int term_columns(void)
52 char *col_string
= getenv("COLUMNS");
55 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
61 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
71 static inline void mput_char(char c
, unsigned int num
)
77 static struct cmdnames
{
84 } main_cmds
, other_cmds
;
86 static void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
88 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
);
91 memcpy(ent
->name
, name
, len
);
94 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
95 cmds
->names
[cmds
->cnt
++] = ent
;
98 static int cmdname_compare(const void *a_
, const void *b_
)
100 struct cmdname
*a
= *(struct cmdname
**)a_
;
101 struct cmdname
*b
= *(struct cmdname
**)b_
;
102 return strcmp(a
->name
, b
->name
);
105 static void uniq(struct cmdnames
*cmds
)
112 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
113 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
114 cmds
->names
[j
++] = cmds
->names
[i
];
119 static void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
125 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
126 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
128 cmds
->names
[cj
++] = cmds
->names
[ci
++];
135 while (ci
< cmds
->cnt
)
136 cmds
->names
[cj
++] = cmds
->names
[ci
++];
141 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
144 int space
= longest
+ 1; /* min 1 SP between words */
145 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
148 if (space
< max_cols
)
149 cols
= max_cols
/ space
;
150 rows
= (cmds
->cnt
+ cols
- 1) / cols
;
152 for (i
= 0; i
< rows
; i
++) {
155 for (j
= 0; j
< cols
; j
++) {
156 int n
= j
* rows
+ i
;
160 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
162 printf("%-*s", size
, cmds
->names
[n
]->name
);
168 static unsigned int list_commands_in_dir(struct cmdnames
*cmds
,
171 unsigned int longest
= 0;
172 const char *prefix
= "git-";
173 int prefix_len
= strlen(prefix
);
174 DIR *dir
= opendir(path
);
177 if (!dir
|| chdir(path
))
180 while ((de
= readdir(dir
)) != NULL
) {
184 if (prefixcmp(de
->d_name
, prefix
))
187 if (stat(de
->d_name
, &st
) || /* stat, not lstat */
188 !S_ISREG(st
.st_mode
) ||
189 !(st
.st_mode
& S_IXUSR
))
192 entlen
= strlen(de
->d_name
) - prefix_len
;
193 if (has_extension(de
->d_name
, ".exe"))
196 if (longest
< entlen
)
199 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
206 static void list_commands(void)
208 unsigned int longest
= 0;
210 const char *env_path
= getenv("PATH");
211 char *paths
, *path
, *colon
;
212 const char *exec_path
= git_exec_path();
215 longest
= list_commands_in_dir(&main_cmds
, exec_path
);
218 fprintf(stderr
, "PATH not set\n");
222 path
= paths
= xstrdup(env_path
);
224 if ((colon
= strchr(path
, ':')))
227 len
= list_commands_in_dir(&other_cmds
, path
);
237 qsort(main_cmds
.names
, main_cmds
.cnt
,
238 sizeof(*main_cmds
.names
), cmdname_compare
);
241 qsort(other_cmds
.names
, other_cmds
.cnt
,
242 sizeof(*other_cmds
.names
), cmdname_compare
);
244 exclude_cmds(&other_cmds
, &main_cmds
);
247 printf("available git commands in '%s'\n", exec_path
);
248 printf("----------------------------");
249 mput_char('-', strlen(exec_path
));
251 pretty_print_string_list(&main_cmds
, longest
);
255 if (other_cmds
.cnt
) {
256 printf("git commands available from elsewhere on your $PATH\n");
257 printf("---------------------------------------------------\n");
258 pretty_print_string_list(&other_cmds
, longest
);
263 void list_common_cmds_help(void)
267 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
268 if (longest
< strlen(common_cmds
[i
].name
))
269 longest
= strlen(common_cmds
[i
].name
);
272 puts("The most commonly used git commands are:");
273 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
274 printf(" %s ", common_cmds
[i
].name
);
275 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
276 puts(common_cmds
[i
].help
);
280 static const char *cmd_to_page(const char *git_cmd
)
284 else if (!prefixcmp(git_cmd
, "git"))
287 int page_len
= strlen(git_cmd
) + 4;
288 char *p
= xmalloc(page_len
+ 1);
290 strcpy(p
+ 4, git_cmd
);
296 static void setup_man_path(void)
298 struct strbuf new_path
;
299 const char *old_path
= getenv("MANPATH");
301 strbuf_init(&new_path
, 0);
303 /* We should always put ':' after our path. If there is no
304 * old_path, the ':' at the end will let 'man' to try
305 * system-wide paths after ours to find the manual page. If
306 * there is old_path, we need ':' as delimiter. */
307 strbuf_addstr(&new_path
, GIT_MAN_PATH
);
308 strbuf_addch(&new_path
, ':');
310 strbuf_addstr(&new_path
, old_path
);
312 setenv("MANPATH", new_path
.buf
, 1);
314 strbuf_release(&new_path
);
317 static void show_man_page(const char *git_cmd
)
319 const char *page
= cmd_to_page(git_cmd
);
321 execlp("man", "man", page
, NULL
);
324 static void show_info_page(const char *git_cmd
)
326 const char *page
= cmd_to_page(git_cmd
);
327 setenv("INFOPATH", GIT_INFO_PATH
, 1);
328 execlp("info", "info", "gitman", page
, NULL
);
331 static void show_html_page(const char *git_cmd
)
333 const char *page
= cmd_to_page(git_cmd
);
334 execl_git_cmd("help--browse", page
, NULL
);
337 void help_unknown_cmd(const char *cmd
)
339 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
343 int cmd_version(int argc
, const char **argv
, const char *prefix
)
345 printf("git version %s\n", git_version_string
);
349 int cmd_help(int argc
, const char **argv
, const char *prefix
)
351 const char *help_cmd
= argv
[1];
354 printf("usage: %s\n\n", git_usage_string
);
355 list_common_cmds_help();
359 if (!strcmp(help_cmd
, "--all") || !strcmp(help_cmd
, "-a")) {
360 printf("usage: %s\n\n", git_usage_string
);
364 else if (!strcmp(help_cmd
, "--web") || !strcmp(help_cmd
, "-w")) {
365 show_html_page(argc
> 2 ? argv
[2] : NULL
);
368 else if (!strcmp(help_cmd
, "--info") || !strcmp(help_cmd
, "-i")) {
369 show_info_page(argc
> 2 ? argv
[2] : NULL
);
372 else if (!strcmp(help_cmd
, "--man") || !strcmp(help_cmd
, "-m")) {
373 show_man_page(argc
> 2 ? argv
[2] : NULL
);
379 setup_git_directory_gently(&nongit
);
380 git_config(git_help_config
);
381 if (help_default_format
)
382 parse_help_format(help_default_format
);
384 switch (help_format
) {
386 show_man_page(help_cmd
);
389 show_info_page(help_cmd
);
392 show_html_page(help_cmd
);