builtin/index-pack.c: write reverse indexes
[git.git] / help.c
blob3c3bdec21356d9e346ef2185d7ddaffa8229d6d0
1 #include "cache.h"
2 #include "config.h"
3 #include "builtin.h"
4 #include "exec-cmd.h"
5 #include "run-command.h"
6 #include "levenshtein.h"
7 #include "help.h"
8 #include "command-list.h"
9 #include "string-list.h"
10 #include "column.h"
11 #include "version.h"
12 #include "refs.h"
13 #include "parse-options.h"
15 struct category_description {
16 uint32_t category;
17 const char *desc;
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)") },
28 { 0, NULL }
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 / Syncing Repositories") },
38 { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
39 { 0, NULL }
42 static const char *drop_prefix(const char *name, uint32_t category)
44 const char *new_name;
46 if (skip_prefix(name, "git-", &new_name))
47 return new_name;
48 if (category == CAT_guide && skip_prefix(name, "git", &new_name))
49 return new_name;
50 return name;
54 static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
56 int i, nr = 0;
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))
68 continue;
70 cmds[nr] = *cmd;
71 cmds[nr].name = drop_prefix(cmd->name, cmd->category);
73 nr++;
75 cmds[nr].name = NULL;
76 *p_cmds = cmds;
79 static void print_command_list(const struct cmdname_help *cmds,
80 uint32_t mask, int longest)
82 int i;
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 if (longest > len)
89 mput_char(' ', longest - len);
90 puts(_(cmds[i].help));
95 static int cmd_name_cmp(const void *elem1, const void *elem2)
97 const struct cmdname_help *e1 = elem1;
98 const struct cmdname_help *e2 = elem2;
100 return strcmp(e1->name, e2->name);
103 static void print_cmd_by_category(const struct category_description *catdesc,
104 int *longest_p)
106 struct cmdname_help *cmds;
107 int longest = 0;
108 int i, nr = 0;
109 uint32_t mask = 0;
111 for (i = 0; catdesc[i].desc; i++)
112 mask |= catdesc[i].category;
114 extract_cmds(&cmds, mask);
116 for (i = 0; cmds[i].name; i++, nr++) {
117 if (longest < strlen(cmds[i].name))
118 longest = strlen(cmds[i].name);
120 QSORT(cmds, nr, cmd_name_cmp);
122 for (i = 0; catdesc[i].desc; i++) {
123 uint32_t mask = catdesc[i].category;
124 const char *desc = catdesc[i].desc;
126 printf("\n%s\n", _(desc));
127 print_command_list(cmds, mask, longest);
129 free(cmds);
130 if (longest_p)
131 *longest_p = longest;
134 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
136 struct cmdname *ent;
137 FLEX_ALLOC_MEM(ent, name, name, len);
138 ent->len = len;
140 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
141 cmds->names[cmds->cnt++] = ent;
144 static void clean_cmdnames(struct cmdnames *cmds)
146 int i;
147 for (i = 0; i < cmds->cnt; ++i)
148 free(cmds->names[i]);
149 free(cmds->names);
150 cmds->cnt = 0;
151 cmds->alloc = 0;
154 static int cmdname_compare(const void *a_, const void *b_)
156 struct cmdname *a = *(struct cmdname **)a_;
157 struct cmdname *b = *(struct cmdname **)b_;
158 return strcmp(a->name, b->name);
161 static void uniq(struct cmdnames *cmds)
163 int i, j;
165 if (!cmds->cnt)
166 return;
168 for (i = j = 1; i < cmds->cnt; i++) {
169 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
170 free(cmds->names[i]);
171 else
172 cmds->names[j++] = cmds->names[i];
175 cmds->cnt = j;
178 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
180 int ci, cj, ei;
181 int cmp;
183 ci = cj = ei = 0;
184 while (ci < cmds->cnt && ei < excludes->cnt) {
185 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
186 if (cmp < 0)
187 cmds->names[cj++] = cmds->names[ci++];
188 else if (cmp == 0) {
189 ei++;
190 free(cmds->names[ci++]);
191 } else if (cmp > 0)
192 ei++;
195 while (ci < cmds->cnt)
196 cmds->names[cj++] = cmds->names[ci++];
198 cmds->cnt = cj;
201 static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
203 struct string_list list = STRING_LIST_INIT_NODUP;
204 struct column_options copts;
205 int i;
207 for (i = 0; i < cmds->cnt; i++)
208 string_list_append(&list, cmds->names[i]->name);
210 * always enable column display, we only consult column.*
211 * about layout strategy and stuff
213 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
214 memset(&copts, 0, sizeof(copts));
215 copts.indent = " ";
216 copts.padding = 2;
217 print_columns(&list, colopts, &copts);
218 string_list_clear(&list, 0);
221 static void list_commands_in_dir(struct cmdnames *cmds,
222 const char *path,
223 const char *prefix)
225 DIR *dir = opendir(path);
226 struct dirent *de;
227 struct strbuf buf = STRBUF_INIT;
228 int len;
230 if (!dir)
231 return;
232 if (!prefix)
233 prefix = "git-";
235 strbuf_addf(&buf, "%s/", path);
236 len = buf.len;
238 while ((de = readdir(dir)) != NULL) {
239 const char *ent;
240 size_t entlen;
242 if (!skip_prefix(de->d_name, prefix, &ent))
243 continue;
245 strbuf_setlen(&buf, len);
246 strbuf_addstr(&buf, de->d_name);
247 if (!is_executable(buf.buf))
248 continue;
250 entlen = strlen(ent);
251 strip_suffix(ent, ".exe", &entlen);
253 add_cmdname(cmds, ent, entlen);
255 closedir(dir);
256 strbuf_release(&buf);
259 void load_command_list(const char *prefix,
260 struct cmdnames *main_cmds,
261 struct cmdnames *other_cmds)
263 const char *env_path = getenv("PATH");
264 const char *exec_path = git_exec_path();
266 load_builtin_commands(prefix, main_cmds);
268 if (exec_path) {
269 list_commands_in_dir(main_cmds, exec_path, prefix);
270 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
271 uniq(main_cmds);
274 if (env_path) {
275 char *paths, *path, *colon;
276 path = paths = xstrdup(env_path);
277 while (1) {
278 if ((colon = strchr(path, PATH_SEP)))
279 *colon = 0;
280 if (!exec_path || strcmp(path, exec_path))
281 list_commands_in_dir(other_cmds, path, prefix);
283 if (!colon)
284 break;
285 path = colon + 1;
287 free(paths);
289 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
290 uniq(other_cmds);
292 exclude_cmds(other_cmds, main_cmds);
295 void list_commands(unsigned int colopts,
296 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
298 if (main_cmds->cnt) {
299 const char *exec_path = git_exec_path();
300 printf_ln(_("available git commands in '%s'"), exec_path);
301 putchar('\n');
302 pretty_print_cmdnames(main_cmds, colopts);
303 putchar('\n');
306 if (other_cmds->cnt) {
307 printf_ln(_("git commands available from elsewhere on your $PATH"));
308 putchar('\n');
309 pretty_print_cmdnames(other_cmds, colopts);
310 putchar('\n');
314 void list_common_cmds_help(void)
316 puts(_("These are common Git commands used in various situations:"));
317 print_cmd_by_category(common_categories, NULL);
320 void list_all_main_cmds(struct string_list *list)
322 struct cmdnames main_cmds, other_cmds;
323 int i;
325 memset(&main_cmds, 0, sizeof(main_cmds));
326 memset(&other_cmds, 0, sizeof(other_cmds));
327 load_command_list("git-", &main_cmds, &other_cmds);
329 for (i = 0; i < main_cmds.cnt; i++)
330 string_list_append(list, main_cmds.names[i]->name);
332 clean_cmdnames(&main_cmds);
333 clean_cmdnames(&other_cmds);
336 void list_all_other_cmds(struct string_list *list)
338 struct cmdnames main_cmds, other_cmds;
339 int i;
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 < other_cmds.cnt; i++)
346 string_list_append(list, other_cmds.names[i]->name);
348 clean_cmdnames(&main_cmds);
349 clean_cmdnames(&other_cmds);
352 void list_cmds_by_category(struct string_list *list,
353 const char *cat)
355 int i, n = ARRAY_SIZE(command_list);
356 uint32_t cat_id = 0;
358 for (i = 0; category_names[i]; i++) {
359 if (!strcmp(cat, category_names[i])) {
360 cat_id = 1UL << i;
361 break;
364 if (!cat_id)
365 die(_("unsupported command listing type '%s'"), cat);
367 for (i = 0; i < n; i++) {
368 struct cmdname_help *cmd = command_list + i;
370 if (!(cmd->category & cat_id))
371 continue;
372 string_list_append(list, drop_prefix(cmd->name, cmd->category));
376 void list_cmds_by_config(struct string_list *list)
378 const char *cmd_list;
380 if (git_config_get_string_tmp("completion.commands", &cmd_list))
381 return;
383 string_list_sort(list);
384 string_list_remove_duplicates(list, 0);
386 while (*cmd_list) {
387 struct strbuf sb = STRBUF_INIT;
388 const char *p = strchrnul(cmd_list, ' ');
390 strbuf_add(&sb, cmd_list, p - cmd_list);
391 if (sb.buf[0] == '-')
392 string_list_remove(list, sb.buf + 1, 0);
393 else
394 string_list_insert(list, sb.buf);
395 strbuf_release(&sb);
396 while (*p == ' ')
397 p++;
398 cmd_list = p;
402 void list_guides_help(void)
404 struct category_description catdesc[] = {
405 { CAT_guide, N_("The Git concept guides are:") },
406 { 0, NULL }
408 print_cmd_by_category(catdesc, NULL);
409 putchar('\n');
412 static int get_alias(const char *var, const char *value, void *data)
414 struct string_list *list = data;
416 if (skip_prefix(var, "alias.", &var))
417 string_list_append(list, var)->util = xstrdup(value);
419 return 0;
422 void list_all_cmds_help(void)
424 struct string_list others = STRING_LIST_INIT_DUP;
425 struct string_list alias_list = STRING_LIST_INIT_DUP;
426 struct cmdname_help *aliases;
427 int i, longest;
429 printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
430 print_cmd_by_category(main_categories, &longest);
432 list_all_other_cmds(&others);
433 if (others.nr)
434 printf("\n%s\n", _("External commands"));
435 for (i = 0; i < others.nr; i++)
436 printf(" %s\n", others.items[i].string);
437 string_list_clear(&others, 0);
439 git_config(get_alias, &alias_list);
440 string_list_sort(&alias_list);
442 for (i = 0; i < alias_list.nr; i++) {
443 size_t len = strlen(alias_list.items[i].string);
444 if (longest < len)
445 longest = len;
448 if (alias_list.nr) {
449 printf("\n%s\n", _("Command aliases"));
450 ALLOC_ARRAY(aliases, alias_list.nr + 1);
451 for (i = 0; i < alias_list.nr; i++) {
452 aliases[i].name = alias_list.items[i].string;
453 aliases[i].help = alias_list.items[i].util;
454 aliases[i].category = 1;
456 aliases[alias_list.nr].name = NULL;
457 print_command_list(aliases, 1, longest);
458 free(aliases);
460 string_list_clear(&alias_list, 1);
463 int is_in_cmdlist(struct cmdnames *c, const char *s)
465 int i;
466 for (i = 0; i < c->cnt; i++)
467 if (!strcmp(s, c->names[i]->name))
468 return 1;
469 return 0;
472 static int autocorrect;
473 static struct cmdnames aliases;
475 #define AUTOCORRECT_NEVER (-2)
476 #define AUTOCORRECT_IMMEDIATELY (-1)
478 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
480 const char *p;
482 if (!strcmp(var, "help.autocorrect")) {
483 if (!value)
484 return config_error_nonbool(var);
485 if (!strcmp(value, "never")) {
486 autocorrect = AUTOCORRECT_NEVER;
487 } else if (!strcmp(value, "immediate")) {
488 autocorrect = AUTOCORRECT_IMMEDIATELY;
489 } else {
490 int v = git_config_int(var, value);
491 autocorrect = (v < 0)
492 ? AUTOCORRECT_IMMEDIATELY : v;
495 /* Also use aliases for command lookup */
496 if (skip_prefix(var, "alias.", &p))
497 add_cmdname(&aliases, p, strlen(p));
499 return git_default_config(var, value, cb);
502 static int levenshtein_compare(const void *p1, const void *p2)
504 const struct cmdname *const *c1 = p1, *const *c2 = p2;
505 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
506 int l1 = (*c1)->len;
507 int l2 = (*c2)->len;
508 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
511 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
513 int i;
514 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
516 for (i = 0; i < old->cnt; i++)
517 cmds->names[cmds->cnt++] = old->names[i];
518 FREE_AND_NULL(old->names);
519 old->cnt = 0;
522 /* An empirically derived magic number */
523 #define SIMILARITY_FLOOR 7
524 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
526 static const char bad_interpreter_advice[] =
527 N_("'%s' appears to be a git command, but we were not\n"
528 "able to execute it. Maybe git-%s is broken?");
530 const char *help_unknown_cmd(const char *cmd)
532 int i, n, best_similarity = 0;
533 struct cmdnames main_cmds, other_cmds;
534 struct cmdname_help *common_cmds;
536 memset(&main_cmds, 0, sizeof(main_cmds));
537 memset(&other_cmds, 0, sizeof(other_cmds));
538 memset(&aliases, 0, sizeof(aliases));
540 read_early_config(git_unknown_cmd_config, NULL);
542 if (autocorrect == AUTOCORRECT_NEVER) {
543 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
544 exit(1);
547 load_command_list("git-", &main_cmds, &other_cmds);
549 add_cmd_list(&main_cmds, &aliases);
550 add_cmd_list(&main_cmds, &other_cmds);
551 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
552 uniq(&main_cmds);
554 extract_cmds(&common_cmds, common_mask);
556 /* This abuses cmdname->len for levenshtein distance */
557 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
558 int cmp = 0; /* avoid compiler stupidity */
559 const char *candidate = main_cmds.names[i]->name;
562 * An exact match means we have the command, but
563 * for some reason exec'ing it gave us ENOENT; probably
564 * it's a bad interpreter in the #! line.
566 if (!strcmp(candidate, cmd))
567 die(_(bad_interpreter_advice), cmd, cmd);
569 /* Does the candidate appear in common_cmds list? */
570 while (common_cmds[n].name &&
571 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
572 n++;
573 if (common_cmds[n].name && !cmp) {
574 /* Yes, this is one of the common commands */
575 n++; /* use the entry from common_cmds[] */
576 if (starts_with(candidate, cmd)) {
577 /* Give prefix match a very good score */
578 main_cmds.names[i]->len = 0;
579 continue;
583 main_cmds.names[i]->len =
584 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
586 FREE_AND_NULL(common_cmds);
588 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
590 if (!main_cmds.cnt)
591 die(_("Uh oh. Your system reports no Git commands at all."));
593 /* skip and count prefix matches */
594 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
595 ; /* still counting */
597 if (main_cmds.cnt <= n) {
598 /* prefix matches with everything? that is too ambiguous */
599 best_similarity = SIMILARITY_FLOOR + 1;
600 } else {
601 /* count all the most similar ones */
602 for (best_similarity = main_cmds.names[n++]->len;
603 (n < main_cmds.cnt &&
604 best_similarity == main_cmds.names[n]->len);
605 n++)
606 ; /* still counting */
608 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
609 const char *assumed = main_cmds.names[0]->name;
610 main_cmds.names[0] = NULL;
611 clean_cmdnames(&main_cmds);
612 fprintf_ln(stderr,
613 _("WARNING: You called a Git command named '%s', "
614 "which does not exist."),
615 cmd);
616 if (autocorrect == AUTOCORRECT_IMMEDIATELY)
617 fprintf_ln(stderr,
618 _("Continuing under the assumption that "
619 "you meant '%s'."),
620 assumed);
621 else {
622 fprintf_ln(stderr,
623 _("Continuing in %0.1f seconds, "
624 "assuming that you meant '%s'."),
625 (float)autocorrect/10.0, assumed);
626 sleep_millisec(autocorrect * 100);
628 return assumed;
631 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
633 if (SIMILAR_ENOUGH(best_similarity)) {
634 fprintf_ln(stderr,
635 Q_("\nThe most similar command is",
636 "\nThe most similar commands are",
637 n));
639 for (i = 0; i < n; i++)
640 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
643 exit(1);
646 void get_version_info(struct strbuf *buf, int show_build_options)
649 * The format of this string should be kept stable for compatibility
650 * with external projects that rely on the output of "git version".
652 * Always show the version, even if other options are given.
654 strbuf_addf(buf, "git version %s\n", git_version_string);
656 if (show_build_options) {
657 strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
658 if (git_built_from_commit_string[0])
659 strbuf_addf(buf, "built from commit: %s\n",
660 git_built_from_commit_string);
661 else
662 strbuf_addstr(buf, "no commit associated with this build\n");
663 strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
664 strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
665 strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
666 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
670 int cmd_version(int argc, const char **argv, const char *prefix)
672 struct strbuf buf = STRBUF_INIT;
673 int build_options = 0;
674 const char * const usage[] = {
675 N_("git version [<options>]"),
676 NULL
678 struct option options[] = {
679 OPT_BOOL(0, "build-options", &build_options,
680 "also print build options"),
681 OPT_END()
684 argc = parse_options(argc, argv, prefix, options, usage, 0);
686 get_version_info(&buf, build_options);
687 printf("%s", buf.buf);
689 strbuf_release(&buf);
691 return 0;
694 struct similar_ref_cb {
695 const char *base_ref;
696 struct string_list *similar_refs;
699 static int append_similar_ref(const char *refname, const struct object_id *oid,
700 int flags, void *cb_data)
702 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
703 char *branch = strrchr(refname, '/') + 1;
705 /* A remote branch of the same name is deemed similar */
706 if (starts_with(refname, "refs/remotes/") &&
707 !strcmp(branch, cb->base_ref))
708 string_list_append_nodup(cb->similar_refs,
709 shorten_unambiguous_ref(refname, 1));
710 return 0;
713 static struct string_list guess_refs(const char *ref)
715 struct similar_ref_cb ref_cb;
716 struct string_list similar_refs = STRING_LIST_INIT_DUP;
718 ref_cb.base_ref = ref;
719 ref_cb.similar_refs = &similar_refs;
720 for_each_ref(append_similar_ref, &ref_cb);
721 return similar_refs;
724 NORETURN void help_unknown_ref(const char *ref, const char *cmd,
725 const char *error)
727 int i;
728 struct string_list suggested_refs = guess_refs(ref);
730 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
732 if (suggested_refs.nr > 0) {
733 fprintf_ln(stderr,
734 Q_("\nDid you mean this?",
735 "\nDid you mean one of these?",
736 suggested_refs.nr));
737 for (i = 0; i < suggested_refs.nr; i++)
738 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
741 string_list_clear(&suggested_refs, 0);
742 exit(1);