]>
Commit | Line | Data |
---|---|---|
36bf1958 | 1 | #include "git-compat-util.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" |
a64215b6 | 7 | #include "gettext.h" |
940208a7 | 8 | #include "help.h" |
cfb22a02 | 9 | #include "command-list.h" |
dbfae689 NTND |
10 | #include "string-list.h" |
11 | #include "column.h" | |
816fb46b | 12 | #include "version.h" |
e5618106 | 13 | #include "refs.h" |
b48cbfc5 | 14 | #include "parse-options.h" |
dc66e3c7 | 15 | #include "prompt.h" |
dd77cf61 | 16 | #include "fsmonitor-ipc.h" |
64949984 | 17 | |
cfb22a02 NTND |
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 | }; | |
63eae83f NTND |
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") }, | |
96c0caf5 | 40 | { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") }, |
63eae83f | 41 | { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") }, |
d976c510 | 42 | { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") }, |
225e815e | 43 | { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") }, |
63eae83f NTND |
44 | { 0, NULL } |
45 | }; | |
cfb22a02 | 46 | |
1b81d8cb | 47 | static const char *drop_prefix(const char *name, uint32_t category) |
cfb22a02 NTND |
48 | { |
49 | const char *new_name; | |
936b8eb6 | 50 | const char *prefix; |
cfb22a02 | 51 | |
2f8b3ea6 ÆAB |
52 | switch (category) { |
53 | case CAT_guide: | |
d976c510 | 54 | case CAT_userinterfaces: |
844739ba | 55 | case CAT_developerinterfaces: |
2f8b3ea6 ÆAB |
56 | prefix = "git"; |
57 | break; | |
936b8eb6 ÆAB |
58 | default: |
59 | prefix = "git-"; | |
60 | break; | |
2f8b3ea6 | 61 | } |
936b8eb6 | 62 | if (skip_prefix(name, prefix, &new_name)) |
1b81d8cb | 63 | return new_name; |
cfb22a02 | 64 | |
2f8b3ea6 | 65 | return name; |
cfb22a02 NTND |
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; | |
1b81d8cb | 85 | cmds[nr].name = drop_prefix(cmd->name, cmd->category); |
cfb22a02 NTND |
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) { | |
1c4b9859 | 100 | size_t len = strlen(cmds[i].name); |
cfb22a02 | 101 | printf(" %s ", cmds[i].name); |
6195a76d NTND |
102 | if (longest > len) |
103 | mput_char(' ', longest - len); | |
cfb22a02 NTND |
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 | ||
26c7d067 NTND |
117 | static void print_cmd_by_category(const struct category_description *catdesc, |
118 | int *longest_p) | |
cfb22a02 NTND |
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 | ||
93de1b69 ÆAB |
140 | if (i) |
141 | putchar('\n'); | |
cd87ce7d | 142 | puts(_(desc)); |
cfb22a02 NTND |
143 | print_command_list(cmds, mask, longest); |
144 | } | |
145 | free(cmds); | |
26c7d067 NTND |
146 | if (longest_p) |
147 | *longest_p = longest; | |
cfb22a02 NTND |
148 | } |
149 | ||
940208a7 | 150 | void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
70827b15 | 151 | { |
96ffc06f JK |
152 | struct cmdname *ent; |
153 | FLEX_ALLOC_MEM(ent, name, name, len); | |
70827b15 | 154 | ent->len = len; |
1eb05690 SP |
155 | |
156 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); | |
157 | cmds->names[cmds->cnt++] = ent; | |
70827b15 LT |
158 | } |
159 | ||
8af84dad JS |
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 | ||
70827b15 LT |
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 | ||
1eb05690 SP |
177 | static void uniq(struct cmdnames *cmds) |
178 | { | |
179 | int i, j; | |
180 | ||
181 | if (!cmds->cnt) | |
182 | return; | |
183 | ||
4a15758f JK |
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 | |
1eb05690 | 188 | cmds->names[j++] = cmds->names[i]; |
4a15758f | 189 | } |
1eb05690 SP |
190 | |
191 | cmds->cnt = j; | |
192 | } | |
193 | ||
940208a7 | 194 | void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
f3fa1838 | 195 | { |
1eb05690 SP |
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++]; | |
6a17f583 JH |
204 | else if (cmp == 0) { |
205 | ei++; | |
206 | free(cmds->names[ci++]); | |
207 | } else if (cmp > 0) | |
1eb05690 SP |
208 | ei++; |
209 | } | |
210 | ||
211 | while (ci < cmds->cnt) | |
212 | cmds->names[cj++] = cmds->names[ci++]; | |
213 | ||
214 | cmds->cnt = cj; | |
215 | } | |
216 | ||
d10cb3df | 217 | static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts) |
70827b15 | 218 | { |
dbfae689 NTND |
219 | struct string_list list = STRING_LIST_INIT_NODUP; |
220 | struct column_options copts; | |
221 | int i; | |
70827b15 | 222 | |
dbfae689 NTND |
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); | |
70827b15 LT |
235 | } |
236 | ||
e321180e | 237 | static void list_commands_in_dir(struct cmdnames *cmds, |
940208a7 MV |
238 | const char *path, |
239 | const char *prefix) | |
70827b15 | 240 | { |
1eb05690 | 241 | DIR *dir = opendir(path); |
70827b15 | 242 | struct dirent *de; |
f5d600e2 JS |
243 | struct strbuf buf = STRBUF_INIT; |
244 | int len; | |
70827b15 | 245 | |
f5d600e2 | 246 | if (!dir) |
e321180e | 247 | return; |
940208a7 MV |
248 | if (!prefix) |
249 | prefix = "git-"; | |
70827b15 | 250 | |
f5d600e2 JS |
251 | strbuf_addf(&buf, "%s/", path); |
252 | len = buf.len; | |
253 | ||
70827b15 | 254 | while ((de = readdir(dir)) != NULL) { |
de8118e1 | 255 | const char *ent; |
26936bfd | 256 | size_t entlen; |
70827b15 | 257 | |
de8118e1 | 258 | if (!skip_prefix(de->d_name, prefix, &ent)) |
70827b15 | 259 | continue; |
0966003c | 260 | |
f5d600e2 JS |
261 | strbuf_setlen(&buf, len); |
262 | strbuf_addstr(&buf, de->d_name); | |
263 | if (!is_executable(buf.buf)) | |
70827b15 LT |
264 | continue; |
265 | ||
de8118e1 | 266 | entlen = strlen(ent); |
6e409473 | 267 | strip_suffix(ent, ".exe", &entlen); |
70827b15 | 268 | |
de8118e1 | 269 | add_cmdname(cmds, ent, entlen); |
70827b15 LT |
270 | } |
271 | closedir(dir); | |
f5d600e2 | 272 | strbuf_release(&buf); |
1eb05690 SP |
273 | } |
274 | ||
e321180e | 275 | void load_command_list(const char *prefix, |
940208a7 MV |
276 | struct cmdnames *main_cmds, |
277 | struct cmdnames *other_cmds) | |
1eb05690 | 278 | { |
1eb05690 | 279 | const char *env_path = getenv("PATH"); |
1eb05690 SP |
280 | const char *exec_path = git_exec_path(); |
281 | ||
722fc374 JS |
282 | load_builtin_commands(prefix, main_cmds); |
283 | ||
1f08e5ce | 284 | if (exec_path) { |
e321180e | 285 | list_commands_in_dir(main_cmds, exec_path, prefix); |
9ed0d8d6 | 286 | QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare); |
1f08e5ce | 287 | uniq(main_cmds); |
1eb05690 SP |
288 | } |
289 | ||
1f08e5ce AR |
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; | |
746c221a PB |
296 | if (!exec_path || strcmp(path, exec_path)) |
297 | list_commands_in_dir(other_cmds, path, prefix); | |
1eb05690 | 298 | |
1f08e5ce AR |
299 | if (!colon) |
300 | break; | |
301 | path = colon + 1; | |
302 | } | |
303 | free(paths); | |
1eb05690 | 304 | |
9ed0d8d6 | 305 | QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare); |
1f08e5ce AR |
306 | uniq(other_cmds); |
307 | } | |
940208a7 | 308 | exclude_cmds(other_cmds, main_cmds); |
2156435f JK |
309 | } |
310 | ||
a4e7e317 GC |
311 | static int get_colopts(const char *var, const char *value, |
312 | const struct config_context *ctx UNUSED, void *data) | |
2156435f | 313 | { |
06fa4db3 ÆAB |
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 | ||
940208a7 | 327 | if (main_cmds->cnt) { |
61c5d431 | 328 | const char *exec_path = git_exec_path(); |
4470ef94 | 329 | printf_ln(_("available git commands in '%s'"), exec_path); |
1eb05690 | 330 | putchar('\n'); |
d10cb3df | 331 | pretty_print_cmdnames(main_cmds, colopts); |
1eb05690 SP |
332 | putchar('\n'); |
333 | } | |
334 | ||
940208a7 | 335 | if (other_cmds->cnt) { |
cd87ce7d | 336 | puts(_("git commands available from elsewhere on your $PATH")); |
940208a7 | 337 | putchar('\n'); |
d10cb3df | 338 | pretty_print_cmdnames(other_cmds, colopts); |
1eb05690 SP |
339 | putchar('\n'); |
340 | } | |
70827b15 LT |
341 | } |
342 | ||
1542d4cd | 343 | void list_common_cmds_help(void) |
22414770 | 344 | { |
22414770 | 345 | puts(_("These are common Git commands used in various situations:")); |
93de1b69 | 346 | putchar('\n'); |
26c7d067 | 347 | print_cmd_by_category(common_categories, NULL); |
1542d4cd | 348 | } |
22414770 | 349 | |
6bb2dc0b NTND |
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); | |
22414770 SG |
364 | } |
365 | ||
6bb2dc0b | 366 | void list_all_other_cmds(struct string_list *list) |
1542d4cd | 367 | { |
6bb2dc0b NTND |
368 | struct cmdnames main_cmds, other_cmds; |
369 | int i; | |
1542d4cd | 370 | |
6bb2dc0b NTND |
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); | |
1542d4cd | 374 | |
6bb2dc0b NTND |
375 | for (i = 0; i < other_cmds.cnt; i++) |
376 | string_list_append(list, other_cmds.names[i]->name); | |
22414770 | 377 | |
6bb2dc0b NTND |
378 | clean_cmdnames(&main_cmds); |
379 | clean_cmdnames(&other_cmds); | |
380 | } | |
381 | ||
3c777767 NTND |
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; | |
22414770 | 387 | |
3c777767 NTND |
388 | for (i = 0; category_names[i]; i++) { |
389 | if (!strcmp(cat, category_names[i])) { | |
390 | cat_id = 1UL << i; | |
391 | break; | |
22414770 | 392 | } |
3c777767 NTND |
393 | } |
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 | ||
1b81d8cb NTND |
400 | if (!(cmd->category & cat_id)) |
401 | continue; | |
402 | string_list_append(list, drop_prefix(cmd->name, cmd->category)); | |
3c777767 NTND |
403 | } |
404 | } | |
405 | ||
6532f374 NTND |
406 | void list_cmds_by_config(struct string_list *list) |
407 | { | |
408 | const char *cmd_list; | |
409 | ||
f1de981e | 410 | if (git_config_get_string_tmp("completion.commands", &cmd_list)) |
6532f374 NTND |
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, ' '); | |
22414770 | 419 | |
6532f374 | 420 | strbuf_add(&sb, cmd_list, p - cmd_list); |
057ab54b JK |
421 | if (sb.buf[0] == '-') |
422 | string_list_remove(list, sb.buf + 1, 0); | |
6532f374 NTND |
423 | else |
424 | string_list_insert(list, sb.buf); | |
425 | strbuf_release(&sb); | |
426 | while (*p == ' ') | |
427 | p++; | |
428 | cmd_list = p; | |
1542d4cd JH |
429 | } |
430 | } | |
431 | ||
0371a764 | 432 | void list_guides_help(void) |
1b81d8cb NTND |
433 | { |
434 | struct category_description catdesc[] = { | |
0371a764 | 435 | { CAT_guide, N_("The Git concept guides are:") }, |
d976c510 ÆAB |
436 | { 0, NULL } |
437 | }; | |
438 | print_cmd_by_category(catdesc, NULL); | |
439 | putchar('\n'); | |
440 | } | |
441 | ||
442 | void list_user_interfaces_help(void) | |
443 | { | |
444 | struct category_description catdesc[] = { | |
445 | { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") }, | |
844739ba ÆAB |
446 | { 0, NULL } |
447 | }; | |
448 | print_cmd_by_category(catdesc, NULL); | |
449 | putchar('\n'); | |
450 | } | |
451 | ||
452 | void list_developer_interfaces_help(void) | |
453 | { | |
454 | struct category_description catdesc[] = { | |
455 | { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") }, | |
1b81d8cb NTND |
456 | { 0, NULL } |
457 | }; | |
26c7d067 | 458 | print_cmd_by_category(catdesc, NULL); |
1b81d8cb NTND |
459 | putchar('\n'); |
460 | } | |
461 | ||
a4e7e317 GC |
462 | static int get_alias(const char *var, const char *value, |
463 | const struct config_context *ctx UNUSED, void *data) | |
26c7d067 NTND |
464 | { |
465 | struct string_list *list = data; | |
466 | ||
467 | if (skip_prefix(var, "alias.", &var)) | |
468 | string_list_append(list, var)->util = xstrdup(value); | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
6fb427ab | 473 | static void list_all_cmds_help_external_commands(void) |
63eae83f | 474 | { |
26c7d067 | 475 | struct string_list others = STRING_LIST_INIT_DUP; |
6fb427ab | 476 | int i; |
26c7d067 NTND |
477 | |
478 | list_all_other_cmds(&others); | |
479 | if (others.nr) | |
480 | printf("\n%s\n", _("External commands")); | |
481 | for (i = 0; i < others.nr; i++) | |
482 | printf(" %s\n", others.items[i].string); | |
483 | string_list_clear(&others, 0); | |
6fb427ab ÆAB |
484 | } |
485 | ||
486 | static void list_all_cmds_help_aliases(int longest) | |
487 | { | |
488 | struct string_list alias_list = STRING_LIST_INIT_DUP; | |
489 | struct cmdname_help *aliases; | |
490 | int i; | |
26c7d067 NTND |
491 | |
492 | git_config(get_alias, &alias_list); | |
493 | string_list_sort(&alias_list); | |
1c4b9859 JS |
494 | |
495 | for (i = 0; i < alias_list.nr; i++) { | |
496 | size_t len = strlen(alias_list.items[i].string); | |
497 | if (longest < len) | |
498 | longest = len; | |
499 | } | |
500 | ||
26c7d067 NTND |
501 | if (alias_list.nr) { |
502 | printf("\n%s\n", _("Command aliases")); | |
503 | ALLOC_ARRAY(aliases, alias_list.nr + 1); | |
504 | for (i = 0; i < alias_list.nr; i++) { | |
505 | aliases[i].name = alias_list.items[i].string; | |
506 | aliases[i].help = alias_list.items[i].util; | |
507 | aliases[i].category = 1; | |
508 | } | |
509 | aliases[alias_list.nr].name = NULL; | |
510 | print_command_list(aliases, 1, longest); | |
511 | free(aliases); | |
512 | } | |
513 | string_list_clear(&alias_list, 1); | |
63eae83f NTND |
514 | } |
515 | ||
1ce59013 | 516 | void list_all_cmds_help(int show_external_commands, int show_aliases) |
6fb427ab ÆAB |
517 | { |
518 | int longest; | |
519 | ||
520 | puts(_("See 'git help <command>' to read about a specific subcommand")); | |
93de1b69 | 521 | putchar('\n'); |
6fb427ab ÆAB |
522 | print_cmd_by_category(main_categories, &longest); |
523 | ||
1ce59013 ÆAB |
524 | if (show_external_commands) |
525 | list_all_cmds_help_external_commands(); | |
526 | if (show_aliases) | |
527 | list_all_cmds_help_aliases(longest); | |
6fb427ab ÆAB |
528 | } |
529 | ||
940208a7 | 530 | int is_in_cmdlist(struct cmdnames *c, const char *s) |
2156435f JK |
531 | { |
532 | int i; | |
533 | for (i = 0; i < c->cnt; i++) | |
534 | if (!strcmp(s, c->names[i]->name)) | |
535 | return 1; | |
536 | return 0; | |
537 | } | |
538 | ||
f0e90716 | 539 | static int autocorrect; |
746c221a | 540 | static struct cmdnames aliases; |
f0e90716 | 541 | |
dc66e3c7 | 542 | #define AUTOCORRECT_PROMPT (-3) |
644bb953 DD |
543 | #define AUTOCORRECT_NEVER (-2) |
544 | #define AUTOCORRECT_IMMEDIATELY (-1) | |
545 | ||
506ebaac | 546 | static int git_unknown_cmd_config(const char *var, const char *value, |
8868b1eb | 547 | const struct config_context *ctx, |
506ebaac | 548 | void *cb UNUSED) |
f0e90716 | 549 | { |
ae021d87 JK |
550 | const char *p; |
551 | ||
644bb953 DD |
552 | if (!strcmp(var, "help.autocorrect")) { |
553 | if (!value) | |
554 | return config_error_nonbool(var); | |
555 | if (!strcmp(value, "never")) { | |
556 | autocorrect = AUTOCORRECT_NEVER; | |
557 | } else if (!strcmp(value, "immediate")) { | |
558 | autocorrect = AUTOCORRECT_IMMEDIATELY; | |
dc66e3c7 ABA |
559 | } else if (!strcmp(value, "prompt")) { |
560 | autocorrect = AUTOCORRECT_PROMPT; | |
644bb953 | 561 | } else { |
8868b1eb | 562 | int v = git_config_int(var, value, ctx->kvi); |
644bb953 DD |
563 | autocorrect = (v < 0) |
564 | ? AUTOCORRECT_IMMEDIATELY : v; | |
565 | } | |
566 | } | |
746c221a | 567 | /* Also use aliases for command lookup */ |
ae021d87 JK |
568 | if (skip_prefix(var, "alias.", &p)) |
569 | add_cmdname(&aliases, p, strlen(p)); | |
f0e90716 | 570 | |
0918d088 | 571 | return 0; |
f0e90716 AR |
572 | } |
573 | ||
8af84dad | 574 | static int levenshtein_compare(const void *p1, const void *p2) |
822a7d50 | 575 | { |
8af84dad JS |
576 | const struct cmdname *const *c1 = p1, *const *c2 = p2; |
577 | const char *s1 = (*c1)->name, *s2 = (*c2)->name; | |
578 | int l1 = (*c1)->len; | |
579 | int l2 = (*c2)->len; | |
580 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); | |
581 | } | |
582 | ||
746c221a PB |
583 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
584 | { | |
585 | int i; | |
586 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); | |
587 | ||
588 | for (i = 0; i < old->cnt; i++) | |
589 | cmds->names[cmds->cnt++] = old->names[i]; | |
88ce3ef6 | 590 | FREE_AND_NULL(old->names); |
746c221a | 591 | old->cnt = 0; |
746c221a PB |
592 | } |
593 | ||
06500a02 | 594 | /* An empirically derived magic number */ |
6612b9e4 EFL |
595 | #define SIMILARITY_FLOOR 7 |
596 | #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR) | |
06500a02 | 597 | |
823e0ded MS |
598 | static const char bad_interpreter_advice[] = |
599 | N_("'%s' appears to be a git command, but we were not\n" | |
600 | "able to execute it. Maybe git-%s is broken?"); | |
601 | ||
8af84dad | 602 | const char *help_unknown_cmd(const char *cmd) |
822a7d50 | 603 | { |
8af84dad JS |
604 | int i, n, best_similarity = 0; |
605 | struct cmdnames main_cmds, other_cmds; | |
cfb22a02 | 606 | struct cmdname_help *common_cmds; |
8af84dad JS |
607 | |
608 | memset(&main_cmds, 0, sizeof(main_cmds)); | |
0b74f5dc | 609 | memset(&other_cmds, 0, sizeof(other_cmds)); |
746c221a | 610 | memset(&aliases, 0, sizeof(aliases)); |
8af84dad | 611 | |
659fef19 | 612 | read_early_config(git_unknown_cmd_config, NULL); |
f0e90716 | 613 | |
dc66e3c7 ABA |
614 | /* |
615 | * Disable autocorrection prompt in a non-interactive session | |
616 | */ | |
617 | if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2))) | |
618 | autocorrect = AUTOCORRECT_NEVER; | |
619 | ||
644bb953 DD |
620 | if (autocorrect == AUTOCORRECT_NEVER) { |
621 | fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); | |
622 | exit(1); | |
623 | } | |
624 | ||
8af84dad JS |
625 | load_command_list("git-", &main_cmds, &other_cmds); |
626 | ||
746c221a PB |
627 | add_cmd_list(&main_cmds, &aliases); |
628 | add_cmd_list(&main_cmds, &other_cmds); | |
9ed0d8d6 | 629 | QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare); |
746c221a | 630 | uniq(&main_cmds); |
8af84dad | 631 | |
cfb22a02 NTND |
632 | extract_cmds(&common_cmds, common_mask); |
633 | ||
6612b9e4 EFL |
634 | /* This abuses cmdname->len for levenshtein distance */ |
635 | for (i = 0, n = 0; i < main_cmds.cnt; i++) { | |
636 | int cmp = 0; /* avoid compiler stupidity */ | |
637 | const char *candidate = main_cmds.names[i]->name; | |
638 | ||
823e0ded MS |
639 | /* |
640 | * An exact match means we have the command, but | |
641 | * for some reason exec'ing it gave us ENOENT; probably | |
642 | * it's a bad interpreter in the #! line. | |
643 | */ | |
644 | if (!strcmp(candidate, cmd)) | |
645 | die(_(bad_interpreter_advice), cmd, cmd); | |
646 | ||
6612b9e4 | 647 | /* Does the candidate appear in common_cmds list? */ |
cfb22a02 | 648 | while (common_cmds[n].name && |
6612b9e4 EFL |
649 | (cmp = strcmp(common_cmds[n].name, candidate)) < 0) |
650 | n++; | |
cfb22a02 | 651 | if (common_cmds[n].name && !cmp) { |
6612b9e4 EFL |
652 | /* Yes, this is one of the common commands */ |
653 | n++; /* use the entry from common_cmds[] */ | |
59556548 | 654 | if (starts_with(candidate, cmd)) { |
6612b9e4 EFL |
655 | /* Give prefix match a very good score */ |
656 | main_cmds.names[i]->len = 0; | |
657 | continue; | |
658 | } | |
659 | } | |
660 | ||
8af84dad | 661 | main_cmds.names[i]->len = |
c41494f8 | 662 | levenshtein(cmd, candidate, 0, 2, 1, 3) + 1; |
6612b9e4 | 663 | } |
cfb22a02 | 664 | FREE_AND_NULL(common_cmds); |
8af84dad | 665 | |
9ed0d8d6 | 666 | QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare); |
8af84dad JS |
667 | |
668 | if (!main_cmds.cnt) | |
9665627d | 669 | die(_("Uh oh. Your system reports no Git commands at all.")); |
8af84dad | 670 | |
6612b9e4 EFL |
671 | /* skip and count prefix matches */ |
672 | for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++) | |
673 | ; /* still counting */ | |
674 | ||
675 | if (main_cmds.cnt <= n) { | |
676 | /* prefix matches with everything? that is too ambiguous */ | |
677 | best_similarity = SIMILARITY_FLOOR + 1; | |
678 | } else { | |
679 | /* count all the most similar ones */ | |
680 | for (best_similarity = main_cmds.names[n++]->len; | |
681 | (n < main_cmds.cnt && | |
682 | best_similarity == main_cmds.names[n]->len); | |
683 | n++) | |
684 | ; /* still counting */ | |
685 | } | |
06500a02 | 686 | if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { |
8af84dad JS |
687 | const char *assumed = main_cmds.names[0]->name; |
688 | main_cmds.names[0] = NULL; | |
689 | clean_cmdnames(&main_cmds); | |
9665627d NTND |
690 | fprintf_ln(stderr, |
691 | _("WARNING: You called a Git command named '%s', " | |
968b1fe2 MB |
692 | "which does not exist."), |
693 | cmd); | |
644bb953 | 694 | if (autocorrect == AUTOCORRECT_IMMEDIATELY) |
968b1fe2 MB |
695 | fprintf_ln(stderr, |
696 | _("Continuing under the assumption that " | |
697 | "you meant '%s'."), | |
698 | assumed); | |
dc66e3c7 ABA |
699 | else if (autocorrect == AUTOCORRECT_PROMPT) { |
700 | char *answer; | |
701 | struct strbuf msg = STRBUF_INIT; | |
0fc8ed15 | 702 | strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed); |
dc66e3c7 ABA |
703 | answer = git_prompt(msg.buf, PROMPT_ECHO); |
704 | strbuf_release(&msg); | |
705 | if (!(starts_with(answer, "y") || | |
706 | starts_with(answer, "Y"))) | |
707 | exit(1); | |
708 | } else { | |
968b1fe2 MB |
709 | fprintf_ln(stderr, |
710 | _("Continuing in %0.1f seconds, " | |
711 | "assuming that you meant '%s'."), | |
712 | (float)autocorrect/10.0, assumed); | |
2024d317 | 713 | sleep_millisec(autocorrect * 100); |
f0e90716 | 714 | } |
8af84dad JS |
715 | return assumed; |
716 | } | |
717 | ||
9665627d | 718 | fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); |
8af84dad | 719 | |
06500a02 | 720 | if (SIMILAR_ENOUGH(best_similarity)) { |
9665627d | 721 | fprintf_ln(stderr, |
6c486862 JNA |
722 | Q_("\nThe most similar command is", |
723 | "\nThe most similar commands are", | |
9665627d | 724 | n)); |
8af84dad JS |
725 | |
726 | for (i = 0; i < n; i++) | |
727 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); | |
728 | } | |
729 | ||
822a7d50 RJ |
730 | exit(1); |
731 | } | |
732 | ||
617d5719 ES |
733 | void get_version_info(struct strbuf *buf, int show_build_options) |
734 | { | |
735 | /* | |
736 | * The format of this string should be kept stable for compatibility | |
737 | * with external projects that rely on the output of "git version". | |
738 | * | |
739 | * Always show the version, even if other options are given. | |
740 | */ | |
741 | strbuf_addf(buf, "git version %s\n", git_version_string); | |
742 | ||
743 | if (show_build_options) { | |
744 | strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU); | |
745 | if (git_built_from_commit_string[0]) | |
746 | strbuf_addf(buf, "built from commit: %s\n", | |
747 | git_built_from_commit_string); | |
748 | else | |
749 | strbuf_addstr(buf, "no commit associated with this build\n"); | |
750 | strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long)); | |
751 | strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t)); | |
39f4919d | 752 | strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH); |
617d5719 | 753 | /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */ |
dd77cf61 JH |
754 | |
755 | if (fsmonitor_ipc__is_supported()) | |
756 | strbuf_addstr(buf, "feature: fsmonitor--daemon\n"); | |
617d5719 ES |
757 | } |
758 | } | |
759 | ||
a633fca0 | 760 | int cmd_version(int argc, const char **argv, const char *prefix) |
70827b15 | 761 | { |
617d5719 | 762 | struct strbuf buf = STRBUF_INIT; |
b48cbfc5 JK |
763 | int build_options = 0; |
764 | const char * const usage[] = { | |
8c9e292d | 765 | N_("git version [--build-options]"), |
b48cbfc5 JK |
766 | NULL |
767 | }; | |
768 | struct option options[] = { | |
769 | OPT_BOOL(0, "build-options", &build_options, | |
770 | "also print build options"), | |
771 | OPT_END() | |
772 | }; | |
773 | ||
774 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
775 | ||
617d5719 ES |
776 | get_version_info(&buf, build_options); |
777 | printf("%s", buf.buf); | |
778 | ||
779 | strbuf_release(&buf); | |
b48cbfc5 | 780 | |
70827b15 LT |
781 | return 0; |
782 | } | |
e5618106 VV |
783 | |
784 | struct similar_ref_cb { | |
785 | const char *base_ref; | |
786 | struct string_list *similar_refs; | |
787 | }; | |
788 | ||
63e14ee2 | 789 | static int append_similar_ref(const char *refname, |
5cf88fd8 ÆAB |
790 | const struct object_id *oid UNUSED, |
791 | int flags UNUSED, void *cb_data) | |
e5618106 VV |
792 | { |
793 | struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); | |
794 | char *branch = strrchr(refname, '/') + 1; | |
95b567c7 | 795 | |
e5618106 | 796 | /* A remote branch of the same name is deemed similar */ |
2ed2e199 | 797 | if (starts_with(refname, "refs/remotes/") && |
e5618106 | 798 | !strcmp(branch, cb->base_ref)) |
2ed2e199 JK |
799 | string_list_append_nodup(cb->similar_refs, |
800 | shorten_unambiguous_ref(refname, 1)); | |
e5618106 VV |
801 | return 0; |
802 | } | |
803 | ||
804 | static struct string_list guess_refs(const char *ref) | |
805 | { | |
806 | struct similar_ref_cb ref_cb; | |
8ed51b06 | 807 | struct string_list similar_refs = STRING_LIST_INIT_DUP; |
e5618106 VV |
808 | |
809 | ref_cb.base_ref = ref; | |
810 | ref_cb.similar_refs = &similar_refs; | |
811 | for_each_ref(append_similar_ref, &ref_cb); | |
812 | return similar_refs; | |
813 | } | |
814 | ||
80e36586 RS |
815 | NORETURN void help_unknown_ref(const char *ref, const char *cmd, |
816 | const char *error) | |
e5618106 VV |
817 | { |
818 | int i; | |
819 | struct string_list suggested_refs = guess_refs(ref); | |
820 | ||
821 | fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error); | |
822 | ||
823 | if (suggested_refs.nr > 0) { | |
824 | fprintf_ln(stderr, | |
825 | Q_("\nDid you mean this?", | |
826 | "\nDid you mean one of these?", | |
827 | suggested_refs.nr)); | |
828 | for (i = 0; i < suggested_refs.nr; i++) | |
829 | fprintf(stderr, "\t%s\n", suggested_refs.items[i].string); | |
830 | } | |
831 | ||
832 | string_list_clear(&suggested_refs, 0); | |
833 | exit(1); | |
834 | } |