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