Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 3 | #include "builtin.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 4 | #include "exec-cmd.h" |
Brandon Williams | 38124a4 | 2017-04-25 16:46:59 -0700 | [diff] [blame] | 5 | #include "run-command.h" |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 6 | #include "levenshtein.h" |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 7 | #include "help.h" |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 8 | #include "command-list.h" |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 9 | #include "string-list.h" |
| 10 | #include "column.h" |
Jeff King | 816fb46 | 2012-06-02 14:51:42 -0400 | [diff] [blame] | 11 | #include "version.h" |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 12 | #include "refs.h" |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 13 | #include "parse-options.h" |
Christian Couder | 6494998 | 2008-03-07 08:46:28 +0100 | [diff] [blame] | 14 | |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 15 | struct category_description { |
| 16 | uint32_t category; |
| 17 | const char *desc; |
| 18 | }; |
| 19 | static uint32_t common_mask = |
| 20 | CAT_init | CAT_worktree | CAT_info | |
| 21 | CAT_history | CAT_remote; |
| 22 | static struct category_description common_categories[] = { |
| 23 | { CAT_init, N_("start a working area (see also: git help tutorial)") }, |
| 24 | { CAT_worktree, N_("work on the current change (see also: git help everyday)") }, |
| 25 | { CAT_info, N_("examine the history and state (see also: git help revisions)") }, |
| 26 | { CAT_history, N_("grow, mark and tweak your common history") }, |
| 27 | { CAT_remote, N_("collaborate (see also: git help workflows)") }, |
| 28 | { 0, NULL } |
| 29 | }; |
Nguyễn Thái Ngọc Duy | 63eae83 | 2018-05-20 20:40:01 +0200 | [diff] [blame] | 30 | static struct category_description main_categories[] = { |
| 31 | { CAT_mainporcelain, N_("Main Porcelain Commands") }, |
| 32 | { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") }, |
| 33 | { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") }, |
| 34 | { CAT_foreignscminterface, N_("Interacting with Others") }, |
| 35 | { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") }, |
| 36 | { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") }, |
| 37 | { CAT_synchingrepositories, N_("Low-level Commands / Synching Repositories") }, |
| 38 | { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") }, |
| 39 | { 0, NULL } |
| 40 | }; |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 41 | |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 42 | static const char *drop_prefix(const char *name, uint32_t category) |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 43 | { |
| 44 | const char *new_name; |
| 45 | |
| 46 | if (skip_prefix(name, "git-", &new_name)) |
| 47 | return new_name; |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 48 | if (category == CAT_guide && skip_prefix(name, "git", &new_name)) |
| 49 | return new_name; |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 50 | return name; |
| 51 | |
| 52 | } |
| 53 | |
| 54 | static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask) |
| 55 | { |
| 56 | int i, nr = 0; |
| 57 | struct cmdname_help *cmds; |
| 58 | |
| 59 | if (ARRAY_SIZE(command_list) == 0) |
| 60 | BUG("empty command_list[] is a sign of broken generate-cmdlist.sh"); |
| 61 | |
| 62 | ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1); |
| 63 | |
| 64 | for (i = 0; i < ARRAY_SIZE(command_list); i++) { |
| 65 | const struct cmdname_help *cmd = command_list + i; |
| 66 | |
| 67 | if (!(cmd->category & mask)) |
| 68 | continue; |
| 69 | |
| 70 | cmds[nr] = *cmd; |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 71 | cmds[nr].name = drop_prefix(cmd->name, cmd->category); |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 72 | |
| 73 | nr++; |
| 74 | } |
| 75 | cmds[nr].name = NULL; |
| 76 | *p_cmds = cmds; |
| 77 | } |
| 78 | |
| 79 | static void print_command_list(const struct cmdname_help *cmds, |
| 80 | uint32_t mask, int longest) |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | for (i = 0; cmds[i].name; i++) { |
| 85 | if (cmds[i].category & mask) { |
Johannes Schindelin | 1c4b985 | 2018-12-11 06:58:11 -0800 | [diff] [blame] | 86 | size_t len = strlen(cmds[i].name); |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 87 | printf(" %s ", cmds[i].name); |
Nguyễn Thái Ngọc Duy | 6195a76 | 2019-01-31 16:23:49 +0700 | [diff] [blame] | 88 | if (longest > len) |
| 89 | mput_char(' ', longest - len); |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 90 | puts(_(cmds[i].help)); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static int cmd_name_cmp(const void *elem1, const void *elem2) |
| 96 | { |
| 97 | const struct cmdname_help *e1 = elem1; |
| 98 | const struct cmdname_help *e2 = elem2; |
| 99 | |
| 100 | return strcmp(e1->name, e2->name); |
| 101 | } |
| 102 | |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 103 | static void print_cmd_by_category(const struct category_description *catdesc, |
| 104 | int *longest_p) |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 105 | { |
| 106 | struct cmdname_help *cmds; |
| 107 | int longest = 0; |
| 108 | int i, nr = 0; |
| 109 | uint32_t mask = 0; |
| 110 | |
| 111 | for (i = 0; catdesc[i].desc; i++) |
| 112 | mask |= catdesc[i].category; |
| 113 | |
| 114 | extract_cmds(&cmds, mask); |
| 115 | |
| 116 | for (i = 0; cmds[i].name; i++, nr++) { |
| 117 | if (longest < strlen(cmds[i].name)) |
| 118 | longest = strlen(cmds[i].name); |
| 119 | } |
| 120 | QSORT(cmds, nr, cmd_name_cmp); |
| 121 | |
| 122 | for (i = 0; catdesc[i].desc; i++) { |
| 123 | uint32_t mask = catdesc[i].category; |
| 124 | const char *desc = catdesc[i].desc; |
| 125 | |
| 126 | printf("\n%s\n", _(desc)); |
| 127 | print_command_list(cmds, mask, longest); |
| 128 | } |
| 129 | free(cmds); |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 130 | if (longest_p) |
| 131 | *longest_p = longest; |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 134 | void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 135 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 136 | struct cmdname *ent; |
| 137 | FLEX_ALLOC_MEM(ent, name, name, len); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 138 | ent->len = len; |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 139 | |
| 140 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); |
| 141 | cmds->names[cmds->cnt++] = ent; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 144 | static void clean_cmdnames(struct cmdnames *cmds) |
| 145 | { |
| 146 | int i; |
| 147 | for (i = 0; i < cmds->cnt; ++i) |
| 148 | free(cmds->names[i]); |
| 149 | free(cmds->names); |
| 150 | cmds->cnt = 0; |
| 151 | cmds->alloc = 0; |
| 152 | } |
| 153 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 154 | static int cmdname_compare(const void *a_, const void *b_) |
| 155 | { |
| 156 | struct cmdname *a = *(struct cmdname **)a_; |
| 157 | struct cmdname *b = *(struct cmdname **)b_; |
| 158 | return strcmp(a->name, b->name); |
| 159 | } |
| 160 | |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 161 | static void uniq(struct cmdnames *cmds) |
| 162 | { |
| 163 | int i, j; |
| 164 | |
| 165 | if (!cmds->cnt) |
| 166 | return; |
| 167 | |
Jeff King | 4a15758 | 2012-07-26 00:16:19 +0800 | [diff] [blame] | 168 | for (i = j = 1; i < cmds->cnt; i++) { |
| 169 | if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name)) |
| 170 | free(cmds->names[i]); |
| 171 | else |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 172 | cmds->names[j++] = cmds->names[i]; |
Jeff King | 4a15758 | 2012-07-26 00:16:19 +0800 | [diff] [blame] | 173 | } |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 174 | |
| 175 | cmds->cnt = j; |
| 176 | } |
| 177 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 178 | void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 179 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 180 | int ci, cj, ei; |
| 181 | int cmp; |
| 182 | |
| 183 | ci = cj = ei = 0; |
| 184 | while (ci < cmds->cnt && ei < excludes->cnt) { |
| 185 | cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); |
| 186 | if (cmp < 0) |
| 187 | cmds->names[cj++] = cmds->names[ci++]; |
Junio C Hamano | 6a17f58 | 2012-07-25 11:01:12 -0700 | [diff] [blame] | 188 | else if (cmp == 0) { |
| 189 | ei++; |
| 190 | free(cmds->names[ci++]); |
| 191 | } else if (cmp > 0) |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 192 | ei++; |
| 193 | } |
| 194 | |
| 195 | while (ci < cmds->cnt) |
| 196 | cmds->names[cj++] = cmds->names[ci++]; |
| 197 | |
| 198 | cmds->cnt = cj; |
| 199 | } |
| 200 | |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 201 | static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 202 | { |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 203 | struct string_list list = STRING_LIST_INIT_NODUP; |
| 204 | struct column_options copts; |
| 205 | int i; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 206 | |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 207 | for (i = 0; i < cmds->cnt; i++) |
| 208 | string_list_append(&list, cmds->names[i]->name); |
| 209 | /* |
| 210 | * always enable column display, we only consult column.* |
| 211 | * about layout strategy and stuff |
| 212 | */ |
| 213 | colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED; |
| 214 | memset(&copts, 0, sizeof(copts)); |
| 215 | copts.indent = " "; |
| 216 | copts.padding = 2; |
| 217 | print_columns(&list, colopts, &copts); |
| 218 | string_list_clear(&list, 0); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 221 | static void list_commands_in_dir(struct cmdnames *cmds, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 222 | const char *path, |
| 223 | const char *prefix) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 224 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 225 | DIR *dir = opendir(path); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 226 | struct dirent *de; |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 227 | struct strbuf buf = STRBUF_INIT; |
| 228 | int len; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 229 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 230 | if (!dir) |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 231 | return; |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 232 | if (!prefix) |
| 233 | prefix = "git-"; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 234 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 235 | strbuf_addf(&buf, "%s/", path); |
| 236 | len = buf.len; |
| 237 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 238 | while ((de = readdir(dir)) != NULL) { |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 239 | const char *ent; |
Jeff King | 26936bf | 2014-06-30 12:58:51 -0400 | [diff] [blame] | 240 | size_t entlen; |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 241 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 242 | if (!skip_prefix(de->d_name, prefix, &ent)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 243 | continue; |
Scott R Parish | 0966003 | 2007-10-27 01:36:52 -0700 | [diff] [blame] | 244 | |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 245 | strbuf_setlen(&buf, len); |
| 246 | strbuf_addstr(&buf, de->d_name); |
| 247 | if (!is_executable(buf.buf)) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 248 | continue; |
| 249 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 250 | entlen = strlen(ent); |
Junio C Hamano | 6e40947 | 2014-07-16 11:25:59 -0700 | [diff] [blame] | 251 | strip_suffix(ent, ".exe", &entlen); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 252 | |
Jeff King | de8118e | 2014-06-18 15:57:17 -0400 | [diff] [blame] | 253 | add_cmdname(cmds, ent, entlen); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 254 | } |
| 255 | closedir(dir); |
Johannes Schindelin | f5d600e | 2008-07-27 22:34:14 +0200 | [diff] [blame] | 256 | strbuf_release(&buf); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 259 | void load_command_list(const char *prefix, |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 260 | struct cmdnames *main_cmds, |
| 261 | struct cmdnames *other_cmds) |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 262 | { |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 263 | const char *env_path = getenv("PATH"); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 264 | const char *exec_path = git_exec_path(); |
| 265 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 266 | if (exec_path) { |
Alex Riesen | e321180 | 2008-08-28 19:15:33 +0200 | [diff] [blame] | 267 | list_commands_in_dir(main_cmds, exec_path, prefix); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 268 | QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare); |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 269 | uniq(main_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 272 | if (env_path) { |
| 273 | char *paths, *path, *colon; |
| 274 | path = paths = xstrdup(env_path); |
| 275 | while (1) { |
| 276 | if ((colon = strchr(path, PATH_SEP))) |
| 277 | *colon = 0; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 278 | if (!exec_path || strcmp(path, exec_path)) |
| 279 | list_commands_in_dir(other_cmds, path, prefix); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 280 | |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 281 | if (!colon) |
| 282 | break; |
| 283 | path = colon + 1; |
| 284 | } |
| 285 | free(paths); |
| 286 | |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 287 | QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare); |
Alex Riesen | 1f08e5c | 2008-08-28 19:19:07 +0200 | [diff] [blame] | 288 | uniq(other_cmds); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 289 | } |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 290 | exclude_cmds(other_cmds, main_cmds); |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Junio C Hamano | f4ed0af | 2012-05-03 15:13:31 -0700 | [diff] [blame] | 293 | void list_commands(unsigned int colopts, |
Nguyễn Thái Ngọc Duy | dbfae68 | 2012-04-13 17:54:37 +0700 | [diff] [blame] | 294 | struct cmdnames *main_cmds, struct cmdnames *other_cmds) |
Jeff King | 2156435 | 2008-02-24 17:17:37 -0500 | [diff] [blame] | 295 | { |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 296 | if (main_cmds->cnt) { |
Alex Riesen | 61c5d43 | 2008-08-28 19:19:42 +0200 | [diff] [blame] | 297 | const char *exec_path = git_exec_path(); |
Nguyễn Thái Ngọc Duy | 4470ef9 | 2012-04-25 18:21:53 +0700 | [diff] [blame] | 298 | printf_ln(_("available git commands in '%s'"), exec_path); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 299 | putchar('\n'); |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 300 | pretty_print_cmdnames(main_cmds, colopts); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 301 | putchar('\n'); |
| 302 | } |
| 303 | |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 304 | if (other_cmds->cnt) { |
Nguyễn Thái Ngọc Duy | 4470ef9 | 2012-04-25 18:21:53 +0700 | [diff] [blame] | 305 | printf_ln(_("git commands available from elsewhere on your $PATH")); |
Miklos Vajna | 940208a | 2008-07-30 01:16:58 +0200 | [diff] [blame] | 306 | putchar('\n'); |
Ralf Thielow | d10cb3d | 2014-02-28 20:27:29 +0100 | [diff] [blame] | 307 | pretty_print_cmdnames(other_cmds, colopts); |
Scott R Parish | 1eb0569 | 2007-10-28 20:30:52 -0700 | [diff] [blame] | 308 | putchar('\n'); |
| 309 | } |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 312 | void list_common_cmds_help(void) |
| 313 | { |
Sébastien Guimmara | 2241477 | 2015-05-21 19:39:22 +0200 | [diff] [blame] | 314 | puts(_("These are common Git commands used in various situations:")); |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 315 | print_cmd_by_category(common_categories, NULL); |
Junio C Hamano | 1542d4c | 2013-01-18 22:35:04 -0800 | [diff] [blame] | 316 | } |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 317 | |
Nguyễn Thái Ngọc Duy | 6bb2dc0 | 2018-05-20 20:39:59 +0200 | [diff] [blame] | 318 | void list_all_main_cmds(struct string_list *list) |
| 319 | { |
| 320 | struct cmdnames main_cmds, other_cmds; |
| 321 | int i; |
| 322 | |
| 323 | memset(&main_cmds, 0, sizeof(main_cmds)); |
| 324 | memset(&other_cmds, 0, sizeof(other_cmds)); |
| 325 | load_command_list("git-", &main_cmds, &other_cmds); |
| 326 | |
| 327 | for (i = 0; i < main_cmds.cnt; i++) |
| 328 | string_list_append(list, main_cmds.names[i]->name); |
| 329 | |
| 330 | clean_cmdnames(&main_cmds); |
| 331 | clean_cmdnames(&other_cmds); |
| 332 | } |
| 333 | |
| 334 | void list_all_other_cmds(struct string_list *list) |
| 335 | { |
| 336 | struct cmdnames main_cmds, other_cmds; |
| 337 | int i; |
| 338 | |
| 339 | memset(&main_cmds, 0, sizeof(main_cmds)); |
| 340 | memset(&other_cmds, 0, sizeof(other_cmds)); |
| 341 | load_command_list("git-", &main_cmds, &other_cmds); |
| 342 | |
| 343 | for (i = 0; i < other_cmds.cnt; i++) |
| 344 | string_list_append(list, other_cmds.names[i]->name); |
| 345 | |
| 346 | clean_cmdnames(&main_cmds); |
| 347 | clean_cmdnames(&other_cmds); |
| 348 | } |
| 349 | |
Nguyễn Thái Ngọc Duy | 3c77776 | 2018-05-20 20:40:00 +0200 | [diff] [blame] | 350 | void list_cmds_by_category(struct string_list *list, |
| 351 | const char *cat) |
| 352 | { |
| 353 | int i, n = ARRAY_SIZE(command_list); |
| 354 | uint32_t cat_id = 0; |
| 355 | |
| 356 | for (i = 0; category_names[i]; i++) { |
| 357 | if (!strcmp(cat, category_names[i])) { |
| 358 | cat_id = 1UL << i; |
| 359 | break; |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 360 | } |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 361 | } |
Nguyễn Thái Ngọc Duy | 3c77776 | 2018-05-20 20:40:00 +0200 | [diff] [blame] | 362 | if (!cat_id) |
| 363 | die(_("unsupported command listing type '%s'"), cat); |
| 364 | |
| 365 | for (i = 0; i < n; i++) { |
| 366 | struct cmdname_help *cmd = command_list + i; |
| 367 | |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 368 | if (!(cmd->category & cat_id)) |
| 369 | continue; |
| 370 | string_list_append(list, drop_prefix(cmd->name, cmd->category)); |
Nguyễn Thái Ngọc Duy | 3c77776 | 2018-05-20 20:40:00 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Nguyễn Thái Ngọc Duy | 6532f37 | 2018-05-20 20:40:09 +0200 | [diff] [blame] | 374 | void list_cmds_by_config(struct string_list *list) |
| 375 | { |
| 376 | const char *cmd_list; |
| 377 | |
| 378 | /* |
| 379 | * There's no actual repository setup at this point (and even |
| 380 | * if there is, we don't really care; only global config |
| 381 | * matters). If we accidentally set up a repository, it's ok |
| 382 | * too since the caller (git --list-cmds=) should exit shortly |
| 383 | * anyway. |
| 384 | */ |
| 385 | if (git_config_get_string_const("completion.commands", &cmd_list)) |
| 386 | return; |
| 387 | |
| 388 | string_list_sort(list); |
| 389 | string_list_remove_duplicates(list, 0); |
| 390 | |
| 391 | while (*cmd_list) { |
| 392 | struct strbuf sb = STRBUF_INIT; |
| 393 | const char *p = strchrnul(cmd_list, ' '); |
| 394 | |
| 395 | strbuf_add(&sb, cmd_list, p - cmd_list); |
| 396 | if (*cmd_list == '-') |
| 397 | string_list_remove(list, cmd_list + 1, 0); |
| 398 | else |
| 399 | string_list_insert(list, sb.buf); |
| 400 | strbuf_release(&sb); |
| 401 | while (*p == ' ') |
| 402 | p++; |
| 403 | cmd_list = p; |
| 404 | } |
| 405 | } |
| 406 | |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 407 | void list_common_guides_help(void) |
| 408 | { |
| 409 | struct category_description catdesc[] = { |
| 410 | { CAT_guide, N_("The common Git guides are:") }, |
| 411 | { 0, NULL } |
| 412 | }; |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 413 | print_cmd_by_category(catdesc, NULL); |
Nguyễn Thái Ngọc Duy | 1b81d8c | 2018-05-20 20:40:02 +0200 | [diff] [blame] | 414 | putchar('\n'); |
| 415 | } |
| 416 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 417 | struct slot_expansion { |
| 418 | const char *prefix; |
| 419 | const char *placeholder; |
| 420 | void (*fn)(struct string_list *list, const char *prefix); |
| 421 | int found; |
| 422 | }; |
| 423 | |
Nguyễn Thái Ngọc Duy | e17ca92 | 2018-05-26 15:55:28 +0200 | [diff] [blame] | 424 | void list_config_help(int for_human) |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 425 | { |
| 426 | struct slot_expansion slot_expansions[] = { |
| 427 | { "advice", "*", list_config_advices }, |
| 428 | { "color.branch", "<slot>", list_config_color_branch_slots }, |
| 429 | { "color.decorate", "<slot>", list_config_color_decorate_slots }, |
| 430 | { "color.diff", "<slot>", list_config_color_diff_slots }, |
| 431 | { "color.grep", "<slot>", list_config_color_grep_slots }, |
| 432 | { "color.interactive", "<slot>", list_config_color_interactive_slots }, |
Han-Wen Nienhuys | bf1a11f | 2018-08-07 14:51:08 +0200 | [diff] [blame] | 433 | { "color.remote", "<slot>", list_config_color_sideband_slots }, |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 434 | { "color.status", "<slot>", list_config_color_status_slots }, |
| 435 | { "fsck", "<msg-id>", list_config_fsck_msg_ids }, |
| 436 | { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids }, |
| 437 | { NULL, NULL, NULL } |
| 438 | }; |
| 439 | const char **p; |
| 440 | struct slot_expansion *e; |
| 441 | struct string_list keys = STRING_LIST_INIT_DUP; |
| 442 | int i; |
| 443 | |
| 444 | for (p = config_name_list; *p; p++) { |
| 445 | const char *var = *p; |
| 446 | struct strbuf sb = STRBUF_INIT; |
| 447 | |
| 448 | for (e = slot_expansions; e->prefix; e++) { |
| 449 | |
| 450 | strbuf_reset(&sb); |
| 451 | strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder); |
| 452 | if (!strcasecmp(var, sb.buf)) { |
| 453 | e->fn(&keys, e->prefix); |
| 454 | e->found++; |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | strbuf_release(&sb); |
| 459 | if (!e->prefix) |
| 460 | string_list_append(&keys, var); |
| 461 | } |
| 462 | |
| 463 | for (e = slot_expansions; e->prefix; e++) |
| 464 | if (!e->found) |
| 465 | BUG("slot_expansion %s.%s is not used", |
| 466 | e->prefix, e->placeholder); |
| 467 | |
| 468 | string_list_sort(&keys); |
Nguyễn Thái Ngọc Duy | e17ca92 | 2018-05-26 15:55:28 +0200 | [diff] [blame] | 469 | for (i = 0; i < keys.nr; i++) { |
| 470 | const char *var = keys.items[i].string; |
| 471 | const char *wildcard, *tag, *cut; |
| 472 | |
| 473 | if (for_human) { |
| 474 | puts(var); |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | wildcard = strchr(var, '*'); |
| 479 | tag = strchr(var, '<'); |
| 480 | |
| 481 | if (!wildcard && !tag) { |
| 482 | puts(var); |
| 483 | continue; |
| 484 | } |
| 485 | |
| 486 | if (wildcard && !tag) |
| 487 | cut = wildcard; |
| 488 | else if (!wildcard && tag) |
| 489 | cut = tag; |
| 490 | else |
| 491 | cut = wildcard < tag ? wildcard : tag; |
| 492 | |
| 493 | /* |
| 494 | * We may produce duplicates, but that's up to |
| 495 | * git-completion.bash to handle |
| 496 | */ |
| 497 | printf("%.*s\n", (int)(cut - var), var); |
| 498 | } |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 499 | string_list_clear(&keys, 0); |
| 500 | } |
| 501 | |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 502 | static int get_alias(const char *var, const char *value, void *data) |
| 503 | { |
| 504 | struct string_list *list = data; |
| 505 | |
| 506 | if (skip_prefix(var, "alias.", &var)) |
| 507 | string_list_append(list, var)->util = xstrdup(value); |
| 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Nguyễn Thái Ngọc Duy | 63eae83 | 2018-05-20 20:40:01 +0200 | [diff] [blame] | 512 | void list_all_cmds_help(void) |
| 513 | { |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 514 | struct string_list others = STRING_LIST_INIT_DUP; |
| 515 | struct string_list alias_list = STRING_LIST_INIT_DUP; |
| 516 | struct cmdname_help *aliases; |
| 517 | int i, longest; |
| 518 | |
| 519 | printf_ln(_("See 'git help <command>' to read about a specific subcommand")); |
| 520 | print_cmd_by_category(main_categories, &longest); |
| 521 | |
| 522 | list_all_other_cmds(&others); |
| 523 | if (others.nr) |
| 524 | printf("\n%s\n", _("External commands")); |
| 525 | for (i = 0; i < others.nr; i++) |
| 526 | printf(" %s\n", others.items[i].string); |
| 527 | string_list_clear(&others, 0); |
| 528 | |
| 529 | git_config(get_alias, &alias_list); |
| 530 | string_list_sort(&alias_list); |
Johannes Schindelin | 1c4b985 | 2018-12-11 06:58:11 -0800 | [diff] [blame] | 531 | |
| 532 | for (i = 0; i < alias_list.nr; i++) { |
| 533 | size_t len = strlen(alias_list.items[i].string); |
| 534 | if (longest < len) |
| 535 | longest = len; |
| 536 | } |
| 537 | |
Nguyễn Thái Ngọc Duy | 26c7d06 | 2018-09-29 08:08:14 +0200 | [diff] [blame] | 538 | if (alias_list.nr) { |
| 539 | printf("\n%s\n", _("Command aliases")); |
| 540 | ALLOC_ARRAY(aliases, alias_list.nr + 1); |
| 541 | for (i = 0; i < alias_list.nr; i++) { |
| 542 | aliases[i].name = alias_list.items[i].string; |
| 543 | aliases[i].help = alias_list.items[i].util; |
| 544 | aliases[i].category = 1; |
| 545 | } |
| 546 | aliases[alias_list.nr].name = NULL; |
| 547 | print_command_list(aliases, 1, longest); |
| 548 | free(aliases); |
| 549 | } |
| 550 | string_list_clear(&alias_list, 1); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 551 | } |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 552 | |
| 553 | int is_in_cmdlist(struct cmdnames *c, const char *s) |
| 554 | { |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 555 | int i; |
| 556 | for (i = 0; i < c->cnt; i++) |
| 557 | if (!strcmp(s, c->names[i]->name)) |
| 558 | return 1; |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 559 | return 0; |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 560 | } |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 561 | |
| 562 | static int autocorrect; |
| 563 | static struct cmdnames aliases; |
| 564 | |
| 565 | static int git_unknown_cmd_config(const char *var, const char *value, void *cb) |
| 566 | { |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 567 | const char *p; |
| 568 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 569 | if (!strcmp(var, "help.autocorrect")) |
Ramsay Allan Jones | 822a7d5 | 2006-07-30 22:42:25 +0100 | [diff] [blame] | 570 | autocorrect = git_config_int(var,value); |
| 571 | /* Also use aliases for command lookup */ |
Jeff King | ae021d8 | 2014-06-18 15:47:50 -0400 | [diff] [blame] | 572 | if (skip_prefix(var, "alias.", &p)) |
| 573 | add_cmdname(&aliases, p, strlen(p)); |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 574 | |
| 575 | return git_default_config(var, value, cb); |
| 576 | } |
| 577 | |
Christian Couder | 5d6491c | 2007-12-02 06:07:55 +0100 | [diff] [blame] | 578 | static int levenshtein_compare(const void *p1, const void *p2) |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 579 | { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 580 | const struct cmdname *const *c1 = p1, *const *c2 = p2; |
| 581 | const char *s1 = (*c1)->name, *s2 = (*c2)->name; |
| 582 | int l1 = (*c1)->len; |
| 583 | int l2 = (*c2)->len; |
| 584 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); |
| 585 | } |
| 586 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 587 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
| 588 | { |
| 589 | int i; |
| 590 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); |
| 591 | |
| 592 | for (i = 0; i < old->cnt; i++) |
| 593 | cmds->names[cmds->cnt++] = old->names[i]; |
Ævar Arnfjörð Bjarmason | 88ce3ef | 2017-06-15 23:15:49 +0000 | [diff] [blame] | 594 | FREE_AND_NULL(old->names); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 595 | old->cnt = 0; |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 596 | } |
| 597 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 598 | /* An empirically derived magic number */ |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 599 | #define SIMILARITY_FLOOR 7 |
| 600 | #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR) |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 601 | |
Michael Schubert | 823e0de | 2011-07-08 12:08:49 +0200 | [diff] [blame] | 602 | static const char bad_interpreter_advice[] = |
| 603 | N_("'%s' appears to be a git command, but we were not\n" |
| 604 | "able to execute it. Maybe git-%s is broken?"); |
| 605 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 606 | const char *help_unknown_cmd(const char *cmd) |
| 607 | { |
| 608 | int i, n, best_similarity = 0; |
| 609 | struct cmdnames main_cmds, other_cmds; |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 610 | struct cmdname_help *common_cmds; |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 611 | |
| 612 | memset(&main_cmds, 0, sizeof(main_cmds)); |
Johan Herland | 0b74f5d | 2009-08-11 12:10:21 +0200 | [diff] [blame] | 613 | memset(&other_cmds, 0, sizeof(other_cmds)); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 614 | memset(&aliases, 0, sizeof(aliases)); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 615 | |
Johannes Schindelin | 659fef1 | 2017-06-14 13:35:50 +0200 | [diff] [blame] | 616 | read_early_config(git_unknown_cmd_config, NULL); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 617 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 618 | load_command_list("git-", &main_cmds, &other_cmds); |
| 619 | |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 620 | add_cmd_list(&main_cmds, &aliases); |
| 621 | add_cmd_list(&main_cmds, &other_cmds); |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 622 | QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare); |
Pieter de Bie | 746c221 | 2008-09-10 17:54:28 +0200 | [diff] [blame] | 623 | uniq(&main_cmds); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 624 | |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 625 | extract_cmds(&common_cmds, common_mask); |
| 626 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 627 | /* This abuses cmdname->len for levenshtein distance */ |
| 628 | for (i = 0, n = 0; i < main_cmds.cnt; i++) { |
| 629 | int cmp = 0; /* avoid compiler stupidity */ |
| 630 | const char *candidate = main_cmds.names[i]->name; |
| 631 | |
Michael Schubert | 823e0de | 2011-07-08 12:08:49 +0200 | [diff] [blame] | 632 | /* |
| 633 | * An exact match means we have the command, but |
| 634 | * for some reason exec'ing it gave us ENOENT; probably |
| 635 | * it's a bad interpreter in the #! line. |
| 636 | */ |
| 637 | if (!strcmp(candidate, cmd)) |
| 638 | die(_(bad_interpreter_advice), cmd, cmd); |
| 639 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 640 | /* Does the candidate appear in common_cmds list? */ |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 641 | while (common_cmds[n].name && |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 642 | (cmp = strcmp(common_cmds[n].name, candidate)) < 0) |
| 643 | n++; |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 644 | if (common_cmds[n].name && !cmp) { |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 645 | /* Yes, this is one of the common commands */ |
| 646 | n++; /* use the entry from common_cmds[] */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 647 | if (starts_with(candidate, cmd)) { |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 648 | /* Give prefix match a very good score */ |
| 649 | main_cmds.names[i]->len = 0; |
| 650 | continue; |
| 651 | } |
| 652 | } |
| 653 | |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 654 | main_cmds.names[i]->len = |
Matthieu Moy | c41494f | 2012-05-27 18:02:58 +0200 | [diff] [blame] | 655 | levenshtein(cmd, candidate, 0, 2, 1, 3) + 1; |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 656 | } |
Nguyễn Thái Ngọc Duy | cfb22a0 | 2018-05-10 10:46:42 +0200 | [diff] [blame] | 657 | FREE_AND_NULL(common_cmds); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 658 | |
René Scharfe | 9ed0d8d | 2016-09-29 17:27:31 +0200 | [diff] [blame] | 659 | QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 660 | |
| 661 | if (!main_cmds.cnt) |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 662 | die(_("Uh oh. Your system reports no Git commands at all.")); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 663 | |
Erik Faye-Lund | 6612b9e | 2010-11-26 17:00:39 +0100 | [diff] [blame] | 664 | /* skip and count prefix matches */ |
| 665 | for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++) |
| 666 | ; /* still counting */ |
| 667 | |
| 668 | if (main_cmds.cnt <= n) { |
| 669 | /* prefix matches with everything? that is too ambiguous */ |
| 670 | best_similarity = SIMILARITY_FLOOR + 1; |
| 671 | } else { |
| 672 | /* count all the most similar ones */ |
| 673 | for (best_similarity = main_cmds.names[n++]->len; |
| 674 | (n < main_cmds.cnt && |
| 675 | best_similarity == main_cmds.names[n]->len); |
| 676 | n++) |
| 677 | ; /* still counting */ |
| 678 | } |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 679 | if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 680 | const char *assumed = main_cmds.names[0]->name; |
| 681 | main_cmds.names[0] = NULL; |
| 682 | clean_cmdnames(&main_cmds); |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 683 | fprintf_ln(stderr, |
| 684 | _("WARNING: You called a Git command named '%s', " |
Marc Branchaud | 968b1fe | 2017-06-21 09:57:38 -0400 | [diff] [blame] | 685 | "which does not exist."), |
| 686 | cmd); |
| 687 | if (autocorrect < 0) |
| 688 | fprintf_ln(stderr, |
| 689 | _("Continuing under the assumption that " |
| 690 | "you meant '%s'."), |
| 691 | assumed); |
| 692 | else { |
| 693 | fprintf_ln(stderr, |
| 694 | _("Continuing in %0.1f seconds, " |
| 695 | "assuming that you meant '%s'."), |
| 696 | (float)autocorrect/10.0, assumed); |
Johannes Sixt | 2024d31 | 2015-06-05 21:45:05 +0200 | [diff] [blame] | 697 | sleep_millisec(autocorrect * 100); |
Alex Riesen | f0e9071 | 2008-08-31 15:54:58 +0200 | [diff] [blame] | 698 | } |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 699 | return assumed; |
| 700 | } |
| 701 | |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 702 | 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] | 703 | |
Johannes Sixt | 06500a0 | 2009-12-15 08:57:18 +0100 | [diff] [blame] | 704 | if (SIMILAR_ENOUGH(best_similarity)) { |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 705 | fprintf_ln(stderr, |
Jean-Noel Avila | 6c48686 | 2017-05-11 14:06:32 +0200 | [diff] [blame] | 706 | Q_("\nThe most similar command is", |
| 707 | "\nThe most similar commands are", |
Nguyễn Thái Ngọc Duy | 9665627 | 2012-04-23 19:30:24 +0700 | [diff] [blame] | 708 | n)); |
Johannes Schindelin | 8af84da | 2008-08-31 15:50:23 +0200 | [diff] [blame] | 709 | |
| 710 | for (i = 0; i < n; i++) |
| 711 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); |
| 712 | } |
| 713 | |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 714 | exit(1); |
| 715 | } |
| 716 | |
| 717 | int cmd_version(int argc, const char **argv, const char *prefix) |
| 718 | { |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 719 | int build_options = 0; |
| 720 | const char * const usage[] = { |
| 721 | N_("git version [<options>]"), |
| 722 | NULL |
| 723 | }; |
| 724 | struct option options[] = { |
| 725 | OPT_BOOL(0, "build-options", &build_options, |
| 726 | "also print build options"), |
| 727 | OPT_END() |
| 728 | }; |
| 729 | |
| 730 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 731 | |
David Aguilar | f2de0b9 | 2013-04-16 13:33:25 -0700 | [diff] [blame] | 732 | /* |
| 733 | * The format of this string should be kept stable for compatibility |
| 734 | * with external projects that rely on the output of "git version". |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 735 | * |
| 736 | * Always show the version, even if other options are given. |
David Aguilar | f2de0b9 | 2013-04-16 13:33:25 -0700 | [diff] [blame] | 737 | */ |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 738 | printf("git version %s\n", git_version_string); |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 739 | |
| 740 | if (build_options) { |
Eric Sunshine | b228940 | 2017-12-15 00:34:34 +0100 | [diff] [blame] | 741 | printf("cpu: %s\n", GIT_HOST_CPU); |
Johannes Schindelin | ed32b78 | 2017-12-15 00:34:38 +0100 | [diff] [blame] | 742 | if (git_built_from_commit_string[0]) |
| 743 | printf("built from commit: %s\n", |
| 744 | git_built_from_commit_string); |
| 745 | else |
| 746 | printf("no commit associated with this build\n"); |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 747 | printf("sizeof-long: %d\n", (int)sizeof(long)); |
Max Kirillov | 6c213e8 | 2018-07-27 06:48:59 +0300 | [diff] [blame] | 748 | printf("sizeof-size_t: %d\n", (int)sizeof(size_t)); |
Jeff King | b48cbfc | 2017-05-30 01:17:42 -0400 | [diff] [blame] | 749 | /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */ |
Jeff King | 6b9c38e | 2016-07-11 19:54:18 -0400 | [diff] [blame] | 750 | } |
Linus Torvalds | 70827b1 | 2006-04-21 10:27:34 -0700 | [diff] [blame] | 751 | return 0; |
| 752 | } |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 753 | |
| 754 | struct similar_ref_cb { |
| 755 | const char *base_ref; |
| 756 | struct string_list *similar_refs; |
| 757 | }; |
| 758 | |
Michael Haggerty | 91d6e94 | 2015-05-25 18:38:54 +0000 | [diff] [blame] | 759 | 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] | 760 | int flags, void *cb_data) |
| 761 | { |
| 762 | struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); |
| 763 | char *branch = strrchr(refname, '/') + 1; |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 764 | const char *remote; |
| 765 | |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 766 | /* A remote branch of the same name is deemed similar */ |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 767 | if (skip_prefix(refname, "refs/remotes/", &remote) && |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 768 | !strcmp(branch, cb->base_ref)) |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 769 | string_list_append(cb->similar_refs, remote); |
Vikrant Varma | e561810 | 2013-05-04 05:34:19 +0530 | [diff] [blame] | 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | static struct string_list guess_refs(const char *ref) |
| 774 | { |
| 775 | struct similar_ref_cb ref_cb; |
| 776 | struct string_list similar_refs = STRING_LIST_INIT_NODUP; |
| 777 | |
| 778 | ref_cb.base_ref = ref; |
| 779 | ref_cb.similar_refs = &similar_refs; |
| 780 | for_each_ref(append_similar_ref, &ref_cb); |
| 781 | return similar_refs; |
| 782 | } |
| 783 | |
| 784 | void help_unknown_ref(const char *ref, const char *cmd, const char *error) |
| 785 | { |
| 786 | int i; |
| 787 | struct string_list suggested_refs = guess_refs(ref); |
| 788 | |
| 789 | fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error); |
| 790 | |
| 791 | if (suggested_refs.nr > 0) { |
| 792 | fprintf_ln(stderr, |
| 793 | Q_("\nDid you mean this?", |
| 794 | "\nDid you mean one of these?", |
| 795 | suggested_refs.nr)); |
| 796 | for (i = 0; i < suggested_refs.nr; i++) |
| 797 | fprintf(stderr, "\t%s\n", suggested_refs.items[i].string); |
| 798 | } |
| 799 | |
| 800 | string_list_clear(&suggested_refs, 0); |
| 801 | exit(1); |
| 802 | } |