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)
109 * On Windows there is no executable bit. The file extension
110 * indicates whether it can be run as an executable, and Git
111 * has special-handling to detect scripts and launch them
112 * through the indicated script interpreter. We test for the
113 * file extension first because virus scanners may make
114 * it quite expensive to open many files.
116 if (ends_with(name, ".exe"))
121 * Now that we know it does not have an executable extension,
122 * peek into the file instead.
126 int fd = open(name, O_RDONLY);
127 st.st_mode &= ~S_IXUSR;
129 n = read(fd, buf, 2);
131 /* look for a she-bang */
132 if (!strcmp(buf, "#!"))
133 st.st_mode |= S_IXUSR;
138 return st.st_mode & S_IXUSR;
141 static void list_commands_in_dir(struct cmdnames *cmds,
145 DIR *dir = opendir(path);
147 struct strbuf buf = STRBUF_INIT;
155 strbuf_addf(&buf, "%s/", path);
158 while ((de = readdir(dir)) != NULL) {
162 if (!skip_prefix(de->d_name, prefix, &ent))
165 strbuf_setlen(&buf, len);
166 strbuf_addstr(&buf, de->d_name);
167 if (!is_executable(buf.buf))
170 entlen = strlen(ent);
171 strip_suffix(ent, ".exe", &entlen);
173 add_cmdname(cmds, ent, entlen);
176 strbuf_release(&buf);
179 void load_command_list(const char *prefix,
180 struct cmdnames *main_cmds,
181 struct cmdnames *other_cmds)
183 const char *env_path = getenv("PATH");
184 const char *exec_path = git_exec_path();
187 list_commands_in_dir(main_cmds, exec_path, prefix);
188 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
193 char *paths, *path, *colon;
194 path = paths = xstrdup(env_path);
196 if ((colon = strchr(path, PATH_SEP)))
198 if (!exec_path || strcmp(path, exec_path))
199 list_commands_in_dir(other_cmds, path, prefix);
207 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
210 exclude_cmds(other_cmds, main_cmds);
213 void list_commands(unsigned int colopts,
214 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
216 if (main_cmds->cnt) {
217 const char *exec_path = git_exec_path();
218 printf_ln(_("available git commands in '%s'"), exec_path);
220 pretty_print_cmdnames(main_cmds, colopts);
224 if (other_cmds->cnt) {
225 printf_ln(_("git commands available from elsewhere on your $PATH"));
227 pretty_print_cmdnames(other_cmds, colopts);
232 static int cmd_group_cmp(const void *elem1, const void *elem2)
234 const struct cmdname_help *e1 = elem1;
235 const struct cmdname_help *e2 = elem2;
237 if (e1->group < e2->group)
239 if (e1->group > e2->group)
241 return strcmp(e1->name, e2->name);
244 void list_common_cmds_help(void)
247 int current_grp = -1;
249 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
250 if (longest < strlen(common_cmds[i].name))
251 longest = strlen(common_cmds[i].name);
254 QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
256 puts(_("These are common Git commands used in various situations:"));
258 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
259 if (common_cmds[i].group != current_grp) {
260 printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
261 current_grp = common_cmds[i].group;
264 printf(" %s ", common_cmds[i].name);
265 mput_char(' ', longest - strlen(common_cmds[i].name));
266 puts(_(common_cmds[i].help));
270 int is_in_cmdlist(struct cmdnames *c, const char *s)
273 for (i = 0; i < c->cnt; i++)
274 if (!strcmp(s, c->names[i]->name))
279 static int autocorrect;
280 static struct cmdnames aliases;
282 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
286 if (!strcmp(var, "help.autocorrect"))
287 autocorrect = git_config_int(var,value);
288 /* Also use aliases for command lookup */
289 if (skip_prefix(var, "alias.", &p))
290 add_cmdname(&aliases, p, strlen(p));
292 return git_default_config(var, value, cb);
295 static int levenshtein_compare(const void *p1, const void *p2)
297 const struct cmdname *const *c1 = p1, *const *c2 = p2;
298 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
301 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
304 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
307 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
309 for (i = 0; i < old->cnt; i++)
310 cmds->names[cmds->cnt++] = old->names[i];
316 /* An empirically derived magic number */
317 #define SIMILARITY_FLOOR 7
318 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
320 static const char bad_interpreter_advice[] =
321 N_("'%s' appears to be a git command, but we were not\n"
322 "able to execute it. Maybe git-%s is broken?");
324 const char *help_unknown_cmd(const char *cmd)
326 int i, n, best_similarity = 0;
327 struct cmdnames main_cmds, other_cmds;
329 memset(&main_cmds, 0, sizeof(main_cmds));
330 memset(&other_cmds, 0, sizeof(other_cmds));
331 memset(&aliases, 0, sizeof(aliases));
333 read_early_config(git_unknown_cmd_config, NULL);
335 load_command_list("git-", &main_cmds, &other_cmds);
337 add_cmd_list(&main_cmds, &aliases);
338 add_cmd_list(&main_cmds, &other_cmds);
339 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
342 /* This abuses cmdname->len for levenshtein distance */
343 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
344 int cmp = 0; /* avoid compiler stupidity */
345 const char *candidate = main_cmds.names[i]->name;
348 * An exact match means we have the command, but
349 * for some reason exec'ing it gave us ENOENT; probably
350 * it's a bad interpreter in the #! line.
352 if (!strcmp(candidate, cmd))
353 die(_(bad_interpreter_advice), cmd, cmd);
355 /* Does the candidate appear in common_cmds list? */
356 while (n < ARRAY_SIZE(common_cmds) &&
357 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
359 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
360 /* Yes, this is one of the common commands */
361 n++; /* use the entry from common_cmds[] */
362 if (starts_with(candidate, cmd)) {
363 /* Give prefix match a very good score */
364 main_cmds.names[i]->len = 0;
369 main_cmds.names[i]->len =
370 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
373 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
376 die(_("Uh oh. Your system reports no Git commands at all."));
378 /* skip and count prefix matches */
379 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
380 ; /* still counting */
382 if (main_cmds.cnt <= n) {
383 /* prefix matches with everything? that is too ambiguous */
384 best_similarity = SIMILARITY_FLOOR + 1;
386 /* count all the most similar ones */
387 for (best_similarity = main_cmds.names[n++]->len;
388 (n < main_cmds.cnt &&
389 best_similarity == main_cmds.names[n]->len);
391 ; /* still counting */
393 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
394 const char *assumed = main_cmds.names[0]->name;
395 main_cmds.names[0] = NULL;
396 clean_cmdnames(&main_cmds);
398 _("WARNING: You called a Git command named '%s', "
399 "which does not exist.\n"
400 "Continuing under the assumption that you meant '%s'"),
402 if (autocorrect > 0) {
403 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
404 (float)autocorrect/10.0);
405 sleep_millisec(autocorrect * 100);
410 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
412 if (SIMILAR_ENOUGH(best_similarity)) {
414 Q_("\nDid you mean this?",
415 "\nDid you mean one of these?",
418 for (i = 0; i < n; i++)
419 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
425 int cmd_version(int argc, const char **argv, const char *prefix)
428 * The format of this string should be kept stable for compatibility
429 * with external projects that rely on the output of "git version".
431 printf("git version %s\n", git_version_string);
433 if (!strcmp(*argv, "--build-options")) {
434 printf("sizeof-long: %d\n", (int)sizeof(long));
435 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
441 struct similar_ref_cb {
442 const char *base_ref;
443 struct string_list *similar_refs;
446 static int append_similar_ref(const char *refname, const struct object_id *oid,
447 int flags, void *cb_data)
449 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
450 char *branch = strrchr(refname, '/') + 1;
453 /* A remote branch of the same name is deemed similar */
454 if (skip_prefix(refname, "refs/remotes/", &remote) &&
455 !strcmp(branch, cb->base_ref))
456 string_list_append(cb->similar_refs, remote);
460 static struct string_list guess_refs(const char *ref)
462 struct similar_ref_cb ref_cb;
463 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
465 ref_cb.base_ref = ref;
466 ref_cb.similar_refs = &similar_refs;
467 for_each_ref(append_similar_ref, &ref_cb);
471 void help_unknown_ref(const char *ref, const char *cmd, const char *error)
474 struct string_list suggested_refs = guess_refs(ref);
476 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
478 if (suggested_refs.nr > 0) {
480 Q_("\nDid you mean this?",
481 "\nDid you mean one of these?",
483 for (i = 0; i < suggested_refs.nr; i++)
484 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
487 string_list_clear(&suggested_refs, 0);