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
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
49/* most GUI terminals set COLUMNS (although some don't export it) */
50static int term_columns(void)
51{
52 char *col_string = getenv("COLUMNS");
53 int n_cols;
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
71static inline void mput_char(char c, unsigned int num)
72{
73 while(num--)
74 putchar(c);
75}
76
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)
87{
88 struct cmdname *ent = xmalloc(sizeof(*ent) + len);
89
90 ent->len = len;
91 memcpy(ent->name, name, len);
92 ent->name[len] = 0;
93
94 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
95 cmds->names[cmds->cnt++] = ent;
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
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
119static void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
120{
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)
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;
150 rows = (cmds->cnt + cols - 1) / cols;
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;
158 if (n >= cmds->cnt)
159 break;
160 if (j == cols-1 || n + rows >= cmds->cnt)
161 size = 1;
162 printf("%-*s", size, cmds->names[n]->name);
163 }
164 putchar('\n');
165 }
166}
167
168static unsigned int list_commands_in_dir(struct cmdnames *cmds,
169 const char *path)
170{
171 unsigned int longest = 0;
172 const char *prefix = "git-";
173 int prefix_len = strlen(prefix);
174 DIR *dir = opendir(path);
175 struct dirent *de;
176
177 if (!dir || chdir(path))
178 return 0;
179
180 while ((de = readdir(dir)) != NULL) {
181 struct stat st;
182 int entlen;
183
184 if (prefixcmp(de->d_name, prefix))
185 continue;
186
187 if (stat(de->d_name, &st) || /* stat, not lstat */
188 !S_ISREG(st.st_mode) ||
189 !(st.st_mode & S_IXUSR))
190 continue;
191
192 entlen = strlen(de->d_name) - prefix_len;
193 if (has_extension(de->d_name, ".exe"))
194 entlen -= 4;
195
196 if (longest < entlen)
197 longest = entlen;
198
199 add_cmdname(cmds, de->d_name + prefix_len, entlen);
200 }
201 closedir(dir);
202
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 }
261}
262
263void list_common_cmds_help(void)
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++) {
274 printf(" %s ", common_cmds[i].name);
275 mput_char(' ', longest - strlen(common_cmds[i].name));
276 puts(common_cmds[i].help);
277 }
278}
279
280static const char *cmd_to_page(const char *git_cmd)
281{
282 if (!git_cmd)
283 return "git";
284 else if (!prefixcmp(git_cmd, "git"))
285 return git_cmd;
286 else {
287 int page_len = strlen(git_cmd) + 4;
288 char *p = xmalloc(page_len + 1);
289 strcpy(p, "git-");
290 strcpy(p + 4, git_cmd);
291 p[page_len] = 0;
292 return p;
293 }
294}
295
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
317static void show_man_page(const char *git_cmd)
318{
319 const char *page = cmd_to_page(git_cmd);
320 setup_man_path();
321 execlp("man", "man", page, NULL);
322}
323
324static void show_info_page(const char *git_cmd)
325{
326 const char *page = cmd_to_page(git_cmd);
327 setenv("INFOPATH", GIT_INFO_PATH, 1);
328 execlp("info", "info", "gitman", page, NULL);
329}
330
331static void show_html_page(const char *git_cmd)
332{
333 const char *page = cmd_to_page(git_cmd);
334 execl_git_cmd("browse--help", page, NULL);
335}
336
337void help_unknown_cmd(const char *cmd)
338{
339 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
340 exit(1);
341}
342
343int cmd_version(int argc, const char **argv, const char *prefix)
344{
345 printf("git version %s\n", git_version_string);
346 return 0;
347}
348
349int cmd_help(int argc, const char **argv, const char *prefix)
350{
351 const char *help_cmd = argv[1];
352
353 if (argc < 2) {
354 printf("usage: %s\n\n", git_usage_string);
355 list_common_cmds_help();
356 exit(0);
357 }
358
359 if (!strcmp(help_cmd, "--all") || !strcmp(help_cmd, "-a")) {
360 printf("usage: %s\n\n", git_usage_string);
361 list_commands();
362 }
363
364 else if (!strcmp(help_cmd, "--web") || !strcmp(help_cmd, "-w")) {
365 show_html_page(argc > 2 ? argv[2] : NULL);
366 }
367
368 else if (!strcmp(help_cmd, "--info") || !strcmp(help_cmd, "-i")) {
369 show_info_page(argc > 2 ? argv[2] : NULL);
370 }
371
372 else if (!strcmp(help_cmd, "--man") || !strcmp(help_cmd, "-m")) {
373 show_man_page(argc > 2 ? argv[2] : NULL);
374 }
375
376 else {
377 int nongit;
378
379 setup_git_directory_gently(&nongit);
380 git_config(git_help_config);
381 if (help_default_format)
382 parse_help_format(help_default_format);
383
384 switch (help_format) {
385 case man_format:
386 show_man_page(help_cmd);
387 break;
388 case info_format:
389 show_info_page(help_cmd);
390 break;
391 case web_format:
392 show_html_page(help_cmd);
393 break;
394 }
395 }
396
397 return 0;
398}