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