]> git.ipfire.org Git - thirdparty/git.git/blame - help.c
free_ref_entry(): new function
[thirdparty/git.git] / help.c
CommitLineData
70827b15
LT
1#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
8af84dad 4#include "levenshtein.h"
940208a7 5#include "help.h"
6612b9e4 6#include "common-cmds.h"
64949984 7
940208a7 8void add_cmdname(struct cmdnames *cmds, const char *name, int len)
70827b15 9{
940208a7 10 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
1eb05690 11
70827b15
LT
12 ent->len = len;
13 memcpy(ent->name, name, len);
14 ent->name[len] = 0;
1eb05690
SP
15
16 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
17 cmds->names[cmds->cnt++] = ent;
70827b15
LT
18}
19
8af84dad
JS
20static void clean_cmdnames(struct cmdnames *cmds)
21{
22 int i;
23 for (i = 0; i < cmds->cnt; ++i)
24 free(cmds->names[i]);
25 free(cmds->names);
26 cmds->cnt = 0;
27 cmds->alloc = 0;
28}
29
70827b15
LT
30static int cmdname_compare(const void *a_, const void *b_)
31{
32 struct cmdname *a = *(struct cmdname **)a_;
33 struct cmdname *b = *(struct cmdname **)b_;
34 return strcmp(a->name, b->name);
35}
36
1eb05690
SP
37static void uniq(struct cmdnames *cmds)
38{
39 int i, j;
40
41 if (!cmds->cnt)
42 return;
43
44 for (i = j = 1; i < cmds->cnt; i++)
45 if (strcmp(cmds->names[i]->name, cmds->names[i-1]->name))
46 cmds->names[j++] = cmds->names[i];
47
48 cmds->cnt = j;
49}
50
940208a7 51void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
f3fa1838 52{
1eb05690
SP
53 int ci, cj, ei;
54 int cmp;
55
56 ci = cj = ei = 0;
57 while (ci < cmds->cnt && ei < excludes->cnt) {
58 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
59 if (cmp < 0)
60 cmds->names[cj++] = cmds->names[ci++];
61 else if (cmp == 0)
62 ci++, ei++;
63 else if (cmp > 0)
64 ei++;
65 }
66
67 while (ci < cmds->cnt)
68 cmds->names[cj++] = cmds->names[ci++];
69
70 cmds->cnt = cj;
71}
72
73static void pretty_print_string_list(struct cmdnames *cmds, int longest)
70827b15
LT
74{
75 int cols = 1, rows;
76 int space = longest + 1; /* min 1 SP between words */
77 int max_cols = term_columns() - 1; /* don't print *on* the edge */
78 int i, j;
79
80 if (space < max_cols)
81 cols = max_cols / space;
98cb6f30 82 rows = DIV_ROUND_UP(cmds->cnt, cols);
70827b15
LT
83
84 for (i = 0; i < rows; i++) {
85 printf(" ");
86
87 for (j = 0; j < cols; j++) {
88 int n = j * rows + i;
89 int size = space;
1eb05690 90 if (n >= cmds->cnt)
70827b15 91 break;
1eb05690 92 if (j == cols-1 || n + rows >= cmds->cnt)
70827b15 93 size = 1;
1eb05690 94 printf("%-*s", size, cmds->names[n]->name);
70827b15
LT
95 }
96 putchar('\n');
97 }
98}
99
cc3b7a97
JS
100static int is_executable(const char *name)
101{
102 struct stat st;
103
104 if (stat(name, &st) || /* stat, not lstat */
105 !S_ISREG(st.st_mode))
106 return 0;
107
b8a97333
RJ
108#if defined(WIN32) || defined(__CYGWIN__)
109#if defined(__CYGWIN__)
110if ((st.st_mode & S_IXUSR) == 0)
111#endif
0d30ad71 112{ /* cannot trust the executable bit, peek into the file instead */
cc3b7a97
JS
113 char buf[3] = { 0 };
114 int n;
115 int fd = open(name, O_RDONLY);
116 st.st_mode &= ~S_IXUSR;
117 if (fd >= 0) {
118 n = read(fd, buf, 2);
119 if (n == 2)
120 /* DOS executables start with "MZ" */
121 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
122 st.st_mode |= S_IXUSR;
123 close(fd);
124 }
0d30ad71 125}
cc3b7a97
JS
126#endif
127 return st.st_mode & S_IXUSR;
128}
129
e321180e 130static void list_commands_in_dir(struct cmdnames *cmds,
940208a7
MV
131 const char *path,
132 const char *prefix)
70827b15 133{
940208a7 134 int prefix_len;
1eb05690 135 DIR *dir = opendir(path);
70827b15 136 struct dirent *de;
f5d600e2
JS
137 struct strbuf buf = STRBUF_INIT;
138 int len;
70827b15 139
f5d600e2 140 if (!dir)
e321180e 141 return;
940208a7
MV
142 if (!prefix)
143 prefix = "git-";
144 prefix_len = strlen(prefix);
70827b15 145
f5d600e2
JS
146 strbuf_addf(&buf, "%s/", path);
147 len = buf.len;
148
70827b15 149 while ((de = readdir(dir)) != NULL) {
70827b15
LT
150 int entlen;
151
edb6ddc5 152 if (prefixcmp(de->d_name, prefix))
70827b15 153 continue;
0966003c 154
f5d600e2
JS
155 strbuf_setlen(&buf, len);
156 strbuf_addstr(&buf, de->d_name);
157 if (!is_executable(buf.buf))
70827b15
LT
158 continue;
159
edb6ddc5 160 entlen = strlen(de->d_name) - prefix_len;
5bb1cda5 161 if (has_extension(de->d_name, ".exe"))
70827b15
LT
162 entlen -= 4;
163
1eb05690 164 add_cmdname(cmds, de->d_name + prefix_len, entlen);
70827b15
LT
165 }
166 closedir(dir);
f5d600e2 167 strbuf_release(&buf);
1eb05690
SP
168}
169
e321180e 170void load_command_list(const char *prefix,
940208a7
MV
171 struct cmdnames *main_cmds,
172 struct cmdnames *other_cmds)
1eb05690 173{
1eb05690 174 const char *env_path = getenv("PATH");
1eb05690
SP
175 const char *exec_path = git_exec_path();
176
1f08e5ce 177 if (exec_path) {
e321180e 178 list_commands_in_dir(main_cmds, exec_path, prefix);
1f08e5ce
AR
179 qsort(main_cmds->names, main_cmds->cnt,
180 sizeof(*main_cmds->names), cmdname_compare);
181 uniq(main_cmds);
1eb05690
SP
182 }
183
1f08e5ce
AR
184 if (env_path) {
185 char *paths, *path, *colon;
186 path = paths = xstrdup(env_path);
187 while (1) {
188 if ((colon = strchr(path, PATH_SEP)))
189 *colon = 0;
746c221a
PB
190 if (!exec_path || strcmp(path, exec_path))
191 list_commands_in_dir(other_cmds, path, prefix);
1eb05690 192
1f08e5ce
AR
193 if (!colon)
194 break;
195 path = colon + 1;
196 }
197 free(paths);
1eb05690 198
1f08e5ce
AR
199 qsort(other_cmds->names, other_cmds->cnt,
200 sizeof(*other_cmds->names), cmdname_compare);
201 uniq(other_cmds);
202 }
940208a7 203 exclude_cmds(other_cmds, main_cmds);
2156435f
JK
204}
205
e321180e
AR
206void list_commands(const char *title, struct cmdnames *main_cmds,
207 struct cmdnames *other_cmds)
2156435f 208{
e321180e
AR
209 int i, longest = 0;
210
211 for (i = 0; i < main_cmds->cnt; i++)
212 if (longest < main_cmds->names[i]->len)
213 longest = main_cmds->names[i]->len;
214 for (i = 0; i < other_cmds->cnt; i++)
215 if (longest < other_cmds->names[i]->len)
216 longest = other_cmds->names[i]->len;
2156435f 217
940208a7 218 if (main_cmds->cnt) {
61c5d431 219 const char *exec_path = git_exec_path();
940208a7
MV
220 printf("available %s in '%s'\n", title, exec_path);
221 printf("----------------");
222 mput_char('-', strlen(title) + strlen(exec_path));
1eb05690 223 putchar('\n');
940208a7 224 pretty_print_string_list(main_cmds, longest);
1eb05690
SP
225 putchar('\n');
226 }
227
940208a7
MV
228 if (other_cmds->cnt) {
229 printf("%s available from elsewhere on your $PATH\n", title);
230 printf("---------------------------------------");
231 mput_char('-', strlen(title));
232 putchar('\n');
233 pretty_print_string_list(other_cmds, longest);
1eb05690
SP
234 putchar('\n');
235 }
70827b15
LT
236}
237
940208a7 238int is_in_cmdlist(struct cmdnames *c, const char *s)
2156435f
JK
239{
240 int i;
241 for (i = 0; i < c->cnt; i++)
242 if (!strcmp(s, c->names[i]->name))
243 return 1;
244 return 0;
245}
246
f0e90716 247static int autocorrect;
746c221a 248static struct cmdnames aliases;
f0e90716
AR
249
250static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
251{
252 if (!strcmp(var, "help.autocorrect"))
253 autocorrect = git_config_int(var,value);
746c221a
PB
254 /* Also use aliases for command lookup */
255 if (!prefixcmp(var, "alias."))
256 add_cmdname(&aliases, var + 6, strlen(var + 6));
f0e90716
AR
257
258 return git_default_config(var, value, cb);
259}
260
8af84dad 261static int levenshtein_compare(const void *p1, const void *p2)
822a7d50 262{
8af84dad
JS
263 const struct cmdname *const *c1 = p1, *const *c2 = p2;
264 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
265 int l1 = (*c1)->len;
266 int l2 = (*c2)->len;
267 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
268}
269
746c221a
PB
270static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
271{
272 int i;
273 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
274
275 for (i = 0; i < old->cnt; i++)
276 cmds->names[cmds->cnt++] = old->names[i];
277 free(old->names);
278 old->cnt = 0;
279 old->names = NULL;
280}
281
06500a02 282/* An empirically derived magic number */
6612b9e4
EFL
283#define SIMILARITY_FLOOR 7
284#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
06500a02 285
823e0ded
MS
286static const char bad_interpreter_advice[] =
287 N_("'%s' appears to be a git command, but we were not\n"
288 "able to execute it. Maybe git-%s is broken?");
289
8af84dad 290const char *help_unknown_cmd(const char *cmd)
822a7d50 291{
8af84dad
JS
292 int i, n, best_similarity = 0;
293 struct cmdnames main_cmds, other_cmds;
294
295 memset(&main_cmds, 0, sizeof(main_cmds));
0b74f5dc 296 memset(&other_cmds, 0, sizeof(other_cmds));
746c221a 297 memset(&aliases, 0, sizeof(aliases));
8af84dad 298
f0e90716
AR
299 git_config(git_unknown_cmd_config, NULL);
300
8af84dad
JS
301 load_command_list("git-", &main_cmds, &other_cmds);
302
746c221a
PB
303 add_cmd_list(&main_cmds, &aliases);
304 add_cmd_list(&main_cmds, &other_cmds);
305 qsort(main_cmds.names, main_cmds.cnt,
306 sizeof(main_cmds.names), cmdname_compare);
307 uniq(&main_cmds);
8af84dad 308
6612b9e4
EFL
309 /* This abuses cmdname->len for levenshtein distance */
310 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
311 int cmp = 0; /* avoid compiler stupidity */
312 const char *candidate = main_cmds.names[i]->name;
313
823e0ded
MS
314 /*
315 * An exact match means we have the command, but
316 * for some reason exec'ing it gave us ENOENT; probably
317 * it's a bad interpreter in the #! line.
318 */
319 if (!strcmp(candidate, cmd))
320 die(_(bad_interpreter_advice), cmd, cmd);
321
6612b9e4
EFL
322 /* Does the candidate appear in common_cmds list? */
323 while (n < ARRAY_SIZE(common_cmds) &&
324 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
325 n++;
326 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
327 /* Yes, this is one of the common commands */
328 n++; /* use the entry from common_cmds[] */
329 if (!prefixcmp(candidate, cmd)) {
330 /* Give prefix match a very good score */
331 main_cmds.names[i]->len = 0;
332 continue;
333 }
334 }
335
8af84dad 336 main_cmds.names[i]->len =
6612b9e4
EFL
337 levenshtein(cmd, candidate, 0, 2, 1, 4) + 1;
338 }
8af84dad
JS
339
340 qsort(main_cmds.names, main_cmds.cnt,
341 sizeof(*main_cmds.names), levenshtein_compare);
342
343 if (!main_cmds.cnt)
344 die ("Uh oh. Your system reports no Git commands at all.");
345
6612b9e4
EFL
346 /* skip and count prefix matches */
347 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
348 ; /* still counting */
349
350 if (main_cmds.cnt <= n) {
351 /* prefix matches with everything? that is too ambiguous */
352 best_similarity = SIMILARITY_FLOOR + 1;
353 } else {
354 /* count all the most similar ones */
355 for (best_similarity = main_cmds.names[n++]->len;
356 (n < main_cmds.cnt &&
357 best_similarity == main_cmds.names[n]->len);
358 n++)
359 ; /* still counting */
360 }
06500a02 361 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
362 const char *assumed = main_cmds.names[0]->name;
363 main_cmds.names[0] = NULL;
364 clean_cmdnames(&main_cmds);
57f6ec02 365 fprintf(stderr, "WARNING: You called a Git command named '%s', "
8af84dad
JS
366 "which does not exist.\n"
367 "Continuing under the assumption that you meant '%s'\n",
368 cmd, assumed);
f0e90716
AR
369 if (autocorrect > 0) {
370 fprintf(stderr, "in %0.1f seconds automatically...\n",
371 (float)autocorrect/10.0);
372 poll(NULL, 0, autocorrect * 100);
373 }
8af84dad
JS
374 return assumed;
375 }
376
7283bbc7 377 fprintf(stderr, "git: '%s' is not a git command. See 'git --help'.\n", cmd);
8af84dad 378
06500a02 379 if (SIMILAR_ENOUGH(best_similarity)) {
8af84dad
JS
380 fprintf(stderr, "\nDid you mean %s?\n",
381 n < 2 ? "this": "one of these");
382
383 for (i = 0; i < n; i++)
384 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
385 }
386
822a7d50
RJ
387 exit(1);
388}
389
a633fca0 390int cmd_version(int argc, const char **argv, const char *prefix)
70827b15
LT
391{
392 printf("git version %s\n", git_version_string);
393 return 0;
394}