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