Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "builtin.h" |
| 3 | #include "exec_cmd.h" |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 4 | #include "levenshtein.h" |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 5 | #include "help.h" |
Christian Couder | 6494998 | 2008-03-07 08:46:28 +0100 | [diff] [blame] | 6 | |
Pavel Roskin | 82e5a82 | 2006-07-10 01:50:18 -0400 | [diff] [blame] | 7 | /* most GUI terminals set COLUMNS (although some don't export it) */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 8 | static int term_columns(void) |
| 9 | { |
| 10 | char *col_string = getenv("COLUMNS"); |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 11 | int n_cols; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 12 | |
| 13 | if (col_string && (n_cols = atoi(col_string)) > 0) |
| 14 | return n_cols; |
| 15 | |
| 16 | #ifdef TIOCGWINSZ |
| 17 | { |
| 18 | struct winsize ws; |
| 19 | if (!ioctl(1, TIOCGWINSZ, &ws)) { |
| 20 | if (ws.ws_col) |
| 21 | return ws.ws_col; |
| 22 | } |
| 23 | } |
| 24 | #endif |
| 25 | |
| 26 | return 80; |
| 27 | } |
| 28 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 29 | void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 30 | { |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 31 | struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 32 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 33 | ent->len = len; |
| 34 | memcpy(ent->name, name, len); |
| 35 | ent->name[len] = 0; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 36 | |
| 37 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); |
| 38 | cmds->names[cmds->cnt++] = ent; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 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 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 51 | static int cmdname_compare(const void *a_, const void *b_) |
| 52 | { |
| 53 | struct cmdname *a = *(struct cmdname **)a_; |
| 54 | struct cmdname *b = *(struct cmdname **)b_; |
| 55 | return strcmp(a->name, b->name); |
| 56 | } |
| 57 | |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 58 | static void uniq(struct cmdnames *cmds) |
| 59 | { |
| 60 | int i, j; |
| 61 | |
| 62 | if (!cmds->cnt) |
| 63 | return; |
| 64 | |
| 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]; |
| 68 | |
| 69 | cmds->cnt = j; |
| 70 | } |
| 71 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 72 | void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 73 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 74 | int ci, cj, ei; |
| 75 | int cmp; |
| 76 | |
| 77 | ci = cj = ei = 0; |
| 78 | while (ci < cmds->cnt && ei < excludes->cnt) { |
| 79 | cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); |
| 80 | if (cmp < 0) |
| 81 | cmds->names[cj++] = cmds->names[ci++]; |
| 82 | else if (cmp == 0) |
| 83 | ci++, ei++; |
| 84 | else if (cmp > 0) |
| 85 | ei++; |
| 86 | } |
| 87 | |
| 88 | while (ci < cmds->cnt) |
| 89 | cmds->names[cj++] = cmds->names[ci++]; |
| 90 | |
| 91 | cmds->cnt = cj; |
| 92 | } |
| 93 | |
| 94 | static void pretty_print_string_list(struct cmdnames *cmds, int longest) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 95 | { |
| 96 | int cols = 1, rows; |
| 97 | int space = longest + 1; /* min 1 SP between words */ |
| 98 | int max_cols = term_columns() - 1; /* don't print *on* the edge */ |
| 99 | int i, j; |
| 100 | |
| 101 | if (space < max_cols) |
| 102 | cols = max_cols / space; |
Pierre Habouzit | 98cb6f3 | 2009-07-22 23:34:35 +0200 | [diff] [blame] | 103 | rows = DIV_ROUND_UP(cmds->cnt, cols); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 104 | |
| 105 | for (i = 0; i < rows; i++) { |
| 106 | printf(" "); |
| 107 | |
| 108 | for (j = 0; j < cols; j++) { |
| 109 | int n = j * rows + i; |
| 110 | int size = space; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 111 | if (n >= cmds->cnt) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 112 | break; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 113 | if (j == cols-1 || n + rows >= cmds->cnt) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 114 | size = 1; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 115 | printf("%-*s", size, cmds->names[n]->name); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 116 | } |
| 117 | putchar('\n'); |
| 118 | } |
| 119 | } |
| 120 | |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 121 | static int is_executable(const char *name) |
| 122 | { |
| 123 | struct stat st; |
| 124 | |
| 125 | if (stat(name, &st) || /* stat, not lstat */ |
| 126 | !S_ISREG(st.st_mode)) |
| 127 | return 0; |
| 128 | |
Frank Li | 71064e3 | 2009-09-16 10:20:22 +0200 | [diff] [blame] | 129 | #ifdef WIN32 |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 130 | { /* cannot trust the executable bit, peek into the file instead */ |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 131 | char buf[3] = { 0 }; |
| 132 | int n; |
| 133 | int fd = open(name, O_RDONLY); |
| 134 | st.st_mode &= ~S_IXUSR; |
| 135 | if (fd >= 0) { |
| 136 | n = read(fd, buf, 2); |
| 137 | if (n == 2) |
| 138 | /* DOS executables start with "MZ" */ |
| 139 | if (!strcmp(buf, "#!") || !strcmp(buf, "MZ")) |
| 140 | st.st_mode |= S_IXUSR; |
| 141 | close(fd); |
| 142 | } |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 143 | } |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 144 | #endif |
| 145 | return st.st_mode & S_IXUSR; |
| 146 | } |
| 147 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 148 | static void list_commands_in_dir(struct cmdnames *cmds, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 149 | const char *path, |
| 150 | const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 151 | { |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 152 | int prefix_len; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 153 | DIR *dir = opendir(path); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 154 | struct dirent *de; |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 155 | struct strbuf buf = STRBUF_INIT; |
| 156 | int len; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 157 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 158 | if (!dir) |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 159 | return; |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 160 | if (!prefix) |
| 161 | prefix = "git-"; |
| 162 | prefix_len = strlen(prefix); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 163 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 164 | strbuf_addf(&buf, "%s/", path); |
| 165 | len = buf.len; |
| 166 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 167 | while ((de = readdir(dir)) != NULL) { |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 168 | int entlen; |
| 169 | |
Scott R Parish | edb6ddc | 2007-10-27 01:36:50 -0700 | [diff] [blame] | 170 | if (prefixcmp(de->d_name, prefix)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 171 | continue; |
Scott R Parish | 0966003 | 2007-10-27 01:36:52 -0700 | [diff] [blame] | 172 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 173 | strbuf_setlen(&buf, len); |
| 174 | strbuf_addstr(&buf, de->d_name); |
| 175 | if (!is_executable(buf.buf)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 176 | continue; |
| 177 | |
Scott R Parish | edb6ddc | 2007-10-27 01:36:50 -0700 | [diff] [blame] | 178 | entlen = strlen(de->d_name) - prefix_len; |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 179 | if (has_extension(de->d_name, ".exe")) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 180 | entlen -= 4; |
| 181 | |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 182 | add_cmdname(cmds, de->d_name + prefix_len, entlen); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 183 | } |
| 184 | closedir(dir); |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 185 | strbuf_release(&buf); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 188 | void load_command_list(const char *prefix, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 189 | struct cmdnames *main_cmds, |
| 190 | struct cmdnames *other_cmds) |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 191 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 192 | const char *env_path = getenv("PATH"); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 193 | const char *exec_path = git_exec_path(); |
| 194 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 195 | if (exec_path) { |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 196 | list_commands_in_dir(main_cmds, exec_path, prefix); |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 197 | qsort(main_cmds->names, main_cmds->cnt, |
| 198 | sizeof(*main_cmds->names), cmdname_compare); |
| 199 | uniq(main_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 202 | if (env_path) { |
| 203 | char *paths, *path, *colon; |
| 204 | path = paths = xstrdup(env_path); |
| 205 | while (1) { |
| 206 | if ((colon = strchr(path, PATH_SEP))) |
| 207 | *colon = 0; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 208 | if (!exec_path || strcmp(path, exec_path)) |
| 209 | list_commands_in_dir(other_cmds, path, prefix); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 210 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 211 | if (!colon) |
| 212 | break; |
| 213 | path = colon + 1; |
| 214 | } |
| 215 | free(paths); |
| 216 | |
| 217 | qsort(other_cmds->names, other_cmds->cnt, |
| 218 | sizeof(*other_cmds->names), cmdname_compare); |
| 219 | uniq(other_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 220 | } |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 221 | exclude_cmds(other_cmds, main_cmds); |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 224 | void list_commands(const char *title, struct cmdnames *main_cmds, |
| 225 | struct cmdnames *other_cmds) |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 226 | { |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 227 | int i, longest = 0; |
| 228 | |
| 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; |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 235 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 236 | if (main_cmds->cnt) { |
Alex Riesen | 61c5d43 | 2008-08-28 19:19:42 +0200 | [diff] [blame] | 237 | const char *exec_path = git_exec_path(); |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 238 | printf("available %s in '%s'\n", title, exec_path); |
| 239 | printf("----------------"); |
| 240 | mput_char('-', strlen(title) + strlen(exec_path)); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 241 | putchar('\n'); |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 242 | pretty_print_string_list(main_cmds, longest); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 243 | putchar('\n'); |
| 244 | } |
| 245 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 246 | if (other_cmds->cnt) { |
| 247 | printf("%s available from elsewhere on your $PATH\n", title); |
| 248 | printf("---------------------------------------"); |
| 249 | mput_char('-', strlen(title)); |
| 250 | putchar('\n'); |
| 251 | pretty_print_string_list(other_cmds, longest); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 252 | putchar('\n'); |
| 253 | } |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 256 | int is_in_cmdlist(struct cmdnames *c, const char *s) |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 257 | { |
| 258 | int i; |
| 259 | for (i = 0; i < c->cnt; i++) |
| 260 | if (!strcmp(s, c->names[i]->name)) |
| 261 | return 1; |
| 262 | return 0; |
| 263 | } |
| 264 | |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 265 | static int autocorrect; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 266 | static struct cmdnames aliases; |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 267 | |
| 268 | static int git_unknown_cmd_config(const char *var, const char *value, void *cb) |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 269 | { |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 270 | if (!strcmp(var, "help.autocorrect")) |
| 271 | autocorrect = git_config_int(var,value); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 272 | /* Also use aliases for command lookup */ |
| 273 | if (!prefixcmp(var, "alias.")) |
| 274 | add_cmdname(&aliases, var + 6, strlen(var + 6)); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 275 | |
| 276 | return git_default_config(var, value, cb); |
| 277 | } |
| 278 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 279 | static int levenshtein_compare(const void *p1, const void *p2) |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 280 | { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 281 | const struct cmdname *const *c1 = p1, *const *c2 = p2; |
| 282 | const char *s1 = (*c1)->name, *s2 = (*c2)->name; |
| 283 | int l1 = (*c1)->len; |
| 284 | int l2 = (*c2)->len; |
| 285 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); |
| 286 | } |
| 287 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 288 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
| 289 | { |
| 290 | int i; |
| 291 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); |
| 292 | |
| 293 | for (i = 0; i < old->cnt; i++) |
| 294 | cmds->names[cmds->cnt++] = old->names[i]; |
| 295 | free(old->names); |
| 296 | old->cnt = 0; |
| 297 | old->names = NULL; |
| 298 | } |
| 299 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 300 | /* An empirically derived magic number */ |
| 301 | #define SIMILAR_ENOUGH(x) ((x) < 6) |
| 302 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 303 | const char *help_unknown_cmd(const char *cmd) |
| 304 | { |
| 305 | int i, n, best_similarity = 0; |
| 306 | struct cmdnames main_cmds, other_cmds; |
| 307 | |
| 308 | memset(&main_cmds, 0, sizeof(main_cmds)); |
Johan Herland | 0b74f5d | 2009-08-11 12:10:21 +0200 | [diff] [blame] | 309 | memset(&other_cmds, 0, sizeof(other_cmds)); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 310 | memset(&aliases, 0, sizeof(aliases)); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 311 | |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 312 | git_config(git_unknown_cmd_config, NULL); |
| 313 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 314 | load_command_list("git-", &main_cmds, &other_cmds); |
| 315 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 316 | add_cmd_list(&main_cmds, &aliases); |
| 317 | add_cmd_list(&main_cmds, &other_cmds); |
| 318 | qsort(main_cmds.names, main_cmds.cnt, |
| 319 | sizeof(main_cmds.names), cmdname_compare); |
| 320 | uniq(&main_cmds); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 321 | |
| 322 | /* This reuses cmdname->len for similarity index */ |
| 323 | for (i = 0; i < main_cmds.cnt; ++i) |
| 324 | main_cmds.names[i]->len = |
| 325 | levenshtein(cmd, main_cmds.names[i]->name, 0, 2, 1, 4); |
| 326 | |
| 327 | qsort(main_cmds.names, main_cmds.cnt, |
| 328 | sizeof(*main_cmds.names), levenshtein_compare); |
| 329 | |
| 330 | if (!main_cmds.cnt) |
| 331 | die ("Uh oh. Your system reports no Git commands at all."); |
| 332 | |
| 333 | best_similarity = main_cmds.names[0]->len; |
| 334 | n = 1; |
| 335 | while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len) |
| 336 | ++n; |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 337 | if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 338 | const char *assumed = main_cmds.names[0]->name; |
| 339 | main_cmds.names[0] = NULL; |
| 340 | clean_cmdnames(&main_cmds); |
Ori Avtalion | 57f6ec0 | 2009-08-07 17:24:21 +0300 | [diff] [blame] | 341 | fprintf(stderr, "WARNING: You called a Git command named '%s', " |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 342 | "which does not exist.\n" |
| 343 | "Continuing under the assumption that you meant '%s'\n", |
| 344 | cmd, assumed); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 345 | if (autocorrect > 0) { |
| 346 | fprintf(stderr, "in %0.1f seconds automatically...\n", |
| 347 | (float)autocorrect/10.0); |
| 348 | poll(NULL, 0, autocorrect * 100); |
| 349 | } |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 350 | return assumed; |
| 351 | } |
| 352 | |
Pete Harlan | 7283bbc | 2010-02-15 15:33:18 -0800 | [diff] [blame] | 353 | fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 354 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 355 | if (SIMILAR_ENOUGH(best_similarity)) { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 356 | fprintf(stderr, "\nDid you mean %s?\n", |
| 357 | n < 2 ? "this": "one of these"); |
| 358 | |
| 359 | for (i = 0; i < n; i++) |
| 360 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); |
| 361 | } |
| 362 | |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 363 | exit(1); |
| 364 | } |
| 365 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 366 | int cmd_version(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 367 | { |
| 368 | printf("git version %s\n", git_version_string); |
| 369 | return 0; |
| 370 | } |