gitk: Use an external icon file on Windows
[git/dscho.git] / help.c
blobb41fa21f9396e8dd2159bdce132e2d48ca6741bd
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[i-1]->name))
49 cmds->names[j++] = cmds->names[i];
51 cmds->cnt = j;
54 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
56 int ci, cj, ei;
57 int cmp;
59 ci = cj = ei = 0;
60 while (ci < cmds->cnt && ei < excludes->cnt) {
61 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
62 if (cmp < 0)
63 cmds->names[cj++] = cmds->names[ci++];
64 else if (cmp == 0)
65 ci++, ei++;
66 else if (cmp > 0)
67 ei++;
70 while (ci < cmds->cnt)
71 cmds->names[cj++] = cmds->names[ci++];
73 cmds->cnt = cj;
76 static void pretty_print_string_list(struct cmdnames *cmds,
77 unsigned int colopts)
79 struct string_list list = STRING_LIST_INIT_NODUP;
80 struct column_options copts;
81 int i;
83 for (i = 0; i < cmds->cnt; i++)
84 string_list_append(&list, cmds->names[i]->name);
86 * always enable column display, we only consult column.*
87 * about layout strategy and stuff
89 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
90 memset(&copts, 0, sizeof(copts));
91 copts.indent = " ";
92 copts.padding = 2;
93 print_columns(&list, colopts, &copts);
94 string_list_clear(&list, 0);
97 static int is_executable(const char *name)
99 struct stat st;
101 if (stat(name, &st) || /* stat, not lstat */
102 !S_ISREG(st.st_mode))
103 return 0;
105 #if defined(WIN32) || defined(__CYGWIN__)
106 /* On Windows we cannot use the executable bit. The executable
107 * state is determined by extension only. We do this first
108 * because with virus scanners opening an executeable for
109 * reading is potentially expensive.
111 if (has_extension(name, ".exe"))
112 return S_IXUSR;
114 #if defined(__CYGWIN__)
115 if ((st.st_mode & S_IXUSR) == 0)
116 #endif
117 { /* now that we know it does not have an executable extension,
118 peek into the file instead */
119 char buf[3] = { 0 };
120 int n;
121 int fd = open(name, O_RDONLY);
122 st.st_mode &= ~S_IXUSR;
123 if (fd >= 0) {
124 n = read(fd, buf, 2);
125 if (n == 2)
126 /* look for a she-bang */
127 if (!strcmp(buf, "#!"))
128 st.st_mode |= S_IXUSR;
129 close(fd);
132 #endif
133 return st.st_mode & S_IXUSR;
136 static void list_commands_in_dir(struct cmdnames *cmds,
137 const char *path,
138 const char *prefix)
140 int prefix_len;
141 DIR *dir = opendir(path);
142 struct dirent *de;
143 struct strbuf buf = STRBUF_INIT;
144 int len;
146 if (!dir)
147 return;
148 if (!prefix)
149 prefix = "git-";
150 prefix_len = strlen(prefix);
152 strbuf_addf(&buf, "%s/", path);
153 len = buf.len;
155 while ((de = readdir(dir)) != NULL) {
156 int entlen;
158 if (prefixcmp(de->d_name, prefix))
159 continue;
161 strbuf_setlen(&buf, len);
162 strbuf_addstr(&buf, de->d_name);
163 if (!is_executable(buf.buf))
164 continue;
166 entlen = strlen(de->d_name) - prefix_len;
167 if (has_extension(de->d_name, ".exe"))
168 entlen -= 4;
170 add_cmdname(cmds, de->d_name + prefix_len, entlen);
172 closedir(dir);
173 strbuf_release(&buf);
176 void load_command_list(const char *prefix,
177 struct cmdnames *main_cmds,
178 struct cmdnames *other_cmds)
180 const char *env_path = getenv("PATH");
181 const char *exec_path = git_exec_path();
183 if (exec_path) {
184 list_commands_in_dir(main_cmds, exec_path, prefix);
185 qsort(main_cmds->names, main_cmds->cnt,
186 sizeof(*main_cmds->names), cmdname_compare);
187 uniq(main_cmds);
190 if (env_path) {
191 char *paths, *path, *colon;
192 path = paths = xstrdup(env_path);
193 while (1) {
194 if ((colon = strchr(path, PATH_SEP)))
195 *colon = 0;
196 if (!exec_path || strcmp(path, exec_path))
197 list_commands_in_dir(other_cmds, path, prefix);
199 if (!colon)
200 break;
201 path = colon + 1;
203 free(paths);
205 qsort(other_cmds->names, other_cmds->cnt,
206 sizeof(*other_cmds->names), cmdname_compare);
207 uniq(other_cmds);
209 exclude_cmds(other_cmds, main_cmds);
212 void list_commands(unsigned int colopts,
213 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
215 if (main_cmds->cnt) {
216 const char *exec_path = git_exec_path();
217 printf_ln(_("available git commands in '%s'"), exec_path);
218 putchar('\n');
219 pretty_print_string_list(main_cmds, colopts);
220 putchar('\n');
223 if (other_cmds->cnt) {
224 printf_ln(_("git commands available from elsewhere on your $PATH"));
225 putchar('\n');
226 pretty_print_string_list(other_cmds, colopts);
227 putchar('\n');
231 int is_in_cmdlist(struct cmdnames *c, const char *s)
233 int i;
234 for (i = 0; i < c->cnt; i++)
235 if (!strcmp(s, c->names[i]->name))
236 return 1;
237 return 0;
240 static int autocorrect;
241 static struct cmdnames aliases;
243 static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
245 if (!strcmp(var, "help.autocorrect"))
246 autocorrect = git_config_int(var,value);
247 /* Also use aliases for command lookup */
248 if (!prefixcmp(var, "alias."))
249 add_cmdname(&aliases, var + 6, strlen(var + 6));
251 return git_default_config(var, value, cb);
254 static int levenshtein_compare(const void *p1, const void *p2)
256 const struct cmdname *const *c1 = p1, *const *c2 = p2;
257 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
258 int l1 = (*c1)->len;
259 int l2 = (*c2)->len;
260 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
263 static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
265 int i;
266 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
268 for (i = 0; i < old->cnt; i++)
269 cmds->names[cmds->cnt++] = old->names[i];
270 free(old->names);
271 old->cnt = 0;
272 old->names = NULL;
275 /* An empirically derived magic number */
276 #define SIMILARITY_FLOOR 7
277 #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
279 static const char bad_interpreter_advice[] =
280 N_("'%s' appears to be a git command, but we were not\n"
281 "able to execute it. Maybe git-%s is broken?");
283 const char *help_unknown_cmd(const char *cmd)
285 int i, n, best_similarity = 0;
286 struct cmdnames main_cmds, other_cmds;
288 memset(&main_cmds, 0, sizeof(main_cmds));
289 memset(&other_cmds, 0, sizeof(other_cmds));
290 memset(&aliases, 0, sizeof(aliases));
292 git_config(git_unknown_cmd_config, NULL);
294 load_command_list("git-", &main_cmds, &other_cmds);
296 add_cmd_list(&main_cmds, &aliases);
297 add_cmd_list(&main_cmds, &other_cmds);
298 qsort(main_cmds.names, main_cmds.cnt,
299 sizeof(main_cmds.names), cmdname_compare);
300 uniq(&main_cmds);
302 /* This abuses cmdname->len for levenshtein distance */
303 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
304 int cmp = 0; /* avoid compiler stupidity */
305 const char *candidate = main_cmds.names[i]->name;
308 * An exact match means we have the command, but
309 * for some reason exec'ing it gave us ENOENT; probably
310 * it's a bad interpreter in the #! line.
312 if (!strcmp(candidate, cmd))
313 die(_(bad_interpreter_advice), cmd, cmd);
315 /* Does the candidate appear in common_cmds list? */
316 while (n < ARRAY_SIZE(common_cmds) &&
317 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
318 n++;
319 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
320 /* Yes, this is one of the common commands */
321 n++; /* use the entry from common_cmds[] */
322 if (!prefixcmp(candidate, cmd)) {
323 /* Give prefix match a very good score */
324 main_cmds.names[i]->len = 0;
325 continue;
329 main_cmds.names[i]->len =
330 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
333 qsort(main_cmds.names, main_cmds.cnt,
334 sizeof(*main_cmds.names), levenshtein_compare);
336 if (!main_cmds.cnt)
337 die(_("Uh oh. Your system reports no Git commands at all."));
339 /* skip and count prefix matches */
340 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
341 ; /* still counting */
343 if (main_cmds.cnt <= n) {
344 /* prefix matches with everything? that is too ambiguous */
345 best_similarity = SIMILARITY_FLOOR + 1;
346 } else {
347 /* count all the most similar ones */
348 for (best_similarity = main_cmds.names[n++]->len;
349 (n < main_cmds.cnt &&
350 best_similarity == main_cmds.names[n]->len);
351 n++)
352 ; /* still counting */
354 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
355 const char *assumed = main_cmds.names[0]->name;
356 main_cmds.names[0] = NULL;
357 clean_cmdnames(&main_cmds);
358 fprintf_ln(stderr,
359 _("WARNING: You called a Git command named '%s', "
360 "which does not exist.\n"
361 "Continuing under the assumption that you meant '%s'"),
362 cmd, assumed);
363 if (autocorrect > 0) {
364 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
365 (float)autocorrect/10.0);
366 poll(NULL, 0, autocorrect * 100);
368 return assumed;
371 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
373 if (SIMILAR_ENOUGH(best_similarity)) {
374 fprintf_ln(stderr,
375 Q_("\nDid you mean this?",
376 "\nDid you mean one of these?",
377 n));
379 for (i = 0; i < n; i++)
380 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
383 exit(1);
386 int cmd_version(int argc, const char **argv, const char *prefix)
388 printf("git version %s\n", git_version_string);
389 return 0;