]> rtime.felk.cvut.cz Git - git.git/blob - help.c
Revert "gitweb: Add 'status_str' to parse_difftree_raw_line output"
[git.git] / help.c
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"
10 #include "parse-options.h"
11 #include "run-command.h"
12
13 static struct man_viewer_list {
14         void (*exec)(const char *);
15         struct man_viewer_list *next;
16 } *man_viewer_list;
17
18 enum help_format {
19         HELP_FORMAT_MAN,
20         HELP_FORMAT_INFO,
21         HELP_FORMAT_WEB,
22 };
23
24 static int show_all = 0;
25 static enum help_format help_format = HELP_FORMAT_MAN;
26 static struct option builtin_help_options[] = {
27         OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
28         OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
29         OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
30                         HELP_FORMAT_WEB),
31         OPT_SET_INT('i', "info", &help_format, "show info page",
32                         HELP_FORMAT_INFO),
33         OPT_END(),
34 };
35
36 static const char * const builtin_help_usage[] = {
37         "git-help [--all] [--man|--web|--info] [command]",
38         NULL
39 };
40
41 static enum help_format parse_help_format(const char *format)
42 {
43         if (!strcmp(format, "man"))
44                 return HELP_FORMAT_MAN;
45         if (!strcmp(format, "info"))
46                 return HELP_FORMAT_INFO;
47         if (!strcmp(format, "web") || !strcmp(format, "html"))
48                 return HELP_FORMAT_WEB;
49         die("unrecognized help format '%s'", format);
50 }
51
52 static int check_emacsclient_version(void)
53 {
54         struct strbuf buffer = STRBUF_INIT;
55         struct child_process ec_process;
56         const char *argv_ec[] = { "emacsclient", "--version", NULL };
57         int version;
58
59         /* emacsclient prints its version number on stderr */
60         memset(&ec_process, 0, sizeof(ec_process));
61         ec_process.argv = argv_ec;
62         ec_process.err = -1;
63         ec_process.stdout_to_stderr = 1;
64         if (start_command(&ec_process)) {
65                 fprintf(stderr, "Failed to start emacsclient.\n");
66                 return -1;
67         }
68         strbuf_read(&buffer, ec_process.err, 20);
69         close(ec_process.err);
70
71         /*
72          * Don't bother checking return value, because "emacsclient --version"
73          * seems to always exits with code 1.
74          */
75         finish_command(&ec_process);
76
77         if (prefixcmp(buffer.buf, "emacsclient")) {
78                 fprintf(stderr, "Failed to parse emacsclient version.\n");
79                 strbuf_release(&buffer);
80                 return -1;
81         }
82
83         strbuf_remove(&buffer, 0, strlen("emacsclient"));
84         version = atoi(buffer.buf);
85
86         if (version < 22) {
87                 fprintf(stderr,
88                         "emacsclient version '%d' too old (< 22).\n",
89                         version);
90                 strbuf_release(&buffer);
91                 return -1;
92         }
93
94         strbuf_release(&buffer);
95         return 0;
96 }
97
98 static void exec_woman_emacs(const char *page)
99 {
100         if (!check_emacsclient_version()) {
101                 /* This works only with emacsclient version >= 22. */
102                 struct strbuf man_page = STRBUF_INIT;
103                 strbuf_addf(&man_page, "(woman \"%s\")", page);
104                 execlp("emacsclient", "emacsclient", "-e", man_page.buf, NULL);
105         }
106 }
107
108 static void exec_man_konqueror(const char *page)
109 {
110         const char *display = getenv("DISPLAY");
111         if (display && *display) {
112                 struct strbuf man_page = STRBUF_INIT;
113                 strbuf_addf(&man_page, "man:%s(1)", page);
114                 execlp("kfmclient", "kfmclient", "newTab", man_page.buf, NULL);
115         }
116 }
117
118 static void exec_man_man(const char *page)
119 {
120         execlp("man", "man", page, NULL);
121 }
122
123 static void do_add_man_viewer(void (*exec)(const char *))
124 {
125         struct man_viewer_list **p = &man_viewer_list;
126
127         while (*p)
128                 p = &((*p)->next);
129         *p = xmalloc(sizeof(**p));
130         (*p)->next = NULL;
131         (*p)->exec = exec;
132 }
133
134 static int add_man_viewer(const char *value)
135 {
136         if (!strcasecmp(value, "man"))
137                 do_add_man_viewer(exec_man_man);
138         else if (!strcasecmp(value, "woman"))
139                 do_add_man_viewer(exec_woman_emacs);
140         else if (!strcasecmp(value, "konqueror"))
141                 do_add_man_viewer(exec_man_konqueror);
142         else
143                 warning("'%s': unsupported man viewer.", value);
144
145         return 0;
146 }
147
148 static int git_help_config(const char *var, const char *value)
149 {
150         if (!strcmp(var, "help.format")) {
151                 if (!value)
152                         return config_error_nonbool(var);
153                 help_format = parse_help_format(value);
154                 return 0;
155         }
156         if (!strcmp(var, "man.viewer")) {
157                 if (!value)
158                         return config_error_nonbool(var);
159                 return add_man_viewer(value);
160         }
161         return git_default_config(var, value);
162 }
163
164 /* most GUI terminals set COLUMNS (although some don't export it) */
165 static int term_columns(void)
166 {
167         char *col_string = getenv("COLUMNS");
168         int n_cols;
169
170         if (col_string && (n_cols = atoi(col_string)) > 0)
171                 return n_cols;
172
173 #ifdef TIOCGWINSZ
174         {
175                 struct winsize ws;
176                 if (!ioctl(1, TIOCGWINSZ, &ws)) {
177                         if (ws.ws_col)
178                                 return ws.ws_col;
179                 }
180         }
181 #endif
182
183         return 80;
184 }
185
186 static inline void mput_char(char c, unsigned int num)
187 {
188         while(num--)
189                 putchar(c);
190 }
191
192 static struct cmdnames {
193         int alloc;
194         int cnt;
195         struct cmdname {
196                 size_t len;
197                 char name[1];
198         } **names;
199 } main_cmds, other_cmds;
200
201 static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
202 {
203         struct cmdname *ent = xmalloc(sizeof(*ent) + len);
204
205         ent->len = len;
206         memcpy(ent->name, name, len);
207         ent->name[len] = 0;
208
209         ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
210         cmds->names[cmds->cnt++] = ent;
211 }
212
213 static int cmdname_compare(const void *a_, const void *b_)
214 {
215         struct cmdname *a = *(struct cmdname **)a_;
216         struct cmdname *b = *(struct cmdname **)b_;
217         return strcmp(a->name, b->name);
218 }
219
220 static void uniq(struct cmdnames *cmds)
221 {
222         int i, j;
223
224         if (!cmds->cnt)
225                 return;
226
227         for (i = j = 1; i < cmds->cnt; i++)
228                 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
229                         cmds->names[j++] = cmds->names[i];
230
231         cmds->cnt = j;
232 }
233
234 static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
235 {
236         int ci, cj, ei;
237         int cmp;
238
239         ci = cj = ei = 0;
240         while (ci < cmds->cnt && ei < excludes->cnt) {
241                 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
242                 if (cmp < 0)
243                         cmds->names[cj++] = cmds->names[ci++];
244                 else if (cmp == 0)
245                         ci++, ei++;
246                 else if (cmp > 0)
247                         ei++;
248         }
249
250         while (ci < cmds->cnt)
251                 cmds->names[cj++] = cmds->names[ci++];
252
253         cmds->cnt = cj;
254 }
255
256 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
257 {
258         int cols = 1, rows;
259         int space = longest + 1; /* min 1 SP between words */
260         int max_cols = term_columns() - 1; /* don't print *on* the edge */
261         int i, j;
262
263         if (space < max_cols)
264                 cols = max_cols / space;
265         rows = (cmds->cnt + cols - 1) / cols;
266
267         for (i = 0; i < rows; i++) {
268                 printf("  ");
269
270                 for (j = 0; j < cols; j++) {
271                         int n = j * rows + i;
272                         int size = space;
273                         if (n >= cmds->cnt)
274                                 break;
275                         if (j == cols-1 || n + rows >= cmds->cnt)
276                                 size = 1;
277                         printf("%-*s", size, cmds->names[n]->name);
278                 }
279                 putchar('\n');
280         }
281 }
282
283 static unsigned int list_commands_in_dir(struct cmdnames *cmds,
284                                          const char *path)
285 {
286         unsigned int longest = 0;
287         const char *prefix = "git-";
288         int prefix_len = strlen(prefix);
289         DIR *dir = opendir(path);
290         struct dirent *de;
291
292         if (!dir || chdir(path))
293                 return 0;
294
295         while ((de = readdir(dir)) != NULL) {
296                 struct stat st;
297                 int entlen;
298
299                 if (prefixcmp(de->d_name, prefix))
300                         continue;
301
302                 if (stat(de->d_name, &st) || /* stat, not lstat */
303                     !S_ISREG(st.st_mode) ||
304                     !(st.st_mode & S_IXUSR))
305                         continue;
306
307                 entlen = strlen(de->d_name) - prefix_len;
308                 if (has_extension(de->d_name, ".exe"))
309                         entlen -= 4;
310
311                 if (longest < entlen)
312                         longest = entlen;
313
314                 add_cmdname(cmds, de->d_name + prefix_len, entlen);
315         }
316         closedir(dir);
317
318         return longest;
319 }
320
321 static unsigned int load_command_list(void)
322 {
323         unsigned int longest = 0;
324         unsigned int len;
325         const char *env_path = getenv("PATH");
326         char *paths, *path, *colon;
327         const char *exec_path = git_exec_path();
328
329         if (exec_path)
330                 longest = list_commands_in_dir(&main_cmds, exec_path);
331
332         if (!env_path) {
333                 fprintf(stderr, "PATH not set\n");
334                 exit(1);
335         }
336
337         path = paths = xstrdup(env_path);
338         while (1) {
339                 if ((colon = strchr(path, ':')))
340                         *colon = 0;
341
342                 len = list_commands_in_dir(&other_cmds, path);
343                 if (len > longest)
344                         longest = len;
345
346                 if (!colon)
347                         break;
348                 path = colon + 1;
349         }
350         free(paths);
351
352         qsort(main_cmds.names, main_cmds.cnt,
353               sizeof(*main_cmds.names), cmdname_compare);
354         uniq(&main_cmds);
355
356         qsort(other_cmds.names, other_cmds.cnt,
357               sizeof(*other_cmds.names), cmdname_compare);
358         uniq(&other_cmds);
359         exclude_cmds(&other_cmds, &main_cmds);
360
361         return longest;
362 }
363
364 static void list_commands(void)
365 {
366         unsigned int longest = load_command_list();
367         const char *exec_path = git_exec_path();
368
369         if (main_cmds.cnt) {
370                 printf("available git commands in '%s'\n", exec_path);
371                 printf("----------------------------");
372                 mput_char('-', strlen(exec_path));
373                 putchar('\n');
374                 pretty_print_string_list(&main_cmds, longest);
375                 putchar('\n');
376         }
377
378         if (other_cmds.cnt) {
379                 printf("git commands available from elsewhere on your $PATH\n");
380                 printf("---------------------------------------------------\n");
381                 pretty_print_string_list(&other_cmds, longest);
382                 putchar('\n');
383         }
384 }
385
386 void list_common_cmds_help(void)
387 {
388         int i, longest = 0;
389
390         for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
391                 if (longest < strlen(common_cmds[i].name))
392                         longest = strlen(common_cmds[i].name);
393         }
394
395         puts("The most commonly used git commands are:");
396         for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
397                 printf("   %s   ", common_cmds[i].name);
398                 mput_char(' ', longest - strlen(common_cmds[i].name));
399                 puts(common_cmds[i].help);
400         }
401 }
402
403 static int is_in_cmdlist(struct cmdnames *c, const char *s)
404 {
405         int i;
406         for (i = 0; i < c->cnt; i++)
407                 if (!strcmp(s, c->names[i]->name))
408                         return 1;
409         return 0;
410 }
411
412 static int is_git_command(const char *s)
413 {
414         load_command_list();
415         return is_in_cmdlist(&main_cmds, s) ||
416                 is_in_cmdlist(&other_cmds, s);
417 }
418
419 static const char *cmd_to_page(const char *git_cmd)
420 {
421         if (!git_cmd)
422                 return "git";
423         else if (!prefixcmp(git_cmd, "git"))
424                 return git_cmd;
425         else {
426                 int page_len = strlen(git_cmd) + 4;
427                 char *p = xmalloc(page_len + 1);
428                 strcpy(p, "git-");
429                 strcpy(p + 4, git_cmd);
430                 p[page_len] = 0;
431                 return p;
432         }
433 }
434
435 static void setup_man_path(void)
436 {
437         struct strbuf new_path;
438         const char *old_path = getenv("MANPATH");
439
440         strbuf_init(&new_path, 0);
441
442         /* We should always put ':' after our path. If there is no
443          * old_path, the ':' at the end will let 'man' to try
444          * system-wide paths after ours to find the manual page. If
445          * there is old_path, we need ':' as delimiter. */
446         strbuf_addstr(&new_path, GIT_MAN_PATH);
447         strbuf_addch(&new_path, ':');
448         if (old_path)
449                 strbuf_addstr(&new_path, old_path);
450
451         setenv("MANPATH", new_path.buf, 1);
452
453         strbuf_release(&new_path);
454 }
455
456 static void show_man_page(const char *git_cmd)
457 {
458         struct man_viewer_list *viewer;
459         const char *page = cmd_to_page(git_cmd);
460
461         setup_man_path();
462         for (viewer = man_viewer_list; viewer; viewer = viewer->next)
463         {
464                 viewer->exec(page); /* will return when unable */
465         }
466         exec_man_man(page);
467         die("no man viewer handled the request");
468 }
469
470 static void show_info_page(const char *git_cmd)
471 {
472         const char *page = cmd_to_page(git_cmd);
473         setenv("INFOPATH", GIT_INFO_PATH, 1);
474         execlp("info", "info", "gitman", page, NULL);
475 }
476
477 static void get_html_page_path(struct strbuf *page_path, const char *page)
478 {
479         struct stat st;
480
481         /* Check that we have a git documentation directory. */
482         if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
483                 die("'%s': not a documentation directory.", GIT_HTML_PATH);
484
485         strbuf_init(page_path, 0);
486         strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
487 }
488
489 static void show_html_page(const char *git_cmd)
490 {
491         const char *page = cmd_to_page(git_cmd);
492         struct strbuf page_path; /* it leaks but we exec bellow */
493
494         get_html_page_path(&page_path, page);
495
496         execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
497 }
498
499 void help_unknown_cmd(const char *cmd)
500 {
501         fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
502         exit(1);
503 }
504
505 int cmd_version(int argc, const char **argv, const char *prefix)
506 {
507         printf("git version %s\n", git_version_string);
508         return 0;
509 }
510
511 int cmd_help(int argc, const char **argv, const char *prefix)
512 {
513         int nongit;
514         const char *alias;
515
516         setup_git_directory_gently(&nongit);
517         git_config(git_help_config);
518
519         argc = parse_options(argc, argv, builtin_help_options,
520                         builtin_help_usage, 0);
521
522         if (show_all) {
523                 printf("usage: %s\n\n", git_usage_string);
524                 list_commands();
525                 return 0;
526         }
527
528         if (!argv[0]) {
529                 printf("usage: %s\n\n", git_usage_string);
530                 list_common_cmds_help();
531                 return 0;
532         }
533
534         alias = alias_lookup(argv[0]);
535         if (alias && !is_git_command(argv[0])) {
536                 printf("`git %s' is aliased to `%s'\n", argv[0], alias);
537                 return 0;
538         }
539
540         switch (help_format) {
541         case HELP_FORMAT_MAN:
542                 show_man_page(argv[0]);
543                 break;
544         case HELP_FORMAT_INFO:
545                 show_info_page(argv[0]);
546                 break;
547         case HELP_FORMAT_WEB:
548                 show_html_page(argv[0]);
549                 break;
550         }
551
552         return 0;
553 }