treewide: reduce includes of cache.h in other headers
[git.git] / help.c
blob5d7637dce92867c1c0bee7f6f6b904852d237222
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "builtin.h"
5 #include "exec-cmd.h"
6 #include "run-command.h"
7 #include "levenshtein.h"
8 #include "gettext.h"
9 #include "help.h"
10 #include "command-list.h"
11 #include "string-list.h"
12 #include "column.h"
13 #include "version.h"
14 #include "refs.h"
15 #include "parse-options.h"
16 #include "prompt.h"
17 #include "fsmonitor-ipc.h"
19 struct category_description {
20 uint32_t category;
21 const char *desc;
23 static uint32_t common_mask =
24 CAT_init | CAT_worktree | CAT_info |
25 CAT_history | CAT_remote;
26 static struct category_description common_categories[] = {
27 { CAT_init, N_("start a working area (see also: git help tutorial)") },
28 { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
29 { CAT_info, N_("examine the history and state (see also: git help revisions)") },
30 { CAT_history, N_("grow, mark and tweak your common history") },
31 { CAT_remote, N_("collaborate (see also: git help workflows)") },
32 { 0, NULL }
34 static struct category_description main_categories[] = {
35 { CAT_mainporcelain, N_("Main Porcelain Commands") },
36 { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
37 { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
38 { CAT_foreignscminterface, N_("Interacting with Others") },
39 { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
40 { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
41 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
42 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
43 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
44 { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
45 { 0, NULL }
48 static const char *drop_prefix(const char *name, uint32_t category)
50 const char *new_name;
51 const char *prefix;
53 switch (category) {
54 case CAT_guide:
55 case CAT_userinterfaces:
56 case CAT_developerinterfaces:
57 prefix = "git";
58 break;
59 default:
60 prefix = "git-";
61 break;
63 if (skip_prefix(name, prefix, &new_name))
64 return new_name;
66 return name;
69 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
71 int i, nr = 0;
72 struct cmdname_help *cmds;
74 if (ARRAY_SIZE(command_list) == 0)
75 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
77 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
79 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
80 const struct cmdname_help *cmd = command_list + i;
82 if (!(cmd->category & mask))
83 continue;
85 cmds[nr] = *cmd;
86 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
88 nr++;
90 cmds[nr].name = NULL;
91 *p_cmds = cmds;
94 static void print_command_list(const struct cmdname_help *cmds,
95 uint32_t mask, int longest)
97 int i;
99 for (i = 0; cmds[i].name; i++) {
100 if (cmds[i].category & mask) {
101 size_t len = strlen(cmds[i].name);
102 printf(" %s ", cmds[i].name);
103 if (longest > len)
104 mput_char(' ', longest - len);
105 puts(_(cmds[i].help));
110 static int cmd_name_cmp(const void *elem1, const void *elem2)
112 const struct cmdname_help *e1 = elem1;
113 const struct cmdname_help *e2 = elem2;
115 return strcmp(e1->name, e2->name);
118 static void print_cmd_by_category(const struct category_description *catdesc,
119 int *longest_p)
121 struct cmdname_help *cmds;
122 int longest = 0;
123 int i, nr = 0;
124 uint32_t mask = 0;
126 for (i = 0; catdesc[i].desc; i++)
127 mask |= catdesc[i].category;
129 extract_cmds(&cmds, mask);
131 for (i = 0; cmds[i].name; i++, nr++) {
132 if (longest < strlen(cmds[i].name))
133 longest = strlen(cmds[i].name);
135 QSORT(cmds, nr, cmd_name_cmp);
137 for (i = 0; catdesc[i].desc; i++) {
138 uint32_t mask = catdesc[i].category;
139 const char *desc = catdesc[i].desc;
141 if (i)
142 putchar('\n');
143 puts(_(desc));
144 print_command_list(cmds, mask, longest);
146 free(cmds);
147 if (longest_p)
148 *longest_p = longest;
151 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
153 struct cmdname *ent;
154 FLEX_ALLOC_MEM(ent, name, name, len);
155 ent->len = len;
157 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
158 cmds->names[cmds->cnt++] = ent;
161 static void clean_cmdnames(struct cmdnames *cmds)
163 int i;
164 for (i = 0; i < cmds->cnt; ++i)
165 free(cmds->names[i]);
166 free(cmds->names);
167 cmds->cnt = 0;
168 cmds->alloc = 0;
171 static int cmdname_compare(const void *a_, const void *b_)
173 struct cmdname *a = *(struct cmdname **)a_;
174 struct cmdname *b = *(struct cmdname **)b_;
175 return strcmp(a->name, b->name);
178 static void uniq(struct cmdnames *cmds)
180 int i, j;
182 if (!cmds->cnt)
183 return;
185 for (i = j = 1; i < cmds->cnt; i++) {
186 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
187 free(cmds->names[i]);
188 else
189 cmds->names[j++] = cmds->names[i];
192 cmds->cnt = j;
195 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
197 int ci, cj, ei;
198 int cmp;
200 ci = cj = ei = 0;
201 while (ci < cmds->cnt && ei < excludes->cnt) {
202 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
203 if (cmp < 0)
204 cmds->names[cj++] = cmds->names[ci++];
205 else if (cmp == 0) {
206 ei++;
207 free(cmds->names[ci++]);
208 } else if (cmp > 0)
209 ei++;
212 while (ci < cmds->cnt)
213 cmds->names[cj++] = cmds->names[ci++];
215 cmds->cnt = cj;
218 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
220 struct string_list list = STRING_LIST_INIT_NODUP;
221 struct column_options copts;
222 int i;
224 for (i = 0; i < cmds->cnt; i++)
225 string_list_append(&list, cmds->names[i]->name);
227 * always enable column display, we only consult column.*
228 * about layout strategy and stuff
230 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
231 memset(&copts, 0, sizeof(copts));
232 copts.indent = " ";
233 copts.padding = 2;
234 print_columns(&list, colopts, &copts);
235 string_list_clear(&list, 0);
238 static void list_commands_in_dir(struct cmdnames *cmds,
239 const char *path,
240 const char *prefix)
242 DIR *dir = opendir(path);
243 struct dirent *de;
244 struct strbuf buf = STRBUF_INIT;
245 int len;
247 if (!dir)
248 return;
249 if (!prefix)
250 prefix = "git-";
252 strbuf_addf(&buf, "%s/", path);
253 len = buf.len;
255 while ((de = readdir(dir)) != NULL) {
256 const char *ent;
257 size_t entlen;
259 if (!skip_prefix(de->d_name, prefix, &ent))
260 continue;
262 strbuf_setlen(&buf, len);
263 strbuf_addstr(&buf, de->d_name);
264 if (!is_executable(buf.buf))
265 continue;
267 entlen = strlen(ent);
268 strip_suffix(ent, ".exe", &entlen);
270 add_cmdname(cmds, ent, entlen);
272 closedir(dir);
273 strbuf_release(&buf);
276 void load_command_list(const char *prefix,
277 struct cmdnames *main_cmds,
278 struct cmdnames *other_cmds)
280 const char *env_path = getenv("PATH");
281 const char *exec_path = git_exec_path();
283 load_builtin_commands(prefix, main_cmds);
285 if (exec_path) {
286 list_commands_in_dir(main_cmds, exec_path, prefix);
287 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
288 uniq(main_cmds);
291 if (env_path) {
292 char *paths, *path, *colon;
293 path = paths = xstrdup(env_path);
294 while (1) {
295 if ((colon = strchr(path, PATH_SEP)))
296 *colon = 0;
297 if (!exec_path || strcmp(path, exec_path))
298 list_commands_in_dir(other_cmds, path, prefix);
300 if (!colon)
301 break;
302 path = colon + 1;
304 free(paths);
306 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
307 uniq(other_cmds);
309 exclude_cmds(other_cmds, main_cmds);
312 static int get_colopts(const char *var, const char *value, void *data)
314 unsigned int *colopts = data;
316 if (starts_with(var, "column."))
317 return git_column_config(var, value, "help", colopts);
319 return 0;
322 void list_commands(struct cmdnames *main_cmds, struct cmdnames *other_cmds)
324 unsigned int colopts = 0;
325 git_config(get_colopts, &colopts);
327 if (main_cmds->cnt) {
328 const char *exec_path = git_exec_path();
329 printf_ln(_("available git commands in '%s'"), exec_path);
330 putchar('\n');
331 pretty_print_cmdnames(main_cmds, colopts);
332 putchar('\n');
335 if (other_cmds->cnt) {
336 puts(_("git commands available from elsewhere on your $PATH"));
337 putchar('\n');
338 pretty_print_cmdnames(other_cmds, colopts);
339 putchar('\n');
343 void list_common_cmds_help(void)
345 puts(_("These are common Git commands used in various situations:"));
346 putchar('\n');
347 print_cmd_by_category(common_categories, NULL);
350 void list_all_main_cmds(struct string_list *list)
352 struct cmdnames main_cmds, other_cmds;
353 int i;
355 memset(&main_cmds, 0, sizeof(main_cmds));
356 memset(&other_cmds, 0, sizeof(other_cmds));
357 load_command_list("git-", &main_cmds, &other_cmds);
359 for (i = 0; i < main_cmds.cnt; i++)
360 string_list_append(list, main_cmds.names[i]->name);
362 clean_cmdnames(&main_cmds);
363 clean_cmdnames(&other_cmds);
366 void list_all_other_cmds(struct string_list *list)
368 struct cmdnames main_cmds, other_cmds;
369 int i;
371 memset(&main_cmds, 0, sizeof(main_cmds));
372 memset(&other_cmds, 0, sizeof(other_cmds));
373 load_command_list("git-", &main_cmds, &other_cmds);
375 for (i = 0; i < other_cmds.cnt; i++)
376 string_list_append(list, other_cmds.names[i]->name);
378 clean_cmdnames(&main_cmds);
379 clean_cmdnames(&other_cmds);
382 void list_cmds_by_category(struct string_list *list,
383 const char *cat)
385 int i, n = ARRAY_SIZE(command_list);
386 uint32_t cat_id = 0;
388 for (i = 0; category_names[i]; i++) {
389 if (!strcmp(cat, category_names[i])) {
390 cat_id = 1UL << i;
391 break;
394 if (!cat_id)
395 die(_("unsupported command listing type '%s'"), cat);
397 for (i = 0; i < n; i++) {
398 struct cmdname_help *cmd = command_list + i;
400 if (!(cmd->category & cat_id))
401 continue;
402 string_list_append(list, drop_prefix(cmd->name, cmd->category));
406 void list_cmds_by_config(struct string_list *list)
408 const char *cmd_list;
410 if (git_config_get_string_tmp("completion.commands", &cmd_list))
411 return;
413 string_list_sort(list);
414 string_list_remove_duplicates(list, 0);
416 while (*cmd_list) {
417 struct strbuf sb = STRBUF_INIT;
418 const char *p = strchrnul(cmd_list, ' ');
420 strbuf_add(&sb, cmd_list, p - cmd_list);
421 if (sb.buf[0] == '-')
422 string_list_remove(list, sb.buf + 1, 0);
423 else
424 string_list_insert(list, sb.buf);
425 strbuf_release(&sb);
426 while (*p == ' ')
427 p++;
428 cmd_list = p;
432 void list_guides_help(void)
434 struct category_description catdesc[] = {
435 { CAT_guide, N_("The Git concept guides are:") },
436 { 0, NULL }
438 print_cmd_by_category(catdesc, NULL);
439 putchar('\n');
442 void list_user_interfaces_help(void)
444 struct category_description catdesc[] = {
445 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces:") },
446 { 0, NULL }
448 print_cmd_by_category(catdesc, NULL);
449 putchar('\n');
452 void list_developer_interfaces_help(void)
454 struct category_description catdesc[] = {
455 { CAT_developerinterfaces, N_("File formats, protocols and other developer interfaces:") },
456 { 0, NULL }
458 print_cmd_by_category(catdesc, NULL);
459 putchar('\n');
462 static int get_alias(const char *var, const char *value, void *data)
464 struct string_list *list = data;
466 if (skip_prefix(var, "alias.", &var))
467 string_list_append(list, var)->util = xstrdup(value);
469 return 0;
472 static void list_all_cmds_help_external_commands(void)
474 struct string_list others = STRING_LIST_INIT_DUP;
475 int i;
477 list_all_other_cmds(&others);
478 if (others.nr)
479 printf("\n%s\n", _("External commands"));
480 for (i = 0; i < others.nr; i++)
481 printf(" %s\n", others.items[i].string);
482 string_list_clear(&others, 0);
485 static void list_all_cmds_help_aliases(int longest)
487 struct string_list alias_list = STRING_LIST_INIT_DUP;
488 struct cmdname_help *aliases;
489 int i;
491 git_config(get_alias, &alias_list);
492 string_list_sort(&alias_list);
494 for (i = 0; i < alias_list.nr; i++) {
495 size_t len = strlen(alias_list.items[i].string);
496 if (longest < len)
497 longest = len;
500 if (alias_list.nr) {
501 printf("\n%s\n", _("Command aliases"));
502 ALLOC_ARRAY(aliases, alias_list.nr + 1);
503 for (i = 0; i < alias_list.nr; i++) {
504 aliases[i].name = alias_list.items[i].string;
505 aliases[i].help = alias_list.items[i].util;
506 aliases[i].category = 1;
508 aliases[alias_list.nr].name = NULL;
509 print_command_list(aliases, 1, longest);
510 free(aliases);
512 string_list_clear(&alias_list, 1);
515 void list_all_cmds_help(int show_external_commands, int show_aliases)
517 int longest;
519 puts(_("See 'git help <command>' to read about a specific subcommand"));
520 putchar('\n');
521 print_cmd_by_category(main_categories, &longest);
523 if (show_external_commands)
524 list_all_cmds_help_external_commands();
525 if (show_aliases)
526 list_all_cmds_help_aliases(longest);
529 int is_in_cmdlist(struct cmdnames *c, const char *s)
531 int i;
532 for (i = 0; i < c->cnt; i++)
533 if (!strcmp(s, c->names[i]->name))
534 return 1;
535 return 0;
538 static int autocorrect;
539 static struct cmdnames aliases;
541 #define AUTOCORRECT_PROMPT (-3)
542 #define AUTOCORRECT_NEVER (-2)
543 #define AUTOCORRECT_IMMEDIATELY (-1)
545 static int git_unknown_cmd_config(const char *var, const char *value,
546 void *cb UNUSED)
548 const char *p;
550 if (!strcmp(var, "help.autocorrect")) {
551 if (!value)
552 return config_error_nonbool(var);
553 if (!strcmp(value, "never")) {
554 autocorrect = AUTOCORRECT_NEVER;
555 } else if (!strcmp(value, "immediate")) {
556 autocorrect = AUTOCORRECT_IMMEDIATELY;
557 } else if (!strcmp(value, "prompt")) {
558 autocorrect = AUTOCORRECT_PROMPT;
559 } else {
560 int v = git_config_int(var, value);
561 autocorrect = (v < 0)
562 ? AUTOCORRECT_IMMEDIATELY : v;
565 /* Also use aliases for command lookup */
566 if (skip_prefix(var, "alias.", &p))
567 add_cmdname(&aliases, p, strlen(p));
569 return 0;
572 static int levenshtein_compare(const void *p1, const void *p2)
574 const struct cmdname *const *c1 = p1, *const *c2 = p2;
575 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
576 int l1 = (*c1)->len;
577 int l2 = (*c2)->len;
578 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
581 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
583 int i;
584 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
586 for (i = 0; i < old->cnt; i++)
587 cmds->names[cmds->cnt++] = old->names[i];
588 FREE_AND_NULL(old->names);
589 old->cnt = 0;
592 /* An empirically derived magic number */
593 #define SIMILARITY_FLOOR 7
594 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
596 static const char bad_interpreter_advice[] =
597 N_("'%s' appears to be a git command, but we were not\n"
598 "able to execute it. Maybe git-%s is broken?");
600 const char *help_unknown_cmd(const char *cmd)
602 int i, n, best_similarity = 0;
603 struct cmdnames main_cmds, other_cmds;
604 struct cmdname_help *common_cmds;
606 memset(&main_cmds, 0, sizeof(main_cmds));
607 memset(&other_cmds, 0, sizeof(other_cmds));
608 memset(&aliases, 0, sizeof(aliases));
610 read_early_config(git_unknown_cmd_config, NULL);
613 * Disable autocorrection prompt in a non-interactive session
615 if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
616 autocorrect = AUTOCORRECT_NEVER;
618 if (autocorrect == AUTOCORRECT_NEVER) {
619 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
620 exit(1);
623 load_command_list("git-", &main_cmds, &other_cmds);
625 add_cmd_list(&main_cmds, &aliases);
626 add_cmd_list(&main_cmds, &other_cmds);
627 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
628 uniq(&main_cmds);
630 extract_cmds(&common_cmds, common_mask);
632 /* This abuses cmdname->len for levenshtein distance */
633 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
634 int cmp = 0; /* avoid compiler stupidity */
635 const char *candidate = main_cmds.names[i]->name;
638 * An exact match means we have the command, but
639 * for some reason exec'ing it gave us ENOENT; probably
640 * it's a bad interpreter in the #! line.
642 if (!strcmp(candidate, cmd))
643 die(_(bad_interpreter_advice), cmd, cmd);
645 /* Does the candidate appear in common_cmds list? */
646 while (common_cmds[n].name &&
647 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
648 n++;
649 if (common_cmds[n].name && !cmp) {
650 /* Yes, this is one of the common commands */
651 n++; /* use the entry from common_cmds[] */
652 if (starts_with(candidate, cmd)) {
653 /* Give prefix match a very good score */
654 main_cmds.names[i]->len = 0;
655 continue;
659 main_cmds.names[i]->len =
660 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
662 FREE_AND_NULL(common_cmds);
664 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
666 if (!main_cmds.cnt)
667 die(_("Uh oh. Your system reports no Git commands at all."));
669 /* skip and count prefix matches */
670 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
671 ; /* still counting */
673 if (main_cmds.cnt <= n) {
674 /* prefix matches with everything? that is too ambiguous */
675 best_similarity = SIMILARITY_FLOOR + 1;
676 } else {
677 /* count all the most similar ones */
678 for (best_similarity = main_cmds.names[n++]->len;
679 (n < main_cmds.cnt &&
680 best_similarity == main_cmds.names[n]->len);
681 n++)
682 ; /* still counting */
684 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
685 const char *assumed = main_cmds.names[0]->name;
686 main_cmds.names[0] = NULL;
687 clean_cmdnames(&main_cmds);
688 fprintf_ln(stderr,
689 _("WARNING: You called a Git command named '%s', "
690 "which does not exist."),
691 cmd);
692 if (autocorrect == AUTOCORRECT_IMMEDIATELY)
693 fprintf_ln(stderr,
694 _("Continuing under the assumption that "
695 "you meant '%s'."),
696 assumed);
697 else if (autocorrect == AUTOCORRECT_PROMPT) {
698 char *answer;
699 struct strbuf msg = STRBUF_INIT;
700 strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
701 answer = git_prompt(msg.buf, PROMPT_ECHO);
702 strbuf_release(&msg);
703 if (!(starts_with(answer, "y") ||
704 starts_with(answer, "Y")))
705 exit(1);
706 } else {
707 fprintf_ln(stderr,
708 _("Continuing in %0.1f seconds, "
709 "assuming that you meant '%s'."),
710 (float)autocorrect/10.0, assumed);
711 sleep_millisec(autocorrect * 100);
713 return assumed;
716 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
718 if (SIMILAR_ENOUGH(best_similarity)) {
719 fprintf_ln(stderr,
720 Q_("\nThe most similar command is",
721 "\nThe most similar commands are",
722 n));
724 for (i = 0; i < n; i++)
725 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
728 exit(1);
731 void get_version_info(struct strbuf *buf, int show_build_options)
734 * The format of this string should be kept stable for compatibility
735 * with external projects that rely on the output of "git version".
737 * Always show the version, even if other options are given.
739 strbuf_addf(buf, "git version %s\n", git_version_string);
741 if (show_build_options) {
742 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
743 if (git_built_from_commit_string[0])
744 strbuf_addf(buf, "built from commit: %s\n",
745 git_built_from_commit_string);
746 else
747 strbuf_addstr(buf, "no commit associated with this build\n");
748 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
749 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
750 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
751 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
753 if (fsmonitor_ipc__is_supported())
754 strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
758 int cmd_version(int argc, const char **argv, const char *prefix)
760 struct strbuf buf = STRBUF_INIT;
761 int build_options = 0;
762 const char * const usage[] = {
763 N_("git version [--build-options]"),
764 NULL
766 struct option options[] = {
767 OPT_BOOL(0, "build-options", &build_options,
768 "also print build options"),
769 OPT_END()
772 argc = parse_options(argc, argv, prefix, options, usage, 0);
774 get_version_info(&buf, build_options);
775 printf("%s", buf.buf);
777 strbuf_release(&buf);
779 return 0;
782 struct similar_ref_cb {
783 const char *base_ref;
784 struct string_list *similar_refs;
787 static int append_similar_ref(const char *refname,
788 const struct object_id *oid UNUSED,
789 int flags UNUSED, void *cb_data)
791 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
792 char *branch = strrchr(refname, '/') + 1;
794 /* A remote branch of the same name is deemed similar */
795 if (starts_with(refname, "refs/remotes/") &&
796 !strcmp(branch, cb->base_ref))
797 string_list_append_nodup(cb->similar_refs,
798 shorten_unambiguous_ref(refname, 1));
799 return 0;
802 static struct string_list guess_refs(const char *ref)
804 struct similar_ref_cb ref_cb;
805 struct string_list similar_refs = STRING_LIST_INIT_DUP;
807 ref_cb.base_ref = ref;
808 ref_cb.similar_refs = &similar_refs;
809 for_each_ref(append_similar_ref, &ref_cb);
810 return similar_refs;
813 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
814 const char *error)
816 int i;
817 struct string_list suggested_refs = guess_refs(ref);
819 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
821 if (suggested_refs.nr > 0) {
822 fprintf_ln(stderr,
823 Q_("\nDid you mean this?",
824 "\nDid you mean one of these?",
825 suggested_refs.nr));
826 for (i = 0; i < suggested_refs.nr; i++)
827 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
830 string_list_clear(&suggested_refs, 0);
831 exit(1);