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