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