4 #include "levenshtein.h"
6 #include "common-cmds.h"
8 /* most GUI terminals set COLUMNS (although some don't export it) */
9 static int term_columns(void)
11 char *col_string = getenv("COLUMNS");
14 if (col_string && (n_cols = atoi(col_string)) > 0)
20 if (!ioctl(1, TIOCGWINSZ, &ws)) {
30 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
32 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
35 memcpy(ent->name, name, len);
38 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
39 cmds->names[cmds->cnt++] = ent;
42 static void clean_cmdnames(struct cmdnames *cmds)
45 for (i = 0; i < cmds->cnt; ++i)
52 static int cmdname_compare(const void *a_, const void *b_)
54 struct cmdname *a = *(struct cmdname **)a_;
55 struct cmdname *b = *(struct cmdname **)b_;
56 return strcmp(a->name, b->name);
59 static void uniq(struct cmdnames *cmds)
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];
73 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
79 while (ci < cmds->cnt && ei < excludes->cnt) {
80 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
82 cmds->names[cj++] = cmds->names[ci++];
89 while (ci < cmds->cnt)
90 cmds->names[cj++] = cmds->names[ci++];
95 static void pretty_print_string_list(struct cmdnames *cmds, int longest)
98 int space = longest + 1; /* min 1 SP between words */
99 int max_cols = term_columns() - 1; /* don't print *on* the edge */
102 if (space < max_cols)
103 cols = max_cols / space;
104 rows = DIV_ROUND_UP(cmds->cnt, cols);
106 for (i = 0; i < rows; i++) {
109 for (j = 0; j < cols; j++) {
110 int n = j * rows + i;
114 if (j == cols-1 || n + rows >= cmds->cnt)
116 printf("%-*s", size, cmds->names[n]->name);
122 static int is_executable(const char *name)
126 if (stat(name, &st) || /* stat, not lstat */
127 !S_ISREG(st.st_mode))
130 #if defined(WIN32) || defined(__CYGWIN__)
131 #if defined(__CYGWIN__)
132 if ((st.st_mode & S_IXUSR) == 0)
134 { /* cannot trust the executable bit, peek into the file instead */
137 int fd = open(name, O_RDONLY);
138 st.st_mode &= ~S_IXUSR;
140 n = read(fd, buf, 2);
142 /* DOS executables start with "MZ" */
143 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
144 st.st_mode |= S_IXUSR;
149 return st.st_mode & S_IXUSR;
152 static void list_commands_in_dir(struct cmdnames *cmds,
157 DIR *dir = opendir(path);
159 struct strbuf buf = STRBUF_INIT;
166 prefix_len = strlen(prefix);
168 strbuf_addf(&buf, "%s/", path);
171 while ((de = readdir(dir)) != NULL) {
174 if (prefixcmp(de->d_name, prefix))
177 strbuf_setlen(&buf, len);
178 strbuf_addstr(&buf, de->d_name);
179 if (!is_executable(buf.buf))
182 entlen = strlen(de->d_name) - prefix_len;
183 if (has_extension(de->d_name, ".exe"))
186 add_cmdname(cmds, de->d_name + prefix_len, entlen);
189 strbuf_release(&buf);
192 void load_command_list(const char *prefix,
193 struct cmdnames *main_cmds,
194 struct cmdnames *other_cmds)
196 const char *env_path = getenv("PATH");
197 const char *exec_path = git_exec_path();
200 list_commands_in_dir(main_cmds, exec_path, prefix);
201 qsort(main_cmds->names, main_cmds->cnt,
202 sizeof(*main_cmds->names), cmdname_compare);
207 char *paths, *path, *colon;
208 path = paths = xstrdup(env_path);
210 if ((colon = strchr(path, PATH_SEP)))
212 if (!exec_path || strcmp(path, exec_path))
213 list_commands_in_dir(other_cmds, path, prefix);
221 qsort(other_cmds->names, other_cmds->cnt,
222 sizeof(*other_cmds->names), cmdname_compare);
225 exclude_cmds(other_cmds, main_cmds);
228 void list_commands(const char *title, struct cmdnames *main_cmds,
229 struct cmdnames *other_cmds)
233 for (i = 0; i < main_cmds->cnt; i++)
234 if (longest < main_cmds->names[i]->len)
235 longest = main_cmds->names[i]->len;
236 for (i = 0; i < other_cmds->cnt; i++)
237 if (longest < other_cmds->names[i]->len)
238 longest = other_cmds->names[i]->len;
240 if (main_cmds->cnt) {
241 const char *exec_path = git_exec_path();
242 printf("available %s in '%s'\n", title, exec_path);
243 printf("----------------");
244 mput_char('-', strlen(title) + strlen(exec_path));
246 pretty_print_string_list(main_cmds, longest);
250 if (other_cmds->cnt) {
251 printf("%s available from elsewhere on your $PATH\n", title);
252 printf("---------------------------------------");
253 mput_char('-', strlen(title));
255 pretty_print_string_list(other_cmds, longest);
260 int is_in_cmdlist(struct cmdnames *c, const char *s)
263 for (i = 0; i < c->cnt; i++)
264 if (!strcmp(s, c->names[i]->name))
269 static int autocorrect;
270 static struct cmdnames aliases;
272 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
274 if (!strcmp(var, "help.autocorrect"))
275 autocorrect = git_config_int(var,value);
276 /* Also use aliases for command lookup */
277 if (!prefixcmp(var, "alias."))
278 add_cmdname(&aliases, var + 6, strlen(var + 6));
280 return git_default_config(var, value, cb);
283 static int levenshtein_compare(const void *p1, const void *p2)
285 const struct cmdname *const *c1 = p1, *const *c2 = p2;
286 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
289 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
292 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
295 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
297 for (i = 0; i < old->cnt; i++)
298 cmds->names[cmds->cnt++] = old->names[i];
304 /* An empirically derived magic number */
305 #define SIMILARITY_FLOOR 7
306 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
308 const char *help_unknown_cmd(const char *cmd)
310 int i, n, best_similarity = 0;
311 struct cmdnames main_cmds, other_cmds;
313 memset(&main_cmds, 0, sizeof(main_cmds));
314 memset(&other_cmds, 0, sizeof(other_cmds));
315 memset(&aliases, 0, sizeof(aliases));
317 git_config(git_unknown_cmd_config, NULL);
319 load_command_list("git-", &main_cmds, &other_cmds);
321 add_cmd_list(&main_cmds, &aliases);
322 add_cmd_list(&main_cmds, &other_cmds);
323 qsort(main_cmds.names, main_cmds.cnt,
324 sizeof(main_cmds.names), cmdname_compare);
327 /* This abuses cmdname->len for levenshtein distance */
328 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
329 int cmp = 0; /* avoid compiler stupidity */
330 const char *candidate = main_cmds.names[i]->name;
332 /* Does the candidate appear in common_cmds list? */
333 while (n < ARRAY_SIZE(common_cmds) &&
334 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
336 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
337 /* Yes, this is one of the common commands */
338 n++; /* use the entry from common_cmds[] */
339 if (!prefixcmp(candidate, cmd)) {
340 /* Give prefix match a very good score */
341 main_cmds.names[i]->len = 0;
346 main_cmds.names[i]->len =
347 levenshtein(cmd, candidate, 0, 2, 1, 4) + 1;
350 qsort(main_cmds.names, main_cmds.cnt,
351 sizeof(*main_cmds.names), levenshtein_compare);
354 die ("Uh oh. Your system reports no Git commands at all.");
356 /* skip and count prefix matches */
357 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
358 ; /* still counting */
360 if (main_cmds.cnt <= n) {
361 /* prefix matches with everything? that is too ambiguous */
362 best_similarity = SIMILARITY_FLOOR + 1;
364 /* count all the most similar ones */
365 for (best_similarity = main_cmds.names[n++]->len;
366 (n < main_cmds.cnt &&
367 best_similarity == main_cmds.names[n]->len);
369 ; /* still counting */
371 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
372 const char *assumed = main_cmds.names[0]->name;
373 main_cmds.names[0] = NULL;
374 clean_cmdnames(&main_cmds);
375 fprintf(stderr, "WARNING: You called a Git command named '%s', "
376 "which does not exist.\n"
377 "Continuing under the assumption that you meant '%s'\n",
379 if (autocorrect > 0) {
380 fprintf(stderr, "in %0.1f seconds automatically...\n",
381 (float)autocorrect/10.0);
382 poll(NULL, 0, autocorrect * 100);
387 fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd);
389 if (SIMILAR_ENOUGH(best_similarity)) {
390 fprintf(stderr, "\nDid you mean %s?\n",
391 n < 2 ? "this": "one of these");
393 for (i = 0; i < n; i++)
394 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
400 int cmd_version(int argc, const char **argv, const char *prefix)
402 printf("git version %s\n", git_version_string);