1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
4#include "levenshtein.h"
5#include "help.h"
6#include "common-cmds.h"
7
8void add_cmdname(struct cmdnames *cmds, const char *name, int len)
9{
10 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
11
12 ent->len = len;
13 memcpy(ent->name, name, len);
14 ent->name[len] = 0;
15
16 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
17 cmds->names[cmds->cnt++] = ent;
18}
19
20static void clean_cmdnames(struct cmdnames *cmds)
21{
22 int i;
23 for (i = 0; i < cmds->cnt; ++i)
24 free(cmds->names[i]);
25 free(cmds->names);
26 cmds->cnt = 0;
27 cmds->alloc = 0;
28}
29
30static int cmdname_compare(const void *a_, const void *b_)
31{
32 struct cmdname *a = *(struct cmdname **)a_;
33 struct cmdname *b = *(struct cmdname **)b_;
34 return strcmp(a->name, b->name);
35}
36
37static void uniq(struct cmdnames *cmds)
38{
39 int i, j;
40
41 if (!cmds->cnt)
42 return;
43
44 for (i = j = 1; i < cmds->cnt; i++)
45 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
46 cmds->names[j++] = cmds->names[i];
47
48 cmds->cnt = j;
49}
50
51void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
52{
53 int ci, cj, ei;
54 int cmp;
55
56 ci = cj = ei = 0;
57 while (ci < cmds->cnt && ei < excludes->cnt) {
58 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
59 if (cmp < 0)
60 cmds->names[cj++] = cmds->names[ci++];
61 else if (cmp == 0)
62 ci++, ei++;
63 else if (cmp > 0)
64 ei++;
65 }
66
67 while (ci < cmds->cnt)
68 cmds->names[cj++] = cmds->names[ci++];
69
70 cmds->cnt = cj;
71}
72
73static void pretty_print_string_list(struct cmdnames *cmds, int longest)
74{
75 int cols = 1, rows;
76 int space = longest + 1; /* min 1 SP between words */
77 int max_cols = term_columns() - 1; /* don't print *on* the edge */
78 int i, j;
79
80 if (space < max_cols)
81 cols = max_cols / space;
82 rows = DIV_ROUND_UP(cmds->cnt, cols);
83
84 for (i = 0; i < rows; i++) {
85 printf(" ");
86
87 for (j = 0; j < cols; j++) {
88 int n = j * rows + i;
89 int size = space;
90 if (n >= cmds->cnt)
91 break;
92 if (j == cols-1 || n + rows >= cmds->cnt)
93 size = 1;
94 printf("%-*s", size, cmds->names[n]->name);
95 }
96 putchar('\n');
97 }
98}
99
100static int is_executable(const char *name)
101{
102 struct stat st;
103
104 if (stat(name, &st) || /* stat, not lstat */
105 !S_ISREG(st.st_mode))
106 return 0;
107
108#if defined(WIN32) || defined(__CYGWIN__)
109#if defined(__CYGWIN__)
110if ((st.st_mode & S_IXUSR) == 0)
111#endif
112{ /* cannot trust the executable bit, peek into the file instead */
113 char buf[3] = { 0 };
114 int n;
115 int fd = open(name, O_RDONLY);
116 st.st_mode &= ~S_IXUSR;
117 if (fd >= 0) {
118 n = read(fd, buf, 2);
119 if (n == 2)
120 /* DOS executables start with "MZ" */
121 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
122 st.st_mode |= S_IXUSR;
123 close(fd);
124 }
125}
126#endif
127 return st.st_mode & S_IXUSR;
128}
129
130static void list_commands_in_dir(struct cmdnames *cmds,
131 const char *path,
132 const char *prefix)
133{
134 int prefix_len;
135 DIR *dir = opendir(path);
136 struct dirent *de;
137 struct strbuf buf = STRBUF_INIT;
138 int len;
139
140 if (!dir)
141 return;
142 if (!prefix)
143 prefix = "git-";
144 prefix_len = strlen(prefix);
145
146 strbuf_addf(&buf, "%s/", path);
147 len = buf.len;
148
149 while ((de = readdir(dir)) != NULL) {
150 int entlen;
151
152 if (prefixcmp(de->d_name, prefix))
153 continue;
154
155 strbuf_setlen(&buf, len);
156 strbuf_addstr(&buf, de->d_name);
157 if (!is_executable(buf.buf))
158 continue;
159
160 entlen = strlen(de->d_name) - prefix_len;
161 if (has_extension(de->d_name, ".exe"))
162 entlen -= 4;
163
164 add_cmdname(cmds, de->d_name + prefix_len, entlen);
165 }
166 closedir(dir);
167 strbuf_release(&buf);
168}
169
170void load_command_list(const char *prefix,
171 struct cmdnames *main_cmds,
172 struct cmdnames *other_cmds)
173{
174 const char *env_path = getenv("PATH");
175 const char *exec_path = git_exec_path();
176
177 if (exec_path) {
178 list_commands_in_dir(main_cmds, exec_path, prefix);
179 qsort(main_cmds->names, main_cmds->cnt,
180 sizeof(*main_cmds->names), cmdname_compare);
181 uniq(main_cmds);
182 }
183
184 if (env_path) {
185 char *paths, *path, *colon;
186 path = paths = xstrdup(env_path);
187 while (1) {
188 if ((colon = strchr(path, PATH_SEP)))
189 *colon = 0;
190 if (!exec_path || strcmp(path, exec_path))
191 list_commands_in_dir(other_cmds, path, prefix);
192
193 if (!colon)
194 break;
195 path = colon + 1;
196 }
197 free(paths);
198
199 qsort(other_cmds->names, other_cmds->cnt,
200 sizeof(*other_cmds->names), cmdname_compare);
201 uniq(other_cmds);
202 }
203 exclude_cmds(other_cmds, main_cmds);
204}
205
206void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
207{
208 int i, longest = 0;
209
210 for (i = 0; i < main_cmds->cnt; i++)
211 if (longest < main_cmds->names[i]->len)
212 longest = main_cmds->names[i]->len;
213 for (i = 0; i < other_cmds->cnt; i++)
214 if (longest < other_cmds->names[i]->len)
215 longest = other_cmds->names[i]->len;
216
217 if (main_cmds->cnt) {
218 const char *exec_path = git_exec_path();
219 printf_ln(_("available git commands in '%s'"), exec_path);
220 putchar('\n');
221 pretty_print_string_list(main_cmds, longest);
222 putchar('\n');
223 }
224
225 if (other_cmds->cnt) {
226 printf_ln(_("git commands available from elsewhere on your $PATH"));
227 putchar('\n');
228 pretty_print_string_list(other_cmds, longest);
229 putchar('\n');
230 }
231}
232
233int is_in_cmdlist(struct cmdnames *c, const char *s)
234{
235 int i;
236 for (i = 0; i < c->cnt; i++)
237 if (!strcmp(s, c->names[i]->name))
238 return 1;
239 return 0;
240}
241
242static int autocorrect;
243static struct cmdnames aliases;
244
245static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
246{
247 if (!strcmp(var, "help.autocorrect"))
248 autocorrect = git_config_int(var,value);
249 /* Also use aliases for command lookup */
250 if (!prefixcmp(var, "alias."))
251 add_cmdname(&aliases, var + 6, strlen(var + 6));
252
253 return git_default_config(var, value, cb);
254}
255
256static int levenshtein_compare(const void *p1, const void *p2)
257{
258 const struct cmdname *const *c1 = p1, *const *c2 = p2;
259 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
260 int l1 = (*c1)->len;
261 int l2 = (*c2)->len;
262 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
263}
264
265static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
266{
267 int i;
268 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
269
270 for (i = 0; i < old->cnt; i++)
271 cmds->names[cmds->cnt++] = old->names[i];
272 free(old->names);
273 old->cnt = 0;
274 old->names = NULL;
275}
276
277/* An empirically derived magic number */
278#define SIMILARITY_FLOOR 7
279#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
280
281static const char bad_interpreter_advice[] =
282 N_("'%s' appears to be a git command, but we were not\n"
283 "able to execute it. Maybe git-%s is broken?");
284
285const char *help_unknown_cmd(const char *cmd)
286{
287 int i, n, best_similarity = 0;
288 struct cmdnames main_cmds, other_cmds;
289
290 memset(&main_cmds, 0, sizeof(main_cmds));
291 memset(&other_cmds, 0, sizeof(other_cmds));
292 memset(&aliases, 0, sizeof(aliases));
293
294 git_config(git_unknown_cmd_config, NULL);
295
296 load_command_list("git-", &main_cmds, &other_cmds);
297
298 add_cmd_list(&main_cmds, &aliases);
299 add_cmd_list(&main_cmds, &other_cmds);
300 qsort(main_cmds.names, main_cmds.cnt,
301 sizeof(main_cmds.names), cmdname_compare);
302 uniq(&main_cmds);
303
304 /* This abuses cmdname->len for levenshtein distance */
305 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
306 int cmp = 0; /* avoid compiler stupidity */
307 const char *candidate = main_cmds.names[i]->name;
308
309 /*
310 * An exact match means we have the command, but
311 * for some reason exec'ing it gave us ENOENT; probably
312 * it's a bad interpreter in the #! line.
313 */
314 if (!strcmp(candidate, cmd))
315 die(_(bad_interpreter_advice), cmd, cmd);
316
317 /* Does the candidate appear in common_cmds list? */
318 while (n < ARRAY_SIZE(common_cmds) &&
319 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
320 n++;
321 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
322 /* Yes, this is one of the common commands */
323 n++; /* use the entry from common_cmds[] */
324 if (!prefixcmp(candidate, cmd)) {
325 /* Give prefix match a very good score */
326 main_cmds.names[i]->len = 0;
327 continue;
328 }
329 }
330
331 main_cmds.names[i]->len =
332 levenshtein(cmd, candidate, 0, 2, 1, 4) + 1;
333 }
334
335 qsort(main_cmds.names, main_cmds.cnt,
336 sizeof(*main_cmds.names), levenshtein_compare);
337
338 if (!main_cmds.cnt)
339 die(_("Uh oh. Your system reports no Git commands at all."));
340
341 /* skip and count prefix matches */
342 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
343 ; /* still counting */
344
345 if (main_cmds.cnt <= n) {
346 /* prefix matches with everything? that is too ambiguous */
347 best_similarity = SIMILARITY_FLOOR + 1;
348 } else {
349 /* count all the most similar ones */
350 for (best_similarity = main_cmds.names[n++]->len;
351 (n < main_cmds.cnt &&
352 best_similarity == main_cmds.names[n]->len);
353 n++)
354 ; /* still counting */
355 }
356 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
357 const char *assumed = main_cmds.names[0]->name;
358 main_cmds.names[0] = NULL;
359 clean_cmdnames(&main_cmds);
360 fprintf_ln(stderr,
361 _("WARNING: You called a Git command named '%s', "
362 "which does not exist.\n"
363 "Continuing under the assumption that you meant '%s'"),
364 cmd, assumed);
365 if (autocorrect > 0) {
366 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
367 (float)autocorrect/10.0);
368 poll(NULL, 0, autocorrect * 100);
369 }
370 return assumed;
371 }
372
373 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
374
375 if (SIMILAR_ENOUGH(best_similarity)) {
376 fprintf_ln(stderr,
377 Q_("\nDid you mean this?",
378 "\nDid you mean one of these?",
379 n));
380
381 for (i = 0; i < n; i++)
382 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
383 }
384
385 exit(1);
386}
387
388int cmd_version(int argc, const char **argv, const char *prefix)
389{
390 printf("git version %s\n", git_version_string);
391 return 0;
392}