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