5 #include "run-command.h"
6 #include "levenshtein.h"
8 #include "command-list.h"
9 #include "string-list.h"
13 #include "parse-options.h"
15 struct category_description {
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)") },
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") },
42 static const char *drop_prefix(const char *name, uint32_t category)
46 if (skip_prefix(name, "git-", &new_name))
48 if (category == CAT_guide && skip_prefix(name, "git", &new_name))
54 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
57 struct cmdname_help *cmds;
59 if (ARRAY_SIZE(command_list) == 0)
60 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
62 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
64 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
65 const struct cmdname_help *cmd = command_list + i;
67 if (!(cmd->category & mask))
71 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
79 static void print_command_list(const struct cmdname_help *cmds,
80 uint32_t mask, int longest)
84 for (i = 0; cmds[i].name; i++) {
85 if (cmds[i].category & mask) {
86 size_t len = strlen(cmds[i].name);
87 printf(" %s ", cmds[i].name);
89 mput_char(' ', longest - len);
90 puts(_(cmds[i].help));
95 static int cmd_name_cmp(const void *elem1, const void *elem2)
97 const struct cmdname_help *e1 = elem1;
98 const struct cmdname_help *e2 = elem2;
100 return strcmp(e1->name, e2->name);
103 static void print_cmd_by_category(const struct category_description *catdesc,
106 struct cmdname_help *cmds;
111 for (i = 0; catdesc[i].desc; i++)
112 mask |= catdesc[i].category;
114 extract_cmds(&cmds, mask);
116 for (i = 0; cmds[i].name; i++, nr++) {
117 if (longest < strlen(cmds[i].name))
118 longest = strlen(cmds[i].name);
120 QSORT(cmds, nr, cmd_name_cmp);
122 for (i = 0; catdesc[i].desc; i++) {
123 uint32_t mask = catdesc[i].category;
124 const char *desc = catdesc[i].desc;
126 printf("\n%s\n", _(desc));
127 print_command_list(cmds, mask, longest);
131 *longest_p = longest;
134 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
137 FLEX_ALLOC_MEM(ent, name, name, len);
140 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
141 cmds->names[cmds->cnt++] = ent;
144 static void clean_cmdnames(struct cmdnames *cmds)
147 for (i = 0; i < cmds->cnt; ++i)
148 free(cmds->names[i]);
154 static int cmdname_compare(const void *a_, const void *b_)
156 struct cmdname *a = *(struct cmdname **)a_;
157 struct cmdname *b = *(struct cmdname **)b_;
158 return strcmp(a->name, b->name);
161 static void uniq(struct cmdnames *cmds)
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]);
172 cmds->names[j++] = cmds->names[i];
178 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
184 while (ci < cmds->cnt && ei < excludes->cnt) {
185 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
187 cmds->names[cj++] = cmds->names[ci++];
190 free(cmds->names[ci++]);
195 while (ci < cmds->cnt)
196 cmds->names[cj++] = cmds->names[ci++];
201 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
203 struct string_list list = STRING_LIST_INIT_NODUP;
204 struct column_options copts;
207 for (i = 0; i < cmds->cnt; i++)
208 string_list_append(&list, cmds->names[i]->name);
210 * always enable column display, we only consult column.*
211 * about layout strategy and stuff
213 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
214 memset(&copts, 0, sizeof(copts));
217 print_columns(&list, colopts, &copts);
218 string_list_clear(&list, 0);
221 static void list_commands_in_dir(struct cmdnames *cmds,
225 DIR *dir = opendir(path);
227 struct strbuf buf = STRBUF_INIT;
235 strbuf_addf(&buf, "%s/", path);
238 while ((de = readdir(dir)) != NULL) {
242 if (!skip_prefix(de->d_name, prefix, &ent))
245 strbuf_setlen(&buf, len);
246 strbuf_addstr(&buf, de->d_name);
247 if (!is_executable(buf.buf))
250 entlen = strlen(ent);
251 strip_suffix(ent, ".exe", &entlen);
253 add_cmdname(cmds, ent, entlen);
256 strbuf_release(&buf);
259 void load_command_list(const char *prefix,
260 struct cmdnames *main_cmds,
261 struct cmdnames *other_cmds)
263 const char *env_path = getenv("PATH");
264 const char *exec_path = git_exec_path();
267 list_commands_in_dir(main_cmds, exec_path, prefix);
268 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
273 char *paths, *path, *colon;
274 path = paths = xstrdup(env_path);
276 if ((colon = strchr(path, PATH_SEP)))
278 if (!exec_path || strcmp(path, exec_path))
279 list_commands_in_dir(other_cmds, path, prefix);
287 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
290 exclude_cmds(other_cmds, main_cmds);
293 void list_commands(unsigned int colopts,
294 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
296 if (main_cmds->cnt) {
297 const char *exec_path = git_exec_path();
298 printf_ln(_("available git commands in '%s'"), exec_path);
300 pretty_print_cmdnames(main_cmds, colopts);
304 if (other_cmds->cnt) {
305 printf_ln(_("git commands available from elsewhere on your $PATH"));
307 pretty_print_cmdnames(other_cmds, colopts);
312 void list_common_cmds_help(void)
314 puts(_("These are common Git commands used in various situations:"));
315 print_cmd_by_category(common_categories, NULL);
318 void list_all_main_cmds(struct string_list *list)
320 struct cmdnames main_cmds, other_cmds;
323 memset(&main_cmds, 0, sizeof(main_cmds));
324 memset(&other_cmds, 0, sizeof(other_cmds));
325 load_command_list("git-", &main_cmds, &other_cmds);
327 for (i = 0; i < main_cmds.cnt; i++)
328 string_list_append(list, main_cmds.names[i]->name);
330 clean_cmdnames(&main_cmds);
331 clean_cmdnames(&other_cmds);
334 void list_all_other_cmds(struct string_list *list)
336 struct cmdnames main_cmds, other_cmds;
339 memset(&main_cmds, 0, sizeof(main_cmds));
340 memset(&other_cmds, 0, sizeof(other_cmds));
341 load_command_list("git-", &main_cmds, &other_cmds);
343 for (i = 0; i < other_cmds.cnt; i++)
344 string_list_append(list, other_cmds.names[i]->name);
346 clean_cmdnames(&main_cmds);
347 clean_cmdnames(&other_cmds);
350 void list_cmds_by_category(struct string_list *list,
353 int i, n = ARRAY_SIZE(command_list);
356 for (i = 0; category_names[i]; i++) {
357 if (!strcmp(cat, category_names[i])) {
363 die(_("unsupported command listing type '%s'"), cat);
365 for (i = 0; i < n; i++) {
366 struct cmdname_help *cmd = command_list + i;
368 if (!(cmd->category & cat_id))
370 string_list_append(list, drop_prefix(cmd->name, cmd->category));
374 void list_cmds_by_config(struct string_list *list)
376 const char *cmd_list;
378 if (git_config_get_string_const("completion.commands", &cmd_list))
381 string_list_sort(list);
382 string_list_remove_duplicates(list, 0);
385 struct strbuf sb = STRBUF_INIT;
386 const char *p = strchrnul(cmd_list, ' ');
388 strbuf_add(&sb, cmd_list, p - cmd_list);
389 if (sb.buf[0] == '-')
390 string_list_remove(list, sb.buf + 1, 0);
392 string_list_insert(list, sb.buf);
400 void list_common_guides_help(void)
402 struct category_description catdesc[] = {
403 { CAT_guide, N_("The common Git guides are:") },
406 print_cmd_by_category(catdesc, NULL);
410 struct slot_expansion {
412 const char *placeholder;
413 void (*fn)(struct string_list *list, const char *prefix);
417 void list_config_help(int for_human)
419 struct slot_expansion slot_expansions[] = {
420 { "advice", "*", list_config_advices },
421 { "color.branch", "<slot>", list_config_color_branch_slots },
422 { "color.decorate", "<slot>", list_config_color_decorate_slots },
423 { "color.diff", "<slot>", list_config_color_diff_slots },
424 { "color.grep", "<slot>", list_config_color_grep_slots },
425 { "color.interactive", "<slot>", list_config_color_interactive_slots },
426 { "color.remote", "<slot>", list_config_color_sideband_slots },
427 { "color.status", "<slot>", list_config_color_status_slots },
428 { "fsck", "<msg-id>", list_config_fsck_msg_ids },
429 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
433 struct slot_expansion *e;
434 struct string_list keys = STRING_LIST_INIT_DUP;
437 for (p = config_name_list; *p; p++) {
438 const char *var = *p;
439 struct strbuf sb = STRBUF_INIT;
441 for (e = slot_expansions; e->prefix; e++) {
444 strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
445 if (!strcasecmp(var, sb.buf)) {
446 e->fn(&keys, e->prefix);
453 string_list_append(&keys, var);
456 for (e = slot_expansions; e->prefix; e++)
458 BUG("slot_expansion %s.%s is not used",
459 e->prefix, e->placeholder);
461 string_list_sort(&keys);
462 for (i = 0; i < keys.nr; i++) {
463 const char *var = keys.items[i].string;
464 const char *wildcard, *tag, *cut;
471 wildcard = strchr(var, '*');
472 tag = strchr(var, '<');
474 if (!wildcard && !tag) {
479 if (wildcard && !tag)
481 else if (!wildcard && tag)
484 cut = wildcard < tag ? wildcard : tag;
487 * We may produce duplicates, but that's up to
488 * git-completion.bash to handle
490 printf("%.*s\n", (int)(cut - var), var);
492 string_list_clear(&keys, 0);
495 static int get_alias(const char *var, const char *value, void *data)
497 struct string_list *list = data;
499 if (skip_prefix(var, "alias.", &var))
500 string_list_append(list, var)->util = xstrdup(value);
505 void list_all_cmds_help(void)
507 struct string_list others = STRING_LIST_INIT_DUP;
508 struct string_list alias_list = STRING_LIST_INIT_DUP;
509 struct cmdname_help *aliases;
512 printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
513 print_cmd_by_category(main_categories, &longest);
515 list_all_other_cmds(&others);
517 printf("\n%s\n", _("External commands"));
518 for (i = 0; i < others.nr; i++)
519 printf(" %s\n", others.items[i].string);
520 string_list_clear(&others, 0);
522 git_config(get_alias, &alias_list);
523 string_list_sort(&alias_list);
525 for (i = 0; i < alias_list.nr; i++) {
526 size_t len = strlen(alias_list.items[i].string);
532 printf("\n%s\n", _("Command aliases"));
533 ALLOC_ARRAY(aliases, alias_list.nr + 1);
534 for (i = 0; i < alias_list.nr; i++) {
535 aliases[i].name = alias_list.items[i].string;
536 aliases[i].help = alias_list.items[i].util;
537 aliases[i].category = 1;
539 aliases[alias_list.nr].name = NULL;
540 print_command_list(aliases, 1, longest);
543 string_list_clear(&alias_list, 1);
546 int is_in_cmdlist(struct cmdnames *c, const char *s)
549 for (i = 0; i < c->cnt; i++)
550 if (!strcmp(s, c->names[i]->name))
555 static int autocorrect;
556 static struct cmdnames aliases;
558 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
562 if (!strcmp(var, "help.autocorrect"))
563 autocorrect = git_config_int(var,value);
564 /* Also use aliases for command lookup */
565 if (skip_prefix(var, "alias.", &p))
566 add_cmdname(&aliases, p, strlen(p));
568 return git_default_config(var, value, cb);
571 static int levenshtein_compare(const void *p1, const void *p2)
573 const struct cmdname *const *c1 = p1, *const *c2 = p2;
574 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
577 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
580 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
583 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
585 for (i = 0; i < old->cnt; i++)
586 cmds->names[cmds->cnt++] = old->names[i];
587 FREE_AND_NULL(old->names);
591 /* An empirically derived magic number */
592 #define SIMILARITY_FLOOR 7
593 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
595 static const char bad_interpreter_advice[] =
596 N_("'%s' appears to be a git command, but we were not\n"
597 "able to execute it. Maybe git-%s is broken?");
599 const char *help_unknown_cmd(const char *cmd)
601 int i, n, best_similarity = 0;
602 struct cmdnames main_cmds, other_cmds;
603 struct cmdname_help *common_cmds;
605 memset(&main_cmds, 0, sizeof(main_cmds));
606 memset(&other_cmds, 0, sizeof(other_cmds));
607 memset(&aliases, 0, sizeof(aliases));
609 read_early_config(git_unknown_cmd_config, NULL);
611 load_command_list("git-", &main_cmds, &other_cmds);
613 add_cmd_list(&main_cmds, &aliases);
614 add_cmd_list(&main_cmds, &other_cmds);
615 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
618 extract_cmds(&common_cmds, common_mask);
620 /* This abuses cmdname->len for levenshtein distance */
621 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
622 int cmp = 0; /* avoid compiler stupidity */
623 const char *candidate = main_cmds.names[i]->name;
626 * An exact match means we have the command, but
627 * for some reason exec'ing it gave us ENOENT; probably
628 * it's a bad interpreter in the #! line.
630 if (!strcmp(candidate, cmd))
631 die(_(bad_interpreter_advice), cmd, cmd);
633 /* Does the candidate appear in common_cmds list? */
634 while (common_cmds[n].name &&
635 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
637 if (common_cmds[n].name && !cmp) {
638 /* Yes, this is one of the common commands */
639 n++; /* use the entry from common_cmds[] */
640 if (starts_with(candidate, cmd)) {
641 /* Give prefix match a very good score */
642 main_cmds.names[i]->len = 0;
647 main_cmds.names[i]->len =
648 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
650 FREE_AND_NULL(common_cmds);
652 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
655 die(_("Uh oh. Your system reports no Git commands at all."));
657 /* skip and count prefix matches */
658 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
659 ; /* still counting */
661 if (main_cmds.cnt <= n) {
662 /* prefix matches with everything? that is too ambiguous */
663 best_similarity = SIMILARITY_FLOOR + 1;
665 /* count all the most similar ones */
666 for (best_similarity = main_cmds.names[n++]->len;
667 (n < main_cmds.cnt &&
668 best_similarity == main_cmds.names[n]->len);
670 ; /* still counting */
672 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
673 const char *assumed = main_cmds.names[0]->name;
674 main_cmds.names[0] = NULL;
675 clean_cmdnames(&main_cmds);
677 _("WARNING: You called a Git command named '%s', "
678 "which does not exist."),
682 _("Continuing under the assumption that "
687 _("Continuing in %0.1f seconds, "
688 "assuming that you meant '%s'."),
689 (float)autocorrect/10.0, assumed);
690 sleep_millisec(autocorrect * 100);
695 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
697 if (SIMILAR_ENOUGH(best_similarity)) {
699 Q_("\nThe most similar command is",
700 "\nThe most similar commands are",
703 for (i = 0; i < n; i++)
704 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
710 int cmd_version(int argc, const char **argv, const char *prefix)
712 int build_options = 0;
713 const char * const usage[] = {
714 N_("git version [<options>]"),
717 struct option options[] = {
718 OPT_BOOL(0, "build-options", &build_options,
719 "also print build options"),
723 argc = parse_options(argc, argv, prefix, options, usage, 0);
726 * The format of this string should be kept stable for compatibility
727 * with external projects that rely on the output of "git version".
729 * Always show the version, even if other options are given.
731 printf("git version %s\n", git_version_string);
734 printf("cpu: %s\n", GIT_HOST_CPU);
735 if (git_built_from_commit_string[0])
736 printf("built from commit: %s\n",
737 git_built_from_commit_string);
739 printf("no commit associated with this build\n");
740 printf("sizeof-long: %d\n", (int)sizeof(long));
741 printf("sizeof-size_t: %d\n", (int)sizeof(size_t));
742 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
747 struct similar_ref_cb {
748 const char *base_ref;
749 struct string_list *similar_refs;
752 static int append_similar_ref(const char *refname, const struct object_id *oid,
753 int flags, void *cb_data)
755 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
756 char *branch = strrchr(refname, '/') + 1;
758 /* A remote branch of the same name is deemed similar */
759 if (starts_with(refname, "refs/remotes/") &&
760 !strcmp(branch, cb->base_ref))
761 string_list_append_nodup(cb->similar_refs,
762 shorten_unambiguous_ref(refname, 1));
766 static struct string_list guess_refs(const char *ref)
768 struct similar_ref_cb ref_cb;
769 struct string_list similar_refs = STRING_LIST_INIT_DUP;
771 ref_cb.base_ref = ref;
772 ref_cb.similar_refs = &similar_refs;
773 for_each_ref(append_similar_ref, &ref_cb);
777 void help_unknown_ref(const char *ref, const char *cmd, const char *error)
780 struct string_list suggested_refs = guess_refs(ref);
782 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
784 if (suggested_refs.nr > 0) {
786 Q_("\nDid you mean this?",
787 "\nDid you mean one of these?",
789 for (i = 0; i < suggested_refs.nr; i++)
790 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
793 string_list_clear(&suggested_refs, 0);