Merge branch 'jc/t5512-sigpipe-fix'
[git/gitster.git] / help.c
blob413c93edaea0be0dd21edc3a8c205b4d91a1a405
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"
20 #ifndef NO_CURL
21 #include "git-curl-compat.h" /* For LIBCURL_VERSION only */
22 #endif
24 struct category_description {
25 uint32_t category;
26 const char *desc;
28 static uint32_t common_mask =
29 CAT_init | CAT_worktree | CAT_info |
30 CAT_history | CAT_remote;
31 static struct category_description common_categories[] = {
32 { CAT_init, N_("start a working area (see also: git help tutorial)") },
33 { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
34 { CAT_info, N_("examine the history and state (see also: git help revisions)") },
35 { CAT_history, N_("grow, mark and tweak your common history") },
36 { CAT_remote, N_("collaborate (see also: git help workflows)") },
37 { 0, NULL }
39 static struct category_description main_categories[] = {
40 { CAT_mainporcelain, N_("Main Porcelain Commands") },
41 { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
42 { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
43 { CAT_foreignscminterface, N_("Interacting with Others") },
44 { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
45 { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
46 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
47 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
48 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
49 { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
50 { 0, NULL }
53 static const char *drop_prefix(const char *name, uint32_t category)
55 const char *new_name;
56 const char *prefix;
58 switch (category) {
59 case CAT_guide:
60 case CAT_userinterfaces:
61 case CAT_developerinterfaces:
62 prefix = "git";
63 break;
64 default:
65 prefix = "git-";
66 break;
68 if (skip_prefix(name, prefix, &new_name))
69 return new_name;
71 return name;
74 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
76 int i, nr = 0;
77 struct cmdname_help *cmds;
79 if (ARRAY_SIZE(command_list) == 0)
80 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
82 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
84 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
85 const struct cmdname_help *cmd = command_list + i;
87 if (!(cmd->category & mask))
88 continue;
90 cmds[nr] = *cmd;
91 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
93 nr++;
95 cmds[nr].name = NULL;
96 *p_cmds = cmds;
99 static void print_command_list(const struct cmdname_help *cmds,
100 uint32_t mask, int longest)
102 int i;
104 for (i = 0; cmds[i].name; i++) {
105 if (cmds[i].category & mask) {
106 size_t len = strlen(cmds[i].name);
107 printf(" %s ", cmds[i].name);
108 if (longest > len)
109 mput_char(' ', longest - len);
110 puts(_(cmds[i].help));
115 static int cmd_name_cmp(const void *elem1, const void *elem2)
117 const struct cmdname_help *e1 = elem1;
118 const struct cmdname_help *e2 = elem2;
120 return strcmp(e1->name, e2->name);
123 static void print_cmd_by_category(const struct category_description *catdesc,
124 int *longest_p)
126 struct cmdname_help *cmds;
127 int longest = 0;
128 int i, nr = 0;
129 uint32_t mask = 0;
131 for (i = 0; catdesc[i].desc; i++)
132 mask |= catdesc[i].category;
134 extract_cmds(&cmds, mask);
136 for (i = 0; cmds[i].name; i++, nr++) {
137 if (longest < strlen(cmds[i].name))
138 longest = strlen(cmds[i].name);
140 QSORT(cmds, nr, cmd_name_cmp);
142 for (i = 0; catdesc[i].desc; i++) {
143 uint32_t mask = catdesc[i].category;
144 const char *desc = catdesc[i].desc;
146 if (i)
147 putchar('\n');
148 puts(_(desc));
149 print_command_list(cmds, mask, longest);
151 free(cmds);
152 if (longest_p)
153 *longest_p = longest;
156 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
158 struct cmdname *ent;
159 FLEX_ALLOC_MEM(ent, name, name, len);
160 ent->len = len;
162 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
163 cmds->names[cmds->cnt++] = ent;
166 void cmdnames_release(struct cmdnames *cmds)
168 int i;
169 for (i = 0; i < cmds->cnt; ++i)
170 free(cmds->names[i]);
171 free(cmds->names);
172 cmds->cnt = 0;
173 cmds->alloc = 0;
176 static int cmdname_compare(const void *a_, const void *b_)
178 struct cmdname *a = *(struct cmdname **)a_;
179 struct cmdname *b = *(struct cmdname **)b_;
180 return strcmp(a->name, b->name);
183 static void uniq(struct cmdnames *cmds)
185 int i, j;
187 if (!cmds->cnt)
188 return;
190 for (i = j = 1; i < cmds->cnt; i++) {
191 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
192 free(cmds->names[i]);
193 else
194 cmds->names[j++] = cmds->names[i];
197 cmds->cnt = j;
200 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
202 int ci, cj, ei;
203 int cmp;
205 ci = cj = ei = 0;
206 while (ci < cmds->cnt && ei < excludes->cnt) {
207 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
208 if (cmp < 0)
209 cmds->names[cj++] = cmds->names[ci++];
210 else if (cmp == 0) {
211 ei++;
212 free(cmds->names[ci++]);
213 } else if (cmp > 0)
214 ei++;
217 while (ci < cmds->cnt)
218 cmds->names[cj++] = cmds->names[ci++];
220 cmds->cnt = cj;
223 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
225 struct string_list list = STRING_LIST_INIT_NODUP;
226 struct column_options copts;
227 int i;
229 for (i = 0; i < cmds->cnt; i++)
230 string_list_append(&list, cmds->names[i]->name);
232 * always enable column display, we only consult column.*
233 * about layout strategy and stuff
235 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
236 memset(&copts, 0, sizeof(copts));
237 copts.indent = " ";
238 copts.padding = 2;
239 print_columns(&list, colopts, &copts);
240 string_list_clear(&list, 0);
243 static void list_commands_in_dir(struct cmdnames *cmds,
244 const char *path,
245 const char *prefix)
247 DIR *dir = opendir(path);
248 struct dirent *de;
249 struct strbuf buf = STRBUF_INIT;
250 int len;
252 if (!dir)
253 return;
254 if (!prefix)
255 prefix = "git-";
257 strbuf_addf(&buf, "%s/", path);
258 len = buf.len;
260 while ((de = readdir(dir)) != NULL) {
261 const char *ent;
262 size_t entlen;
264 if (!skip_prefix(de->d_name, prefix, &ent))
265 continue;
267 strbuf_setlen(&buf, len);
268 strbuf_addstr(&buf, de->d_name);
269 if (!is_executable(buf.buf))
270 continue;
272 entlen = strlen(ent);
273 strip_suffix(ent, ".exe", &entlen);
275 add_cmdname(cmds, ent, entlen);
277 closedir(dir);
278 strbuf_release(&buf);
281 void load_command_list(const char *prefix,
282 struct cmdnames *main_cmds,
283 struct cmdnames *other_cmds)
285 const char *env_path = getenv("PATH");
286 const char *exec_path = git_exec_path();
288 load_builtin_commands(prefix, main_cmds);
290 if (exec_path) {
291 list_commands_in_dir(main_cmds, exec_path, prefix);
292 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
293 uniq(main_cmds);
296 if (env_path) {
297 char *paths, *path, *colon;
298 path = paths = xstrdup(env_path);
299 while (1) {
300 if ((colon = strchr(path, PATH_SEP)))
301 *colon = 0;
302 if (!exec_path || strcmp(path, exec_path))
303 list_commands_in_dir(other_cmds, path, prefix);
305 if (!colon)
306 break;
307 path = colon + 1;
309 free(paths);
311 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
312 uniq(other_cmds);
314 exclude_cmds(other_cmds, main_cmds);
317 static int get_colopts(const char *var, const char *value,
318 const struct config_context *ctx UNUSED, void *data)
320 unsigned int *colopts = data;
322 if (starts_with(var, "column."))
323 return git_column_config(var, value, "help", colopts);
325 return 0;
328 void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
330 unsigned int colopts = 0;
331 git_config(get_colopts, &colopts);
333 if (main_cmds->cnt) {
334 const char *exec_path = git_exec_path();
335 printf_ln(_("available git commands in '%s'"), exec_path);
336 putchar('\n');
337 pretty_print_cmdnames(main_cmds, colopts);
338 putchar('\n');
341 if (other_cmds->cnt) {
342 puts(_("git commands available from elsewhere on your $PATH"));
343 putchar('\n');
344 pretty_print_cmdnames(other_cmds, colopts);
345 putchar('\n');
349 void list_common_cmds_help(void)
351 puts(_("These are common Git commands used in various situations:"));
352 putchar('\n');
353 print_cmd_by_category(common_categories, NULL);
356 void list_all_main_cmds(struct string_list *list)
358 struct cmdnames main_cmds, other_cmds;
359 int i;
361 memset(&main_cmds, 0, sizeof(main_cmds));
362 memset(&other_cmds, 0, sizeof(other_cmds));
363 load_command_list("git-", &main_cmds, &other_cmds);
365 for (i = 0; i < main_cmds.cnt; i++)
366 string_list_append(list, main_cmds.names[i]->name);
368 cmdnames_release(&main_cmds);
369 cmdnames_release(&other_cmds);
372 void list_all_other_cmds(struct string_list *list)
374 struct cmdnames main_cmds, other_cmds;
375 int i;
377 memset(&main_cmds, 0, sizeof(main_cmds));
378 memset(&other_cmds, 0, sizeof(other_cmds));
379 load_command_list("git-", &main_cmds, &other_cmds);
381 for (i = 0; i < other_cmds.cnt; i++)
382 string_list_append(list, other_cmds.names[i]->name);
384 cmdnames_release(&main_cmds);
385 cmdnames_release(&other_cmds);
388 void list_cmds_by_category(struct string_list *list,
389 const char *cat)
391 int i, n = ARRAY_SIZE(command_list);
392 uint32_t cat_id = 0;
394 for (i = 0; category_names[i]; i++) {
395 if (!strcmp(cat, category_names[i])) {
396 cat_id = 1UL << i;
397 break;
400 if (!cat_id)
401 die(_("unsupported command listing type '%s'"), cat);
403 for (i = 0; i < n; i++) {
404 struct cmdname_help *cmd = command_list + i;
406 if (!(cmd->category & cat_id))
407 continue;
408 string_list_append(list, drop_prefix(cmd->name, cmd->category));
412 void list_cmds_by_config(struct string_list *list)
414 const char *cmd_list;
416 if (git_config_get_string_tmp("completion.commands", &cmd_list))
417 return;
419 string_list_sort(list);
420 string_list_remove_duplicates(list, 0);
422 while (*cmd_list) {
423 struct strbuf sb = STRBUF_INIT;
424 const char *p = strchrnul(cmd_list, ' ');
426 strbuf_add(&sb, cmd_list, p - cmd_list);
427 if (sb.buf[0] == '-')
428 string_list_remove(list, sb.buf + 1, 0);
429 else
430 string_list_insert(list, sb.buf);
431 strbuf_release(&sb);
432 while (*p == ' ')
433 p++;
434 cmd_list = p;
438 void list_guides_help(void)
440 struct category_description catdesc[] = {
441 { CAT_guide, N_("The Git concept guides are:") },
442 { 0, NULL }
444 print_cmd_by_category(catdesc, NULL);
445 putchar('\n');
448 void list_user_interfaces_help(void)
450 struct category_description catdesc[] = {
451 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
452 { 0, NULL }
454 print_cmd_by_category(catdesc, NULL);
455 putchar('\n');
458 void list_developer_interfaces_help(void)
460 struct category_description catdesc[] = {
461 { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
462 { 0, NULL }
464 print_cmd_by_category(catdesc, NULL);
465 putchar('\n');
468 static int get_alias(const char *var, const char *value,
469 const struct config_context *ctx UNUSED, void *data)
471 struct string_list *list = data;
473 if (skip_prefix(var, "alias.", &var)) {
474 if (!value)
475 return config_error_nonbool(var);
476 string_list_append(list, var)->util = xstrdup(value);
479 return 0;
482 static void list_all_cmds_help_external_commands(void)
484 struct string_list others = STRING_LIST_INIT_DUP;
485 int i;
487 list_all_other_cmds(&others);
488 if (others.nr)
489 printf("\n%s\n", _("External commands"));
490 for (i = 0; i < others.nr; i++)
491 printf(" %s\n", others.items[i].string);
492 string_list_clear(&others, 0);
495 static void list_all_cmds_help_aliases(int longest)
497 struct string_list alias_list = STRING_LIST_INIT_DUP;
498 struct cmdname_help *aliases;
499 int i;
501 git_config(get_alias, &alias_list);
502 string_list_sort(&alias_list);
504 for (i = 0; i < alias_list.nr; i++) {
505 size_t len = strlen(alias_list.items[i].string);
506 if (longest < len)
507 longest = len;
510 if (alias_list.nr) {
511 printf("\n%s\n", _("Command aliases"));
512 ALLOC_ARRAY(aliases, alias_list.nr + 1);
513 for (i = 0; i < alias_list.nr; i++) {
514 aliases[i].name = alias_list.items[i].string;
515 aliases[i].help = alias_list.items[i].util;
516 aliases[i].category = 1;
518 aliases[alias_list.nr].name = NULL;
519 print_command_list(aliases, 1, longest);
520 free(aliases);
522 string_list_clear(&alias_list, 1);
525 void list_all_cmds_help(int show_external_commands, int show_aliases)
527 int longest;
529 puts(_("See 'git help <command>' to read about a specific subcommand"));
530 putchar('\n');
531 print_cmd_by_category(main_categories, &longest);
533 if (show_external_commands)
534 list_all_cmds_help_external_commands();
535 if (show_aliases)
536 list_all_cmds_help_aliases(longest);
539 int is_in_cmdlist(struct cmdnames *c, const char *s)
541 int i;
542 for (i = 0; i < c->cnt; i++)
543 if (!strcmp(s, c->names[i]->name))
544 return 1;
545 return 0;
548 static int autocorrect;
549 static struct cmdnames aliases;
551 #define AUTOCORRECT_PROMPT (-3)
552 #define AUTOCORRECT_NEVER (-2)
553 #define AUTOCORRECT_IMMEDIATELY (-1)
555 static int git_unknown_cmd_config(const char *var, const char *value,
556 const struct config_context *ctx,
557 void *cb UNUSED)
559 const char *p;
561 if (!strcmp(var, "help.autocorrect")) {
562 if (!value)
563 return config_error_nonbool(var);
564 if (!strcmp(value, "never")) {
565 autocorrect = AUTOCORRECT_NEVER;
566 } else if (!strcmp(value, "immediate")) {
567 autocorrect = AUTOCORRECT_IMMEDIATELY;
568 } else if (!strcmp(value, "prompt")) {
569 autocorrect = AUTOCORRECT_PROMPT;
570 } else {
571 int v = git_config_int(var, value, ctx->kvi);
572 autocorrect = (v < 0)
573 ? AUTOCORRECT_IMMEDIATELY : v;
576 /* Also use aliases for command lookup */
577 if (skip_prefix(var, "alias.", &p))
578 add_cmdname(&aliases, p, strlen(p));
580 return 0;
583 static int levenshtein_compare(const void *p1, const void *p2)
585 const struct cmdname *const *c1 = p1, *const *c2 = p2;
586 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
587 int l1 = (*c1)->len;
588 int l2 = (*c2)->len;
589 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
592 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
594 int i;
595 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
597 for (i = 0; i < old->cnt; i++)
598 cmds->names[cmds->cnt++] = old->names[i];
599 FREE_AND_NULL(old->names);
600 old->cnt = 0;
603 /* An empirically derived magic number */
604 #define SIMILARITY_FLOOR 7
605 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
607 static const char bad_interpreter_advice[] =
608 N_("'%s' appears to be a git command, but we were not\n"
609 "able to execute it. Maybe git-%s is broken?");
611 const char *help_unknown_cmd(const char *cmd)
613 int i, n, best_similarity = 0;
614 struct cmdnames main_cmds, other_cmds;
615 struct cmdname_help *common_cmds;
617 memset(&main_cmds, 0, sizeof(main_cmds));
618 memset(&other_cmds, 0, sizeof(other_cmds));
619 memset(&aliases, 0, sizeof(aliases));
621 read_early_config(the_repository, git_unknown_cmd_config, NULL);
624 * Disable autocorrection prompt in a non-interactive session
626 if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
627 autocorrect = AUTOCORRECT_NEVER;
629 if (autocorrect == AUTOCORRECT_NEVER) {
630 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
631 exit(1);
634 load_command_list("git-", &main_cmds, &other_cmds);
636 add_cmd_list(&main_cmds, &aliases);
637 add_cmd_list(&main_cmds, &other_cmds);
638 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
639 uniq(&main_cmds);
641 extract_cmds(&common_cmds, common_mask);
643 /* This abuses cmdname->len for levenshtein distance */
644 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
645 int cmp = 0; /* avoid compiler stupidity */
646 const char *candidate = main_cmds.names[i]->name;
649 * An exact match means we have the command, but
650 * for some reason exec'ing it gave us ENOENT; probably
651 * it's a bad interpreter in the #! line.
653 if (!strcmp(candidate, cmd))
654 die(_(bad_interpreter_advice), cmd, cmd);
656 /* Does the candidate appear in common_cmds list? */
657 while (common_cmds[n].name &&
658 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
659 n++;
660 if (common_cmds[n].name && !cmp) {
661 /* Yes, this is one of the common commands */
662 n++; /* use the entry from common_cmds[] */
663 if (starts_with(candidate, cmd)) {
664 /* Give prefix match a very good score */
665 main_cmds.names[i]->len = 0;
666 continue;
670 main_cmds.names[i]->len =
671 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
673 FREE_AND_NULL(common_cmds);
675 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
677 if (!main_cmds.cnt)
678 die(_("Uh oh. Your system reports no Git commands at all."));
680 /* skip and count prefix matches */
681 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
682 ; /* still counting */
684 if (main_cmds.cnt <= n) {
685 /* prefix matches with everything? that is too ambiguous */
686 best_similarity = SIMILARITY_FLOOR + 1;
687 } else {
688 /* count all the most similar ones */
689 for (best_similarity = main_cmds.names[n++]->len;
690 (n < main_cmds.cnt &&
691 best_similarity == main_cmds.names[n]->len);
692 n++)
693 ; /* still counting */
695 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
696 const char *assumed = main_cmds.names[0]->name;
697 main_cmds.names[0] = NULL;
698 cmdnames_release(&main_cmds);
699 fprintf_ln(stderr,
700 _("WARNING: You called a Git command named '%s', "
701 "which does not exist."),
702 cmd);
703 if (autocorrect == AUTOCORRECT_IMMEDIATELY)
704 fprintf_ln(stderr,
705 _("Continuing under the assumption that "
706 "you meant '%s'."),
707 assumed);
708 else if (autocorrect == AUTOCORRECT_PROMPT) {
709 char *answer;
710 struct strbuf msg = STRBUF_INIT;
711 strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
712 answer = git_prompt(msg.buf, PROMPT_ECHO);
713 strbuf_release(&msg);
714 if (!(starts_with(answer, "y") ||
715 starts_with(answer, "Y")))
716 exit(1);
717 } else {
718 fprintf_ln(stderr,
719 _("Continuing in %0.1f seconds, "
720 "assuming that you meant '%s'."),
721 (float)autocorrect/10.0, assumed);
722 sleep_millisec(autocorrect * 100);
724 return assumed;
727 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
729 if (SIMILAR_ENOUGH(best_similarity)) {
730 fprintf_ln(stderr,
731 Q_("\nThe most similar command is",
732 "\nThe most similar commands are",
733 n));
735 for (i = 0; i < n; i++)
736 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
739 exit(1);
742 void get_version_info(struct strbuf *buf, int show_build_options)
745 * The format of this string should be kept stable for compatibility
746 * with external projects that rely on the output of "git version".
748 * Always show the version, even if other options are given.
750 strbuf_addf(buf, "git version %s\n", git_version_string);
752 if (show_build_options) {
753 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
754 if (git_built_from_commit_string[0])
755 strbuf_addf(buf, "built from commit: %s\n",
756 git_built_from_commit_string);
757 else
758 strbuf_addstr(buf, "no commit associated with this build\n");
759 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
760 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
761 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
762 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
764 if (fsmonitor_ipc__is_supported())
765 strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
766 #if defined LIBCURL_VERSION
767 strbuf_addf(buf, "libcurl: %s\n", LIBCURL_VERSION);
768 #endif
769 #if defined OPENSSL_VERSION_TEXT
770 strbuf_addf(buf, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT);
771 #endif
772 #if defined ZLIB_VERSION
773 strbuf_addf(buf, "zlib: %s\n", ZLIB_VERSION);
774 #endif
778 int cmd_version(int argc, const char **argv, const char *prefix)
780 struct strbuf buf = STRBUF_INIT;
781 int build_options = 0;
782 const char * const usage[] = {
783 N_("git version [--build-options]"),
784 NULL
786 struct option options[] = {
787 OPT_BOOL(0, "build-options", &build_options,
788 "also print build options"),
789 OPT_END()
792 argc = parse_options(argc, argv, prefix, options, usage, 0);
794 get_version_info(&buf, build_options);
795 printf("%s", buf.buf);
797 strbuf_release(&buf);
799 return 0;
802 struct similar_ref_cb {
803 const char *base_ref;
804 struct string_list *similar_refs;
807 static int append_similar_ref(const char *refname, const char *referent UNUSED,
808 const struct object_id *oid UNUSED,
809 int flags UNUSED, void *cb_data)
811 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
812 char *branch = strrchr(refname, '/') + 1;
814 /* A remote branch of the same name is deemed similar */
815 if (starts_with(refname, "refs/remotes/") &&
816 !strcmp(branch, cb->base_ref))
817 string_list_append_nodup(cb->similar_refs,
818 refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1));
819 return 0;
822 static struct string_list guess_refs(const char *ref)
824 struct similar_ref_cb ref_cb;
825 struct string_list similar_refs = STRING_LIST_INIT_DUP;
827 ref_cb.base_ref = ref;
828 ref_cb.similar_refs = &similar_refs;
829 refs_for_each_ref(get_main_ref_store(the_repository),
830 append_similar_ref, &ref_cb);
831 return similar_refs;
834 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
835 const char *error)
837 int i;
838 struct string_list suggested_refs = guess_refs(ref);
840 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
842 if (suggested_refs.nr > 0) {
843 fprintf_ln(stderr,
844 Q_("\nDid you mean this?",
845 "\nDid you mean one of these?",
846 suggested_refs.nr));
847 for (i = 0; i < suggested_refs.nr; i++)
848 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
851 string_list_clear(&suggested_refs, 0);
852 exit(1);