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