1 #include "git-compat-util.h"
5 #include "run-command.h"
6 #include "levenshtein.h"
9 #include "command-list.h"
10 #include "string-list.h"
14 #include "parse-options.h"
16 #include "fsmonitor-ipc.h"
18 struct category_description
{
22 static uint32_t common_mask
=
23 CAT_init
| CAT_worktree
| CAT_info
|
24 CAT_history
| CAT_remote
;
25 static struct category_description common_categories
[] = {
26 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
27 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
28 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
29 { CAT_history
, N_("grow, mark and tweak your common history") },
30 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
33 static struct category_description main_categories
[] = {
34 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
35 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
36 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
37 { CAT_foreignscminterface
, N_("Interacting with Others") },
38 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
39 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
40 { CAT_synchingrepositories
, N_("Low-level Commands / Syncing Repositories") },
41 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
42 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces") },
43 { CAT_developerinterfaces
, N_("Developer-facing file formats, protocols and other interfaces") },
47 static const char *drop_prefix(const char *name
, uint32_t category
)
54 case CAT_userinterfaces
:
55 case CAT_developerinterfaces
:
62 if (skip_prefix(name
, prefix
, &new_name
))
68 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
71 struct cmdname_help
*cmds
;
73 if (ARRAY_SIZE(command_list
) == 0)
74 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
76 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
78 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
79 const struct cmdname_help
*cmd
= command_list
+ i
;
81 if (!(cmd
->category
& mask
))
85 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
93 static void print_command_list(const struct cmdname_help
*cmds
,
94 uint32_t mask
, int longest
)
98 for (i
= 0; cmds
[i
].name
; i
++) {
99 if (cmds
[i
].category
& mask
) {
100 size_t len
= strlen(cmds
[i
].name
);
101 printf(" %s ", cmds
[i
].name
);
103 mput_char(' ', longest
- len
);
104 puts(_(cmds
[i
].help
));
109 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
111 const struct cmdname_help
*e1
= elem1
;
112 const struct cmdname_help
*e2
= elem2
;
114 return strcmp(e1
->name
, e2
->name
);
117 static void print_cmd_by_category(const struct category_description
*catdesc
,
120 struct cmdname_help
*cmds
;
125 for (i
= 0; catdesc
[i
].desc
; i
++)
126 mask
|= catdesc
[i
].category
;
128 extract_cmds(&cmds
, mask
);
130 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
131 if (longest
< strlen(cmds
[i
].name
))
132 longest
= strlen(cmds
[i
].name
);
134 QSORT(cmds
, nr
, cmd_name_cmp
);
136 for (i
= 0; catdesc
[i
].desc
; i
++) {
137 uint32_t mask
= catdesc
[i
].category
;
138 const char *desc
= catdesc
[i
].desc
;
143 print_command_list(cmds
, mask
, longest
);
147 *longest_p
= longest
;
150 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
153 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
156 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
157 cmds
->names
[cmds
->cnt
++] = ent
;
160 static void clean_cmdnames(struct cmdnames
*cmds
)
163 for (i
= 0; i
< cmds
->cnt
; ++i
)
164 free(cmds
->names
[i
]);
170 static int cmdname_compare(const void *a_
, const void *b_
)
172 struct cmdname
*a
= *(struct cmdname
**)a_
;
173 struct cmdname
*b
= *(struct cmdname
**)b_
;
174 return strcmp(a
->name
, b
->name
);
177 static void uniq(struct cmdnames
*cmds
)
184 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
185 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
186 free(cmds
->names
[i
]);
188 cmds
->names
[j
++] = cmds
->names
[i
];
194 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
200 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
201 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
203 cmds
->names
[cj
++] = cmds
->names
[ci
++];
206 free(cmds
->names
[ci
++]);
211 while (ci
< cmds
->cnt
)
212 cmds
->names
[cj
++] = cmds
->names
[ci
++];
217 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
219 struct string_list list
= STRING_LIST_INIT_NODUP
;
220 struct column_options copts
;
223 for (i
= 0; i
< cmds
->cnt
; i
++)
224 string_list_append(&list
, cmds
->names
[i
]->name
);
226 * always enable column display, we only consult column.*
227 * about layout strategy and stuff
229 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
230 memset(&copts
, 0, sizeof(copts
));
233 print_columns(&list
, colopts
, &copts
);
234 string_list_clear(&list
, 0);
237 static void list_commands_in_dir(struct cmdnames
*cmds
,
241 DIR *dir
= opendir(path
);
243 struct strbuf buf
= STRBUF_INIT
;
251 strbuf_addf(&buf
, "%s/", path
);
254 while ((de
= readdir(dir
)) != NULL
) {
258 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
261 strbuf_setlen(&buf
, len
);
262 strbuf_addstr(&buf
, de
->d_name
);
263 if (!is_executable(buf
.buf
))
266 entlen
= strlen(ent
);
267 strip_suffix(ent
, ".exe", &entlen
);
269 add_cmdname(cmds
, ent
, entlen
);
272 strbuf_release(&buf
);
275 void load_command_list(const char *prefix
,
276 struct cmdnames
*main_cmds
,
277 struct cmdnames
*other_cmds
)
279 const char *env_path
= getenv("PATH");
280 const char *exec_path
= git_exec_path();
282 load_builtin_commands(prefix
, main_cmds
);
285 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
286 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
291 char *paths
, *path
, *colon
;
292 path
= paths
= xstrdup(env_path
);
294 if ((colon
= strchr(path
, PATH_SEP
)))
296 if (!exec_path
|| strcmp(path
, exec_path
))
297 list_commands_in_dir(other_cmds
, path
, prefix
);
305 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
308 exclude_cmds(other_cmds
, main_cmds
);
311 static int get_colopts(const char *var
, const char *value
,
312 const struct config_context
*ctx UNUSED
, void *data
)
314 unsigned int *colopts
= data
;
316 if (starts_with(var
, "column."))
317 return git_column_config(var
, value
, "help", colopts
);
322 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
324 unsigned int colopts
= 0;
325 git_config(get_colopts
, &colopts
);
327 if (main_cmds
->cnt
) {
328 const char *exec_path
= git_exec_path();
329 printf_ln(_("available git commands in '%s'"), exec_path
);
331 pretty_print_cmdnames(main_cmds
, colopts
);
335 if (other_cmds
->cnt
) {
336 puts(_("git commands available from elsewhere on your $PATH"));
338 pretty_print_cmdnames(other_cmds
, colopts
);
343 void list_common_cmds_help(void)
345 puts(_("These are common Git commands used in various situations:"));
347 print_cmd_by_category(common_categories
, NULL
);
350 void list_all_main_cmds(struct string_list
*list
)
352 struct cmdnames main_cmds
, other_cmds
;
355 memset(&main_cmds
, 0, sizeof(main_cmds
));
356 memset(&other_cmds
, 0, sizeof(other_cmds
));
357 load_command_list("git-", &main_cmds
, &other_cmds
);
359 for (i
= 0; i
< main_cmds
.cnt
; i
++)
360 string_list_append(list
, main_cmds
.names
[i
]->name
);
362 clean_cmdnames(&main_cmds
);
363 clean_cmdnames(&other_cmds
);
366 void list_all_other_cmds(struct string_list
*list
)
368 struct cmdnames main_cmds
, other_cmds
;
371 memset(&main_cmds
, 0, sizeof(main_cmds
));
372 memset(&other_cmds
, 0, sizeof(other_cmds
));
373 load_command_list("git-", &main_cmds
, &other_cmds
);
375 for (i
= 0; i
< other_cmds
.cnt
; i
++)
376 string_list_append(list
, other_cmds
.names
[i
]->name
);
378 clean_cmdnames(&main_cmds
);
379 clean_cmdnames(&other_cmds
);
382 void list_cmds_by_category(struct string_list
*list
,
385 int i
, n
= ARRAY_SIZE(command_list
);
388 for (i
= 0; category_names
[i
]; i
++) {
389 if (!strcmp(cat
, category_names
[i
])) {
395 die(_("unsupported command listing type '%s'"), cat
);
397 for (i
= 0; i
< n
; i
++) {
398 struct cmdname_help
*cmd
= command_list
+ i
;
400 if (!(cmd
->category
& cat_id
))
402 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
406 void list_cmds_by_config(struct string_list
*list
)
408 const char *cmd_list
;
410 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
413 string_list_sort(list
);
414 string_list_remove_duplicates(list
, 0);
417 struct strbuf sb
= STRBUF_INIT
;
418 const char *p
= strchrnul(cmd_list
, ' ');
420 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
421 if (sb
.buf
[0] == '-')
422 string_list_remove(list
, sb
.buf
+ 1, 0);
424 string_list_insert(list
, sb
.buf
);
432 void list_guides_help(void)
434 struct category_description catdesc
[] = {
435 { CAT_guide
, N_("The Git concept guides are:") },
438 print_cmd_by_category(catdesc
, NULL
);
442 void list_user_interfaces_help(void)
444 struct category_description catdesc
[] = {
445 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces:") },
448 print_cmd_by_category(catdesc
, NULL
);
452 void list_developer_interfaces_help(void)
454 struct category_description catdesc
[] = {
455 { CAT_developerinterfaces
, N_("File formats, protocols and other developer interfaces:") },
458 print_cmd_by_category(catdesc
, NULL
);
462 static int get_alias(const char *var
, const char *value
,
463 const struct config_context
*ctx UNUSED
, void *data
)
465 struct string_list
*list
= data
;
467 if (skip_prefix(var
, "alias.", &var
)) {
469 return config_error_nonbool(var
);
470 string_list_append(list
, var
)->util
= xstrdup(value
);
476 static void list_all_cmds_help_external_commands(void)
478 struct string_list others
= STRING_LIST_INIT_DUP
;
481 list_all_other_cmds(&others
);
483 printf("\n%s\n", _("External commands"));
484 for (i
= 0; i
< others
.nr
; i
++)
485 printf(" %s\n", others
.items
[i
].string
);
486 string_list_clear(&others
, 0);
489 static void list_all_cmds_help_aliases(int longest
)
491 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
492 struct cmdname_help
*aliases
;
495 git_config(get_alias
, &alias_list
);
496 string_list_sort(&alias_list
);
498 for (i
= 0; i
< alias_list
.nr
; i
++) {
499 size_t len
= strlen(alias_list
.items
[i
].string
);
505 printf("\n%s\n", _("Command aliases"));
506 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
507 for (i
= 0; i
< alias_list
.nr
; i
++) {
508 aliases
[i
].name
= alias_list
.items
[i
].string
;
509 aliases
[i
].help
= alias_list
.items
[i
].util
;
510 aliases
[i
].category
= 1;
512 aliases
[alias_list
.nr
].name
= NULL
;
513 print_command_list(aliases
, 1, longest
);
516 string_list_clear(&alias_list
, 1);
519 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
523 puts(_("See 'git help <command>' to read about a specific subcommand"));
525 print_cmd_by_category(main_categories
, &longest
);
527 if (show_external_commands
)
528 list_all_cmds_help_external_commands();
530 list_all_cmds_help_aliases(longest
);
533 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
536 for (i
= 0; i
< c
->cnt
; i
++)
537 if (!strcmp(s
, c
->names
[i
]->name
))
542 static int autocorrect
;
543 static struct cmdnames aliases
;
545 #define AUTOCORRECT_PROMPT (-3)
546 #define AUTOCORRECT_NEVER (-2)
547 #define AUTOCORRECT_IMMEDIATELY (-1)
549 static int git_unknown_cmd_config(const char *var
, const char *value
,
550 const struct config_context
*ctx
,
555 if (!strcmp(var
, "help.autocorrect")) {
557 return config_error_nonbool(var
);
558 if (!strcmp(value
, "never")) {
559 autocorrect
= AUTOCORRECT_NEVER
;
560 } else if (!strcmp(value
, "immediate")) {
561 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
562 } else if (!strcmp(value
, "prompt")) {
563 autocorrect
= AUTOCORRECT_PROMPT
;
565 int v
= git_config_int(var
, value
, ctx
->kvi
);
566 autocorrect
= (v
< 0)
567 ? AUTOCORRECT_IMMEDIATELY
: v
;
570 /* Also use aliases for command lookup */
571 if (skip_prefix(var
, "alias.", &p
))
572 add_cmdname(&aliases
, p
, strlen(p
));
577 static int levenshtein_compare(const void *p1
, const void *p2
)
579 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
580 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
583 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
586 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
589 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
591 for (i
= 0; i
< old
->cnt
; i
++)
592 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
593 FREE_AND_NULL(old
->names
);
597 /* An empirically derived magic number */
598 #define SIMILARITY_FLOOR 7
599 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
601 static const char bad_interpreter_advice
[] =
602 N_("'%s' appears to be a git command, but we were not\n"
603 "able to execute it. Maybe git-%s is broken?");
605 const char *help_unknown_cmd(const char *cmd
)
607 int i
, n
, best_similarity
= 0;
608 struct cmdnames main_cmds
, other_cmds
;
609 struct cmdname_help
*common_cmds
;
611 memset(&main_cmds
, 0, sizeof(main_cmds
));
612 memset(&other_cmds
, 0, sizeof(other_cmds
));
613 memset(&aliases
, 0, sizeof(aliases
));
615 read_early_config(git_unknown_cmd_config
, NULL
);
618 * Disable autocorrection prompt in a non-interactive session
620 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
621 autocorrect
= AUTOCORRECT_NEVER
;
623 if (autocorrect
== AUTOCORRECT_NEVER
) {
624 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
628 load_command_list("git-", &main_cmds
, &other_cmds
);
630 add_cmd_list(&main_cmds
, &aliases
);
631 add_cmd_list(&main_cmds
, &other_cmds
);
632 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
635 extract_cmds(&common_cmds
, common_mask
);
637 /* This abuses cmdname->len for levenshtein distance */
638 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
639 int cmp
= 0; /* avoid compiler stupidity */
640 const char *candidate
= main_cmds
.names
[i
]->name
;
643 * An exact match means we have the command, but
644 * for some reason exec'ing it gave us ENOENT; probably
645 * it's a bad interpreter in the #! line.
647 if (!strcmp(candidate
, cmd
))
648 die(_(bad_interpreter_advice
), cmd
, cmd
);
650 /* Does the candidate appear in common_cmds list? */
651 while (common_cmds
[n
].name
&&
652 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
654 if (common_cmds
[n
].name
&& !cmp
) {
655 /* Yes, this is one of the common commands */
656 n
++; /* use the entry from common_cmds[] */
657 if (starts_with(candidate
, cmd
)) {
658 /* Give prefix match a very good score */
659 main_cmds
.names
[i
]->len
= 0;
664 main_cmds
.names
[i
]->len
=
665 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
667 FREE_AND_NULL(common_cmds
);
669 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
672 die(_("Uh oh. Your system reports no Git commands at all."));
674 /* skip and count prefix matches */
675 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
676 ; /* still counting */
678 if (main_cmds
.cnt
<= n
) {
679 /* prefix matches with everything? that is too ambiguous */
680 best_similarity
= SIMILARITY_FLOOR
+ 1;
682 /* count all the most similar ones */
683 for (best_similarity
= main_cmds
.names
[n
++]->len
;
684 (n
< main_cmds
.cnt
&&
685 best_similarity
== main_cmds
.names
[n
]->len
);
687 ; /* still counting */
689 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
690 const char *assumed
= main_cmds
.names
[0]->name
;
691 main_cmds
.names
[0] = NULL
;
692 clean_cmdnames(&main_cmds
);
694 _("WARNING: You called a Git command named '%s', "
695 "which does not exist."),
697 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
699 _("Continuing under the assumption that "
702 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
704 struct strbuf msg
= STRBUF_INIT
;
705 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
706 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
707 strbuf_release(&msg
);
708 if (!(starts_with(answer
, "y") ||
709 starts_with(answer
, "Y")))
713 _("Continuing in %0.1f seconds, "
714 "assuming that you meant '%s'."),
715 (float)autocorrect
/10.0, assumed
);
716 sleep_millisec(autocorrect
* 100);
721 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
723 if (SIMILAR_ENOUGH(best_similarity
)) {
725 Q_("\nThe most similar command is",
726 "\nThe most similar commands are",
729 for (i
= 0; i
< n
; i
++)
730 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
736 void get_version_info(struct strbuf
*buf
, int show_build_options
)
739 * The format of this string should be kept stable for compatibility
740 * with external projects that rely on the output of "git version".
742 * Always show the version, even if other options are given.
744 strbuf_addf(buf
, "git version %s\n", git_version_string
);
746 if (show_build_options
) {
747 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
748 if (git_built_from_commit_string
[0])
749 strbuf_addf(buf
, "built from commit: %s\n",
750 git_built_from_commit_string
);
752 strbuf_addstr(buf
, "no commit associated with this build\n");
753 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
754 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
755 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
756 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
758 if (fsmonitor_ipc__is_supported())
759 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
763 int cmd_version(int argc
, const char **argv
, const char *prefix
)
765 struct strbuf buf
= STRBUF_INIT
;
766 int build_options
= 0;
767 const char * const usage
[] = {
768 N_("git version [--build-options]"),
771 struct option options
[] = {
772 OPT_BOOL(0, "build-options", &build_options
,
773 "also print build options"),
777 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
779 get_version_info(&buf
, build_options
);
780 printf("%s", buf
.buf
);
782 strbuf_release(&buf
);
787 struct similar_ref_cb
{
788 const char *base_ref
;
789 struct string_list
*similar_refs
;
792 static int append_similar_ref(const char *refname
,
793 const struct object_id
*oid UNUSED
,
794 int flags UNUSED
, void *cb_data
)
796 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
797 char *branch
= strrchr(refname
, '/') + 1;
799 /* A remote branch of the same name is deemed similar */
800 if (starts_with(refname
, "refs/remotes/") &&
801 !strcmp(branch
, cb
->base_ref
))
802 string_list_append_nodup(cb
->similar_refs
,
803 shorten_unambiguous_ref(refname
, 1));
807 static struct string_list
guess_refs(const char *ref
)
809 struct similar_ref_cb ref_cb
;
810 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
812 ref_cb
.base_ref
= ref
;
813 ref_cb
.similar_refs
= &similar_refs
;
814 for_each_ref(append_similar_ref
, &ref_cb
);
818 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
822 struct string_list suggested_refs
= guess_refs(ref
);
824 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
826 if (suggested_refs
.nr
> 0) {
828 Q_("\nDid you mean this?",
829 "\nDid you mean one of these?",
831 for (i
= 0; i
< suggested_refs
.nr
; i
++)
832 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
835 string_list_clear(&suggested_refs
, 0);