]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
The sixth batch
[thirdparty/git.git] / help.c
CommitLineData
e7da9385 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82 2#define DISABLE_SIGN_COMPARE_WARNINGS
e7da9385 3
36bf1958 4#include "git-compat-util.h"
49d9cd8d 5#include "git-zlib.h"
b2141fc1 6#include "config.h"
70827b15 7#include "builtin.h"
d807c4a0 8#include "exec-cmd.h"
38124a40 9#include "run-command.h"
8af84dad 10#include "levenshtein.h"
a64215b6 11#include "gettext.h"
16fd6c85 12#include "hash.h"
940208a7 13#include "help.h"
cfb22a02 14#include "command-list.h"
dbfae689
NTND
15#include "string-list.h"
16#include "column.h"
816fb46b 17#include "version.h"
e5618106 18#include "refs.h"
b48cbfc5 19#include "parse-options.h"
dc66e3c7 20#include "prompt.h"
dd77cf61 21#include "fsmonitor-ipc.h"
9b1cb507 22#include "repository.h"
64949984 23
2e220316
RB
24#ifndef NO_CURL
25#include "git-curl-compat.h" /* For LIBCURL_VERSION only */
26#endif
27
cfb22a02
NTND
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};
63eae83f
NTND
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") },
96c0caf5 50 { CAT_synchingrepositories, N_("Low-level Commands / Syncing Repositories") },
63eae83f 51 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
d976c510 52 { CAT_userinterfaces, N_("User-facing repository, command and file interfaces") },
225e815e 53 { CAT_developerinterfaces, N_("Developer-facing file formats, protocols and other interfaces") },
63eae83f
NTND
54 { 0, NULL }
55};
cfb22a02 56
1b81d8cb 57static const char *drop_prefix(const char *name, uint32_t category)
cfb22a02
NTND
58{
59 const char *new_name;
936b8eb6 60 const char *prefix;
cfb22a02 61
2f8b3ea6
ÆAB
62 switch (category) {
63 case CAT_guide:
d976c510 64 case CAT_userinterfaces:
844739ba 65 case CAT_developerinterfaces:
2f8b3ea6
ÆAB
66 prefix = "git";
67 break;
936b8eb6
ÆAB
68 default:
69 prefix = "git-";
70 break;
2f8b3ea6 71 }
936b8eb6 72 if (skip_prefix(name, prefix, &new_name))
1b81d8cb 73 return new_name;
cfb22a02 74
2f8b3ea6 75 return name;
cfb22a02
NTND
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;
1b81d8cb 95 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
cfb22a02
NTND
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) {
1c4b9859 110 size_t len = strlen(cmds[i].name);
cfb22a02 111 printf(" %s ", cmds[i].name);
6195a76d
NTND
112 if (longest > len)
113 mput_char(' ', longest - len);
cfb22a02
NTND
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
26c7d067
NTND
127static void print_cmd_by_category(const struct category_description *catdesc,
128 int *longest_p)
cfb22a02
NTND
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
93de1b69
ÆAB
150 if (i)
151 putchar('\n');
cd87ce7d 152 puts(_(desc));
cfb22a02
NTND
153 print_command_list(cmds, mask, longest);
154 }
155 free(cmds);
26c7d067
NTND
156 if (longest_p)
157 *longest_p = longest;
cfb22a02
NTND
158}
159
940208a7 160void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 161{
96ffc06f
JK
162 struct cmdname *ent;
163 FLEX_ALLOC_MEM(ent, name, name, len);
70827b15 164 ent->len = len;
1eb05690
SP
165
166 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
167 cmds->names[cmds->cnt++] = ent;
70827b15
LT
168}
169
77241a6b 170void cmdnames_release(struct cmdnames *cmds)
8af84dad
JS
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
70827b15
LT
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
1eb05690
SP
187static void uniq(struct cmdnames *cmds)
188{
189 int i, j;
190
191 if (!cmds->cnt)
192 return;
193
4a15758f
JK
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
1eb05690 198 cmds->names[j++] = cmds->names[i];
4a15758f 199 }
1eb05690
SP
200
201 cmds->cnt = j;
202}
203
940208a7 204void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 205{
1eb05690
SP
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++];
6a17f583
JH
214 else if (cmp == 0) {
215 ei++;
216 free(cmds->names[ci++]);
3d39bcd9 217 } else
1eb05690
SP
218 ei++;
219 }
220
221 while (ci < cmds->cnt)
222 cmds->names[cj++] = cmds->names[ci++];
223
224 cmds->cnt = cj;
225}
226
d10cb3df 227static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
70827b15 228{
dbfae689
NTND
229 struct string_list list = STRING_LIST_INIT_NODUP;
230 struct column_options copts;
231 int i;
70827b15 232
dbfae689
NTND
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);
70827b15
LT
245}
246
e321180e 247static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
248 const char *path,
249 const char *prefix)
70827b15 250{
1eb05690 251 DIR *dir = opendir(path);
70827b15 252 struct dirent *de;
f5d600e2
JS
253 struct strbuf buf = STRBUF_INIT;
254 int len;
70827b15 255
f5d600e2 256 if (!dir)
e321180e 257 return;
940208a7
MV
258 if (!prefix)
259 prefix = "git-";
70827b15 260
f5d600e2
JS
261 strbuf_addf(&buf, "%s/", path);
262 len = buf.len;
263
70827b15 264 while ((de = readdir(dir)) != NULL) {
de8118e1 265 const char *ent;
26936bfd 266 size_t entlen;
70827b15 267
de8118e1 268 if (!skip_prefix(de->d_name, prefix, &ent))
70827b15 269 continue;
0966003c 270
f5d600e2
JS
271 strbuf_setlen(&buf, len);
272 strbuf_addstr(&buf, de->d_name);
273 if (!is_executable(buf.buf))
70827b15
LT
274 continue;
275
de8118e1 276 entlen = strlen(ent);
6e409473 277 strip_suffix(ent, ".exe", &entlen);
70827b15 278
de8118e1 279 add_cmdname(cmds, ent, entlen);
70827b15
LT
280 }
281 closedir(dir);
f5d600e2 282 strbuf_release(&buf);
1eb05690
SP
283}
284
e321180e 285void load_command_list(const char *prefix,
940208a7
MV
286 struct cmdnames *main_cmds,
287 struct cmdnames *other_cmds)
1eb05690 288{
1eb05690 289 const char *env_path = getenv("PATH");
1eb05690
SP
290 const char *exec_path = git_exec_path();
291
722fc374
JS
292 load_builtin_commands(prefix, main_cmds);
293
1f08e5ce 294 if (exec_path) {
e321180e 295 list_commands_in_dir(main_cmds, exec_path, prefix);
9ed0d8d6 296 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
1f08e5ce 297 uniq(main_cmds);
1eb05690
SP
298 }
299
1f08e5ce
AR
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;
746c221a
PB
306 if (!exec_path || strcmp(path, exec_path))
307 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 308
1f08e5ce
AR
309 if (!colon)
310 break;
311 path = colon + 1;
312 }
313 free(paths);
1eb05690 314
9ed0d8d6 315 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
1f08e5ce
AR
316 uniq(other_cmds);
317 }
940208a7 318 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
319}
320
a4e7e317
GC
321static int get_colopts(const char *var, const char *value,
322 const struct config_context *ctx UNUSED, void *data)
2156435f 323{
06fa4db3
ÆAB
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
940208a7 337 if (main_cmds->cnt) {
61c5d431 338 const char *exec_path = git_exec_path();
4470ef94 339 printf_ln(_("available git commands in '%s'"), exec_path);
1eb05690 340 putchar('\n');
d10cb3df 341 pretty_print_cmdnames(main_cmds, colopts);
1eb05690
SP
342 putchar('\n');
343 }
344
940208a7 345 if (other_cmds->cnt) {
cd87ce7d 346 puts(_("git commands available from elsewhere on your $PATH"));
940208a7 347 putchar('\n');
d10cb3df 348 pretty_print_cmdnames(other_cmds, colopts);
1eb05690
SP
349 putchar('\n');
350 }
70827b15
LT
351}
352
1542d4cd 353void list_common_cmds_help(void)
22414770 354{
22414770 355 puts(_("These are common Git commands used in various situations:"));
93de1b69 356 putchar('\n');
26c7d067 357 print_cmd_by_category(common_categories, NULL);
1542d4cd 358}
22414770 359
6bb2dc0b
NTND
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
77241a6b
PS
372 cmdnames_release(&main_cmds);
373 cmdnames_release(&other_cmds);
22414770
SG
374}
375
6bb2dc0b 376void list_all_other_cmds(struct string_list *list)
1542d4cd 377{
6bb2dc0b
NTND
378 struct cmdnames main_cmds, other_cmds;
379 int i;
1542d4cd 380
6bb2dc0b
NTND
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);
1542d4cd 384
6bb2dc0b
NTND
385 for (i = 0; i < other_cmds.cnt; i++)
386 string_list_append(list, other_cmds.names[i]->name);
22414770 387
77241a6b
PS
388 cmdnames_release(&main_cmds);
389 cmdnames_release(&other_cmds);
6bb2dc0b
NTND
390}
391
3c777767
NTND
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;
22414770 397
3c777767
NTND
398 for (i = 0; category_names[i]; i++) {
399 if (!strcmp(cat, category_names[i])) {
400 cat_id = 1UL << i;
401 break;
22414770 402 }
3c777767
NTND
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
1b81d8cb
NTND
410 if (!(cmd->category & cat_id))
411 continue;
412 string_list_append(list, drop_prefix(cmd->name, cmd->category));
3c777767
NTND
413 }
414}
415
6532f374
NTND
416void list_cmds_by_config(struct string_list *list)
417{
418 const char *cmd_list;
419
f1de981e 420 if (git_config_get_string_tmp("completion.commands", &cmd_list))
6532f374
NTND
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, ' ');
22414770 429
6532f374 430 strbuf_add(&sb, cmd_list, p - cmd_list);
057ab54b
JK
431 if (sb.buf[0] == '-')
432 string_list_remove(list, sb.buf + 1, 0);
6532f374
NTND
433 else
434 string_list_insert(list, sb.buf);
435 strbuf_release(&sb);
436 while (*p == ' ')
437 p++;
438 cmd_list = p;
1542d4cd
JH
439 }
440}
441
0371a764 442void list_guides_help(void)
1b81d8cb
NTND
443{
444 struct category_description catdesc[] = {
0371a764 445 { CAT_guide, N_("The Git concept guides are:") },
d976c510
ÆAB
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:") },
844739ba
ÆAB
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:") },
1b81d8cb
NTND
466 { 0, NULL }
467 };
26c7d067 468 print_cmd_by_category(catdesc, NULL);
1b81d8cb
NTND
469 putchar('\n');
470}
471
a4e7e317
GC
472static int get_alias(const char *var, const char *value,
473 const struct config_context *ctx UNUSED, void *data)
26c7d067
NTND
474{
475 struct string_list *list = data;
476
89086c94
JK
477 if (skip_prefix(var, "alias.", &var)) {
478 if (!value)
479 return config_error_nonbool(var);
26c7d067 480 string_list_append(list, var)->util = xstrdup(value);
89086c94 481 }
26c7d067
NTND
482
483 return 0;
484}
485
6fb427ab 486static void list_all_cmds_help_external_commands(void)
63eae83f 487{
26c7d067 488 struct string_list others = STRING_LIST_INIT_DUP;
6fb427ab 489 int i;
26c7d067
NTND
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);
6fb427ab
ÆAB
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;
26c7d067
NTND
504
505 git_config(get_alias, &alias_list);
506 string_list_sort(&alias_list);
1c4b9859
JS
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
26c7d067
NTND
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);
63eae83f
NTND
527}
528
1ce59013 529void list_all_cmds_help(int show_external_commands, int show_aliases)
6fb427ab
ÆAB
530{
531 int longest;
532
533 puts(_("See 'git help <command>' to read about a specific subcommand"));
93de1b69 534 putchar('\n');
6fb427ab
ÆAB
535 print_cmd_by_category(main_categories, &longest);
536
1ce59013
ÆAB
537 if (show_external_commands)
538 list_all_cmds_help_external_commands();
539 if (show_aliases)
540 list_all_cmds_help_aliases(longest);
6fb427ab
ÆAB
541}
542
940208a7 543int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
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
94aa96cd
PS
552struct help_unknown_cmd_config {
553 int autocorrect;
554 struct cmdnames aliases;
555};
f0e90716 556
e21bf2c4 557#define AUTOCORRECT_SHOW (-4)
dc66e3c7 558#define AUTOCORRECT_PROMPT (-3)
644bb953
DD
559#define AUTOCORRECT_NEVER (-2)
560#define AUTOCORRECT_IMMEDIATELY (-1)
561
4e3dd47c
SC
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:
e21bf2c4 568 return AUTOCORRECT_SHOW;
4e3dd47c
SC
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;
e4542d8b
DA
579 if (!strcmp(value, "show"))
580 return AUTOCORRECT_SHOW;
4e3dd47c
SC
581
582 return 0;
583}
584
506ebaac 585static int git_unknown_cmd_config(const char *var, const char *value,
8868b1eb 586 const struct config_context *ctx,
94aa96cd 587 void *cb)
f0e90716 588{
94aa96cd 589 struct help_unknown_cmd_config *cfg = cb;
ae021d87
JK
590 const char *p;
591
644bb953 592 if (!strcmp(var, "help.autocorrect")) {
4e3dd47c
SC
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;
644bb953 599 }
4e3dd47c
SC
600
601 cfg->autocorrect = v;
644bb953 602 }
4e3dd47c 603
746c221a 604 /* Also use aliases for command lookup */
ae021d87 605 if (skip_prefix(var, "alias.", &p))
94aa96cd 606 add_cmdname(&cfg->aliases, p, strlen(p));
f0e90716 607
0918d088 608 return 0;
f0e90716
AR
609}
610
8af84dad 611static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 612{
8af84dad
JS
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
746c221a
PB
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];
88ce3ef6 627 FREE_AND_NULL(old->names);
746c221a 628 old->cnt = 0;
746c221a
PB
629}
630
06500a02 631/* An empirically derived magic number */
6612b9e4
EFL
632#define SIMILARITY_FLOOR 7
633#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 634
823e0ded
MS
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
7720dbe9 639char *help_unknown_cmd(const char *cmd)
822a7d50 640{
94aa96cd 641 struct help_unknown_cmd_config cfg = { 0 };
8af84dad 642 int i, n, best_similarity = 0;
94aa96cd
PS
643 struct cmdnames main_cmds = { 0 };
644 struct cmdnames other_cmds = { 0 };
cfb22a02 645 struct cmdname_help *common_cmds;
8af84dad 646
94aa96cd 647 read_early_config(the_repository, git_unknown_cmd_config, &cfg);
f0e90716 648
dc66e3c7
ABA
649 /*
650 * Disable autocorrection prompt in a non-interactive session
651 */
94aa96cd
PS
652 if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
653 cfg.autocorrect = AUTOCORRECT_NEVER;
dc66e3c7 654
94aa96cd 655 if (cfg.autocorrect == AUTOCORRECT_NEVER) {
644bb953
DD
656 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
657 exit(1);
658 }
659
8af84dad
JS
660 load_command_list("git-", &main_cmds, &other_cmds);
661
94aa96cd 662 add_cmd_list(&main_cmds, &cfg.aliases);
746c221a 663 add_cmd_list(&main_cmds, &other_cmds);
9ed0d8d6 664 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
746c221a 665 uniq(&main_cmds);
8af84dad 666
cfb22a02
NTND
667 extract_cmds(&common_cmds, common_mask);
668
6612b9e4
EFL
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
823e0ded
MS
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
6612b9e4 682 /* Does the candidate appear in common_cmds list? */
cfb22a02 683 while (common_cmds[n].name &&
6612b9e4
EFL
684 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
685 n++;
cfb22a02 686 if (common_cmds[n].name && !cmp) {
6612b9e4
EFL
687 /* Yes, this is one of the common commands */
688 n++; /* use the entry from common_cmds[] */
59556548 689 if (starts_with(candidate, cmd)) {
6612b9e4
EFL
690 /* Give prefix match a very good score */
691 main_cmds.names[i]->len = 0;
692 continue;
693 }
694 }
695
8af84dad 696 main_cmds.names[i]->len =
c41494f8 697 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
6612b9e4 698 }
cfb22a02 699 FREE_AND_NULL(common_cmds);
8af84dad 700
9ed0d8d6 701 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
8af84dad
JS
702
703 if (!main_cmds.cnt)
9665627d 704 die(_("Uh oh. Your system reports no Git commands at all."));
8af84dad 705
6612b9e4
EFL
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 }
e21bf2c4
DA
721 if (cfg.autocorrect && cfg.autocorrect != AUTOCORRECT_SHOW && n == 1 &&
722 SIMILAR_ENOUGH(best_similarity)) {
7720dbe9
PS
723 char *assumed = xstrdup(main_cmds.names[0]->name);
724
9665627d
NTND
725 fprintf_ln(stderr,
726 _("WARNING: You called a Git command named '%s', "
968b1fe2
MB
727 "which does not exist."),
728 cmd);
94aa96cd 729 if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY)
968b1fe2
MB
730 fprintf_ln(stderr,
731 _("Continuing under the assumption that "
732 "you meant '%s'."),
733 assumed);
94aa96cd 734 else if (cfg.autocorrect == AUTOCORRECT_PROMPT) {
dc66e3c7
ABA
735 char *answer;
736 struct strbuf msg = STRBUF_INIT;
0fc8ed15 737 strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed);
dc66e3c7
ABA
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 {
968b1fe2
MB
744 fprintf_ln(stderr,
745 _("Continuing in %0.1f seconds, "
746 "assuming that you meant '%s'."),
94aa96cd
PS
747 (float)cfg.autocorrect/10.0, assumed);
748 sleep_millisec(cfg.autocorrect * 100);
f0e90716 749 }
889c5979
PS
750
751 cmdnames_release(&cfg.aliases);
752 cmdnames_release(&main_cmds);
753 cmdnames_release(&other_cmds);
8af84dad
JS
754 return assumed;
755 }
756
9665627d 757 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
8af84dad 758
06500a02 759 if (SIMILAR_ENOUGH(best_similarity)) {
9665627d 760 fprintf_ln(stderr,
6c486862
JNA
761 Q_("\nThe most similar command is",
762 "\nThe most similar commands are",
9665627d 763 n));
8af84dad
JS
764
765 for (i = 0; i < n; i++)
766 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
767 }
768
822a7d50
RJ
769 exit(1);
770}
771
617d5719
ES
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));
39f4919d 791 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
617d5719 792 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
dd77cf61
JH
793
794 if (fsmonitor_ipc__is_supported())
795 strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
2e220316
RB
796#if defined LIBCURL_VERSION
797 strbuf_addf(buf, "libcurl: %s\n", LIBCURL_VERSION);
798#endif
8b731b8d
RB
799#if defined OPENSSL_VERSION_TEXT
800 strbuf_addf(buf, "OpenSSL: %s\n", OPENSSL_VERSION_TEXT);
57139818 801#endif
2b1e0f8c
TC
802#if defined ZLIBNG_VERSION
803 strbuf_addf(buf, "zlib-ng: %s\n", ZLIBNG_VERSION);
804#elif defined ZLIB_VERSION
57139818 805 strbuf_addf(buf, "zlib: %s\n", ZLIB_VERSION);
8b731b8d 806#endif
16fd6c85 807 strbuf_addf(buf, "SHA-1: %s\n", SHA1_BACKEND);
6cf65440
JT
808#if defined SHA1_UNSAFE_BACKEND
809 strbuf_addf(buf, "non-collision-detecting-SHA-1: %s\n",
810 SHA1_UNSAFE_BACKEND);
811#endif
16fd6c85 812 strbuf_addf(buf, "SHA-256: %s\n", SHA256_BACKEND);
617d5719
ES
813 }
814}
815
9b1cb507 816int cmd_version(int argc, const char **argv, const char *prefix, struct repository *repository UNUSED)
70827b15 817{
617d5719 818 struct strbuf buf = STRBUF_INIT;
b48cbfc5
JK
819 int build_options = 0;
820 const char * const usage[] = {
8c9e292d 821 N_("git version [--build-options]"),
b48cbfc5
JK
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
617d5719
ES
832 get_version_info(&buf, build_options);
833 printf("%s", buf.buf);
834
835 strbuf_release(&buf);
b48cbfc5 836
70827b15
LT
837 return 0;
838}
e5618106
VV
839
840struct similar_ref_cb {
841 const char *base_ref;
842 struct string_list *similar_refs;
843};
844
e8207717 845static int append_similar_ref(const char *refname, const char *referent UNUSED,
5cf88fd8
ÆAB
846 const struct object_id *oid UNUSED,
847 int flags UNUSED, void *cb_data)
e5618106
VV
848{
849 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
850 char *branch = strrchr(refname, '/') + 1;
95b567c7 851
e5618106 852 /* A remote branch of the same name is deemed similar */
2ed2e199 853 if (starts_with(refname, "refs/remotes/") &&
e5618106 854 !strcmp(branch, cb->base_ref))
2ed2e199 855 string_list_append_nodup(cb->similar_refs,
2e5c4758 856 refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1));
e5618106
VV
857 return 0;
858}
859
860static struct string_list guess_refs(const char *ref)
861{
862 struct similar_ref_cb ref_cb;
8ed51b06 863 struct string_list similar_refs = STRING_LIST_INIT_DUP;
e5618106
VV
864
865 ref_cb.base_ref = ref;
866 ref_cb.similar_refs = &similar_refs;
2e5c4758
PS
867 refs_for_each_ref(get_main_ref_store(the_repository),
868 append_similar_ref, &ref_cb);
e5618106
VV
869 return similar_refs;
870}
871
80e36586
RS
872NORETURN void help_unknown_ref(const char *ref, const char *cmd,
873 const char *error)
e5618106
VV
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}