THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

797 lines
20KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "builtin.h"
  4. #include "exec-cmd.h"
  5. #include "run-command.h"
  6. #include "levenshtein.h"
  7. #include "help.h"
  8. #include "command-list.h"
  9. #include "string-list.h"
  10. #include "column.h"
  11. #include "version.h"
  12. #include "refs.h"
  13. #include "parse-options.h"
  14. struct category_description {
  15. uint32_t category;
  16. const char *desc;
  17. };
  18. static uint32_t common_mask =
  19. CAT_init | CAT_worktree | CAT_info |
  20. CAT_history | CAT_remote;
  21. static struct category_description common_categories[] = {
  22. { CAT_init, N_("start a working area (see also: git help tutorial)") },
  23. { CAT_worktree, N_("work on the current change (see also: git help everyday)") },
  24. { CAT_info, N_("examine the history and state (see also: git help revisions)") },
  25. { CAT_history, N_("grow, mark and tweak your common history") },
  26. { CAT_remote, N_("collaborate (see also: git help workflows)") },
  27. { 0, NULL }
  28. };
  29. static struct category_description main_categories[] = {
  30. { CAT_mainporcelain, N_("Main Porcelain Commands") },
  31. { CAT_ancillarymanipulators, N_("Ancillary Commands / Manipulators") },
  32. { CAT_ancillaryinterrogators, N_("Ancillary Commands / Interrogators") },
  33. { CAT_foreignscminterface, N_("Interacting with Others") },
  34. { CAT_plumbingmanipulators, N_("Low-level Commands / Manipulators") },
  35. { CAT_plumbinginterrogators, N_("Low-level Commands / Interrogators") },
  36. { CAT_synchingrepositories, N_("Low-level Commands / Synching Repositories") },
  37. { CAT_purehelpers, N_("Low-level Commands / Internal Helpers") },
  38. { 0, NULL }
  39. };
  40. static const char *drop_prefix(const char *name, uint32_t category)
  41. {
  42. const char *new_name;
  43. if (skip_prefix(name, "git-", &new_name))
  44. return new_name;
  45. if (category == CAT_guide && skip_prefix(name, "git", &new_name))
  46. return new_name;
  47. return name;
  48. }
  49. static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
  50. {
  51. int i, nr = 0;
  52. struct cmdname_help *cmds;
  53. if (ARRAY_SIZE(command_list) == 0)
  54. BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
  55. ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
  56. for (i = 0; i < ARRAY_SIZE(command_list); i++) {
  57. const struct cmdname_help *cmd = command_list + i;
  58. if (!(cmd->category & mask))
  59. continue;
  60. cmds[nr] = *cmd;
  61. cmds[nr].name = drop_prefix(cmd->name, cmd->category);
  62. nr++;
  63. }
  64. cmds[nr].name = NULL;
  65. *p_cmds = cmds;
  66. }
  67. static void print_command_list(const struct cmdname_help *cmds,
  68. uint32_t mask, int longest)
  69. {
  70. int i;
  71. for (i = 0; cmds[i].name; i++) {
  72. if (cmds[i].category & mask) {
  73. size_t len = strlen(cmds[i].name);
  74. printf(" %s ", cmds[i].name);
  75. if (longest > len)
  76. mput_char(' ', longest - len);
  77. puts(_(cmds[i].help));
  78. }
  79. }
  80. }
  81. static int cmd_name_cmp(const void *elem1, const void *elem2)
  82. {
  83. const struct cmdname_help *e1 = elem1;
  84. const struct cmdname_help *e2 = elem2;
  85. return strcmp(e1->name, e2->name);
  86. }
  87. static void print_cmd_by_category(const struct category_description *catdesc,
  88. int *longest_p)
  89. {
  90. struct cmdname_help *cmds;
  91. int longest = 0;
  92. int i, nr = 0;
  93. uint32_t mask = 0;
  94. for (i = 0; catdesc[i].desc; i++)
  95. mask |= catdesc[i].category;
  96. extract_cmds(&cmds, mask);
  97. for (i = 0; cmds[i].name; i++, nr++) {
  98. if (longest < strlen(cmds[i].name))
  99. longest = strlen(cmds[i].name);
  100. }
  101. QSORT(cmds, nr, cmd_name_cmp);
  102. for (i = 0; catdesc[i].desc; i++) {
  103. uint32_t mask = catdesc[i].category;
  104. const char *desc = catdesc[i].desc;
  105. printf("\n%s\n", _(desc));
  106. print_command_list(cmds, mask, longest);
  107. }
  108. free(cmds);
  109. if (longest_p)
  110. *longest_p = longest;
  111. }
  112. void add_cmdname(struct cmdnames *cmds, const char *name, int len)
  113. {
  114. struct cmdname *ent;
  115. FLEX_ALLOC_MEM(ent, name, name, len);
  116. ent->len = len;
  117. ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
  118. cmds->names[cmds->cnt++] = ent;
  119. }
  120. static void clean_cmdnames(struct cmdnames *cmds)
  121. {
  122. int i;
  123. for (i = 0; i < cmds->cnt; ++i)
  124. free(cmds->names[i]);
  125. free(cmds->names);
  126. cmds->cnt = 0;
  127. cmds->alloc = 0;
  128. }
  129. static int cmdname_compare(const void *a_, const void *b_)
  130. {
  131. struct cmdname *a = *(struct cmdname **)a_;
  132. struct cmdname *b = *(struct cmdname **)b_;
  133. return strcmp(a->name, b->name);
  134. }
  135. static void uniq(struct cmdnames *cmds)
  136. {
  137. int i, j;
  138. if (!cmds->cnt)
  139. return;
  140. for (i = j = 1; i < cmds->cnt; i++) {
  141. if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
  142. free(cmds->names[i]);
  143. else
  144. cmds->names[j++] = cmds->names[i];
  145. }
  146. cmds->cnt = j;
  147. }
  148. void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
  149. {
  150. int ci, cj, ei;
  151. int cmp;
  152. ci = cj = ei = 0;
  153. while (ci < cmds->cnt && ei < excludes->cnt) {
  154. cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
  155. if (cmp < 0)
  156. cmds->names[cj++] = cmds->names[ci++];
  157. else if (cmp == 0) {
  158. ei++;
  159. free(cmds->names[ci++]);
  160. } else if (cmp > 0)
  161. ei++;
  162. }
  163. while (ci < cmds->cnt)
  164. cmds->names[cj++] = cmds->names[ci++];
  165. cmds->cnt = cj;
  166. }
  167. static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
  168. {
  169. struct string_list list = STRING_LIST_INIT_NODUP;
  170. struct column_options copts;
  171. int i;
  172. for (i = 0; i < cmds->cnt; i++)
  173. string_list_append(&list, cmds->names[i]->name);
  174. /*
  175. * always enable column display, we only consult column.*
  176. * about layout strategy and stuff
  177. */
  178. colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
  179. memset(&copts, 0, sizeof(copts));
  180. copts.indent = " ";
  181. copts.padding = 2;
  182. print_columns(&list, colopts, &copts);
  183. string_list_clear(&list, 0);
  184. }
  185. static void list_commands_in_dir(struct cmdnames *cmds,
  186. const char *path,
  187. const char *prefix)
  188. {
  189. DIR *dir = opendir(path);
  190. struct dirent *de;
  191. struct strbuf buf = STRBUF_INIT;
  192. int len;
  193. if (!dir)
  194. return;
  195. if (!prefix)
  196. prefix = "git-";
  197. strbuf_addf(&buf, "%s/", path);
  198. len = buf.len;
  199. while ((de = readdir(dir)) != NULL) {
  200. const char *ent;
  201. size_t entlen;
  202. if (!skip_prefix(de->d_name, prefix, &ent))
  203. continue;
  204. strbuf_setlen(&buf, len);
  205. strbuf_addstr(&buf, de->d_name);
  206. if (!is_executable(buf.buf))
  207. continue;
  208. entlen = strlen(ent);
  209. strip_suffix(ent, ".exe", &entlen);
  210. add_cmdname(cmds, ent, entlen);
  211. }
  212. closedir(dir);
  213. strbuf_release(&buf);
  214. }
  215. void load_command_list(const char *prefix,
  216. struct cmdnames *main_cmds,
  217. struct cmdnames *other_cmds)
  218. {
  219. const char *env_path = getenv("PATH");
  220. const char *exec_path = git_exec_path();
  221. if (exec_path) {
  222. list_commands_in_dir(main_cmds, exec_path, prefix);
  223. QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
  224. uniq(main_cmds);
  225. }
  226. if (env_path) {
  227. char *paths, *path, *colon;
  228. path = paths = xstrdup(env_path);
  229. while (1) {
  230. if ((colon = strchr(path, PATH_SEP)))
  231. *colon = 0;
  232. if (!exec_path || strcmp(path, exec_path))
  233. list_commands_in_dir(other_cmds, path, prefix);
  234. if (!colon)
  235. break;
  236. path = colon + 1;
  237. }
  238. free(paths);
  239. QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
  240. uniq(other_cmds);
  241. }
  242. exclude_cmds(other_cmds, main_cmds);
  243. }
  244. void list_commands(unsigned int colopts,
  245. struct cmdnames *main_cmds, struct cmdnames *other_cmds)
  246. {
  247. if (main_cmds->cnt) {
  248. const char *exec_path = git_exec_path();
  249. printf_ln(_("available git commands in '%s'"), exec_path);
  250. putchar('\n');
  251. pretty_print_cmdnames(main_cmds, colopts);
  252. putchar('\n');
  253. }
  254. if (other_cmds->cnt) {
  255. printf_ln(_("git commands available from elsewhere on your $PATH"));
  256. putchar('\n');
  257. pretty_print_cmdnames(other_cmds, colopts);
  258. putchar('\n');
  259. }
  260. }
  261. void list_common_cmds_help(void)
  262. {
  263. puts(_("These are common Git commands used in various situations:"));
  264. print_cmd_by_category(common_categories, NULL);
  265. }
  266. void list_all_main_cmds(struct string_list *list)
  267. {
  268. struct cmdnames main_cmds, other_cmds;
  269. int i;
  270. memset(&main_cmds, 0, sizeof(main_cmds));
  271. memset(&other_cmds, 0, sizeof(other_cmds));
  272. load_command_list("git-", &main_cmds, &other_cmds);
  273. for (i = 0; i < main_cmds.cnt; i++)
  274. string_list_append(list, main_cmds.names[i]->name);
  275. clean_cmdnames(&main_cmds);
  276. clean_cmdnames(&other_cmds);
  277. }
  278. void list_all_other_cmds(struct string_list *list)
  279. {
  280. struct cmdnames main_cmds, other_cmds;
  281. int i;
  282. memset(&main_cmds, 0, sizeof(main_cmds));
  283. memset(&other_cmds, 0, sizeof(other_cmds));
  284. load_command_list("git-", &main_cmds, &other_cmds);
  285. for (i = 0; i < other_cmds.cnt; i++)
  286. string_list_append(list, other_cmds.names[i]->name);
  287. clean_cmdnames(&main_cmds);
  288. clean_cmdnames(&other_cmds);
  289. }
  290. void list_cmds_by_category(struct string_list *list,
  291. const char *cat)
  292. {
  293. int i, n = ARRAY_SIZE(command_list);
  294. uint32_t cat_id = 0;
  295. for (i = 0; category_names[i]; i++) {
  296. if (!strcmp(cat, category_names[i])) {
  297. cat_id = 1UL << i;
  298. break;
  299. }
  300. }
  301. if (!cat_id)
  302. die(_("unsupported command listing type '%s'"), cat);
  303. for (i = 0; i < n; i++) {
  304. struct cmdname_help *cmd = command_list + i;
  305. if (!(cmd->category & cat_id))
  306. continue;
  307. string_list_append(list, drop_prefix(cmd->name, cmd->category));
  308. }
  309. }
  310. void list_cmds_by_config(struct string_list *list)
  311. {
  312. const char *cmd_list;
  313. if (git_config_get_string_const("completion.commands", &cmd_list))
  314. return;
  315. string_list_sort(list);
  316. string_list_remove_duplicates(list, 0);
  317. while (*cmd_list) {
  318. struct strbuf sb = STRBUF_INIT;
  319. const char *p = strchrnul(cmd_list, ' ');
  320. strbuf_add(&sb, cmd_list, p - cmd_list);
  321. if (sb.buf[0] == '-')
  322. string_list_remove(list, sb.buf + 1, 0);
  323. else
  324. string_list_insert(list, sb.buf);
  325. strbuf_release(&sb);
  326. while (*p == ' ')
  327. p++;
  328. cmd_list = p;
  329. }
  330. }
  331. void list_common_guides_help(void)
  332. {
  333. struct category_description catdesc[] = {
  334. { CAT_guide, N_("The common Git guides are:") },
  335. { 0, NULL }
  336. };
  337. print_cmd_by_category(catdesc, NULL);
  338. putchar('\n');
  339. }
  340. struct slot_expansion {
  341. const char *prefix;
  342. const char *placeholder;
  343. void (*fn)(struct string_list *list, const char *prefix);
  344. int found;
  345. };
  346. void list_config_help(int for_human)
  347. {
  348. struct slot_expansion slot_expansions[] = {
  349. { "advice", "*", list_config_advices },
  350. { "color.branch", "<slot>", list_config_color_branch_slots },
  351. { "color.decorate", "<slot>", list_config_color_decorate_slots },
  352. { "color.diff", "<slot>", list_config_color_diff_slots },
  353. { "color.grep", "<slot>", list_config_color_grep_slots },
  354. { "color.interactive", "<slot>", list_config_color_interactive_slots },
  355. { "color.remote", "<slot>", list_config_color_sideband_slots },
  356. { "color.status", "<slot>", list_config_color_status_slots },
  357. { "fsck", "<msg-id>", list_config_fsck_msg_ids },
  358. { "receive.fsck", "<msg-id>", list_config_fsck_msg_ids },
  359. { NULL, NULL, NULL }
  360. };
  361. const char **p;
  362. struct slot_expansion *e;
  363. struct string_list keys = STRING_LIST_INIT_DUP;
  364. int i;
  365. for (p = config_name_list; *p; p++) {
  366. const char *var = *p;
  367. struct strbuf sb = STRBUF_INIT;
  368. for (e = slot_expansions; e->prefix; e++) {
  369. strbuf_reset(&sb);
  370. strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
  371. if (!strcasecmp(var, sb.buf)) {
  372. e->fn(&keys, e->prefix);
  373. e->found++;
  374. break;
  375. }
  376. }
  377. strbuf_release(&sb);
  378. if (!e->prefix)
  379. string_list_append(&keys, var);
  380. }
  381. for (e = slot_expansions; e->prefix; e++)
  382. if (!e->found)
  383. BUG("slot_expansion %s.%s is not used",
  384. e->prefix, e->placeholder);
  385. string_list_sort(&keys);
  386. for (i = 0; i < keys.nr; i++) {
  387. const char *var = keys.items[i].string;
  388. const char *wildcard, *tag, *cut;
  389. if (for_human) {
  390. puts(var);
  391. continue;
  392. }
  393. wildcard = strchr(var, '*');
  394. tag = strchr(var, '<');
  395. if (!wildcard && !tag) {
  396. puts(var);
  397. continue;
  398. }
  399. if (wildcard && !tag)
  400. cut = wildcard;
  401. else if (!wildcard && tag)
  402. cut = tag;
  403. else
  404. cut = wildcard < tag ? wildcard : tag;
  405. /*
  406. * We may produce duplicates, but that's up to
  407. * git-completion.bash to handle
  408. */
  409. printf("%.*s\n", (int)(cut - var), var);
  410. }
  411. string_list_clear(&keys, 0);
  412. }
  413. static int get_alias(const char *var, const char *value, void *data)
  414. {
  415. struct string_list *list = data;
  416. if (skip_prefix(var, "alias.", &var))
  417. string_list_append(list, var)->util = xstrdup(value);
  418. return 0;
  419. }
  420. void list_all_cmds_help(void)
  421. {
  422. struct string_list others = STRING_LIST_INIT_DUP;
  423. struct string_list alias_list = STRING_LIST_INIT_DUP;
  424. struct cmdname_help *aliases;
  425. int i, longest;
  426. printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
  427. print_cmd_by_category(main_categories, &longest);
  428. list_all_other_cmds(&others);
  429. if (others.nr)
  430. printf("\n%s\n", _("External commands"));
  431. for (i = 0; i < others.nr; i++)
  432. printf(" %s\n", others.items[i].string);
  433. string_list_clear(&others, 0);
  434. git_config(get_alias, &alias_list);
  435. string_list_sort(&alias_list);
  436. for (i = 0; i < alias_list.nr; i++) {
  437. size_t len = strlen(alias_list.items[i].string);
  438. if (longest < len)
  439. longest = len;
  440. }
  441. if (alias_list.nr) {
  442. printf("\n%s\n", _("Command aliases"));
  443. ALLOC_ARRAY(aliases, alias_list.nr + 1);
  444. for (i = 0; i < alias_list.nr; i++) {
  445. aliases[i].name = alias_list.items[i].string;
  446. aliases[i].help = alias_list.items[i].util;
  447. aliases[i].category = 1;
  448. }
  449. aliases[alias_list.nr].name = NULL;
  450. print_command_list(aliases, 1, longest);
  451. free(aliases);
  452. }
  453. string_list_clear(&alias_list, 1);
  454. }
  455. int is_in_cmdlist(struct cmdnames *c, const char *s)
  456. {
  457. int i;
  458. for (i = 0; i < c->cnt; i++)
  459. if (!strcmp(s, c->names[i]->name))
  460. return 1;
  461. return 0;
  462. }
  463. static int autocorrect;
  464. static struct cmdnames aliases;
  465. static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
  466. {
  467. const char *p;
  468. if (!strcmp(var, "help.autocorrect"))
  469. autocorrect = git_config_int(var,value);
  470. /* Also use aliases for command lookup */
  471. if (skip_prefix(var, "alias.", &p))
  472. add_cmdname(&aliases, p, strlen(p));
  473. return git_default_config(var, value, cb);
  474. }
  475. static int levenshtein_compare(const void *p1, const void *p2)
  476. {
  477. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  478. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  479. int l1 = (*c1)->len;
  480. int l2 = (*c2)->len;
  481. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  482. }
  483. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  484. {
  485. int i;
  486. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  487. for (i = 0; i < old->cnt; i++)
  488. cmds->names[cmds->cnt++] = old->names[i];
  489. FREE_AND_NULL(old->names);
  490. old->cnt = 0;
  491. }
  492. /* An empirically derived magic number */
  493. #define SIMILARITY_FLOOR 7
  494. #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
  495. static const char bad_interpreter_advice[] =
  496. N_("'%s' appears to be a git command, but we were not\n"
  497. "able to execute it. Maybe git-%s is broken?");
  498. const char *help_unknown_cmd(const char *cmd)
  499. {
  500. int i, n, best_similarity = 0;
  501. struct cmdnames main_cmds, other_cmds;
  502. struct cmdname_help *common_cmds;
  503. memset(&main_cmds, 0, sizeof(main_cmds));
  504. memset(&other_cmds, 0, sizeof(other_cmds));
  505. memset(&aliases, 0, sizeof(aliases));
  506. read_early_config(git_unknown_cmd_config, NULL);
  507. load_command_list("git-", &main_cmds, &other_cmds);
  508. add_cmd_list(&main_cmds, &aliases);
  509. add_cmd_list(&main_cmds, &other_cmds);
  510. QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
  511. uniq(&main_cmds);
  512. extract_cmds(&common_cmds, common_mask);
  513. /* This abuses cmdname->len for levenshtein distance */
  514. for (i = 0, n = 0; i < main_cmds.cnt; i++) {
  515. int cmp = 0; /* avoid compiler stupidity */
  516. const char *candidate = main_cmds.names[i]->name;
  517. /*
  518. * An exact match means we have the command, but
  519. * for some reason exec'ing it gave us ENOENT; probably
  520. * it's a bad interpreter in the #! line.
  521. */
  522. if (!strcmp(candidate, cmd))
  523. die(_(bad_interpreter_advice), cmd, cmd);
  524. /* Does the candidate appear in common_cmds list? */
  525. while (common_cmds[n].name &&
  526. (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
  527. n++;
  528. if (common_cmds[n].name && !cmp) {
  529. /* Yes, this is one of the common commands */
  530. n++; /* use the entry from common_cmds[] */
  531. if (starts_with(candidate, cmd)) {
  532. /* Give prefix match a very good score */
  533. main_cmds.names[i]->len = 0;
  534. continue;
  535. }
  536. }
  537. main_cmds.names[i]->len =
  538. levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
  539. }
  540. FREE_AND_NULL(common_cmds);
  541. QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
  542. if (!main_cmds.cnt)
  543. die(_("Uh oh. Your system reports no Git commands at all."));
  544. /* skip and count prefix matches */
  545. for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
  546. ; /* still counting */
  547. if (main_cmds.cnt <= n) {
  548. /* prefix matches with everything? that is too ambiguous */
  549. best_similarity = SIMILARITY_FLOOR + 1;
  550. } else {
  551. /* count all the most similar ones */
  552. for (best_similarity = main_cmds.names[n++]->len;
  553. (n < main_cmds.cnt &&
  554. best_similarity == main_cmds.names[n]->len);
  555. n++)
  556. ; /* still counting */
  557. }
  558. if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
  559. const char *assumed = main_cmds.names[0]->name;
  560. main_cmds.names[0] = NULL;
  561. clean_cmdnames(&main_cmds);
  562. fprintf_ln(stderr,
  563. _("WARNING: You called a Git command named '%s', "
  564. "which does not exist."),
  565. cmd);
  566. if (autocorrect < 0)
  567. fprintf_ln(stderr,
  568. _("Continuing under the assumption that "
  569. "you meant '%s'."),
  570. assumed);
  571. else {
  572. fprintf_ln(stderr,
  573. _("Continuing in %0.1f seconds, "
  574. "assuming that you meant '%s'."),
  575. (float)autocorrect/10.0, assumed);
  576. sleep_millisec(autocorrect * 100);
  577. }
  578. return assumed;
  579. }
  580. fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
  581. if (SIMILAR_ENOUGH(best_similarity)) {
  582. fprintf_ln(stderr,
  583. Q_("\nThe most similar command is",
  584. "\nThe most similar commands are",
  585. n));
  586. for (i = 0; i < n; i++)
  587. fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
  588. }
  589. exit(1);
  590. }
  591. int cmd_version(int argc, const char **argv, const char *prefix)
  592. {
  593. int build_options = 0;
  594. const char * const usage[] = {
  595. N_("git version [<options>]"),
  596. NULL
  597. };
  598. struct option options[] = {
  599. OPT_BOOL(0, "build-options", &build_options,
  600. "also print build options"),
  601. OPT_END()
  602. };
  603. argc = parse_options(argc, argv, prefix, options, usage, 0);
  604. /*
  605. * The format of this string should be kept stable for compatibility
  606. * with external projects that rely on the output of "git version".
  607. *
  608. * Always show the version, even if other options are given.
  609. */
  610. printf("git version %s\n", git_version_string);
  611. if (build_options) {
  612. printf("cpu: %s\n", GIT_HOST_CPU);
  613. if (git_built_from_commit_string[0])
  614. printf("built from commit: %s\n",
  615. git_built_from_commit_string);
  616. else
  617. printf("no commit associated with this build\n");
  618. printf("sizeof-long: %d\n", (int)sizeof(long));
  619. printf("sizeof-size_t: %d\n", (int)sizeof(size_t));
  620. /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
  621. }
  622. return 0;
  623. }
  624. struct similar_ref_cb {
  625. const char *base_ref;
  626. struct string_list *similar_refs;
  627. };
  628. static int append_similar_ref(const char *refname, const struct object_id *oid,
  629. int flags, void *cb_data)
  630. {
  631. struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
  632. char *branch = strrchr(refname, '/') + 1;
  633. /* A remote branch of the same name is deemed similar */
  634. if (starts_with(refname, "refs/remotes/") &&
  635. !strcmp(branch, cb->base_ref))
  636. string_list_append_nodup(cb->similar_refs,
  637. shorten_unambiguous_ref(refname, 1));
  638. return 0;
  639. }
  640. static struct string_list guess_refs(const char *ref)
  641. {
  642. struct similar_ref_cb ref_cb;
  643. struct string_list similar_refs = STRING_LIST_INIT_DUP;
  644. ref_cb.base_ref = ref;
  645. ref_cb.similar_refs = &similar_refs;
  646. for_each_ref(append_similar_ref, &ref_cb);
  647. return similar_refs;
  648. }
  649. NORETURN void help_unknown_ref(const char *ref, const char *cmd,
  650. const char *error)
  651. {
  652. int i;
  653. struct string_list suggested_refs = guess_refs(ref);
  654. fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
  655. if (suggested_refs.nr > 0) {
  656. fprintf_ln(stderr,
  657. Q_("\nDid you mean this?",
  658. "\nDid you mean one of these?",
  659. suggested_refs.nr));
  660. for (i = 0; i < suggested_refs.nr; i++)
  661. fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
  662. }
  663. string_list_clear(&suggested_refs, 0);
  664. exit(1);
  665. }