1 #include "git-compat-util.h"
6 #include "run-command.h"
7 #include "levenshtein.h"
10 #include "command-list.h"
11 #include "string-list.h"
15 #include "parse-options.h"
17 #include "fsmonitor-ipc.h"
19 struct category_description
{
23 static uint32_t common_mask
=
24 CAT_init
| CAT_worktree
| CAT_info
|
25 CAT_history
| CAT_remote
;
26 static struct category_description common_categories
[] = {
27 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
28 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
29 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
30 { CAT_history
, N_("grow, mark and tweak your common history") },
31 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
34 static struct category_description main_categories
[] = {
35 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
36 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
37 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
38 { CAT_foreignscminterface
, N_("Interacting with Others") },
39 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
40 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
41 { CAT_synchingrepositories
, N_("Low-level Commands / Syncing Repositories") },
42 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
43 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces") },
44 { CAT_developerinterfaces
, N_("Developer-facing file formats, protocols and other interfaces") },
48 static const char *drop_prefix(const char *name
, uint32_t category
)
55 case CAT_userinterfaces
:
56 case CAT_developerinterfaces
:
63 if (skip_prefix(name
, prefix
, &new_name
))
69 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
72 struct cmdname_help
*cmds
;
74 if (ARRAY_SIZE(command_list
) == 0)
75 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
77 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
79 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
80 const struct cmdname_help
*cmd
= command_list
+ i
;
82 if (!(cmd
->category
& mask
))
86 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
94 static void print_command_list(const struct cmdname_help
*cmds
,
95 uint32_t mask
, int longest
)
99 for (i
= 0; cmds
[i
].name
; i
++) {
100 if (cmds
[i
].category
& mask
) {
101 size_t len
= strlen(cmds
[i
].name
);
102 printf(" %s ", cmds
[i
].name
);
104 mput_char(' ', longest
- len
);
105 puts(_(cmds
[i
].help
));
110 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
112 const struct cmdname_help
*e1
= elem1
;
113 const struct cmdname_help
*e2
= elem2
;
115 return strcmp(e1
->name
, e2
->name
);
118 static void print_cmd_by_category(const struct category_description
*catdesc
,
121 struct cmdname_help
*cmds
;
126 for (i
= 0; catdesc
[i
].desc
; i
++)
127 mask
|= catdesc
[i
].category
;
129 extract_cmds(&cmds
, mask
);
131 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
132 if (longest
< strlen(cmds
[i
].name
))
133 longest
= strlen(cmds
[i
].name
);
135 QSORT(cmds
, nr
, cmd_name_cmp
);
137 for (i
= 0; catdesc
[i
].desc
; i
++) {
138 uint32_t mask
= catdesc
[i
].category
;
139 const char *desc
= catdesc
[i
].desc
;
144 print_command_list(cmds
, mask
, longest
);
148 *longest_p
= longest
;
151 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
154 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
157 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
158 cmds
->names
[cmds
->cnt
++] = ent
;
161 static void clean_cmdnames(struct cmdnames
*cmds
)
164 for (i
= 0; i
< cmds
->cnt
; ++i
)
165 free(cmds
->names
[i
]);
171 static int cmdname_compare(const void *a_
, const void *b_
)
173 struct cmdname
*a
= *(struct cmdname
**)a_
;
174 struct cmdname
*b
= *(struct cmdname
**)b_
;
175 return strcmp(a
->name
, b
->name
);
178 static void uniq(struct cmdnames
*cmds
)
185 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
186 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
187 free(cmds
->names
[i
]);
189 cmds
->names
[j
++] = cmds
->names
[i
];
195 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
201 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
202 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
204 cmds
->names
[cj
++] = cmds
->names
[ci
++];
207 free(cmds
->names
[ci
++]);
212 while (ci
< cmds
->cnt
)
213 cmds
->names
[cj
++] = cmds
->names
[ci
++];
218 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
220 struct string_list list
= STRING_LIST_INIT_NODUP
;
221 struct column_options copts
;
224 for (i
= 0; i
< cmds
->cnt
; i
++)
225 string_list_append(&list
, cmds
->names
[i
]->name
);
227 * always enable column display, we only consult column.*
228 * about layout strategy and stuff
230 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
231 memset(&copts
, 0, sizeof(copts
));
234 print_columns(&list
, colopts
, &copts
);
235 string_list_clear(&list
, 0);
238 static void list_commands_in_dir(struct cmdnames
*cmds
,
242 DIR *dir
= opendir(path
);
244 struct strbuf buf
= STRBUF_INIT
;
252 strbuf_addf(&buf
, "%s/", path
);
255 while ((de
= readdir(dir
)) != NULL
) {
259 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
262 strbuf_setlen(&buf
, len
);
263 strbuf_addstr(&buf
, de
->d_name
);
264 if (!is_executable(buf
.buf
))
267 entlen
= strlen(ent
);
268 strip_suffix(ent
, ".exe", &entlen
);
270 add_cmdname(cmds
, ent
, entlen
);
273 strbuf_release(&buf
);
276 void load_command_list(const char *prefix
,
277 struct cmdnames
*main_cmds
,
278 struct cmdnames
*other_cmds
)
280 const char *env_path
= getenv("PATH");
281 const char *exec_path
= git_exec_path();
283 load_builtin_commands(prefix
, main_cmds
);
286 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
287 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
292 char *paths
, *path
, *colon
;
293 path
= paths
= xstrdup(env_path
);
295 if ((colon
= strchr(path
, PATH_SEP
)))
297 if (!exec_path
|| strcmp(path
, exec_path
))
298 list_commands_in_dir(other_cmds
, path
, prefix
);
306 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
309 exclude_cmds(other_cmds
, main_cmds
);
312 static int get_colopts(const char *var
, const char *value
,
313 const struct config_context
*ctx UNUSED
, void *data
)
315 unsigned int *colopts
= data
;
317 if (starts_with(var
, "column."))
318 return git_column_config(var
, value
, "help", colopts
);
323 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
325 unsigned int colopts
= 0;
326 git_config(get_colopts
, &colopts
);
328 if (main_cmds
->cnt
) {
329 const char *exec_path
= git_exec_path();
330 printf_ln(_("available git commands in '%s'"), exec_path
);
332 pretty_print_cmdnames(main_cmds
, colopts
);
336 if (other_cmds
->cnt
) {
337 puts(_("git commands available from elsewhere on your $PATH"));
339 pretty_print_cmdnames(other_cmds
, colopts
);
344 void list_common_cmds_help(void)
346 puts(_("These are common Git commands used in various situations:"));
348 print_cmd_by_category(common_categories
, NULL
);
351 void list_all_main_cmds(struct string_list
*list
)
353 struct cmdnames main_cmds
, other_cmds
;
356 memset(&main_cmds
, 0, sizeof(main_cmds
));
357 memset(&other_cmds
, 0, sizeof(other_cmds
));
358 load_command_list("git-", &main_cmds
, &other_cmds
);
360 for (i
= 0; i
< main_cmds
.cnt
; i
++)
361 string_list_append(list
, main_cmds
.names
[i
]->name
);
363 clean_cmdnames(&main_cmds
);
364 clean_cmdnames(&other_cmds
);
367 void list_all_other_cmds(struct string_list
*list
)
369 struct cmdnames main_cmds
, other_cmds
;
372 memset(&main_cmds
, 0, sizeof(main_cmds
));
373 memset(&other_cmds
, 0, sizeof(other_cmds
));
374 load_command_list("git-", &main_cmds
, &other_cmds
);
376 for (i
= 0; i
< other_cmds
.cnt
; i
++)
377 string_list_append(list
, other_cmds
.names
[i
]->name
);
379 clean_cmdnames(&main_cmds
);
380 clean_cmdnames(&other_cmds
);
383 void list_cmds_by_category(struct string_list
*list
,
386 int i
, n
= ARRAY_SIZE(command_list
);
389 for (i
= 0; category_names
[i
]; i
++) {
390 if (!strcmp(cat
, category_names
[i
])) {
396 die(_("unsupported command listing type '%s'"), cat
);
398 for (i
= 0; i
< n
; i
++) {
399 struct cmdname_help
*cmd
= command_list
+ i
;
401 if (!(cmd
->category
& cat_id
))
403 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
407 void list_cmds_by_config(struct string_list
*list
)
409 const char *cmd_list
;
411 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
414 string_list_sort(list
);
415 string_list_remove_duplicates(list
, 0);
418 struct strbuf sb
= STRBUF_INIT
;
419 const char *p
= strchrnul(cmd_list
, ' ');
421 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
422 if (sb
.buf
[0] == '-')
423 string_list_remove(list
, sb
.buf
+ 1, 0);
425 string_list_insert(list
, sb
.buf
);
433 void list_guides_help(void)
435 struct category_description catdesc
[] = {
436 { CAT_guide
, N_("The Git concept guides are:") },
439 print_cmd_by_category(catdesc
, NULL
);
443 void list_user_interfaces_help(void)
445 struct category_description catdesc
[] = {
446 { CAT_userinterfaces
, N_("User-facing repository, command and file interfaces:") },
449 print_cmd_by_category(catdesc
, NULL
);
453 void list_developer_interfaces_help(void)
455 struct category_description catdesc
[] = {
456 { CAT_developerinterfaces
, N_("File formats, protocols and other developer interfaces:") },
459 print_cmd_by_category(catdesc
, NULL
);
463 static int get_alias(const char *var
, const char *value
,
464 const struct config_context
*ctx UNUSED
, void *data
)
466 struct string_list
*list
= data
;
468 if (skip_prefix(var
, "alias.", &var
))
469 string_list_append(list
, var
)->util
= xstrdup(value
);
474 static void list_all_cmds_help_external_commands(void)
476 struct string_list others
= STRING_LIST_INIT_DUP
;
479 list_all_other_cmds(&others
);
481 printf("\n%s\n", _("External commands"));
482 for (i
= 0; i
< others
.nr
; i
++)
483 printf(" %s\n", others
.items
[i
].string
);
484 string_list_clear(&others
, 0);
487 static void list_all_cmds_help_aliases(int longest
)
489 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
490 struct cmdname_help
*aliases
;
493 git_config(get_alias
, &alias_list
);
494 string_list_sort(&alias_list
);
496 for (i
= 0; i
< alias_list
.nr
; i
++) {
497 size_t len
= strlen(alias_list
.items
[i
].string
);
503 printf("\n%s\n", _("Command aliases"));
504 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
505 for (i
= 0; i
< alias_list
.nr
; i
++) {
506 aliases
[i
].name
= alias_list
.items
[i
].string
;
507 aliases
[i
].help
= alias_list
.items
[i
].util
;
508 aliases
[i
].category
= 1;
510 aliases
[alias_list
.nr
].name
= NULL
;
511 print_command_list(aliases
, 1, longest
);
514 string_list_clear(&alias_list
, 1);
517 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
521 puts(_("See 'git help <command>' to read about a specific subcommand"));
523 print_cmd_by_category(main_categories
, &longest
);
525 if (show_external_commands
)
526 list_all_cmds_help_external_commands();
528 list_all_cmds_help_aliases(longest
);
531 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
534 for (i
= 0; i
< c
->cnt
; i
++)
535 if (!strcmp(s
, c
->names
[i
]->name
))
540 static int autocorrect
;
541 static struct cmdnames aliases
;
543 #define AUTOCORRECT_PROMPT (-3)
544 #define AUTOCORRECT_NEVER (-2)
545 #define AUTOCORRECT_IMMEDIATELY (-1)
547 static int git_unknown_cmd_config(const char *var
, const char *value
,
548 const struct config_context
*ctx
,
553 if (!strcmp(var
, "help.autocorrect")) {
555 return config_error_nonbool(var
);
556 if (!strcmp(value
, "never")) {
557 autocorrect
= AUTOCORRECT_NEVER
;
558 } else if (!strcmp(value
, "immediate")) {
559 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
560 } else if (!strcmp(value
, "prompt")) {
561 autocorrect
= AUTOCORRECT_PROMPT
;
563 int v
= git_config_int(var
, value
, ctx
->kvi
);
564 autocorrect
= (v
< 0)
565 ? AUTOCORRECT_IMMEDIATELY
: v
;
568 /* Also use aliases for command lookup */
569 if (skip_prefix(var
, "alias.", &p
))
570 add_cmdname(&aliases
, p
, strlen(p
));
575 static int levenshtein_compare(const void *p1
, const void *p2
)
577 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
578 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
581 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
584 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
587 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
589 for (i
= 0; i
< old
->cnt
; i
++)
590 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
591 FREE_AND_NULL(old
->names
);
595 /* An empirically derived magic number */
596 #define SIMILARITY_FLOOR 7
597 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
599 static const char bad_interpreter_advice
[] =
600 N_("'%s' appears to be a git command, but we were not\n"
601 "able to execute it. Maybe git-%s is broken?");
603 const char *help_unknown_cmd(const char *cmd
)
605 int i
, n
, best_similarity
= 0;
606 struct cmdnames main_cmds
, other_cmds
;
607 struct cmdname_help
*common_cmds
;
609 memset(&main_cmds
, 0, sizeof(main_cmds
));
610 memset(&other_cmds
, 0, sizeof(other_cmds
));
611 memset(&aliases
, 0, sizeof(aliases
));
613 read_early_config(git_unknown_cmd_config
, NULL
);
616 * Disable autocorrection prompt in a non-interactive session
618 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
619 autocorrect
= AUTOCORRECT_NEVER
;
621 if (autocorrect
== AUTOCORRECT_NEVER
) {
622 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
626 load_command_list("git-", &main_cmds
, &other_cmds
);
628 add_cmd_list(&main_cmds
, &aliases
);
629 add_cmd_list(&main_cmds
, &other_cmds
);
630 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
633 extract_cmds(&common_cmds
, common_mask
);
635 /* This abuses cmdname->len for levenshtein distance */
636 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
637 int cmp
= 0; /* avoid compiler stupidity */
638 const char *candidate
= main_cmds
.names
[i
]->name
;
641 * An exact match means we have the command, but
642 * for some reason exec'ing it gave us ENOENT; probably
643 * it's a bad interpreter in the #! line.
645 if (!strcmp(candidate
, cmd
))
646 die(_(bad_interpreter_advice
), cmd
, cmd
);
648 /* Does the candidate appear in common_cmds list? */
649 while (common_cmds
[n
].name
&&
650 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
652 if (common_cmds
[n
].name
&& !cmp
) {
653 /* Yes, this is one of the common commands */
654 n
++; /* use the entry from common_cmds[] */
655 if (starts_with(candidate
, cmd
)) {
656 /* Give prefix match a very good score */
657 main_cmds
.names
[i
]->len
= 0;
662 main_cmds
.names
[i
]->len
=
663 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
665 FREE_AND_NULL(common_cmds
);
667 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
670 die(_("Uh oh. Your system reports no Git commands at all."));
672 /* skip and count prefix matches */
673 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
674 ; /* still counting */
676 if (main_cmds
.cnt
<= n
) {
677 /* prefix matches with everything? that is too ambiguous */
678 best_similarity
= SIMILARITY_FLOOR
+ 1;
680 /* count all the most similar ones */
681 for (best_similarity
= main_cmds
.names
[n
++]->len
;
682 (n
< main_cmds
.cnt
&&
683 best_similarity
== main_cmds
.names
[n
]->len
);
685 ; /* still counting */
687 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
688 const char *assumed
= main_cmds
.names
[0]->name
;
689 main_cmds
.names
[0] = NULL
;
690 clean_cmdnames(&main_cmds
);
692 _("WARNING: You called a Git command named '%s', "
693 "which does not exist."),
695 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
697 _("Continuing under the assumption that "
700 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
702 struct strbuf msg
= STRBUF_INIT
;
703 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
704 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
705 strbuf_release(&msg
);
706 if (!(starts_with(answer
, "y") ||
707 starts_with(answer
, "Y")))
711 _("Continuing in %0.1f seconds, "
712 "assuming that you meant '%s'."),
713 (float)autocorrect
/10.0, assumed
);
714 sleep_millisec(autocorrect
* 100);
719 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
721 if (SIMILAR_ENOUGH(best_similarity
)) {
723 Q_("\nThe most similar command is",
724 "\nThe most similar commands are",
727 for (i
= 0; i
< n
; i
++)
728 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
734 void get_version_info(struct strbuf
*buf
, int show_build_options
)
737 * The format of this string should be kept stable for compatibility
738 * with external projects that rely on the output of "git version".
740 * Always show the version, even if other options are given.
742 strbuf_addf(buf
, "git version %s\n", git_version_string
);
744 if (show_build_options
) {
745 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
746 if (git_built_from_commit_string
[0])
747 strbuf_addf(buf
, "built from commit: %s\n",
748 git_built_from_commit_string
);
750 strbuf_addstr(buf
, "no commit associated with this build\n");
751 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
752 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
753 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
754 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
756 if (fsmonitor_ipc__is_supported())
757 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
761 int cmd_version(int argc
, const char **argv
, const char *prefix
)
763 struct strbuf buf
= STRBUF_INIT
;
764 int build_options
= 0;
765 const char * const usage
[] = {
766 N_("git version [--build-options]"),
769 struct option options
[] = {
770 OPT_BOOL(0, "build-options", &build_options
,
771 "also print build options"),
775 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
777 get_version_info(&buf
, build_options
);
778 printf("%s", buf
.buf
);
780 strbuf_release(&buf
);
785 struct similar_ref_cb
{
786 const char *base_ref
;
787 struct string_list
*similar_refs
;
790 static int append_similar_ref(const char *refname
,
791 const struct object_id
*oid UNUSED
,
792 int flags UNUSED
, void *cb_data
)
794 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
795 char *branch
= strrchr(refname
, '/') + 1;
797 /* A remote branch of the same name is deemed similar */
798 if (starts_with(refname
, "refs/remotes/") &&
799 !strcmp(branch
, cb
->base_ref
))
800 string_list_append_nodup(cb
->similar_refs
,
801 shorten_unambiguous_ref(refname
, 1));
805 static struct string_list
guess_refs(const char *ref
)
807 struct similar_ref_cb ref_cb
;
808 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
810 ref_cb
.base_ref
= ref
;
811 ref_cb
.similar_refs
= &similar_refs
;
812 for_each_ref(append_similar_ref
, &ref_cb
);
816 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
820 struct string_list suggested_refs
= guess_refs(ref
);
822 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
824 if (suggested_refs
.nr
> 0) {
826 Q_("\nDid you mean this?",
827 "\nDid you mean one of these?",
829 for (i
= 0; i
< suggested_refs
.nr
; i
++)
830 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
833 string_list_clear(&suggested_refs
, 0);