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