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))
131 { /* cannot trust the executable bit, peek into the file instead */
134 int fd = open(name, O_RDONLY);
135 st.st_mode &= ~S_IXUSR;
137 n = read(fd, buf, 2);
139 /* DOS executables start with "MZ" */
140 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
141 st.st_mode |= S_IXUSR;
146 return st.st_mode & S_IXUSR;
149 static void list_commands_in_dir(struct cmdnames *cmds,
154 DIR *dir = opendir(path);
156 struct strbuf buf = STRBUF_INIT;
163 prefix_len = strlen(prefix);
165 strbuf_addf(&buf, "%s/", path);
168 while ((de = readdir(dir)) != NULL) {
171 if (prefixcmp(de->d_name, prefix))
174 strbuf_setlen(&buf, len);
175 strbuf_addstr(&buf, de->d_name);
176 if (!is_executable(buf.buf))
179 entlen = strlen(de->d_name) - prefix_len;
180 if (has_extension(de->d_name, ".exe"))
183 add_cmdname(cmds, de->d_name + prefix_len, entlen);
186 strbuf_release(&buf);
189 void load_command_list(const char *prefix,
190 struct cmdnames *main_cmds,
191 struct cmdnames *other_cmds)
193 const char *env_path = getenv("PATH");
194 const char *exec_path = git_exec_path();
197 list_commands_in_dir(main_cmds, exec_path, prefix);
198 qsort(main_cmds->names, main_cmds->cnt,
199 sizeof(*main_cmds->names), cmdname_compare);
204 char *paths, *path, *colon;
205 path = paths = xstrdup(env_path);
207 if ((colon = strchr(path, PATH_SEP)))
209 if (!exec_path || strcmp(path, exec_path))
210 list_commands_in_dir(other_cmds, path, prefix);
218 qsort(other_cmds->names, other_cmds->cnt,
219 sizeof(*other_cmds->names), cmdname_compare);
222 exclude_cmds(other_cmds, main_cmds);
225 void list_commands(const char *title, struct cmdnames *main_cmds,
226 struct cmdnames *other_cmds)
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;
237 if (main_cmds->cnt) {
238 const char *exec_path = git_exec_path();
239 printf("available %s in '%s'\n", title, exec_path);
240 printf("----------------");
241 mput_char('-', strlen(title) + strlen(exec_path));
243 pretty_print_string_list(main_cmds, longest);
247 if (other_cmds->cnt) {
248 printf("%s available from elsewhere on your $PATH\n", title);
249 printf("---------------------------------------");
250 mput_char('-', strlen(title));
252 pretty_print_string_list(other_cmds, longest);
257 int is_in_cmdlist(struct cmdnames *c, const char *s)
260 for (i = 0; i < c->cnt; i++)
261 if (!strcmp(s, c->names[i]->name))
266 static int autocorrect;
267 static struct cmdnames aliases;
269 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
271 if (!strcmp(var, "help.autocorrect"))
272 autocorrect = git_config_int(var,value);
273 /* Also use aliases for command lookup */
274 if (!prefixcmp(var, "alias."))
275 add_cmdname(&aliases, var + 6, strlen(var + 6));
277 return git_default_config(var, value, cb);
280 static int levenshtein_compare(const void *p1, const void *p2)
282 const struct cmdname *const *c1 = p1, *const *c2 = p2;
283 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
286 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
289 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
292 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
294 for (i = 0; i < old->cnt; i++)
295 cmds->names[cmds->cnt++] = old->names[i];
301 /* An empirically derived magic number */
302 #define SIMILARITY_FLOOR 7
303 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
305 static const char bad_interpreter_advice[] =
306 N_("'%s' appears to be a git command, but we were not\n"
307 "able to execute it. Maybe git-%s is broken?");
309 const char *help_unknown_cmd(const char *cmd)
311 int i, n, best_similarity = 0;
312 struct cmdnames main_cmds, other_cmds;
314 memset(&main_cmds, 0, sizeof(main_cmds));
315 memset(&other_cmds, 0, sizeof(other_cmds));
316 memset(&aliases, 0, sizeof(aliases));
318 git_config(git_unknown_cmd_config, NULL);
320 load_command_list("git-", &main_cmds, &other_cmds);
322 add_cmd_list(&main_cmds, &aliases);
323 add_cmd_list(&main_cmds, &other_cmds);
324 qsort(main_cmds.names, main_cmds.cnt,
325 sizeof(main_cmds.names), cmdname_compare);
328 /* This abuses cmdname->len for levenshtein distance */
329 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
330 int cmp = 0; /* avoid compiler stupidity */
331 const char *candidate = main_cmds.names[i]->name;
334 * An exact match means we have the command, but
335 * for some reason exec'ing it gave us ENOENT; probably
336 * it's a bad interpreter in the #! line.
338 if (!strcmp(candidate, cmd))
339 die(_(bad_interpreter_advice), cmd, cmd);
341 /* Does the candidate appear in common_cmds list? */
342 while (n < ARRAY_SIZE(common_cmds) &&
343 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
345 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
346 /* Yes, this is one of the common commands */
347 n++; /* use the entry from common_cmds[] */
348 if (!prefixcmp(candidate, cmd)) {
349 /* Give prefix match a very good score */
350 main_cmds.names[i]->len = 0;
355 main_cmds.names[i]->len =
356 levenshtein(cmd, candidate, 0, 2, 1, 4) + 1;
359 qsort(main_cmds.names, main_cmds.cnt,
360 sizeof(*main_cmds.names), levenshtein_compare);
363 die ("Uh oh. Your system reports no Git commands at all.");
365 /* skip and count prefix matches */
366 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
367 ; /* still counting */
369 if (main_cmds.cnt <= n) {
370 /* prefix matches with everything? that is too ambiguous */
371 best_similarity = SIMILARITY_FLOOR + 1;
373 /* count all the most similar ones */
374 for (best_similarity = main_cmds.names[n++]->len;
375 (n < main_cmds.cnt &&
376 best_similarity == main_cmds.names[n]->len);
378 ; /* still counting */
380 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
381 const char *assumed = main_cmds.names[0]->name;
382 main_cmds.names[0] = NULL;
383 clean_cmdnames(&main_cmds);
384 fprintf(stderr, "WARNING: You called a Git command named '%s', "
385 "which does not exist.\n"
386 "Continuing under the assumption that you meant '%s'\n",
388 if (autocorrect > 0) {
389 fprintf(stderr, "in %0.1f seconds automatically...\n",
390 (float)autocorrect/10.0);
391 poll(NULL, 0, autocorrect * 100);
396 fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd);
398 if (SIMILAR_ENOUGH(best_similarity)) {
399 fprintf(stderr, "\nDid you mean %s?\n",
400 n < 2 ? "this": "one of these");
402 for (i = 0; i < n; i++)
403 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
409 int cmd_version(int argc, const char **argv, const char *prefix)
411 printf("git version %s\n", git_version_string);