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
;
131 print_command_list(cmds
, mask
, longest
);
135 *longest_p
= longest
;
138 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
141 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
144 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
145 cmds
->names
[cmds
->cnt
++] = ent
;
148 static void clean_cmdnames(struct cmdnames
*cmds
)
151 for (i
= 0; i
< cmds
->cnt
; ++i
)
152 free(cmds
->names
[i
]);
158 static int cmdname_compare(const void *a_
, const void *b_
)
160 struct cmdname
*a
= *(struct cmdname
**)a_
;
161 struct cmdname
*b
= *(struct cmdname
**)b_
;
162 return strcmp(a
->name
, b
->name
);
165 static void uniq(struct cmdnames
*cmds
)
172 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
173 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
174 free(cmds
->names
[i
]);
176 cmds
->names
[j
++] = cmds
->names
[i
];
182 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
188 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
189 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
191 cmds
->names
[cj
++] = cmds
->names
[ci
++];
194 free(cmds
->names
[ci
++]);
199 while (ci
< cmds
->cnt
)
200 cmds
->names
[cj
++] = cmds
->names
[ci
++];
205 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
207 struct string_list list
= STRING_LIST_INIT_NODUP
;
208 struct column_options copts
;
211 for (i
= 0; i
< cmds
->cnt
; i
++)
212 string_list_append(&list
, cmds
->names
[i
]->name
);
214 * always enable column display, we only consult column.*
215 * about layout strategy and stuff
217 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
218 memset(&copts
, 0, sizeof(copts
));
221 print_columns(&list
, colopts
, &copts
);
222 string_list_clear(&list
, 0);
225 static void list_commands_in_dir(struct cmdnames
*cmds
,
229 DIR *dir
= opendir(path
);
231 struct strbuf buf
= STRBUF_INIT
;
239 strbuf_addf(&buf
, "%s/", path
);
242 while ((de
= readdir(dir
)) != NULL
) {
246 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
249 strbuf_setlen(&buf
, len
);
250 strbuf_addstr(&buf
, de
->d_name
);
251 if (!is_executable(buf
.buf
))
254 entlen
= strlen(ent
);
255 strip_suffix(ent
, ".exe", &entlen
);
257 add_cmdname(cmds
, ent
, entlen
);
260 strbuf_release(&buf
);
263 void load_command_list(const char *prefix
,
264 struct cmdnames
*main_cmds
,
265 struct cmdnames
*other_cmds
)
267 const char *env_path
= getenv("PATH");
268 const char *exec_path
= git_exec_path();
270 load_builtin_commands(prefix
, main_cmds
);
273 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
274 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
279 char *paths
, *path
, *colon
;
280 path
= paths
= xstrdup(env_path
);
282 if ((colon
= strchr(path
, PATH_SEP
)))
284 if (!exec_path
|| strcmp(path
, exec_path
))
285 list_commands_in_dir(other_cmds
, path
, prefix
);
293 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
296 exclude_cmds(other_cmds
, main_cmds
);
299 static int get_colopts(const char *var
, const char *value
, void *data
)
301 unsigned int *colopts
= data
;
303 if (starts_with(var
, "column."))
304 return git_column_config(var
, value
, "help", colopts
);
309 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
311 unsigned int colopts
= 0;
312 git_config(get_colopts
, &colopts
);
314 if (main_cmds
->cnt
) {
315 const char *exec_path
= git_exec_path();
316 printf_ln(_("available git commands in '%s'"), exec_path
);
318 pretty_print_cmdnames(main_cmds
, colopts
);
322 if (other_cmds
->cnt
) {
323 puts(_("git commands available from elsewhere on your $PATH"));
325 pretty_print_cmdnames(other_cmds
, colopts
);
330 void list_common_cmds_help(void)
332 puts(_("These are common Git commands used in various situations:"));
334 print_cmd_by_category(common_categories
, NULL
);
337 void list_all_main_cmds(struct string_list
*list
)
339 struct cmdnames main_cmds
, other_cmds
;
342 memset(&main_cmds
, 0, sizeof(main_cmds
));
343 memset(&other_cmds
, 0, sizeof(other_cmds
));
344 load_command_list("git-", &main_cmds
, &other_cmds
);
346 for (i
= 0; i
< main_cmds
.cnt
; i
++)
347 string_list_append(list
, main_cmds
.names
[i
]->name
);
349 clean_cmdnames(&main_cmds
);
350 clean_cmdnames(&other_cmds
);
353 void list_all_other_cmds(struct string_list
*list
)
355 struct cmdnames main_cmds
, other_cmds
;
358 memset(&main_cmds
, 0, sizeof(main_cmds
));
359 memset(&other_cmds
, 0, sizeof(other_cmds
));
360 load_command_list("git-", &main_cmds
, &other_cmds
);
362 for (i
= 0; i
< other_cmds
.cnt
; i
++)
363 string_list_append(list
, other_cmds
.names
[i
]->name
);
365 clean_cmdnames(&main_cmds
);
366 clean_cmdnames(&other_cmds
);
369 void list_cmds_by_category(struct string_list
*list
,
372 int i
, n
= ARRAY_SIZE(command_list
);
375 for (i
= 0; category_names
[i
]; i
++) {
376 if (!strcmp(cat
, category_names
[i
])) {
382 die(_("unsupported command listing type '%s'"), cat
);
384 for (i
= 0; i
< n
; i
++) {
385 struct cmdname_help
*cmd
= command_list
+ i
;
387 if (!(cmd
->category
& cat_id
))
389 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
393 void list_cmds_by_config(struct string_list
*list
)
395 const char *cmd_list
;
397 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
400 string_list_sort(list
);
401 string_list_remove_duplicates(list
, 0);
404 struct strbuf sb
= STRBUF_INIT
;
405 const char *p
= strchrnul(cmd_list
, ' ');
407 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
408 if (sb
.buf
[0] == '-')
409 string_list_remove(list
, sb
.buf
+ 1, 0);
411 string_list_insert(list
, sb
.buf
);
419 void list_guides_help(void)
421 struct category_description catdesc
[] = {
422 { CAT_guide
, N_("The Git concept guides are:") },
425 print_cmd_by_category(catdesc
, NULL
);
429 static int get_alias(const char *var
, const char *value
, void *data
)
431 struct string_list
*list
= data
;
433 if (skip_prefix(var
, "alias.", &var
))
434 string_list_append(list
, var
)->util
= xstrdup(value
);
439 static void list_all_cmds_help_external_commands(void)
441 struct string_list others
= STRING_LIST_INIT_DUP
;
444 list_all_other_cmds(&others
);
446 printf("\n%s\n", _("External commands"));
447 for (i
= 0; i
< others
.nr
; i
++)
448 printf(" %s\n", others
.items
[i
].string
);
449 string_list_clear(&others
, 0);
452 static void list_all_cmds_help_aliases(int longest
)
454 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
455 struct cmdname_help
*aliases
;
458 git_config(get_alias
, &alias_list
);
459 string_list_sort(&alias_list
);
461 for (i
= 0; i
< alias_list
.nr
; i
++) {
462 size_t len
= strlen(alias_list
.items
[i
].string
);
468 printf("\n%s\n", _("Command aliases"));
469 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
470 for (i
= 0; i
< alias_list
.nr
; i
++) {
471 aliases
[i
].name
= alias_list
.items
[i
].string
;
472 aliases
[i
].help
= alias_list
.items
[i
].util
;
473 aliases
[i
].category
= 1;
475 aliases
[alias_list
.nr
].name
= NULL
;
476 print_command_list(aliases
, 1, longest
);
479 string_list_clear(&alias_list
, 1);
482 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
486 puts(_("See 'git help <command>' to read about a specific subcommand"));
488 print_cmd_by_category(main_categories
, &longest
);
490 if (show_external_commands
)
491 list_all_cmds_help_external_commands();
493 list_all_cmds_help_aliases(longest
);
496 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
499 for (i
= 0; i
< c
->cnt
; i
++)
500 if (!strcmp(s
, c
->names
[i
]->name
))
505 static int autocorrect
;
506 static struct cmdnames aliases
;
508 #define AUTOCORRECT_PROMPT (-3)
509 #define AUTOCORRECT_NEVER (-2)
510 #define AUTOCORRECT_IMMEDIATELY (-1)
512 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
516 if (!strcmp(var
, "help.autocorrect")) {
518 return config_error_nonbool(var
);
519 if (!strcmp(value
, "never")) {
520 autocorrect
= AUTOCORRECT_NEVER
;
521 } else if (!strcmp(value
, "immediate")) {
522 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
523 } else if (!strcmp(value
, "prompt")) {
524 autocorrect
= AUTOCORRECT_PROMPT
;
526 int v
= git_config_int(var
, value
);
527 autocorrect
= (v
< 0)
528 ? AUTOCORRECT_IMMEDIATELY
: v
;
531 /* Also use aliases for command lookup */
532 if (skip_prefix(var
, "alias.", &p
))
533 add_cmdname(&aliases
, p
, strlen(p
));
535 return git_default_config(var
, value
, cb
);
538 static int levenshtein_compare(const void *p1
, const void *p2
)
540 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
541 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
544 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
547 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
550 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
552 for (i
= 0; i
< old
->cnt
; i
++)
553 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
554 FREE_AND_NULL(old
->names
);
558 /* An empirically derived magic number */
559 #define SIMILARITY_FLOOR 7
560 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
562 static const char bad_interpreter_advice
[] =
563 N_("'%s' appears to be a git command, but we were not\n"
564 "able to execute it. Maybe git-%s is broken?");
566 const char *help_unknown_cmd(const char *cmd
)
568 int i
, n
, best_similarity
= 0;
569 struct cmdnames main_cmds
, other_cmds
;
570 struct cmdname_help
*common_cmds
;
572 memset(&main_cmds
, 0, sizeof(main_cmds
));
573 memset(&other_cmds
, 0, sizeof(other_cmds
));
574 memset(&aliases
, 0, sizeof(aliases
));
576 read_early_config(git_unknown_cmd_config
, NULL
);
579 * Disable autocorrection prompt in a non-interactive session
581 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
582 autocorrect
= AUTOCORRECT_NEVER
;
584 if (autocorrect
== AUTOCORRECT_NEVER
) {
585 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
589 load_command_list("git-", &main_cmds
, &other_cmds
);
591 add_cmd_list(&main_cmds
, &aliases
);
592 add_cmd_list(&main_cmds
, &other_cmds
);
593 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
596 extract_cmds(&common_cmds
, common_mask
);
598 /* This abuses cmdname->len for levenshtein distance */
599 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
600 int cmp
= 0; /* avoid compiler stupidity */
601 const char *candidate
= main_cmds
.names
[i
]->name
;
604 * An exact match means we have the command, but
605 * for some reason exec'ing it gave us ENOENT; probably
606 * it's a bad interpreter in the #! line.
608 if (!strcmp(candidate
, cmd
))
609 die(_(bad_interpreter_advice
), cmd
, cmd
);
611 /* Does the candidate appear in common_cmds list? */
612 while (common_cmds
[n
].name
&&
613 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
615 if (common_cmds
[n
].name
&& !cmp
) {
616 /* Yes, this is one of the common commands */
617 n
++; /* use the entry from common_cmds[] */
618 if (starts_with(candidate
, cmd
)) {
619 /* Give prefix match a very good score */
620 main_cmds
.names
[i
]->len
= 0;
625 main_cmds
.names
[i
]->len
=
626 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
628 FREE_AND_NULL(common_cmds
);
630 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
633 die(_("Uh oh. Your system reports no Git commands at all."));
635 /* skip and count prefix matches */
636 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
637 ; /* still counting */
639 if (main_cmds
.cnt
<= n
) {
640 /* prefix matches with everything? that is too ambiguous */
641 best_similarity
= SIMILARITY_FLOOR
+ 1;
643 /* count all the most similar ones */
644 for (best_similarity
= main_cmds
.names
[n
++]->len
;
645 (n
< main_cmds
.cnt
&&
646 best_similarity
== main_cmds
.names
[n
]->len
);
648 ; /* still counting */
650 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
651 const char *assumed
= main_cmds
.names
[0]->name
;
652 main_cmds
.names
[0] = NULL
;
653 clean_cmdnames(&main_cmds
);
655 _("WARNING: You called a Git command named '%s', "
656 "which does not exist."),
658 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
660 _("Continuing under the assumption that "
663 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
665 struct strbuf msg
= STRBUF_INIT
;
666 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
667 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
668 strbuf_release(&msg
);
669 if (!(starts_with(answer
, "y") ||
670 starts_with(answer
, "Y")))
674 _("Continuing in %0.1f seconds, "
675 "assuming that you meant '%s'."),
676 (float)autocorrect
/10.0, assumed
);
677 sleep_millisec(autocorrect
* 100);
682 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
684 if (SIMILAR_ENOUGH(best_similarity
)) {
686 Q_("\nThe most similar command is",
687 "\nThe most similar commands are",
690 for (i
= 0; i
< n
; i
++)
691 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
697 void get_version_info(struct strbuf
*buf
, int show_build_options
)
700 * The format of this string should be kept stable for compatibility
701 * with external projects that rely on the output of "git version".
703 * Always show the version, even if other options are given.
705 strbuf_addf(buf
, "git version %s\n", git_version_string
);
707 if (show_build_options
) {
708 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
709 if (git_built_from_commit_string
[0])
710 strbuf_addf(buf
, "built from commit: %s\n",
711 git_built_from_commit_string
);
713 strbuf_addstr(buf
, "no commit associated with this build\n");
714 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
715 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
716 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
717 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
719 if (fsmonitor_ipc__is_supported())
720 strbuf_addstr(buf
, "feature: fsmonitor--daemon\n");
724 int cmd_version(int argc
, const char **argv
, const char *prefix
)
726 struct strbuf buf
= STRBUF_INIT
;
727 int build_options
= 0;
728 const char * const usage
[] = {
729 N_("git version [<options>]"),
732 struct option options
[] = {
733 OPT_BOOL(0, "build-options", &build_options
,
734 "also print build options"),
738 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
740 get_version_info(&buf
, build_options
);
741 printf("%s", buf
.buf
);
743 strbuf_release(&buf
);
748 struct similar_ref_cb
{
749 const char *base_ref
;
750 struct string_list
*similar_refs
;
753 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
754 int flags
, void *cb_data
)
756 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
757 char *branch
= strrchr(refname
, '/') + 1;
759 /* A remote branch of the same name is deemed similar */
760 if (starts_with(refname
, "refs/remotes/") &&
761 !strcmp(branch
, cb
->base_ref
))
762 string_list_append_nodup(cb
->similar_refs
,
763 shorten_unambiguous_ref(refname
, 1));
767 static struct string_list
guess_refs(const char *ref
)
769 struct similar_ref_cb ref_cb
;
770 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
772 ref_cb
.base_ref
= ref
;
773 ref_cb
.similar_refs
= &similar_refs
;
774 for_each_ref(append_similar_ref
, &ref_cb
);
778 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
782 struct string_list suggested_refs
= guess_refs(ref
);
784 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
786 if (suggested_refs
.nr
> 0) {
788 Q_("\nDid you mean this?",
789 "\nDid you mean one of these?",
791 for (i
= 0; i
< suggested_refs
.nr
; i
++)
792 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
795 string_list_clear(&suggested_refs
, 0);