4 #include "levenshtein.h"
6 #include "common-cmds.h"
7 #include "string-list.h"
12 void add_cmdname(struct cmdnames
*cmds
, const char *name
, int len
)
15 FLEX_ALLOC_MEM(ent
, name
, name
, len
);
18 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ 1, cmds
->alloc
);
19 cmds
->names
[cmds
->cnt
++] = ent
;
22 static void clean_cmdnames(struct cmdnames
*cmds
)
25 for (i
= 0; i
< cmds
->cnt
; ++i
)
32 static int cmdname_compare(const void *a_
, const void *b_
)
34 struct cmdname
*a
= *(struct cmdname
**)a_
;
35 struct cmdname
*b
= *(struct cmdname
**)b_
;
36 return strcmp(a
->name
, b
->name
);
39 static void uniq(struct cmdnames
*cmds
)
46 for (i
= j
= 1; i
< cmds
->cnt
; i
++) {
47 if (!strcmp(cmds
->names
[i
]->name
, cmds
->names
[j
-1]->name
))
50 cmds
->names
[j
++] = cmds
->names
[i
];
56 void exclude_cmds(struct cmdnames
*cmds
, struct cmdnames
*excludes
)
62 while (ci
< cmds
->cnt
&& ei
< excludes
->cnt
) {
63 cmp
= strcmp(cmds
->names
[ci
]->name
, excludes
->names
[ei
]->name
);
65 cmds
->names
[cj
++] = cmds
->names
[ci
++];
68 free(cmds
->names
[ci
++]);
73 while (ci
< cmds
->cnt
)
74 cmds
->names
[cj
++] = cmds
->names
[ci
++];
79 static void pretty_print_cmdnames(struct cmdnames
*cmds
, unsigned int colopts
)
81 struct string_list list
= STRING_LIST_INIT_NODUP
;
82 struct column_options copts
;
85 for (i
= 0; i
< cmds
->cnt
; i
++)
86 string_list_append(&list
, cmds
->names
[i
]->name
);
88 * always enable column display, we only consult column.*
89 * about layout strategy and stuff
91 colopts
= (colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
92 memset(&copts
, 0, sizeof(copts
));
95 print_columns(&list
, colopts
, &copts
);
96 string_list_clear(&list
, 0);
99 static int is_executable(const char *name
)
103 if (stat(name
, &st
) || /* stat, not lstat */
104 !S_ISREG(st
.st_mode
))
107 #if defined(GIT_WINDOWS_NATIVE)
108 { /* cannot trust the executable bit, peek into the file instead */
111 int fd
= open(name
, O_RDONLY
);
112 st
.st_mode
&= ~S_IXUSR
;
114 n
= read(fd
, buf
, 2);
116 /* DOS executables start with "MZ" */
117 if (!strcmp(buf
, "#!") || !strcmp(buf
, "MZ"))
118 st
.st_mode
|= S_IXUSR
;
123 return st
.st_mode
& S_IXUSR
;
126 static void list_commands_in_dir(struct cmdnames
*cmds
,
130 DIR *dir
= opendir(path
);
132 struct strbuf buf
= STRBUF_INIT
;
140 strbuf_addf(&buf
, "%s/", path
);
143 while ((de
= readdir(dir
)) != NULL
) {
147 if (!skip_prefix(de
->d_name
, prefix
, &ent
))
150 strbuf_setlen(&buf
, len
);
151 strbuf_addstr(&buf
, de
->d_name
);
152 if (!is_executable(buf
.buf
))
155 entlen
= strlen(ent
);
156 strip_suffix(ent
, ".exe", &entlen
);
158 add_cmdname(cmds
, ent
, entlen
);
161 strbuf_release(&buf
);
164 void load_command_list(const char *prefix
,
165 struct cmdnames
*main_cmds
,
166 struct cmdnames
*other_cmds
)
168 const char *env_path
= getenv("PATH");
169 const char *exec_path
= git_exec_path();
172 list_commands_in_dir(main_cmds
, exec_path
, prefix
);
173 qsort(main_cmds
->names
, main_cmds
->cnt
,
174 sizeof(*main_cmds
->names
), cmdname_compare
);
179 char *paths
, *path
, *colon
;
180 path
= paths
= xstrdup(env_path
);
182 if ((colon
= strchr(path
, PATH_SEP
)))
184 if (!exec_path
|| strcmp(path
, exec_path
))
185 list_commands_in_dir(other_cmds
, path
, prefix
);
193 qsort(other_cmds
->names
, other_cmds
->cnt
,
194 sizeof(*other_cmds
->names
), cmdname_compare
);
197 exclude_cmds(other_cmds
, main_cmds
);
200 void list_commands(unsigned int colopts
,
201 struct cmdnames
*main_cmds
, struct cmdnames
*other_cmds
)
203 if (main_cmds
->cnt
) {
204 const char *exec_path
= git_exec_path();
205 printf_ln(_("available git commands in '%s'"), exec_path
);
207 pretty_print_cmdnames(main_cmds
, colopts
);
211 if (other_cmds
->cnt
) {
212 printf_ln(_("git commands available from elsewhere on your $PATH"));
214 pretty_print_cmdnames(other_cmds
, colopts
);
219 static int cmd_group_cmp(const void *elem1
, const void *elem2
)
221 const struct cmdname_help
*e1
= elem1
;
222 const struct cmdname_help
*e2
= elem2
;
224 if (e1
->group
< e2
->group
)
226 if (e1
->group
> e2
->group
)
228 return strcmp(e1
->name
, e2
->name
);
231 void list_common_cmds_help(void)
234 int current_grp
= -1;
236 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
237 if (longest
< strlen(common_cmds
[i
].name
))
238 longest
= strlen(common_cmds
[i
].name
);
241 qsort(common_cmds
, ARRAY_SIZE(common_cmds
),
242 sizeof(common_cmds
[0]), cmd_group_cmp
);
244 puts(_("These are common Git commands used in various situations:"));
246 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
247 if (common_cmds
[i
].group
!= current_grp
) {
248 printf("\n%s\n", _(common_cmd_groups
[common_cmds
[i
].group
]));
249 current_grp
= common_cmds
[i
].group
;
252 printf(" %s ", common_cmds
[i
].name
);
253 mput_char(' ', longest
- strlen(common_cmds
[i
].name
));
254 puts(_(common_cmds
[i
].help
));
258 int is_in_cmdlist(struct cmdnames
*c
, const char *s
)
261 for (i
= 0; i
< c
->cnt
; i
++)
262 if (!strcmp(s
, c
->names
[i
]->name
))
267 static int autocorrect
;
268 static struct cmdnames aliases
;
270 static int git_unknown_cmd_config(const char *var
, const char *value
, void *cb
)
274 if (!strcmp(var
, "help.autocorrect"))
275 autocorrect
= git_config_int(var
,value
);
276 /* Also use aliases for command lookup */
277 if (skip_prefix(var
, "alias.", &p
))
278 add_cmdname(&aliases
, p
, strlen(p
));
280 return git_default_config(var
, value
, cb
);
283 static int levenshtein_compare(const void *p1
, const void *p2
)
285 const struct cmdname
*const *c1
= p1
, *const *c2
= p2
;
286 const char *s1
= (*c1
)->name
, *s2
= (*c2
)->name
;
289 return l1
!= l2
? l1
- l2
: strcmp(s1
, s2
);
292 static void add_cmd_list(struct cmdnames
*cmds
, struct cmdnames
*old
)
295 ALLOC_GROW(cmds
->names
, cmds
->cnt
+ old
->cnt
, cmds
->alloc
);
297 for (i
= 0; i
< old
->cnt
; i
++)
298 cmds
->names
[cmds
->cnt
++] = old
->names
[i
];
304 /* An empirically derived magic number */
305 #define SIMILARITY_FLOOR 7
306 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
308 static const char bad_interpreter_advice
[] =
309 N_("'%s' appears to be a git command, but we were not\n"
310 "able to execute it. Maybe git-%s is broken?");
312 const char *help_unknown_cmd(const char *cmd
)
314 int i
, n
, best_similarity
= 0;
315 struct cmdnames main_cmds
, other_cmds
;
317 memset(&main_cmds
, 0, sizeof(main_cmds
));
318 memset(&other_cmds
, 0, sizeof(other_cmds
));
319 memset(&aliases
, 0, sizeof(aliases
));
321 git_config(git_unknown_cmd_config
, NULL
);
323 load_command_list("git-", &main_cmds
, &other_cmds
);
325 add_cmd_list(&main_cmds
, &aliases
);
326 add_cmd_list(&main_cmds
, &other_cmds
);
327 qsort(main_cmds
.names
, main_cmds
.cnt
,
328 sizeof(*main_cmds
.names
), cmdname_compare
);
331 /* This abuses cmdname->len for levenshtein distance */
332 for (i
= 0, n
= 0; i
< main_cmds
.cnt
; i
++) {
333 int cmp
= 0; /* avoid compiler stupidity */
334 const char *candidate
= main_cmds
.names
[i
]->name
;
337 * An exact match means we have the command, but
338 * for some reason exec'ing it gave us ENOENT; probably
339 * it's a bad interpreter in the #! line.
341 if (!strcmp(candidate
, cmd
))
342 die(_(bad_interpreter_advice
), cmd
, cmd
);
344 /* Does the candidate appear in common_cmds list? */
345 while (n
< ARRAY_SIZE(common_cmds
) &&
346 (cmp
= strcmp(common_cmds
[n
].name
, candidate
)) < 0)
348 if ((n
< ARRAY_SIZE(common_cmds
)) && !cmp
) {
349 /* Yes, this is one of the common commands */
350 n
++; /* use the entry from common_cmds[] */
351 if (starts_with(candidate
, cmd
)) {
352 /* Give prefix match a very good score */
353 main_cmds
.names
[i
]->len
= 0;
358 main_cmds
.names
[i
]->len
=
359 levenshtein(cmd
, candidate
, 0, 2, 1, 3) + 1;
362 qsort(main_cmds
.names
, main_cmds
.cnt
,
363 sizeof(*main_cmds
.names
), levenshtein_compare
);
366 die(_("Uh oh. Your system reports no Git commands at all."));
368 /* skip and count prefix matches */
369 for (n
= 0; n
< main_cmds
.cnt
&& !main_cmds
.names
[n
]->len
; n
++)
370 ; /* still counting */
372 if (main_cmds
.cnt
<= n
) {
373 /* prefix matches with everything? that is too ambiguous */
374 best_similarity
= SIMILARITY_FLOOR
+ 1;
376 /* count all the most similar ones */
377 for (best_similarity
= main_cmds
.names
[n
++]->len
;
378 (n
< main_cmds
.cnt
&&
379 best_similarity
== main_cmds
.names
[n
]->len
);
381 ; /* still counting */
383 if (autocorrect
&& n
== 1 && SIMILAR_ENOUGH(best_similarity
)) {
384 const char *assumed
= main_cmds
.names
[0]->name
;
385 main_cmds
.names
[0] = NULL
;
386 clean_cmdnames(&main_cmds
);
388 _("WARNING: You called a Git command named '%s', "
389 "which does not exist.\n"
390 "Continuing under the assumption that you meant '%s'"),
392 if (autocorrect
> 0) {
393 fprintf_ln(stderr
, _("in %0.1f seconds automatically..."),
394 (float)autocorrect
/10.0);
395 sleep_millisec(autocorrect
* 100);
400 fprintf_ln(stderr
, _("git: '%s' is not a git command. See 'git --help'."), cmd
);
402 if (SIMILAR_ENOUGH(best_similarity
)) {
404 Q_("\nDid you mean this?",
405 "\nDid you mean one of these?",
408 for (i
= 0; i
< n
; i
++)
409 fprintf(stderr
, "\t%s\n", main_cmds
.names
[i
]->name
);
415 int cmd_version(int argc
, const char **argv
, const char *prefix
)
418 * The format of this string should be kept stable for compatibility
419 * with external projects that rely on the output of "git version".
421 printf("git version %s\n", git_version_string
);
423 if (!strcmp(*argv
, "--build-options")) {
424 printf("sizeof-long: %d\n", (int)sizeof(long));
425 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
431 struct similar_ref_cb
{
432 const char *base_ref
;
433 struct string_list
*similar_refs
;
436 static int append_similar_ref(const char *refname
, const struct object_id
*oid
,
437 int flags
, void *cb_data
)
439 struct similar_ref_cb
*cb
= (struct similar_ref_cb
*)(cb_data
);
440 char *branch
= strrchr(refname
, '/') + 1;
443 /* A remote branch of the same name is deemed similar */
444 if (skip_prefix(refname
, "refs/remotes/", &remote
) &&
445 !strcmp(branch
, cb
->base_ref
))
446 string_list_append(cb
->similar_refs
, remote
);
450 static struct string_list
guess_refs(const char *ref
)
452 struct similar_ref_cb ref_cb
;
453 struct string_list similar_refs
= STRING_LIST_INIT_NODUP
;
455 ref_cb
.base_ref
= ref
;
456 ref_cb
.similar_refs
= &similar_refs
;
457 for_each_ref(append_similar_ref
, &ref_cb
);
461 void help_unknown_ref(const char *ref
, const char *cmd
, const char *error
)
464 struct string_list suggested_refs
= guess_refs(ref
);
466 fprintf_ln(stderr
, _("%s: %s - %s"), cmd
, ref
, error
);
468 if (suggested_refs
.nr
> 0) {
470 Q_("\nDid you mean this?",
471 "\nDid you mean one of these?",
473 for (i
= 0; i
< suggested_refs
.nr
; i
++)
474 fprintf(stderr
, "\t%s\n", suggested_refs
.items
[i
].string
);
477 string_list_clear(&suggested_refs
, 0);