]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
help: add "-a --verbose" to list all commands with synopsis
[thirdparty/git.git] / help.c
CommitLineData
70827b15 1#include "cache.h"
b2141fc1 2#include "config.h"
70827b15
LT
3#include "builtin.h"
4#include "exec_cmd.h"
38124a40 5#include "run-command.h"
8af84dad 6#include "levenshtein.h"
940208a7 7#include "help.h"
cfb22a02 8#include "command-list.h"
dbfae689
NTND
9#include "string-list.h"
10#include "column.h"
816fb46b 11#include "version.h"
e5618106 12#include "refs.h"
b48cbfc5 13#include "parse-options.h"
64949984 14
cfb22a02
NTND
15struct category_description {
16 uint32_t category;
17 const char *desc;
18};
19static uint32_t common_mask =
20 CAT_init | CAT_worktree | CAT_info |
21 CAT_history | CAT_remote;
22static 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 }
29};
63eae83f
NTND
30static 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") },
39 { 0, NULL }
40};
cfb22a02
NTND
41
42static const char *drop_prefix(const char *name)
43{
44 const char *new_name;
45
46 if (skip_prefix(name, "git-", &new_name))
47 return new_name;
48 return name;
49
50}
51
52static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
53{
54 int i, nr = 0;
55 struct cmdname_help *cmds;
56
57 if (ARRAY_SIZE(command_list) == 0)
58 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
59
60 ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
61
62 for (i = 0; i < ARRAY_SIZE(command_list); i++) {
63 const struct cmdname_help *cmd = command_list + i;
64
65 if (!(cmd->category & mask))
66 continue;
67
68 cmds[nr] = *cmd;
69 cmds[nr].name = drop_prefix(cmd->name);
70
71 nr++;
72 }
73 cmds[nr].name = NULL;
74 *p_cmds = cmds;
75}
76
77static void print_command_list(const struct cmdname_help *cmds,
78 uint32_t mask, int longest)
79{
80 int i;
81
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));
87 }
88 }
89}
90
91static int cmd_name_cmp(const void *elem1, const void *elem2)
92{
93 const struct cmdname_help *e1 = elem1;
94 const struct cmdname_help *e2 = elem2;
95
96 return strcmp(e1->name, e2->name);
97}
98
99static void print_cmd_by_category(const struct category_description *catdesc)
100{
101 struct cmdname_help *cmds;
102 int longest = 0;
103 int i, nr = 0;
104 uint32_t mask = 0;
105
106 for (i = 0; catdesc[i].desc; i++)
107 mask |= catdesc[i].category;
108
109 extract_cmds(&cmds, mask);
110
111 for (i = 0; cmds[i].name; i++, nr++) {
112 if (longest < strlen(cmds[i].name))
113 longest = strlen(cmds[i].name);
114 }
115 QSORT(cmds, nr, cmd_name_cmp);
116
117 for (i = 0; catdesc[i].desc; i++) {
118 uint32_t mask = catdesc[i].category;
119 const char *desc = catdesc[i].desc;
120
121 printf("\n%s\n", _(desc));
122 print_command_list(cmds, mask, longest);
123 }
124 free(cmds);
125}
126
940208a7 127void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 128{
96ffc06f
JK
129 struct cmdname *ent;
130 FLEX_ALLOC_MEM(ent, name, name, len);
70827b15 131 ent->len = len;
1eb05690
SP
132
133 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
134 cmds->names[cmds->cnt++] = ent;
70827b15
LT
135}
136
8af84dad
JS
137static void clean_cmdnames(struct cmdnames *cmds)
138{
139 int i;
140 for (i = 0; i < cmds->cnt; ++i)
141 free(cmds->names[i]);
142 free(cmds->names);
143 cmds->cnt = 0;
144 cmds->alloc = 0;
145}
146
70827b15
LT
147static int cmdname_compare(const void *a_, const void *b_)
148{
149 struct cmdname *a = *(struct cmdname **)a_;
150 struct cmdname *b = *(struct cmdname **)b_;
151 return strcmp(a->name, b->name);
152}
153
1eb05690
SP
154static void uniq(struct cmdnames *cmds)
155{
156 int i, j;
157
158 if (!cmds->cnt)
159 return;
160
4a15758f
JK
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]);
164 else
1eb05690 165 cmds->names[j++] = cmds->names[i];
4a15758f 166 }
1eb05690
SP
167
168 cmds->cnt = j;
169}
170
940208a7 171void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 172{
1eb05690
SP
173 int ci, cj, ei;
174 int cmp;
175
176 ci = cj = ei = 0;
177 while (ci < cmds->cnt && ei < excludes->cnt) {
178 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
179 if (cmp < 0)
180 cmds->names[cj++] = cmds->names[ci++];
6a17f583
JH
181 else if (cmp == 0) {
182 ei++;
183 free(cmds->names[ci++]);
184 } else if (cmp > 0)
1eb05690
SP
185 ei++;
186 }
187
188 while (ci < cmds->cnt)
189 cmds->names[cj++] = cmds->names[ci++];
190
191 cmds->cnt = cj;
192}
193
d10cb3df 194static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
70827b15 195{
dbfae689
NTND
196 struct string_list list = STRING_LIST_INIT_NODUP;
197 struct column_options copts;
198 int i;
70827b15 199
dbfae689
NTND
200 for (i = 0; i < cmds->cnt; i++)
201 string_list_append(&list, cmds->names[i]->name);
202 /*
203 * always enable column display, we only consult column.*
204 * about layout strategy and stuff
205 */
206 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
207 memset(&copts, 0, sizeof(copts));
208 copts.indent = " ";
209 copts.padding = 2;
210 print_columns(&list, colopts, &copts);
211 string_list_clear(&list, 0);
70827b15
LT
212}
213
e321180e 214static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
215 const char *path,
216 const char *prefix)
70827b15 217{
1eb05690 218 DIR *dir = opendir(path);
70827b15 219 struct dirent *de;
f5d600e2
JS
220 struct strbuf buf = STRBUF_INIT;
221 int len;
70827b15 222
f5d600e2 223 if (!dir)
e321180e 224 return;
940208a7
MV
225 if (!prefix)
226 prefix = "git-";
70827b15 227
f5d600e2
JS
228 strbuf_addf(&buf, "%s/", path);
229 len = buf.len;
230
70827b15 231 while ((de = readdir(dir)) != NULL) {
de8118e1 232 const char *ent;
26936bfd 233 size_t entlen;
70827b15 234
de8118e1 235 if (!skip_prefix(de->d_name, prefix, &ent))
70827b15 236 continue;
0966003c 237
f5d600e2
JS
238 strbuf_setlen(&buf, len);
239 strbuf_addstr(&buf, de->d_name);
240 if (!is_executable(buf.buf))
70827b15
LT
241 continue;
242
de8118e1 243 entlen = strlen(ent);
6e409473 244 strip_suffix(ent, ".exe", &entlen);
70827b15 245
de8118e1 246 add_cmdname(cmds, ent, entlen);
70827b15
LT
247 }
248 closedir(dir);
f5d600e2 249 strbuf_release(&buf);
1eb05690
SP
250}
251
e321180e 252void load_command_list(const char *prefix,
940208a7
MV
253 struct cmdnames *main_cmds,
254 struct cmdnames *other_cmds)
1eb05690 255{
1eb05690 256 const char *env_path = getenv("PATH");
1eb05690
SP
257 const char *exec_path = git_exec_path();
258
1f08e5ce 259 if (exec_path) {
e321180e 260 list_commands_in_dir(main_cmds, exec_path, prefix);
9ed0d8d6 261 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
1f08e5ce 262 uniq(main_cmds);
1eb05690
SP
263 }
264
1f08e5ce
AR
265 if (env_path) {
266 char *paths, *path, *colon;
267 path = paths = xstrdup(env_path);
268 while (1) {
269 if ((colon = strchr(path, PATH_SEP)))
270 *colon = 0;
746c221a
PB
271 if (!exec_path || strcmp(path, exec_path))
272 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 273
1f08e5ce
AR
274 if (!colon)
275 break;
276 path = colon + 1;
277 }
278 free(paths);
1eb05690 279
9ed0d8d6 280 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
1f08e5ce
AR
281 uniq(other_cmds);
282 }
940208a7 283 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
284}
285
f4ed0af6 286void list_commands(unsigned int colopts,
dbfae689 287 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
2156435f 288{
940208a7 289 if (main_cmds->cnt) {
61c5d431 290 const char *exec_path = git_exec_path();
4470ef94 291 printf_ln(_("available git commands in '%s'"), exec_path);
1eb05690 292 putchar('\n');
d10cb3df 293 pretty_print_cmdnames(main_cmds, colopts);
1eb05690
SP
294 putchar('\n');
295 }
296
940208a7 297 if (other_cmds->cnt) {
4470ef94 298 printf_ln(_("git commands available from elsewhere on your $PATH"));
940208a7 299 putchar('\n');
d10cb3df 300 pretty_print_cmdnames(other_cmds, colopts);
1eb05690
SP
301 putchar('\n');
302 }
70827b15
LT
303}
304
1542d4cd
JH
305void list_common_cmds_help(void)
306{
22414770 307 puts(_("These are common Git commands used in various situations:"));
cfb22a02 308 print_cmd_by_category(common_categories);
1542d4cd
JH
309}
310
6bb2dc0b
NTND
311void list_all_main_cmds(struct string_list *list)
312{
313 struct cmdnames main_cmds, other_cmds;
314 int i;
315
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);
319
320 for (i = 0; i < main_cmds.cnt; i++)
321 string_list_append(list, main_cmds.names[i]->name);
322
323 clean_cmdnames(&main_cmds);
324 clean_cmdnames(&other_cmds);
325}
326
327void list_all_other_cmds(struct string_list *list)
328{
329 struct cmdnames main_cmds, other_cmds;
330 int i;
331
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);
335
336 for (i = 0; i < other_cmds.cnt; i++)
337 string_list_append(list, other_cmds.names[i]->name);
338
339 clean_cmdnames(&main_cmds);
340 clean_cmdnames(&other_cmds);
341}
342
3c777767
NTND
343void list_cmds_by_category(struct string_list *list,
344 const char *cat)
345{
346 int i, n = ARRAY_SIZE(command_list);
347 uint32_t cat_id = 0;
348
349 for (i = 0; category_names[i]; i++) {
350 if (!strcmp(cat, category_names[i])) {
351 cat_id = 1UL << i;
352 break;
353 }
354 }
355 if (!cat_id)
356 die(_("unsupported command listing type '%s'"), cat);
357
358 for (i = 0; i < n; i++) {
359 struct cmdname_help *cmd = command_list + i;
360
361 if (cmd->category & cat_id)
362 string_list_append(list, drop_prefix(cmd->name));
363 }
364}
365
63eae83f
NTND
366void list_all_cmds_help(void)
367{
368 print_cmd_by_category(main_categories);
369}
370
940208a7 371int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
372{
373 int i;
374 for (i = 0; i < c->cnt; i++)
375 if (!strcmp(s, c->names[i]->name))
376 return 1;
377 return 0;
378}
379
f0e90716 380static int autocorrect;
746c221a 381static struct cmdnames aliases;
f0e90716
AR
382
383static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
384{
ae021d87
JK
385 const char *p;
386
f0e90716
AR
387 if (!strcmp(var, "help.autocorrect"))
388 autocorrect = git_config_int(var,value);
746c221a 389 /* Also use aliases for command lookup */
ae021d87
JK
390 if (skip_prefix(var, "alias.", &p))
391 add_cmdname(&aliases, p, strlen(p));
f0e90716
AR
392
393 return git_default_config(var, value, cb);
394}
395
8af84dad 396static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 397{
8af84dad
JS
398 const struct cmdname *const *c1 = p1, *const *c2 = p2;
399 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
400 int l1 = (*c1)->len;
401 int l2 = (*c2)->len;
402 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
403}
404
746c221a
PB
405static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
406{
407 int i;
408 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
409
410 for (i = 0; i < old->cnt; i++)
411 cmds->names[cmds->cnt++] = old->names[i];
88ce3ef6 412 FREE_AND_NULL(old->names);
746c221a 413 old->cnt = 0;
746c221a
PB
414}
415
06500a02 416/* An empirically derived magic number */
6612b9e4
EFL
417#define SIMILARITY_FLOOR 7
418#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 419
823e0ded
MS
420static 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?");
423
8af84dad 424const char *help_unknown_cmd(const char *cmd)
822a7d50 425{
8af84dad
JS
426 int i, n, best_similarity = 0;
427 struct cmdnames main_cmds, other_cmds;
cfb22a02 428 struct cmdname_help *common_cmds;
8af84dad
JS
429
430 memset(&main_cmds, 0, sizeof(main_cmds));
0b74f5dc 431 memset(&other_cmds, 0, sizeof(other_cmds));
746c221a 432 memset(&aliases, 0, sizeof(aliases));
8af84dad 433
659fef19 434 read_early_config(git_unknown_cmd_config, NULL);
f0e90716 435
8af84dad
JS
436 load_command_list("git-", &main_cmds, &other_cmds);
437
746c221a
PB
438 add_cmd_list(&main_cmds, &aliases);
439 add_cmd_list(&main_cmds, &other_cmds);
9ed0d8d6 440 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
746c221a 441 uniq(&main_cmds);
8af84dad 442
cfb22a02
NTND
443 extract_cmds(&common_cmds, common_mask);
444
6612b9e4
EFL
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;
449
823e0ded
MS
450 /*
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.
454 */
455 if (!strcmp(candidate, cmd))
456 die(_(bad_interpreter_advice), cmd, cmd);
457
6612b9e4 458 /* Does the candidate appear in common_cmds list? */
cfb22a02 459 while (common_cmds[n].name &&
6612b9e4
EFL
460 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
461 n++;
cfb22a02 462 if (common_cmds[n].name && !cmp) {
6612b9e4
EFL
463 /* Yes, this is one of the common commands */
464 n++; /* use the entry from common_cmds[] */
59556548 465 if (starts_with(candidate, cmd)) {
6612b9e4
EFL
466 /* Give prefix match a very good score */
467 main_cmds.names[i]->len = 0;
468 continue;
469 }
470 }
471
8af84dad 472 main_cmds.names[i]->len =
c41494f8 473 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
6612b9e4 474 }
cfb22a02 475 FREE_AND_NULL(common_cmds);
8af84dad 476
9ed0d8d6 477 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
8af84dad
JS
478
479 if (!main_cmds.cnt)
9665627d 480 die(_("Uh oh. Your system reports no Git commands at all."));
8af84dad 481
6612b9e4
EFL
482 /* skip and count prefix matches */
483 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
484 ; /* still counting */
485
486 if (main_cmds.cnt <= n) {
487 /* prefix matches with everything? that is too ambiguous */
488 best_similarity = SIMILARITY_FLOOR + 1;
489 } else {
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);
494 n++)
495 ; /* still counting */
496 }
06500a02 497 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
498 const char *assumed = main_cmds.names[0]->name;
499 main_cmds.names[0] = NULL;
500 clean_cmdnames(&main_cmds);
9665627d
NTND
501 fprintf_ln(stderr,
502 _("WARNING: You called a Git command named '%s', "
968b1fe2
MB
503 "which does not exist."),
504 cmd);
505 if (autocorrect < 0)
506 fprintf_ln(stderr,
507 _("Continuing under the assumption that "
508 "you meant '%s'."),
509 assumed);
510 else {
511 fprintf_ln(stderr,
512 _("Continuing in %0.1f seconds, "
513 "assuming that you meant '%s'."),
514 (float)autocorrect/10.0, assumed);
2024d317 515 sleep_millisec(autocorrect * 100);
f0e90716 516 }
8af84dad
JS
517 return assumed;
518 }
519
9665627d 520 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
8af84dad 521
06500a02 522 if (SIMILAR_ENOUGH(best_similarity)) {
9665627d 523 fprintf_ln(stderr,
6c486862
JNA
524 Q_("\nThe most similar command is",
525 "\nThe most similar commands are",
9665627d 526 n));
8af84dad
JS
527
528 for (i = 0; i < n; i++)
529 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
530 }
531
822a7d50
RJ
532 exit(1);
533}
534
a633fca0 535int cmd_version(int argc, const char **argv, const char *prefix)
70827b15 536{
b48cbfc5
JK
537 int build_options = 0;
538 const char * const usage[] = {
539 N_("git version [<options>]"),
540 NULL
541 };
542 struct option options[] = {
543 OPT_BOOL(0, "build-options", &build_options,
544 "also print build options"),
545 OPT_END()
546 };
547
548 argc = parse_options(argc, argv, prefix, options, usage, 0);
549
f2de0b97
DA
550 /*
551 * The format of this string should be kept stable for compatibility
552 * with external projects that rely on the output of "git version".
b48cbfc5
JK
553 *
554 * Always show the version, even if other options are given.
f2de0b97 555 */
70827b15 556 printf("git version %s\n", git_version_string);
b48cbfc5
JK
557
558 if (build_options) {
b2289404 559 printf("cpu: %s\n", GIT_HOST_CPU);
ed32b788
JS
560 if (git_built_from_commit_string[0])
561 printf("built from commit: %s\n",
562 git_built_from_commit_string);
563 else
564 printf("no commit associated with this build\n");
b48cbfc5
JK
565 printf("sizeof-long: %d\n", (int)sizeof(long));
566 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
6b9c38e1 567 }
70827b15
LT
568 return 0;
569}
e5618106
VV
570
571struct similar_ref_cb {
572 const char *base_ref;
573 struct string_list *similar_refs;
574};
575
91d6e94e 576static int append_similar_ref(const char *refname, const struct object_id *oid,
e5618106
VV
577 int flags, void *cb_data)
578{
579 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
580 char *branch = strrchr(refname, '/') + 1;
95b567c7
JK
581 const char *remote;
582
e5618106 583 /* A remote branch of the same name is deemed similar */
95b567c7 584 if (skip_prefix(refname, "refs/remotes/", &remote) &&
e5618106 585 !strcmp(branch, cb->base_ref))
95b567c7 586 string_list_append(cb->similar_refs, remote);
e5618106
VV
587 return 0;
588}
589
590static struct string_list guess_refs(const char *ref)
591{
592 struct similar_ref_cb ref_cb;
593 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
594
595 ref_cb.base_ref = ref;
596 ref_cb.similar_refs = &similar_refs;
597 for_each_ref(append_similar_ref, &ref_cb);
598 return similar_refs;
599}
600
601void help_unknown_ref(const char *ref, const char *cmd, const char *error)
602{
603 int i;
604 struct string_list suggested_refs = guess_refs(ref);
605
606 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
607
608 if (suggested_refs.nr > 0) {
609 fprintf_ln(stderr,
610 Q_("\nDid you mean this?",
611 "\nDid you mean one of these?",
612 suggested_refs.nr));
613 for (i = 0; i < suggested_refs.nr; i++)
614 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
615 }
616
617 string_list_clear(&suggested_refs, 0);
618 exit(1);
619}