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