4 #include "levenshtein.h"
7 /* most GUI terminals set COLUMNS (although some don't export it) */
8 static int term_columns(void)
10 char *col_string
= getenv("COLUMNS");
13 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
19 if (!ioctl(1, TIOCGWINSZ
, &ws
)) {
29 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
31 struct cmdname
*ent
= xmalloc(sizeof(*ent
) + len
+ 1);
34 memcpy(ent
->name
, name
, len
);
37 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
38 cmds
->names
[cmds
->cnt
++] = ent
;
41 static void clean_cmdnames(struct cmdnames
*cmds
)
44 for (i
= 0; i
< cmds
->cnt
; ++i
)
51 static int cmdname_compare(const void *a_
, const void *b_
)
53 struct cmdname
*a
= *(struct cmdname
**)a_
;
54 struct cmdname
*b
= *(struct cmdname
**)b_
;
55 return strcmp(a
->name
, b
->name
);
58 static void uniq(struct cmdnames
*cmds
)
65 for (i
= j
= 1; i
< cmds
->cnt
; i
++)
66 if (strcmp(cmds
->names
[i
]->name
, cmds
->names
[i
-1]->name
))
67 cmds
->names
[j
++] = cmds
->names
[i
];
72 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
78 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
79 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
81 cmds
->names
[cj
++] = cmds
->names
[ci
++];
88 while (ci
< cmds
->cnt
)
89 cmds
->names
[cj
++] = cmds
->names
[ci
++];
94 static void pretty_print_string_list(struct cmdnames
*cmds
, int longest
)
97 int space
= longest
+ 1; /* min 1 SP between words */
98 int max_cols
= term_columns() - 1; /* don't print *on* the edge */
101 if (space
< max_cols
)
102 cols
= max_cols
/ space
;
103 rows
= DIV_ROUND_UP(cmds
->cnt
, cols
);
105 for (i
= 0; i
< rows
; i
++) {
108 for (j
= 0; j
< cols
; j
++) {
109 int n
= j
* rows
+ i
;
113 if (j
== cols
-1 || n
+ rows
>= cmds
->cnt
)
115 printf("%-*s", size
, cmds
->names
[n
]->name
);
121 static int is_executable(const char *name
)
125 if (stat(name
, &st
) || /* stat, not lstat */
126 !S_ISREG(st
.st_mode
))
130 { /* cannot trust the executable bit, peek into the file instead */
133 int fd
= open(name
, O_RDONLY
);
134 st
.st_mode
&= ~S_IXUSR
;
136 n
= read(fd
, buf
, 2);
138 /* DOS executables start with "MZ" */
139 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
140 st
.st_mode
|= S_IXUSR
;
145 return st
.st_mode
& S_IXUSR
;
148 static void list_commands_in_dir(struct cmdnames
*cmds
,
153 DIR *dir
= opendir(path
);
155 struct strbuf buf
= STRBUF_INIT
;
162 prefix_len
= strlen(prefix
);
164 strbuf_addf(&buf
, "%s/", path
);
167 while ((de
= readdir(dir
)) != NULL
) {
170 if (prefixcmp(de
->d_name
, prefix
))
173 strbuf_setlen(&buf
, len
);
174 strbuf_addstr(&buf
, de
->d_name
);
175 if (!is_executable(buf
.buf
))
178 entlen
= strlen(de
->d_name
) - prefix_len
;
179 if (has_extension(de
->d_name
, ".exe"))
182 add_cmdname(cmds
, de
->d_name
+ prefix_len
, entlen
);
185 strbuf_release(&buf
);
188 void load_command_list(const char *prefix
,
189 struct cmdnames
*main_cmds
,
190 struct cmdnames
*other_cmds
)
192 const char *env_path
= getenv("PATH");
193 const char *exec_path
= git_exec_path();
196 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
197 qsort(main_cmds
->names
, main_cmds
->cnt
,
198 sizeof(*main_cmds
->names
), cmdname_compare
);
203 char *paths
, *path
, *colon
;
204 path
= paths
= xstrdup(env_path
);
206 if ((colon
= strchr(path
, PATH_SEP
)))
208 if (!exec_path
|| strcmp(path
, exec_path
))
209 list_commands_in_dir(other_cmds
, path
, prefix
);
217 qsort(other_cmds
->names
, other_cmds
->cnt
,
218 sizeof(*other_cmds
->names
), cmdname_compare
);
221 exclude_cmds(other_cmds
, main_cmds
);
224 void list_commands(const char *title
, struct cmdnames
*main_cmds
,
225 struct cmdnames
*other_cmds
)
229 for (i
= 0; i
< main_cmds
->cnt
; i
++)
230 if (longest
< main_cmds
->names
[i
]->len
)
231 longest
= main_cmds
->names
[i
]->len
;
232 for (i
= 0; i
< other_cmds
->cnt
; i
++)
233 if (longest
< other_cmds
->names
[i
]->len
)
234 longest
= other_cmds
->names
[i
]->len
;
236 if (main_cmds
->cnt
) {
237 const char *exec_path
= git_exec_path();
238 printf("available %s in '%s'\n", title
, exec_path
);
239 printf("----------------");
240 mput_char('-', strlen(title
) + strlen(exec_path
));
242 pretty_print_string_list(main_cmds
, longest
);
246 if (other_cmds
->cnt
) {
247 printf("%s available from elsewhere on your $PATH\n", title
);
248 printf("---------------------------------------");
249 mput_char('-', strlen(title
));
251 pretty_print_string_list(other_cmds
, longest
);
256 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
259 for (i
= 0; i
< c
->cnt
; i
++)
260 if (!strcmp(s
, c
->names
[i
]->name
))
265 static int autocorrect
;
266 static struct cmdnames aliases
;
268 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
270 if (!strcmp(var
, "help.autocorrect"))
271 autocorrect
= git_config_int(var
,value
);
272 /* Also use aliases for command lookup */
273 if (!prefixcmp(var
, "alias."))
274 add_cmdname(&aliases
, var
+ 6, strlen(var
+ 6));
276 return git_default_config(var
, value
, cb
);
279 static int levenshtein_compare(const void *p1
, const void *p2
)
281 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
282 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
285 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
288 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
291 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
293 for (i
= 0; i
< old
->cnt
; i
++)
294 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
300 const char *help_unknown_cmd(const char *cmd
)
302 int i
, n
, best_similarity
= 0;
303 struct cmdnames main_cmds
, other_cmds
;
305 memset(&main_cmds
, 0, sizeof(main_cmds
));
306 memset(&other_cmds
, 0, sizeof(other_cmds
));
307 memset(&aliases
, 0, sizeof(aliases
));
309 git_config(git_unknown_cmd_config
, NULL
);
311 load_command_list("git-", &main_cmds
, &other_cmds
);
313 add_cmd_list(&main_cmds
, &aliases
);
314 add_cmd_list(&main_cmds
, &other_cmds
);
315 qsort(main_cmds
.names
, main_cmds
.cnt
,
316 sizeof(main_cmds
.names
), cmdname_compare
);
319 /* This reuses cmdname->len for similarity index */
320 for (i
= 0; i
< main_cmds
.cnt
; ++i
)
321 main_cmds
.names
[i
]->len
=
322 levenshtein(cmd
, main_cmds
.names
[i
]->name
, 0, 2, 1, 4);
324 qsort(main_cmds
.names
, main_cmds
.cnt
,
325 sizeof(*main_cmds
.names
), levenshtein_compare
);
328 die ("Uh oh. Your system reports no Git commands at all.");
330 best_similarity
= main_cmds
.names
[0]->len
;
332 while (n
< main_cmds
.cnt
&& best_similarity
== main_cmds
.names
[n
]->len
)
334 if (autocorrect
&& n
== 1) {
335 const char *assumed
= main_cmds
.names
[0]->name
;
336 main_cmds
.names
[0] = NULL
;
337 clean_cmdnames(&main_cmds
);
338 fprintf(stderr
, "WARNING: You called a Git command named '%s', "
339 "which does not exist.\n"
340 "Continuing under the assumption that you meant '%s'\n",
342 if (autocorrect
> 0) {
343 fprintf(stderr
, "in %0.1f seconds automatically...\n",
344 (float)autocorrect
/10.0);
345 poll(NULL
, 0, autocorrect
* 100);
350 fprintf(stderr
, "git: '%s' is not a git-command. See 'git --help'.\n", cmd
);
352 if (best_similarity
< 6) {
353 fprintf(stderr
, "\nDid you mean %s?\n",
354 n
< 2 ? "this": "one of these");
356 for (i
= 0; i
< n
; i
++)
357 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
363 int cmd_version(int argc
, const char **argv
, const char *prefix
)
365 printf("git version %s\n", git_version_string
);