help--browse: add '--config' option to check a config option for a browser.
[git/git.git] / help.c
CommitLineData
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"
70827b15 10
70087cdb
CC
11static const char *help_default_format;
12
13static enum help_format {
14 man_format,
15 info_format,
16 web_format,
17} help_format = man_format;
18
19static void parse_help_format(const char *format)
20{
21 if (!format) {
22 help_format = man_format;
23 return;
24 }
25 if (!strcmp(format, "man")) {
26 help_format = man_format;
27 return;
28 }
29 if (!strcmp(format, "info")) {
30 help_format = info_format;
31 return;
32 }
33 if (!strcmp(format, "web") || !strcmp(format, "html")) {
34 help_format = web_format;
35 return;
36 }
37 die("unrecognized help format '%s'", format);
38}
39
40static int git_help_config(const char *var, const char *value)
41{
42 if (!strcmp(var, "help.format")) {
43 help_default_format = xstrdup(value);
44 return 0;
45 }
46 return git_default_config(var, value);
47}
48
82e5a82f 49/* most GUI terminals set COLUMNS (although some don't export it) */
70827b15
LT
50static int term_columns(void)
51{
52 char *col_string = getenv("COLUMNS");
96f1e58f 53 int n_cols;
70827b15
LT
54
55 if (col_string && (n_cols = atoi(col_string)) > 0)
56 return n_cols;
57
58#ifdef TIOCGWINSZ
59 {
60 struct winsize ws;
61 if (!ioctl(1, TIOCGWINSZ, &ws)) {
62 if (ws.ws_col)
63 return ws.ws_col;
64 }
65 }
66#endif
67
68 return 80;
69}
70
70827b15
LT
71static inline void mput_char(char c, unsigned int num)
72{
73 while(num--)
74 putchar(c);
75}
76
1eb05690
SP
77static struct cmdnames {
78 int alloc;
79 int cnt;
80 struct cmdname {
81 size_t len;
82 char name[1];
83 } **names;
84} main_cmds, other_cmds;
85
86static void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 87{
1eb05690
SP
88 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
89
70827b15
LT
90 ent->len = len;
91 memcpy(ent->name, name, len);
92 ent->name[len] = 0;
1eb05690
SP
93
94 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
95 cmds->names[cmds->cnt++] = ent;
70827b15
LT
96}
97
98static int cmdname_compare(const void *a_, const void *b_)
99{
100 struct cmdname *a = *(struct cmdname **)a_;
101 struct cmdname *b = *(struct cmdname **)b_;
102 return strcmp(a->name, b->name);
103}
104
1eb05690
SP
105static void uniq(struct cmdnames *cmds)
106{
107 int i, j;
108
109 if (!cmds->cnt)
110 return;
111
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];
115
116 cmds->cnt = j;
117}
118
f3fa1838
JH
119static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
120{
1eb05690
SP
121 int ci, cj, ei;
122 int cmp;
123
124 ci = cj = ei = 0;
125 while (ci < cmds->cnt && ei < excludes->cnt) {
126 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
127 if (cmp < 0)
128 cmds->names[cj++] = cmds->names[ci++];
129 else if (cmp == 0)
130 ci++, ei++;
131 else if (cmp > 0)
132 ei++;
133 }
134
135 while (ci < cmds->cnt)
136 cmds->names[cj++] = cmds->names[ci++];
137
138 cmds->cnt = cj;
139}
140
141static void pretty_print_string_list(struct cmdnames *cmds, int longest)
70827b15
LT
142{
143 int cols = 1, rows;
144 int space = longest + 1; /* min 1 SP between words */
145 int max_cols = term_columns() - 1; /* don't print *on* the edge */
146 int i, j;
147
148 if (space < max_cols)
149 cols = max_cols / space;
1eb05690 150 rows = (cmds->cnt + cols - 1) / cols;
70827b15
LT
151
152 for (i = 0; i < rows; i++) {
153 printf(" ");
154
155 for (j = 0; j < cols; j++) {
156 int n = j * rows + i;
157 int size = space;
1eb05690 158 if (n >= cmds->cnt)
70827b15 159 break;
1eb05690 160 if (j == cols-1 || n + rows >= cmds->cnt)
70827b15 161 size = 1;
1eb05690 162 printf("%-*s", size, cmds->names[n]->name);
70827b15
LT
163 }
164 putchar('\n');
165 }
166}
167
1eb05690
SP
168static unsigned int list_commands_in_dir(struct cmdnames *cmds,
169 const char *path)
70827b15
LT
170{
171 unsigned int longest = 0;
edb6ddc5
SP
172 const char *prefix = "git-";
173 int prefix_len = strlen(prefix);
1eb05690 174 DIR *dir = opendir(path);
70827b15
LT
175 struct dirent *de;
176
1eb05690
SP
177 if (!dir || chdir(path))
178 return 0;
70827b15 179
70827b15
LT
180 while ((de = readdir(dir)) != NULL) {
181 struct stat st;
182 int entlen;
183
edb6ddc5 184 if (prefixcmp(de->d_name, prefix))
70827b15 185 continue;
0966003c
SP
186
187 if (stat(de->d_name, &st) || /* stat, not lstat */
70827b15
LT
188 !S_ISREG(st.st_mode) ||
189 !(st.st_mode & S_IXUSR))
190 continue;
191
edb6ddc5 192 entlen = strlen(de->d_name) - prefix_len;
5bb1cda5 193 if (has_extension(de->d_name, ".exe"))
70827b15
LT
194 entlen -= 4;
195
196 if (longest < entlen)
197 longest = entlen;
198
1eb05690 199 add_cmdname(cmds, de->d_name + prefix_len, entlen);
70827b15
LT
200 }
201 closedir(dir);
202
1eb05690
SP
203 return longest;
204}
205
206static void list_commands(void)
207{
208 unsigned int longest = 0;
209 unsigned int len;
210 const char *env_path = getenv("PATH");
211 char *paths, *path, *colon;
212 const char *exec_path = git_exec_path();
213
214 if (exec_path)
215 longest = list_commands_in_dir(&main_cmds, exec_path);
216
217 if (!env_path) {
218 fprintf(stderr, "PATH not set\n");
219 exit(1);
220 }
221
222 path = paths = xstrdup(env_path);
223 while (1) {
224 if ((colon = strchr(path, ':')))
225 *colon = 0;
226
227 len = list_commands_in_dir(&other_cmds, path);
228 if (len > longest)
229 longest = len;
230
231 if (!colon)
232 break;
233 path = colon + 1;
234 }
235 free(paths);
236
237 qsort(main_cmds.names, main_cmds.cnt,
238 sizeof(*main_cmds.names), cmdname_compare);
239 uniq(&main_cmds);
240
241 qsort(other_cmds.names, other_cmds.cnt,
242 sizeof(*other_cmds.names), cmdname_compare);
243 uniq(&other_cmds);
244 exclude_cmds(&other_cmds, &main_cmds);
245
246 if (main_cmds.cnt) {
247 printf("available git commands in '%s'\n", exec_path);
248 printf("----------------------------");
249 mput_char('-', strlen(exec_path));
250 putchar('\n');
251 pretty_print_string_list(&main_cmds, longest);
252 putchar('\n');
253 }
254
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);
259 putchar('\n');
260 }
70827b15
LT
261}
262
3d7e2d85 263void list_common_cmds_help(void)
70827b15
LT
264{
265 int i, longest = 0;
266
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);
270 }
271
272 puts("The most commonly used git commands are:");
273 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
4f75b115
RS
274 printf(" %s ", common_cmds[i].name);
275 mput_char(' ', longest - strlen(common_cmds[i].name));
70827b15
LT
276 puts(common_cmds[i].help);
277 }
70827b15
LT
278}
279
df55c9cb 280static const char *cmd_to_page(const char *git_cmd)
70827b15 281{
5d6491c7
CC
282 if (!git_cmd)
283 return "git";
284 else if (!prefixcmp(git_cmd, "git"))
df55c9cb 285 return git_cmd;
70827b15
LT
286 else {
287 int page_len = strlen(git_cmd) + 4;
2d7320d0 288 char *p = xmalloc(page_len + 1);
70827b15
LT
289 strcpy(p, "git-");
290 strcpy(p + 4, git_cmd);
291 p[page_len] = 0;
df55c9cb 292 return p;
70827b15 293 }
df55c9cb 294}
70827b15 295
8e566f24
SO
296static void setup_man_path(void)
297{
298 struct strbuf new_path;
299 const char *old_path = getenv("MANPATH");
300
301 strbuf_init(&new_path, 0);
302
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, ':');
309 if (old_path)
310 strbuf_addstr(&new_path, old_path);
311
312 setenv("MANPATH", new_path.buf, 1);
313
314 strbuf_release(&new_path);
315}
316
df55c9cb
CC
317static void show_man_page(const char *git_cmd)
318{
319 const char *page = cmd_to_page(git_cmd);
8e566f24 320 setup_man_path();
70827b15
LT
321 execlp("man", "man", page, NULL);
322}
323
df55c9cb
CC
324static void show_info_page(const char *git_cmd)
325{
326 const char *page = cmd_to_page(git_cmd);
a149a1a4 327 setenv("INFOPATH", GIT_INFO_PATH, 1);
78d39f98 328 execlp("info", "info", "gitman", page, NULL);
df55c9cb
CC
329}
330
482cce82
CC
331static void get_html_page_path(struct strbuf *page_path, const char *page)
332{
333 struct stat st;
334
335 /* Check that we have a git documentation directory. */
336 if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode))
337 die("'%s': not a documentation directory.", GIT_HTML_PATH);
338
339 strbuf_init(page_path, 0);
340 strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
341}
342
5d6491c7
CC
343static void show_html_page(const char *git_cmd)
344{
345 const char *page = cmd_to_page(git_cmd);
482cce82
CC
346 struct strbuf page_path; /* it leaks but we exec bellow */
347
348 get_html_page_path(&page_path, page);
349
caa87713 350 execl_git_cmd("help--browse", "-c", "help.browser", page_path.buf, NULL);
5d6491c7
CC
351}
352
822a7d50
RJ
353void help_unknown_cmd(const char *cmd)
354{
a238917b 355 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
822a7d50
RJ
356 exit(1);
357}
358
a633fca0 359int cmd_version(int argc, const char **argv, const char *prefix)
70827b15
LT
360{
361 printf("git version %s\n", git_version_string);
362 return 0;
363}
364
5d6491c7 365int cmd_help(int argc, const char **argv, const char *prefix)
70827b15 366{
5d6491c7
CC
367 const char *help_cmd = argv[1];
368
369 if (argc < 2) {
822a7d50
RJ
370 printf("usage: %s\n\n", git_usage_string);
371 list_common_cmds_help();
3d7e2d85 372 exit(0);
822a7d50
RJ
373 }
374
5d6491c7 375 if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
822a7d50 376 printf("usage: %s\n\n", git_usage_string);
1eb05690 377 list_commands();
822a7d50 378 }
df55c9cb 379
5d6491c7
CC
380 else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
381 show_html_page(argc > 2 ? argv[2] : NULL);
382 }
df55c9cb 383
5d6491c7
CC
384 else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
385 show_info_page(argc > 2 ? argv[2] : NULL);
df55c9cb 386 }
822a7d50 387
70087cdb
CC
388 else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
389 show_man_page(argc > 2 ? argv[2] : NULL);
390 }
391
392 else {
393 int nongit;
394
395 setup_git_directory_gently(&nongit);
396 git_config(git_help_config);
397 if (help_default_format)
398 parse_help_format(help_default_format);
399
400 switch (help_format) {
401 case man_format:
402 show_man_page(help_cmd);
403 break;
404 case info_format:
405 show_info_page(help_cmd);
406 break;
407 case web_format:
408 show_html_page(help_cmd);
409 break;
410 }
411 }
822a7d50 412
70827b15
LT
413 return 0;
414}