]>
Commit | Line | Data |
---|---|---|
70827b15 LT |
1 | /* |
2 | * builtin-help.c | |
3 | * | |
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" | |
41eb33bd | 10 | #include "parse-options.h" |
64949984 CC |
11 | #include "run-command.h" |
12 | ||
69099d6b | 13 | static struct man_viewer_list { |
69099d6b | 14 | struct man_viewer_list *next; |
7b15f872 | 15 | char name[FLEX_ARRAY]; |
69099d6b | 16 | } *man_viewer_list; |
41eb33bd | 17 | |
7b15f872 CC |
18 | static struct man_viewer_info_list { |
19 | struct man_viewer_info_list *next; | |
20 | const char *info; | |
21 | char name[FLEX_ARRAY]; | |
22 | } *man_viewer_info_list; | |
23 | ||
41eb33bd JK |
24 | enum help_format { |
25 | HELP_FORMAT_MAN, | |
26 | HELP_FORMAT_INFO, | |
27 | HELP_FORMAT_WEB, | |
28 | }; | |
29 | ||
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", | |
36 | HELP_FORMAT_WEB), | |
37 | OPT_SET_INT('i', "info", &help_format, "show info page", | |
38 | HELP_FORMAT_INFO), | |
4637e47a | 39 | OPT_END(), |
41eb33bd JK |
40 | }; |
41 | ||
42 | static const char * const builtin_help_usage[] = { | |
43 | "git-help [--all] [--man|--web|--info] [command]", | |
44 | NULL | |
45 | }; | |
46 | ||
47 | static enum help_format parse_help_format(const char *format) | |
70087cdb | 48 | { |
41eb33bd JK |
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; | |
70087cdb CC |
55 | die("unrecognized help format '%s'", format); |
56 | } | |
57 | ||
7b15f872 CC |
58 | static const char *get_man_viewer_info(const char *name) |
59 | { | |
60 | struct man_viewer_info_list *viewer; | |
61 | ||
62 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) | |
63 | { | |
64 | if (!strcasecmp(name, viewer->name)) | |
65 | return viewer->info; | |
66 | } | |
67 | return NULL; | |
68 | } | |
69 | ||
69099d6b CC |
70 | static int check_emacsclient_version(void) |
71 | { | |
72 | struct strbuf buffer = STRBUF_INIT; | |
73 | struct child_process ec_process; | |
74 | const char *argv_ec[] = { "emacsclient", "--version", NULL }; | |
75 | int version; | |
76 | ||
77 | /* emacsclient prints its version number on stderr */ | |
78 | memset(&ec_process, 0, sizeof(ec_process)); | |
79 | ec_process.argv = argv_ec; | |
80 | ec_process.err = -1; | |
81 | ec_process.stdout_to_stderr = 1; | |
82 | if (start_command(&ec_process)) { | |
83 | fprintf(stderr, "Failed to start emacsclient.\n"); | |
84 | return -1; | |
85 | } | |
86 | strbuf_read(&buffer, ec_process.err, 20); | |
87 | close(ec_process.err); | |
88 | ||
89 | /* | |
90 | * Don't bother checking return value, because "emacsclient --version" | |
91 | * seems to always exits with code 1. | |
92 | */ | |
93 | finish_command(&ec_process); | |
94 | ||
95 | if (prefixcmp(buffer.buf, "emacsclient")) { | |
96 | fprintf(stderr, "Failed to parse emacsclient version.\n"); | |
97 | strbuf_release(&buffer); | |
98 | return -1; | |
99 | } | |
100 | ||
101 | strbuf_remove(&buffer, 0, strlen("emacsclient")); | |
102 | version = atoi(buffer.buf); | |
103 | ||
104 | if (version < 22) { | |
105 | fprintf(stderr, | |
106 | "emacsclient version '%d' too old (< 22).\n", | |
107 | version); | |
108 | strbuf_release(&buffer); | |
109 | return -1; | |
110 | } | |
111 | ||
112 | strbuf_release(&buffer); | |
113 | return 0; | |
114 | } | |
115 | ||
7b15f872 | 116 | static void exec_woman_emacs(const char* path, const char *page) |
69099d6b CC |
117 | { |
118 | if (!check_emacsclient_version()) { | |
119 | /* This works only with emacsclient version >= 22. */ | |
120 | struct strbuf man_page = STRBUF_INIT; | |
7b15f872 CC |
121 | |
122 | if (!path) | |
123 | path = "emacsclient"; | |
69099d6b | 124 | strbuf_addf(&man_page, "(woman \"%s\")", page); |
7b15f872 CC |
125 | execlp(path, "emacsclient", "-e", man_page.buf, NULL); |
126 | warning("failed to exec '%s': %s", path, strerror(errno)); | |
69099d6b CC |
127 | } |
128 | } | |
129 | ||
7b15f872 | 130 | static void exec_man_konqueror(const char* path, const char *page) |
69099d6b CC |
131 | { |
132 | const char *display = getenv("DISPLAY"); | |
133 | if (display && *display) { | |
134 | struct strbuf man_page = STRBUF_INIT; | |
7b15f872 CC |
135 | const char *filename = "kfmclient"; |
136 | ||
137 | /* It's simpler to launch konqueror using kfmclient. */ | |
138 | if (path) { | |
139 | const char *file = strrchr(path, '/'); | |
140 | if (file && !strcmp(file + 1, "konqueror")) { | |
141 | char *new = xstrdup(path); | |
142 | char *dest = strrchr(new, '/'); | |
143 | ||
144 | /* strlen("konqueror") == strlen("kfmclient") */ | |
145 | strcpy(dest + 1, "kfmclient"); | |
146 | path = new; | |
147 | } | |
148 | if (file) | |
149 | filename = file; | |
150 | } else | |
151 | path = "kfmclient"; | |
69099d6b | 152 | strbuf_addf(&man_page, "man:%s(1)", page); |
7b15f872 CC |
153 | execlp(path, filename, "newTab", man_page.buf, NULL); |
154 | warning("failed to exec '%s': %s", path, strerror(errno)); | |
69099d6b CC |
155 | } |
156 | } | |
157 | ||
7b15f872 | 158 | static void exec_man_man(const char* path, const char *page) |
69099d6b | 159 | { |
7b15f872 CC |
160 | if (!path) |
161 | path = "man"; | |
162 | execlp(path, "man", page, NULL); | |
163 | warning("failed to exec '%s': %s", path, strerror(errno)); | |
69099d6b CC |
164 | } |
165 | ||
a26a06af CC |
166 | static void exec_man_cmd(const char *cmd, const char *page) |
167 | { | |
168 | struct strbuf shell_cmd = STRBUF_INIT; | |
169 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); | |
170 | execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); | |
171 | warning("failed to exec '%s': %s", cmd, strerror(errno)); | |
172 | } | |
173 | ||
174 | static void add_man_viewer(const char *name) | |
69099d6b CC |
175 | { |
176 | struct man_viewer_list **p = &man_viewer_list; | |
7b15f872 | 177 | size_t len = strlen(name); |
69099d6b CC |
178 | |
179 | while (*p) | |
180 | p = &((*p)->next); | |
7b15f872 CC |
181 | *p = xcalloc(1, (sizeof(**p) + len + 1)); |
182 | strncpy((*p)->name, name, len); | |
183 | } | |
184 | ||
185 | static int supported_man_viewer(const char *name, size_t len) | |
186 | { | |
187 | return (!strncasecmp("man", name, len) || | |
188 | !strncasecmp("woman", name, len) || | |
189 | !strncasecmp("konqueror", name, len)); | |
69099d6b CC |
190 | } |
191 | ||
7b15f872 CC |
192 | static void do_add_man_viewer_info(const char *name, |
193 | size_t len, | |
194 | const char *value) | |
195 | { | |
196 | struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1); | |
197 | ||
198 | strncpy(new->name, name, len); | |
199 | new->info = xstrdup(value); | |
200 | new->next = man_viewer_info_list; | |
201 | man_viewer_info_list = new; | |
202 | } | |
203 | ||
204 | static int add_man_viewer_path(const char *name, | |
205 | size_t len, | |
206 | const char *value) | |
207 | { | |
208 | if (supported_man_viewer(name, len)) | |
209 | do_add_man_viewer_info(name, len, value); | |
210 | else | |
a26a06af CC |
211 | warning("'%s': path for unsupported man viewer.\n" |
212 | "Please consider using 'man.<tool>.cmd' instead.", | |
213 | name); | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
218 | static int add_man_viewer_cmd(const char *name, | |
219 | size_t len, | |
220 | const char *value) | |
221 | { | |
222 | if (supported_man_viewer(name, len)) | |
223 | warning("'%s': cmd for supported man viewer.\n" | |
224 | "Please consider using 'man.<tool>.path' instead.", | |
225 | name); | |
226 | else | |
227 | do_add_man_viewer_info(name, len, value); | |
7b15f872 CC |
228 | |
229 | return 0; | |
230 | } | |
231 | ||
232 | static int add_man_viewer_info(const char *var, const char *value) | |
233 | { | |
234 | const char *name = var + 4; | |
235 | const char *subkey = strrchr(name, '.'); | |
236 | ||
237 | if (!subkey) | |
238 | return error("Config with no key for man viewer: %s", name); | |
239 | ||
240 | if (!strcmp(subkey, ".path")) { | |
241 | if (!value) | |
242 | return config_error_nonbool(var); | |
243 | return add_man_viewer_path(name, subkey - name, value); | |
244 | } | |
a26a06af CC |
245 | if (!strcmp(subkey, ".cmd")) { |
246 | if (!value) | |
247 | return config_error_nonbool(var); | |
248 | return add_man_viewer_cmd(name, subkey - name, value); | |
249 | } | |
7b15f872 CC |
250 | |
251 | warning("'%s': unsupported man viewer sub key.", subkey); | |
252 | return 0; | |
253 | } | |
254 | ||
ef90d6d4 | 255 | static int git_help_config(const char *var, const char *value, void *cb) |
70087cdb | 256 | { |
41eb33bd JK |
257 | if (!strcmp(var, "help.format")) { |
258 | if (!value) | |
259 | return config_error_nonbool(var); | |
260 | help_format = parse_help_format(value); | |
261 | return 0; | |
262 | } | |
69099d6b CC |
263 | if (!strcmp(var, "man.viewer")) { |
264 | if (!value) | |
265 | return config_error_nonbool(var); | |
a26a06af CC |
266 | add_man_viewer(value); |
267 | return 0; | |
69099d6b | 268 | } |
7b15f872 CC |
269 | if (!prefixcmp(var, "man.")) |
270 | return add_man_viewer_info(var, value); | |
271 | ||
ef90d6d4 | 272 | return git_default_config(var, value, cb); |
70087cdb CC |
273 | } |
274 | ||
82e5a82f | 275 | /* most GUI terminals set COLUMNS (although some don't export it) */ |
70827b15 LT |
276 | static int term_columns(void) |
277 | { | |
278 | char *col_string = getenv("COLUMNS"); | |
96f1e58f | 279 | int n_cols; |
70827b15 LT |
280 | |
281 | if (col_string && (n_cols = atoi(col_string)) > 0) | |
282 | return n_cols; | |
283 | ||
284 | #ifdef TIOCGWINSZ | |
285 | { | |
286 | struct winsize ws; | |
287 | if (!ioctl(1, TIOCGWINSZ, &ws)) { | |
288 | if (ws.ws_col) | |
289 | return ws.ws_col; | |
290 | } | |
291 | } | |
292 | #endif | |
293 | ||
294 | return 80; | |
295 | } | |
296 | ||
70827b15 LT |
297 | static inline void mput_char(char c, unsigned int num) |
298 | { | |
299 | while(num--) | |
300 | putchar(c); | |
301 | } | |
302 | ||
1eb05690 SP |
303 | static struct cmdnames { |
304 | int alloc; | |
305 | int cnt; | |
306 | struct cmdname { | |
307 | size_t len; | |
308 | char name[1]; | |
309 | } **names; | |
310 | } main_cmds, other_cmds; | |
311 | ||
312 | static void add_cmdname(struct cmdnames *cmds, const char *name, int len) | |
70827b15 | 313 | { |
1eb05690 SP |
314 | struct cmdname *ent = xmalloc(sizeof(*ent) + len); |
315 | ||
70827b15 LT |
316 | ent->len = len; |
317 | memcpy(ent->name, name, len); | |
318 | ent->name[len] = 0; | |
1eb05690 SP |
319 | |
320 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); | |
321 | cmds->names[cmds->cnt++] = ent; | |
70827b15 LT |
322 | } |
323 | ||
324 | static int cmdname_compare(const void *a_, const void *b_) | |
325 | { | |
326 | struct cmdname *a = *(struct cmdname **)a_; | |
327 | struct cmdname *b = *(struct cmdname **)b_; | |
328 | return strcmp(a->name, b->name); | |
329 | } | |
330 | ||
1eb05690 SP |
331 | static void uniq(struct cmdnames *cmds) |
332 | { | |
333 | int i, j; | |
334 | ||
335 | if (!cmds->cnt) | |
336 | return; | |
337 | ||
338 | for (i = j = 1; i < cmds->cnt; i++) | |
339 | if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name)) | |
340 | cmds->names[j++] = cmds->names[i]; | |
341 | ||
342 | cmds->cnt = j; | |
343 | } | |
344 | ||
f3fa1838 JH |
345 | static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
346 | { | |
1eb05690 SP |
347 | int ci, cj, ei; |
348 | int cmp; | |
349 | ||
350 | ci = cj = ei = 0; | |
351 | while (ci < cmds->cnt && ei < excludes->cnt) { | |
352 | cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); | |
353 | if (cmp < 0) | |
354 | cmds->names[cj++] = cmds->names[ci++]; | |
355 | else if (cmp == 0) | |
356 | ci++, ei++; | |
357 | else if (cmp > 0) | |
358 | ei++; | |
359 | } | |
360 | ||
361 | while (ci < cmds->cnt) | |
362 | cmds->names[cj++] = cmds->names[ci++]; | |
363 | ||
364 | cmds->cnt = cj; | |
365 | } | |
366 | ||
367 | static void pretty_print_string_list(struct cmdnames *cmds, int longest) | |
70827b15 LT |
368 | { |
369 | int cols = 1, rows; | |
370 | int space = longest + 1; /* min 1 SP between words */ | |
371 | int max_cols = term_columns() - 1; /* don't print *on* the edge */ | |
372 | int i, j; | |
373 | ||
374 | if (space < max_cols) | |
375 | cols = max_cols / space; | |
1eb05690 | 376 | rows = (cmds->cnt + cols - 1) / cols; |
70827b15 LT |
377 | |
378 | for (i = 0; i < rows; i++) { | |
379 | printf(" "); | |
380 | ||
381 | for (j = 0; j < cols; j++) { | |
382 | int n = j * rows + i; | |
383 | int size = space; | |
1eb05690 | 384 | if (n >= cmds->cnt) |
70827b15 | 385 | break; |
1eb05690 | 386 | if (j == cols-1 || n + rows >= cmds->cnt) |
70827b15 | 387 | size = 1; |
1eb05690 | 388 | printf("%-*s", size, cmds->names[n]->name); |
70827b15 LT |
389 | } |
390 | putchar('\n'); | |
391 | } | |
392 | } | |
393 | ||
1eb05690 SP |
394 | static unsigned int list_commands_in_dir(struct cmdnames *cmds, |
395 | const char *path) | |
70827b15 LT |
396 | { |
397 | unsigned int longest = 0; | |
edb6ddc5 SP |
398 | const char *prefix = "git-"; |
399 | int prefix_len = strlen(prefix); | |
1eb05690 | 400 | DIR *dir = opendir(path); |
70827b15 LT |
401 | struct dirent *de; |
402 | ||
1eb05690 SP |
403 | if (!dir || chdir(path)) |
404 | return 0; | |
70827b15 | 405 | |
70827b15 LT |
406 | while ((de = readdir(dir)) != NULL) { |
407 | struct stat st; | |
408 | int entlen; | |
409 | ||
edb6ddc5 | 410 | if (prefixcmp(de->d_name, prefix)) |
70827b15 | 411 | continue; |
0966003c SP |
412 | |
413 | if (stat(de->d_name, &st) || /* stat, not lstat */ | |
70827b15 LT |
414 | !S_ISREG(st.st_mode) || |
415 | !(st.st_mode & S_IXUSR)) | |
416 | continue; | |
417 | ||
edb6ddc5 | 418 | entlen = strlen(de->d_name) - prefix_len; |
5bb1cda5 | 419 | if (has_extension(de->d_name, ".exe")) |
70827b15 LT |
420 | entlen -= 4; |
421 | ||
422 | if (longest < entlen) | |
423 | longest = entlen; | |
424 | ||
1eb05690 | 425 | add_cmdname(cmds, de->d_name + prefix_len, entlen); |
70827b15 LT |
426 | } |
427 | closedir(dir); | |
428 | ||
1eb05690 SP |
429 | return longest; |
430 | } | |
431 | ||
2156435f | 432 | static unsigned int load_command_list(void) |
1eb05690 SP |
433 | { |
434 | unsigned int longest = 0; | |
435 | unsigned int len; | |
436 | const char *env_path = getenv("PATH"); | |
437 | char *paths, *path, *colon; | |
438 | const char *exec_path = git_exec_path(); | |
439 | ||
440 | if (exec_path) | |
441 | longest = list_commands_in_dir(&main_cmds, exec_path); | |
442 | ||
443 | if (!env_path) { | |
444 | fprintf(stderr, "PATH not set\n"); | |
445 | exit(1); | |
446 | } | |
447 | ||
448 | path = paths = xstrdup(env_path); | |
449 | while (1) { | |
450 | if ((colon = strchr(path, ':'))) | |
451 | *colon = 0; | |
452 | ||
453 | len = list_commands_in_dir(&other_cmds, path); | |
454 | if (len > longest) | |
455 | longest = len; | |
456 | ||
457 | if (!colon) | |
458 | break; | |
459 | path = colon + 1; | |
460 | } | |
461 | free(paths); | |
462 | ||
463 | qsort(main_cmds.names, main_cmds.cnt, | |
464 | sizeof(*main_cmds.names), cmdname_compare); | |
465 | uniq(&main_cmds); | |
466 | ||
467 | qsort(other_cmds.names, other_cmds.cnt, | |
468 | sizeof(*other_cmds.names), cmdname_compare); | |
469 | uniq(&other_cmds); | |
470 | exclude_cmds(&other_cmds, &main_cmds); | |
471 | ||
2156435f JK |
472 | return longest; |
473 | } | |
474 | ||
475 | static void list_commands(void) | |
476 | { | |
477 | unsigned int longest = load_command_list(); | |
478 | const char *exec_path = git_exec_path(); | |
479 | ||
1eb05690 SP |
480 | if (main_cmds.cnt) { |
481 | printf("available git commands in '%s'\n", exec_path); | |
482 | printf("----------------------------"); | |
483 | mput_char('-', strlen(exec_path)); | |
484 | putchar('\n'); | |
485 | pretty_print_string_list(&main_cmds, longest); | |
486 | putchar('\n'); | |
487 | } | |
488 | ||
489 | if (other_cmds.cnt) { | |
490 | printf("git commands available from elsewhere on your $PATH\n"); | |
491 | printf("---------------------------------------------------\n"); | |
492 | pretty_print_string_list(&other_cmds, longest); | |
493 | putchar('\n'); | |
494 | } | |
70827b15 LT |
495 | } |
496 | ||
3d7e2d85 | 497 | void list_common_cmds_help(void) |
70827b15 LT |
498 | { |
499 | int i, longest = 0; | |
500 | ||
501 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { | |
502 | if (longest < strlen(common_cmds[i].name)) | |
503 | longest = strlen(common_cmds[i].name); | |
504 | } | |
505 | ||
506 | puts("The most commonly used git commands are:"); | |
507 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { | |
4f75b115 RS |
508 | printf(" %s ", common_cmds[i].name); |
509 | mput_char(' ', longest - strlen(common_cmds[i].name)); | |
70827b15 LT |
510 | puts(common_cmds[i].help); |
511 | } | |
70827b15 LT |
512 | } |
513 | ||
2156435f JK |
514 | static int is_in_cmdlist(struct cmdnames *c, const char *s) |
515 | { | |
516 | int i; | |
517 | for (i = 0; i < c->cnt; i++) | |
518 | if (!strcmp(s, c->names[i]->name)) | |
519 | return 1; | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static int is_git_command(const char *s) | |
524 | { | |
525 | load_command_list(); | |
526 | return is_in_cmdlist(&main_cmds, s) || | |
527 | is_in_cmdlist(&other_cmds, s); | |
528 | } | |
529 | ||
2dce956e CC |
530 | static const char *prepend(const char *prefix, const char *cmd) |
531 | { | |
532 | size_t pre_len = strlen(prefix); | |
533 | size_t cmd_len = strlen(cmd); | |
534 | char *p = xmalloc(pre_len + cmd_len + 1); | |
535 | memcpy(p, prefix, pre_len); | |
536 | strcpy(p + pre_len, cmd); | |
537 | return p; | |
538 | } | |
539 | ||
df55c9cb | 540 | static const char *cmd_to_page(const char *git_cmd) |
70827b15 | 541 | { |
5d6491c7 CC |
542 | if (!git_cmd) |
543 | return "git"; | |
544 | else if (!prefixcmp(git_cmd, "git")) | |
df55c9cb | 545 | return git_cmd; |
2dce956e CC |
546 | else if (is_git_command(git_cmd)) |
547 | return prepend("git-", git_cmd); | |
548 | else | |
549 | return prepend("git", git_cmd); | |
df55c9cb | 550 | } |
70827b15 | 551 | |
8e566f24 SO |
552 | static void setup_man_path(void) |
553 | { | |
554 | struct strbuf new_path; | |
555 | const char *old_path = getenv("MANPATH"); | |
556 | ||
557 | strbuf_init(&new_path, 0); | |
558 | ||
559 | /* We should always put ':' after our path. If there is no | |
560 | * old_path, the ':' at the end will let 'man' to try | |
561 | * system-wide paths after ours to find the manual page. If | |
562 | * there is old_path, we need ':' as delimiter. */ | |
563 | strbuf_addstr(&new_path, GIT_MAN_PATH); | |
564 | strbuf_addch(&new_path, ':'); | |
565 | if (old_path) | |
566 | strbuf_addstr(&new_path, old_path); | |
567 | ||
568 | setenv("MANPATH", new_path.buf, 1); | |
569 | ||
570 | strbuf_release(&new_path); | |
571 | } | |
572 | ||
7b15f872 CC |
573 | static void exec_viewer(const char *name, const char *page) |
574 | { | |
a26a06af | 575 | const char *info = get_man_viewer_info(name); |
7b15f872 CC |
576 | |
577 | if (!strcasecmp(name, "man")) | |
a26a06af | 578 | exec_man_man(info, page); |
7b15f872 | 579 | else if (!strcasecmp(name, "woman")) |
a26a06af | 580 | exec_woman_emacs(info, page); |
7b15f872 | 581 | else if (!strcasecmp(name, "konqueror")) |
a26a06af CC |
582 | exec_man_konqueror(info, page); |
583 | else if (info) | |
584 | exec_man_cmd(info, page); | |
7b15f872 | 585 | else |
a26a06af | 586 | warning("'%s': unknown man viewer.", name); |
7b15f872 CC |
587 | } |
588 | ||
df55c9cb CC |
589 | static void show_man_page(const char *git_cmd) |
590 | { | |
69099d6b | 591 | struct man_viewer_list *viewer; |
df55c9cb | 592 | const char *page = cmd_to_page(git_cmd); |
69099d6b | 593 | |
8e566f24 | 594 | setup_man_path(); |
69099d6b CC |
595 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
596 | { | |
7b15f872 | 597 | exec_viewer(viewer->name, page); /* will return when unable */ |
69099d6b | 598 | } |
7b15f872 | 599 | exec_viewer("man", page); |
69099d6b | 600 | die("no man viewer handled the request"); |
70827b15 LT |
601 | } |
602 | ||
df55c9cb CC |
603 | static void show_info_page(const char *git_cmd) |
604 | { | |
605 | const char *page = cmd_to_page(git_cmd); | |
a149a1a4 | 606 | setenv("INFOPATH", GIT_INFO_PATH, 1); |
78d39f98 | 607 | execlp("info", "info", "gitman", page, NULL); |
df55c9cb CC |
608 | } |
609 | ||
482cce82 CC |
610 | static void get_html_page_path(struct strbuf *page_path, const char *page) |
611 | { | |
612 | struct stat st; | |
613 | ||
614 | /* Check that we have a git documentation directory. */ | |
615 | if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode)) | |
616 | die("'%s': not a documentation directory.", GIT_HTML_PATH); | |
617 | ||
618 | strbuf_init(page_path, 0); | |
619 | strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page); | |
620 | } | |
621 | ||
5d6491c7 CC |
622 | static void show_html_page(const char *git_cmd) |
623 | { | |
624 | const char *page = cmd_to_page(git_cmd); | |
482cce82 CC |
625 | struct strbuf page_path; /* it leaks but we exec bellow */ |
626 | ||
627 | get_html_page_path(&page_path, page); | |
628 | ||
5884f1fe | 629 | execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL); |
5d6491c7 CC |
630 | } |
631 | ||
822a7d50 RJ |
632 | void help_unknown_cmd(const char *cmd) |
633 | { | |
a238917b | 634 | fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd); |
822a7d50 RJ |
635 | exit(1); |
636 | } | |
637 | ||
a633fca0 | 638 | int cmd_version(int argc, const char **argv, const char *prefix) |
70827b15 LT |
639 | { |
640 | printf("git version %s\n", git_version_string); | |
641 | return 0; | |
642 | } | |
643 | ||
5d6491c7 | 644 | int cmd_help(int argc, const char **argv, const char *prefix) |
70827b15 | 645 | { |
41eb33bd | 646 | int nongit; |
2156435f | 647 | const char *alias; |
5d6491c7 | 648 | |
41eb33bd | 649 | setup_git_directory_gently(&nongit); |
ef90d6d4 | 650 | git_config(git_help_config, NULL); |
822a7d50 | 651 | |
41eb33bd JK |
652 | argc = parse_options(argc, argv, builtin_help_options, |
653 | builtin_help_usage, 0); | |
654 | ||
655 | if (show_all) { | |
822a7d50 | 656 | printf("usage: %s\n\n", git_usage_string); |
1eb05690 | 657 | list_commands(); |
b7d96819 | 658 | printf("%s\n", git_more_info_string); |
41eb33bd | 659 | return 0; |
822a7d50 | 660 | } |
df55c9cb | 661 | |
41eb33bd JK |
662 | if (!argv[0]) { |
663 | printf("usage: %s\n\n", git_usage_string); | |
664 | list_common_cmds_help(); | |
b7d96819 | 665 | printf("\n%s\n", git_more_info_string); |
41eb33bd | 666 | return 0; |
70087cdb CC |
667 | } |
668 | ||
2156435f JK |
669 | alias = alias_lookup(argv[0]); |
670 | if (alias && !is_git_command(argv[0])) { | |
671 | printf("`git %s' is aliased to `%s'\n", argv[0], alias); | |
672 | return 0; | |
673 | } | |
674 | ||
41eb33bd JK |
675 | switch (help_format) { |
676 | case HELP_FORMAT_MAN: | |
677 | show_man_page(argv[0]); | |
678 | break; | |
679 | case HELP_FORMAT_INFO: | |
680 | show_info_page(argv[0]); | |
681 | break; | |
682 | case HELP_FORMAT_WEB: | |
683 | show_html_page(argv[0]); | |
684 | break; | |
70087cdb | 685 | } |
822a7d50 | 686 | |
70827b15 LT |
687 | return 0; |
688 | } |