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