help.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 / Syncing 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_tmp("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_guides_help(void)
  332. {
  333. struct category_description catdesc[] = {
  334. { CAT_guide, N_("The Git concept guides are:") },
  335. { 0, NULL }
  336. };
  337. print_cmd_by_category(catdesc, NULL);
  338. putchar('\n');
  339. }
  340. static int get_alias(const char *var, const char *value, void *data)
  341. {
  342. struct string_list *list = data;
  343. if (skip_prefix(var, "alias.", &var))
  344. string_list_append(list, var)->util = xstrdup(value);
  345. return 0;
  346. }
  347. void list_all_cmds_help(void)
  348. {
  349. struct string_list others = STRING_LIST_INIT_DUP;
  350. struct string_list alias_list = STRING_LIST_INIT_DUP;
  351. struct cmdname_help *aliases;
  352. int i, longest;
  353. printf_ln(_("See 'git help <command>' to read about a specific subcommand"));
  354. print_cmd_by_category(main_categories, &longest);
  355. list_all_other_cmds(&others);
  356. if (others.nr)
  357. printf("\n%s\n", _("External commands"));
  358. for (i = 0; i < others.nr; i++)
  359. printf(" %s\n", others.items[i].string);
  360. string_list_clear(&others, 0);
  361. git_config(get_alias, &alias_list);
  362. string_list_sort(&alias_list);
  363. for (i = 0; i < alias_list.nr; i++) {
  364. size_t len = strlen(alias_list.items[i].string);
  365. if (longest < len)
  366. longest = len;
  367. }
  368. if (alias_list.nr) {
  369. printf("\n%s\n", _("Command aliases"));
  370. ALLOC_ARRAY(aliases, alias_list.nr + 1);
  371. for (i = 0; i < alias_list.nr; i++) {
  372. aliases[i].name = alias_list.items[i].string;
  373. aliases[i].help = alias_list.items[i].util;
  374. aliases[i].category = 1;
  375. }
  376. aliases[alias_list.nr].name = NULL;
  377. print_command_list(aliases, 1, longest);
  378. free(aliases);
  379. }
  380. string_list_clear(&alias_list, 1);
  381. }
  382. int is_in_cmdlist(struct cmdnames *c, const char *s)
  383. {
  384. int i;
  385. for (i = 0; i < c->cnt; i++)
  386. if (!strcmp(s, c->names[i]->name))
  387. return 1;
  388. return 0;
  389. }
  390. static int autocorrect;
  391. static struct cmdnames aliases;
  392. static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
  393. {
  394. const char *p;
  395. if (!strcmp(var, "help.autocorrect"))
  396. autocorrect = git_config_int(var,value);
  397. /* Also use aliases for command lookup */
  398. if (skip_prefix(var, "alias.", &p))
  399. add_cmdname(&aliases, p, strlen(p));
  400. return git_default_config(var, value, cb);
  401. }
  402. static int levenshtein_compare(const void *p1, const void *p2)
  403. {
  404. const struct cmdname *const *c1 = p1, *const *c2 = p2;
  405. const char *s1 = (*c1)->name, *s2 = (*c2)->name;
  406. int l1 = (*c1)->len;
  407. int l2 = (*c2)->len;
  408. return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
  409. }
  410. static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
  411. {
  412. int i;
  413. ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
  414. for (i = 0; i < old->cnt; i++)
  415. cmds->names[cmds->cnt++] = old->names[i];
  416. FREE_AND_NULL(old->names);
  417. old->cnt = 0;
  418. }
  419. /* An empirically derived magic number */
  420. #define SIMILARITY_FLOOR 7
  421. #define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
  422. static const char bad_interpreter_advice[] =
  423. N_("'%s' appears to be a git command, but we were not\n"
  424. "able to execute it. Maybe git-%s is broken?");
  425. const char *help_unknown_cmd(const char *cmd)
  426. {
  427. int i, n, best_similarity = 0;
  428. struct cmdnames main_cmds, other_cmds;
  429. struct cmdname_help *common_cmds;
  430. memset(&main_cmds, 0, sizeof(main_cmds));
  431. memset(&other_cmds, 0, sizeof(other_cmds));
  432. memset(&aliases, 0, sizeof(aliases));
  433. read_early_config(git_unknown_cmd_config, NULL);
  434. load_command_list("git-", &main_cmds, &other_cmds);
  435. add_cmd_list(&main_cmds, &aliases);
  436. add_cmd_list(&main_cmds, &other_cmds);
  437. QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
  438. uniq(&main_cmds);
  439. extract_cmds(&common_cmds, common_mask);
  440. /* This abuses cmdname->len for levenshtein distance */
  441. for (i = 0, n = 0; i < main_cmds.cnt; i++) {
  442. int cmp = 0; /* avoid compiler stupidity */
  443. const char *candidate = main_cmds.names[i]->name;
  444. /*
  445. * An exact match means we have the command, but
  446. * for some reason exec'ing it gave us ENOENT; probably
  447. * it's a bad interpreter in the #! line.
  448. */
  449. if (!strcmp(candidate, cmd))
  450. die(_(bad_interpreter_advice), cmd, cmd);
  451. /* Does the candidate appear in common_cmds list? */
  452. while (common_cmds[n].name &&
  453. (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
  454. n++;
  455. if (common_cmds[n].name && !cmp) {
  456. /* Yes, this is one of the common commands */
  457. n++; /* use the entry from common_cmds[] */
  458. if (starts_with(candidate, cmd)) {
  459. /* Give prefix match a very good score */
  460. main_cmds.names[i]->len = 0;
  461. continue;
  462. }
  463. }
  464. main_cmds.names[i]->len =
  465. levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
  466. }
  467. FREE_AND_NULL(common_cmds);
  468. QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
  469. if (!main_cmds.cnt)
  470. die(_("Uh oh. Your system reports no Git commands at all."));
  471. /* skip and count prefix matches */
  472. for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
  473. ; /* still counting */
  474. if (main_cmds.cnt <= n) {
  475. /* prefix matches with everything? that is too ambiguous */
  476. best_similarity = SIMILARITY_FLOOR + 1;
  477. } else {
  478. /* count all the most similar ones */
  479. for (best_similarity = main_cmds.names[n++]->len;
  480. (n < main_cmds.cnt &&
  481. best_similarity == main_cmds.names[n]->len);
  482. n++)
  483. ; /* still counting */
  484. }
  485. if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
  486. const char *assumed = main_cmds.names[0]->name;
  487. main_cmds.names[0] = NULL;
  488. clean_cmdnames(&main_cmds);
  489. fprintf_ln(stderr,
  490. _("WARNING: You called a Git command named '%s', "
  491. "which does not exist."),
  492. cmd);
  493. if (autocorrect < 0)
  494. fprintf_ln(stderr,
  495. _("Continuing under the assumption that "
  496. "you meant '%s'."),
  497. assumed);
  498. else {
  499. fprintf_ln(stderr,
  500. _("Continuing in %0.1f seconds, "
  501. "assuming that you meant '%s'."),
  502. (float)autocorrect/10.0, assumed);
  503. sleep_millisec(autocorrect * 100);
  504. }
  505. return assumed;
  506. }
  507. fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
  508. if (SIMILAR_ENOUGH(best_similarity)) {
  509. fprintf_ln(stderr,
  510. Q_("\nThe most similar command is",
  511. "\nThe most similar commands are",
  512. n));
  513. for (i = 0; i < n; i++)
  514. fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
  515. }
  516. exit(1);
  517. }
  518. void get_version_info(struct strbuf *buf, int show_build_options)
  519. {
  520. /*
  521. * The format of this string should be kept stable for compatibility
  522. * with external projects that rely on the output of "git version".
  523. *
  524. * Always show the version, even if other options are given.
  525. */
  526. strbuf_addf(buf, "git version %s\n", git_version_string);
  527. if (show_build_options) {
  528. strbuf_addf(buf, "cpu: %s\n", GIT_HOST_CPU);
  529. if (git_built_from_commit_string[0])
  530. strbuf_addf(buf, "built from commit: %s\n",
  531. git_built_from_commit_string);
  532. else
  533. strbuf_addstr(buf, "no commit associated with this build\n");
  534. strbuf_addf(buf, "sizeof-long: %d\n", (int)sizeof(long));
  535. strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
  536. strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
  537. /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
  538. }
  539. }
  540. int cmd_version(int argc, const char **argv, const char *prefix)
  541. {
  542. struct strbuf buf = STRBUF_INIT;
  543. int build_options = 0;
  544. const char * const usage[] = {
  545. N_("git version [<options>]"),
  546. NULL
  547. };
  548. struct option options[] = {
  549. OPT_BOOL(0, "build-options", &build_options,
  550. "also print build options"),
  551. OPT_END()
  552. };
  553. argc = parse_options(argc, argv, prefix, options, usage, 0);
  554. get_version_info(&buf, build_options);
  555. printf("%s", buf.buf);
  556. strbuf_release(&buf);
  557. return 0;
  558. }
  559. struct similar_ref_cb {
  560. const char *base_ref;
  561. struct string_list *similar_refs;
  562. };
  563. static int append_similar_ref(const char *refname, const struct object_id *oid,
  564. int flags, void *cb_data)
  565. {
  566. struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
  567. char *branch = strrchr(refname, '/') + 1;
  568. /* A remote branch of the same name is deemed similar */
  569. if (starts_with(refname, "refs/remotes/") &&
  570. !strcmp(branch, cb->base_ref))
  571. string_list_append_nodup(cb->similar_refs,
  572. shorten_unambiguous_ref(refname, 1));
  573. return 0;
  574. }
  575. static struct string_list guess_refs(const char *ref)
  576. {
  577. struct similar_ref_cb ref_cb;
  578. struct string_list similar_refs = STRING_LIST_INIT_DUP;
  579. ref_cb.base_ref = ref;
  580. ref_cb.similar_refs = &similar_refs;
  581. for_each_ref(append_similar_ref, &ref_cb);
  582. return similar_refs;
  583. }
  584. NORETURN void help_unknown_ref(const char *ref, const char *cmd,
  585. const char *error)
  586. {
  587. int i;
  588. struct string_list suggested_refs = guess_refs(ref);
  589. fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
  590. if (suggested_refs.nr > 0) {
  591. fprintf_ln(stderr,
  592. Q_("\nDid you mean this?",
  593. "\nDid you mean one of these?",
  594. suggested_refs.nr));
  595. for (i = 0; i < suggested_refs.nr; i++)
  596. fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
  597. }
  598. string_list_clear(&suggested_refs, 0);
  599. exit(1);
  600. }