Make main_cmds and other_cmds local to builtin-help.c
[git/git.git] / help.c
CommitLineData
70827b15
LT
1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
940208a7 4#include "help.h"
64949984 5
82e5a82f 6/* most GUI terminals set COLUMNS (although some don't export it) */
70827b15
LT
7static int term_columns(void)
8{
9 char *col_string = getenv("COLUMNS");
96f1e58f 10 int n_cols;
70827b15
LT
11
12 if (col_string && (n_cols = atoi(col_string)) > 0)
13 return n_cols;
14
15#ifdef TIOCGWINSZ
16 {
17 struct winsize ws;
18 if (!ioctl(1, TIOCGWINSZ, &ws)) {
19 if (ws.ws_col)
20 return ws.ws_col;
21 }
22 }
23#endif
24
25 return 80;
26}
27
940208a7 28void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 29{
940208a7 30 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
1eb05690 31
70827b15
LT
32 ent->len = len;
33 memcpy(ent->name, name, len);
34 ent->name[len] = 0;
1eb05690
SP
35
36 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
37 cmds->names[cmds->cnt++] = ent;
70827b15
LT
38}
39
40static int cmdname_compare(const void *a_, const void *b_)
41{
42 struct cmdname *a = *(struct cmdname **)a_;
43 struct cmdname *b = *(struct cmdname **)b_;
44 return strcmp(a->name, b->name);
45}
46
1eb05690
SP
47static void uniq(struct cmdnames *cmds)
48{
49 int i, j;
50
51 if (!cmds->cnt)
52 return;
53
54 for (i = j = 1; i < cmds->cnt; i++)
55 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
56 cmds->names[j++] = cmds->names[i];
57
58 cmds->cnt = j;
59}
60
940208a7 61void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 62{
1eb05690
SP
63 int ci, cj, ei;
64 int cmp;
65
66 ci = cj = ei = 0;
67 while (ci < cmds->cnt && ei < excludes->cnt) {
68 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
69 if (cmp < 0)
70 cmds->names[cj++] = cmds->names[ci++];
71 else if (cmp == 0)
72 ci++, ei++;
73 else if (cmp > 0)
74 ei++;
75 }
76
77 while (ci < cmds->cnt)
78 cmds->names[cj++] = cmds->names[ci++];
79
80 cmds->cnt = cj;
81}
82
83static void pretty_print_string_list(struct cmdnames *cmds, int longest)
70827b15
LT
84{
85 int cols = 1, rows;
86 int space = longest + 1; /* min 1 SP between words */
87 int max_cols = term_columns() - 1; /* don't print *on* the edge */
88 int i, j;
89
90 if (space < max_cols)
91 cols = max_cols / space;
1eb05690 92 rows = (cmds->cnt + cols - 1) / cols;
70827b15
LT
93
94 for (i = 0; i < rows; i++) {
95 printf(" ");
96
97 for (j = 0; j < cols; j++) {
98 int n = j * rows + i;
99 int size = space;
1eb05690 100 if (n >= cmds->cnt)
70827b15 101 break;
1eb05690 102 if (j == cols-1 || n + rows >= cmds->cnt)
70827b15 103 size = 1;
1eb05690 104 printf("%-*s", size, cmds->names[n]->name);
70827b15
LT
105 }
106 putchar('\n');
107 }
108}
109
cc3b7a97
JS
110static int is_executable(const char *name)
111{
112 struct stat st;
113
114 if (stat(name, &st) || /* stat, not lstat */
115 !S_ISREG(st.st_mode))
116 return 0;
117
118#ifdef __MINGW32__
119 /* cannot trust the executable bit, peek into the file instead */
120 char buf[3] = { 0 };
121 int n;
122 int fd = open(name, O_RDONLY);
123 st.st_mode &= ~S_IXUSR;
124 if (fd >= 0) {
125 n = read(fd, buf, 2);
126 if (n == 2)
127 /* DOS executables start with "MZ" */
128 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
129 st.st_mode |= S_IXUSR;
130 close(fd);
131 }
132#endif
133 return st.st_mode & S_IXUSR;
134}
135
e321180e 136static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
137 const char *path,
138 const char *prefix)
70827b15 139{
940208a7 140 int prefix_len;
1eb05690 141 DIR *dir = opendir(path);
70827b15 142 struct dirent *de;
f5d600e2
JS
143 struct strbuf buf = STRBUF_INIT;
144 int len;
70827b15 145
f5d600e2 146 if (!dir)
e321180e 147 return;
940208a7
MV
148 if (!prefix)
149 prefix = "git-";
150 prefix_len = strlen(prefix);
70827b15 151
f5d600e2
JS
152 strbuf_addf(&buf, "%s/", path);
153 len = buf.len;
154
70827b15 155 while ((de = readdir(dir)) != NULL) {
70827b15
LT
156 int entlen;
157
edb6ddc5 158 if (prefixcmp(de->d_name, prefix))
70827b15 159 continue;
0966003c 160
f5d600e2
JS
161 strbuf_setlen(&buf, len);
162 strbuf_addstr(&buf, de->d_name);
163 if (!is_executable(buf.buf))
70827b15
LT
164 continue;
165
edb6ddc5 166 entlen = strlen(de->d_name) - prefix_len;
5bb1cda5 167 if (has_extension(de->d_name, ".exe"))
70827b15
LT
168 entlen -= 4;
169
1eb05690 170 add_cmdname(cmds, de->d_name + prefix_len, entlen);
70827b15
LT
171 }
172 closedir(dir);
f5d600e2 173 strbuf_release(&buf);
1eb05690
SP
174}
175
e321180e 176void load_command_list(const char *prefix,
940208a7
MV
177 struct cmdnames *main_cmds,
178 struct cmdnames *other_cmds)
1eb05690 179{
1eb05690
SP
180 const char *env_path = getenv("PATH");
181 char *paths, *path, *colon;
182 const char *exec_path = git_exec_path();
183
184 if (exec_path)
e321180e 185 list_commands_in_dir(main_cmds, exec_path, prefix);
1eb05690
SP
186
187 if (!env_path) {
188 fprintf(stderr, "PATH not set\n");
189 exit(1);
190 }
191
192 path = paths = xstrdup(env_path);
193 while (1) {
cc3b7a97 194 if ((colon = strchr(path, PATH_SEP)))
1eb05690
SP
195 *colon = 0;
196
e321180e 197 list_commands_in_dir(other_cmds, path, prefix);
1eb05690
SP
198
199 if (!colon)
200 break;
201 path = colon + 1;
202 }
203 free(paths);
204
940208a7
MV
205 qsort(main_cmds->names, main_cmds->cnt,
206 sizeof(*main_cmds->names), cmdname_compare);
207 uniq(main_cmds);
1eb05690 208
940208a7
MV
209 qsort(other_cmds->names, other_cmds->cnt,
210 sizeof(*other_cmds->names), cmdname_compare);
211 uniq(other_cmds);
212 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
213}
214
e321180e
AR
215void list_commands(const char *title, struct cmdnames *main_cmds,
216 struct cmdnames *other_cmds)
2156435f 217{
2156435f 218 const char *exec_path = git_exec_path();
e321180e
AR
219 int i, longest = 0;
220
221 for (i = 0; i < main_cmds->cnt; i++)
222 if (longest < main_cmds->names[i]->len)
223 longest = main_cmds->names[i]->len;
224 for (i = 0; i < other_cmds->cnt; i++)
225 if (longest < other_cmds->names[i]->len)
226 longest = other_cmds->names[i]->len;
2156435f 227
940208a7
MV
228 if (main_cmds->cnt) {
229 printf("available %s in '%s'\n", title, exec_path);
230 printf("----------------");
231 mput_char('-', strlen(title) + strlen(exec_path));
1eb05690 232 putchar('\n');
940208a7 233 pretty_print_string_list(main_cmds, longest);
1eb05690
SP
234 putchar('\n');
235 }
236
940208a7
MV
237 if (other_cmds->cnt) {
238 printf("%s available from elsewhere on your $PATH\n", title);
239 printf("---------------------------------------");
240 mput_char('-', strlen(title));
241 putchar('\n');
242 pretty_print_string_list(other_cmds, longest);
1eb05690
SP
243 putchar('\n');
244 }
70827b15
LT
245}
246
940208a7 247int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
248{
249 int i;
250 for (i = 0; i < c->cnt; i++)
251 if (!strcmp(s, c->names[i]->name))
252 return 1;
253 return 0;
254}
255
822a7d50
RJ
256void help_unknown_cmd(const char *cmd)
257{
a238917b 258 fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd);
822a7d50
RJ
259 exit(1);
260}
261
a633fca0 262int cmd_version(int argc, const char **argv, const char *prefix)
70827b15
LT
263{
264 printf("git version %s\n", git_version_string);
265 return 0;
266}