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