t4210: skip command-line encoding tests on mingw
[git/mingw/4msysgit.git] / help.c
blob45938e85ac931ac49aea235f88b64d6961658ed1
1 #include "cache.h"
2 #include "builtin.h"
3 #include "exec_cmd.h"
4 #include "levenshtein.h"
5 #include "help.h"
6 #include "common-cmds.h"
7 #include "string-list.h"
8 #include "column.h"
9 #include "version.h"
11 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
13 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
15 ent->len = len;
16 memcpy(ent->name, name, len);
17 ent->name[len] = 0;
19 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
20 cmds->names[cmds->cnt++] = ent;
23 static void clean_cmdnames(struct cmdnames *cmds)
25 int i;
26 for (i = 0; i < cmds->cnt; ++i)
27 free(cmds->names[i]);
28 free(cmds->names);
29 cmds->cnt = 0;
30 cmds->alloc = 0;
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)
42 int i, j;
44 if (!cmds->cnt)
45 return;
47 for (i = j = 1; i < cmds->cnt; i++) {
48 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
49 free(cmds->names[i]);
50 else
51 cmds->names[j++] = cmds->names[i];
54 cmds->cnt = j;
57 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
59 int ci, cj, ei;
60 int cmp;
62 ci = cj = ei = 0;
63 while (ci < cmds->cnt && ei < excludes->cnt) {
64 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
65 if (cmp < 0)
66 cmds->names[cj++] = cmds->names[ci++];
67 else if (cmp == 0) {
68 ei++;
69 free(cmds->names[ci++]);
70 } else if (cmp > 0)
71 ei++;
74 while (ci < cmds->cnt)
75 cmds->names[cj++] = cmds->names[ci++];
77 cmds->cnt = cj;
80 static void pretty_print_string_list(struct cmdnames *cmds,
81 unsigned int colopts)
83 struct string_list list = STRING_LIST_INIT_NODUP;
84 struct column_options copts;
85 int i;
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));
95 copts.indent = " ";
96 copts.padding = 2;
97 print_columns(&list, colopts, &copts);
98 string_list_clear(&list, 0);
101 static int is_executable(const char *name)
103 struct stat st;
105 if (stat(name, &st) || /* stat, not lstat */
106 !S_ISREG(st.st_mode))
107 return 0;
109 #if defined(WIN32) || defined(__CYGWIN__)
110 /* On Windows we cannot use the executable bit. The executable
111 * state is determined by extension only. We do this first
112 * because with virus scanners opening an executeable for
113 * reading is potentially expensive.
115 if (has_extension(name, ".exe"))
116 return S_IXUSR;
118 #if defined(__CYGWIN__)
119 if ((st.st_mode & S_IXUSR) == 0)
120 #endif
121 { /* now that we know it does not have an executable extension,
122 peek into the file instead */
123 char buf[3] = { 0 };
124 int n;
125 int fd = open(name, O_RDONLY);
126 st.st_mode &= ~S_IXUSR;
127 if (fd >= 0) {
128 n = read(fd, buf, 2);
129 if (n == 2)
130 /* look for a she-bang */
131 if (!strcmp(buf, "#!"))
132 st.st_mode |= S_IXUSR;
133 close(fd);
136 #endif
137 return st.st_mode & S_IXUSR;
140 static void list_commands_in_dir(struct cmdnames *cmds,
141 const char *path,
142 const char *prefix)
144 int prefix_len;
145 DIR *dir = opendir(path);
146 struct dirent *de;
147 struct strbuf buf = STRBUF_INIT;
148 int len;
150 if (!dir)
151 return;
152 if (!prefix)
153 prefix = "git-";
154 prefix_len = strlen(prefix);
156 strbuf_addf(&buf, "%s/", path);
157 len = buf.len;
159 while ((de = readdir(dir)) != NULL) {
160 int entlen;
162 if (prefixcmp(de->d_name, prefix))
163 continue;
165 strbuf_setlen(&buf, len);
166 strbuf_addstr(&buf, de->d_name);
167 if (!is_executable(buf.buf))
168 continue;
170 entlen = strlen(de->d_name) - prefix_len;
171 if (has_extension(de->d_name, ".exe"))
172 entlen -= 4;
174 add_cmdname(cmds, de->d_name + prefix_len, entlen);
176 closedir(dir);
177 strbuf_release(&buf);
180 void load_command_list(const char *prefix,
181 struct cmdnames *main_cmds,
182 struct cmdnames *other_cmds)
184 const char *env_path = getenv("PATH");
185 const char *exec_path = git_exec_path();
187 if (exec_path) {
188 list_commands_in_dir(main_cmds, exec_path, prefix);
189 qsort(main_cmds->names, main_cmds->cnt,
190 sizeof(*main_cmds->names), cmdname_compare);
191 uniq(main_cmds);
194 if (env_path) {
195 char *paths, *path, *colon;
196 path = paths = xstrdup(env_path);
197 while (1) {
198 if ((colon = strchr(path, PATH_SEP)))
199 *colon = 0;
200 if (!exec_path || strcmp(path, exec_path))
201 list_commands_in_dir(other_cmds, path, prefix);
203 if (!colon)
204 break;
205 path = colon + 1;
207 free(paths);
209 qsort(other_cmds->names, other_cmds->cnt,
210 sizeof(*other_cmds->names), cmdname_compare);
211 uniq(other_cmds);
213 exclude_cmds(other_cmds, main_cmds);
216 void list_commands(unsigned int colopts,
217 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
219 if (main_cmds->cnt) {
220 const char *exec_path = git_exec_path();
221 printf_ln(_("available git commands in '%s'"), exec_path);
222 putchar('\n');
223 pretty_print_string_list(main_cmds, colopts);
224 putchar('\n');
227 if (other_cmds->cnt) {
228 printf_ln(_("git commands available from elsewhere on your $PATH"));
229 putchar('\n');
230 pretty_print_string_list(other_cmds, colopts);
231 putchar('\n');
235 void list_common_cmds_help(void)
237 int i, longest = 0;
239 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
240 if (longest < strlen(common_cmds[i].name))
241 longest = strlen(common_cmds[i].name);
244 puts(_("The most commonly used git commands are:"));
245 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
246 printf(" %s ", common_cmds[i].name);
247 mput_char(' ', longest - strlen(common_cmds[i].name));
248 puts(_(common_cmds[i].help));
252 int is_in_cmdlist(struct cmdnames *c, const char *s)
254 int i;
255 for (i = 0; i < c->cnt; i++)
256 if (!strcmp(s, c->names[i]->name))
257 return 1;
258 return 0;
261 static int autocorrect;
262 static struct cmdnames aliases;
264 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
266 if (!strcmp(var, "help.autocorrect"))
267 autocorrect = git_config_int(var,value);
268 /* Also use aliases for command lookup */
269 if (!prefixcmp(var, "alias."))
270 add_cmdname(&aliases, var + 6, strlen(var + 6));
272 return git_default_config(var, value, cb);
275 static int levenshtein_compare(const void *p1, const void *p2)
277 const struct cmdname *const *c1 = p1, *const *c2 = p2;
278 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
279 int l1 = (*c1)->len;
280 int l2 = (*c2)->len;
281 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
284 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
286 int i;
287 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
289 for (i = 0; i < old->cnt; i++)
290 cmds->names[cmds->cnt++] = old->names[i];
291 free(old->names);
292 old->cnt = 0;
293 old->names = NULL;
296 /* An empirically derived magic number */
297 #define SIMILARITY_FLOOR 7
298 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
300 static const char bad_interpreter_advice[] =
301 N_("'%s' appears to be a git command, but we were not\n"
302 "able to execute it. Maybe git-%s is broken?");
304 const char *help_unknown_cmd(const char *cmd)
306 int i, n, best_similarity = 0;
307 struct cmdnames main_cmds, other_cmds;
309 memset(&main_cmds, 0, sizeof(main_cmds));
310 memset(&other_cmds, 0, sizeof(other_cmds));
311 memset(&aliases, 0, sizeof(aliases));
313 git_config(git_unknown_cmd_config, NULL);
315 load_command_list("git-", &main_cmds, &other_cmds);
317 add_cmd_list(&main_cmds, &aliases);
318 add_cmd_list(&main_cmds, &other_cmds);
319 qsort(main_cmds.names, main_cmds.cnt,
320 sizeof(main_cmds.names), cmdname_compare);
321 uniq(&main_cmds);
323 /* This abuses cmdname->len for levenshtein distance */
324 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
325 int cmp = 0; /* avoid compiler stupidity */
326 const char *candidate = main_cmds.names[i]->name;
329 * An exact match means we have the command, but
330 * for some reason exec'ing it gave us ENOENT; probably
331 * it's a bad interpreter in the #! line.
333 if (!strcmp(candidate, cmd))
334 die(_(bad_interpreter_advice), cmd, cmd);
336 /* Does the candidate appear in common_cmds list? */
337 while (n < ARRAY_SIZE(common_cmds) &&
338 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
339 n++;
340 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
341 /* Yes, this is one of the common commands */
342 n++; /* use the entry from common_cmds[] */
343 if (!prefixcmp(candidate, cmd)) {
344 /* Give prefix match a very good score */
345 main_cmds.names[i]->len = 0;
346 continue;
350 main_cmds.names[i]->len =
351 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
354 qsort(main_cmds.names, main_cmds.cnt,
355 sizeof(*main_cmds.names), levenshtein_compare);
357 if (!main_cmds.cnt)
358 die(_("Uh oh. Your system reports no Git commands at all."));
360 /* skip and count prefix matches */
361 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
362 ; /* still counting */
364 if (main_cmds.cnt <= n) {
365 /* prefix matches with everything? that is too ambiguous */
366 best_similarity = SIMILARITY_FLOOR + 1;
367 } else {
368 /* count all the most similar ones */
369 for (best_similarity = main_cmds.names[n++]->len;
370 (n < main_cmds.cnt &&
371 best_similarity == main_cmds.names[n]->len);
372 n++)
373 ; /* still counting */
375 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
376 const char *assumed = main_cmds.names[0]->name;
377 main_cmds.names[0] = NULL;
378 clean_cmdnames(&main_cmds);
379 fprintf_ln(stderr,
380 _("WARNING: You called a Git command named '%s', "
381 "which does not exist.\n"
382 "Continuing under the assumption that you meant '%s'"),
383 cmd, assumed);
384 if (autocorrect > 0) {
385 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
386 (float)autocorrect/10.0);
387 poll(NULL, 0, autocorrect * 100);
389 return assumed;
392 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
394 if (SIMILAR_ENOUGH(best_similarity)) {
395 fprintf_ln(stderr,
396 Q_("\nDid you mean this?",
397 "\nDid you mean one of these?",
398 n));
400 for (i = 0; i < n; i++)
401 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
404 exit(1);
407 int cmd_version(int argc, const char **argv, const char *prefix)
410 * The format of this string should be kept stable for compatibility
411 * with external projects that rely on the output of "git version".
413 printf("git version %s\n", git_version_string);
414 return 0;