l10n: bg.po: Updated Bulgarian translation (5484t)
[git.git] / help.c
blobd04542d8261dd4e8eb2287edc3623492b34f3ce4
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "exec-cmd.h"
5 #include "run-command.h"
6 #include "levenshtein.h"
7 #include "help.h"
8 #include "command-list.h"
9 #include "string-list.h"
10 #include "column.h"
11 #include "version.h"
12 #include "refs.h"
13 #include "parse-options.h"
14 #include "prompt.h"
15 #include "fsmonitor-ipc.h"
17 struct category_description {
18 uint32_t category;
19 const char *desc;
21 static uint32_t common_mask =
22 CAT_init | CAT_worktree | CAT_info |
23 CAT_history | CAT_remote;
24 static struct category_description common_categories[] = {
25 { CAT_init, N_("start a working area (see also: git help tutorial)") },
26 { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
27 { CAT_info, N_("examine the history and state (see also: git help revisions)") },
28 { CAT_history, N_("grow, mark and tweak your common history") },
29 { CAT_remote, N_("collaborate (see also: git help workflows)") },
30 { 0, NULL }
32 static struct category_description main_categories[] = {
33 { CAT_mainporcelain, N_("Main Porcelain Commands") },
34 { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
35 { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
36 { CAT_foreignscminterface, N_("Interacting with Others") },
37 { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
38 { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
39 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
40 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
41 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
42 { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
43 { 0, NULL }
46 static const char *drop_prefix(const char *name, uint32_t category)
48 const char *new_name;
49 const char *prefix;
51 switch (category) {
52 case CAT_guide:
53 case CAT_userinterfaces:
54 case CAT_developerinterfaces:
55 prefix = "git";
56 break;
57 default:
58 prefix = "git-";
59 break;
61 if (skip_prefix(name, prefix, &new_name))
62 return new_name;
64 return name;
67 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
69 int i, nr = 0;
70 struct cmdname_help *cmds;
72 if (ARRAY_SIZE(command_list) == 0)
73 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
75 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
77 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
78 const struct cmdname_help *cmd = command_list + i;
80 if (!(cmd->category & mask))
81 continue;
83 cmds[nr] = *cmd;
84 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
86 nr++;
88 cmds[nr].name = NULL;
89 *p_cmds = cmds;
92 static void print_command_list(const struct cmdname_help *cmds,
93 uint32_t mask, int longest)
95 int i;
97 for (i = 0; cmds[i].name; i++) {
98 if (cmds[i].category & mask) {
99 size_t len = strlen(cmds[i].name);
100 printf(" %s ", cmds[i].name);
101 if (longest > len)
102 mput_char(' ', longest - len);
103 puts(_(cmds[i].help));
108 static int cmd_name_cmp(const void *elem1, const void *elem2)
110 const struct cmdname_help *e1 = elem1;
111 const struct cmdname_help *e2 = elem2;
113 return strcmp(e1->name, e2->name);
116 static void print_cmd_by_category(const struct category_description *catdesc,
117 int *longest_p)
119 struct cmdname_help *cmds;
120 int longest = 0;
121 int i, nr = 0;
122 uint32_t mask = 0;
124 for (i = 0; catdesc[i].desc; i++)
125 mask |= catdesc[i].category;
127 extract_cmds(&cmds, mask);
129 for (i = 0; cmds[i].name; i++, nr++) {
130 if (longest < strlen(cmds[i].name))
131 longest = strlen(cmds[i].name);
133 QSORT(cmds, nr, cmd_name_cmp);
135 for (i = 0; catdesc[i].desc; i++) {
136 uint32_t mask = catdesc[i].category;
137 const char *desc = catdesc[i].desc;
139 if (i)
140 putchar('\n');
141 puts(_(desc));
142 print_command_list(cmds, mask, longest);
144 free(cmds);
145 if (longest_p)
146 *longest_p = longest;
149 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
151 struct cmdname *ent;
152 FLEX_ALLOC_MEM(ent, name, name, len);
153 ent->len = len;
155 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
156 cmds->names[cmds->cnt++] = ent;
159 static void clean_cmdnames(struct cmdnames *cmds)
161 int i;
162 for (i = 0; i < cmds->cnt; ++i)
163 free(cmds->names[i]);
164 free(cmds->names);
165 cmds->cnt = 0;
166 cmds->alloc = 0;
169 static int cmdname_compare(const void *a_, const void *b_)
171 struct cmdname *a = *(struct cmdname **)a_;
172 struct cmdname *b = *(struct cmdname **)b_;
173 return strcmp(a->name, b->name);
176 static void uniq(struct cmdnames *cmds)
178 int i, j;
180 if (!cmds->cnt)
181 return;
183 for (i = j = 1; i < cmds->cnt; i++) {
184 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
185 free(cmds->names[i]);
186 else
187 cmds->names[j++] = cmds->names[i];
190 cmds->cnt = j;
193 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
195 int ci, cj, ei;
196 int cmp;
198 ci = cj = ei = 0;
199 while (ci < cmds->cnt && ei < excludes->cnt) {
200 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
201 if (cmp < 0)
202 cmds->names[cj++] = cmds->names[ci++];
203 else if (cmp == 0) {
204 ei++;
205 free(cmds->names[ci++]);
206 } else if (cmp > 0)
207 ei++;
210 while (ci < cmds->cnt)
211 cmds->names[cj++] = cmds->names[ci++];
213 cmds->cnt = cj;
216 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
218 struct string_list list = STRING_LIST_INIT_NODUP;
219 struct column_options copts;
220 int i;
222 for (i = 0; i < cmds->cnt; i++)
223 string_list_append(&list, cmds->names[i]->name);
225 * always enable column display, we only consult column.*
226 * about layout strategy and stuff
228 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
229 memset(&copts, 0, sizeof(copts));
230 copts.indent = " ";
231 copts.padding = 2;
232 print_columns(&list, colopts, &copts);
233 string_list_clear(&list, 0);
236 static void list_commands_in_dir(struct cmdnames *cmds,
237 const char *path,
238 const char *prefix)
240 DIR *dir = opendir(path);
241 struct dirent *de;
242 struct strbuf buf = STRBUF_INIT;
243 int len;
245 if (!dir)
246 return;
247 if (!prefix)
248 prefix = "git-";
250 strbuf_addf(&buf, "%s/", path);
251 len = buf.len;
253 while ((de = readdir(dir)) != NULL) {
254 const char *ent;
255 size_t entlen;
257 if (!skip_prefix(de->d_name, prefix, &ent))
258 continue;
260 strbuf_setlen(&buf, len);
261 strbuf_addstr(&buf, de->d_name);
262 if (!is_executable(buf.buf))
263 continue;
265 entlen = strlen(ent);
266 strip_suffix(ent, ".exe", &entlen);
268 add_cmdname(cmds, ent, entlen);
270 closedir(dir);
271 strbuf_release(&buf);
274 void load_command_list(const char *prefix,
275 struct cmdnames *main_cmds,
276 struct cmdnames *other_cmds)
278 const char *env_path = getenv("PATH");
279 const char *exec_path = git_exec_path();
281 load_builtin_commands(prefix, main_cmds);
283 if (exec_path) {
284 list_commands_in_dir(main_cmds, exec_path, prefix);
285 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
286 uniq(main_cmds);
289 if (env_path) {
290 char *paths, *path, *colon;
291 path = paths = xstrdup(env_path);
292 while (1) {
293 if ((colon = strchr(path, PATH_SEP)))
294 *colon = 0;
295 if (!exec_path || strcmp(path, exec_path))
296 list_commands_in_dir(other_cmds, path, prefix);
298 if (!colon)
299 break;
300 path = colon + 1;
302 free(paths);
304 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
305 uniq(other_cmds);
307 exclude_cmds(other_cmds, main_cmds);
310 static int get_colopts(const char *var, const char *value, void *data)
312 unsigned int *colopts = data;
314 if (starts_with(var, "column."))
315 return git_column_config(var, value, "help", colopts);
317 return 0;
320 void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
322 unsigned int colopts = 0;
323 git_config(get_colopts, &colopts);
325 if (main_cmds->cnt) {
326 const char *exec_path = git_exec_path();
327 printf_ln(_("available git commands in '%s'"), exec_path);
328 putchar('\n');
329 pretty_print_cmdnames(main_cmds, colopts);
330 putchar('\n');
333 if (other_cmds->cnt) {
334 puts(_("git commands available from elsewhere on your $PATH"));
335 putchar('\n');
336 pretty_print_cmdnames(other_cmds, colopts);
337 putchar('\n');
341 void list_common_cmds_help(void)
343 puts(_("These are common Git commands used in various situations:"));
344 putchar('\n');
345 print_cmd_by_category(common_categories, NULL);
348 void list_all_main_cmds(struct string_list *list)
350 struct cmdnames main_cmds, other_cmds;
351 int i;
353 memset(&main_cmds, 0, sizeof(main_cmds));
354 memset(&other_cmds, 0, sizeof(other_cmds));
355 load_command_list("git-", &main_cmds, &other_cmds);
357 for (i = 0; i < main_cmds.cnt; i++)
358 string_list_append(list, main_cmds.names[i]->name);
360 clean_cmdnames(&main_cmds);
361 clean_cmdnames(&other_cmds);
364 void list_all_other_cmds(struct string_list *list)
366 struct cmdnames main_cmds, other_cmds;
367 int i;
369 memset(&main_cmds, 0, sizeof(main_cmds));
370 memset(&other_cmds, 0, sizeof(other_cmds));
371 load_command_list("git-", &main_cmds, &other_cmds);
373 for (i = 0; i < other_cmds.cnt; i++)
374 string_list_append(list, other_cmds.names[i]->name);
376 clean_cmdnames(&main_cmds);
377 clean_cmdnames(&other_cmds);
380 void list_cmds_by_category(struct string_list *list,
381 const char *cat)
383 int i, n = ARRAY_SIZE(command_list);
384 uint32_t cat_id = 0;
386 for (i = 0; category_names[i]; i++) {
387 if (!strcmp(cat, category_names[i])) {
388 cat_id = 1UL << i;
389 break;
392 if (!cat_id)
393 die(_("unsupported command listing type '%s'"), cat);
395 for (i = 0; i < n; i++) {
396 struct cmdname_help *cmd = command_list + i;
398 if (!(cmd->category & cat_id))
399 continue;
400 string_list_append(list, drop_prefix(cmd->name, cmd->category));
404 void list_cmds_by_config(struct string_list *list)
406 const char *cmd_list;
408 if (git_config_get_string_tmp("completion.commands", &cmd_list))
409 return;
411 string_list_sort(list);
412 string_list_remove_duplicates(list, 0);
414 while (*cmd_list) {
415 struct strbuf sb = STRBUF_INIT;
416 const char *p = strchrnul(cmd_list, ' ');
418 strbuf_add(&sb, cmd_list, p - cmd_list);
419 if (sb.buf[0] == '-')
420 string_list_remove(list, sb.buf + 1, 0);
421 else
422 string_list_insert(list, sb.buf);
423 strbuf_release(&sb);
424 while (*p == ' ')
425 p++;
426 cmd_list = p;
430 void list_guides_help(void)
432 struct category_description catdesc[] = {
433 { CAT_guide, N_("The Git concept guides are:") },
434 { 0, NULL }
436 print_cmd_by_category(catdesc, NULL);
437 putchar('\n');
440 void list_user_interfaces_help(void)
442 struct category_description catdesc[] = {
443 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
444 { 0, NULL }
446 print_cmd_by_category(catdesc, NULL);
447 putchar('\n');
450 void list_developer_interfaces_help(void)
452 struct category_description catdesc[] = {
453 { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
454 { 0, NULL }
456 print_cmd_by_category(catdesc, NULL);
457 putchar('\n');
460 static int get_alias(const char *var, const char *value, void *data)
462 struct string_list *list = data;
464 if (skip_prefix(var, "alias.", &var))
465 string_list_append(list, var)->util = xstrdup(value);
467 return 0;
470 static void list_all_cmds_help_external_commands(void)
472 struct string_list others = STRING_LIST_INIT_DUP;
473 int i;
475 list_all_other_cmds(&others);
476 if (others.nr)
477 printf("\n%s\n", _("External commands"));
478 for (i = 0; i < others.nr; i++)
479 printf(" %s\n", others.items[i].string);
480 string_list_clear(&others, 0);
483 static void list_all_cmds_help_aliases(int longest)
485 struct string_list alias_list = STRING_LIST_INIT_DUP;
486 struct cmdname_help *aliases;
487 int i;
489 git_config(get_alias, &alias_list);
490 string_list_sort(&alias_list);
492 for (i = 0; i < alias_list.nr; i++) {
493 size_t len = strlen(alias_list.items[i].string);
494 if (longest < len)
495 longest = len;
498 if (alias_list.nr) {
499 printf("\n%s\n", _("Command aliases"));
500 ALLOC_ARRAY(aliases, alias_list.nr + 1);
501 for (i = 0; i < alias_list.nr; i++) {
502 aliases[i].name = alias_list.items[i].string;
503 aliases[i].help = alias_list.items[i].util;
504 aliases[i].category = 1;
506 aliases[alias_list.nr].name = NULL;
507 print_command_list(aliases, 1, longest);
508 free(aliases);
510 string_list_clear(&alias_list, 1);
513 void list_all_cmds_help(int show_external_commands, int show_aliases)
515 int longest;
517 puts(_("See 'git help <command>' to read about a specific subcommand"));
518 putchar('\n');
519 print_cmd_by_category(main_categories, &longest);
521 if (show_external_commands)
522 list_all_cmds_help_external_commands();
523 if (show_aliases)
524 list_all_cmds_help_aliases(longest);
527 int is_in_cmdlist(struct cmdnames *c, const char *s)
529 int i;
530 for (i = 0; i < c->cnt; i++)
531 if (!strcmp(s, c->names[i]->name))
532 return 1;
533 return 0;
536 static int autocorrect;
537 static struct cmdnames aliases;
539 #define AUTOCORRECT_PROMPT (-3)
540 #define AUTOCORRECT_NEVER (-2)
541 #define AUTOCORRECT_IMMEDIATELY (-1)
543 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
545 const char *p;
547 if (!strcmp(var, "help.autocorrect")) {
548 if (!value)
549 return config_error_nonbool(var);
550 if (!strcmp(value, "never")) {
551 autocorrect = AUTOCORRECT_NEVER;
552 } else if (!strcmp(value, "immediate")) {
553 autocorrect = AUTOCORRECT_IMMEDIATELY;
554 } else if (!strcmp(value, "prompt")) {
555 autocorrect = AUTOCORRECT_PROMPT;
556 } else {
557 int v = git_config_int(var, value);
558 autocorrect = (v < 0)
559 ? AUTOCORRECT_IMMEDIATELY : v;
562 /* Also use aliases for command lookup */
563 if (skip_prefix(var, "alias.", &p))
564 add_cmdname(&aliases, p, strlen(p));
566 return git_default_config(var, value, cb);
569 static int levenshtein_compare(const void *p1, const void *p2)
571 const struct cmdname *const *c1 = p1, *const *c2 = p2;
572 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
573 int l1 = (*c1)->len;
574 int l2 = (*c2)->len;
575 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
578 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
580 int i;
581 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
583 for (i = 0; i < old->cnt; i++)
584 cmds->names[cmds->cnt++] = old->names[i];
585 FREE_AND_NULL(old->names);
586 old->cnt = 0;
589 /* An empirically derived magic number */
590 #define SIMILARITY_FLOOR 7
591 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
593 static const char bad_interpreter_advice[] =
594 N_("'%s' appears to be a git command, but we were not\n"
595 "able to execute it. Maybe git-%s is broken?");
597 const char *help_unknown_cmd(const char *cmd)
599 int i, n, best_similarity = 0;
600 struct cmdnames main_cmds, other_cmds;
601 struct cmdname_help *common_cmds;
603 memset(&main_cmds, 0, sizeof(main_cmds));
604 memset(&other_cmds, 0, sizeof(other_cmds));
605 memset(&aliases, 0, sizeof(aliases));
607 read_early_config(git_unknown_cmd_config, NULL);
610 * Disable autocorrection prompt in a non-interactive session
612 if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
613 autocorrect = AUTOCORRECT_NEVER;
615 if (autocorrect == AUTOCORRECT_NEVER) {
616 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
617 exit(1);
620 load_command_list("git-", &main_cmds, &other_cmds);
622 add_cmd_list(&main_cmds, &aliases);
623 add_cmd_list(&main_cmds, &other_cmds);
624 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
625 uniq(&main_cmds);
627 extract_cmds(&common_cmds, common_mask);
629 /* This abuses cmdname->len for levenshtein distance */
630 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
631 int cmp = 0; /* avoid compiler stupidity */
632 const char *candidate = main_cmds.names[i]->name;
635 * An exact match means we have the command, but
636 * for some reason exec'ing it gave us ENOENT; probably
637 * it's a bad interpreter in the #! line.
639 if (!strcmp(candidate, cmd))
640 die(_(bad_interpreter_advice), cmd, cmd);
642 /* Does the candidate appear in common_cmds list? */
643 while (common_cmds[n].name &&
644 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
645 n++;
646 if (common_cmds[n].name && !cmp) {
647 /* Yes, this is one of the common commands */
648 n++; /* use the entry from common_cmds[] */
649 if (starts_with(candidate, cmd)) {
650 /* Give prefix match a very good score */
651 main_cmds.names[i]->len = 0;
652 continue;
656 main_cmds.names[i]->len =
657 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
659 FREE_AND_NULL(common_cmds);
661 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
663 if (!main_cmds.cnt)
664 die(_("Uh oh. Your system reports no Git commands at all."));
666 /* skip and count prefix matches */
667 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
668 ; /* still counting */
670 if (main_cmds.cnt <= n) {
671 /* prefix matches with everything? that is too ambiguous */
672 best_similarity = SIMILARITY_FLOOR + 1;
673 } else {
674 /* count all the most similar ones */
675 for (best_similarity = main_cmds.names[n++]->len;
676 (n < main_cmds.cnt &&
677 best_similarity == main_cmds.names[n]->len);
678 n++)
679 ; /* still counting */
681 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
682 const char *assumed = main_cmds.names[0]->name;
683 main_cmds.names[0] = NULL;
684 clean_cmdnames(&main_cmds);
685 fprintf_ln(stderr,
686 _("WARNING: You called a Git command named '%s', "
687 "which does not exist."),
688 cmd);
689 if (autocorrect == AUTOCORRECT_IMMEDIATELY)
690 fprintf_ln(stderr,
691 _("Continuing under the assumption that "
692 "you meant '%s'."),
693 assumed);
694 else if (autocorrect == AUTOCORRECT_PROMPT) {
695 char *answer;
696 struct strbuf msg = STRBUF_INIT;
697 strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
698 answer = git_prompt(msg.buf, PROMPT_ECHO);
699 strbuf_release(&msg);
700 if (!(starts_with(answer, "y") ||
701 starts_with(answer, "Y")))
702 exit(1);
703 } else {
704 fprintf_ln(stderr,
705 _("Continuing in %0.1f seconds, "
706 "assuming that you meant '%s'."),
707 (float)autocorrect/10.0, assumed);
708 sleep_millisec(autocorrect * 100);
710 return assumed;
713 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
715 if (SIMILAR_ENOUGH(best_similarity)) {
716 fprintf_ln(stderr,
717 Q_("\nThe most similar command is",
718 "\nThe most similar commands are",
719 n));
721 for (i = 0; i < n; i++)
722 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
725 exit(1);
728 void get_version_info(struct strbuf *buf, int show_build_options)
731 * The format of this string should be kept stable for compatibility
732 * with external projects that rely on the output of "git version".
734 * Always show the version, even if other options are given.
736 strbuf_addf(buf, "git version %s\n", git_version_string);
738 if (show_build_options) {
739 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
740 if (git_built_from_commit_string[0])
741 strbuf_addf(buf, "built from commit: %s\n",
742 git_built_from_commit_string);
743 else
744 strbuf_addstr(buf, "no commit associated with this build\n");
745 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
746 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
747 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
748 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
750 if (fsmonitor_ipc__is_supported())
751 strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
755 int cmd_version(int argc, const char **argv, const char *prefix)
757 struct strbuf buf = STRBUF_INIT;
758 int build_options = 0;
759 const char * const usage[] = {
760 N_("git version [<options>]"),
761 NULL
763 struct option options[] = {
764 OPT_BOOL(0, "build-options", &build_options,
765 "also print build options"),
766 OPT_END()
769 argc = parse_options(argc, argv, prefix, options, usage, 0);
771 get_version_info(&buf, build_options);
772 printf("%s", buf.buf);
774 strbuf_release(&buf);
776 return 0;
779 struct similar_ref_cb {
780 const char *base_ref;
781 struct string_list *similar_refs;
784 static int append_similar_ref(const char *refname,
785 const struct object_id *oid UNUSED,
786 int flags UNUSED, void *cb_data)
788 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
789 char *branch = strrchr(refname, '/') + 1;
791 /* A remote branch of the same name is deemed similar */
792 if (starts_with(refname, "refs/remotes/") &&
793 !strcmp(branch, cb->base_ref))
794 string_list_append_nodup(cb->similar_refs,
795 shorten_unambiguous_ref(refname, 1));
796 return 0;
799 static struct string_list guess_refs(const char *ref)
801 struct similar_ref_cb ref_cb;
802 struct string_list similar_refs = STRING_LIST_INIT_DUP;
804 ref_cb.base_ref = ref;
805 ref_cb.similar_refs = &similar_refs;
806 for_each_ref(append_similar_ref, &ref_cb);
807 return similar_refs;
810 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
811 const char *error)
813 int i;
814 struct string_list suggested_refs = guess_refs(ref);
816 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
818 if (suggested_refs.nr > 0) {
819 fprintf_ln(stderr,
820 Q_("\nDid you mean this?",
821 "\nDid you mean one of these?",
822 suggested_refs.nr));
823 for (i = 0; i < suggested_refs.nr; i++)
824 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
827 string_list_clear(&suggested_refs, 0);
828 exit(1);