5 #include "run-command.h"
6 #include "levenshtein.h"
8 #include "command-list.h"
9 #include "string-list.h"
13 #include "parse-options.h"
16 struct category_description
{
20 static uint32_t common_mask
=
21 CAT_init
| CAT_worktree
| CAT_info
|
22 CAT_history
| CAT_remote
;
23 static struct category_description common_categories
[] = {
24 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
25 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
26 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
27 { CAT_history
, N_("grow, mark and tweak your common history") },
28 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
31 static struct category_description main_categories
[] = {
32 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
33 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
34 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
35 { CAT_foreignscminterface
, N_("Interacting with Others") },
36 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
37 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
38 { CAT_synchingrepositories
, N_("Low-level Commands / Syncing Repositories") },
39 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
43 static const char *drop_prefix(const char *name
, uint32_t category
)
47 if (skip_prefix(name
, "git-", &new_name
))
49 if (category
== CAT_guide
&& skip_prefix(name
, "git", &new_name
))
55 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
58 struct cmdname_help
*cmds
;
60 if (ARRAY_SIZE(command_list
) == 0)
61 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
63 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
65 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
66 const struct cmdname_help
*cmd
= command_list
+ i
;
68 if (!(cmd
->category
& mask
))
72 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
80 static void print_command_list(const struct cmdname_help
*cmds
,
81 uint32_t mask
, int longest
)
85 for (i
= 0; cmds
[i
].name
; i
++) {
86 if (cmds
[i
].category
& mask
) {
87 size_t len
= strlen(cmds
[i
].name
);
88 printf(" %s ", cmds
[i
].name
);
90 mput_char(' ', longest
- len
);
91 puts(_(cmds
[i
].help
));
96 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
98 const struct cmdname_help
*e1
= elem1
;
99 const struct cmdname_help
*e2
= elem2
;
101 return strcmp(e1
->name
, e2
->name
);
104 static void print_cmd_by_category(const struct category_description
*catdesc
,
107 struct cmdname_help
*cmds
;
112 for (i
= 0; catdesc
[i
].desc
; i
++)
113 mask
|= catdesc
[i
].category
;
115 extract_cmds(&cmds
, mask
);
117 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
118 if (longest
< strlen(cmds
[i
].name
))
119 longest
= strlen(cmds
[i
].name
);
121 QSORT(cmds
, nr
, cmd_name_cmp
);
123 for (i
= 0; catdesc
[i
].desc
; i
++) {
124 uint32_t mask
= catdesc
[i
].category
;
125 const char *desc
= catdesc
[i
].desc
;
130 print_command_list(cmds
, mask
, longest
);
134 *longest_p
= longest
;
137 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
140 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
143 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
144 cmds
->names
[cmds
->cnt
++] = ent
;
147 static void clean_cmdnames(struct cmdnames
*cmds
)
150 for (i
= 0; i
< cmds
->cnt
; ++i
)
151 free(cmds
->names
[i
]);
157 static int cmdname_compare(const void *a_
, const void *b_
)
159 struct cmdname
*a
= *(struct cmdname
**)a_
;
160 struct cmdname
*b
= *(struct cmdname
**)b_
;
161 return strcmp(a
->name
, b
->name
);
164 static void uniq(struct cmdnames
*cmds
)
171 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
172 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
173 free(cmds
->names
[i
]);
175 cmds
->names
[j
++] = cmds
->names
[i
];
181 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
187 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
188 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
190 cmds
->names
[cj
++] = cmds
->names
[ci
++];
193 free(cmds
->names
[ci
++]);
198 while (ci
< cmds
->cnt
)
199 cmds
->names
[cj
++] = cmds
->names
[ci
++];
204 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
206 struct string_list list
= STRING_LIST_INIT_NODUP
;
207 struct column_options copts
;
210 for (i
= 0; i
< cmds
->cnt
; i
++)
211 string_list_append(&list
, cmds
->names
[i
]->name
);
213 * always enable column display, we only consult column.*
214 * about layout strategy and stuff
216 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
217 memset(&copts
, 0, sizeof(copts
));
220 print_columns(&list
, colopts
, &copts
);
221 string_list_clear(&list
, 0);
224 static void list_commands_in_dir(struct cmdnames
*cmds
,
228 DIR *dir
= opendir(path
);
230 struct strbuf buf
= STRBUF_INIT
;
238 strbuf_addf(&buf
, "%s/", path
);
241 while ((de
= readdir(dir
)) != NULL
) {
245 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
248 strbuf_setlen(&buf
, len
);
249 strbuf_addstr(&buf
, de
->d_name
);
250 if (!is_executable(buf
.buf
))
253 entlen
= strlen(ent
);
254 strip_suffix(ent
, ".exe", &entlen
);
256 add_cmdname(cmds
, ent
, entlen
);
259 strbuf_release(&buf
);
262 void load_command_list(const char *prefix
,
263 struct cmdnames
*main_cmds
,
264 struct cmdnames
*other_cmds
)
266 const char *env_path
= getenv("PATH");
267 const char *exec_path
= git_exec_path();
269 load_builtin_commands(prefix
, main_cmds
);
272 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
273 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
278 char *paths
, *path
, *colon
;
279 path
= paths
= xstrdup(env_path
);
281 if ((colon
= strchr(path
, PATH_SEP
)))
283 if (!exec_path
|| strcmp(path
, exec_path
))
284 list_commands_in_dir(other_cmds
, path
, prefix
);
292 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
295 exclude_cmds(other_cmds
, main_cmds
);
298 static int get_colopts(const char *var
, const char *value
, void *data
)
300 unsigned int *colopts
= data
;
302 if (starts_with(var
, "column."))
303 return git_column_config(var
, value
, "help", colopts
);
308 void list_commands(struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
310 unsigned int colopts
= 0;
311 git_config(get_colopts
, &colopts
);
313 if (main_cmds
->cnt
) {
314 const char *exec_path
= git_exec_path();
315 printf_ln(_("available git commands in '%s'"), exec_path
);
317 pretty_print_cmdnames(main_cmds
, colopts
);
321 if (other_cmds
->cnt
) {
322 puts(_("git commands available from elsewhere on your $PATH"));
324 pretty_print_cmdnames(other_cmds
, colopts
);
329 void list_common_cmds_help(void)
331 puts(_("These are common Git commands used in various situations:"));
333 print_cmd_by_category(common_categories
, NULL
);
336 void list_all_main_cmds(struct string_list
*list
)
338 struct cmdnames main_cmds
, other_cmds
;
341 memset(&main_cmds
, 0, sizeof(main_cmds
));
342 memset(&other_cmds
, 0, sizeof(other_cmds
));
343 load_command_list("git-", &main_cmds
, &other_cmds
);
345 for (i
= 0; i
< main_cmds
.cnt
; i
++)
346 string_list_append(list
, main_cmds
.names
[i
]->name
);
348 clean_cmdnames(&main_cmds
);
349 clean_cmdnames(&other_cmds
);
352 void list_all_other_cmds(struct string_list
*list
)
354 struct cmdnames main_cmds
, other_cmds
;
357 memset(&main_cmds
, 0, sizeof(main_cmds
));
358 memset(&other_cmds
, 0, sizeof(other_cmds
));
359 load_command_list("git-", &main_cmds
, &other_cmds
);
361 for (i
= 0; i
< other_cmds
.cnt
; i
++)
362 string_list_append(list
, other_cmds
.names
[i
]->name
);
364 clean_cmdnames(&main_cmds
);
365 clean_cmdnames(&other_cmds
);
368 void list_cmds_by_category(struct string_list
*list
,
371 int i
, n
= ARRAY_SIZE(command_list
);
374 for (i
= 0; category_names
[i
]; i
++) {
375 if (!strcmp(cat
, category_names
[i
])) {
381 die(_("unsupported command listing type '%s'"), cat
);
383 for (i
= 0; i
< n
; i
++) {
384 struct cmdname_help
*cmd
= command_list
+ i
;
386 if (!(cmd
->category
& cat_id
))
388 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
392 void list_cmds_by_config(struct string_list
*list
)
394 const char *cmd_list
;
396 if (git_config_get_string_tmp("completion.commands", &cmd_list
))
399 string_list_sort(list
);
400 string_list_remove_duplicates(list
, 0);
403 struct strbuf sb
= STRBUF_INIT
;
404 const char *p
= strchrnul(cmd_list
, ' ');
406 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
407 if (sb
.buf
[0] == '-')
408 string_list_remove(list
, sb
.buf
+ 1, 0);
410 string_list_insert(list
, sb
.buf
);
418 void list_guides_help(void)
420 struct category_description catdesc
[] = {
421 { CAT_guide
, N_("The Git concept guides are:") },
424 print_cmd_by_category(catdesc
, NULL
);
428 static int get_alias(const char *var
, const char *value
, void *data
)
430 struct string_list
*list
= data
;
432 if (skip_prefix(var
, "alias.", &var
))
433 string_list_append(list
, var
)->util
= xstrdup(value
);
438 static void list_all_cmds_help_external_commands(void)
440 struct string_list others
= STRING_LIST_INIT_DUP
;
443 list_all_other_cmds(&others
);
445 printf("\n%s\n", _("External commands"));
446 for (i
= 0; i
< others
.nr
; i
++)
447 printf(" %s\n", others
.items
[i
].string
);
448 string_list_clear(&others
, 0);
451 static void list_all_cmds_help_aliases(int longest
)
453 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
454 struct cmdname_help
*aliases
;
457 git_config(get_alias
, &alias_list
);
458 string_list_sort(&alias_list
);
460 for (i
= 0; i
< alias_list
.nr
; i
++) {
461 size_t len
= strlen(alias_list
.items
[i
].string
);
467 printf("\n%s\n", _("Command aliases"));
468 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
469 for (i
= 0; i
< alias_list
.nr
; i
++) {
470 aliases
[i
].name
= alias_list
.items
[i
].string
;
471 aliases
[i
].help
= alias_list
.items
[i
].util
;
472 aliases
[i
].category
= 1;
474 aliases
[alias_list
.nr
].name
= NULL
;
475 print_command_list(aliases
, 1, longest
);
478 string_list_clear(&alias_list
, 1);
481 void list_all_cmds_help(int show_external_commands
, int show_aliases
)
485 puts(_("See 'git help <command>' to read about a specific subcommand"));
487 print_cmd_by_category(main_categories
, &longest
);
489 if (show_external_commands
)
490 list_all_cmds_help_external_commands();
492 list_all_cmds_help_aliases(longest
);
495 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
498 for (i
= 0; i
< c
->cnt
; i
++)
499 if (!strcmp(s
, c
->names
[i
]->name
))
504 static int autocorrect
;
505 static struct cmdnames aliases
;
507 #define AUTOCORRECT_PROMPT (-3)
508 #define AUTOCORRECT_NEVER (-2)
509 #define AUTOCORRECT_IMMEDIATELY (-1)
511 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
515 if (!strcmp(var
, "help.autocorrect")) {
517 return config_error_nonbool(var
);
518 if (!strcmp(value
, "never")) {
519 autocorrect
= AUTOCORRECT_NEVER
;
520 } else if (!strcmp(value
, "immediate")) {
521 autocorrect
= AUTOCORRECT_IMMEDIATELY
;
522 } else if (!strcmp(value
, "prompt")) {
523 autocorrect
= AUTOCORRECT_PROMPT
;
525 int v
= git_config_int(var
, value
);
526 autocorrect
= (v
< 0)
527 ? AUTOCORRECT_IMMEDIATELY
: v
;
530 /* Also use aliases for command lookup */
531 if (skip_prefix(var
, "alias.", &p
))
532 add_cmdname(&aliases
, p
, strlen(p
));
534 return git_default_config(var
, value
, cb
);
537 static int levenshtein_compare(const void *p1
, const void *p2
)
539 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
540 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
543 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
546 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
549 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
551 for (i
= 0; i
< old
->cnt
; i
++)
552 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
553 FREE_AND_NULL(old
->names
);
557 /* An empirically derived magic number */
558 #define SIMILARITY_FLOOR 7
559 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
561 static const char bad_interpreter_advice
[] =
562 N_("'%s' appears to be a git command, but we were not\n"
563 "able to execute it. Maybe git-%s is broken?");
565 const char *help_unknown_cmd(const char *cmd
)
567 int i
, n
, best_similarity
= 0;
568 struct cmdnames main_cmds
, other_cmds
;
569 struct cmdname_help
*common_cmds
;
571 memset(&main_cmds
, 0, sizeof(main_cmds
));
572 memset(&other_cmds
, 0, sizeof(other_cmds
));
573 memset(&aliases
, 0, sizeof(aliases
));
575 read_early_config(git_unknown_cmd_config
, NULL
);
578 * Disable autocorrection prompt in a non-interactive session
580 if ((autocorrect
== AUTOCORRECT_PROMPT
) && (!isatty(0) || !isatty(2)))
581 autocorrect
= AUTOCORRECT_NEVER
;
583 if (autocorrect
== AUTOCORRECT_NEVER
) {
584 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
588 load_command_list("git-", &main_cmds
, &other_cmds
);
590 add_cmd_list(&main_cmds
, &aliases
);
591 add_cmd_list(&main_cmds
, &other_cmds
);
592 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
595 extract_cmds(&common_cmds
, common_mask
);
597 /* This abuses cmdname->len for levenshtein distance */
598 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
599 int cmp
= 0; /* avoid compiler stupidity */
600 const char *candidate
= main_cmds
.names
[i
]->name
;
603 * An exact match means we have the command, but
604 * for some reason exec'ing it gave us ENOENT; probably
605 * it's a bad interpreter in the #! line.
607 if (!strcmp(candidate
, cmd
))
608 die(_(bad_interpreter_advice
), cmd
, cmd
);
610 /* Does the candidate appear in common_cmds list? */
611 while (common_cmds
[n
].name
&&
612 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
614 if (common_cmds
[n
].name
&& !cmp
) {
615 /* Yes, this is one of the common commands */
616 n
++; /* use the entry from common_cmds[] */
617 if (starts_with(candidate
, cmd
)) {
618 /* Give prefix match a very good score */
619 main_cmds
.names
[i
]->len
= 0;
624 main_cmds
.names
[i
]->len
=
625 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
627 FREE_AND_NULL(common_cmds
);
629 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
632 die(_("Uh oh. Your system reports no Git commands at all."));
634 /* skip and count prefix matches */
635 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
636 ; /* still counting */
638 if (main_cmds
.cnt
<= n
) {
639 /* prefix matches with everything? that is too ambiguous */
640 best_similarity
= SIMILARITY_FLOOR
+ 1;
642 /* count all the most similar ones */
643 for (best_similarity
= main_cmds
.names
[n
++]->len
;
644 (n
< main_cmds
.cnt
&&
645 best_similarity
== main_cmds
.names
[n
]->len
);
647 ; /* still counting */
649 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
650 const char *assumed
= main_cmds
.names
[0]->name
;
651 main_cmds
.names
[0] = NULL
;
652 clean_cmdnames(&main_cmds
);
654 _("WARNING: You called a Git command named '%s', "
655 "which does not exist."),
657 if (autocorrect
== AUTOCORRECT_IMMEDIATELY
)
659 _("Continuing under the assumption that "
662 else if (autocorrect
== AUTOCORRECT_PROMPT
) {
664 struct strbuf msg
= STRBUF_INIT
;
665 strbuf_addf(&msg
, _("Run '%s' instead [y/N]? "), assumed
);
666 answer
= git_prompt(msg
.buf
, PROMPT_ECHO
);
667 strbuf_release(&msg
);
668 if (!(starts_with(answer
, "y") ||
669 starts_with(answer
, "Y")))
673 _("Continuing in %0.1f seconds, "
674 "assuming that you meant '%s'."),
675 (float)autocorrect
/10.0, assumed
);
676 sleep_millisec(autocorrect
* 100);
681 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
683 if (SIMILAR_ENOUGH(best_similarity
)) {
685 Q_("\nThe most similar command is",
686 "\nThe most similar commands are",
689 for (i
= 0; i
< n
; i
++)
690 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
696 void get_version_info(struct strbuf
*buf
, int show_build_options
)
699 * The format of this string should be kept stable for compatibility
700 * with external projects that rely on the output of "git version".
702 * Always show the version, even if other options are given.
704 strbuf_addf(buf
, "git version %s\n", git_version_string
);
706 if (show_build_options
) {
707 strbuf_addf(buf
, "cpu: %s\n", GIT_HOST_CPU
);
708 if (git_built_from_commit_string
[0])
709 strbuf_addf(buf
, "built from commit: %s\n",
710 git_built_from_commit_string
);
712 strbuf_addstr(buf
, "no commit associated with this build\n");
713 strbuf_addf(buf
, "sizeof-long: %d\n", (int)sizeof(long));
714 strbuf_addf(buf
, "sizeof-size_t: %d\n", (int)sizeof(size_t));
715 strbuf_addf(buf
, "shell-path: %s\n", SHELL_PATH
);
716 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
720 int cmd_version(int argc
, const char **argv
, const char *prefix
)
722 struct strbuf buf
= STRBUF_INIT
;
723 int build_options
= 0;
724 const char * const usage
[] = {
725 N_("git version [<options>]"),
728 struct option options
[] = {
729 OPT_BOOL(0, "build-options", &build_options
,
730 "also print build options"),
734 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
736 get_version_info(&buf
, build_options
);
737 printf("%s", buf
.buf
);
739 strbuf_release(&buf
);
744 struct similar_ref_cb
{
745 const char *base_ref
;
746 struct string_list
*similar_refs
;
749 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
750 int flags
, void *cb_data
)
752 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
753 char *branch
= strrchr(refname
, '/') + 1;
755 /* A remote branch of the same name is deemed similar */
756 if (starts_with(refname
, "refs/remotes/") &&
757 !strcmp(branch
, cb
->base_ref
))
758 string_list_append_nodup(cb
->similar_refs
,
759 shorten_unambiguous_ref(refname
, 1));
763 static struct string_list
guess_refs(const char *ref
)
765 struct similar_ref_cb ref_cb
;
766 struct string_list similar_refs
= STRING_LIST_INIT_DUP
;
768 ref_cb
.base_ref
= ref
;
769 ref_cb
.similar_refs
= &similar_refs
;
770 for_each_ref(append_similar_ref
, &ref_cb
);
774 NORETURN
void help_unknown_ref(const char *ref
, const char *cmd
,
778 struct string_list suggested_refs
= guess_refs(ref
);
780 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
782 if (suggested_refs
.nr
> 0) {
784 Q_("\nDid you mean this?",
785 "\nDid you mean one of these?",
787 for (i
= 0; i
< suggested_refs
.nr
; i
++)
788 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
791 string_list_clear(&suggested_refs
, 0);