Include compat/mingw.h early in git-compat-util.h.
[git/mingw.git] / help.c
blob8d83df54662d14dbf4927e7850c925fa76e30b93
1 /*
2 * builtin-help.c
4 * Builtin help-related commands (help, usage, version)
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "common-cmds.h"
10 #include "parse-options.h"
12 enum help_format {
13 HELP_FORMAT_MAN,
14 HELP_FORMAT_INFO,
15 HELP_FORMAT_WEB,
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",
24 HELP_FORMAT_WEB),
25 OPT_SET_INT('i', "info", &help_format, "show info page",
26 HELP_FORMAT_INFO),
29 static const char * const builtin_help_usage[] = {
30 "git-help [--all] [--man|--web|--info] [command]",
31 NULL
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")) {
48 if (!value)
49 return config_error_nonbool(var);
50 help_format = parse_help_format(value);
51 return 0;
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");
60 int n_cols;
62 if (col_string && (n_cols = atoi(col_string)) > 0)
63 return n_cols;
65 #ifdef TIOCGWINSZ
67 struct winsize ws;
68 if (!ioctl(1, TIOCGWINSZ, &ws)) {
69 if (ws.ws_col)
70 return ws.ws_col;
73 #endif
75 return 80;
78 static inline void mput_char(char c, unsigned int num)
80 while(num--)
81 putchar(c);
84 static struct cmdnames {
85 int alloc;
86 int cnt;
87 struct cmdname {
88 size_t len;
89 char name[1];
90 } **names;
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);
97 ent->len = len;
98 memcpy(ent->name, name, len);
99 ent->name[len] = 0;
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)
114 int i, j;
116 if (!cmds->cnt)
117 return;
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];
123 cmds->cnt = j;
126 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
128 int ci, cj, ei;
129 int cmp;
131 ci = cj = ei = 0;
132 while (ci < cmds->cnt && ei < excludes->cnt) {
133 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
134 if (cmp < 0)
135 cmds->names[cj++] = cmds->names[ci++];
136 else if (cmp == 0)
137 ci++, ei++;
138 else if (cmp > 0)
139 ei++;
142 while (ci < cmds->cnt)
143 cmds->names[cj++] = cmds->names[ci++];
145 cmds->cnt = cj;
148 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
150 int cols = 1, rows;
151 int space = longest + 1; /* min 1 SP between words */
152 int max_cols = term_columns() - 1; /* don't print *on* the edge */
153 int i, j;
155 if (space < max_cols)
156 cols = max_cols / space;
157 rows = (cmds->cnt + cols - 1) / cols;
159 for (i = 0; i < rows; i++) {
160 printf(" ");
162 for (j = 0; j < cols; j++) {
163 int n = j * rows + i;
164 int size = space;
165 if (n >= cmds->cnt)
166 break;
167 if (j == cols-1 || n + rows >= cmds->cnt)
168 size = 1;
169 printf("%-*s", size, cmds->names[n]->name);
171 putchar('\n');
175 static int is_executable(const char *name)
177 struct stat st;
179 if (stat(name, &st) || /* stat, not lstat */
180 !S_ISREG(st.st_mode))
181 return 0;
183 #ifdef __MINGW32__
184 /* cannot trust the executable bit, peek into the file instead */
185 char buf[3] = { 0 };
186 int n;
187 int fd = open(name, O_RDONLY);
188 st.st_mode &= ~S_IXUSR;
189 if (fd >= 0) {
190 n = read(fd, buf, 2);
191 if (n == 2)
192 /* DOS executables start with "MZ" */
193 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
194 st.st_mode |= S_IXUSR;
195 close(fd);
197 #endif
198 return st.st_mode & S_IXUSR;
201 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
202 const char *path)
204 unsigned int longest = 0;
205 const char *prefix = "git-";
206 int prefix_len = strlen(prefix);
207 DIR *dir = opendir(path);
208 struct dirent *de;
210 if (!dir || chdir(path))
211 return 0;
213 while ((de = readdir(dir)) != NULL) {
214 int entlen;
216 if (prefixcmp(de->d_name, prefix))
217 continue;
219 if (!is_executable(de->d_name))
220 continue;
222 entlen = strlen(de->d_name) - prefix_len;
223 if (has_extension(de->d_name, ".exe"))
224 entlen -= 4;
226 if (longest < entlen)
227 longest = entlen;
229 add_cmdname(cmds, de->d_name + prefix_len, entlen);
231 closedir(dir);
233 return longest;
236 static unsigned int load_command_list(void)
238 unsigned int longest = 0;
239 unsigned int len;
240 const char *env_path = getenv("PATH");
241 char *paths, *path, *colon;
242 const char *exec_path = git_exec_path();
243 #ifdef __MINGW32__
244 char sep = ';';
245 #else
246 char sep = ':';
247 #endif
249 if (exec_path)
250 longest = list_commands_in_dir(&main_cmds, exec_path);
252 if (!env_path) {
253 fprintf(stderr, "PATH not set\n");
254 exit(1);
257 path = paths = xstrdup(env_path);
258 while (1) {
259 if ((colon = strchr(path, sep)))
260 *colon = 0;
262 len = list_commands_in_dir(&other_cmds, path);
263 if (len > longest)
264 longest = len;
266 if (!colon)
267 break;
268 path = colon + 1;
270 free(paths);
272 qsort(main_cmds.names, main_cmds.cnt,
273 sizeof(*main_cmds.names), cmdname_compare);
274 uniq(&main_cmds);
276 qsort(other_cmds.names, other_cmds.cnt,
277 sizeof(*other_cmds.names), cmdname_compare);
278 uniq(&other_cmds);
279 exclude_cmds(&other_cmds, &main_cmds);
281 return longest;
284 static void list_commands(void)
286 unsigned int longest = load_command_list();
287 const char *exec_path = git_exec_path();
289 if (main_cmds.cnt) {
290 printf("available git commands in '%s'\n", exec_path);
291 printf("----------------------------");
292 mput_char('-', strlen(exec_path));
293 putchar('\n');
294 pretty_print_string_list(&main_cmds, longest);
295 putchar('\n');
298 if (other_cmds.cnt) {
299 printf("git commands available from elsewhere on your $PATH\n");
300 printf("---------------------------------------------------\n");
301 pretty_print_string_list(&other_cmds, longest);
302 putchar('\n');
306 void list_common_cmds_help(void)
308 int i, longest = 0;
310 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
311 if (longest < strlen(common_cmds[i].name))
312 longest = strlen(common_cmds[i].name);
315 puts("The most commonly used git commands are:");
316 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
317 printf(" %s ", common_cmds[i].name);
318 mput_char(' ', longest - strlen(common_cmds[i].name));
319 puts(common_cmds[i].help);
323 static int is_in_cmdlist(struct cmdnames *c, const char *s)
325 int i;
326 for (i = 0; i < c->cnt; i++)
327 if (!strcmp(s, c->names[i]->name))
328 return 1;
329 return 0;
332 static int is_git_command(const char *s)
334 load_command_list();
335 return is_in_cmdlist(&main_cmds, s) ||
336 is_in_cmdlist(&other_cmds, s);
339 static const char *cmd_to_page(const char *git_cmd)
341 if (!git_cmd)
342 return "git";
343 else if (!prefixcmp(git_cmd, "git"))
344 return git_cmd;
345 else {
346 int page_len = strlen(git_cmd) + 4;
347 char *p = xmalloc(page_len + 1);
348 strcpy(p, "git-");
349 strcpy(p + 4, git_cmd);
350 p[page_len] = 0;
351 return p;
355 static void setup_man_path(void)
357 struct strbuf new_path;
358 const char *old_path = getenv("MANPATH");
360 strbuf_init(&new_path, 0);
362 /* We should always put ':' after our path. If there is no
363 * old_path, the ':' at the end will let 'man' to try
364 * system-wide paths after ours to find the manual page. If
365 * there is old_path, we need ':' as delimiter. */
366 strbuf_addstr(&new_path, GIT_MAN_PATH);
367 strbuf_addch(&new_path, ':');
368 if (old_path)
369 strbuf_addstr(&new_path, old_path);
371 setenv("MANPATH", new_path.buf, 1);
373 strbuf_release(&new_path);
376 static void show_man_page(const char *git_cmd)
378 const char *page = cmd_to_page(git_cmd);
379 setup_man_path();
380 execlp("man", "man", page, NULL);
383 static void show_info_page(const char *git_cmd)
385 const char *page = cmd_to_page(git_cmd);
386 setenv("INFOPATH", GIT_INFO_PATH, 1);
387 execlp("info", "info", "gitman", page, NULL);
390 static void get_html_page_path(struct strbuf *page_path, const char *page)
392 struct stat st;
394 /* Check that we have a git documentation directory. */
395 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
396 die("'%s': not a documentation directory.", GIT_HTML_PATH);
398 strbuf_init(page_path, 0);
399 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
402 static void show_html_page(const char *git_cmd)
404 const char *page = cmd_to_page(git_cmd);
405 struct strbuf page_path; /* it leaks but we exec bellow */
407 get_html_page_path(&page_path, page);
409 execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
412 void help_unknown_cmd(const char *cmd)
414 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
415 exit(1);
418 int cmd_version(int argc, const char **argv, const char *prefix)
420 printf("git version %s\n", git_version_string);
421 return 0;
424 int cmd_help(int argc, const char **argv, const char *prefix)
426 int nongit;
427 const char *alias;
429 setup_git_directory_gently(&nongit);
430 git_config(git_help_config);
432 argc = parse_options(argc, argv, builtin_help_options,
433 builtin_help_usage, 0);
435 if (show_all) {
436 printf("usage: %s\n\n", git_usage_string);
437 list_commands();
438 return 0;
441 if (!argv[0]) {
442 printf("usage: %s\n\n", git_usage_string);
443 list_common_cmds_help();
444 return 0;
447 alias = alias_lookup(argv[0]);
448 if (alias && !is_git_command(argv[0])) {
449 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
450 return 0;
453 switch (help_format) {
454 case HELP_FORMAT_MAN:
455 show_man_page(argv[0]);
456 break;
457 case HELP_FORMAT_INFO:
458 show_info_page(argv[0]);
459 break;
460 case HELP_FORMAT_WEB:
461 show_html_page(argv[0]);
462 break;
465 return 0;