🌐 AI搜索 & 代理 主页
blob: 7654f1bfd608d151a213937614449cc497bb638b [file] [log] [blame]
Linus Torvalds70827b12006-04-21 17:27:341#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
Johannes Schindelin8af84da2008-08-31 13:50:234#include "levenshtein.h"
Miklos Vajna940208a2008-07-29 23:16:585#include "help.h"
Erik Faye-Lund6612b9e2010-11-26 16:00:396#include "common-cmds.h"
Christian Couder64949982008-03-07 07:46:287
Pavel Roskin82e5a822006-07-10 05:50:188/* most GUI terminals set COLUMNS (although some don't export it) */
Linus Torvalds70827b12006-04-21 17:27:349static int term_columns(void)
10{
11 char *col_string = getenv("COLUMNS");
David Rientjes96f1e582006-08-15 17:23:4812 int n_cols;
Linus Torvalds70827b12006-04-21 17:27:3413
14 if (col_string && (n_cols = atoi(col_string)) > 0)
15 return n_cols;
16
17#ifdef TIOCGWINSZ
18 {
19 struct winsize ws;
20 if (!ioctl(1, TIOCGWINSZ, &ws)) {
21 if (ws.ws_col)
22 return ws.ws_col;
23 }
24 }
25#endif
26
27 return 80;
28}
29
Miklos Vajna940208a2008-07-29 23:16:5830void add_cmdname(struct cmdnames *cmds, const char *name, int len)
Linus Torvalds70827b12006-04-21 17:27:3431{
Miklos Vajna940208a2008-07-29 23:16:5832 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
Scott R Parish1eb05692007-10-29 03:30:5233
Linus Torvalds70827b12006-04-21 17:27:3434 ent->len = len;
35 memcpy(ent->name, name, len);
36 ent->name[len] = 0;
Scott R Parish1eb05692007-10-29 03:30:5237
38 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
39 cmds->names[cmds->cnt++] = ent;
Linus Torvalds70827b12006-04-21 17:27:3440}
41
Johannes Schindelin8af84da2008-08-31 13:50:2342static void clean_cmdnames(struct cmdnames *cmds)
43{
44 int i;
45 for (i = 0; i < cmds->cnt; ++i)
46 free(cmds->names[i]);
47 free(cmds->names);
48 cmds->cnt = 0;
49 cmds->alloc = 0;
50}
51
Linus Torvalds70827b12006-04-21 17:27:3452static int cmdname_compare(const void *a_, const void *b_)
53{
54 struct cmdname *a = *(struct cmdname **)a_;
55 struct cmdname *b = *(struct cmdname **)b_;
56 return strcmp(a->name, b->name);
57}
58
Scott R Parish1eb05692007-10-29 03:30:5259static void uniq(struct cmdnames *cmds)
60{
61 int i, j;
62
63 if (!cmds->cnt)
64 return;
65
66 for (i = j = 1; i < cmds->cnt; i++)
67 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
68 cmds->names[j++] = cmds->names[i];
69
70 cmds->cnt = j;
71}
72
Miklos Vajna940208a2008-07-29 23:16:5873void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
Junio C Hamanof3fa1832007-11-08 23:35:3274{
Scott R Parish1eb05692007-10-29 03:30:5275 int ci, cj, ei;
76 int cmp;
77
78 ci = cj = ei = 0;
79 while (ci < cmds->cnt && ei < excludes->cnt) {
80 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
81 if (cmp < 0)
82 cmds->names[cj++] = cmds->names[ci++];
83 else if (cmp == 0)
84 ci++, ei++;
85 else if (cmp > 0)
86 ei++;
87 }
88
89 while (ci < cmds->cnt)
90 cmds->names[cj++] = cmds->names[ci++];
91
92 cmds->cnt = cj;
93}
94
95static void pretty_print_string_list(struct cmdnames *cmds, int longest)
Linus Torvalds70827b12006-04-21 17:27:3496{
97 int cols = 1, rows;
98 int space = longest + 1; /* min 1 SP between words */
99 int max_cols = term_columns() - 1; /* don't print *on* the edge */
100 int i, j;
101
102 if (space < max_cols)
103 cols = max_cols / space;
Pierre Habouzit98cb6f32009-07-22 21:34:35104 rows = DIV_ROUND_UP(cmds->cnt, cols);
Linus Torvalds70827b12006-04-21 17:27:34105
106 for (i = 0; i < rows; i++) {
107 printf(" ");
108
109 for (j = 0; j < cols; j++) {
110 int n = j * rows + i;
111 int size = space;
Scott R Parish1eb05692007-10-29 03:30:52112 if (n >= cmds->cnt)
Linus Torvalds70827b12006-04-21 17:27:34113 break;
Scott R Parish1eb05692007-10-29 03:30:52114 if (j == cols-1 || n + rows >= cmds->cnt)
Linus Torvalds70827b12006-04-21 17:27:34115 size = 1;
Scott R Parish1eb05692007-10-29 03:30:52116 printf("%-*s", size, cmds->names[n]->name);
Linus Torvalds70827b12006-04-21 17:27:34117 }
118 putchar('\n');
119 }
120}
121
Johannes Sixtcc3b7a92008-01-14 13:05:33122static int is_executable(const char *name)
123{
124 struct stat st;
125
126 if (stat(name, &st) || /* stat, not lstat */
127 !S_ISREG(st.st_mode))
128 return 0;
129
Frank Li71064e32009-09-16 08:20:22130#ifdef WIN32
Frank Li0d30ad72009-09-16 08:20:17131{ /* cannot trust the executable bit, peek into the file instead */
Johannes Sixtcc3b7a92008-01-14 13:05:33132 char buf[3] = { 0 };
133 int n;
134 int fd = open(name, O_RDONLY);
135 st.st_mode &= ~S_IXUSR;
136 if (fd >= 0) {
137 n = read(fd, buf, 2);
138 if (n == 2)
139 /* DOS executables start with "MZ" */
140 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
141 st.st_mode |= S_IXUSR;
142 close(fd);
143 }
Frank Li0d30ad72009-09-16 08:20:17144}
Johannes Sixtcc3b7a92008-01-14 13:05:33145#endif
146 return st.st_mode & S_IXUSR;
147}
148
Alex Riesene3211802008-08-28 17:15:33149static void list_commands_in_dir(struct cmdnames *cmds,
Miklos Vajna940208a2008-07-29 23:16:58150 const char *path,
151 const char *prefix)
Linus Torvalds70827b12006-04-21 17:27:34152{
Miklos Vajna940208a2008-07-29 23:16:58153 int prefix_len;
Scott R Parish1eb05692007-10-29 03:30:52154 DIR *dir = opendir(path);
Linus Torvalds70827b12006-04-21 17:27:34155 struct dirent *de;
Johannes Schindelinf5d600e2008-07-27 20:34:14156 struct strbuf buf = STRBUF_INIT;
157 int len;
Linus Torvalds70827b12006-04-21 17:27:34158
Johannes Schindelinf5d600e2008-07-27 20:34:14159 if (!dir)
Alex Riesene3211802008-08-28 17:15:33160 return;
Miklos Vajna940208a2008-07-29 23:16:58161 if (!prefix)
162 prefix = "git-";
163 prefix_len = strlen(prefix);
Linus Torvalds70827b12006-04-21 17:27:34164
Johannes Schindelinf5d600e2008-07-27 20:34:14165 strbuf_addf(&buf, "%s/", path);
166 len = buf.len;
167
Linus Torvalds70827b12006-04-21 17:27:34168 while ((de = readdir(dir)) != NULL) {
Linus Torvalds70827b12006-04-21 17:27:34169 int entlen;
170
Scott R Parishedb6ddc2007-10-27 08:36:50171 if (prefixcmp(de->d_name, prefix))
Linus Torvalds70827b12006-04-21 17:27:34172 continue;
Scott R Parish09660032007-10-27 08:36:52173
Johannes Schindelinf5d600e2008-07-27 20:34:14174 strbuf_setlen(&buf, len);
175 strbuf_addstr(&buf, de->d_name);
176 if (!is_executable(buf.buf))
Linus Torvalds70827b12006-04-21 17:27:34177 continue;
178
Scott R Parishedb6ddc2007-10-27 08:36:50179 entlen = strlen(de->d_name) - prefix_len;
Rene Scharfe5bb1cda2006-08-11 12:01:45180 if (has_extension(de->d_name, ".exe"))
Linus Torvalds70827b12006-04-21 17:27:34181 entlen -= 4;
182
Scott R Parish1eb05692007-10-29 03:30:52183 add_cmdname(cmds, de->d_name + prefix_len, entlen);
Linus Torvalds70827b12006-04-21 17:27:34184 }
185 closedir(dir);
Johannes Schindelinf5d600e2008-07-27 20:34:14186 strbuf_release(&buf);
Scott R Parish1eb05692007-10-29 03:30:52187}
188
Alex Riesene3211802008-08-28 17:15:33189void load_command_list(const char *prefix,
Miklos Vajna940208a2008-07-29 23:16:58190 struct cmdnames *main_cmds,
191 struct cmdnames *other_cmds)
Scott R Parish1eb05692007-10-29 03:30:52192{
Scott R Parish1eb05692007-10-29 03:30:52193 const char *env_path = getenv("PATH");
Scott R Parish1eb05692007-10-29 03:30:52194 const char *exec_path = git_exec_path();
195
Alex Riesen1f08e5c2008-08-28 17:19:07196 if (exec_path) {
Alex Riesene3211802008-08-28 17:15:33197 list_commands_in_dir(main_cmds, exec_path, prefix);
Alex Riesen1f08e5c2008-08-28 17:19:07198 qsort(main_cmds->names, main_cmds->cnt,
199 sizeof(*main_cmds->names), cmdname_compare);
200 uniq(main_cmds);
Scott R Parish1eb05692007-10-29 03:30:52201 }
202
Alex Riesen1f08e5c2008-08-28 17:19:07203 if (env_path) {
204 char *paths, *path, *colon;
205 path = paths = xstrdup(env_path);
206 while (1) {
207 if ((colon = strchr(path, PATH_SEP)))
208 *colon = 0;
Pieter de Bie746c2212008-09-10 15:54:28209 if (!exec_path || strcmp(path, exec_path))
210 list_commands_in_dir(other_cmds, path, prefix);
Scott R Parish1eb05692007-10-29 03:30:52211
Alex Riesen1f08e5c2008-08-28 17:19:07212 if (!colon)
213 break;
214 path = colon + 1;
215 }
216 free(paths);
217
218 qsort(other_cmds->names, other_cmds->cnt,
219 sizeof(*other_cmds->names), cmdname_compare);
220 uniq(other_cmds);
Scott R Parish1eb05692007-10-29 03:30:52221 }
Miklos Vajna940208a2008-07-29 23:16:58222 exclude_cmds(other_cmds, main_cmds);
Jeff King21564352008-02-24 22:17:37223}
224
Alex Riesene3211802008-08-28 17:15:33225void list_commands(const char *title, struct cmdnames *main_cmds,
226 struct cmdnames *other_cmds)
Jeff King21564352008-02-24 22:17:37227{
Alex Riesene3211802008-08-28 17:15:33228 int i, longest = 0;
229
230 for (i = 0; i < main_cmds->cnt; i++)
231 if (longest < main_cmds->names[i]->len)
232 longest = main_cmds->names[i]->len;
233 for (i = 0; i < other_cmds->cnt; i++)
234 if (longest < other_cmds->names[i]->len)
235 longest = other_cmds->names[i]->len;
Jeff King21564352008-02-24 22:17:37236
Miklos Vajna940208a2008-07-29 23:16:58237 if (main_cmds->cnt) {
Alex Riesen61c5d432008-08-28 17:19:42238 const char *exec_path = git_exec_path();
Miklos Vajna940208a2008-07-29 23:16:58239 printf("available %s in '%s'\n", title, exec_path);
240 printf("----------------");
241 mput_char('-', strlen(title) + strlen(exec_path));
Scott R Parish1eb05692007-10-29 03:30:52242 putchar('\n');
Miklos Vajna940208a2008-07-29 23:16:58243 pretty_print_string_list(main_cmds, longest);
Scott R Parish1eb05692007-10-29 03:30:52244 putchar('\n');
245 }
246
Miklos Vajna940208a2008-07-29 23:16:58247 if (other_cmds->cnt) {
248 printf("%s available from elsewhere on your $PATH\n", title);
249 printf("---------------------------------------");
250 mput_char('-', strlen(title));
251 putchar('\n');
252 pretty_print_string_list(other_cmds, longest);
Scott R Parish1eb05692007-10-29 03:30:52253 putchar('\n');
254 }
Linus Torvalds70827b12006-04-21 17:27:34255}
256
Miklos Vajna940208a2008-07-29 23:16:58257int is_in_cmdlist(struct cmdnames *c, const char *s)
Jeff King21564352008-02-24 22:17:37258{
259 int i;
260 for (i = 0; i < c->cnt; i++)
261 if (!strcmp(s, c->names[i]->name))
262 return 1;
263 return 0;
264}
265
Alex Riesenf0e90712008-08-31 13:54:58266static int autocorrect;
Pieter de Bie746c2212008-09-10 15:54:28267static struct cmdnames aliases;
Alex Riesenf0e90712008-08-31 13:54:58268
269static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
Ramsay Allan Jones822a7d52006-07-30 21:42:25270{
Alex Riesenf0e90712008-08-31 13:54:58271 if (!strcmp(var, "help.autocorrect"))
272 autocorrect = git_config_int(var,value);
Pieter de Bie746c2212008-09-10 15:54:28273 /* Also use aliases for command lookup */
274 if (!prefixcmp(var, "alias."))
275 add_cmdname(&aliases, var + 6, strlen(var + 6));
Alex Riesenf0e90712008-08-31 13:54:58276
277 return git_default_config(var, value, cb);
278}
279
Johannes Schindelin8af84da2008-08-31 13:50:23280static int levenshtein_compare(const void *p1, const void *p2)
Ramsay Allan Jones822a7d52006-07-30 21:42:25281{
Johannes Schindelin8af84da2008-08-31 13:50:23282 const struct cmdname *const *c1 = p1, *const *c2 = p2;
283 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
284 int l1 = (*c1)->len;
285 int l2 = (*c2)->len;
286 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
287}
288
Pieter de Bie746c2212008-09-10 15:54:28289static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
290{
291 int i;
292 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
293
294 for (i = 0; i < old->cnt; i++)
295 cmds->names[cmds->cnt++] = old->names[i];
296 free(old->names);
297 old->cnt = 0;
298 old->names = NULL;
299}
300
Johannes Sixt06500a02009-12-15 07:57:18301/* An empirically derived magic number */
Erik Faye-Lund6612b9e2010-11-26 16:00:39302#define SIMILARITY_FLOOR 7
303#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
Johannes Sixt06500a02009-12-15 07:57:18304
Johannes Schindelin8af84da2008-08-31 13:50:23305const char *help_unknown_cmd(const char *cmd)
306{
307 int i, n, best_similarity = 0;
308 struct cmdnames main_cmds, other_cmds;
309
310 memset(&main_cmds, 0, sizeof(main_cmds));
Johan Herland0b74f5d2009-08-11 10:10:21311 memset(&other_cmds, 0, sizeof(other_cmds));
Pieter de Bie746c2212008-09-10 15:54:28312 memset(&aliases, 0, sizeof(aliases));
Johannes Schindelin8af84da2008-08-31 13:50:23313
Alex Riesenf0e90712008-08-31 13:54:58314 git_config(git_unknown_cmd_config, NULL);
315
Johannes Schindelin8af84da2008-08-31 13:50:23316 load_command_list("git-", &main_cmds, &other_cmds);
317
Pieter de Bie746c2212008-09-10 15:54:28318 add_cmd_list(&main_cmds, &aliases);
319 add_cmd_list(&main_cmds, &other_cmds);
320 qsort(main_cmds.names, main_cmds.cnt,
321 sizeof(main_cmds.names), cmdname_compare);
322 uniq(&main_cmds);
Johannes Schindelin8af84da2008-08-31 13:50:23323
Erik Faye-Lund6612b9e2010-11-26 16:00:39324 /* This abuses cmdname->len for levenshtein distance */
325 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
326 int cmp = 0; /* avoid compiler stupidity */
327 const char *candidate = main_cmds.names[i]->name;
328
329 /* Does the candidate appear in common_cmds list? */
330 while (n < ARRAY_SIZE(common_cmds) &&
331 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
332 n++;
333 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
334 /* Yes, this is one of the common commands */
335 n++; /* use the entry from common_cmds[] */
336 if (!prefixcmp(candidate, cmd)) {
337 /* Give prefix match a very good score */
338 main_cmds.names[i]->len = 0;
339 continue;
340 }
341 }
342
Johannes Schindelin8af84da2008-08-31 13:50:23343 main_cmds.names[i]->len =
Erik Faye-Lund6612b9e2010-11-26 16:00:39344 levenshtein(cmd, candidate, 0, 2, 1, 4) + 1;
345 }
Johannes Schindelin8af84da2008-08-31 13:50:23346
347 qsort(main_cmds.names, main_cmds.cnt,
348 sizeof(*main_cmds.names), levenshtein_compare);
349
350 if (!main_cmds.cnt)
351 die ("Uh oh. Your system reports no Git commands at all.");
352
Erik Faye-Lund6612b9e2010-11-26 16:00:39353 /* skip and count prefix matches */
354 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
355 ; /* still counting */
356
357 if (main_cmds.cnt <= n) {
358 /* prefix matches with everything? that is too ambiguous */
359 best_similarity = SIMILARITY_FLOOR + 1;
360 } else {
361 /* count all the most similar ones */
362 for (best_similarity = main_cmds.names[n++]->len;
363 (n < main_cmds.cnt &&
364 best_similarity == main_cmds.names[n]->len);
365 n++)
366 ; /* still counting */
367 }
Johannes Sixt06500a02009-12-15 07:57:18368 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
Johannes Schindelin8af84da2008-08-31 13:50:23369 const char *assumed = main_cmds.names[0]->name;
370 main_cmds.names[0] = NULL;
371 clean_cmdnames(&main_cmds);
Ori Avtalion57f6ec02009-08-07 14:24:21372 fprintf(stderr, "WARNING: You called a Git command named '%s', "
Johannes Schindelin8af84da2008-08-31 13:50:23373 "which does not exist.\n"
374 "Continuing under the assumption that you meant '%s'\n",
375 cmd, assumed);
Alex Riesenf0e90712008-08-31 13:54:58376 if (autocorrect > 0) {
377 fprintf(stderr, "in %0.1f seconds automatically...\n",
378 (float)autocorrect/10.0);
379 poll(NULL, 0, autocorrect * 100);
380 }
Johannes Schindelin8af84da2008-08-31 13:50:23381 return assumed;
382 }
383
Pete Harlan7283bbc2010-02-15 23:33:18384 fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd);
Johannes Schindelin8af84da2008-08-31 13:50:23385
Johannes Sixt06500a02009-12-15 07:57:18386 if (SIMILAR_ENOUGH(best_similarity)) {
Johannes Schindelin8af84da2008-08-31 13:50:23387 fprintf(stderr, "\nDid you mean %s?\n",
388 n < 2 ? "this": "one of these");
389
390 for (i = 0; i < n; i++)
391 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
392 }
393
Ramsay Allan Jones822a7d52006-07-30 21:42:25394 exit(1);
395}
396
Linus Torvaldsa633fca2006-07-29 05:44:25397int cmd_version(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 17:27:34398{
399 printf("git version %s\n", git_version_string);
400 return 0;
401}