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