4 #include "levenshtein.h"
6 #include "common-cmds.h"
7 #include "string-list.h"
10 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
12 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
+ 1);
15 memcpy(ent
->name
, name
, len
);
18 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
19 cmds
->names
[cmds
->cnt
++] = ent
;
22 static void clean_cmdnames(struct cmdnames
*cmds
)
25 for (i
= 0; i
< cmds
->cnt
; ++i
)
32 static int cmdname_compare(const void *a_
, const void *b_
)
34 struct cmdname
*a
= *(struct cmdname
**)a_
;
35 struct cmdname
*b
= *(struct cmdname
**)b_
;
36 return strcmp(a
->name
, b
->name
);
39 static void uniq(struct cmdnames
*cmds
)
46 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
47 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
48 cmds
->names
[j
++] = cmds
->names
[i
];
53 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
59 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
60 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
62 cmds
->names
[cj
++] = cmds
->names
[ci
++];
69 while (ci
< cmds
->cnt
)
70 cmds
->names
[cj
++] = cmds
->names
[ci
++];
75 static void pretty_print_string_list(struct cmdnames
*cmds
,
78 struct string_list list
= STRING_LIST_INIT_NODUP
;
79 struct column_options copts
;
82 for (i
= 0; i
< cmds
->cnt
; i
++)
83 string_list_append(&list
, cmds
->names
[i
]->name
);
85 * always enable column display, we only consult column.*
86 * about layout strategy and stuff
88 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
89 memset(&copts
, 0, sizeof(copts
));
92 print_columns(&list
, colopts
, &copts
);
93 string_list_clear(&list
, 0);
96 static int is_executable(const char *name
)
100 if (stat(name
, &st
) || /* stat, not lstat */
101 !S_ISREG(st
.st_mode
))
104 #if defined(WIN32) || defined(__CYGWIN__)
105 #if defined(__CYGWIN__)
106 if ((st
.st_mode
& S_IXUSR
) == 0)
108 { /* cannot trust the executable bit, peek into the file instead */
111 int fd
= open(name
, O_RDONLY
);
112 st
.st_mode
&= ~S_IXUSR
;
114 n
= read(fd
, buf
, 2);
116 /* DOS executables start with "MZ" */
117 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
118 st
.st_mode
|= S_IXUSR
;
123 return st
.st_mode
& S_IXUSR
;
126 static void list_commands_in_dir(struct cmdnames
*cmds
,
131 DIR *dir
= opendir(path
);
133 struct strbuf buf
= STRBUF_INIT
;
140 prefix_len
= strlen(prefix
);
142 strbuf_addf(&buf
, "%s/", path
);
145 while ((de
= readdir(dir
)) != NULL
) {
148 if (prefixcmp(de
->d_name
, prefix
))
151 strbuf_setlen(&buf
, len
);
152 strbuf_addstr(&buf
, de
->d_name
);
153 if (!is_executable(buf
.buf
))
156 entlen
= strlen(de
->d_name
) - prefix_len
;
157 if (has_extension(de
->d_name
, ".exe"))
160 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
163 strbuf_release(&buf
);
166 void load_command_list(const char *prefix
,
167 struct cmdnames
*main_cmds
,
168 struct cmdnames
*other_cmds
)
170 const char *env_path
= getenv("PATH");
171 const char *exec_path
= git_exec_path();
174 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
175 qsort(main_cmds
->names
, main_cmds
->cnt
,
176 sizeof(*main_cmds
->names
), cmdname_compare
);
181 char *paths
, *path
, *colon
;
182 path
= paths
= xstrdup(env_path
);
184 if ((colon
= strchr(path
, PATH_SEP
)))
186 if (!exec_path
|| strcmp(path
, exec_path
))
187 list_commands_in_dir(other_cmds
, path
, prefix
);
195 qsort(other_cmds
->names
, other_cmds
->cnt
,
196 sizeof(*other_cmds
->names
), cmdname_compare
);
199 exclude_cmds(other_cmds
, main_cmds
);
202 void list_commands(const char *title
, unsigned int colopts
,
203 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
205 if (main_cmds
->cnt
) {
206 const char *exec_path
= git_exec_path();
207 printf("available %s in '%s'\n", title
, exec_path
);
208 printf("----------------");
209 mput_char('-', strlen(title
) + strlen(exec_path
));
211 pretty_print_string_list(main_cmds
, colopts
);
215 if (other_cmds
->cnt
) {
216 printf("%s available from elsewhere on your $PATH\n", title
);
217 printf("---------------------------------------");
218 mput_char('-', strlen(title
));
220 pretty_print_string_list(other_cmds
, colopts
);
225 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
228 for (i
= 0; i
< c
->cnt
; i
++)
229 if (!strcmp(s
, c
->names
[i
]->name
))
234 static int autocorrect
;
235 static struct cmdnames aliases
;
237 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
239 if (!strcmp(var
, "help.autocorrect"))
240 autocorrect
= git_config_int(var
,value
);
241 /* Also use aliases for command lookup */
242 if (!prefixcmp(var
, "alias."))
243 add_cmdname(&aliases
, var
+ 6, strlen(var
+ 6));
245 return git_default_config(var
, value
, cb
);
248 static int levenshtein_compare(const void *p1
, const void *p2
)
250 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
251 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
254 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
257 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
260 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
262 for (i
= 0; i
< old
->cnt
; i
++)
263 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
269 /* An empirically derived magic number */
270 #define SIMILARITY_FLOOR 7
271 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
273 static const char bad_interpreter_advice
[] =
274 N_("'%s' appears to be a git command, but we were not\n"
275 "able to execute it. Maybe git-%s is broken?");
277 const char *help_unknown_cmd(const char *cmd
)
279 int i
, n
, best_similarity
= 0;
280 struct cmdnames main_cmds
, other_cmds
;
282 memset(&main_cmds
, 0, sizeof(main_cmds
));
283 memset(&other_cmds
, 0, sizeof(other_cmds
));
284 memset(&aliases
, 0, sizeof(aliases
));
286 git_config(git_unknown_cmd_config
, NULL
);
288 load_command_list("git-", &main_cmds
, &other_cmds
);
290 add_cmd_list(&main_cmds
, &aliases
);
291 add_cmd_list(&main_cmds
, &other_cmds
);
292 qsort(main_cmds
.names
, main_cmds
.cnt
,
293 sizeof(main_cmds
.names
), cmdname_compare
);
296 /* This abuses cmdname->len for levenshtein distance */
297 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
298 int cmp
= 0; /* avoid compiler stupidity */
299 const char *candidate
= main_cmds
.names
[i
]->name
;
302 * An exact match means we have the command, but
303 * for some reason exec'ing it gave us ENOENT; probably
304 * it's a bad interpreter in the #! line.
306 if (!strcmp(candidate
, cmd
))
307 die(_(bad_interpreter_advice
), cmd
, cmd
);
309 /* Does the candidate appear in common_cmds list? */
310 while (n
< ARRAY_SIZE(common_cmds
) &&
311 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
313 if ((n
< ARRAY_SIZE(common_cmds
)) && !cmp
) {
314 /* Yes, this is one of the common commands */
315 n
++; /* use the entry from common_cmds[] */
316 if (!prefixcmp(candidate
, cmd
)) {
317 /* Give prefix match a very good score */
318 main_cmds
.names
[i
]->len
= 0;
323 main_cmds
.names
[i
]->len
=
324 levenshtein(cmd
, candidate
, 0, 2, 1, 4) + 1;
327 qsort(main_cmds
.names
, main_cmds
.cnt
,
328 sizeof(*main_cmds
.names
), levenshtein_compare
);
331 die ("Uh oh. Your system reports no Git commands at all.");
333 /* skip and count prefix matches */
334 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
335 ; /* still counting */
337 if (main_cmds
.cnt
<= n
) {
338 /* prefix matches with everything? that is too ambiguous */
339 best_similarity
= SIMILARITY_FLOOR
+ 1;
341 /* count all the most similar ones */
342 for (best_similarity
= main_cmds
.names
[n
++]->len
;
343 (n
< main_cmds
.cnt
&&
344 best_similarity
== main_cmds
.names
[n
]->len
);
346 ; /* still counting */
348 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
349 const char *assumed
= main_cmds
.names
[0]->name
;
350 main_cmds
.names
[0] = NULL
;
351 clean_cmdnames(&main_cmds
);
352 fprintf(stderr
, "WARNING: You called a Git command named '%s', "
353 "which does not exist.\n"
354 "Continuing under the assumption that you meant '%s'\n",
356 if (autocorrect
> 0) {
357 fprintf(stderr
, "in %0.1f seconds automatically...\n",
358 (float)autocorrect
/10.0);
359 poll(NULL
, 0, autocorrect
* 100);
364 fprintf(stderr
, "git: '%s' is not a git command. See 'git --help'.\n", cmd
);
366 if (SIMILAR_ENOUGH(best_similarity
)) {
367 fprintf(stderr
, "\nDid you mean %s?\n",
368 n
< 2 ? "this": "one of these");
370 for (i
= 0; i
< n
; i
++)
371 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
377 int cmd_version(int argc
, const char **argv
, const char *prefix
)
379 printf("git version %s\n", git_version_string
);