4 #include "levenshtein.h"
6 #include "common-cmds.h"
8 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
10 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
+ 1);
13 memcpy(ent
->name
, name
, len
);
16 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
17 cmds
->names
[cmds
->cnt
++] = ent
;
20 static void clean_cmdnames(struct cmdnames
*cmds
)
23 for (i
= 0; i
< cmds
->cnt
; ++i
)
30 static int cmdname_compare(const void *a_
, const void *b_
)
32 struct cmdname
*a
= *(struct cmdname
**)a_
;
33 struct cmdname
*b
= *(struct cmdname
**)b_
;
34 return strcmp(a
->name
, b
->name
);
37 static void uniq(struct cmdnames
*cmds
)
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
];
51 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
57 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
58 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
60 cmds
->names
[cj
++] = cmds
->names
[ci
++];
67 while (ci
< cmds
->cnt
)
68 cmds
->names
[cj
++] = cmds
->names
[ci
++];
73 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
76 int space
= longest
+ 1; /* min 1 SP between words */
77 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
81 cols
= max_cols
/ space
;
82 rows
= DIV_ROUND_UP(cmds
->cnt
, cols
);
84 for (i
= 0; i
< rows
; i
++) {
87 for (j
= 0; j
< cols
; j
++) {
92 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
94 printf("%-*s", size
, cmds
->names
[n
]->name
);
100 static int is_executable(const char *name
)
104 if (stat(name
, &st
) || /* stat, not lstat */
105 !S_ISREG(st
.st_mode
))
108 #if defined(WIN32) || defined(__CYGWIN__)
109 #if defined(__CYGWIN__)
110 if ((st
.st_mode
& S_IXUSR
) == 0)
112 { /* cannot trust the executable bit, peek into the file instead */
115 int fd
= open(name
, O_RDONLY
);
116 st
.st_mode
&= ~S_IXUSR
;
118 n
= read(fd
, buf
, 2);
120 /* DOS executables start with "MZ" */
121 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
122 st
.st_mode
|= S_IXUSR
;
127 return st
.st_mode
& S_IXUSR
;
130 static void list_commands_in_dir(struct cmdnames
*cmds
,
135 DIR *dir
= opendir(path
);
137 struct strbuf buf
= STRBUF_INIT
;
144 prefix_len
= strlen(prefix
);
146 strbuf_addf(&buf
, "%s/", path
);
149 while ((de
= readdir(dir
)) != NULL
) {
152 if (prefixcmp(de
->d_name
, prefix
))
155 strbuf_setlen(&buf
, len
);
156 strbuf_addstr(&buf
, de
->d_name
);
157 if (!is_executable(buf
.buf
))
160 entlen
= strlen(de
->d_name
) - prefix_len
;
161 if (has_extension(de
->d_name
, ".exe"))
164 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
167 strbuf_release(&buf
);
170 void load_command_list(const char *prefix
,
171 struct cmdnames
*main_cmds
,
172 struct cmdnames
*other_cmds
)
174 const char *env_path
= getenv("PATH");
175 const char *exec_path
= git_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
);
185 char *paths
, *path
, *colon
;
186 path
= paths
= xstrdup(env_path
);
188 if ((colon
= strchr(path
, PATH_SEP
)))
190 if (!exec_path
|| strcmp(path
, exec_path
))
191 list_commands_in_dir(other_cmds
, path
, prefix
);
199 qsort(other_cmds
->names
, other_cmds
->cnt
,
200 sizeof(*other_cmds
->names
), cmdname_compare
);
203 exclude_cmds(other_cmds
, main_cmds
);
206 void list_commands(const char *title
, struct cmdnames
*main_cmds
,
207 struct cmdnames
*other_cmds
)
211 for (i
= 0; i
< main_cmds
->cnt
; i
++)
212 if (longest
< main_cmds
->names
[i
]->len
)
213 longest
= main_cmds
->names
[i
]->len
;
214 for (i
= 0; i
< other_cmds
->cnt
; i
++)
215 if (longest
< other_cmds
->names
[i
]->len
)
216 longest
= other_cmds
->names
[i
]->len
;
218 if (main_cmds
->cnt
) {
219 const char *exec_path
= git_exec_path();
220 printf("available %s in '%s'\n", title
, exec_path
);
221 printf("----------------");
222 mput_char('-', strlen(title
) + strlen(exec_path
));
224 pretty_print_string_list(main_cmds
, longest
);
228 if (other_cmds
->cnt
) {
229 printf("%s available from elsewhere on your $PATH\n", title
);
230 printf("---------------------------------------");
231 mput_char('-', strlen(title
));
233 pretty_print_string_list(other_cmds
, longest
);
238 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
241 for (i
= 0; i
< c
->cnt
; i
++)
242 if (!strcmp(s
, c
->names
[i
]->name
))
247 static int autocorrect
;
248 static struct cmdnames aliases
;
250 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
252 if (!strcmp(var
, "help.autocorrect"))
253 autocorrect
= git_config_int(var
,value
);
254 /* Also use aliases for command lookup */
255 if (!prefixcmp(var
, "alias."))
256 add_cmdname(&aliases
, var
+ 6, strlen(var
+ 6));
258 return git_default_config(var
, value
, cb
);
261 static int levenshtein_compare(const void *p1
, const void *p2
)
263 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
264 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
267 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
270 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
273 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
275 for (i
= 0; i
< old
->cnt
; i
++)
276 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
282 /* An empirically derived magic number */
283 #define SIMILARITY_FLOOR 7
284 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
286 static const char bad_interpreter_advice
[] =
287 N_("'%s' appears to be a git command, but we were not\n"
288 "able to execute it. Maybe git-%s is broken?");
290 const char *help_unknown_cmd(const char *cmd
)
292 int i
, n
, best_similarity
= 0;
293 struct cmdnames main_cmds
, other_cmds
;
295 memset(&main_cmds
, 0, sizeof(main_cmds
));
296 memset(&other_cmds
, 0, sizeof(other_cmds
));
297 memset(&aliases
, 0, sizeof(aliases
));
299 git_config(git_unknown_cmd_config
, NULL
);
301 load_command_list("git-", &main_cmds
, &other_cmds
);
303 add_cmd_list(&main_cmds
, &aliases
);
304 add_cmd_list(&main_cmds
, &other_cmds
);
305 qsort(main_cmds
.names
, main_cmds
.cnt
,
306 sizeof(main_cmds
.names
), cmdname_compare
);
309 /* This abuses cmdname->len for levenshtein distance */
310 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
311 int cmp
= 0; /* avoid compiler stupidity */
312 const char *candidate
= main_cmds
.names
[i
]->name
;
315 * An exact match means we have the command, but
316 * for some reason exec'ing it gave us ENOENT; probably
317 * it's a bad interpreter in the #! line.
319 if (!strcmp(candidate
, cmd
))
320 die(_(bad_interpreter_advice
), cmd
, cmd
);
322 /* Does the candidate appear in common_cmds list? */
323 while (n
< ARRAY_SIZE(common_cmds
) &&
324 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
326 if ((n
< ARRAY_SIZE(common_cmds
)) && !cmp
) {
327 /* Yes, this is one of the common commands */
328 n
++; /* use the entry from common_cmds[] */
329 if (!prefixcmp(candidate
, cmd
)) {
330 /* Give prefix match a very good score */
331 main_cmds
.names
[i
]->len
= 0;
336 main_cmds
.names
[i
]->len
=
337 levenshtein(cmd
, candidate
, 0, 2, 1, 4) + 1;
340 qsort(main_cmds
.names
, main_cmds
.cnt
,
341 sizeof(*main_cmds
.names
), levenshtein_compare
);
344 die ("Uh oh. Your system reports no Git commands at all.");
346 /* skip and count prefix matches */
347 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
348 ; /* still counting */
350 if (main_cmds
.cnt
<= n
) {
351 /* prefix matches with everything? that is too ambiguous */
352 best_similarity
= SIMILARITY_FLOOR
+ 1;
354 /* count all the most similar ones */
355 for (best_similarity
= main_cmds
.names
[n
++]->len
;
356 (n
< main_cmds
.cnt
&&
357 best_similarity
== main_cmds
.names
[n
]->len
);
359 ; /* still counting */
361 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
362 const char *assumed
= main_cmds
.names
[0]->name
;
363 main_cmds
.names
[0] = NULL
;
364 clean_cmdnames(&main_cmds
);
365 fprintf(stderr
, "WARNING: You called a Git command named '%s', "
366 "which does not exist.\n"
367 "Continuing under the assumption that you meant '%s'\n",
369 if (autocorrect
> 0) {
370 fprintf(stderr
, "in %0.1f seconds automatically...\n",
371 (float)autocorrect
/10.0);
372 poll(NULL
, 0, autocorrect
* 100);
377 fprintf(stderr
, "git: '%s' is not a git command. See 'git --help'.\n", cmd
);
379 if (SIMILAR_ENOUGH(best_similarity
)) {
380 fprintf(stderr
, "\nDid you mean %s?\n",
381 n
< 2 ? "this": "one of these");
383 for (i
= 0; i
< n
; i
++)
384 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
390 int cmd_version(int argc
, const char **argv
, const char *prefix
)
392 printf("git version %s\n", git_version_string
);