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