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