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" |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 6 | #include "common-cmds.h" |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 7 | #include "string-list.h" |
| 8 | #include "column.h" |
Jeff King | 816fb46 | 2012-06-02 14:51:42 -0400 | [diff] [blame] | 9 | #include "version.h" |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 10 | #include "refs.h" |
Christian Couder | 6494998 | 2008-03-07 08:46:28 +0100 | [diff] [blame] | 11 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 12 | void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 13 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 14 | struct cmdname *ent; |
| 15 | FLEX_ALLOC_MEM(ent, name, name, len); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 16 | ent->len = len; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 17 | |
| 18 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); |
| 19 | cmds->names[cmds->cnt++] = ent; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 22 | static void clean_cmdnames(struct cmdnames *cmds) |
| 23 | { |
| 24 | int i; |
| 25 | for (i = 0; i < cmds->cnt; ++i) |
| 26 | free(cmds->names[i]); |
| 27 | free(cmds->names); |
| 28 | cmds->cnt = 0; |
| 29 | cmds->alloc = 0; |
| 30 | } |
| 31 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 32 | static int cmdname_compare(const void *a_, const void *b_) |
| 33 | { |
| 34 | struct cmdname *a = *(struct cmdname **)a_; |
| 35 | struct cmdname *b = *(struct cmdname **)b_; |
| 36 | return strcmp(a->name, b->name); |
| 37 | } |
| 38 | |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 39 | static void uniq(struct cmdnames *cmds) |
| 40 | { |
| 41 | int i, j; |
| 42 | |
| 43 | if (!cmds->cnt) |
| 44 | return; |
| 45 | |
Jeff King | 4a15758 | 2012-07-26 00:16:19 +0800 | [diff] [blame] | 46 | for (i = j = 1; i < cmds->cnt; i++) { |
| 47 | if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name)) |
| 48 | free(cmds->names[i]); |
| 49 | else |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 50 | cmds->names[j++] = cmds->names[i]; |
Jeff King | 4a15758 | 2012-07-26 00:16:19 +0800 | [diff] [blame] | 51 | } |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 52 | |
| 53 | cmds->cnt = j; |
| 54 | } |
| 55 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 56 | void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 57 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 58 | int ci, cj, ei; |
| 59 | int cmp; |
| 60 | |
| 61 | ci = cj = ei = 0; |
| 62 | while (ci < cmds->cnt && ei < excludes->cnt) { |
| 63 | cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); |
| 64 | if (cmp < 0) |
| 65 | cmds->names[cj++] = cmds->names[ci++]; |
Junio C Hamano | 6a17f58 | 2012-07-25 11:01:12 -0700 | [diff] [blame] | 66 | else if (cmp == 0) { |
| 67 | ei++; |
| 68 | free(cmds->names[ci++]); |
| 69 | } else if (cmp > 0) |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 70 | ei++; |
| 71 | } |
| 72 | |
| 73 | while (ci < cmds->cnt) |
| 74 | cmds->names[cj++] = cmds->names[ci++]; |
| 75 | |
| 76 | cmds->cnt = cj; |
| 77 | } |
| 78 | |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 79 | static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 80 | { |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 81 | struct string_list list = STRING_LIST_INIT_NODUP; |
| 82 | struct column_options copts; |
| 83 | int i; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 84 | |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 85 | for (i = 0; i < cmds->cnt; i++) |
| 86 | string_list_append(&list, cmds->names[i]->name); |
| 87 | /* |
| 88 | * always enable column display, we only consult column.* |
| 89 | * about layout strategy and stuff |
| 90 | */ |
| 91 | colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED; |
| 92 | memset(&copts, 0, sizeof(copts)); |
| 93 | copts.indent = " "; |
| 94 | copts.padding = 2; |
| 95 | print_columns(&list, colopts, &copts); |
| 96 | string_list_clear(&list, 0); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 99 | static int is_executable(const char *name) |
| 100 | { |
| 101 | struct stat st; |
| 102 | |
| 103 | if (stat(name, &st) || /* stat, not lstat */ |
| 104 | !S_ISREG(st.st_mode)) |
| 105 | return 0; |
| 106 | |
Ramsay Jones | f66450a | 2013-06-22 20:42:47 +0100 | [diff] [blame] | 107 | #if defined(GIT_WINDOWS_NATIVE) |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 108 | { /* cannot trust the executable bit, peek into the file instead */ |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 109 | char buf[3] = { 0 }; |
| 110 | int n; |
| 111 | int fd = open(name, O_RDONLY); |
| 112 | st.st_mode &= ~S_IXUSR; |
| 113 | if (fd >= 0) { |
| 114 | n = read(fd, buf, 2); |
| 115 | if (n == 2) |
| 116 | /* DOS executables start with "MZ" */ |
| 117 | if (!strcmp(buf, "#!") || !strcmp(buf, "MZ")) |
| 118 | st.st_mode |= S_IXUSR; |
| 119 | close(fd); |
| 120 | } |
Frank Li | 0d30ad7 | 2009-09-16 10:20:17 +0200 | [diff] [blame] | 121 | } |
Johannes Sixt | cc3b7a9 | 2008-01-14 14:05:33 +0100 | [diff] [blame] | 122 | #endif |
| 123 | return st.st_mode & S_IXUSR; |
| 124 | } |
| 125 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 126 | static void list_commands_in_dir(struct cmdnames *cmds, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 127 | const char *path, |
| 128 | const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 129 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 130 | DIR *dir = opendir(path); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 131 | struct dirent *de; |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 132 | struct strbuf buf = STRBUF_INIT; |
| 133 | int len; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 134 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 135 | if (!dir) |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 136 | return; |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 137 | if (!prefix) |
| 138 | prefix = "git-"; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 139 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 140 | strbuf_addf(&buf, "%s/", path); |
| 141 | len = buf.len; |
| 142 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 143 | while ((de = readdir(dir)) != NULL) { |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 144 | const char *ent; |
Jeff King | 26936bf | 2014-06-30 12:58:51 -0400 | [diff] [blame] | 145 | size_t entlen; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 146 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 147 | if (!skip_prefix(de->d_name, prefix, &ent)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 148 | continue; |
Scott R Parish | 0966003 | 2007-10-27 01:36:52 -0700 | [diff] [blame] | 149 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 150 | strbuf_setlen(&buf, len); |
| 151 | strbuf_addstr(&buf, de->d_name); |
| 152 | if (!is_executable(buf.buf)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 153 | continue; |
| 154 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 155 | entlen = strlen(ent); |
Junio C Hamano | 6e40947 | 2014-07-16 11:25:59 -0700 | [diff] [blame] | 156 | strip_suffix(ent, ".exe", &entlen); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 157 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 158 | add_cmdname(cmds, ent, entlen); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 159 | } |
| 160 | closedir(dir); |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 161 | strbuf_release(&buf); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 164 | void load_command_list(const char *prefix, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 165 | struct cmdnames *main_cmds, |
| 166 | struct cmdnames *other_cmds) |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 167 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 168 | const char *env_path = getenv("PATH"); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 169 | const char *exec_path = git_exec_path(); |
| 170 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 171 | if (exec_path) { |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 172 | list_commands_in_dir(main_cmds, exec_path, prefix); |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 173 | qsort(main_cmds->names, main_cmds->cnt, |
| 174 | sizeof(*main_cmds->names), cmdname_compare); |
| 175 | uniq(main_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 178 | if (env_path) { |
| 179 | char *paths, *path, *colon; |
| 180 | path = paths = xstrdup(env_path); |
| 181 | while (1) { |
| 182 | if ((colon = strchr(path, PATH_SEP))) |
| 183 | *colon = 0; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 184 | if (!exec_path || strcmp(path, exec_path)) |
| 185 | list_commands_in_dir(other_cmds, path, prefix); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 186 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 187 | if (!colon) |
| 188 | break; |
| 189 | path = colon + 1; |
| 190 | } |
| 191 | free(paths); |
| 192 | |
| 193 | qsort(other_cmds->names, other_cmds->cnt, |
| 194 | sizeof(*other_cmds->names), cmdname_compare); |
| 195 | uniq(other_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 196 | } |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 197 | exclude_cmds(other_cmds, main_cmds); |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Junio C Hamano | f4ed0af | 2012-05-03 15:13:31 -0700 | [diff] [blame] | 200 | void list_commands(unsigned int colopts, |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 201 | struct cmdnames *main_cmds, struct cmdnames *other_cmds) |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 202 | { |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 203 | if (main_cmds->cnt) { |
Alex Riesen | 61c5d43 | 2008-08-28 19:19:42 +0200 | [diff] [blame] | 204 | const char *exec_path = git_exec_path(); |
Nguyễn Thái Ngọc Duy | 4470ef9 | 2012-04-25 18:21:53 +0700 | [diff] [blame] | 205 | printf_ln(_("available git commands in '%s'"), exec_path); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 206 | putchar('\n'); |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 207 | pretty_print_cmdnames(main_cmds, colopts); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 208 | putchar('\n'); |
| 209 | } |
| 210 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 211 | if (other_cmds->cnt) { |
Nguyễn Thái Ngọc Duy | 4470ef9 | 2012-04-25 18:21:53 +0700 | [diff] [blame] | 212 | printf_ln(_("git commands available from elsewhere on your $PATH")); |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 213 | putchar('\n'); |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 214 | pretty_print_cmdnames(other_cmds, colopts); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 215 | putchar('\n'); |
| 216 | } |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Sébastien Guimmara | 2241477 | 2015-05-21 19:39:22 +0200 | [diff] [blame] | 219 | static int cmd_group_cmp(const void *elem1, const void *elem2) |
| 220 | { |
| 221 | const struct cmdname_help *e1 = elem1; |
| 222 | const struct cmdname_help *e2 = elem2; |
| 223 | |
| 224 | if (e1->group < e2->group) |
| 225 | return -1; |
| 226 | if (e1->group > e2->group) |
| 227 | return 1; |
| 228 | return strcmp(e1->name, e2->name); |
| 229 | } |
| 230 | |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 231 | void list_common_cmds_help(void) |
| 232 | { |
| 233 | int i, longest = 0; |
Sébastien Guimmara | 2241477 | 2015-05-21 19:39:22 +0200 | [diff] [blame] | 234 | int current_grp = -1; |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 235 | |
| 236 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
| 237 | if (longest < strlen(common_cmds[i].name)) |
| 238 | longest = strlen(common_cmds[i].name); |
| 239 | } |
| 240 | |
Sébastien Guimmara | 2241477 | 2015-05-21 19:39:22 +0200 | [diff] [blame] | 241 | qsort(common_cmds, ARRAY_SIZE(common_cmds), |
| 242 | sizeof(common_cmds[0]), cmd_group_cmp); |
| 243 | |
| 244 | puts(_("These are common Git commands used in various situations:")); |
| 245 | |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 246 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
Sébastien Guimmara | 2241477 | 2015-05-21 19:39:22 +0200 | [diff] [blame] | 247 | if (common_cmds[i].group != current_grp) { |
| 248 | printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group])); |
| 249 | current_grp = common_cmds[i].group; |
| 250 | } |
| 251 | |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 252 | printf(" %s ", common_cmds[i].name); |
| 253 | mput_char(' ', longest - strlen(common_cmds[i].name)); |
| 254 | puts(_(common_cmds[i].help)); |
| 255 | } |
| 256 | } |
| 257 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 258 | int is_in_cmdlist(struct cmdnames *c, const char *s) |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 259 | { |
| 260 | int i; |
| 261 | for (i = 0; i < c->cnt; i++) |
| 262 | if (!strcmp(s, c->names[i]->name)) |
| 263 | return 1; |
| 264 | return 0; |
| 265 | } |
| 266 | |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 267 | static int autocorrect; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 268 | static struct cmdnames aliases; |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 269 | |
| 270 | 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] | 271 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 272 | const char *p; |
| 273 | |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 274 | if (!strcmp(var, "help.autocorrect")) |
| 275 | autocorrect = git_config_int(var,value); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 276 | /* Also use aliases for command lookup */ |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 277 | if (skip_prefix(var, "alias.", &p)) |
| 278 | add_cmdname(&aliases, p, strlen(p)); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 279 | |
| 280 | return git_default_config(var, value, cb); |
| 281 | } |
| 282 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 283 | static int levenshtein_compare(const void *p1, const void *p2) |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 284 | { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 285 | const struct cmdname *const *c1 = p1, *const *c2 = p2; |
| 286 | const char *s1 = (*c1)->name, *s2 = (*c2)->name; |
| 287 | int l1 = (*c1)->len; |
| 288 | int l2 = (*c2)->len; |
| 289 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); |
| 290 | } |
| 291 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 292 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
| 293 | { |
| 294 | int i; |
| 295 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); |
| 296 | |
| 297 | for (i = 0; i < old->cnt; i++) |
| 298 | cmds->names[cmds->cnt++] = old->names[i]; |
| 299 | free(old->names); |
| 300 | old->cnt = 0; |
| 301 | old->names = NULL; |
| 302 | } |
| 303 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 304 | /* An empirically derived magic number */ |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 305 | #define SIMILARITY_FLOOR 7 |
| 306 | #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR) |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 307 | |
Michael Schubert | 823e0de | 2011-07-08 12:08:49 +0200 | [diff] [blame] | 308 | static const char bad_interpreter_advice[] = |
| 309 | N_("'%s' appears to be a git command, but we were not\n" |
| 310 | "able to execute it. Maybe git-%s is broken?"); |
| 311 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 312 | const char *help_unknown_cmd(const char *cmd) |
| 313 | { |
| 314 | int i, n, best_similarity = 0; |
| 315 | struct cmdnames main_cmds, other_cmds; |
| 316 | |
| 317 | memset(&main_cmds, 0, sizeof(main_cmds)); |
Johan Herland | 0b74f5d | 2009-08-11 12:10:21 +0200 | [diff] [blame] | 318 | memset(&other_cmds, 0, sizeof(other_cmds)); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 319 | memset(&aliases, 0, sizeof(aliases)); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 320 | |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 321 | git_config(git_unknown_cmd_config, NULL); |
| 322 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 323 | load_command_list("git-", &main_cmds, &other_cmds); |
| 324 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 325 | add_cmd_list(&main_cmds, &aliases); |
| 326 | add_cmd_list(&main_cmds, &other_cmds); |
| 327 | qsort(main_cmds.names, main_cmds.cnt, |
Stefan Beller | d333ac1 | 2014-09-17 14:14:39 +0200 | [diff] [blame] | 328 | sizeof(*main_cmds.names), cmdname_compare); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 329 | uniq(&main_cmds); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 330 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 331 | /* This abuses cmdname->len for levenshtein distance */ |
| 332 | for (i = 0, n = 0; i < main_cmds.cnt; i++) { |
| 333 | int cmp = 0; /* avoid compiler stupidity */ |
| 334 | const char *candidate = main_cmds.names[i]->name; |
| 335 | |
Michael Schubert | 823e0de | 2011-07-08 12:08:49 +0200 | [diff] [blame] | 336 | /* |
| 337 | * An exact match means we have the command, but |
| 338 | * for some reason exec'ing it gave us ENOENT; probably |
| 339 | * it's a bad interpreter in the #! line. |
| 340 | */ |
| 341 | if (!strcmp(candidate, cmd)) |
| 342 | die(_(bad_interpreter_advice), cmd, cmd); |
| 343 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 344 | /* Does the candidate appear in common_cmds list? */ |
| 345 | while (n < ARRAY_SIZE(common_cmds) && |
| 346 | (cmp = strcmp(common_cmds[n].name, candidate)) < 0) |
| 347 | n++; |
| 348 | if ((n < ARRAY_SIZE(common_cmds)) && !cmp) { |
| 349 | /* Yes, this is one of the common commands */ |
| 350 | n++; /* use the entry from common_cmds[] */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 351 | if (starts_with(candidate, cmd)) { |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 352 | /* Give prefix match a very good score */ |
| 353 | main_cmds.names[i]->len = 0; |
| 354 | continue; |
| 355 | } |
| 356 | } |
| 357 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 358 | main_cmds.names[i]->len = |
Matthieu Moy | c41494f | 2012-05-27 18:02:58 +0200 | [diff] [blame] | 359 | levenshtein(cmd, candidate, 0, 2, 1, 3) + 1; |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 360 | } |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 361 | |
| 362 | qsort(main_cmds.names, main_cmds.cnt, |
| 363 | sizeof(*main_cmds.names), levenshtein_compare); |
| 364 | |
| 365 | if (!main_cmds.cnt) |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 366 | die(_("Uh oh. Your system reports no Git commands at all.")); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 367 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 368 | /* skip and count prefix matches */ |
| 369 | for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++) |
| 370 | ; /* still counting */ |
| 371 | |
| 372 | if (main_cmds.cnt <= n) { |
| 373 | /* prefix matches with everything? that is too ambiguous */ |
| 374 | best_similarity = SIMILARITY_FLOOR + 1; |
| 375 | } else { |
| 376 | /* count all the most similar ones */ |
| 377 | for (best_similarity = main_cmds.names[n++]->len; |
| 378 | (n < main_cmds.cnt && |
| 379 | best_similarity == main_cmds.names[n]->len); |
| 380 | n++) |
| 381 | ; /* still counting */ |
| 382 | } |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 383 | if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 384 | const char *assumed = main_cmds.names[0]->name; |
| 385 | main_cmds.names[0] = NULL; |
| 386 | clean_cmdnames(&main_cmds); |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 387 | fprintf_ln(stderr, |
| 388 | _("WARNING: You called a Git command named '%s', " |
| 389 | "which does not exist.\n" |
| 390 | "Continuing under the assumption that you meant '%s'"), |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 391 | cmd, assumed); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 392 | if (autocorrect > 0) { |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 393 | fprintf_ln(stderr, _("in %0.1f seconds automatically..."), |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 394 | (float)autocorrect/10.0); |
Johannes Sixt | 2024d31 | 2015-06-05 21:45:05 +0200 | [diff] [blame] | 395 | sleep_millisec(autocorrect * 100); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 396 | } |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 397 | return assumed; |
| 398 | } |
| 399 | |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 400 | fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 401 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 402 | if (SIMILAR_ENOUGH(best_similarity)) { |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 403 | fprintf_ln(stderr, |
| 404 | Q_("\nDid you mean this?", |
| 405 | "\nDid you mean one of these?", |
| 406 | n)); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 407 | |
| 408 | for (i = 0; i < n; i++) |
| 409 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); |
| 410 | } |
| 411 | |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 412 | exit(1); |
| 413 | } |
| 414 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 415 | int cmd_version(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 416 | { |
David Aguilar | f2de0b9 | 2013-04-16 13:33:25 -0700 | [diff] [blame] | 417 | /* |
| 418 | * The format of this string should be kept stable for compatibility |
| 419 | * with external projects that rely on the output of "git version". |
| 420 | */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 421 | printf("git version %s\n", git_version_string); |
| 422 | return 0; |
| 423 | } |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 424 | |
| 425 | struct similar_ref_cb { |
| 426 | const char *base_ref; |
| 427 | struct string_list *similar_refs; |
| 428 | }; |
| 429 | |
Michael Haggerty | 91d6e94 | 2015-05-25 18:38:54 +0000 | [diff] [blame] | 430 | static int append_similar_ref(const char *refname, const struct object_id *oid, |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 431 | int flags, void *cb_data) |
| 432 | { |
| 433 | struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); |
| 434 | char *branch = strrchr(refname, '/') + 1; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 435 | const char *remote; |
| 436 | |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 437 | /* A remote branch of the same name is deemed similar */ |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 438 | if (skip_prefix(refname, "refs/remotes/", &remote) && |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 439 | !strcmp(branch, cb->base_ref)) |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 440 | string_list_append(cb->similar_refs, remote); |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static struct string_list guess_refs(const char *ref) |
| 445 | { |
| 446 | struct similar_ref_cb ref_cb; |
| 447 | struct string_list similar_refs = STRING_LIST_INIT_NODUP; |
| 448 | |
| 449 | ref_cb.base_ref = ref; |
| 450 | ref_cb.similar_refs = &similar_refs; |
| 451 | for_each_ref(append_similar_ref, &ref_cb); |
| 452 | return similar_refs; |
| 453 | } |
| 454 | |
| 455 | void help_unknown_ref(const char *ref, const char *cmd, const char *error) |
| 456 | { |
| 457 | int i; |
| 458 | struct string_list suggested_refs = guess_refs(ref); |
| 459 | |
| 460 | fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error); |
| 461 | |
| 462 | if (suggested_refs.nr > 0) { |
| 463 | fprintf_ln(stderr, |
| 464 | Q_("\nDid you mean this?", |
| 465 | "\nDid you mean one of these?", |
| 466 | suggested_refs.nr)); |
| 467 | for (i = 0; i < suggested_refs.nr; i++) |
| 468 | fprintf(stderr, "\t%s\n", suggested_refs.items[i].string); |
| 469 | } |
| 470 | |
| 471 | string_list_clear(&suggested_refs, 0); |
| 472 | exit(1); |
| 473 | } |