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)") },
31 static const char *drop_prefix(const char *name
)
35 if (skip_prefix(name
, "git-", &new_name
))
41 static void extract_cmds(struct cmdname_help
**p_cmds
, uint32_t mask
)
44 struct cmdname_help
*cmds
;
46 if (ARRAY_SIZE(command_list
) == 0)
47 BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
49 ALLOC_ARRAY(cmds
, ARRAY_SIZE(command_list
) + 1);
51 for (i
= 0; i
< ARRAY_SIZE(command_list
); i
++) {
52 const struct cmdname_help
*cmd
= command_list
+ i
;
54 if (!(cmd
->category
& mask
))
58 cmds
[nr
].name
= drop_prefix(cmd
->name
);
66 static void print_command_list(const struct cmdname_help
*cmds
,
67 uint32_t mask
, int longest
)
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
));
80 static int cmd_name_cmp(const void *elem1
, const void *elem2
)
82 const struct cmdname_help
*e1
= elem1
;
83 const struct cmdname_help
*e2
= elem2
;
85 return strcmp(e1
->name
, e2
->name
);
88 static void print_cmd_by_category(const struct category_description
*catdesc
)
90 struct cmdname_help
*cmds
;
95 for (i
= 0; catdesc
[i
].desc
; i
++)
96 mask
|= catdesc
[i
].category
;
98 extract_cmds(&cmds
, mask
);
100 for (i
= 0; cmds
[i
].name
; i
++, nr
++) {
101 if (longest
< strlen(cmds
[i
].name
))
102 longest
= strlen(cmds
[i
].name
);
104 QSORT(cmds
, nr
, cmd_name_cmp
);
106 for (i
= 0; catdesc
[i
].desc
; i
++) {
107 uint32_t mask
= catdesc
[i
].category
;
108 const char *desc
= catdesc
[i
].desc
;
110 printf("\n%s\n", _(desc
));
111 print_command_list(cmds
, mask
, longest
);
116 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
119 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
122 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
123 cmds
->names
[cmds
->cnt
++] = ent
;
126 static void clean_cmdnames(struct cmdnames
*cmds
)
129 for (i
= 0; i
< cmds
->cnt
; ++i
)
130 free(cmds
->names
[i
]);
136 static int cmdname_compare(const void *a_
, const void *b_
)
138 struct cmdname
*a
= *(struct cmdname
**)a_
;
139 struct cmdname
*b
= *(struct cmdname
**)b_
;
140 return strcmp(a
->name
, b
->name
);
143 static void uniq(struct cmdnames
*cmds
)
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
]);
154 cmds
->names
[j
++] = cmds
->names
[i
];
160 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
166 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
167 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
169 cmds
->names
[cj
++] = cmds
->names
[ci
++];
172 free(cmds
->names
[ci
++]);
177 while (ci
< cmds
->cnt
)
178 cmds
->names
[cj
++] = cmds
->names
[ci
++];
183 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
185 struct string_list list
= STRING_LIST_INIT_NODUP
;
186 struct column_options copts
;
189 for (i
= 0; i
< cmds
->cnt
; i
++)
190 string_list_append(&list
, cmds
->names
[i
]->name
);
192 * always enable column display, we only consult column.*
193 * about layout strategy and stuff
195 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
196 memset(&copts
, 0, sizeof(copts
));
199 print_columns(&list
, colopts
, &copts
);
200 string_list_clear(&list
, 0);
203 static void list_commands_in_dir(struct cmdnames
*cmds
,
207 DIR *dir
= opendir(path
);
209 struct strbuf buf
= STRBUF_INIT
;
217 strbuf_addf(&buf
, "%s/", path
);
220 while ((de
= readdir(dir
)) != NULL
) {
224 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
227 strbuf_setlen(&buf
, len
);
228 strbuf_addstr(&buf
, de
->d_name
);
229 if (!is_executable(buf
.buf
))
232 entlen
= strlen(ent
);
233 strip_suffix(ent
, ".exe", &entlen
);
235 add_cmdname(cmds
, ent
, entlen
);
238 strbuf_release(&buf
);
241 void load_command_list(const char *prefix
,
242 struct cmdnames
*main_cmds
,
243 struct cmdnames
*other_cmds
)
245 const char *env_path
= getenv("PATH");
246 const char *exec_path
= git_exec_path();
249 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
250 QSORT(main_cmds
->names
, main_cmds
->cnt
, cmdname_compare
);
255 char *paths
, *path
, *colon
;
256 path
= paths
= xstrdup(env_path
);
258 if ((colon
= strchr(path
, PATH_SEP
)))
260 if (!exec_path
|| strcmp(path
, exec_path
))
261 list_commands_in_dir(other_cmds
, path
, prefix
);
269 QSORT(other_cmds
->names
, other_cmds
->cnt
, cmdname_compare
);
272 exclude_cmds(other_cmds
, main_cmds
);
275 void list_commands(unsigned int colopts
,
276 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
278 if (main_cmds
->cnt
) {
279 const char *exec_path
= git_exec_path();
280 printf_ln(_("available git commands in '%s'"), exec_path
);
282 pretty_print_cmdnames(main_cmds
, colopts
);
286 if (other_cmds
->cnt
) {
287 printf_ln(_("git commands available from elsewhere on your $PATH"));
289 pretty_print_cmdnames(other_cmds
, colopts
);
294 void list_common_cmds_help(void)
296 puts(_("These are common Git commands used in various situations:"));
297 print_cmd_by_category(common_categories
);
300 void list_all_main_cmds(struct string_list
*list
)
302 struct cmdnames main_cmds
, other_cmds
;
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
);
309 for (i
= 0; i
< main_cmds
.cnt
; i
++)
310 string_list_append(list
, main_cmds
.names
[i
]->name
);
312 clean_cmdnames(&main_cmds
);
313 clean_cmdnames(&other_cmds
);
316 void list_all_other_cmds(struct string_list
*list
)
318 struct cmdnames main_cmds
, other_cmds
;
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
);
325 for (i
= 0; i
< other_cmds
.cnt
; i
++)
326 string_list_append(list
, other_cmds
.names
[i
]->name
);
328 clean_cmdnames(&main_cmds
);
329 clean_cmdnames(&other_cmds
);
332 void list_cmds_by_category(struct string_list
*list
,
335 int i
, n
= ARRAY_SIZE(command_list
);
338 for (i
= 0; category_names
[i
]; i
++) {
339 if (!strcmp(cat
, category_names
[i
])) {
345 die(_("unsupported command listing type '%s'"), cat
);
347 for (i
= 0; i
< n
; i
++) {
348 struct cmdname_help
*cmd
= command_list
+ i
;
350 if (cmd
->category
& cat_id
)
351 string_list_append(list
, drop_prefix(cmd
->name
));
355 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
358 for (i
= 0; i
< c
->cnt
; i
++)
359 if (!strcmp(s
, c
->names
[i
]->name
))
364 static int autocorrect
;
365 static struct cmdnames aliases
;
367 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
371 if (!strcmp(var
, "help.autocorrect"))
372 autocorrect
= git_config_int(var
,value
);
373 /* Also use aliases for command lookup */
374 if (skip_prefix(var
, "alias.", &p
))
375 add_cmdname(&aliases
, p
, strlen(p
));
377 return git_default_config(var
, value
, cb
);
380 static int levenshtein_compare(const void *p1
, const void *p2
)
382 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
383 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
386 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
389 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
392 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
394 for (i
= 0; i
< old
->cnt
; i
++)
395 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
396 FREE_AND_NULL(old
->names
);
400 /* An empirically derived magic number */
401 #define SIMILARITY_FLOOR 7
402 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
404 static const char bad_interpreter_advice
[] =
405 N_("'%s' appears to be a git command, but we were not\n"
406 "able to execute it. Maybe git-%s is broken?");
408 const char *help_unknown_cmd(const char *cmd
)
410 int i
, n
, best_similarity
= 0;
411 struct cmdnames main_cmds
, other_cmds
;
412 struct cmdname_help
*common_cmds
;
414 memset(&main_cmds
, 0, sizeof(main_cmds
));
415 memset(&other_cmds
, 0, sizeof(other_cmds
));
416 memset(&aliases
, 0, sizeof(aliases
));
418 read_early_config(git_unknown_cmd_config
, NULL
);
420 load_command_list("git-", &main_cmds
, &other_cmds
);
422 add_cmd_list(&main_cmds
, &aliases
);
423 add_cmd_list(&main_cmds
, &other_cmds
);
424 QSORT(main_cmds
.names
, main_cmds
.cnt
, cmdname_compare
);
427 extract_cmds(&common_cmds
, common_mask
);
429 /* This abuses cmdname->len for levenshtein distance */
430 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
431 int cmp
= 0; /* avoid compiler stupidity */
432 const char *candidate
= main_cmds
.names
[i
]->name
;
435 * An exact match means we have the command, but
436 * for some reason exec'ing it gave us ENOENT; probably
437 * it's a bad interpreter in the #! line.
439 if (!strcmp(candidate
, cmd
))
440 die(_(bad_interpreter_advice
), cmd
, cmd
);
442 /* Does the candidate appear in common_cmds list? */
443 while (common_cmds
[n
].name
&&
444 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
446 if (common_cmds
[n
].name
&& !cmp
) {
447 /* Yes, this is one of the common commands */
448 n
++; /* use the entry from common_cmds[] */
449 if (starts_with(candidate
, cmd
)) {
450 /* Give prefix match a very good score */
451 main_cmds
.names
[i
]->len
= 0;
456 main_cmds
.names
[i
]->len
=
457 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
459 FREE_AND_NULL(common_cmds
);
461 QSORT(main_cmds
.names
, main_cmds
.cnt
, levenshtein_compare
);
464 die(_("Uh oh. Your system reports no Git commands at all."));
466 /* skip and count prefix matches */
467 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
468 ; /* still counting */
470 if (main_cmds
.cnt
<= n
) {
471 /* prefix matches with everything? that is too ambiguous */
472 best_similarity
= SIMILARITY_FLOOR
+ 1;
474 /* count all the most similar ones */
475 for (best_similarity
= main_cmds
.names
[n
++]->len
;
476 (n
< main_cmds
.cnt
&&
477 best_similarity
== main_cmds
.names
[n
]->len
);
479 ; /* still counting */
481 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
482 const char *assumed
= main_cmds
.names
[0]->name
;
483 main_cmds
.names
[0] = NULL
;
484 clean_cmdnames(&main_cmds
);
486 _("WARNING: You called a Git command named '%s', "
487 "which does not exist."),
491 _("Continuing under the assumption that "
496 _("Continuing in %0.1f seconds, "
497 "assuming that you meant '%s'."),
498 (float)autocorrect
/10.0, assumed
);
499 sleep_millisec(autocorrect
* 100);
504 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
506 if (SIMILAR_ENOUGH(best_similarity
)) {
508 Q_("\nThe most similar command is",
509 "\nThe most similar commands are",
512 for (i
= 0; i
< n
; i
++)
513 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
519 int cmd_version(int argc
, const char **argv
, const char *prefix
)
521 int build_options
= 0;
522 const char * const usage
[] = {
523 N_("git version [<options>]"),
526 struct option options
[] = {
527 OPT_BOOL(0, "build-options", &build_options
,
528 "also print build options"),
532 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
535 * The format of this string should be kept stable for compatibility
536 * with external projects that rely on the output of "git version".
538 * Always show the version, even if other options are given.
540 printf("git version %s\n", git_version_string
);
543 printf("cpu: %s\n", GIT_HOST_CPU
);
544 if (git_built_from_commit_string
[0])
545 printf("built from commit: %s\n",
546 git_built_from_commit_string
);
548 printf("no commit associated with this build\n");
549 printf("sizeof-long: %d\n", (int)sizeof(long));
550 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
555 struct similar_ref_cb
{
556 const char *base_ref
;
557 struct string_list
*similar_refs
;
560 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
561 int flags
, void *cb_data
)
563 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
564 char *branch
= strrchr(refname
, '/') + 1;
567 /* A remote branch of the same name is deemed similar */
568 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
569 !strcmp(branch
, cb
->base_ref
))
570 string_list_append(cb
->similar_refs
, remote
);
574 static struct string_list
guess_refs(const char *ref
)
576 struct similar_ref_cb ref_cb
;
577 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
579 ref_cb
.base_ref
= ref
;
580 ref_cb
.similar_refs
= &similar_refs
;
581 for_each_ref(append_similar_ref
, &ref_cb
);
585 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
588 struct string_list suggested_refs
= guess_refs(ref
);
590 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
592 if (suggested_refs
.nr
> 0) {
594 Q_("\nDid you mean this?",
595 "\nDid you mean one of these?",
597 for (i
= 0; i
< suggested_refs
.nr
; i
++)
598 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
601 string_list_clear(&suggested_refs
, 0);