|
1 | 1 | #include "cache.h" |
2 | 2 | #include "builtin.h" |
3 | 3 | #include "exec_cmd.h" |
| 4 | +#include "levenshtein.h" |
4 | 5 | #include "help.h" |
5 | 6 |
|
6 | 7 | /* most GUI terminals set COLUMNS (although some don't export it) */ |
@@ -37,6 +38,16 @@ void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
37 | 38 | cmds->names[cmds->cnt++] = ent; |
38 | 39 | } |
39 | 40 |
|
| 41 | +static void clean_cmdnames(struct cmdnames *cmds) |
| 42 | +{ |
| 43 | + int i; |
| 44 | + for (i = 0; i < cmds->cnt; ++i) |
| 45 | + free(cmds->names[i]); |
| 46 | + free(cmds->names); |
| 47 | + cmds->cnt = 0; |
| 48 | + cmds->alloc = 0; |
| 49 | +} |
| 50 | + |
40 | 51 | static int cmdname_compare(const void *a_, const void *b_) |
41 | 52 | { |
42 | 53 | struct cmdname *a = *(struct cmdname **)a_; |
@@ -250,9 +261,85 @@ int is_in_cmdlist(struct cmdnames *c, const char *s) |
250 | 261 | return 0; |
251 | 262 | } |
252 | 263 |
|
253 | | -void help_unknown_cmd(const char *cmd) |
| 264 | +static int autocorrect; |
| 265 | + |
| 266 | +static int git_unknown_cmd_config(const char *var, const char *value, void *cb) |
| 267 | +{ |
| 268 | + if (!strcmp(var, "help.autocorrect")) |
| 269 | + autocorrect = git_config_int(var,value); |
| 270 | + |
| 271 | + return git_default_config(var, value, cb); |
| 272 | +} |
| 273 | + |
| 274 | +static int levenshtein_compare(const void *p1, const void *p2) |
254 | 275 | { |
| 276 | + const struct cmdname *const *c1 = p1, *const *c2 = p2; |
| 277 | + const char *s1 = (*c1)->name, *s2 = (*c2)->name; |
| 278 | + int l1 = (*c1)->len; |
| 279 | + int l2 = (*c2)->len; |
| 280 | + return l1 != l2 ? l1 - l2 : strcmp(s1, s2); |
| 281 | +} |
| 282 | + |
| 283 | +const char *help_unknown_cmd(const char *cmd) |
| 284 | +{ |
| 285 | + int i, n, best_similarity = 0; |
| 286 | + struct cmdnames main_cmds, other_cmds; |
| 287 | + |
| 288 | + memset(&main_cmds, 0, sizeof(main_cmds)); |
| 289 | + memset(&other_cmds, 0, sizeof(main_cmds)); |
| 290 | + |
| 291 | + git_config(git_unknown_cmd_config, NULL); |
| 292 | + |
| 293 | + load_command_list("git-", &main_cmds, &other_cmds); |
| 294 | + |
| 295 | + ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt, |
| 296 | + main_cmds.alloc); |
| 297 | + memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names, |
| 298 | + other_cmds.cnt * sizeof(other_cmds.names[0])); |
| 299 | + main_cmds.cnt += other_cmds.cnt; |
| 300 | + free(other_cmds.names); |
| 301 | + |
| 302 | + /* This reuses cmdname->len for similarity index */ |
| 303 | + for (i = 0; i < main_cmds.cnt; ++i) |
| 304 | + main_cmds.names[i]->len = |
| 305 | + levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4); |
| 306 | + |
| 307 | + qsort(main_cmds.names, main_cmds.cnt, |
| 308 | + sizeof(*main_cmds.names), levenshtein_compare); |
| 309 | + |
| 310 | + if (!main_cmds.cnt) |
| 311 | + die ("Uh oh. Your system reports no Git commands at all."); |
| 312 | + |
| 313 | + best_similarity = main_cmds.names[0]->len; |
| 314 | + n = 1; |
| 315 | + while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len) |
| 316 | + ++n; |
| 317 | + if (autocorrect && n == 1) { |
| 318 | + const char *assumed = main_cmds.names[0]->name; |
| 319 | + main_cmds.names[0] = NULL; |
| 320 | + clean_cmdnames(&main_cmds); |
| 321 | + fprintf(stderr, "WARNING: You called a Git program named '%s', " |
| 322 | + "which does not exist.\n" |
| 323 | + "Continuing under the assumption that you meant '%s'\n", |
| 324 | + cmd, assumed); |
| 325 | + if (autocorrect > 0) { |
| 326 | + fprintf(stderr, "in %0.1f seconds automatically...\n", |
| 327 | + (float)autocorrect/10.0); |
| 328 | + poll(NULL, 0, autocorrect * 100); |
| 329 | + } |
| 330 | + return assumed; |
| 331 | + } |
| 332 | + |
255 | 333 | fprintf(stderr, "git: '%s' is not a git-command. See 'git --help'.\n", cmd); |
| 334 | + |
| 335 | + if (best_similarity < 6) { |
| 336 | + fprintf(stderr, "\nDid you mean %s?\n", |
| 337 | + n < 2 ? "this": "one of these"); |
| 338 | + |
| 339 | + for (i = 0; i < n; i++) |
| 340 | + fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); |
| 341 | + } |
| 342 | + |
256 | 343 | exit(1); |
257 | 344 | } |
258 | 345 |
|
|
0 commit comments