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