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 #include "fsmonitor-ipc.h"
17 struct category_description
{
21 static uint32_t common_mask
=
22 CAT_init
| CAT_worktree
| CAT_info
|
23 CAT_history
| CAT_remote
;
24 static struct category_description common_categories
[] = {
25 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
26 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
27 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
28 { CAT_history
, N_("grow, mark and tweak your common history") },
29 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
32 static struct category_description main_categories
[] = {
33 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
34 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
35 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
36 { CAT_foreignscminterface
, N_("Interacting with Others") },
37 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
38 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
39 { CAT_synchingrepositories
, N_("Low-level Commands / Syncing Repositories") },
40 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
41 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces") },
42 { CAT_developerinterfaces
, N_("Developer-facing file file formats, protocols and interfaces") },
46 static const char *drop_prefix(const char *name
, uint32_t category
)
53 case CAT_userinterfaces
:
54 case CAT_developerinterfaces
:
61 if (skip_prefix(name
, prefix
, &new_name
))
67 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
70 struct cmdname_help
*cmds
;
72 if (ARRAY_SIZE(command_list
) == 0)
73 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
75 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
77 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
78 const struct cmdname_help
*cmd
= command_list
+ i
;
80 if (!(cmd
->category
& mask
))
84 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
92 static void print_command_list(const struct cmdname_help
*cmds
,
93 uint32_t mask
, int longest
)
97 for (i
= 0; cmds
[i
].name
; i
++) {
98 if (cmds
[i
].category
& mask
) {
99 size_t len
= strlen(cmds
[i
].name
);
100 printf(" %s ", cmds
[i
].name
);
102 mput_char(' ', longest
- len
);
103 puts(_(cmds
[i
].help
));
108 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
110 const struct cmdname_help
*e1
= elem1
;
111 const struct cmdname_help
*e2
= elem2
;
113 return strcmp(e1
->name
, e2
->name
);
116 static void print_cmd_by_category(const struct category_description
*catdesc
,
119 struct cmdname_help
*cmds
;
124 for (i
= 0; catdesc
[i
].desc
; i
++)
125 mask
|= catdesc
[i
].category
;
127 extract_cmds(&cmds
, mask
);
129 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
130 if (longest
< strlen(cmds
[i
].name
))
131 longest
= strlen(cmds
[i
].name
);
133 QSORT(cmds
, nr
, cmd_name_cmp
);
135 for (i
= 0; catdesc
[i
].desc
; i
++) {
136 uint32_t mask
= catdesc
[i
].category
;
137 const char *desc
= catdesc
[i
].desc
;
142 print_command_list(cmds
, mask
, longest
);
146 *longest_p
= longest
;
149 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
152 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
155 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
156 cmds
->names
[cmds
->cnt
++] = ent
;
159 static void clean_cmdnames(struct cmdnames
*cmds
)
162 for (i
= 0; i
< cmds
->cnt
; ++i
)
163 free(cmds
->names
[i
]);
169 static int cmdname_compare(const void *a_
, const void *b_
)
171 struct cmdname
*a
= *(struct cmdname
**)a_
;
172 struct cmdname
*b
= *(struct cmdname
**)b_
;
173 return strcmp(a
->name
, b
->name
);
176 static void uniq(struct cmdnames
*cmds
)
183 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
184 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
185 free(cmds
->names
[i
]);
187 cmds
->names
[j
++] = cmds
->names
[i
];
193 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
199 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
200 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
202 cmds
->names
[cj
++] = cmds
->names
[ci
++];
205 free(cmds
->names
[ci
++]);
210 while (ci
< cmds
->cnt
)
211 cmds
->names
[cj
++] = cmds
->names
[ci
++];
216 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
218 struct string_list list
= STRING_LIST_INIT_NODUP
;
219 struct column_options copts
;
222 for (i
= 0; i
< cmds
->cnt
; i
++)
223 string_list_append(&list
, cmds
->names
[i
]->name
);
225 * always enable column display, we only consult column.*
226 * about layout strategy and stuff
228 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
229 memset(&copts
, 0, sizeof(copts
));
232 print_columns(&list
, colopts
, &copts
);
233 string_list_clear(&list
, 0);
236 static void list_commands_in_dir(struct cmdnames
*cmds
,
240 DIR *dir
= opendir(path
);
242 struct strbuf buf
= STRBUF_INIT
;
250 strbuf_addf(&buf
, "%s/", path
);
253 while ((de
= readdir(dir
)) != NULL
) {
257 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
260 strbuf_setlen(&buf
, len
);
261 strbuf_addstr(&buf
, de
->d_name
);
262 if (!is_executable(buf
.buf
))
265 entlen
= strlen(ent
);
266 strip_suffix(ent
, ".exe", &entlen
);
268 add_cmdname(cmds
, ent
, entlen
);
271 strbuf_release(&buf
);
274 void load_command_list(const char *prefix
,
275 struct cmdnames
*main_cmds
,
276 struct cmdnames
*other_cmds
)
278 const char *env_path
= getenv("PATH");
279 const char *exec_path
= git_exec_path();
281 load_builtin_commands(prefix
, main_cmds
);
284 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
285 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
290 char *paths
, *path
, *colon
;
291 path
= paths
= xstrdup(env_path
);
293 if ((colon
= strchr(path
, PATH_SEP
)))
295 if (!exec_path
|| strcmp(path
, exec_path
))
296 list_commands_in_dir(other_cmds
, path
, prefix
);
304 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
307 exclude_cmds(other_cmds
, main_cmds
);
310 static int get_colopts(const char *var
, const char *value
, void *data
)
312 unsigned int *colopts
= data
;
314 if (starts_with(var
, "column."))
315 return git_column_config(var
, value
, "help", colopts
);
320 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
322 unsigned int colopts
= 0;
323 git_config(get_colopts
, &colopts
);
325 if (main_cmds
->cnt
) {
326 const char *exec_path
= git_exec_path();
327 printf_ln(_("available git commands in '%s'"), exec_path
);
329 pretty_print_cmdnames(main_cmds
, colopts
);
333 if (other_cmds
->cnt
) {
334 puts(_("git commands available from elsewhere on your $PATH"));
336 pretty_print_cmdnames(other_cmds
, colopts
);
341 void list_common_cmds_help(void)
343 puts(_("These are common Git commands used in various situations:"));
345 print_cmd_by_category(common_categories
, NULL
);
348 void list_all_main_cmds(struct string_list
*list
)
350 struct cmdnames main_cmds
, other_cmds
;
353 memset(&main_cmds
, 0, sizeof(main_cmds
));
354 memset(&other_cmds
, 0, sizeof(other_cmds
));
355 load_command_list("git-", &main_cmds
, &other_cmds
);
357 for (i
= 0; i
< main_cmds
.cnt
; i
++)
358 string_list_append(list
, main_cmds
.names
[i
]->name
);
360 clean_cmdnames(&main_cmds
);
361 clean_cmdnames(&other_cmds
);
364 void list_all_other_cmds(struct string_list
*list
)
366 struct cmdnames main_cmds
, other_cmds
;
369 memset(&main_cmds
, 0, sizeof(main_cmds
));
370 memset(&other_cmds
, 0, sizeof(other_cmds
));
371 load_command_list("git-", &main_cmds
, &other_cmds
);
373 for (i
= 0; i
< other_cmds
.cnt
; i
++)
374 string_list_append(list
, other_cmds
.names
[i
]->name
);
376 clean_cmdnames(&main_cmds
);
377 clean_cmdnames(&other_cmds
);
380 void list_cmds_by_category(struct string_list
*list
,
383 int i
, n
= ARRAY_SIZE(command_list
);
386 for (i
= 0; category_names
[i
]; i
++) {
387 if (!strcmp(cat
, category_names
[i
])) {
393 die(_("unsupported command listing type '%s'"), cat
);
395 for (i
= 0; i
< n
; i
++) {
396 struct cmdname_help
*cmd
= command_list
+ i
;
398 if (!(cmd
->category
& cat_id
))
400 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
404 void list_cmds_by_config(struct string_list
*list
)
406 const char *cmd_list
;
408 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
411 string_list_sort(list
);
412 string_list_remove_duplicates(list
, 0);
415 struct strbuf sb
= STRBUF_INIT
;
416 const char *p
= strchrnul(cmd_list
, ' ');
418 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
419 if (sb
.buf
[0] == '-')
420 string_list_remove(list
, sb
.buf
+ 1, 0);
422 string_list_insert(list
, sb
.buf
);
430 void list_guides_help(void)
432 struct category_description catdesc
[] = {
433 { CAT_guide
, N_("The Git concept guides are:") },
436 print_cmd_by_category(catdesc
, NULL
);
440 void list_user_interfaces_help(void)
442 struct category_description catdesc
[] = {
443 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces:") },
446 print_cmd_by_category(catdesc
, NULL
);
450 void list_developer_interfaces_help(void)
452 struct category_description catdesc
[] = {
453 { CAT_developerinterfaces
, N_("File formats, protocols and other developer interfaces:") },
456 print_cmd_by_category(catdesc
, NULL
);
460 static int get_alias(const char *var
, const char *value
, void *data
)
462 struct string_list
*list
= data
;
464 if (skip_prefix(var
, "alias.", &var
))
465 string_list_append(list
, var
)->util
= xstrdup(value
);
470 static void list_all_cmds_help_external_commands(void)
472 struct string_list others
= STRING_LIST_INIT_DUP
;
475 list_all_other_cmds(&others
);
477 printf("\n%s\n", _("External commands"));
478 for (i
= 0; i
< others
.nr
; i
++)
479 printf(" %s\n", others
.items
[i
].string
);
480 string_list_clear(&others
, 0);
483 static void list_all_cmds_help_aliases(int longest
)
485 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
486 struct cmdname_help
*aliases
;
489 git_config(get_alias
, &alias_list
);
490 string_list_sort(&alias_list
);
492 for (i
= 0; i
< alias_list
.nr
; i
++) {
493 size_t len
= strlen(alias_list
.items
[i
].string
);
499 printf("\n%s\n", _("Command aliases"));
500 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
501 for (i
= 0; i
< alias_list
.nr
; i
++) {
502 aliases
[i
].name
= alias_list
.items
[i
].string
;
503 aliases
[i
].help
= alias_list
.items
[i
].util
;
504 aliases
[i
].category
= 1;
506 aliases
[alias_list
.nr
].name
= NULL
;
507 print_command_list(aliases
, 1, longest
);
510 string_list_clear(&alias_list
, 1);
513 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
517 puts(_("See 'git help <command>' to read about a specific subcommand"));
519 print_cmd_by_category(main_categories
, &longest
);
521 if (show_external_commands
)
522 list_all_cmds_help_external_commands();
524 list_all_cmds_help_aliases(longest
);
527 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
530 for (i
= 0; i
< c
->cnt
; i
++)
531 if (!strcmp(s
, c
->names
[i
]->name
))
536 static int autocorrect
;
537 static struct cmdnames aliases
;
539 #define AUTOCORRECT_PROMPT (-3)
540 #define AUTOCORRECT_NEVER (-2)
541 #define AUTOCORRECT_IMMEDIATELY (-1)
543 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
547 if (!strcmp(var
, "help.autocorrect")) {
549 return config_error_nonbool(var
);
550 if (!strcmp(value
, "never")) {
551 autocorrect
= AUTOCORRECT_NEVER
;
552 } else if (!strcmp(value
, "immediate")) {
553 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
554 } else if (!strcmp(value
, "prompt")) {
555 autocorrect
= AUTOCORRECT_PROMPT
;
557 int v
= git_config_int(var
, value
);
558 autocorrect
= (v
< 0)
559 ? AUTOCORRECT_IMMEDIATELY
: v
;
562 /* Also use aliases for command lookup */
563 if (skip_prefix(var
, "alias.", &p
))
564 add_cmdname(&aliases
, p
, strlen(p
));
566 return git_default_config(var
, value
, cb
);
569 static int levenshtein_compare(const void *p1
, const void *p2
)
571 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
572 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
575 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
578 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
581 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
583 for (i
= 0; i
< old
->cnt
; i
++)
584 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
585 FREE_AND_NULL(old
->names
);
589 /* An empirically derived magic number */
590 #define SIMILARITY_FLOOR 7
591 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
593 static const char bad_interpreter_advice
[] =
594 N_("'%s' appears to be a git command, but we were not\n"
595 "able to execute it. Maybe git-%s is broken?");
597 const char *help_unknown_cmd(const char *cmd
)
599 int i
, n
, best_similarity
= 0;
600 struct cmdnames main_cmds
, other_cmds
;
601 struct cmdname_help
*common_cmds
;
603 memset(&main_cmds
, 0, sizeof(main_cmds
));
604 memset(&other_cmds
, 0, sizeof(other_cmds
));
605 memset(&aliases
, 0, sizeof(aliases
));
607 read_early_config(git_unknown_cmd_config
, NULL
);
610 * Disable autocorrection prompt in a non-interactive session
612 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
613 autocorrect
= AUTOCORRECT_NEVER
;
615 if (autocorrect
== AUTOCORRECT_NEVER
) {
616 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
620 load_command_list("git-", &main_cmds
, &other_cmds
);
622 add_cmd_list(&main_cmds
, &aliases
);
623 add_cmd_list(&main_cmds
, &other_cmds
);
624 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
627 extract_cmds(&common_cmds
, common_mask
);
629 /* This abuses cmdname->len for levenshtein distance */
630 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
631 int cmp
= 0; /* avoid compiler stupidity */
632 const char *candidate
= main_cmds
.names
[i
]->name
;
635 * An exact match means we have the command, but
636 * for some reason exec'ing it gave us ENOENT; probably
637 * it's a bad interpreter in the #! line.
639 if (!strcmp(candidate
, cmd
))
640 die(_(bad_interpreter_advice
), cmd
, cmd
);
642 /* Does the candidate appear in common_cmds list? */
643 while (common_cmds
[n
].name
&&
644 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
646 if (common_cmds
[n
].name
&& !cmp
) {
647 /* Yes, this is one of the common commands */
648 n
++; /* use the entry from common_cmds[] */
649 if (starts_with(candidate
, cmd
)) {
650 /* Give prefix match a very good score */
651 main_cmds
.names
[i
]->len
= 0;
656 main_cmds
.names
[i
]->len
=
657 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
659 FREE_AND_NULL(common_cmds
);
661 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
664 die(_("Uh oh. Your system reports no Git commands at all."));
666 /* skip and count prefix matches */
667 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
668 ; /* still counting */
670 if (main_cmds
.cnt
<= n
) {
671 /* prefix matches with everything? that is too ambiguous */
672 best_similarity
= SIMILARITY_FLOOR
+ 1;
674 /* count all the most similar ones */
675 for (best_similarity
= main_cmds
.names
[n
++]->len
;
676 (n
< main_cmds
.cnt
&&
677 best_similarity
== main_cmds
.names
[n
]->len
);
679 ; /* still counting */
681 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
682 const char *assumed
= main_cmds
.names
[0]->name
;
683 main_cmds
.names
[0] = NULL
;
684 clean_cmdnames(&main_cmds
);
686 _("WARNING: You called a Git command named '%s', "
687 "which does not exist."),
689 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
691 _("Continuing under the assumption that "
694 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
696 struct strbuf msg
= STRBUF_INIT
;
697 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
698 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
699 strbuf_release(&msg
);
700 if (!(starts_with(answer
, "y") ||
701 starts_with(answer
, "Y")))
705 _("Continuing in %0.1f seconds, "
706 "assuming that you meant '%s'."),
707 (float)autocorrect
/10.0, assumed
);
708 sleep_millisec(autocorrect
* 100);
713 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
715 if (SIMILAR_ENOUGH(best_similarity
)) {
717 Q_("\nThe most similar command is",
718 "\nThe most similar commands are",
721 for (i
= 0; i
< n
; i
++)
722 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
728 void get_version_info(struct strbuf
*buf
, int show_build_options
)
731 * The format of this string should be kept stable for compatibility
732 * with external projects that rely on the output of "git version".
734 * Always show the version, even if other options are given.
736 strbuf_addf(buf
, "git version %s\n", git_version_string
);
738 if (show_build_options
) {
739 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
740 if (git_built_from_commit_string
[0])
741 strbuf_addf(buf
, "built from commit: %s\n",
742 git_built_from_commit_string
);
744 strbuf_addstr(buf
, "no commit associated with this build\n");
745 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
746 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
747 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
748 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
750 if (fsmonitor_ipc__is_supported())
751 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
755 int cmd_version(int argc
, const char **argv
, const char *prefix
)
757 struct strbuf buf
= STRBUF_INIT
;
758 int build_options
= 0;
759 const char * const usage
[] = {
760 N_("git version [<options>]"),
763 struct option options
[] = {
764 OPT_BOOL(0, "build-options", &build_options
,
765 "also print build options"),
769 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
771 get_version_info(&buf
, build_options
);
772 printf("%s", buf
.buf
);
774 strbuf_release(&buf
);
779 struct similar_ref_cb
{
780 const char *base_ref
;
781 struct string_list
*similar_refs
;
784 static int append_similar_ref(const char *refname
,
785 const struct object_id
*oid UNUSED
,
786 int flags UNUSED
, void *cb_data
)
788 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
789 char *branch
= strrchr(refname
, '/') + 1;
791 /* A remote branch of the same name is deemed similar */
792 if (starts_with(refname
, "refs/remotes/") &&
793 !strcmp(branch
, cb
->base_ref
))
794 string_list_append_nodup(cb
->similar_refs
,
795 shorten_unambiguous_ref(refname
, 1));
799 static struct string_list
guess_refs(const char *ref
)
801 struct similar_ref_cb ref_cb
;
802 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
804 ref_cb
.base_ref
= ref
;
805 ref_cb
.similar_refs
= &similar_refs
;
806 for_each_ref(append_similar_ref
, &ref_cb
);
810 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
814 struct string_list suggested_refs
= guess_refs(ref
);
816 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
818 if (suggested_refs
.nr
> 0) {
820 Q_("\nDid you mean this?",
821 "\nDid you mean one of these?",
823 for (i
= 0; i
< suggested_refs
.nr
; i
++)
824 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
827 string_list_clear(&suggested_refs
, 0);