4 #include "levenshtein.h"
6 #include "common-cmds.h"
7 #include "string-list.h"
11 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
13 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
16 memcpy(ent->name, name, len);
19 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
20 cmds->names[cmds->cnt++] = ent;
23 static void clean_cmdnames(struct cmdnames *cmds)
26 for (i = 0; i < cmds->cnt; ++i)
33 static int cmdname_compare(const void *a_, const void *b_)
35 struct cmdname *a = *(struct cmdname **)a_;
36 struct cmdname *b = *(struct cmdname **)b_;
37 return strcmp(a->name, b->name);
40 static void uniq(struct cmdnames *cmds)
47 for (i = j = 1; i < cmds->cnt; i++) {
48 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
51 cmds->names[j++] = cmds->names[i];
57 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
63 while (ci < cmds->cnt && ei < excludes->cnt) {
64 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
66 cmds->names[cj++] = cmds->names[ci++];
69 free(cmds->names[ci++]);
74 while (ci < cmds->cnt)
75 cmds->names[cj++] = cmds->names[ci++];
80 static void pretty_print_string_list(struct cmdnames *cmds,
83 struct string_list list = STRING_LIST_INIT_NODUP;
84 struct column_options copts;
87 for (i = 0; i < cmds->cnt; i++)
88 string_list_append(&list, cmds->names[i]->name);
90 * always enable column display, we only consult column.*
91 * about layout strategy and stuff
93 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
94 memset(&copts, 0, sizeof(copts));
97 print_columns(&list, colopts, &copts);
98 string_list_clear(&list, 0);
101 static int is_executable(const char *name)
105 if (stat(name, &st) || /* stat, not lstat */
106 !S_ISREG(st.st_mode))
109 #if defined(WIN32) || defined(__CYGWIN__)
110 #if defined(__CYGWIN__)
111 if ((st.st_mode & S_IXUSR) == 0)
113 { /* cannot trust the executable bit, peek into the file instead */
116 int fd = open(name, O_RDONLY);
117 st.st_mode &= ~S_IXUSR;
119 n = read(fd, buf, 2);
121 /* DOS executables start with "MZ" */
122 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
123 st.st_mode |= S_IXUSR;
128 return st.st_mode & S_IXUSR;
131 static void list_commands_in_dir(struct cmdnames *cmds,
136 DIR *dir = opendir(path);
138 struct strbuf buf = STRBUF_INIT;
145 prefix_len = strlen(prefix);
147 strbuf_addf(&buf, "%s/", path);
150 while ((de = readdir(dir)) != NULL) {
153 if (prefixcmp(de->d_name, prefix))
156 strbuf_setlen(&buf, len);
157 strbuf_addstr(&buf, de->d_name);
158 if (!is_executable(buf.buf))
161 entlen = strlen(de->d_name) - prefix_len;
162 if (has_extension(de->d_name, ".exe"))
165 add_cmdname(cmds, de->d_name + prefix_len, entlen);
168 strbuf_release(&buf);
171 void load_command_list(const char *prefix,
172 struct cmdnames *main_cmds,
173 struct cmdnames *other_cmds)
175 const char *env_path = getenv("PATH");
176 const char *exec_path = git_exec_path();
179 list_commands_in_dir(main_cmds, exec_path, prefix);
180 qsort(main_cmds->names, main_cmds->cnt,
181 sizeof(*main_cmds->names), cmdname_compare);
186 char *paths, *path, *colon;
187 path = paths = xstrdup(env_path);
189 if ((colon = strchr(path, PATH_SEP)))
191 if (!exec_path || strcmp(path, exec_path))
192 list_commands_in_dir(other_cmds, path, prefix);
200 qsort(other_cmds->names, other_cmds->cnt,
201 sizeof(*other_cmds->names), cmdname_compare);
204 exclude_cmds(other_cmds, main_cmds);
207 void list_commands(unsigned int colopts,
208 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
210 if (main_cmds->cnt) {
211 const char *exec_path = git_exec_path();
212 printf_ln(_("available git commands in '%s'"), exec_path);
214 pretty_print_string_list(main_cmds, colopts);
218 if (other_cmds->cnt) {
219 printf_ln(_("git commands available from elsewhere on your $PATH"));
221 pretty_print_string_list(other_cmds, colopts);
226 void list_common_cmds_help(void)
230 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
231 if (longest < strlen(common_cmds[i].name))
232 longest = strlen(common_cmds[i].name);
235 puts(_("The most commonly used git commands are:"));
236 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
237 printf(" %s ", common_cmds[i].name);
238 mput_char(' ', longest - strlen(common_cmds[i].name));
239 puts(_(common_cmds[i].help));
243 int is_in_cmdlist(struct cmdnames *c, const char *s)
246 for (i = 0; i < c->cnt; i++)
247 if (!strcmp(s, c->names[i]->name))
252 static int autocorrect;
253 static struct cmdnames aliases;
255 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
257 if (!strcmp(var, "help.autocorrect"))
258 autocorrect = git_config_int(var,value);
259 /* Also use aliases for command lookup */
260 if (!prefixcmp(var, "alias."))
261 add_cmdname(&aliases, var + 6, strlen(var + 6));
263 return git_default_config(var, value, cb);
266 static int levenshtein_compare(const void *p1, const void *p2)
268 const struct cmdname *const *c1 = p1, *const *c2 = p2;
269 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
272 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
275 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
278 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
280 for (i = 0; i < old->cnt; i++)
281 cmds->names[cmds->cnt++] = old->names[i];
287 /* An empirically derived magic number */
288 #define SIMILARITY_FLOOR 7
289 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
291 static const char bad_interpreter_advice[] =
292 N_("'%s' appears to be a git command, but we were not\n"
293 "able to execute it. Maybe git-%s is broken?");
295 const char *help_unknown_cmd(const char *cmd)
297 int i, n, best_similarity = 0;
298 struct cmdnames main_cmds, other_cmds;
300 memset(&main_cmds, 0, sizeof(main_cmds));
301 memset(&other_cmds, 0, sizeof(other_cmds));
302 memset(&aliases, 0, sizeof(aliases));
304 git_config(git_unknown_cmd_config, NULL);
306 load_command_list("git-", &main_cmds, &other_cmds);
308 add_cmd_list(&main_cmds, &aliases);
309 add_cmd_list(&main_cmds, &other_cmds);
310 qsort(main_cmds.names, main_cmds.cnt,
311 sizeof(main_cmds.names), cmdname_compare);
314 /* This abuses cmdname->len for levenshtein distance */
315 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
316 int cmp = 0; /* avoid compiler stupidity */
317 const char *candidate = main_cmds.names[i]->name;
320 * An exact match means we have the command, but
321 * for some reason exec'ing it gave us ENOENT; probably
322 * it's a bad interpreter in the #! line.
324 if (!strcmp(candidate, cmd))
325 die(_(bad_interpreter_advice), cmd, cmd);
327 /* Does the candidate appear in common_cmds list? */
328 while (n < ARRAY_SIZE(common_cmds) &&
329 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
331 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
332 /* Yes, this is one of the common commands */
333 n++; /* use the entry from common_cmds[] */
334 if (!prefixcmp(candidate, cmd)) {
335 /* Give prefix match a very good score */
336 main_cmds.names[i]->len = 0;
341 main_cmds.names[i]->len =
342 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
345 qsort(main_cmds.names, main_cmds.cnt,
346 sizeof(*main_cmds.names), levenshtein_compare);
349 die(_("Uh oh. Your system reports no Git commands at all."));
351 /* skip and count prefix matches */
352 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
353 ; /* still counting */
355 if (main_cmds.cnt <= n) {
356 /* prefix matches with everything? that is too ambiguous */
357 best_similarity = SIMILARITY_FLOOR + 1;
359 /* count all the most similar ones */
360 for (best_similarity = main_cmds.names[n++]->len;
361 (n < main_cmds.cnt &&
362 best_similarity == main_cmds.names[n]->len);
364 ; /* still counting */
366 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
367 const char *assumed = main_cmds.names[0]->name;
368 main_cmds.names[0] = NULL;
369 clean_cmdnames(&main_cmds);
371 _("WARNING: You called a Git command named '%s', "
372 "which does not exist.\n"
373 "Continuing under the assumption that you meant '%s'"),
375 if (autocorrect > 0) {
376 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
377 (float)autocorrect/10.0);
378 poll(NULL, 0, autocorrect * 100);
383 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
385 if (SIMILAR_ENOUGH(best_similarity)) {
387 Q_("\nDid you mean this?",
388 "\nDid you mean one of these?",
391 for (i = 0; i < n; i++)
392 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
398 int cmd_version(int argc, const char **argv, const char *prefix)
401 * The format of this string should be kept stable for compatibility
402 * with external projects that rely on the output of "git version".
404 printf("git version %s\n", git_version_string);