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