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