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