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