builtin: fix typos
[git/gitster.git] / help.c
blob4ad4ebdd2cfddb3191bb4f6d593a55d3c5d3ed14
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "config.h"
5 #include "builtin.h"
6 #include "exec-cmd.h"
7 #include "run-command.h"
8 #include "levenshtein.h"
9 #include "gettext.h"
10 #include "help.h"
11 #include "command-list.h"
12 #include "string-list.h"
13 #include "column.h"
14 #include "version.h"
15 #include "refs.h"
16 #include "parse-options.h"
17 #include "prompt.h"
18 #include "fsmonitor-ipc.h"
19 #include "repository.h"
21 #ifndef NO_CURL
22 #include "git-curl-compat.h" /* For LIBCURL_VERSION only */
23 #endif
25 struct category_description {
26 uint32_t category;
27 const char *desc;
29 static uint32_t common_mask =
30 CAT_init | CAT_worktree | CAT_info |
31 CAT_history | CAT_remote;
32 static struct category_description common_categories[] = {
33 { CAT_init, N_("start a working area (see also: git help tutorial)") },
34 { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
35 { CAT_info, N_("examine the history and state (see also: git help revisions)") },
36 { CAT_history, N_("grow, mark and tweak your common history") },
37 { CAT_remote, N_("collaborate (see also: git help workflows)") },
38 { 0, NULL }
40 static struct category_description main_categories[] = {
41 { CAT_mainporcelain, N_("Main Porcelain Commands") },
42 { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
43 { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
44 { CAT_foreignscminterface, N_("Interacting with Others") },
45 { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
46 { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
47 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
48 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
49 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
50 { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
51 { 0, NULL }
54 static const char *drop_prefix(const char *name, uint32_t category)
56 const char *new_name;
57 const char *prefix;
59 switch (category) {
60 case CAT_guide:
61 case CAT_userinterfaces:
62 case CAT_developerinterfaces:
63 prefix = "git";
64 break;
65 default:
66 prefix = "git-";
67 break;
69 if (skip_prefix(name, prefix, &new_name))
70 return new_name;
72 return name;
75 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
77 int i, nr = 0;
78 struct cmdname_help *cmds;
80 if (ARRAY_SIZE(command_list) == 0)
81 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
83 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
85 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
86 const struct cmdname_help *cmd = command_list + i;
88 if (!(cmd->category & mask))
89 continue;
91 cmds[nr] = *cmd;
92 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
94 nr++;
96 cmds[nr].name = NULL;
97 *p_cmds = cmds;
100 static void print_command_list(const struct cmdname_help *cmds,
101 uint32_t mask, int longest)
103 int i;
105 for (i = 0; cmds[i].name; i++) {
106 if (cmds[i].category & mask) {
107 size_t len = strlen(cmds[i].name);
108 printf(" %s ", cmds[i].name);
109 if (longest > len)
110 mput_char(' ', longest - len);
111 puts(_(cmds[i].help));
116 static int cmd_name_cmp(const void *elem1, const void *elem2)
118 const struct cmdname_help *e1 = elem1;
119 const struct cmdname_help *e2 = elem2;
121 return strcmp(e1->name, e2->name);
124 static void print_cmd_by_category(const struct category_description *catdesc,
125 int *longest_p)
127 struct cmdname_help *cmds;
128 int longest = 0;
129 int i, nr = 0;
130 uint32_t mask = 0;
132 for (i = 0; catdesc[i].desc; i++)
133 mask |= catdesc[i].category;
135 extract_cmds(&cmds, mask);
137 for (i = 0; cmds[i].name; i++, nr++) {
138 if (longest < strlen(cmds[i].name))
139 longest = strlen(cmds[i].name);
141 QSORT(cmds, nr, cmd_name_cmp);
143 for (i = 0; catdesc[i].desc; i++) {
144 uint32_t mask = catdesc[i].category;
145 const char *desc = catdesc[i].desc;
147 if (i)
148 putchar('\n');
149 puts(_(desc));
150 print_command_list(cmds, mask, longest);
152 free(cmds);
153 if (longest_p)
154 *longest_p = longest;
157 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
159 struct cmdname *ent;
160 FLEX_ALLOC_MEM(ent, name, name, len);
161 ent->len = len;
163 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
164 cmds->names[cmds->cnt++] = ent;
167 void cmdnames_release(struct cmdnames *cmds)
169 int i;
170 for (i = 0; i < cmds->cnt; ++i)
171 free(cmds->names[i]);
172 free(cmds->names);
173 cmds->cnt = 0;
174 cmds->alloc = 0;
177 static int cmdname_compare(const void *a_, const void *b_)
179 struct cmdname *a = *(struct cmdname **)a_;
180 struct cmdname *b = *(struct cmdname **)b_;
181 return strcmp(a->name, b->name);
184 static void uniq(struct cmdnames *cmds)
186 int i, j;
188 if (!cmds->cnt)
189 return;
191 for (i = j = 1; i < cmds->cnt; i++) {
192 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
193 free(cmds->names[i]);
194 else
195 cmds->names[j++] = cmds->names[i];
198 cmds->cnt = j;
201 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
203 int ci, cj, ei;
204 int cmp;
206 ci = cj = ei = 0;
207 while (ci < cmds->cnt && ei < excludes->cnt) {
208 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
209 if (cmp < 0)
210 cmds->names[cj++] = cmds->names[ci++];
211 else if (cmp == 0) {
212 ei++;
213 free(cmds->names[ci++]);
214 } else if (cmp > 0)
215 ei++;
218 while (ci < cmds->cnt)
219 cmds->names[cj++] = cmds->names[ci++];
221 cmds->cnt = cj;
224 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
226 struct string_list list = STRING_LIST_INIT_NODUP;
227 struct column_options copts;
228 int i;
230 for (i = 0; i < cmds->cnt; i++)
231 string_list_append(&list, cmds->names[i]->name);
233 * always enable column display, we only consult column.*
234 * about layout strategy and stuff
236 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
237 memset(&copts, 0, sizeof(copts));
238 copts.indent = " ";
239 copts.padding = 2;
240 print_columns(&list, colopts, &copts);
241 string_list_clear(&list, 0);
244 static void list_commands_in_dir(struct cmdnames *cmds,
245 const char *path,
246 const char *prefix)
248 DIR *dir = opendir(path);
249 struct dirent *de;
250 struct strbuf buf = STRBUF_INIT;
251 int len;
253 if (!dir)
254 return;
255 if (!prefix)
256 prefix = "git-";
258 strbuf_addf(&buf, "%s/", path);
259 len = buf.len;
261 while ((de = readdir(dir)) != NULL) {
262 const char *ent;
263 size_t entlen;
265 if (!skip_prefix(de->d_name, prefix, &ent))
266 continue;
268 strbuf_setlen(&buf, len);
269 strbuf_addstr(&buf, de->d_name);
270 if (!is_executable(buf.buf))
271 continue;
273 entlen = strlen(ent);
274 strip_suffix(ent, ".exe", &entlen);
276 add_cmdname(cmds, ent, entlen);
278 closedir(dir);
279 strbuf_release(&buf);
282 void load_command_list(const char *prefix,
283 struct cmdnames *main_cmds,
284 struct cmdnames *other_cmds)
286 const char *env_path = getenv("PATH");
287 const char *exec_path = git_exec_path();
289 load_builtin_commands(prefix, main_cmds);
291 if (exec_path) {
292 list_commands_in_dir(main_cmds, exec_path, prefix);
293 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
294 uniq(main_cmds);
297 if (env_path) {
298 char *paths, *path, *colon;
299 path = paths = xstrdup(env_path);
300 while (1) {
301 if ((colon = strchr(path, PATH_SEP)))
302 *colon = 0;
303 if (!exec_path || strcmp(path, exec_path))
304 list_commands_in_dir(other_cmds, path, prefix);
306 if (!colon)
307 break;
308 path = colon + 1;
310 free(paths);
312 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
313 uniq(other_cmds);
315 exclude_cmds(other_cmds, main_cmds);
318 static int get_colopts(const char *var, const char *value,
319 const struct config_context *ctx UNUSED, void *data)
321 unsigned int *colopts = data;
323 if (starts_with(var, "column."))
324 return git_column_config(var, value, "help", colopts);
326 return 0;
329 void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
331 unsigned int colopts = 0;
332 git_config(get_colopts, &colopts);
334 if (main_cmds->cnt) {
335 const char *exec_path = git_exec_path();
336 printf_ln(_("available git commands in '%s'"), exec_path);
337 putchar('\n');
338 pretty_print_cmdnames(main_cmds, colopts);
339 putchar('\n');
342 if (other_cmds->cnt) {
343 puts(_("git commands available from elsewhere on your $PATH"));
344 putchar('\n');
345 pretty_print_cmdnames(other_cmds, colopts);
346 putchar('\n');
350 void list_common_cmds_help(void)
352 puts(_("These are common Git commands used in various situations:"));
353 putchar('\n');
354 print_cmd_by_category(common_categories, NULL);
357 void list_all_main_cmds(struct string_list *list)
359 struct cmdnames main_cmds, other_cmds;
360 int i;
362 memset(&main_cmds, 0, sizeof(main_cmds));
363 memset(&other_cmds, 0, sizeof(other_cmds));
364 load_command_list("git-", &main_cmds, &other_cmds);
366 for (i = 0; i < main_cmds.cnt; i++)
367 string_list_append(list, main_cmds.names[i]->name);
369 cmdnames_release(&main_cmds);
370 cmdnames_release(&other_cmds);
373 void list_all_other_cmds(struct string_list *list)
375 struct cmdnames main_cmds, other_cmds;
376 int i;
378 memset(&main_cmds, 0, sizeof(main_cmds));
379 memset(&other_cmds, 0, sizeof(other_cmds));
380 load_command_list("git-", &main_cmds, &other_cmds);
382 for (i = 0; i < other_cmds.cnt; i++)
383 string_list_append(list, other_cmds.names[i]->name);
385 cmdnames_release(&main_cmds);
386 cmdnames_release(&other_cmds);
389 void list_cmds_by_category(struct string_list *list,
390 const char *cat)
392 int i, n = ARRAY_SIZE(command_list);
393 uint32_t cat_id = 0;
395 for (i = 0; category_names[i]; i++) {
396 if (!strcmp(cat, category_names[i])) {
397 cat_id = 1UL << i;
398 break;
401 if (!cat_id)
402 die(_("unsupported command listing type '%s'"), cat);
404 for (i = 0; i < n; i++) {
405 struct cmdname_help *cmd = command_list + i;
407 if (!(cmd->category & cat_id))
408 continue;
409 string_list_append(list, drop_prefix(cmd->name, cmd->category));
413 void list_cmds_by_config(struct string_list *list)
415 const char *cmd_list;
417 if (git_config_get_string_tmp("completion.commands", &cmd_list))
418 return;
420 string_list_sort(list);
421 string_list_remove_duplicates(list, 0);
423 while (*cmd_list) {
424 struct strbuf sb = STRBUF_INIT;
425 const char *p = strchrnul(cmd_list, ' ');
427 strbuf_add(&sb, cmd_list, p - cmd_list);
428 if (sb.buf[0] == '-')
429 string_list_remove(list, sb.buf + 1, 0);
430 else
431 string_list_insert(list, sb.buf);
432 strbuf_release(&sb);
433 while (*p == ' ')
434 p++;
435 cmd_list = p;
439 void list_guides_help(void)
441 struct category_description catdesc[] = {
442 { CAT_guide, N_("The Git concept guides are:") },
443 { 0, NULL }
445 print_cmd_by_category(catdesc, NULL);
446 putchar('\n');
449 void list_user_interfaces_help(void)
451 struct category_description catdesc[] = {
452 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
453 { 0, NULL }
455 print_cmd_by_category(catdesc, NULL);
456 putchar('\n');
459 void list_developer_interfaces_help(void)
461 struct category_description catdesc[] = {
462 { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
463 { 0, NULL }
465 print_cmd_by_category(catdesc, NULL);
466 putchar('\n');
469 static int get_alias(const char *var, const char *value,
470 const struct config_context *ctx UNUSED, void *data)
472 struct string_list *list = data;
474 if (skip_prefix(var, "alias.", &var)) {
475 if (!value)
476 return config_error_nonbool(var);
477 string_list_append(list, var)->util = xstrdup(value);
480 return 0;
483 static void list_all_cmds_help_external_commands(void)
485 struct string_list others = STRING_LIST_INIT_DUP;
486 int i;
488 list_all_other_cmds(&others);
489 if (others.nr)
490 printf("\n%s\n", _("External commands"));
491 for (i = 0; i < others.nr; i++)
492 printf(" %s\n", others.items[i].string);
493 string_list_clear(&others, 0);
496 static void list_all_cmds_help_aliases(int longest)
498 struct string_list alias_list = STRING_LIST_INIT_DUP;
499 struct cmdname_help *aliases;
500 int i;
502 git_config(get_alias, &alias_list);
503 string_list_sort(&alias_list);
505 for (i = 0; i < alias_list.nr; i++) {
506 size_t len = strlen(alias_list.items[i].string);
507 if (longest < len)
508 longest = len;
511 if (alias_list.nr) {
512 printf("\n%s\n", _("Command aliases"));
513 ALLOC_ARRAY(aliases, alias_list.nr + 1);
514 for (i = 0; i < alias_list.nr; i++) {
515 aliases[i].name = alias_list.items[i].string;
516 aliases[i].help = alias_list.items[i].util;
517 aliases[i].category = 1;
519 aliases[alias_list.nr].name = NULL;
520 print_command_list(aliases, 1, longest);
521 free(aliases);
523 string_list_clear(&alias_list, 1);
526 void list_all_cmds_help(int show_external_commands, int show_aliases)
528 int longest;
530 puts(_("See 'git help <command>' to read about a specific subcommand"));
531 putchar('\n');
532 print_cmd_by_category(main_categories, &longest);
534 if (show_external_commands)
535 list_all_cmds_help_external_commands();
536 if (show_aliases)
537 list_all_cmds_help_aliases(longest);
540 int is_in_cmdlist(struct cmdnames *c, const char *s)
542 int i;
543 for (i = 0; i < c->cnt; i++)
544 if (!strcmp(s, c->names[i]->name))
545 return 1;
546 return 0;
549 static int autocorrect;
550 static struct cmdnames aliases;
552 #define AUTOCORRECT_PROMPT (-3)
553 #define AUTOCORRECT_NEVER (-2)
554 #define AUTOCORRECT_IMMEDIATELY (-1)
556 static int git_unknown_cmd_config(const char *var, const char *value,
557 const struct config_context *ctx,
558 void *cb UNUSED)
560 const char *p;
562 if (!strcmp(var, "help.autocorrect")) {
563 if (!value)
564 return config_error_nonbool(var);
565 if (!strcmp(value, "never")) {
566 autocorrect = AUTOCORRECT_NEVER;
567 } else if (!strcmp(value, "immediate")) {
568 autocorrect = AUTOCORRECT_IMMEDIATELY;
569 } else if (!strcmp(value, "prompt")) {
570 autocorrect = AUTOCORRECT_PROMPT;
571 } else {
572 int v = git_config_int(var, value, ctx->kvi);
573 autocorrect = (v < 0)
574 ? AUTOCORRECT_IMMEDIATELY : v;
577 /* Also use aliases for command lookup */
578 if (skip_prefix(var, "alias.", &p))
579 add_cmdname(&aliases, p, strlen(p));
581 return 0;
584 static int levenshtein_compare(const void *p1, const void *p2)
586 const struct cmdname *const *c1 = p1, *const *c2 = p2;
587 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
588 int l1 = (*c1)->len;
589 int l2 = (*c2)->len;
590 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
593 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
595 int i;
596 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
598 for (i = 0; i < old->cnt; i++)
599 cmds->names[cmds->cnt++] = old->names[i];
600 FREE_AND_NULL(old->names);
601 old->cnt = 0;
604 /* An empirically derived magic number */
605 #define SIMILARITY_FLOOR 7
606 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
608 static const char bad_interpreter_advice[] =
609 N_("'%s' appears to be a git command, but we were not\n"
610 "able to execute it. Maybe git-%s is broken?");
612 const char *help_unknown_cmd(const char *cmd)
614 int i, n, best_similarity = 0;
615 struct cmdnames main_cmds, other_cmds;
616 struct cmdname_help *common_cmds;
618 memset(&main_cmds, 0, sizeof(main_cmds));
619 memset(&other_cmds, 0, sizeof(other_cmds));
620 memset(&aliases, 0, sizeof(aliases));
622 read_early_config(the_repository, git_unknown_cmd_config, NULL);
625 * Disable autocorrection prompt in a non-interactive session
627 if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
628 autocorrect = AUTOCORRECT_NEVER;
630 if (autocorrect == AUTOCORRECT_NEVER) {
631 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
632 exit(1);
635 load_command_list("git-", &main_cmds, &other_cmds);
637 add_cmd_list(&main_cmds, &aliases);
638 add_cmd_list(&main_cmds, &other_cmds);
639 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
640 uniq(&main_cmds);
642 extract_cmds(&common_cmds, common_mask);
644 /* This abuses cmdname->len for levenshtein distance */
645 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
646 int cmp = 0; /* avoid compiler stupidity */
647 const char *candidate = main_cmds.names[i]->name;
650 * An exact match means we have the command, but
651 * for some reason exec'ing it gave us ENOENT; probably
652 * it's a bad interpreter in the #! line.
654 if (!strcmp(candidate, cmd))
655 die(_(bad_interpreter_advice), cmd, cmd);
657 /* Does the candidate appear in common_cmds list? */
658 while (common_cmds[n].name &&
659 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
660 n++;
661 if (common_cmds[n].name && !cmp) {
662 /* Yes, this is one of the common commands */
663 n++; /* use the entry from common_cmds[] */
664 if (starts_with(candidate, cmd)) {
665 /* Give prefix match a very good score */
666 main_cmds.names[i]->len = 0;
667 continue;
671 main_cmds.names[i]->len =
672 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
674 FREE_AND_NULL(common_cmds);
676 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
678 if (!main_cmds.cnt)
679 die(_("Uh oh. Your system reports no Git commands at all."));
681 /* skip and count prefix matches */
682 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
683 ; /* still counting */
685 if (main_cmds.cnt <= n) {
686 /* prefix matches with everything? that is too ambiguous */
687 best_similarity = SIMILARITY_FLOOR + 1;
688 } else {
689 /* count all the most similar ones */
690 for (best_similarity = main_cmds.names[n++]->len;
691 (n < main_cmds.cnt &&
692 best_similarity == main_cmds.names[n]->len);
693 n++)
694 ; /* still counting */
696 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
697 const char *assumed = main_cmds.names[0]->name;
698 main_cmds.names[0] = NULL;
699 cmdnames_release(&main_cmds);
700 fprintf_ln(stderr,
701 _("WARNING: You called a Git command named '%s', "
702 "which does not exist."),
703 cmd);
704 if (autocorrect == AUTOCORRECT_IMMEDIATELY)
705 fprintf_ln(stderr,
706 _("Continuing under the assumption that "
707 "you meant '%s'."),
708 assumed);
709 else if (autocorrect == AUTOCORRECT_PROMPT) {
710 char *answer;
711 struct strbuf msg = STRBUF_INIT;
712 strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
713 answer = git_prompt(msg.buf, PROMPT_ECHO);
714 strbuf_release(&msg);
715 if (!(starts_with(answer, "y") ||
716 starts_with(answer, "Y")))
717 exit(1);
718 } else {
719 fprintf_ln(stderr,
720 _("Continuing in %0.1f seconds, "
721 "assuming that you meant '%s'."),
722 (float)autocorrect/10.0, assumed);
723 sleep_millisec(autocorrect * 100);
725 return assumed;
728 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
730 if (SIMILAR_ENOUGH(best_similarity)) {
731 fprintf_ln(stderr,
732 Q_("\nThe most similar command is",
733 "\nThe most similar commands are",
734 n));
736 for (i = 0; i < n; i++)
737 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
740 exit(1);
743 void get_version_info(struct strbuf *buf, int show_build_options)
746 * The format of this string should be kept stable for compatibility
747 * with external projects that rely on the output of "git version".
749 * Always show the version, even if other options are given.
751 strbuf_addf(buf, "git version %s\n", git_version_string);
753 if (show_build_options) {
754 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
755 if (git_built_from_commit_string[0])
756 strbuf_addf(buf, "built from commit: %s\n",
757 git_built_from_commit_string);
758 else
759 strbuf_addstr(buf, "no commit associated with this build\n");
760 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
761 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
762 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
763 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
765 if (fsmonitor_ipc__is_supported())
766 strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
767 #if defined LIBCURL_VERSION
768 strbuf_addf(buf, "libcurl: %s\n", LIBCURL_VERSION);
769 #endif
770 #if defined OPENSSL_VERSION_TEXT
771 strbuf_addf(buf, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT);
772 #endif
773 #if defined ZLIB_VERSION
774 strbuf_addf(buf, "zlib: %s\n", ZLIB_VERSION);
775 #endif
779 int cmd_version(int argc, const char **argv, const char *prefix, struct repository *repository UNUSED)
781 struct strbuf buf = STRBUF_INIT;
782 int build_options = 0;
783 const char * const usage[] = {
784 N_("git version [--build-options]"),
785 NULL
787 struct option options[] = {
788 OPT_BOOL(0, "build-options", &build_options,
789 "also print build options"),
790 OPT_END()
793 argc = parse_options(argc, argv, prefix, options, usage, 0);
795 get_version_info(&buf, build_options);
796 printf("%s", buf.buf);
798 strbuf_release(&buf);
800 return 0;
803 struct similar_ref_cb {
804 const char *base_ref;
805 struct string_list *similar_refs;
808 static int append_similar_ref(const char *refname, const char *referent UNUSED,
809 const struct object_id *oid UNUSED,
810 int flags UNUSED, void *cb_data)
812 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
813 char *branch = strrchr(refname, '/') + 1;
815 /* A remote branch of the same name is deemed similar */
816 if (starts_with(refname, "refs/remotes/") &&
817 !strcmp(branch, cb->base_ref))
818 string_list_append_nodup(cb->similar_refs,
819 refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1));
820 return 0;
823 static struct string_list guess_refs(const char *ref)
825 struct similar_ref_cb ref_cb;
826 struct string_list similar_refs = STRING_LIST_INIT_DUP;
828 ref_cb.base_ref = ref;
829 ref_cb.similar_refs = &similar_refs;
830 refs_for_each_ref(get_main_ref_store(the_repository),
831 append_similar_ref, &ref_cb);
832 return similar_refs;
835 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
836 const char *error)
838 int i;
839 struct string_list suggested_refs = guess_refs(ref);
841 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
843 if (suggested_refs.nr > 0) {
844 fprintf_ln(stderr,
845 Q_("\nDid you mean this?",
846 "\nDid you mean one of these?",
847 suggested_refs.nr));
848 for (i = 0; i < suggested_refs.nr; i++)
849 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
852 string_list_clear(&suggested_refs, 0);
853 exit(1);