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