PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/git-source/help.c

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