]>
Commit | Line | Data |
---|---|---|
70827b15 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
70827b15 LT |
3 | #include "builtin.h" |
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 |
15 | struct category_description { |
16 | uint32_t category; | |
17 | const char *desc; | |
18 | }; | |
19 | static uint32_t common_mask = | |
20 | CAT_init | CAT_worktree | CAT_info | | |
21 | CAT_history | CAT_remote; | |
22 | static 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 |
30 | static 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") }, | |
37 | { CAT_synchingrepositories, N_("Low-level Commands / Synching Repositories") }, | |
38 | { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") }, | |
39 | { 0, NULL } | |
40 | }; | |
cfb22a02 | 41 | |
1b81d8cb | 42 | static 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 | ||
54 | static 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 | ||
79 | static 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) { | |
86 | printf(" %s ", cmds[i].name); | |
87 | mput_char(' ', longest - strlen(cmds[i].name)); | |
88 | puts(_(cmds[i].help)); | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | static int cmd_name_cmp(const void *elem1, const void *elem2) | |
94 | { | |
95 | const struct cmdname_help *e1 = elem1; | |
96 | const struct cmdname_help *e2 = elem2; | |
97 | ||
98 | return strcmp(e1->name, e2->name); | |
99 | } | |
100 | ||
101 | static void print_cmd_by_category(const struct category_description *catdesc) | |
102 | { | |
103 | struct cmdname_help *cmds; | |
104 | int longest = 0; | |
105 | int i, nr = 0; | |
106 | uint32_t mask = 0; | |
107 | ||
108 | for (i = 0; catdesc[i].desc; i++) | |
109 | mask |= catdesc[i].category; | |
110 | ||
111 | extract_cmds(&cmds, mask); | |
112 | ||
113 | for (i = 0; cmds[i].name; i++, nr++) { | |
114 | if (longest < strlen(cmds[i].name)) | |
115 | longest = strlen(cmds[i].name); | |
116 | } | |
117 | QSORT(cmds, nr, cmd_name_cmp); | |
118 | ||
119 | for (i = 0; catdesc[i].desc; i++) { | |
120 | uint32_t mask = catdesc[i].category; | |
121 | const char *desc = catdesc[i].desc; | |
122 | ||
123 | printf("\n%s\n", _(desc)); | |
124 | print_command_list(cmds, mask, longest); | |
125 | } | |
126 | free(cmds); | |
127 | } | |
128 | ||
940208a7 | 129 | void add_cmdname(struct cmdnames *cmds, const char *name, int len) |
70827b15 | 130 | { |
96ffc06f JK |
131 | struct cmdname *ent; |
132 | FLEX_ALLOC_MEM(ent, name, name, len); | |
70827b15 | 133 | ent->len = len; |
1eb05690 SP |
134 | |
135 | ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc); | |
136 | cmds->names[cmds->cnt++] = ent; | |
70827b15 LT |
137 | } |
138 | ||
8af84dad JS |
139 | static void clean_cmdnames(struct cmdnames *cmds) |
140 | { | |
141 | int i; | |
142 | for (i = 0; i < cmds->cnt; ++i) | |
143 | free(cmds->names[i]); | |
144 | free(cmds->names); | |
145 | cmds->cnt = 0; | |
146 | cmds->alloc = 0; | |
147 | } | |
148 | ||
70827b15 LT |
149 | static int cmdname_compare(const void *a_, const void *b_) |
150 | { | |
151 | struct cmdname *a = *(struct cmdname **)a_; | |
152 | struct cmdname *b = *(struct cmdname **)b_; | |
153 | return strcmp(a->name, b->name); | |
154 | } | |
155 | ||
1eb05690 SP |
156 | static void uniq(struct cmdnames *cmds) |
157 | { | |
158 | int i, j; | |
159 | ||
160 | if (!cmds->cnt) | |
161 | return; | |
162 | ||
4a15758f JK |
163 | for (i = j = 1; i < cmds->cnt; i++) { |
164 | if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name)) | |
165 | free(cmds->names[i]); | |
166 | else | |
1eb05690 | 167 | cmds->names[j++] = cmds->names[i]; |
4a15758f | 168 | } |
1eb05690 SP |
169 | |
170 | cmds->cnt = j; | |
171 | } | |
172 | ||
940208a7 | 173 | void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes) |
f3fa1838 | 174 | { |
1eb05690 SP |
175 | int ci, cj, ei; |
176 | int cmp; | |
177 | ||
178 | ci = cj = ei = 0; | |
179 | while (ci < cmds->cnt && ei < excludes->cnt) { | |
180 | cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name); | |
181 | if (cmp < 0) | |
182 | cmds->names[cj++] = cmds->names[ci++]; | |
6a17f583 JH |
183 | else if (cmp == 0) { |
184 | ei++; | |
185 | free(cmds->names[ci++]); | |
186 | } else if (cmp > 0) | |
1eb05690 SP |
187 | ei++; |
188 | } | |
189 | ||
190 | while (ci < cmds->cnt) | |
191 | cmds->names[cj++] = cmds->names[ci++]; | |
192 | ||
193 | cmds->cnt = cj; | |
194 | } | |
195 | ||
d10cb3df | 196 | static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts) |
70827b15 | 197 | { |
dbfae689 NTND |
198 | struct string_list list = STRING_LIST_INIT_NODUP; |
199 | struct column_options copts; | |
200 | int i; | |
70827b15 | 201 | |
dbfae689 NTND |
202 | for (i = 0; i < cmds->cnt; i++) |
203 | string_list_append(&list, cmds->names[i]->name); | |
204 | /* | |
205 | * always enable column display, we only consult column.* | |
206 | * about layout strategy and stuff | |
207 | */ | |
208 | colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED; | |
209 | memset(&copts, 0, sizeof(copts)); | |
210 | copts.indent = " "; | |
211 | copts.padding = 2; | |
212 | print_columns(&list, colopts, &copts); | |
213 | string_list_clear(&list, 0); | |
70827b15 LT |
214 | } |
215 | ||
e321180e | 216 | static void list_commands_in_dir(struct cmdnames *cmds, |
940208a7 MV |
217 | const char *path, |
218 | const char *prefix) | |
70827b15 | 219 | { |
1eb05690 | 220 | DIR *dir = opendir(path); |
70827b15 | 221 | struct dirent *de; |
f5d600e2 JS |
222 | struct strbuf buf = STRBUF_INIT; |
223 | int len; | |
70827b15 | 224 | |
f5d600e2 | 225 | if (!dir) |
e321180e | 226 | return; |
940208a7 MV |
227 | if (!prefix) |
228 | prefix = "git-"; | |
70827b15 | 229 | |
f5d600e2 JS |
230 | strbuf_addf(&buf, "%s/", path); |
231 | len = buf.len; | |
232 | ||
70827b15 | 233 | while ((de = readdir(dir)) != NULL) { |
de8118e1 | 234 | const char *ent; |
26936bfd | 235 | size_t entlen; |
70827b15 | 236 | |
de8118e1 | 237 | if (!skip_prefix(de->d_name, prefix, &ent)) |
70827b15 | 238 | continue; |
0966003c | 239 | |
f5d600e2 JS |
240 | strbuf_setlen(&buf, len); |
241 | strbuf_addstr(&buf, de->d_name); | |
242 | if (!is_executable(buf.buf)) | |
70827b15 LT |
243 | continue; |
244 | ||
de8118e1 | 245 | entlen = strlen(ent); |
6e409473 | 246 | strip_suffix(ent, ".exe", &entlen); |
70827b15 | 247 | |
de8118e1 | 248 | add_cmdname(cmds, ent, entlen); |
70827b15 LT |
249 | } |
250 | closedir(dir); | |
f5d600e2 | 251 | strbuf_release(&buf); |
1eb05690 SP |
252 | } |
253 | ||
e321180e | 254 | void load_command_list(const char *prefix, |
940208a7 MV |
255 | struct cmdnames *main_cmds, |
256 | struct cmdnames *other_cmds) | |
1eb05690 | 257 | { |
1eb05690 | 258 | const char *env_path = getenv("PATH"); |
1eb05690 SP |
259 | const char *exec_path = git_exec_path(); |
260 | ||
1f08e5ce | 261 | if (exec_path) { |
e321180e | 262 | list_commands_in_dir(main_cmds, exec_path, prefix); |
9ed0d8d6 | 263 | QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare); |
1f08e5ce | 264 | uniq(main_cmds); |
1eb05690 SP |
265 | } |
266 | ||
1f08e5ce AR |
267 | if (env_path) { |
268 | char *paths, *path, *colon; | |
269 | path = paths = xstrdup(env_path); | |
270 | while (1) { | |
271 | if ((colon = strchr(path, PATH_SEP))) | |
272 | *colon = 0; | |
746c221a PB |
273 | if (!exec_path || strcmp(path, exec_path)) |
274 | list_commands_in_dir(other_cmds, path, prefix); | |
1eb05690 | 275 | |
1f08e5ce AR |
276 | if (!colon) |
277 | break; | |
278 | path = colon + 1; | |
279 | } | |
280 | free(paths); | |
1eb05690 | 281 | |
9ed0d8d6 | 282 | QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare); |
1f08e5ce AR |
283 | uniq(other_cmds); |
284 | } | |
940208a7 | 285 | exclude_cmds(other_cmds, main_cmds); |
2156435f JK |
286 | } |
287 | ||
f4ed0af6 | 288 | void list_commands(unsigned int colopts, |
dbfae689 | 289 | struct cmdnames *main_cmds, struct cmdnames *other_cmds) |
2156435f | 290 | { |
940208a7 | 291 | if (main_cmds->cnt) { |
61c5d431 | 292 | const char *exec_path = git_exec_path(); |
4470ef94 | 293 | printf_ln(_("available git commands in '%s'"), exec_path); |
1eb05690 | 294 | putchar('\n'); |
d10cb3df | 295 | pretty_print_cmdnames(main_cmds, colopts); |
1eb05690 SP |
296 | putchar('\n'); |
297 | } | |
298 | ||
940208a7 | 299 | if (other_cmds->cnt) { |
4470ef94 | 300 | printf_ln(_("git commands available from elsewhere on your $PATH")); |
940208a7 | 301 | putchar('\n'); |
d10cb3df | 302 | pretty_print_cmdnames(other_cmds, colopts); |
1eb05690 SP |
303 | putchar('\n'); |
304 | } | |
70827b15 LT |
305 | } |
306 | ||
1542d4cd JH |
307 | void list_common_cmds_help(void) |
308 | { | |
22414770 | 309 | puts(_("These are common Git commands used in various situations:")); |
cfb22a02 | 310 | print_cmd_by_category(common_categories); |
1542d4cd JH |
311 | } |
312 | ||
6bb2dc0b NTND |
313 | void list_all_main_cmds(struct string_list *list) |
314 | { | |
315 | struct cmdnames main_cmds, other_cmds; | |
316 | int i; | |
317 | ||
318 | memset(&main_cmds, 0, sizeof(main_cmds)); | |
319 | memset(&other_cmds, 0, sizeof(other_cmds)); | |
320 | load_command_list("git-", &main_cmds, &other_cmds); | |
321 | ||
322 | for (i = 0; i < main_cmds.cnt; i++) | |
323 | string_list_append(list, main_cmds.names[i]->name); | |
324 | ||
325 | clean_cmdnames(&main_cmds); | |
326 | clean_cmdnames(&other_cmds); | |
327 | } | |
328 | ||
329 | void list_all_other_cmds(struct string_list *list) | |
330 | { | |
331 | struct cmdnames main_cmds, other_cmds; | |
332 | int i; | |
333 | ||
334 | memset(&main_cmds, 0, sizeof(main_cmds)); | |
335 | memset(&other_cmds, 0, sizeof(other_cmds)); | |
336 | load_command_list("git-", &main_cmds, &other_cmds); | |
337 | ||
338 | for (i = 0; i < other_cmds.cnt; i++) | |
339 | string_list_append(list, other_cmds.names[i]->name); | |
340 | ||
341 | clean_cmdnames(&main_cmds); | |
342 | clean_cmdnames(&other_cmds); | |
343 | } | |
344 | ||
3c777767 NTND |
345 | void list_cmds_by_category(struct string_list *list, |
346 | const char *cat) | |
347 | { | |
348 | int i, n = ARRAY_SIZE(command_list); | |
349 | uint32_t cat_id = 0; | |
350 | ||
351 | for (i = 0; category_names[i]; i++) { | |
352 | if (!strcmp(cat, category_names[i])) { | |
353 | cat_id = 1UL << i; | |
354 | break; | |
355 | } | |
356 | } | |
357 | if (!cat_id) | |
358 | die(_("unsupported command listing type '%s'"), cat); | |
359 | ||
360 | for (i = 0; i < n; i++) { | |
361 | struct cmdname_help *cmd = command_list + i; | |
362 | ||
1b81d8cb NTND |
363 | if (!(cmd->category & cat_id)) |
364 | continue; | |
365 | string_list_append(list, drop_prefix(cmd->name, cmd->category)); | |
3c777767 NTND |
366 | } |
367 | } | |
368 | ||
1b81d8cb NTND |
369 | void list_common_guides_help(void) |
370 | { | |
371 | struct category_description catdesc[] = { | |
372 | { CAT_guide, N_("The common Git guides are:") }, | |
373 | { 0, NULL } | |
374 | }; | |
375 | print_cmd_by_category(catdesc); | |
376 | putchar('\n'); | |
377 | } | |
378 | ||
63eae83f NTND |
379 | void list_all_cmds_help(void) |
380 | { | |
381 | print_cmd_by_category(main_categories); | |
382 | } | |
383 | ||
940208a7 | 384 | int is_in_cmdlist(struct cmdnames *c, const char *s) |
2156435f JK |
385 | { |
386 | int i; | |
387 | for (i = 0; i < c->cnt; i++) | |
388 | if (!strcmp(s, c->names[i]->name)) | |
389 | return 1; | |
390 | return 0; | |
391 | } | |
392 | ||
f0e90716 | 393 | static int autocorrect; |
746c221a | 394 | static struct cmdnames aliases; |
f0e90716 AR |
395 | |
396 | static int git_unknown_cmd_config(const char *var, const char *value, void *cb) | |
397 | { | |
ae021d87 JK |
398 | const char *p; |
399 | ||
f0e90716 AR |
400 | if (!strcmp(var, "help.autocorrect")) |
401 | autocorrect = git_config_int(var,value); | |
746c221a | 402 | /* Also use aliases for command lookup */ |
ae021d87 JK |
403 | if (skip_prefix(var, "alias.", &p)) |
404 | add_cmdname(&aliases, p, strlen(p)); | |
f0e90716 AR |
405 | |
406 | return git_default_config(var, value, cb); | |
407 | } | |
408 | ||
8af84dad | 409 | static int levenshtein_compare(const void *p1, const void *p2) |
822a7d50 | 410 | { |
8af84dad JS |
411 | const struct cmdname *const *c1 = p1, *const *c2 = p2; |
412 | const char *s1 = (*c1)->name, *s2 = (*c2)->name; | |
413 | int l1 = (*c1)->len; | |
414 | int l2 = (*c2)->len; | |
415 | return l1 != l2 ? l1 - l2 : strcmp(s1, s2); | |
416 | } | |
417 | ||
746c221a PB |
418 | static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) |
419 | { | |
420 | int i; | |
421 | ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc); | |
422 | ||
423 | for (i = 0; i < old->cnt; i++) | |
424 | cmds->names[cmds->cnt++] = old->names[i]; | |
88ce3ef6 | 425 | FREE_AND_NULL(old->names); |
746c221a | 426 | old->cnt = 0; |
746c221a PB |
427 | } |
428 | ||
06500a02 | 429 | /* An empirically derived magic number */ |
6612b9e4 EFL |
430 | #define SIMILARITY_FLOOR 7 |
431 | #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR) | |
06500a02 | 432 | |
823e0ded MS |
433 | static const char bad_interpreter_advice[] = |
434 | N_("'%s' appears to be a git command, but we were not\n" | |
435 | "able to execute it. Maybe git-%s is broken?"); | |
436 | ||
8af84dad | 437 | const char *help_unknown_cmd(const char *cmd) |
822a7d50 | 438 | { |
8af84dad JS |
439 | int i, n, best_similarity = 0; |
440 | struct cmdnames main_cmds, other_cmds; | |
cfb22a02 | 441 | struct cmdname_help *common_cmds; |
8af84dad JS |
442 | |
443 | memset(&main_cmds, 0, sizeof(main_cmds)); | |
0b74f5dc | 444 | memset(&other_cmds, 0, sizeof(other_cmds)); |
746c221a | 445 | memset(&aliases, 0, sizeof(aliases)); |
8af84dad | 446 | |
659fef19 | 447 | read_early_config(git_unknown_cmd_config, NULL); |
f0e90716 | 448 | |
8af84dad JS |
449 | load_command_list("git-", &main_cmds, &other_cmds); |
450 | ||
746c221a PB |
451 | add_cmd_list(&main_cmds, &aliases); |
452 | add_cmd_list(&main_cmds, &other_cmds); | |
9ed0d8d6 | 453 | QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare); |
746c221a | 454 | uniq(&main_cmds); |
8af84dad | 455 | |
cfb22a02 NTND |
456 | extract_cmds(&common_cmds, common_mask); |
457 | ||
6612b9e4 EFL |
458 | /* This abuses cmdname->len for levenshtein distance */ |
459 | for (i = 0, n = 0; i < main_cmds.cnt; i++) { | |
460 | int cmp = 0; /* avoid compiler stupidity */ | |
461 | const char *candidate = main_cmds.names[i]->name; | |
462 | ||
823e0ded MS |
463 | /* |
464 | * An exact match means we have the command, but | |
465 | * for some reason exec'ing it gave us ENOENT; probably | |
466 | * it's a bad interpreter in the #! line. | |
467 | */ | |
468 | if (!strcmp(candidate, cmd)) | |
469 | die(_(bad_interpreter_advice), cmd, cmd); | |
470 | ||
6612b9e4 | 471 | /* Does the candidate appear in common_cmds list? */ |
cfb22a02 | 472 | while (common_cmds[n].name && |
6612b9e4 EFL |
473 | (cmp = strcmp(common_cmds[n].name, candidate)) < 0) |
474 | n++; | |
cfb22a02 | 475 | if (common_cmds[n].name && !cmp) { |
6612b9e4 EFL |
476 | /* Yes, this is one of the common commands */ |
477 | n++; /* use the entry from common_cmds[] */ | |
59556548 | 478 | if (starts_with(candidate, cmd)) { |
6612b9e4 EFL |
479 | /* Give prefix match a very good score */ |
480 | main_cmds.names[i]->len = 0; | |
481 | continue; | |
482 | } | |
483 | } | |
484 | ||
8af84dad | 485 | main_cmds.names[i]->len = |
c41494f8 | 486 | levenshtein(cmd, candidate, 0, 2, 1, 3) + 1; |
6612b9e4 | 487 | } |
cfb22a02 | 488 | FREE_AND_NULL(common_cmds); |
8af84dad | 489 | |
9ed0d8d6 | 490 | QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare); |
8af84dad JS |
491 | |
492 | if (!main_cmds.cnt) | |
9665627d | 493 | die(_("Uh oh. Your system reports no Git commands at all.")); |
8af84dad | 494 | |
6612b9e4 EFL |
495 | /* skip and count prefix matches */ |
496 | for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++) | |
497 | ; /* still counting */ | |
498 | ||
499 | if (main_cmds.cnt <= n) { | |
500 | /* prefix matches with everything? that is too ambiguous */ | |
501 | best_similarity = SIMILARITY_FLOOR + 1; | |
502 | } else { | |
503 | /* count all the most similar ones */ | |
504 | for (best_similarity = main_cmds.names[n++]->len; | |
505 | (n < main_cmds.cnt && | |
506 | best_similarity == main_cmds.names[n]->len); | |
507 | n++) | |
508 | ; /* still counting */ | |
509 | } | |
06500a02 | 510 | if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) { |
8af84dad JS |
511 | const char *assumed = main_cmds.names[0]->name; |
512 | main_cmds.names[0] = NULL; | |
513 | clean_cmdnames(&main_cmds); | |
9665627d NTND |
514 | fprintf_ln(stderr, |
515 | _("WARNING: You called a Git command named '%s', " | |
968b1fe2 MB |
516 | "which does not exist."), |
517 | cmd); | |
518 | if (autocorrect < 0) | |
519 | fprintf_ln(stderr, | |
520 | _("Continuing under the assumption that " | |
521 | "you meant '%s'."), | |
522 | assumed); | |
523 | else { | |
524 | fprintf_ln(stderr, | |
525 | _("Continuing in %0.1f seconds, " | |
526 | "assuming that you meant '%s'."), | |
527 | (float)autocorrect/10.0, assumed); | |
2024d317 | 528 | sleep_millisec(autocorrect * 100); |
f0e90716 | 529 | } |
8af84dad JS |
530 | return assumed; |
531 | } | |
532 | ||
9665627d | 533 | fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); |
8af84dad | 534 | |
06500a02 | 535 | if (SIMILAR_ENOUGH(best_similarity)) { |
9665627d | 536 | fprintf_ln(stderr, |
6c486862 JNA |
537 | Q_("\nThe most similar command is", |
538 | "\nThe most similar commands are", | |
9665627d | 539 | n)); |
8af84dad JS |
540 | |
541 | for (i = 0; i < n; i++) | |
542 | fprintf(stderr, "\t%s\n", main_cmds.names[i]->name); | |
543 | } | |
544 | ||
822a7d50 RJ |
545 | exit(1); |
546 | } | |
547 | ||
a633fca0 | 548 | int cmd_version(int argc, const char **argv, const char *prefix) |
70827b15 | 549 | { |
b48cbfc5 JK |
550 | int build_options = 0; |
551 | const char * const usage[] = { | |
552 | N_("git version [<options>]"), | |
553 | NULL | |
554 | }; | |
555 | struct option options[] = { | |
556 | OPT_BOOL(0, "build-options", &build_options, | |
557 | "also print build options"), | |
558 | OPT_END() | |
559 | }; | |
560 | ||
561 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
562 | ||
f2de0b97 DA |
563 | /* |
564 | * The format of this string should be kept stable for compatibility | |
565 | * with external projects that rely on the output of "git version". | |
b48cbfc5 JK |
566 | * |
567 | * Always show the version, even if other options are given. | |
f2de0b97 | 568 | */ |
70827b15 | 569 | printf("git version %s\n", git_version_string); |
b48cbfc5 JK |
570 | |
571 | if (build_options) { | |
b2289404 | 572 | printf("cpu: %s\n", GIT_HOST_CPU); |
ed32b788 JS |
573 | if (git_built_from_commit_string[0]) |
574 | printf("built from commit: %s\n", | |
575 | git_built_from_commit_string); | |
576 | else | |
577 | printf("no commit associated with this build\n"); | |
b48cbfc5 JK |
578 | printf("sizeof-long: %d\n", (int)sizeof(long)); |
579 | /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */ | |
6b9c38e1 | 580 | } |
70827b15 LT |
581 | return 0; |
582 | } | |
e5618106 VV |
583 | |
584 | struct similar_ref_cb { | |
585 | const char *base_ref; | |
586 | struct string_list *similar_refs; | |
587 | }; | |
588 | ||
91d6e94e | 589 | static int append_similar_ref(const char *refname, const struct object_id *oid, |
e5618106 VV |
590 | int flags, void *cb_data) |
591 | { | |
592 | struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); | |
593 | char *branch = strrchr(refname, '/') + 1; | |
95b567c7 JK |
594 | const char *remote; |
595 | ||
e5618106 | 596 | /* A remote branch of the same name is deemed similar */ |
95b567c7 | 597 | if (skip_prefix(refname, "refs/remotes/", &remote) && |
e5618106 | 598 | !strcmp(branch, cb->base_ref)) |
95b567c7 | 599 | string_list_append(cb->similar_refs, remote); |
e5618106 VV |
600 | return 0; |
601 | } | |
602 | ||
603 | static struct string_list guess_refs(const char *ref) | |
604 | { | |
605 | struct similar_ref_cb ref_cb; | |
606 | struct string_list similar_refs = STRING_LIST_INIT_NODUP; | |
607 | ||
608 | ref_cb.base_ref = ref; | |
609 | ref_cb.similar_refs = &similar_refs; | |
610 | for_each_ref(append_similar_ref, &ref_cb); | |
611 | return similar_refs; | |
612 | } | |
613 | ||
614 | void help_unknown_ref(const char *ref, const char *cmd, const char *error) | |
615 | { | |
616 | int i; | |
617 | struct string_list suggested_refs = guess_refs(ref); | |
618 | ||
619 | fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error); | |
620 | ||
621 | if (suggested_refs.nr > 0) { | |
622 | fprintf_ln(stderr, | |
623 | Q_("\nDid you mean this?", | |
624 | "\nDid you mean one of these?", | |
625 | suggested_refs.nr)); | |
626 | for (i = 0; i < suggested_refs.nr; i++) | |
627 | fprintf(stderr, "\t%s\n", suggested_refs.items[i].string); | |
628 | } | |
629 | ||
630 | string_list_clear(&suggested_refs, 0); | |
631 | exit(1); | |
632 | } |