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