blob: 02ba043319932411e90121d4147f25395782dfb5 [file] [log] [blame]
Linus Torvalds70827b12006-04-21 10:27:34 -07001#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
Johannes Schindelin8af84da2008-08-31 15:50:23 +02004#include "levenshtein.h"
Miklos Vajna940208a2008-07-30 01:16:58 +02005#include "help.h"
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +01006#include "common-cmds.h"
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +07007#include "string-list.h"
8#include "column.h"
Jeff King816fb462012-06-02 14:51:42 -04009#include "version.h"
Christian Couder64949982008-03-07 08:46:28 +010010
Miklos Vajna940208a2008-07-30 01:16:58 +020011void add_cmdname(struct cmdnames *cmds, const char *name, int len)
Linus Torvalds70827b12006-04-21 10:27:34 -070012{
Miklos Vajna940208a2008-07-30 01:16:58 +020013 struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
Scott R Parish1eb05692007-10-28 20:30:52 -070014
Linus Torvalds70827b12006-04-21 10:27:34 -070015 ent->len = len;
16 memcpy(ent->name, name, len);
17 ent->name[len] = 0;
Scott R Parish1eb05692007-10-28 20:30:52 -070018
19 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
20 cmds->names[cmds->cnt++] = ent;
Linus Torvalds70827b12006-04-21 10:27:34 -070021}
22
Johannes Schindelin8af84da2008-08-31 15:50:23 +020023static 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
Linus Torvalds70827b12006-04-21 10:27:34 -070033static 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
Scott R Parish1eb05692007-10-28 20:30:52 -070040static void uniq(struct cmdnames *cmds)
41{
42 int i, j;
43
44 if (!cmds->cnt)
45 return;
46
Jeff King4a157582012-07-26 00:16:19 +080047 for (i = j = 1; i < cmds->cnt; i++) {
48 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
49 free(cmds->names[i]);
50 else
Scott R Parish1eb05692007-10-28 20:30:52 -070051 cmds->names[j++] = cmds->names[i];
Jeff King4a157582012-07-26 00:16:19 +080052 }
Scott R Parish1eb05692007-10-28 20:30:52 -070053
54 cmds->cnt = j;
55}
56
Miklos Vajna940208a2008-07-30 01:16:58 +020057void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
Junio C Hamanof3fa1832007-11-08 15:35:32 -080058{
Scott R Parish1eb05692007-10-28 20:30:52 -070059 int ci, cj, ei;
60 int cmp;
61
62 ci = cj = ei = 0;
63 while (ci < cmds->cnt && ei < excludes->cnt) {
64 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
65 if (cmp < 0)
66 cmds->names[cj++] = cmds->names[ci++];
Junio C Hamano6a17f582012-07-25 11:01:12 -070067 else if (cmp == 0) {
68 ei++;
69 free(cmds->names[ci++]);
70 } else if (cmp > 0)
Scott R Parish1eb05692007-10-28 20:30:52 -070071 ei++;
72 }
73
74 while (ci < cmds->cnt)
75 cmds->names[cj++] = cmds->names[ci++];
76
77 cmds->cnt = cj;
78}
79
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +070080static void pretty_print_string_list(struct cmdnames *cmds,
81 unsigned int colopts)
Linus Torvalds70827b12006-04-21 10:27:34 -070082{
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +070083 struct string_list list = STRING_LIST_INIT_NODUP;
84 struct column_options copts;
85 int i;
Linus Torvalds70827b12006-04-21 10:27:34 -070086
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +070087 for (i = 0; i < cmds->cnt; i++)
88 string_list_append(&list, cmds->names[i]->name);
89 /*
90 * always enable column display, we only consult column.*
91 * about layout strategy and stuff
92 */
93 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
94 memset(&copts, 0, sizeof(copts));
95 copts.indent = " ";
96 copts.padding = 2;
97 print_columns(&list, colopts, &copts);
98 string_list_clear(&list, 0);
Linus Torvalds70827b12006-04-21 10:27:34 -070099}
100
Johannes Sixtcc3b7a92008-01-14 14:05:33 +0100101static int is_executable(const char *name)
102{
103 struct stat st;
104
105 if (stat(name, &st) || /* stat, not lstat */
106 !S_ISREG(st.st_mode))
107 return 0;
108
Ramsay Jonesb8a97332011-06-16 21:22:16 +0100109#if defined(WIN32) || defined(__CYGWIN__)
110#if defined(__CYGWIN__)
111if ((st.st_mode & S_IXUSR) == 0)
112#endif
Frank Li0d30ad72009-09-16 10:20:17 +0200113{ /* cannot trust the executable bit, peek into the file instead */
Johannes Sixtcc3b7a92008-01-14 14:05:33 +0100114 char buf[3] = { 0 };
115 int n;
116 int fd = open(name, O_RDONLY);
117 st.st_mode &= ~S_IXUSR;
118 if (fd >= 0) {
119 n = read(fd, buf, 2);
120 if (n == 2)
121 /* DOS executables start with "MZ" */
122 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
123 st.st_mode |= S_IXUSR;
124 close(fd);
125 }
Frank Li0d30ad72009-09-16 10:20:17 +0200126}
Johannes Sixtcc3b7a92008-01-14 14:05:33 +0100127#endif
128 return st.st_mode & S_IXUSR;
129}
130
Alex Riesene3211802008-08-28 19:15:33 +0200131static void list_commands_in_dir(struct cmdnames *cmds,
Miklos Vajna940208a2008-07-30 01:16:58 +0200132 const char *path,
133 const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700134{
Miklos Vajna940208a2008-07-30 01:16:58 +0200135 int prefix_len;
Scott R Parish1eb05692007-10-28 20:30:52 -0700136 DIR *dir = opendir(path);
Linus Torvalds70827b12006-04-21 10:27:34 -0700137 struct dirent *de;
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200138 struct strbuf buf = STRBUF_INIT;
139 int len;
Linus Torvalds70827b12006-04-21 10:27:34 -0700140
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200141 if (!dir)
Alex Riesene3211802008-08-28 19:15:33 +0200142 return;
Miklos Vajna940208a2008-07-30 01:16:58 +0200143 if (!prefix)
144 prefix = "git-";
145 prefix_len = strlen(prefix);
Linus Torvalds70827b12006-04-21 10:27:34 -0700146
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200147 strbuf_addf(&buf, "%s/", path);
148 len = buf.len;
149
Linus Torvalds70827b12006-04-21 10:27:34 -0700150 while ((de = readdir(dir)) != NULL) {
Linus Torvalds70827b12006-04-21 10:27:34 -0700151 int entlen;
152
Scott R Parishedb6ddc2007-10-27 01:36:50 -0700153 if (prefixcmp(de->d_name, prefix))
Linus Torvalds70827b12006-04-21 10:27:34 -0700154 continue;
Scott R Parish09660032007-10-27 01:36:52 -0700155
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200156 strbuf_setlen(&buf, len);
157 strbuf_addstr(&buf, de->d_name);
158 if (!is_executable(buf.buf))
Linus Torvalds70827b12006-04-21 10:27:34 -0700159 continue;
160
Scott R Parishedb6ddc2007-10-27 01:36:50 -0700161 entlen = strlen(de->d_name) - prefix_len;
Rene Scharfe5bb1cda2006-08-11 14:01:45 +0200162 if (has_extension(de->d_name, ".exe"))
Linus Torvalds70827b12006-04-21 10:27:34 -0700163 entlen -= 4;
164
Scott R Parish1eb05692007-10-28 20:30:52 -0700165 add_cmdname(cmds, de->d_name + prefix_len, entlen);
Linus Torvalds70827b12006-04-21 10:27:34 -0700166 }
167 closedir(dir);
Johannes Schindelinf5d600e2008-07-27 22:34:14 +0200168 strbuf_release(&buf);
Scott R Parish1eb05692007-10-28 20:30:52 -0700169}
170
Alex Riesene3211802008-08-28 19:15:33 +0200171void load_command_list(const char *prefix,
Miklos Vajna940208a2008-07-30 01:16:58 +0200172 struct cmdnames *main_cmds,
173 struct cmdnames *other_cmds)
Scott R Parish1eb05692007-10-28 20:30:52 -0700174{
Scott R Parish1eb05692007-10-28 20:30:52 -0700175 const char *env_path = getenv("PATH");
Scott R Parish1eb05692007-10-28 20:30:52 -0700176 const char *exec_path = git_exec_path();
177
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200178 if (exec_path) {
Alex Riesene3211802008-08-28 19:15:33 +0200179 list_commands_in_dir(main_cmds, exec_path, prefix);
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200180 qsort(main_cmds->names, main_cmds->cnt,
181 sizeof(*main_cmds->names), cmdname_compare);
182 uniq(main_cmds);
Scott R Parish1eb05692007-10-28 20:30:52 -0700183 }
184
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200185 if (env_path) {
186 char *paths, *path, *colon;
187 path = paths = xstrdup(env_path);
188 while (1) {
189 if ((colon = strchr(path, PATH_SEP)))
190 *colon = 0;
Pieter de Bie746c2212008-09-10 17:54:28 +0200191 if (!exec_path || strcmp(path, exec_path))
192 list_commands_in_dir(other_cmds, path, prefix);
Scott R Parish1eb05692007-10-28 20:30:52 -0700193
Alex Riesen1f08e5c2008-08-28 19:19:07 +0200194 if (!colon)
195 break;
196 path = colon + 1;
197 }
198 free(paths);
199
200 qsort(other_cmds->names, other_cmds->cnt,
201 sizeof(*other_cmds->names), cmdname_compare);
202 uniq(other_cmds);
Scott R Parish1eb05692007-10-28 20:30:52 -0700203 }
Miklos Vajna940208a2008-07-30 01:16:58 +0200204 exclude_cmds(other_cmds, main_cmds);
Jeff King21564352008-02-24 17:17:37 -0500205}
206
Junio C Hamanof4ed0af2012-05-03 15:13:31 -0700207void list_commands(unsigned int colopts,
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700208 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
Jeff King21564352008-02-24 17:17:37 -0500209{
Miklos Vajna940208a2008-07-30 01:16:58 +0200210 if (main_cmds->cnt) {
Alex Riesen61c5d432008-08-28 19:19:42 +0200211 const char *exec_path = git_exec_path();
Nguyễn Thái Ngọc Duy4470ef92012-04-25 18:21:53 +0700212 printf_ln(_("available git commands in '%s'"), exec_path);
Scott R Parish1eb05692007-10-28 20:30:52 -0700213 putchar('\n');
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700214 pretty_print_string_list(main_cmds, colopts);
Scott R Parish1eb05692007-10-28 20:30:52 -0700215 putchar('\n');
216 }
217
Miklos Vajna940208a2008-07-30 01:16:58 +0200218 if (other_cmds->cnt) {
Nguyễn Thái Ngọc Duy4470ef92012-04-25 18:21:53 +0700219 printf_ln(_("git commands available from elsewhere on your $PATH"));
Miklos Vajna940208a2008-07-30 01:16:58 +0200220 putchar('\n');
Nguyễn Thái Ngọc Duydbfae682012-04-13 17:54:37 +0700221 pretty_print_string_list(other_cmds, colopts);
Scott R Parish1eb05692007-10-28 20:30:52 -0700222 putchar('\n');
223 }
Linus Torvalds70827b12006-04-21 10:27:34 -0700224}
225
Junio C Hamano1542d4c2013-01-18 22:35:04 -0800226void list_common_cmds_help(void)
227{
228 int i, longest = 0;
229
230 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
231 if (longest < strlen(common_cmds[i].name))
232 longest = strlen(common_cmds[i].name);
233 }
234
235 puts(_("The most commonly used git commands are:"));
236 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
237 printf(" %s ", common_cmds[i].name);
238 mput_char(' ', longest - strlen(common_cmds[i].name));
239 puts(_(common_cmds[i].help));
240 }
241}
242
Miklos Vajna940208a2008-07-30 01:16:58 +0200243int is_in_cmdlist(struct cmdnames *c, const char *s)
Jeff King21564352008-02-24 17:17:37 -0500244{
245 int i;
246 for (i = 0; i < c->cnt; i++)
247 if (!strcmp(s, c->names[i]->name))
248 return 1;
249 return 0;
250}
251
Alex Riesenf0e90712008-08-31 15:54:58 +0200252static int autocorrect;
Pieter de Bie746c2212008-09-10 17:54:28 +0200253static struct cmdnames aliases;
Alex Riesenf0e90712008-08-31 15:54:58 +0200254
255static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100256{
Alex Riesenf0e90712008-08-31 15:54:58 +0200257 if (!strcmp(var, "help.autocorrect"))
258 autocorrect = git_config_int(var,value);
Pieter de Bie746c2212008-09-10 17:54:28 +0200259 /* Also use aliases for command lookup */
260 if (!prefixcmp(var, "alias."))
261 add_cmdname(&aliases, var + 6, strlen(var + 6));
Alex Riesenf0e90712008-08-31 15:54:58 +0200262
263 return git_default_config(var, value, cb);
264}
265
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200266static int levenshtein_compare(const void *p1, const void *p2)
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100267{
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200268 const struct cmdname *const *c1 = p1, *const *c2 = p2;
269 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
270 int l1 = (*c1)->len;
271 int l2 = (*c2)->len;
272 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
273}
274
Pieter de Bie746c2212008-09-10 17:54:28 +0200275static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
276{
277 int i;
278 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
279
280 for (i = 0; i < old->cnt; i++)
281 cmds->names[cmds->cnt++] = old->names[i];
282 free(old->names);
283 old->cnt = 0;
284 old->names = NULL;
285}
286
Johannes Sixt06500a02009-12-15 08:57:18 +0100287/* An empirically derived magic number */
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100288#define SIMILARITY_FLOOR 7
289#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
Johannes Sixt06500a02009-12-15 08:57:18 +0100290
Michael Schubert823e0de2011-07-08 12:08:49 +0200291static const char bad_interpreter_advice[] =
292 N_("'%s' appears to be a git command, but we were not\n"
293 "able to execute it. Maybe git-%s is broken?");
294
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200295const char *help_unknown_cmd(const char *cmd)
296{
297 int i, n, best_similarity = 0;
298 struct cmdnames main_cmds, other_cmds;
299
300 memset(&main_cmds, 0, sizeof(main_cmds));
Johan Herland0b74f5d2009-08-11 12:10:21 +0200301 memset(&other_cmds, 0, sizeof(other_cmds));
Pieter de Bie746c2212008-09-10 17:54:28 +0200302 memset(&aliases, 0, sizeof(aliases));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200303
Alex Riesenf0e90712008-08-31 15:54:58 +0200304 git_config(git_unknown_cmd_config, NULL);
305
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200306 load_command_list("git-", &main_cmds, &other_cmds);
307
Pieter de Bie746c2212008-09-10 17:54:28 +0200308 add_cmd_list(&main_cmds, &aliases);
309 add_cmd_list(&main_cmds, &other_cmds);
310 qsort(main_cmds.names, main_cmds.cnt,
311 sizeof(main_cmds.names), cmdname_compare);
312 uniq(&main_cmds);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200313
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100314 /* This abuses cmdname->len for levenshtein distance */
315 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
316 int cmp = 0; /* avoid compiler stupidity */
317 const char *candidate = main_cmds.names[i]->name;
318
Michael Schubert823e0de2011-07-08 12:08:49 +0200319 /*
320 * An exact match means we have the command, but
321 * for some reason exec'ing it gave us ENOENT; probably
322 * it's a bad interpreter in the #! line.
323 */
324 if (!strcmp(candidate, cmd))
325 die(_(bad_interpreter_advice), cmd, cmd);
326
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100327 /* Does the candidate appear in common_cmds list? */
328 while (n < ARRAY_SIZE(common_cmds) &&
329 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
330 n++;
331 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
332 /* Yes, this is one of the common commands */
333 n++; /* use the entry from common_cmds[] */
334 if (!prefixcmp(candidate, cmd)) {
335 /* Give prefix match a very good score */
336 main_cmds.names[i]->len = 0;
337 continue;
338 }
339 }
340
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200341 main_cmds.names[i]->len =
Matthieu Moyc41494f2012-05-27 18:02:58 +0200342 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100343 }
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200344
345 qsort(main_cmds.names, main_cmds.cnt,
346 sizeof(*main_cmds.names), levenshtein_compare);
347
348 if (!main_cmds.cnt)
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700349 die(_("Uh oh. Your system reports no Git commands at all."));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200350
Erik Faye-Lund6612b9e2010-11-26 17:00:39 +0100351 /* skip and count prefix matches */
352 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
353 ; /* still counting */
354
355 if (main_cmds.cnt <= n) {
356 /* prefix matches with everything? that is too ambiguous */
357 best_similarity = SIMILARITY_FLOOR + 1;
358 } else {
359 /* count all the most similar ones */
360 for (best_similarity = main_cmds.names[n++]->len;
361 (n < main_cmds.cnt &&
362 best_similarity == main_cmds.names[n]->len);
363 n++)
364 ; /* still counting */
365 }
Johannes Sixt06500a02009-12-15 08:57:18 +0100366 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200367 const char *assumed = main_cmds.names[0]->name;
368 main_cmds.names[0] = NULL;
369 clean_cmdnames(&main_cmds);
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700370 fprintf_ln(stderr,
371 _("WARNING: You called a Git command named '%s', "
372 "which does not exist.\n"
373 "Continuing under the assumption that you meant '%s'"),
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200374 cmd, assumed);
Alex Riesenf0e90712008-08-31 15:54:58 +0200375 if (autocorrect > 0) {
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700376 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
Alex Riesenf0e90712008-08-31 15:54:58 +0200377 (float)autocorrect/10.0);
378 poll(NULL, 0, autocorrect * 100);
379 }
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200380 return assumed;
381 }
382
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700383 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200384
Johannes Sixt06500a02009-12-15 08:57:18 +0100385 if (SIMILAR_ENOUGH(best_similarity)) {
Nguyễn Thái Ngọc Duy96656272012-04-23 19:30:24 +0700386 fprintf_ln(stderr,
387 Q_("\nDid you mean this?",
388 "\nDid you mean one of these?",
389 n));
Johannes Schindelin8af84da2008-08-31 15:50:23 +0200390
391 for (i = 0; i < n; i++)
392 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
393 }
394
Ramsay Allan Jones822a7d52006-07-30 22:42:25 +0100395 exit(1);
396}
397
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700398int cmd_version(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 10:27:34 -0700399{
David Aguilarf2de0b92013-04-16 13:33:25 -0700400 /*
401 * The format of this string should be kept stable for compatibility
402 * with external projects that rely on the output of "git version".
403 */
Linus Torvalds70827b12006-04-21 10:27:34 -0700404 printf("git version %s\n", git_version_string);
405 return 0;
406}