🌐 AI搜索 & 代理 主页
blob: 53e2a67e0052b7abb9f01e075f76c4eb5f35cbfc [file] [log] [blame]
Linus Torvalds70827b12006-04-21 17:27:341#include "cache.h"
2#include "builtin.h"
3#include "exec_cmd.h"
Johannes Schindelin8af84da2008-08-31 13:50:234#include "levenshtein.h"
Miklos Vajna940208a2008-07-29 23:16:585#include "help.h"
Erik Faye-Lund6612b9e2010-11-26 16:00:396#include "common-cmds.h"
Nguyễn Thái Ngọc Duydbfae682012-04-13 10:54:377#include "string-list.h"
8#include "column.h"
Jeff King816fb462012-06-02 18:51:429#include "version.h"
Vikrant Varmae5618102013-05-04 00:04:1910#include "refs.h"
Christian Couder64949982008-03-07 07:46:2811
Miklos Vajna940208a2008-07-29 23:16:5812void add_cmdname(struct cmdnames *cmds, const char *name, int len)
Linus Torvalds70827b12006-04-21 17:27:3413{
Jeff King96ffc062016-02-22 22:44:3214 struct cmdname *ent;
15 FLEX_ALLOC_MEM(ent, name, name, len);
Linus Torvalds70827b12006-04-21 17:27:3416 ent->len = len;
Scott R Parish1eb05692007-10-29 03:30:5217
18 ALLOC_GROW(cmds->names, cmds->cnt + 1, cmds->alloc);
19 cmds->names[cmds->cnt++] = ent;
Linus Torvalds70827b12006-04-21 17:27:3420}
21
Johannes Schindelin8af84da2008-08-31 13:50:2322static void clean_cmdnames(struct cmdnames *cmds)
23{
24 int i;
25 for (i = 0; i < cmds->cnt; ++i)
26 free(cmds->names[i]);
27 free(cmds->names);
28 cmds->cnt = 0;
29 cmds->alloc = 0;
30}
31
Linus Torvalds70827b12006-04-21 17:27:3432static int cmdname_compare(const void *a_, const void *b_)
33{
34 struct cmdname *a = *(struct cmdname **)a_;
35 struct cmdname *b = *(struct cmdname **)b_;
36 return strcmp(a->name, b->name);
37}
38
Scott R Parish1eb05692007-10-29 03:30:5239static void uniq(struct cmdnames *cmds)
40{
41 int i, j;
42
43 if (!cmds->cnt)
44 return;
45
Jeff King4a157582012-07-25 16:16:1946 for (i = j = 1; i < cmds->cnt; i++) {
47 if (!strcmp(cmds->names[i]->name, cmds->names[j-1]->name))
48 free(cmds->names[i]);
49 else
Scott R Parish1eb05692007-10-29 03:30:5250 cmds->names[j++] = cmds->names[i];
Jeff King4a157582012-07-25 16:16:1951 }
Scott R Parish1eb05692007-10-29 03:30:5252
53 cmds->cnt = j;
54}
55
Miklos Vajna940208a2008-07-29 23:16:5856void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
Junio C Hamanof3fa1832007-11-08 23:35:3257{
Scott R Parish1eb05692007-10-29 03:30:5258 int ci, cj, ei;
59 int cmp;
60
61 ci = cj = ei = 0;
62 while (ci < cmds->cnt && ei < excludes->cnt) {
63 cmp = strcmp(cmds->names[ci]->name, excludes->names[ei]->name);
64 if (cmp < 0)
65 cmds->names[cj++] = cmds->names[ci++];
Junio C Hamano6a17f582012-07-25 18:01:1266 else if (cmp == 0) {
67 ei++;
68 free(cmds->names[ci++]);
69 } else if (cmp > 0)
Scott R Parish1eb05692007-10-29 03:30:5270 ei++;
71 }
72
73 while (ci < cmds->cnt)
74 cmds->names[cj++] = cmds->names[ci++];
75
76 cmds->cnt = cj;
77}
78
Ralf Thielowd10cb3d2014-02-28 19:27:2979static void pretty_print_cmdnames(struct cmdnames *cmds, unsigned int colopts)
Linus Torvalds70827b12006-04-21 17:27:3480{
Nguyễn Thái Ngọc Duydbfae682012-04-13 10:54:3781 struct string_list list = STRING_LIST_INIT_NODUP;
82 struct column_options copts;
83 int i;
Linus Torvalds70827b12006-04-21 17:27:3484
Nguyễn Thái Ngọc Duydbfae682012-04-13 10:54:3785 for (i = 0; i < cmds->cnt; i++)
86 string_list_append(&list, cmds->names[i]->name);
87 /*
88 * always enable column display, we only consult column.*
89 * about layout strategy and stuff
90 */
91 colopts = (colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
92 memset(&copts, 0, sizeof(copts));
93 copts.indent = " ";
94 copts.padding = 2;
95 print_columns(&list, colopts, &copts);
96 string_list_clear(&list, 0);
Linus Torvalds70827b12006-04-21 17:27:3497}
98
Johannes Sixtcc3b7a92008-01-14 13:05:3399static int is_executable(const char *name)
100{
101 struct stat st;
102
103 if (stat(name, &st) || /* stat, not lstat */
104 !S_ISREG(st.st_mode))
105 return 0;
106
Ramsay Jonesf66450a2013-06-22 19:42:47107#if defined(GIT_WINDOWS_NATIVE)
Frank Li0d30ad72009-09-16 08:20:17108{ /* cannot trust the executable bit, peek into the file instead */
Johannes Sixtcc3b7a92008-01-14 13:05:33109 char buf[3] = { 0 };
110 int n;
111 int fd = open(name, O_RDONLY);
112 st.st_mode &= ~S_IXUSR;
113 if (fd >= 0) {
114 n = read(fd, buf, 2);
115 if (n == 2)
116 /* DOS executables start with "MZ" */
117 if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
118 st.st_mode |= S_IXUSR;
119 close(fd);
120 }
Frank Li0d30ad72009-09-16 08:20:17121}
Johannes Sixtcc3b7a92008-01-14 13:05:33122#endif
123 return st.st_mode & S_IXUSR;
124}
125
Alex Riesene3211802008-08-28 17:15:33126static void list_commands_in_dir(struct cmdnames *cmds,
Miklos Vajna940208a2008-07-29 23:16:58127 const char *path,
128 const char *prefix)
Linus Torvalds70827b12006-04-21 17:27:34129{
Scott R Parish1eb05692007-10-29 03:30:52130 DIR *dir = opendir(path);
Linus Torvalds70827b12006-04-21 17:27:34131 struct dirent *de;
Johannes Schindelinf5d600e2008-07-27 20:34:14132 struct strbuf buf = STRBUF_INIT;
133 int len;
Linus Torvalds70827b12006-04-21 17:27:34134
Johannes Schindelinf5d600e2008-07-27 20:34:14135 if (!dir)
Alex Riesene3211802008-08-28 17:15:33136 return;
Miklos Vajna940208a2008-07-29 23:16:58137 if (!prefix)
138 prefix = "git-";
Linus Torvalds70827b12006-04-21 17:27:34139
Johannes Schindelinf5d600e2008-07-27 20:34:14140 strbuf_addf(&buf, "%s/", path);
141 len = buf.len;
142
Linus Torvalds70827b12006-04-21 17:27:34143 while ((de = readdir(dir)) != NULL) {
Jeff Kingde8118e2014-06-18 19:57:17144 const char *ent;
Jeff King26936bf2014-06-30 16:58:51145 size_t entlen;
Linus Torvalds70827b12006-04-21 17:27:34146
Jeff Kingde8118e2014-06-18 19:57:17147 if (!skip_prefix(de->d_name, prefix, &ent))
Linus Torvalds70827b12006-04-21 17:27:34148 continue;
Scott R Parish09660032007-10-27 08:36:52149
Johannes Schindelinf5d600e2008-07-27 20:34:14150 strbuf_setlen(&buf, len);
151 strbuf_addstr(&buf, de->d_name);
152 if (!is_executable(buf.buf))
Linus Torvalds70827b12006-04-21 17:27:34153 continue;
154
Jeff Kingde8118e2014-06-18 19:57:17155 entlen = strlen(ent);
Junio C Hamano6e409472014-07-16 18:25:59156 strip_suffix(ent, ".exe", &entlen);
Linus Torvalds70827b12006-04-21 17:27:34157
Jeff Kingde8118e2014-06-18 19:57:17158 add_cmdname(cmds, ent, entlen);
Linus Torvalds70827b12006-04-21 17:27:34159 }
160 closedir(dir);
Johannes Schindelinf5d600e2008-07-27 20:34:14161 strbuf_release(&buf);
Scott R Parish1eb05692007-10-29 03:30:52162}
163
Alex Riesene3211802008-08-28 17:15:33164void load_command_list(const char *prefix,
Miklos Vajna940208a2008-07-29 23:16:58165 struct cmdnames *main_cmds,
166 struct cmdnames *other_cmds)
Scott R Parish1eb05692007-10-29 03:30:52167{
Scott R Parish1eb05692007-10-29 03:30:52168 const char *env_path = getenv("PATH");
Scott R Parish1eb05692007-10-29 03:30:52169 const char *exec_path = git_exec_path();
170
Alex Riesen1f08e5c2008-08-28 17:19:07171 if (exec_path) {
Alex Riesene3211802008-08-28 17:15:33172 list_commands_in_dir(main_cmds, exec_path, prefix);
René Scharfe9ed0d8d2016-09-29 15:27:31173 QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
Alex Riesen1f08e5c2008-08-28 17:19:07174 uniq(main_cmds);
Scott R Parish1eb05692007-10-29 03:30:52175 }
176
Alex Riesen1f08e5c2008-08-28 17:19:07177 if (env_path) {
178 char *paths, *path, *colon;
179 path = paths = xstrdup(env_path);
180 while (1) {
181 if ((colon = strchr(path, PATH_SEP)))
182 *colon = 0;
Pieter de Bie746c2212008-09-10 15:54:28183 if (!exec_path || strcmp(path, exec_path))
184 list_commands_in_dir(other_cmds, path, prefix);
Scott R Parish1eb05692007-10-29 03:30:52185
Alex Riesen1f08e5c2008-08-28 17:19:07186 if (!colon)
187 break;
188 path = colon + 1;
189 }
190 free(paths);
191
René Scharfe9ed0d8d2016-09-29 15:27:31192 QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
Alex Riesen1f08e5c2008-08-28 17:19:07193 uniq(other_cmds);
Scott R Parish1eb05692007-10-29 03:30:52194 }
Miklos Vajna940208a2008-07-29 23:16:58195 exclude_cmds(other_cmds, main_cmds);
Jeff King21564352008-02-24 22:17:37196}
197
Junio C Hamanof4ed0af2012-05-03 22:13:31198void list_commands(unsigned int colopts,
Nguyễn Thái Ngọc Duydbfae682012-04-13 10:54:37199 struct cmdnames *main_cmds, struct cmdnames *other_cmds)
Jeff King21564352008-02-24 22:17:37200{
Miklos Vajna940208a2008-07-29 23:16:58201 if (main_cmds->cnt) {
Alex Riesen61c5d432008-08-28 17:19:42202 const char *exec_path = git_exec_path();
Nguyễn Thái Ngọc Duy4470ef92012-04-25 11:21:53203 printf_ln(_("available git commands in '%s'"), exec_path);
Scott R Parish1eb05692007-10-29 03:30:52204 putchar('\n');
Ralf Thielowd10cb3d2014-02-28 19:27:29205 pretty_print_cmdnames(main_cmds, colopts);
Scott R Parish1eb05692007-10-29 03:30:52206 putchar('\n');
207 }
208
Miklos Vajna940208a2008-07-29 23:16:58209 if (other_cmds->cnt) {
Nguyễn Thái Ngọc Duy4470ef92012-04-25 11:21:53210 printf_ln(_("git commands available from elsewhere on your $PATH"));
Miklos Vajna940208a2008-07-29 23:16:58211 putchar('\n');
Ralf Thielowd10cb3d2014-02-28 19:27:29212 pretty_print_cmdnames(other_cmds, colopts);
Scott R Parish1eb05692007-10-29 03:30:52213 putchar('\n');
214 }
Linus Torvalds70827b12006-04-21 17:27:34215}
216
Sébastien Guimmara22414772015-05-21 17:39:22217static int cmd_group_cmp(const void *elem1, const void *elem2)
218{
219 const struct cmdname_help *e1 = elem1;
220 const struct cmdname_help *e2 = elem2;
221
222 if (e1->group < e2->group)
223 return -1;
224 if (e1->group > e2->group)
225 return 1;
226 return strcmp(e1->name, e2->name);
227}
228
Junio C Hamano1542d4c2013-01-19 06:35:04229void list_common_cmds_help(void)
230{
231 int i, longest = 0;
Sébastien Guimmara22414772015-05-21 17:39:22232 int current_grp = -1;
Junio C Hamano1542d4c2013-01-19 06:35:04233
234 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
235 if (longest < strlen(common_cmds[i].name))
236 longest = strlen(common_cmds[i].name);
237 }
238
René Scharfe9ed0d8d2016-09-29 15:27:31239 QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
Sébastien Guimmara22414772015-05-21 17:39:22240
241 puts(_("These are common Git commands used in various situations:"));
242
Junio C Hamano1542d4c2013-01-19 06:35:04243 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
Sébastien Guimmara22414772015-05-21 17:39:22244 if (common_cmds[i].group != current_grp) {
245 printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
246 current_grp = common_cmds[i].group;
247 }
248
Junio C Hamano1542d4c2013-01-19 06:35:04249 printf(" %s ", common_cmds[i].name);
250 mput_char(' ', longest - strlen(common_cmds[i].name));
251 puts(_(common_cmds[i].help));
252 }
253}
254
Miklos Vajna940208a2008-07-29 23:16:58255int is_in_cmdlist(struct cmdnames *c, const char *s)
Jeff King21564352008-02-24 22:17:37256{
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
Alex Riesenf0e90712008-08-31 13:54:58264static int autocorrect;
Pieter de Bie746c2212008-09-10 15:54:28265static struct cmdnames aliases;
Alex Riesenf0e90712008-08-31 13:54:58266
267static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
Ramsay Allan Jones822a7d52006-07-30 21:42:25268{
Jeff Kingae021d82014-06-18 19:47:50269 const char *p;
270
Alex Riesenf0e90712008-08-31 13:54:58271 if (!strcmp(var, "help.autocorrect"))
272 autocorrect = git_config_int(var,value);
Pieter de Bie746c2212008-09-10 15:54:28273 /* Also use aliases for command lookup */
Jeff Kingae021d82014-06-18 19:47:50274 if (skip_prefix(var, "alias.", &p))
275 add_cmdname(&aliases, p, strlen(p));
Alex Riesenf0e90712008-08-31 13:54:58276
277 return git_default_config(var, value, cb);
278}
279
Johannes Schindelin8af84da2008-08-31 13:50:23280static int levenshtein_compare(const void *p1, const void *p2)
Ramsay Allan Jones822a7d52006-07-30 21:42:25281{
Johannes Schindelin8af84da2008-08-31 13:50:23282 const struct cmdname *const *c1 = p1, *const *c2 = p2;
283 const char *s1 = (*c1)->name, *s2 = (*c2)->name;
284 int l1 = (*c1)->len;
285 int l2 = (*c2)->len;
286 return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
287}
288
Pieter de Bie746c2212008-09-10 15:54:28289static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
290{
291 int i;
292 ALLOC_GROW(cmds->names, cmds->cnt + old->cnt, cmds->alloc);
293
294 for (i = 0; i < old->cnt; i++)
295 cmds->names[cmds->cnt++] = old->names[i];
296 free(old->names);
297 old->cnt = 0;
298 old->names = NULL;
299}
300
Johannes Sixt06500a02009-12-15 07:57:18301/* An empirically derived magic number */
Erik Faye-Lund6612b9e2010-11-26 16:00:39302#define SIMILARITY_FLOOR 7
303#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR)
Johannes Sixt06500a02009-12-15 07:57:18304
Michael Schubert823e0de2011-07-08 10:08:49305static const char bad_interpreter_advice[] =
306 N_("'%s' appears to be a git command, but we were not\n"
307 "able to execute it. Maybe git-%s is broken?");
308
Johannes Schindelin8af84da2008-08-31 13:50:23309const char *help_unknown_cmd(const char *cmd)
310{
311 int i, n, best_similarity = 0;
312 struct cmdnames main_cmds, other_cmds;
313
314 memset(&main_cmds, 0, sizeof(main_cmds));
Johan Herland0b74f5d2009-08-11 10:10:21315 memset(&other_cmds, 0, sizeof(other_cmds));
Pieter de Bie746c2212008-09-10 15:54:28316 memset(&aliases, 0, sizeof(aliases));
Johannes Schindelin8af84da2008-08-31 13:50:23317
Alex Riesenf0e90712008-08-31 13:54:58318 git_config(git_unknown_cmd_config, NULL);
319
Johannes Schindelin8af84da2008-08-31 13:50:23320 load_command_list("git-", &main_cmds, &other_cmds);
321
Pieter de Bie746c2212008-09-10 15:54:28322 add_cmd_list(&main_cmds, &aliases);
323 add_cmd_list(&main_cmds, &other_cmds);
René Scharfe9ed0d8d2016-09-29 15:27:31324 QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
Pieter de Bie746c2212008-09-10 15:54:28325 uniq(&main_cmds);
Johannes Schindelin8af84da2008-08-31 13:50:23326
Erik Faye-Lund6612b9e2010-11-26 16:00:39327 /* This abuses cmdname->len for levenshtein distance */
328 for (i = 0, n = 0; i < main_cmds.cnt; i++) {
329 int cmp = 0; /* avoid compiler stupidity */
330 const char *candidate = main_cmds.names[i]->name;
331
Michael Schubert823e0de2011-07-08 10:08:49332 /*
333 * An exact match means we have the command, but
334 * for some reason exec'ing it gave us ENOENT; probably
335 * it's a bad interpreter in the #! line.
336 */
337 if (!strcmp(candidate, cmd))
338 die(_(bad_interpreter_advice), cmd, cmd);
339
Erik Faye-Lund6612b9e2010-11-26 16:00:39340 /* Does the candidate appear in common_cmds list? */
341 while (n < ARRAY_SIZE(common_cmds) &&
342 (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
343 n++;
344 if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
345 /* Yes, this is one of the common commands */
346 n++; /* use the entry from common_cmds[] */
Christian Couder59556542013-11-30 20:55:40347 if (starts_with(candidate, cmd)) {
Erik Faye-Lund6612b9e2010-11-26 16:00:39348 /* Give prefix match a very good score */
349 main_cmds.names[i]->len = 0;
350 continue;
351 }
352 }
353
Johannes Schindelin8af84da2008-08-31 13:50:23354 main_cmds.names[i]->len =
Matthieu Moyc41494f2012-05-27 16:02:58355 levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
Erik Faye-Lund6612b9e2010-11-26 16:00:39356 }
Johannes Schindelin8af84da2008-08-31 13:50:23357
René Scharfe9ed0d8d2016-09-29 15:27:31358 QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
Johannes Schindelin8af84da2008-08-31 13:50:23359
360 if (!main_cmds.cnt)
Nguyễn Thái Ngọc Duy96656272012-04-23 12:30:24361 die(_("Uh oh. Your system reports no Git commands at all."));
Johannes Schindelin8af84da2008-08-31 13:50:23362
Erik Faye-Lund6612b9e2010-11-26 16:00:39363 /* skip and count prefix matches */
364 for (n = 0; n < main_cmds.cnt && !main_cmds.names[n]->len; n++)
365 ; /* still counting */
366
367 if (main_cmds.cnt <= n) {
368 /* prefix matches with everything? that is too ambiguous */
369 best_similarity = SIMILARITY_FLOOR + 1;
370 } else {
371 /* count all the most similar ones */
372 for (best_similarity = main_cmds.names[n++]->len;
373 (n < main_cmds.cnt &&
374 best_similarity == main_cmds.names[n]->len);
375 n++)
376 ; /* still counting */
377 }
Johannes Sixt06500a02009-12-15 07:57:18378 if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
Johannes Schindelin8af84da2008-08-31 13:50:23379 const char *assumed = main_cmds.names[0]->name;
380 main_cmds.names[0] = NULL;
381 clean_cmdnames(&main_cmds);
Nguyễn Thái Ngọc Duy96656272012-04-23 12:30:24382 fprintf_ln(stderr,
383 _("WARNING: You called a Git command named '%s', "
384 "which does not exist.\n"
385 "Continuing under the assumption that you meant '%s'"),
Johannes Schindelin8af84da2008-08-31 13:50:23386 cmd, assumed);
Alex Riesenf0e90712008-08-31 13:54:58387 if (autocorrect > 0) {
Nguyễn Thái Ngọc Duy96656272012-04-23 12:30:24388 fprintf_ln(stderr, _("in %0.1f seconds automatically..."),
Alex Riesenf0e90712008-08-31 13:54:58389 (float)autocorrect/10.0);
Johannes Sixt2024d312015-06-05 19:45:05390 sleep_millisec(autocorrect * 100);
Alex Riesenf0e90712008-08-31 13:54:58391 }
Johannes Schindelin8af84da2008-08-31 13:50:23392 return assumed;
393 }
394
Nguyễn Thái Ngọc Duy96656272012-04-23 12:30:24395 fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
Johannes Schindelin8af84da2008-08-31 13:50:23396
Johannes Sixt06500a02009-12-15 07:57:18397 if (SIMILAR_ENOUGH(best_similarity)) {
Nguyễn Thái Ngọc Duy96656272012-04-23 12:30:24398 fprintf_ln(stderr,
399 Q_("\nDid you mean this?",
400 "\nDid you mean one of these?",
401 n));
Johannes Schindelin8af84da2008-08-31 13:50:23402
403 for (i = 0; i < n; i++)
404 fprintf(stderr, "\t%s\n", main_cmds.names[i]->name);
405 }
406
Ramsay Allan Jones822a7d52006-07-30 21:42:25407 exit(1);
408}
409
Linus Torvaldsa633fca2006-07-29 05:44:25410int cmd_version(int argc, const char **argv, const char *prefix)
Linus Torvalds70827b12006-04-21 17:27:34411{
David Aguilarf2de0b92013-04-16 20:33:25412 /*
413 * The format of this string should be kept stable for compatibility
414 * with external projects that rely on the output of "git version".
415 */
Linus Torvalds70827b12006-04-21 17:27:34416 printf("git version %s\n", git_version_string);
Jeff King6b9c38e2016-07-11 23:54:18417 while (*++argv) {
418 if (!strcmp(*argv, "--build-options")) {
419 printf("sizeof-long: %d\n", (int)sizeof(long));
420 /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
421 }
422 }
Linus Torvalds70827b12006-04-21 17:27:34423 return 0;
424}
Vikrant Varmae5618102013-05-04 00:04:19425
426struct similar_ref_cb {
427 const char *base_ref;
428 struct string_list *similar_refs;
429};
430
Michael Haggerty91d6e942015-05-25 18:38:54431static int append_similar_ref(const char *refname, const struct object_id *oid,
Vikrant Varmae5618102013-05-04 00:04:19432 int flags, void *cb_data)
433{
434 struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
435 char *branch = strrchr(refname, '/') + 1;
Jeff King95b567c2014-06-18 19:48:29436 const char *remote;
437
Vikrant Varmae5618102013-05-04 00:04:19438 /* A remote branch of the same name is deemed similar */
Jeff King95b567c2014-06-18 19:48:29439 if (skip_prefix(refname, "refs/remotes/", &remote) &&
Vikrant Varmae5618102013-05-04 00:04:19440 !strcmp(branch, cb->base_ref))
Jeff King95b567c2014-06-18 19:48:29441 string_list_append(cb->similar_refs, remote);
Vikrant Varmae5618102013-05-04 00:04:19442 return 0;
443}
444
445static struct string_list guess_refs(const char *ref)
446{
447 struct similar_ref_cb ref_cb;
448 struct string_list similar_refs = STRING_LIST_INIT_NODUP;
449
450 ref_cb.base_ref = ref;
451 ref_cb.similar_refs = &similar_refs;
452 for_each_ref(append_similar_ref, &ref_cb);
453 return similar_refs;
454}
455
456void help_unknown_ref(const char *ref, const char *cmd, const char *error)
457{
458 int i;
459 struct string_list suggested_refs = guess_refs(ref);
460
461 fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error);
462
463 if (suggested_refs.nr > 0) {
464 fprintf_ln(stderr,
465 Q_("\nDid you mean this?",
466 "\nDid you mean one of these?",
467 suggested_refs.nr));
468 for (i = 0; i < suggested_refs.nr; i++)
469 fprintf(stderr, "\t%s\n", suggested_refs.items[i].string);
470 }
471
472 string_list_clear(&suggested_refs, 0);
473 exit(1);
474}