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