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 struct category_description
{
19 static uint32_t common_mask
=
20 CAT_init
| CAT_worktree
| CAT_info
|
21 CAT_history
| CAT_remote
;
22 static struct category_description common_categories
[] = {
23 { CAT_init
, N_("start a working area (see also: git help tutorial)") },
24 { CAT_worktree
, N_("work on the current change (see also: git help everyday)") },
25 { CAT_info
, N_("examine the history and state (see also: git help revisions)") },
26 { CAT_history
, N_("grow, mark and tweak your common history") },
27 { CAT_remote
, N_("collaborate (see also: git help workflows)") },
30 static struct category_description main_categories
[] = {
31 { CAT_mainporcelain
, N_("Main Porcelain Commands") },
32 { CAT_ancillarymanipulators
, N_("Ancillary Commands / Manipulators") },
33 { CAT_ancillaryinterrogators
, N_("Ancillary Commands / Interrogators") },
34 { CAT_foreignscminterface
, N_("Interacting with Others") },
35 { CAT_plumbingmanipulators
, N_("Low-level Commands / Manipulators") },
36 { CAT_plumbinginterrogators
, N_("Low-level Commands / Interrogators") },
37 { CAT_synchingrepositories
, N_("Low-level Commands / Synching Repositories") },
38 { CAT_purehelpers
, N_("Low-level Commands / Internal Helpers") },
42 static const char *drop_prefix(const char *name
, uint32_t category
)
46 if (skip_prefix(name
, "git-", &new_name
))
48 if (category
== CAT_guide
&& skip_prefix(name
, "git", &new_name
))
54 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
57 struct cmdname_help
*cmds
;
59 if (ARRAY_SIZE(command_list
) == 0)
60 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
62 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
64 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
65 const struct cmdname_help
*cmd
= command_list
+ i
;
67 if (!(cmd
->category
& mask
))
71 cmds
[nr
].name
= drop_prefix(cmd
->name
, cmd
->category
);
79 static void print_command_list(const struct cmdname_help
*cmds
,
80 uint32_t mask
, int longest
)
84 for (i
= 0; cmds
[i
].name
; i
++) {
85 if (cmds
[i
].category
& mask
) {
86 size_t len
= strlen(cmds
[i
].name
);
87 printf(" %s ", cmds
[i
].name
);
88 mput_char(' ', longest
> len
? longest
- len
: 1);
89 puts(_(cmds
[i
].help
));
94 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
96 const struct cmdname_help
*e1
= elem1
;
97 const struct cmdname_help
*e2
= elem2
;
99 return strcmp(e1
->name
, e2
->name
);
102 static void print_cmd_by_category(const struct category_description
*catdesc
,
105 struct cmdname_help
*cmds
;
110 for (i
= 0; catdesc
[i
].desc
; i
++)
111 mask
|= catdesc
[i
].category
;
113 extract_cmds(&cmds
, mask
);
115 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
116 if (longest
< strlen(cmds
[i
].name
))
117 longest
= strlen(cmds
[i
].name
);
119 QSORT(cmds
, nr
, cmd_name_cmp
);
121 for (i
= 0; catdesc
[i
].desc
; i
++) {
122 uint32_t mask
= catdesc
[i
].category
;
123 const char *desc
= catdesc
[i
].desc
;
125 printf("\n%s\n", _(desc
));
126 print_command_list(cmds
, mask
, longest
);
130 *longest_p
= longest
;
133 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
136 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
139 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
140 cmds
->names
[cmds
->cnt
++] = ent
;
143 static void clean_cmdnames(struct cmdnames
*cmds
)
146 for (i
= 0; i
< cmds
->cnt
; ++i
)
147 free(cmds
->names
[i
]);
153 static int cmdname_compare(const void *a_
, const void *b_
)
155 struct cmdname
*a
= *(struct cmdname
**)a_
;
156 struct cmdname
*b
= *(struct cmdname
**)b_
;
157 return strcmp(a
->name
, b
->name
);
160 static void uniq(struct cmdnames
*cmds
)
167 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
168 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
169 free(cmds
->names
[i
]);
171 cmds
->names
[j
++] = cmds
->names
[i
];
177 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
183 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
184 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
186 cmds
->names
[cj
++] = cmds
->names
[ci
++];
189 free(cmds
->names
[ci
++]);
194 while (ci
< cmds
->cnt
)
195 cmds
->names
[cj
++] = cmds
->names
[ci
++];
200 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
202 struct string_list list
= STRING_LIST_INIT_NODUP
;
203 struct column_options copts
;
206 for (i
= 0; i
< cmds
->cnt
; i
++)
207 string_list_append(&list
, cmds
->names
[i
]->name
);
209 * always enable column display, we only consult column.*
210 * about layout strategy and stuff
212 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
213 memset(&copts
, 0, sizeof(copts
));
216 print_columns(&list
, colopts
, &copts
);
217 string_list_clear(&list
, 0);
220 static void list_commands_in_dir(struct cmdnames
*cmds
,
224 DIR *dir
= opendir(path
);
226 struct strbuf buf
= STRBUF_INIT
;
234 strbuf_addf(&buf
, "%s/", path
);
237 while ((de
= readdir(dir
)) != NULL
) {
241 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
244 strbuf_setlen(&buf
, len
);
245 strbuf_addstr(&buf
, de
->d_name
);
246 if (!is_executable(buf
.buf
))
249 entlen
= strlen(ent
);
250 strip_suffix(ent
, ".exe", &entlen
);
252 add_cmdname(cmds
, ent
, entlen
);
255 strbuf_release(&buf
);
258 void load_command_list(const char *prefix
,
259 struct cmdnames
*main_cmds
,
260 struct cmdnames
*other_cmds
)
262 const char *env_path
= getenv("PATH");
263 const char *exec_path
= git_exec_path();
266 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
267 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
272 char *paths
, *path
, *colon
;
273 path
= paths
= xstrdup(env_path
);
275 if ((colon
= strchr(path
, PATH_SEP
)))
277 if (!exec_path
|| strcmp(path
, exec_path
))
278 list_commands_in_dir(other_cmds
, path
, prefix
);
286 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
289 exclude_cmds(other_cmds
, main_cmds
);
292 void list_commands(unsigned int colopts
,
293 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
295 if (main_cmds
->cnt
) {
296 const char *exec_path
= git_exec_path();
297 printf_ln(_("available git commands in '%s'"), exec_path
);
299 pretty_print_cmdnames(main_cmds
, colopts
);
303 if (other_cmds
->cnt
) {
304 printf_ln(_("git commands available from elsewhere on your $PATH"));
306 pretty_print_cmdnames(other_cmds
, colopts
);
311 void list_common_cmds_help(void)
313 puts(_("These are common Git commands used in various situations:"));
314 print_cmd_by_category(common_categories
, NULL
);
317 void list_all_main_cmds(struct string_list
*list
)
319 struct cmdnames main_cmds
, other_cmds
;
322 memset(&main_cmds
, 0, sizeof(main_cmds
));
323 memset(&other_cmds
, 0, sizeof(other_cmds
));
324 load_command_list("git-", &main_cmds
, &other_cmds
);
326 for (i
= 0; i
< main_cmds
.cnt
; i
++)
327 string_list_append(list
, main_cmds
.names
[i
]->name
);
329 clean_cmdnames(&main_cmds
);
330 clean_cmdnames(&other_cmds
);
333 void list_all_other_cmds(struct string_list
*list
)
335 struct cmdnames main_cmds
, other_cmds
;
338 memset(&main_cmds
, 0, sizeof(main_cmds
));
339 memset(&other_cmds
, 0, sizeof(other_cmds
));
340 load_command_list("git-", &main_cmds
, &other_cmds
);
342 for (i
= 0; i
< other_cmds
.cnt
; i
++)
343 string_list_append(list
, other_cmds
.names
[i
]->name
);
345 clean_cmdnames(&main_cmds
);
346 clean_cmdnames(&other_cmds
);
349 void list_cmds_by_category(struct string_list
*list
,
352 int i
, n
= ARRAY_SIZE(command_list
);
355 for (i
= 0; category_names
[i
]; i
++) {
356 if (!strcmp(cat
, category_names
[i
])) {
362 die(_("unsupported command listing type '%s'"), cat
);
364 for (i
= 0; i
< n
; i
++) {
365 struct cmdname_help
*cmd
= command_list
+ i
;
367 if (!(cmd
->category
& cat_id
))
369 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
373 void list_cmds_by_config(struct string_list
*list
)
375 const char *cmd_list
;
378 * There's no actual repository setup at this point (and even
379 * if there is, we don't really care; only global config
380 * matters). If we accidentally set up a repository, it's ok
381 * too since the caller (git --list-cmds=) should exit shortly
384 if (git_config_get_string_const("completion.commands", &cmd_list
))
387 string_list_sort(list
);
388 string_list_remove_duplicates(list
, 0);
391 struct strbuf sb
= STRBUF_INIT
;
392 const char *p
= strchrnul(cmd_list
, ' ');
394 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
395 if (*cmd_list
== '-')
396 string_list_remove(list
, cmd_list
+ 1, 0);
398 string_list_insert(list
, sb
.buf
);
406 void list_common_guides_help(void)
408 struct category_description catdesc
[] = {
409 { CAT_guide
, N_("The common Git guides are:") },
412 print_cmd_by_category(catdesc
, NULL
);
416 struct slot_expansion
{
418 const char *placeholder
;
419 void (*fn
)(struct string_list
*list
, const char *prefix
);
423 void list_config_help(int for_human
)
425 struct slot_expansion slot_expansions
[] = {
426 { "advice", "*", list_config_advices
},
427 { "color.branch", "<slot>", list_config_color_branch_slots
},
428 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
429 { "color.diff", "<slot>", list_config_color_diff_slots
},
430 { "color.grep", "<slot>", list_config_color_grep_slots
},
431 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
432 { "color.remote", "<slot>", list_config_color_sideband_slots
},
433 { "color.status", "<slot>", list_config_color_status_slots
},
434 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
435 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
439 struct slot_expansion
*e
;
440 struct string_list keys
= STRING_LIST_INIT_DUP
;
443 for (p
= config_name_list
; *p
; p
++) {
444 const char *var
= *p
;
445 struct strbuf sb
= STRBUF_INIT
;
447 for (e
= slot_expansions
; e
->prefix
; e
++) {
450 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
451 if (!strcasecmp(var
, sb
.buf
)) {
452 e
->fn(&keys
, e
->prefix
);
459 string_list_append(&keys
, var
);
462 for (e
= slot_expansions
; e
->prefix
; e
++)
464 BUG("slot_expansion %s.%s is not used",
465 e
->prefix
, e
->placeholder
);
467 string_list_sort(&keys
);
468 for (i
= 0; i
< keys
.nr
; i
++) {
469 const char *var
= keys
.items
[i
].string
;
470 const char *wildcard
, *tag
, *cut
;
477 wildcard
= strchr(var
, '*');
478 tag
= strchr(var
, '<');
480 if (!wildcard
&& !tag
) {
485 if (wildcard
&& !tag
)
487 else if (!wildcard
&& tag
)
490 cut
= wildcard
< tag
? wildcard
: tag
;
493 * We may produce duplicates, but that's up to
494 * git-completion.bash to handle
496 printf("%.*s\n", (int)(cut
- var
), var
);
498 string_list_clear(&keys
, 0);
501 static int get_alias(const char *var
, const char *value
, void *data
)
503 struct string_list
*list
= data
;
505 if (skip_prefix(var
, "alias.", &var
))
506 string_list_append(list
, var
)->util
= xstrdup(value
);
511 void list_all_cmds_help(void)
513 struct string_list others
= STRING_LIST_INIT_DUP
;
514 struct string_list alias_list
= STRING_LIST_INIT_DUP
;
515 struct cmdname_help
*aliases
;
518 printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
519 print_cmd_by_category(main_categories
, &longest
);
521 list_all_other_cmds(&others
);
523 printf("\n%s\n", _("External commands"));
524 for (i
= 0; i
< others
.nr
; i
++)
525 printf(" %s\n", others
.items
[i
].string
);
526 string_list_clear(&others
, 0);
528 git_config(get_alias
, &alias_list
);
529 string_list_sort(&alias_list
);
531 for (i
= 0; i
< alias_list
.nr
; i
++) {
532 size_t len
= strlen(alias_list
.items
[i
].string
);
538 printf("\n%s\n", _("Command aliases"));
539 ALLOC_ARRAY(aliases
, alias_list
.nr
+ 1);
540 for (i
= 0; i
< alias_list
.nr
; i
++) {
541 aliases
[i
].name
= alias_list
.items
[i
].string
;
542 aliases
[i
].help
= alias_list
.items
[i
].util
;
543 aliases
[i
].category
= 1;
545 aliases
[alias_list
.nr
].name
= NULL
;
546 print_command_list(aliases
, 1, longest
);
549 string_list_clear(&alias_list
, 1);
552 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
555 for (i
= 0; i
< c
->cnt
; i
++)
556 if (!strcmp(s
, c
->names
[i
]->name
))
561 static int autocorrect
;
562 static struct cmdnames aliases
;
564 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
568 if (!strcmp(var
, "help.autocorrect"))
569 autocorrect
= git_config_int(var
,value
);
570 /* Also use aliases for command lookup */
571 if (skip_prefix(var
, "alias.", &p
))
572 add_cmdname(&aliases
, p
, strlen(p
));
574 return git_default_config(var
, value
, cb
);
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
);
617 load_command_list("git-", &main_cmds
, &other_cmds
);
619 add_cmd_list(&main_cmds
, &aliases
);
620 add_cmd_list(&main_cmds
, &other_cmds
);
621 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
624 extract_cmds(&common_cmds
, common_mask
);
626 /* This abuses cmdname->len for levenshtein distance */
627 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
628 int cmp
= 0; /* avoid compiler stupidity */
629 const char *candidate
= main_cmds
.names
[i
]->name
;
632 * An exact match means we have the command, but
633 * for some reason exec'ing it gave us ENOENT; probably
634 * it's a bad interpreter in the #! line.
636 if (!strcmp(candidate
, cmd
))
637 die(_(bad_interpreter_advice
), cmd
, cmd
);
639 /* Does the candidate appear in common_cmds list? */
640 while (common_cmds
[n
].name
&&
641 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
643 if (common_cmds
[n
].name
&& !cmp
) {
644 /* Yes, this is one of the common commands */
645 n
++; /* use the entry from common_cmds[] */
646 if (starts_with(candidate
, cmd
)) {
647 /* Give prefix match a very good score */
648 main_cmds
.names
[i
]->len
= 0;
653 main_cmds
.names
[i
]->len
=
654 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
656 FREE_AND_NULL(common_cmds
);
658 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
661 die(_("Uh oh. Your system reports no Git commands at all."));
663 /* skip and count prefix matches */
664 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
665 ; /* still counting */
667 if (main_cmds
.cnt
<= n
) {
668 /* prefix matches with everything? that is too ambiguous */
669 best_similarity
= SIMILARITY_FLOOR
+ 1;
671 /* count all the most similar ones */
672 for (best_similarity
= main_cmds
.names
[n
++]->len
;
673 (n
< main_cmds
.cnt
&&
674 best_similarity
== main_cmds
.names
[n
]->len
);
676 ; /* still counting */
678 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
679 const char *assumed
= main_cmds
.names
[0]->name
;
680 main_cmds
.names
[0] = NULL
;
681 clean_cmdnames(&main_cmds
);
683 _("WARNING: You called a Git command named '%s', "
684 "which does not exist."),
688 _("Continuing under the assumption that "
693 _("Continuing in %0.1f seconds, "
694 "assuming that you meant '%s'."),
695 (float)autocorrect
/10.0, assumed
);
696 sleep_millisec(autocorrect
* 100);
701 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
703 if (SIMILAR_ENOUGH(best_similarity
)) {
705 Q_("\nThe most similar command is",
706 "\nThe most similar commands are",
709 for (i
= 0; i
< n
; i
++)
710 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
716 int cmd_version(int argc
, const char **argv
, const char *prefix
)
718 int build_options
= 0;
719 const char * const usage
[] = {
720 N_("git version [<options>]"),
723 struct option options
[] = {
724 OPT_BOOL(0, "build-options", &build_options
,
725 "also print build options"),
729 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
732 * The format of this string should be kept stable for compatibility
733 * with external projects that rely on the output of "git version".
735 * Always show the version, even if other options are given.
737 printf("git version %s\n", git_version_string
);
740 printf("cpu: %s\n", GIT_HOST_CPU
);
741 if (git_built_from_commit_string
[0])
742 printf("built from commit: %s\n",
743 git_built_from_commit_string
);
745 printf("no commit associated with this build\n");
746 printf("sizeof-long: %d\n", (int)sizeof(long));
747 printf("sizeof-size_t: %d\n", (int)sizeof(size_t));
748 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
753 struct similar_ref_cb
{
754 const char *base_ref
;
755 struct string_list
*similar_refs
;
758 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
759 int flags
, void *cb_data
)
761 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
762 char *branch
= strrchr(refname
, '/') + 1;
765 /* A remote branch of the same name is deemed similar */
766 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
767 !strcmp(branch
, cb
->base_ref
))
768 string_list_append(cb
->similar_refs
, remote
);
772 static struct string_list
guess_refs(const char *ref
)
774 struct similar_ref_cb ref_cb
;
775 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
777 ref_cb
.base_ref
= ref
;
778 ref_cb
.similar_refs
= &similar_refs
;
779 for_each_ref(append_similar_ref
, &ref_cb
);
783 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
786 struct string_list suggested_refs
= guess_refs(ref
);
788 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
790 if (suggested_refs
.nr
> 0) {
792 Q_("\nDid you mean this?",
793 "\nDid you mean one of these?",
795 for (i
= 0; i
< suggested_refs
.nr
; i
++)
796 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
799 string_list_clear(&suggested_refs
, 0);