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