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