1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
9 #include "run-command.h"
10 #include "levenshtein.h"
14 #include "command-list.h"
15 #include "string-list.h"
19 #include "parse-options.h"
21 #include "fsmonitor-ipc.h"
22 #include "repository.h"
25 #include "git-curl-compat.h" /* For LIBCURL_VERSION only */
28 struct category_description
{
32 static uint32_t common_mask
=
33 CAT_init
| CAT_worktree
| CAT_info
|
34 CAT_history
| CAT_remote
;
35 static struct category_description common_categories
[] = {
36 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
37 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
38 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
39 { CAT_history
, N_("grow, mark and tweak your common history") },
40 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
43 static struct category_description main_categories
[] = {
44 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
45 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
46 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
47 { CAT_foreignscminterface
, N_("Interacting with Others") },
48 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
49 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
50 { CAT_synchingrepositories
, N_("Low-level Commands / Syncing Repositories") },
51 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
52 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces") },
53 { CAT_developerinterfaces
, N_("Developer-facing file formats, protocols and other interfaces") },
57 static const char *drop_prefix(const char *name
, uint32_t category
)
64 case CAT_userinterfaces
:
65 case CAT_developerinterfaces
:
72 if (skip_prefix(name
, prefix
, &new_name
))
78 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
81 struct cmdname_help
*cmds
;
83 if (ARRAY_SIZE(command_list
) == 0)
84 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
86 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
88 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
89 const struct cmdname_help
*cmd
= command_list
+ i
;
91 if (!(cmd
->category
& mask
))
95 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
103 static void print_command_list(const struct cmdname_help
*cmds
,
104 uint32_t mask
, int longest
)
108 for (i
= 0; cmds
[i
].name
; i
++) {
109 if (cmds
[i
].category
& mask
) {
110 size_t len
= strlen(cmds
[i
].name
);
111 printf(" %s ", cmds
[i
].name
);
113 mput_char(' ', longest
- len
);
114 puts(_(cmds
[i
].help
));
119 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
121 const struct cmdname_help
*e1
= elem1
;
122 const struct cmdname_help
*e2
= elem2
;
124 return strcmp(e1
->name
, e2
->name
);
127 static void print_cmd_by_category(const struct category_description
*catdesc
,
130 struct cmdname_help
*cmds
;
135 for (i
= 0; catdesc
[i
].desc
; i
++)
136 mask
|= catdesc
[i
].category
;
138 extract_cmds(&cmds
, mask
);
140 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
141 if (longest
< strlen(cmds
[i
].name
))
142 longest
= strlen(cmds
[i
].name
);
144 QSORT(cmds
, nr
, cmd_name_cmp
);
146 for (i
= 0; catdesc
[i
].desc
; i
++) {
147 uint32_t mask
= catdesc
[i
].category
;
148 const char *desc
= catdesc
[i
].desc
;
153 print_command_list(cmds
, mask
, longest
);
157 *longest_p
= longest
;
160 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
163 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
166 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
167 cmds
->names
[cmds
->cnt
++] = ent
;
170 void cmdnames_release(struct cmdnames
*cmds
)
173 for (i
= 0; i
< cmds
->cnt
; ++i
)
174 free(cmds
->names
[i
]);
180 static int cmdname_compare(const void *a_
, const void *b_
)
182 struct cmdname
*a
= *(struct cmdname
**)a_
;
183 struct cmdname
*b
= *(struct cmdname
**)b_
;
184 return strcmp(a
->name
, b
->name
);
187 static void uniq(struct cmdnames
*cmds
)
194 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
195 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
196 free(cmds
->names
[i
]);
198 cmds
->names
[j
++] = cmds
->names
[i
];
204 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
210 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
211 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
213 cmds
->names
[cj
++] = cmds
->names
[ci
++];
216 free(cmds
->names
[ci
++]);
221 while (ci
< cmds
->cnt
)
222 cmds
->names
[cj
++] = cmds
->names
[ci
++];
227 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
229 struct string_list list
= STRING_LIST_INIT_NODUP
;
230 struct column_options copts
;
233 for (i
= 0; i
< cmds
->cnt
; i
++)
234 string_list_append(&list
, cmds
->names
[i
]->name
);
236 * always enable column display, we only consult column.*
237 * about layout strategy and stuff
239 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
240 memset(&copts
, 0, sizeof(copts
));
243 print_columns(&list
, colopts
, &copts
);
244 string_list_clear(&list
, 0);
247 static void list_commands_in_dir(struct cmdnames
*cmds
,
251 DIR *dir
= opendir(path
);
253 struct strbuf buf
= STRBUF_INIT
;
261 strbuf_addf(&buf
, "%s/", path
);
264 while ((de
= readdir(dir
)) != NULL
) {
268 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
271 strbuf_setlen(&buf
, len
);
272 strbuf_addstr(&buf
, de
->d_name
);
273 if (!is_executable(buf
.buf
))
276 entlen
= strlen(ent
);
277 strip_suffix(ent
, ".exe", &entlen
);
279 add_cmdname(cmds
, ent
, entlen
);
282 strbuf_release(&buf
);
285 void load_command_list(const char *prefix
,
286 struct cmdnames
*main_cmds
,
287 struct cmdnames
*other_cmds
)
289 const char *env_path
= getenv("PATH");
290 const char *exec_path
= git_exec_path();
292 load_builtin_commands(prefix
, main_cmds
);
295 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
296 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
301 char *paths
, *path
, *colon
;
302 path
= paths
= xstrdup(env_path
);
304 if ((colon
= strchr(path
, PATH_SEP
)))
306 if (!exec_path
|| strcmp(path
, exec_path
))
307 list_commands_in_dir(other_cmds
, path
, prefix
);
315 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
318 exclude_cmds(other_cmds
, main_cmds
);
321 static int get_colopts(const char *var
, const char *value
,
322 const struct config_context
*ctx UNUSED
, void *data
)
324 unsigned int *colopts
= data
;
326 if (starts_with(var
, "column."))
327 return git_column_config(var
, value
, "help", colopts
);
332 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
334 unsigned int colopts
= 0;
335 git_config(get_colopts
, &colopts
);
337 if (main_cmds
->cnt
) {
338 const char *exec_path
= git_exec_path();
339 printf_ln(_("available git commands in '%s'"), exec_path
);
341 pretty_print_cmdnames(main_cmds
, colopts
);
345 if (other_cmds
->cnt
) {
346 puts(_("git commands available from elsewhere on your $PATH"));
348 pretty_print_cmdnames(other_cmds
, colopts
);
353 void list_common_cmds_help(void)
355 puts(_("These are common Git commands used in various situations:"));
357 print_cmd_by_category(common_categories
, NULL
);
360 void list_all_main_cmds(struct string_list
*list
)
362 struct cmdnames main_cmds
, other_cmds
;
365 memset(&main_cmds
, 0, sizeof(main_cmds
));
366 memset(&other_cmds
, 0, sizeof(other_cmds
));
367 load_command_list("git-", &main_cmds
, &other_cmds
);
369 for (i
= 0; i
< main_cmds
.cnt
; i
++)
370 string_list_append(list
, main_cmds
.names
[i
]->name
);
372 cmdnames_release(&main_cmds
);
373 cmdnames_release(&other_cmds
);
376 void list_all_other_cmds(struct string_list
*list
)
378 struct cmdnames main_cmds
, other_cmds
;
381 memset(&main_cmds
, 0, sizeof(main_cmds
));
382 memset(&other_cmds
, 0, sizeof(other_cmds
));
383 load_command_list("git-", &main_cmds
, &other_cmds
);
385 for (i
= 0; i
< other_cmds
.cnt
; i
++)
386 string_list_append(list
, other_cmds
.names
[i
]->name
);
388 cmdnames_release(&main_cmds
);
389 cmdnames_release(&other_cmds
);
392 void list_cmds_by_category(struct string_list
*list
,
395 int i
, n
= ARRAY_SIZE(command_list
);
398 for (i
= 0; category_names
[i
]; i
++) {
399 if (!strcmp(cat
, category_names
[i
])) {
405 die(_("unsupported command listing type '%s'"), cat
);
407 for (i
= 0; i
< n
; i
++) {
408 struct cmdname_help
*cmd
= command_list
+ i
;
410 if (!(cmd
->category
& cat_id
))
412 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
416 void list_cmds_by_config(struct string_list
*list
)
418 const char *cmd_list
;
420 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
423 string_list_sort(list
);
424 string_list_remove_duplicates(list
, 0);
427 struct strbuf sb
= STRBUF_INIT
;
428 const char *p
= strchrnul(cmd_list
, ' ');
430 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
431 if (sb
.buf
[0] == '-')
432 string_list_remove(list
, sb
.buf
+ 1, 0);
434 string_list_insert(list
, sb
.buf
);
442 void list_guides_help(void)
444 struct category_description catdesc
[] = {
445 { CAT_guide
, N_("The Git concept guides are:") },
448 print_cmd_by_category(catdesc
, NULL
);
452 void list_user_interfaces_help(void)
454 struct category_description catdesc
[] = {
455 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces:") },
458 print_cmd_by_category(catdesc
, NULL
);
462 void list_developer_interfaces_help(void)
464 struct category_description catdesc
[] = {
465 { CAT_developerinterfaces
, N_("File formats, protocols and other developer interfaces:") },
468 print_cmd_by_category(catdesc
, NULL
);
472 static int get_alias(const char *var
, const char *value
,
473 const struct config_context
*ctx UNUSED
, void *data
)
475 struct string_list
*list
= data
;
477 if (skip_prefix(var
, "alias.", &var
)) {
479 return config_error_nonbool(var
);
480 string_list_append(list
, var
)->util
= xstrdup(value
);
486 static void list_all_cmds_help_external_commands(void)
488 struct string_list others
= STRING_LIST_INIT_DUP
;
491 list_all_other_cmds(&others
);
493 printf("\n%s\n", _("External commands"));
494 for (i
= 0; i
< others
.nr
; i
++)
495 printf(" %s\n", others
.items
[i
].string
);
496 string_list_clear(&others
, 0);
499 static void list_all_cmds_help_aliases(int longest
)
501 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
502 struct cmdname_help
*aliases
;
505 git_config(get_alias
, &alias_list
);
506 string_list_sort(&alias_list
);
508 for (i
= 0; i
< alias_list
.nr
; i
++) {
509 size_t len
= strlen(alias_list
.items
[i
].string
);
515 printf("\n%s\n", _("Command aliases"));
516 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
517 for (i
= 0; i
< alias_list
.nr
; i
++) {
518 aliases
[i
].name
= alias_list
.items
[i
].string
;
519 aliases
[i
].help
= alias_list
.items
[i
].util
;
520 aliases
[i
].category
= 1;
522 aliases
[alias_list
.nr
].name
= NULL
;
523 print_command_list(aliases
, 1, longest
);
526 string_list_clear(&alias_list
, 1);
529 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
533 puts(_("See 'git help <command>' to read about a specific subcommand"));
535 print_cmd_by_category(main_categories
, &longest
);
537 if (show_external_commands
)
538 list_all_cmds_help_external_commands();
540 list_all_cmds_help_aliases(longest
);
543 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
546 for (i
= 0; i
< c
->cnt
; i
++)
547 if (!strcmp(s
, c
->names
[i
]->name
))
552 struct help_unknown_cmd_config
{
554 struct cmdnames aliases
;
557 #define AUTOCORRECT_SHOW (-4)
558 #define AUTOCORRECT_PROMPT (-3)
559 #define AUTOCORRECT_NEVER (-2)
560 #define AUTOCORRECT_IMMEDIATELY (-1)
562 static int parse_autocorrect(const char *value
)
564 switch (git_parse_maybe_bool_text(value
)) {
566 return AUTOCORRECT_IMMEDIATELY
;
568 return AUTOCORRECT_SHOW
;
569 default: /* other random text */
573 if (!strcmp(value
, "prompt"))
574 return AUTOCORRECT_PROMPT
;
575 if (!strcmp(value
, "never"))
576 return AUTOCORRECT_NEVER
;
577 if (!strcmp(value
, "immediate"))
578 return AUTOCORRECT_IMMEDIATELY
;
579 if (!strcmp(value
, "show"))
580 return AUTOCORRECT_SHOW
;
585 static int git_unknown_cmd_config(const char *var
, const char *value
,
586 const struct config_context
*ctx
,
589 struct help_unknown_cmd_config
*cfg
= cb
;
592 if (!strcmp(var
, "help.autocorrect")) {
593 int v
= parse_autocorrect(value
);
596 v
= git_config_int(var
, value
, ctx
->kvi
);
598 v
= AUTOCORRECT_IMMEDIATELY
;
601 cfg
->autocorrect
= v
;
604 /* Also use aliases for command lookup */
605 if (skip_prefix(var
, "alias.", &p
))
606 add_cmdname(&cfg
->aliases
, p
, strlen(p
));
611 static int levenshtein_compare(const void *p1
, const void *p2
)
613 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
614 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
617 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
620 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
623 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
625 for (i
= 0; i
< old
->cnt
; i
++)
626 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
627 FREE_AND_NULL(old
->names
);
631 /* An empirically derived magic number */
632 #define SIMILARITY_FLOOR 7
633 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
635 static const char bad_interpreter_advice
[] =
636 N_("'%s' appears to be a git command, but we were not\n"
637 "able to execute it. Maybe git-%s is broken?");
639 char *help_unknown_cmd(const char *cmd
)
641 struct help_unknown_cmd_config cfg
= { 0 };
642 int i
, n
, best_similarity
= 0;
643 struct cmdnames main_cmds
= { 0 };
644 struct cmdnames other_cmds
= { 0 };
645 struct cmdname_help
*common_cmds
;
647 read_early_config(the_repository
, git_unknown_cmd_config
, &cfg
);
650 * Disable autocorrection prompt in a non-interactive session
652 if ((cfg
.autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
653 cfg
.autocorrect
= AUTOCORRECT_NEVER
;
655 if (cfg
.autocorrect
== AUTOCORRECT_NEVER
) {
656 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
660 load_command_list("git-", &main_cmds
, &other_cmds
);
662 add_cmd_list(&main_cmds
, &cfg
.aliases
);
663 add_cmd_list(&main_cmds
, &other_cmds
);
664 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
667 extract_cmds(&common_cmds
, common_mask
);
669 /* This abuses cmdname->len for levenshtein distance */
670 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
671 int cmp
= 0; /* avoid compiler stupidity */
672 const char *candidate
= main_cmds
.names
[i
]->name
;
675 * An exact match means we have the command, but
676 * for some reason exec'ing it gave us ENOENT; probably
677 * it's a bad interpreter in the #! line.
679 if (!strcmp(candidate
, cmd
))
680 die(_(bad_interpreter_advice
), cmd
, cmd
);
682 /* Does the candidate appear in common_cmds list? */
683 while (common_cmds
[n
].name
&&
684 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
686 if (common_cmds
[n
].name
&& !cmp
) {
687 /* Yes, this is one of the common commands */
688 n
++; /* use the entry from common_cmds[] */
689 if (starts_with(candidate
, cmd
)) {
690 /* Give prefix match a very good score */
691 main_cmds
.names
[i
]->len
= 0;
696 main_cmds
.names
[i
]->len
=
697 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
699 FREE_AND_NULL(common_cmds
);
701 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
704 die(_("Uh oh. Your system reports no Git commands at all."));
706 /* skip and count prefix matches */
707 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
708 ; /* still counting */
710 if (main_cmds
.cnt
<= n
) {
711 /* prefix matches with everything? that is too ambiguous */
712 best_similarity
= SIMILARITY_FLOOR
+ 1;
714 /* count all the most similar ones */
715 for (best_similarity
= main_cmds
.names
[n
++]->len
;
716 (n
< main_cmds
.cnt
&&
717 best_similarity
== main_cmds
.names
[n
]->len
);
719 ; /* still counting */
721 if (cfg
.autocorrect
&& cfg
.autocorrect
!= AUTOCORRECT_SHOW
&& n
== 1 &&
722 SIMILAR_ENOUGH(best_similarity
)) {
723 char *assumed
= xstrdup(main_cmds
.names
[0]->name
);
726 _("WARNING: You called a Git command named '%s', "
727 "which does not exist."),
729 if (cfg
.autocorrect
== AUTOCORRECT_IMMEDIATELY
)
731 _("Continuing under the assumption that "
734 else if (cfg
.autocorrect
== AUTOCORRECT_PROMPT
) {
736 struct strbuf msg
= STRBUF_INIT
;
737 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
738 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
739 strbuf_release(&msg
);
740 if (!(starts_with(answer
, "y") ||
741 starts_with(answer
, "Y")))
745 _("Continuing in %0.1f seconds, "
746 "assuming that you meant '%s'."),
747 (float)cfg
.autocorrect
/10.0, assumed
);
748 sleep_millisec(cfg
.autocorrect
* 100);
751 cmdnames_release(&cfg
.aliases
);
752 cmdnames_release(&main_cmds
);
753 cmdnames_release(&other_cmds
);
757 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
759 if (SIMILAR_ENOUGH(best_similarity
)) {
761 Q_("\nThe most similar command is",
762 "\nThe most similar commands are",
765 for (i
= 0; i
< n
; i
++)
766 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
772 void get_version_info(struct strbuf
*buf
, int show_build_options
)
775 * The format of this string should be kept stable for compatibility
776 * with external projects that rely on the output of "git version".
778 * Always show the version, even if other options are given.
780 strbuf_addf(buf
, "git version %s\n", git_version_string
);
782 if (show_build_options
) {
783 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
784 if (git_built_from_commit_string
[0])
785 strbuf_addf(buf
, "built from commit: %s\n",
786 git_built_from_commit_string
);
788 strbuf_addstr(buf
, "no commit associated with this build\n");
789 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
790 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
791 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
792 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
794 if (fsmonitor_ipc__is_supported())
795 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
796 #if defined LIBCURL_VERSION
797 strbuf_addf(buf
, "libcurl: %s\n", LIBCURL_VERSION
);
799 #if defined OPENSSL_VERSION_TEXT
800 strbuf_addf(buf
, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT
);
802 #if defined ZLIBNG_VERSION
803 strbuf_addf(buf
, "zlib-ng: %s\n", ZLIBNG_VERSION
);
804 #elif defined ZLIB_VERSION
805 strbuf_addf(buf
, "zlib: %s\n", ZLIB_VERSION
);
807 strbuf_addf(buf
, "SHA-1: %s\n", SHA1_BACKEND
);
808 #if defined SHA1_UNSAFE_BACKEND
809 strbuf_addf(buf
, "non-collision-detecting-SHA-1: %s\n",
810 SHA1_UNSAFE_BACKEND
);
812 strbuf_addf(buf
, "SHA-256: %s\n", SHA256_BACKEND
);
816 int cmd_version(int argc
, const char **argv
, const char *prefix
, struct repository
*repository UNUSED
)
818 struct strbuf buf
= STRBUF_INIT
;
819 int build_options
= 0;
820 const char * const usage
[] = {
821 N_("git version [--build-options]"),
824 struct option options
[] = {
825 OPT_BOOL(0, "build-options", &build_options
,
826 "also print build options"),
830 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
832 get_version_info(&buf
, build_options
);
833 printf("%s", buf
.buf
);
835 strbuf_release(&buf
);
840 struct similar_ref_cb
{
841 const char *base_ref
;
842 struct string_list
*similar_refs
;
845 static int append_similar_ref(const char *refname
, const char *referent UNUSED
,
846 const struct object_id
*oid UNUSED
,
847 int flags UNUSED
, void *cb_data
)
849 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
850 char *branch
= strrchr(refname
, '/') + 1;
852 /* A remote branch of the same name is deemed similar */
853 if (starts_with(refname
, "refs/remotes/") &&
854 !strcmp(branch
, cb
->base_ref
))
855 string_list_append_nodup(cb
->similar_refs
,
856 refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
), refname
, 1));
860 static struct string_list
guess_refs(const char *ref
)
862 struct similar_ref_cb ref_cb
;
863 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
865 ref_cb
.base_ref
= ref
;
866 ref_cb
.similar_refs
= &similar_refs
;
867 refs_for_each_ref(get_main_ref_store(the_repository
),
868 append_similar_ref
, &ref_cb
);
872 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
876 struct string_list suggested_refs
= guess_refs(ref
);
878 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
880 if (suggested_refs
.nr
> 0) {
882 Q_("\nDid you mean this?",
883 "\nDid you mean one of these?",
885 for (i
= 0; i
< suggested_refs
.nr
; i
++)
886 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
889 string_list_clear(&suggested_refs
, 0);