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)
46 if (skip_prefix(name, "git-", &new_name))
52 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
55 struct cmdname_help *cmds;
57 if (ARRAY_SIZE(command_list) == 0)
58 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
60 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
62 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
63 const struct cmdname_help *cmd = command_list + i;
65 if (!(cmd->category & mask))
69 cmds[nr].name = drop_prefix(cmd->name);
77 static void print_command_list(const struct cmdname_help *cmds,
78 uint32_t mask, int longest)
82 for (i = 0; cmds[i].name; i++) {
83 if (cmds[i].category & mask) {
84 printf(" %s ", cmds[i].name);
85 mput_char(' ', longest - strlen(cmds[i].name));
86 puts(_(cmds[i].help));
91 static int cmd_name_cmp(const void *elem1, const void *elem2)
93 const struct cmdname_help *e1 = elem1;
94 const struct cmdname_help *e2 = elem2;
96 return strcmp(e1->name, e2->name);
99 static void print_cmd_by_category(const struct category_description *catdesc)
101 struct cmdname_help *cmds;
106 for (i = 0; catdesc[i].desc; i++)
107 mask |= catdesc[i].category;
109 extract_cmds(&cmds, mask);
111 for (i = 0; cmds[i].name; i++, nr++) {
112 if (longest < strlen(cmds[i].name))
113 longest = strlen(cmds[i].name);
115 QSORT(cmds, nr, cmd_name_cmp);
117 for (i = 0; catdesc[i].desc; i++) {
118 uint32_t mask = catdesc[i].category;
119 const char *desc = catdesc[i].desc;
121 printf("\n%s\n", _(desc));
122 print_command_list(cmds, mask, longest);
127 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
130 FLEX_ALLOC_MEM(ent, name, name, len);
133 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
134 cmds->names[cmds->cnt++] = ent;
137 static void clean_cmdnames(struct cmdnames *cmds)
140 for (i = 0; i < cmds->cnt; ++i)
141 free(cmds->names[i]);
147 static int cmdname_compare(const void *a_, const void *b_)
149 struct cmdname *a = *(struct cmdname **)a_;
150 struct cmdname *b = *(struct cmdname **)b_;
151 return strcmp(a->name, b->name);
154 static void uniq(struct cmdnames *cmds)
161 for (i = j = 1; i < cmds->cnt; i++) {
162 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
163 free(cmds->names[i]);
165 cmds->names[j++] = cmds->names[i];
171 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
177 while (ci < cmds->cnt && ei < excludes->cnt) {
178 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
180 cmds->names[cj++] = cmds->names[ci++];
183 free(cmds->names[ci++]);
188 while (ci < cmds->cnt)
189 cmds->names[cj++] = cmds->names[ci++];
194 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
196 struct string_list list = STRING_LIST_INIT_NODUP;
197 struct column_options copts;
200 for (i = 0; i < cmds->cnt; i++)
201 string_list_append(&list, cmds->names[i]->name);
203 * always enable column display, we only consult column.*
204 * about layout strategy and stuff
206 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
207 memset(&copts, 0, sizeof(copts));
210 print_columns(&list, colopts, &copts);
211 string_list_clear(&list, 0);
214 static void list_commands_in_dir(struct cmdnames *cmds,
218 DIR *dir = opendir(path);
220 struct strbuf buf = STRBUF_INIT;
228 strbuf_addf(&buf, "%s/", path);
231 while ((de = readdir(dir)) != NULL) {
235 if (!skip_prefix(de->d_name, prefix, &ent))
238 strbuf_setlen(&buf, len);
239 strbuf_addstr(&buf, de->d_name);
240 if (!is_executable(buf.buf))
243 entlen = strlen(ent);
244 strip_suffix(ent, ".exe", &entlen);
246 add_cmdname(cmds, ent, entlen);
249 strbuf_release(&buf);
252 void load_command_list(const char *prefix,
253 struct cmdnames *main_cmds,
254 struct cmdnames *other_cmds)
256 const char *env_path = getenv("PATH");
257 const char *exec_path = git_exec_path();
260 list_commands_in_dir(main_cmds, exec_path, prefix);
261 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
266 char *paths, *path, *colon;
267 path = paths = xstrdup(env_path);
269 if ((colon = strchr(path, PATH_SEP)))
271 if (!exec_path || strcmp(path, exec_path))
272 list_commands_in_dir(other_cmds, path, prefix);
280 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
283 exclude_cmds(other_cmds, main_cmds);
286 void list_commands(unsigned int colopts,
287 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
289 if (main_cmds->cnt) {
290 const char *exec_path = git_exec_path();
291 printf_ln(_("available git commands in '%s'"), exec_path);
293 pretty_print_cmdnames(main_cmds, colopts);
297 if (other_cmds->cnt) {
298 printf_ln(_("git commands available from elsewhere on your $PATH"));
300 pretty_print_cmdnames(other_cmds, colopts);
305 void list_common_cmds_help(void)
307 puts(_("These are common Git commands used in various situations:"));
308 print_cmd_by_category(common_categories);
311 void list_all_main_cmds(struct string_list *list)
313 struct cmdnames main_cmds, other_cmds;
316 memset(&main_cmds, 0, sizeof(main_cmds));
317 memset(&other_cmds, 0, sizeof(other_cmds));
318 load_command_list("git-", &main_cmds, &other_cmds);
320 for (i = 0; i < main_cmds.cnt; i++)
321 string_list_append(list, main_cmds.names[i]->name);
323 clean_cmdnames(&main_cmds);
324 clean_cmdnames(&other_cmds);
327 void list_all_other_cmds(struct string_list *list)
329 struct cmdnames main_cmds, other_cmds;
332 memset(&main_cmds, 0, sizeof(main_cmds));
333 memset(&other_cmds, 0, sizeof(other_cmds));
334 load_command_list("git-", &main_cmds, &other_cmds);
336 for (i = 0; i < other_cmds.cnt; i++)
337 string_list_append(list, other_cmds.names[i]->name);
339 clean_cmdnames(&main_cmds);
340 clean_cmdnames(&other_cmds);
343 void list_cmds_by_category(struct string_list *list,
346 int i, n = ARRAY_SIZE(command_list);
349 for (i = 0; category_names[i]; i++) {
350 if (!strcmp(cat, category_names[i])) {
356 die(_("unsupported command listing type '%s'"), cat);
358 for (i = 0; i < n; i++) {
359 struct cmdname_help *cmd = command_list + i;
361 if (cmd->category & cat_id)
362 string_list_append(list, drop_prefix(cmd->name));
366 void list_all_cmds_help(void)
368 print_cmd_by_category(main_categories);
371 int is_in_cmdlist(struct cmdnames *c, const char *s)
374 for (i = 0; i < c->cnt; i++)
375 if (!strcmp(s, c->names[i]->name))
380 static int autocorrect;
381 static struct cmdnames aliases;
383 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
387 if (!strcmp(var, "help.autocorrect"))
388 autocorrect = git_config_int(var,value);
389 /* Also use aliases for command lookup */
390 if (skip_prefix(var, "alias.", &p))
391 add_cmdname(&aliases, p, strlen(p));
393 return git_default_config(var, value, cb);
396 static int levenshtein_compare(const void *p1, const void *p2)
398 const struct cmdname *const *c1 = p1, *const *c2 = p2;
399 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
402 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
405 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
408 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
410 for (i = 0; i < old->cnt; i++)
411 cmds->names[cmds->cnt++] = old->names[i];
412 FREE_AND_NULL(old->names);
416 /* An empirically derived magic number */
417 #define SIMILARITY_FLOOR 7
418 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
420 static const char bad_interpreter_advice[] =
421 N_("'%s' appears to be a git command, but we were not\n"
422 "able to execute it. Maybe git-%s is broken?");
424 const char *help_unknown_cmd(const char *cmd)
426 int i, n, best_similarity = 0;
427 struct cmdnames main_cmds, other_cmds;
428 struct cmdname_help *common_cmds;
430 memset(&main_cmds, 0, sizeof(main_cmds));
431 memset(&other_cmds, 0, sizeof(other_cmds));
432 memset(&aliases, 0, sizeof(aliases));
434 read_early_config(git_unknown_cmd_config, NULL);
436 load_command_list("git-", &main_cmds, &other_cmds);
438 add_cmd_list(&main_cmds, &aliases);
439 add_cmd_list(&main_cmds, &other_cmds);
440 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
443 extract_cmds(&common_cmds, common_mask);
445 /* This abuses cmdname->len for levenshtein distance */
446 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
447 int cmp = 0; /* avoid compiler stupidity */
448 const char *candidate = main_cmds.names[i]->name;
451 * An exact match means we have the command, but
452 * for some reason exec'ing it gave us ENOENT; probably
453 * it's a bad interpreter in the #! line.
455 if (!strcmp(candidate, cmd))
456 die(_(bad_interpreter_advice), cmd, cmd);
458 /* Does the candidate appear in common_cmds list? */
459 while (common_cmds[n].name &&
460 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
462 if (common_cmds[n].name && !cmp) {
463 /* Yes, this is one of the common commands */
464 n++; /* use the entry from common_cmds[] */
465 if (starts_with(candidate, cmd)) {
466 /* Give prefix match a very good score */
467 main_cmds.names[i]->len = 0;
472 main_cmds.names[i]->len =
473 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
475 FREE_AND_NULL(common_cmds);
477 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
480 die(_("Uh oh. Your system reports no Git commands at all."));
482 /* skip and count prefix matches */
483 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
484 ; /* still counting */
486 if (main_cmds.cnt <= n) {
487 /* prefix matches with everything? that is too ambiguous */
488 best_similarity = SIMILARITY_FLOOR + 1;
490 /* count all the most similar ones */
491 for (best_similarity = main_cmds.names[n++]->len;
492 (n < main_cmds.cnt &&
493 best_similarity == main_cmds.names[n]->len);
495 ; /* still counting */
497 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
498 const char *assumed = main_cmds.names[0]->name;
499 main_cmds.names[0] = NULL;
500 clean_cmdnames(&main_cmds);
502 _("WARNING: You called a Git command named '%s', "
503 "which does not exist."),
507 _("Continuing under the assumption that "
512 _("Continuing in %0.1f seconds, "
513 "assuming that you meant '%s'."),
514 (float)autocorrect/10.0, assumed);
515 sleep_millisec(autocorrect * 100);
520 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
522 if (SIMILAR_ENOUGH(best_similarity)) {
524 Q_("\nThe most similar command is",
525 "\nThe most similar commands are",
528 for (i = 0; i < n; i++)
529 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
535 int cmd_version(int argc, const char **argv, const char *prefix)
537 int build_options = 0;
538 const char * const usage[] = {
539 N_("git version [<options>]"),
542 struct option options[] = {
543 OPT_BOOL(0, "build-options", &build_options,
544 "also print build options"),
548 argc = parse_options(argc, argv, prefix, options, usage, 0);
551 * The format of this string should be kept stable for compatibility
552 * with external projects that rely on the output of "git version".
554 * Always show the version, even if other options are given.
556 printf("git version %s\n", git_version_string);
559 printf("cpu: %s\n", GIT_HOST_CPU);
560 if (git_built_from_commit_string[0])
561 printf("built from commit: %s\n",
562 git_built_from_commit_string);
564 printf("no commit associated with this build\n");
565 printf("sizeof-long: %d\n", (int)sizeof(long));
566 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
571 struct similar_ref_cb {
572 const char *base_ref;
573 struct string_list *similar_refs;
576 static int append_similar_ref(const char *refname, const struct object_id *oid,
577 int flags, void *cb_data)
579 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
580 char *branch = strrchr(refname, '/') + 1;
583 /* A remote branch of the same name is deemed similar */
584 if (skip_prefix(refname, "refs/remotes/", &remote) &&
585 !strcmp(branch, cb->base_ref))
586 string_list_append(cb->similar_refs, remote);
590 static struct string_list guess_refs(const char *ref)
592 struct similar_ref_cb ref_cb;
593 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
595 ref_cb.base_ref = ref;
596 ref_cb.similar_refs = &similar_refs;
597 for_each_ref(append_similar_ref, &ref_cb);
601 void help_unknown_ref(const char *ref, const char *cmd, const char *error)
604 struct string_list suggested_refs = guess_refs(ref);
606 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
608 if (suggested_refs.nr > 0) {
610 Q_("\nDid you mean this?",
611 "\nDid you mean one of these?",
613 for (i = 0; i < suggested_refs.nr; i++)
614 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
617 string_list_clear(&suggested_refs, 0);