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