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 printf(" %s ", cmds
[i
].name
);
87 mput_char(' ', longest
- strlen(cmds
[i
].name
));
88 puts(_(cmds
[i
].help
));
93 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
95 const struct cmdname_help
*e1
= elem1
;
96 const struct cmdname_help
*e2
= elem2
;
98 return strcmp(e1
->name
, e2
->name
);
101 static void print_cmd_by_category(const struct category_description
*catdesc
)
103 struct cmdname_help
*cmds
;
108 for (i
= 0; catdesc
[i
].desc
; i
++)
109 mask
|= catdesc
[i
].category
;
111 extract_cmds(&cmds
, mask
);
113 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
114 if (longest
< strlen(cmds
[i
].name
))
115 longest
= strlen(cmds
[i
].name
);
117 QSORT(cmds
, nr
, cmd_name_cmp
);
119 for (i
= 0; catdesc
[i
].desc
; i
++) {
120 uint32_t mask
= catdesc
[i
].category
;
121 const char *desc
= catdesc
[i
].desc
;
123 printf("\n%s\n", _(desc
));
124 print_command_list(cmds
, mask
, longest
);
129 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
132 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
135 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
136 cmds
->names
[cmds
->cnt
++] = ent
;
139 static void clean_cmdnames(struct cmdnames
*cmds
)
142 for (i
= 0; i
< cmds
->cnt
; ++i
)
143 free(cmds
->names
[i
]);
149 static int cmdname_compare(const void *a_
, const void *b_
)
151 struct cmdname
*a
= *(struct cmdname
**)a_
;
152 struct cmdname
*b
= *(struct cmdname
**)b_
;
153 return strcmp(a
->name
, b
->name
);
156 static void uniq(struct cmdnames
*cmds
)
163 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
164 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
165 free(cmds
->names
[i
]);
167 cmds
->names
[j
++] = cmds
->names
[i
];
173 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
179 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
180 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
182 cmds
->names
[cj
++] = cmds
->names
[ci
++];
185 free(cmds
->names
[ci
++]);
190 while (ci
< cmds
->cnt
)
191 cmds
->names
[cj
++] = cmds
->names
[ci
++];
196 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
198 struct string_list list
= STRING_LIST_INIT_NODUP
;
199 struct column_options copts
;
202 for (i
= 0; i
< cmds
->cnt
; i
++)
203 string_list_append(&list
, cmds
->names
[i
]->name
);
205 * always enable column display, we only consult column.*
206 * about layout strategy and stuff
208 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
209 memset(&copts
, 0, sizeof(copts
));
212 print_columns(&list
, colopts
, &copts
);
213 string_list_clear(&list
, 0);
216 static void list_commands_in_dir(struct cmdnames
*cmds
,
220 DIR *dir
= opendir(path
);
222 struct strbuf buf
= STRBUF_INIT
;
230 strbuf_addf(&buf
, "%s/", path
);
233 while ((de
= readdir(dir
)) != NULL
) {
237 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
240 strbuf_setlen(&buf
, len
);
241 strbuf_addstr(&buf
, de
->d_name
);
242 if (!is_executable(buf
.buf
))
245 entlen
= strlen(ent
);
246 strip_suffix(ent
, ".exe", &entlen
);
248 add_cmdname(cmds
, ent
, entlen
);
251 strbuf_release(&buf
);
254 void load_command_list(const char *prefix
,
255 struct cmdnames
*main_cmds
,
256 struct cmdnames
*other_cmds
)
258 const char *env_path
= getenv("PATH");
259 const char *exec_path
= git_exec_path();
262 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
263 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
268 char *paths
, *path
, *colon
;
269 path
= paths
= xstrdup(env_path
);
271 if ((colon
= strchr(path
, PATH_SEP
)))
273 if (!exec_path
|| strcmp(path
, exec_path
))
274 list_commands_in_dir(other_cmds
, path
, prefix
);
282 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
285 exclude_cmds(other_cmds
, main_cmds
);
288 void list_commands(unsigned int colopts
,
289 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
291 if (main_cmds
->cnt
) {
292 const char *exec_path
= git_exec_path();
293 printf_ln(_("available git commands in '%s'"), exec_path
);
295 pretty_print_cmdnames(main_cmds
, colopts
);
299 if (other_cmds
->cnt
) {
300 printf_ln(_("git commands available from elsewhere on your $PATH"));
302 pretty_print_cmdnames(other_cmds
, colopts
);
307 void list_common_cmds_help(void)
309 puts(_("These are common Git commands used in various situations:"));
310 print_cmd_by_category(common_categories
);
313 void list_all_main_cmds(struct string_list
*list
)
315 struct cmdnames main_cmds
, other_cmds
;
318 memset(&main_cmds
, 0, sizeof(main_cmds
));
319 memset(&other_cmds
, 0, sizeof(other_cmds
));
320 load_command_list("git-", &main_cmds
, &other_cmds
);
322 for (i
= 0; i
< main_cmds
.cnt
; i
++)
323 string_list_append(list
, main_cmds
.names
[i
]->name
);
325 clean_cmdnames(&main_cmds
);
326 clean_cmdnames(&other_cmds
);
329 void list_all_other_cmds(struct string_list
*list
)
331 struct cmdnames main_cmds
, other_cmds
;
334 memset(&main_cmds
, 0, sizeof(main_cmds
));
335 memset(&other_cmds
, 0, sizeof(other_cmds
));
336 load_command_list("git-", &main_cmds
, &other_cmds
);
338 for (i
= 0; i
< other_cmds
.cnt
; i
++)
339 string_list_append(list
, other_cmds
.names
[i
]->name
);
341 clean_cmdnames(&main_cmds
);
342 clean_cmdnames(&other_cmds
);
345 void list_cmds_by_category(struct string_list
*list
,
348 int i
, n
= ARRAY_SIZE(command_list
);
351 for (i
= 0; category_names
[i
]; i
++) {
352 if (!strcmp(cat
, category_names
[i
])) {
358 die(_("unsupported command listing type '%s'"), cat
);
360 for (i
= 0; i
< n
; i
++) {
361 struct cmdname_help
*cmd
= command_list
+ i
;
363 if (!(cmd
->category
& cat_id
))
365 string_list_append(list
, drop_prefix(cmd
->name
, cmd
->category
));
369 void list_cmds_by_config(struct string_list
*list
)
371 const char *cmd_list
;
374 * There's no actual repository setup at this point (and even
375 * if there is, we don't really care; only global config
376 * matters). If we accidentally set up a repository, it's ok
377 * too since the caller (git --list-cmds=) should exit shortly
380 if (git_config_get_string_const("completion.commands", &cmd_list
))
383 string_list_sort(list
);
384 string_list_remove_duplicates(list
, 0);
387 struct strbuf sb
= STRBUF_INIT
;
388 const char *p
= strchrnul(cmd_list
, ' ');
390 strbuf_add(&sb
, cmd_list
, p
- cmd_list
);
391 if (*cmd_list
== '-')
392 string_list_remove(list
, cmd_list
+ 1, 0);
394 string_list_insert(list
, sb
.buf
);
402 void list_common_guides_help(void)
404 struct category_description catdesc
[] = {
405 { CAT_guide
, N_("The common Git guides are:") },
408 print_cmd_by_category(catdesc
);
412 struct slot_expansion
{
414 const char *placeholder
;
415 void (*fn
)(struct string_list
*list
, const char *prefix
);
419 void list_config_help(int for_human
)
421 struct slot_expansion slot_expansions
[] = {
422 { "advice", "*", list_config_advices
},
423 { "color.branch", "<slot>", list_config_color_branch_slots
},
424 { "color.decorate", "<slot>", list_config_color_decorate_slots
},
425 { "color.diff", "<slot>", list_config_color_diff_slots
},
426 { "color.grep", "<slot>", list_config_color_grep_slots
},
427 { "color.interactive", "<slot>", list_config_color_interactive_slots
},
428 { "color.status", "<slot>", list_config_color_status_slots
},
429 { "fsck", "<msg-id>", list_config_fsck_msg_ids
},
430 { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids
},
434 struct slot_expansion
*e
;
435 struct string_list keys
= STRING_LIST_INIT_DUP
;
438 for (p
= config_name_list
; *p
; p
++) {
439 const char *var
= *p
;
440 struct strbuf sb
= STRBUF_INIT
;
442 for (e
= slot_expansions
; e
->prefix
; e
++) {
445 strbuf_addf(&sb
, "%s.%s", e
->prefix
, e
->placeholder
);
446 if (!strcasecmp(var
, sb
.buf
)) {
447 e
->fn(&keys
, e
->prefix
);
454 string_list_append(&keys
, var
);
457 for (e
= slot_expansions
; e
->prefix
; e
++)
459 BUG("slot_expansion %s.%s is not used",
460 e
->prefix
, e
->placeholder
);
462 string_list_sort(&keys
);
463 for (i
= 0; i
< keys
.nr
; i
++) {
464 const char *var
= keys
.items
[i
].string
;
465 const char *wildcard
, *tag
, *cut
;
472 wildcard
= strchr(var
, '*');
473 tag
= strchr(var
, '<');
475 if (!wildcard
&& !tag
) {
480 if (wildcard
&& !tag
)
482 else if (!wildcard
&& tag
)
485 cut
= wildcard
< tag
? wildcard
: tag
;
488 * We may produce duplicates, but that's up to
489 * git-completion.bash to handle
491 printf("%.*s\n", (int)(cut
- var
), var
);
493 string_list_clear(&keys
, 0);
496 void list_all_cmds_help(void)
498 print_cmd_by_category(main_categories
);
501 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
504 for (i
= 0; i
< c
->cnt
; i
++)
505 if (!strcmp(s
, c
->names
[i
]->name
))
510 static int autocorrect
;
511 static struct cmdnames aliases
;
513 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
517 if (!strcmp(var
, "help.autocorrect"))
518 autocorrect
= git_config_int(var
,value
);
519 /* Also use aliases for command lookup */
520 if (skip_prefix(var
, "alias.", &p
))
521 add_cmdname(&aliases
, p
, strlen(p
));
523 return git_default_config(var
, value
, cb
);
526 static int levenshtein_compare(const void *p1
, const void *p2
)
528 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
529 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
532 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
535 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
538 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
540 for (i
= 0; i
< old
->cnt
; i
++)
541 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
542 FREE_AND_NULL(old
->names
);
546 /* An empirically derived magic number */
547 #define SIMILARITY_FLOOR 7
548 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
550 static const char bad_interpreter_advice
[] =
551 N_("'%s' appears to be a git command, but we were not\n"
552 "able to execute it. Maybe git-%s is broken?");
554 const char *help_unknown_cmd(const char *cmd
)
556 int i
, n
, best_similarity
= 0;
557 struct cmdnames main_cmds
, other_cmds
;
558 struct cmdname_help
*common_cmds
;
560 memset(&main_cmds
, 0, sizeof(main_cmds
));
561 memset(&other_cmds
, 0, sizeof(other_cmds
));
562 memset(&aliases
, 0, sizeof(aliases
));
564 read_early_config(git_unknown_cmd_config
, NULL
);
566 load_command_list("git-", &main_cmds
, &other_cmds
);
568 add_cmd_list(&main_cmds
, &aliases
);
569 add_cmd_list(&main_cmds
, &other_cmds
);
570 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
573 extract_cmds(&common_cmds
, common_mask
);
575 /* This abuses cmdname->len for levenshtein distance */
576 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
577 int cmp
= 0; /* avoid compiler stupidity */
578 const char *candidate
= main_cmds
.names
[i
]->name
;
581 * An exact match means we have the command, but
582 * for some reason exec'ing it gave us ENOENT; probably
583 * it's a bad interpreter in the #! line.
585 if (!strcmp(candidate
, cmd
))
586 die(_(bad_interpreter_advice
), cmd
, cmd
);
588 /* Does the candidate appear in common_cmds list? */
589 while (common_cmds
[n
].name
&&
590 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
592 if (common_cmds
[n
].name
&& !cmp
) {
593 /* Yes, this is one of the common commands */
594 n
++; /* use the entry from common_cmds[] */
595 if (starts_with(candidate
, cmd
)) {
596 /* Give prefix match a very good score */
597 main_cmds
.names
[i
]->len
= 0;
602 main_cmds
.names
[i
]->len
=
603 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
605 FREE_AND_NULL(common_cmds
);
607 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
610 die(_("Uh oh. Your system reports no Git commands at all."));
612 /* skip and count prefix matches */
613 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
614 ; /* still counting */
616 if (main_cmds
.cnt
<= n
) {
617 /* prefix matches with everything? that is too ambiguous */
618 best_similarity
= SIMILARITY_FLOOR
+ 1;
620 /* count all the most similar ones */
621 for (best_similarity
= main_cmds
.names
[n
++]->len
;
622 (n
< main_cmds
.cnt
&&
623 best_similarity
== main_cmds
.names
[n
]->len
);
625 ; /* still counting */
627 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
628 const char *assumed
= main_cmds
.names
[0]->name
;
629 main_cmds
.names
[0] = NULL
;
630 clean_cmdnames(&main_cmds
);
632 _("WARNING: You called a Git command named '%s', "
633 "which does not exist."),
637 _("Continuing under the assumption that "
642 _("Continuing in %0.1f seconds, "
643 "assuming that you meant '%s'."),
644 (float)autocorrect
/10.0, assumed
);
645 sleep_millisec(autocorrect
* 100);
650 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
652 if (SIMILAR_ENOUGH(best_similarity
)) {
654 Q_("\nThe most similar command is",
655 "\nThe most similar commands are",
658 for (i
= 0; i
< n
; i
++)
659 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
665 int cmd_version(int argc
, const char **argv
, const char *prefix
)
667 int build_options
= 0;
668 const char * const usage
[] = {
669 N_("git version [<options>]"),
672 struct option options
[] = {
673 OPT_BOOL(0, "build-options", &build_options
,
674 "also print build options"),
678 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
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 printf("git version %s\n", git_version_string
);
689 printf("cpu: %s\n", GIT_HOST_CPU
);
690 if (git_built_from_commit_string
[0])
691 printf("built from commit: %s\n",
692 git_built_from_commit_string
);
694 printf("no commit associated with this build\n");
695 printf("sizeof-long: %d\n", (int)sizeof(long));
696 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
701 struct similar_ref_cb
{
702 const char *base_ref
;
703 struct string_list
*similar_refs
;
706 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
707 int flags
, void *cb_data
)
709 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
710 char *branch
= strrchr(refname
, '/') + 1;
713 /* A remote branch of the same name is deemed similar */
714 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
715 !strcmp(branch
, cb
->base_ref
))
716 string_list_append(cb
->similar_refs
, remote
);
720 static struct string_list
guess_refs(const char *ref
)
722 struct similar_ref_cb ref_cb
;
723 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
725 ref_cb
.base_ref
= ref
;
726 ref_cb
.similar_refs
= &similar_refs
;
727 for_each_ref(append_similar_ref
, &ref_cb
);
731 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
734 struct string_list suggested_refs
= guess_refs(ref
);
736 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
738 if (suggested_refs
.nr
> 0) {
740 Q_("\nDid you mean this?",
741 "\nDid you mean one of these?",
743 for (i
= 0; i
< suggested_refs
.nr
; i
++)
744 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
747 string_list_clear(&suggested_refs
, 0);