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") },
44 static const char *drop_prefix(const char *name
, uint32_t category
)
48 if (skip_prefix(name
, "git-", &new_name
))
50 if (category
== CAT_guide
&& skip_prefix(name
, "git", &new_name
))
56 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
59 struct cmdname_help
*cmds
;
61 if (ARRAY_SIZE(command_list
) == 0)
62 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
64 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
66 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
67 const struct cmdname_help
*cmd
= command_list
+ i
;
69 if (!(cmd
->category
& mask
))
73 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
81 static void print_command_list(const struct cmdname_help
*cmds
,
82 uint32_t mask
, int longest
)
86 for (i
= 0; cmds
[i
].name
; i
++) {
87 if (cmds
[i
].category
& mask
) {
88 size_t len
= strlen(cmds
[i
].name
);
89 printf(" %s ", cmds
[i
].name
);
91 mput_char(' ', longest
- len
);
92 puts(_(cmds
[i
].help
));
97 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
99 const struct cmdname_help
*e1
= elem1
;
100 const struct cmdname_help
*e2
= elem2
;
102 return strcmp(e1
->name
, e2
->name
);
105 static void print_cmd_by_category(const struct category_description
*catdesc
,
108 struct cmdname_help
*cmds
;
113 for (i
= 0; catdesc
[i
].desc
; i
++)
114 mask
|= catdesc
[i
].category
;
116 extract_cmds(&cmds
, mask
);
118 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
119 if (longest
< strlen(cmds
[i
].name
))
120 longest
= strlen(cmds
[i
].name
);
122 QSORT(cmds
, nr
, cmd_name_cmp
);
124 for (i
= 0; catdesc
[i
].desc
; i
++) {
125 uint32_t mask
= catdesc
[i
].category
;
126 const char *desc
= catdesc
[i
].desc
;
128 printf("\n%s\n", _(desc
));
129 print_command_list(cmds
, mask
, longest
);
133 *longest_p
= longest
;
136 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
139 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
142 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
143 cmds
->names
[cmds
->cnt
++] = ent
;
146 static void clean_cmdnames(struct cmdnames
*cmds
)
149 for (i
= 0; i
< cmds
->cnt
; ++i
)
150 free(cmds
->names
[i
]);
156 static int cmdname_compare(const void *a_
, const void *b_
)
158 struct cmdname
*a
= *(struct cmdname
**)a_
;
159 struct cmdname
*b
= *(struct cmdname
**)b_
;
160 return strcmp(a
->name
, b
->name
);
163 static void uniq(struct cmdnames
*cmds
)
170 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
171 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
172 free(cmds
->names
[i
]);
174 cmds
->names
[j
++] = cmds
->names
[i
];
180 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
186 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
187 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
189 cmds
->names
[cj
++] = cmds
->names
[ci
++];
192 free(cmds
->names
[ci
++]);
197 while (ci
< cmds
->cnt
)
198 cmds
->names
[cj
++] = cmds
->names
[ci
++];
203 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
205 struct string_list list
= STRING_LIST_INIT_NODUP
;
206 struct column_options copts
;
209 for (i
= 0; i
< cmds
->cnt
; i
++)
210 string_list_append(&list
, cmds
->names
[i
]->name
);
212 * always enable column display, we only consult column.*
213 * about layout strategy and stuff
215 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
216 memset(&copts
, 0, sizeof(copts
));
219 print_columns(&list
, colopts
, &copts
);
220 string_list_clear(&list
, 0);
223 static void list_commands_in_dir(struct cmdnames
*cmds
,
227 DIR *dir
= opendir(path
);
229 struct strbuf buf
= STRBUF_INIT
;
237 strbuf_addf(&buf
, "%s/", path
);
240 while ((de
= readdir(dir
)) != NULL
) {
244 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
247 strbuf_setlen(&buf
, len
);
248 strbuf_addstr(&buf
, de
->d_name
);
249 if (!is_executable(buf
.buf
))
252 entlen
= strlen(ent
);
253 strip_suffix(ent
, ".exe", &entlen
);
255 add_cmdname(cmds
, ent
, entlen
);
258 strbuf_release(&buf
);
261 void load_command_list(const char *prefix
,
262 struct cmdnames
*main_cmds
,
263 struct cmdnames
*other_cmds
)
265 const char *env_path
= getenv("PATH");
266 const char *exec_path
= git_exec_path();
268 load_builtin_commands(prefix
, main_cmds
);
271 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
272 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
277 char *paths
, *path
, *colon
;
278 path
= paths
= xstrdup(env_path
);
280 if ((colon
= strchr(path
, PATH_SEP
)))
282 if (!exec_path
|| strcmp(path
, exec_path
))
283 list_commands_in_dir(other_cmds
, path
, prefix
);
291 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
294 exclude_cmds(other_cmds
, main_cmds
);
297 static int get_colopts(const char *var
, const char *value
, void *data
)
299 unsigned int *colopts
= data
;
301 if (starts_with(var
, "column."))
302 return git_column_config(var
, value
, "help", colopts
);
307 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
309 unsigned int colopts
= 0;
310 git_config(get_colopts
, &colopts
);
312 if (main_cmds
->cnt
) {
313 const char *exec_path
= git_exec_path();
314 printf_ln(_("available git commands in '%s'"), exec_path
);
316 pretty_print_cmdnames(main_cmds
, colopts
);
320 if (other_cmds
->cnt
) {
321 printf_ln(_("git commands available from elsewhere on your $PATH"));
323 pretty_print_cmdnames(other_cmds
, colopts
);
328 void list_common_cmds_help(void)
330 puts(_("These are common Git commands used in various situations:"));
331 print_cmd_by_category(common_categories
, NULL
);
334 void list_all_main_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
< main_cmds
.cnt
; i
++)
344 string_list_append(list
, main_cmds
.names
[i
]->name
);
346 clean_cmdnames(&main_cmds
);
347 clean_cmdnames(&other_cmds
);
350 void list_all_other_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
< other_cmds
.cnt
; i
++)
360 string_list_append(list
, other_cmds
.names
[i
]->name
);
362 clean_cmdnames(&main_cmds
);
363 clean_cmdnames(&other_cmds
);
366 void list_cmds_by_category(struct string_list
*list
,
369 int i
, n
= ARRAY_SIZE(command_list
);
372 for (i
= 0; category_names
[i
]; i
++) {
373 if (!strcmp(cat
, category_names
[i
])) {
379 die(_("unsupported command listing type '%s'"), cat
);
381 for (i
= 0; i
< n
; i
++) {
382 struct cmdname_help
*cmd
= command_list
+ i
;
384 if (!(cmd
->category
& cat_id
))
386 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
390 void list_cmds_by_config(struct string_list
*list
)
392 const char *cmd_list
;
394 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
397 string_list_sort(list
);
398 string_list_remove_duplicates(list
, 0);
401 struct strbuf sb
= STRBUF_INIT
;
402 const char *p
= strchrnul(cmd_list
, ' ');
404 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
405 if (sb
.buf
[0] == '-')
406 string_list_remove(list
, sb
.buf
+ 1, 0);
408 string_list_insert(list
, sb
.buf
);
416 void list_guides_help(void)
418 struct category_description catdesc
[] = {
419 { CAT_guide
, N_("The Git concept guides are:") },
422 print_cmd_by_category(catdesc
, NULL
);
426 static int get_alias(const char *var
, const char *value
, void *data
)
428 struct string_list
*list
= data
;
430 if (skip_prefix(var
, "alias.", &var
))
431 string_list_append(list
, var
)->util
= xstrdup(value
);
436 void list_all_cmds_help(void)
438 struct string_list others
= STRING_LIST_INIT_DUP
;
439 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
440 struct cmdname_help
*aliases
;
443 printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
444 print_cmd_by_category(main_categories
, &longest
);
446 list_all_other_cmds(&others
);
448 printf("\n%s\n", _("External commands"));
449 for (i
= 0; i
< others
.nr
; i
++)
450 printf(" %s\n", others
.items
[i
].string
);
451 string_list_clear(&others
, 0);
453 git_config(get_alias
, &alias_list
);
454 string_list_sort(&alias_list
);
456 for (i
= 0; i
< alias_list
.nr
; i
++) {
457 size_t len
= strlen(alias_list
.items
[i
].string
);
463 printf("\n%s\n", _("Command aliases"));
464 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
465 for (i
= 0; i
< alias_list
.nr
; i
++) {
466 aliases
[i
].name
= alias_list
.items
[i
].string
;
467 aliases
[i
].help
= alias_list
.items
[i
].util
;
468 aliases
[i
].category
= 1;
470 aliases
[alias_list
.nr
].name
= NULL
;
471 print_command_list(aliases
, 1, longest
);
474 string_list_clear(&alias_list
, 1);
477 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
480 for (i
= 0; i
< c
->cnt
; i
++)
481 if (!strcmp(s
, c
->names
[i
]->name
))
486 static int autocorrect
;
487 static struct cmdnames aliases
;
489 #define AUTOCORRECT_PROMPT (-3)
490 #define AUTOCORRECT_NEVER (-2)
491 #define AUTOCORRECT_IMMEDIATELY (-1)
493 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
497 if (!strcmp(var
, "help.autocorrect")) {
499 return config_error_nonbool(var
);
500 if (!strcmp(value
, "never")) {
501 autocorrect
= AUTOCORRECT_NEVER
;
502 } else if (!strcmp(value
, "immediate")) {
503 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
504 } else if (!strcmp(value
, "prompt")) {
505 autocorrect
= AUTOCORRECT_PROMPT
;
507 int v
= git_config_int(var
, value
);
508 autocorrect
= (v
< 0)
509 ? AUTOCORRECT_IMMEDIATELY
: v
;
512 /* Also use aliases for command lookup */
513 if (skip_prefix(var
, "alias.", &p
))
514 add_cmdname(&aliases
, p
, strlen(p
));
516 return git_default_config(var
, value
, cb
);
519 static int levenshtein_compare(const void *p1
, const void *p2
)
521 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
522 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
525 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
528 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
531 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
533 for (i
= 0; i
< old
->cnt
; i
++)
534 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
535 FREE_AND_NULL(old
->names
);
539 /* An empirically derived magic number */
540 #define SIMILARITY_FLOOR 7
541 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
543 static const char bad_interpreter_advice
[] =
544 N_("'%s' appears to be a git command, but we were not\n"
545 "able to execute it. Maybe git-%s is broken?");
547 const char *help_unknown_cmd(const char *cmd
)
549 int i
, n
, best_similarity
= 0;
550 struct cmdnames main_cmds
, other_cmds
;
551 struct cmdname_help
*common_cmds
;
553 memset(&main_cmds
, 0, sizeof(main_cmds
));
554 memset(&other_cmds
, 0, sizeof(other_cmds
));
555 memset(&aliases
, 0, sizeof(aliases
));
557 read_early_config(git_unknown_cmd_config
, NULL
);
560 * Disable autocorrection prompt in a non-interactive session
562 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
563 autocorrect
= AUTOCORRECT_NEVER
;
565 if (autocorrect
== AUTOCORRECT_NEVER
) {
566 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
570 load_command_list("git-", &main_cmds
, &other_cmds
);
572 add_cmd_list(&main_cmds
, &aliases
);
573 add_cmd_list(&main_cmds
, &other_cmds
);
574 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
577 extract_cmds(&common_cmds
, common_mask
);
579 /* This abuses cmdname->len for levenshtein distance */
580 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
581 int cmp
= 0; /* avoid compiler stupidity */
582 const char *candidate
= main_cmds
.names
[i
]->name
;
585 * An exact match means we have the command, but
586 * for some reason exec'ing it gave us ENOENT; probably
587 * it's a bad interpreter in the #! line.
589 if (!strcmp(candidate
, cmd
))
590 die(_(bad_interpreter_advice
), cmd
, cmd
);
592 /* Does the candidate appear in common_cmds list? */
593 while (common_cmds
[n
].name
&&
594 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
596 if (common_cmds
[n
].name
&& !cmp
) {
597 /* Yes, this is one of the common commands */
598 n
++; /* use the entry from common_cmds[] */
599 if (starts_with(candidate
, cmd
)) {
600 /* Give prefix match a very good score */
601 main_cmds
.names
[i
]->len
= 0;
606 main_cmds
.names
[i
]->len
=
607 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
609 FREE_AND_NULL(common_cmds
);
611 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
614 die(_("Uh oh. Your system reports no Git commands at all."));
616 /* skip and count prefix matches */
617 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
618 ; /* still counting */
620 if (main_cmds
.cnt
<= n
) {
621 /* prefix matches with everything? that is too ambiguous */
622 best_similarity
= SIMILARITY_FLOOR
+ 1;
624 /* count all the most similar ones */
625 for (best_similarity
= main_cmds
.names
[n
++]->len
;
626 (n
< main_cmds
.cnt
&&
627 best_similarity
== main_cmds
.names
[n
]->len
);
629 ; /* still counting */
631 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
632 const char *assumed
= main_cmds
.names
[0]->name
;
633 main_cmds
.names
[0] = NULL
;
634 clean_cmdnames(&main_cmds
);
636 _("WARNING: You called a Git command named '%s', "
637 "which does not exist."),
639 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
641 _("Continuing under the assumption that "
644 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
646 struct strbuf msg
= STRBUF_INIT
;
647 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
648 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
649 strbuf_release(&msg
);
650 if (!(starts_with(answer
, "y") ||
651 starts_with(answer
, "Y")))
655 _("Continuing in %0.1f seconds, "
656 "assuming that you meant '%s'."),
657 (float)autocorrect
/10.0, assumed
);
658 sleep_millisec(autocorrect
* 100);
663 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
665 if (SIMILAR_ENOUGH(best_similarity
)) {
667 Q_("\nThe most similar command is",
668 "\nThe most similar commands are",
671 for (i
= 0; i
< n
; i
++)
672 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
678 void get_version_info(struct strbuf
*buf
, int show_build_options
)
681 * The format of this string should be kept stable for compatibility
682 * with external projects that rely on the output of "git version".
684 * Always show the version, even if other options are given.
686 strbuf_addf(buf
, "git version %s\n", git_version_string
);
688 if (show_build_options
) {
689 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
690 if (git_built_from_commit_string
[0])
691 strbuf_addf(buf
, "built from commit: %s\n",
692 git_built_from_commit_string
);
694 strbuf_addstr(buf
, "no commit associated with this build\n");
695 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
696 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
697 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
698 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
700 if (fsmonitor_ipc__is_supported())
701 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
705 int cmd_version(int argc
, const char **argv
, const char *prefix
)
707 struct strbuf buf
= STRBUF_INIT
;
708 int build_options
= 0;
709 const char * const usage
[] = {
710 N_("git version [<options>]"),
713 struct option options
[] = {
714 OPT_BOOL(0, "build-options", &build_options
,
715 "also print build options"),
719 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
721 get_version_info(&buf
, build_options
);
722 printf("%s", buf
.buf
);
724 strbuf_release(&buf
);
729 struct similar_ref_cb
{
730 const char *base_ref
;
731 struct string_list
*similar_refs
;
734 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
735 int flags
, void *cb_data
)
737 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
738 char *branch
= strrchr(refname
, '/') + 1;
740 /* A remote branch of the same name is deemed similar */
741 if (starts_with(refname
, "refs/remotes/") &&
742 !strcmp(branch
, cb
->base_ref
))
743 string_list_append_nodup(cb
->similar_refs
,
744 shorten_unambiguous_ref(refname
, 1));
748 static struct string_list
guess_refs(const char *ref
)
750 struct similar_ref_cb ref_cb
;
751 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
753 ref_cb
.base_ref
= ref
;
754 ref_cb
.similar_refs
= &similar_refs
;
755 for_each_ref(append_similar_ref
, &ref_cb
);
759 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
763 struct string_list suggested_refs
= guess_refs(ref
);
765 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
767 if (suggested_refs
.nr
> 0) {
769 Q_("\nDid you mean this?",
770 "\nDid you mean one of these?",
772 for (i
= 0; i
< suggested_refs
.nr
; i
++)
773 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
776 string_list_clear(&suggested_refs
, 0);