|
7 | 7 | #include "string-list.h"
|
8 | 8 | #include "column.h"
|
9 | 9 | #include "version.h"
|
| 10 | +#include "refs.h" |
10 | 11 |
|
11 | 12 | void add_cmdname(struct cmdnames *cmds, const char *name, int len)
|
12 | 13 | {
|
@@ -404,3 +405,52 @@ int cmd_version(int argc, const char **argv, const char *prefix)
|
404 | 405 | printf("git version %s\n", git_version_string);
|
405 | 406 | return 0;
|
406 | 407 | }
|
| 408 | + |
| 409 | +struct similar_ref_cb { |
| 410 | + const char *base_ref; |
| 411 | + struct string_list *similar_refs; |
| 412 | +}; |
| 413 | + |
| 414 | +static int append_similar_ref(const char *refname, const unsigned char *sha1, |
| 415 | + int flags, void *cb_data) |
| 416 | +{ |
| 417 | + struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data); |
| 418 | + char *branch = strrchr(refname, '/') + 1; |
| 419 | + /* A remote branch of the same name is deemed similar */ |
| 420 | + if (!prefixcmp(refname, "refs/remotes/") && |
| 421 | + !strcmp(branch, cb->base_ref)) |
| 422 | + string_list_append(cb->similar_refs, |
| 423 | + refname + strlen("refs/remotes/")); |
| 424 | + return 0; |
| 425 | +} |
| 426 | + |
| 427 | +static struct string_list guess_refs(const char *ref) |
| 428 | +{ |
| 429 | + struct similar_ref_cb ref_cb; |
| 430 | + struct string_list similar_refs = STRING_LIST_INIT_NODUP; |
| 431 | + |
| 432 | + ref_cb.base_ref = ref; |
| 433 | + ref_cb.similar_refs = &similar_refs; |
| 434 | + for_each_ref(append_similar_ref, &ref_cb); |
| 435 | + return similar_refs; |
| 436 | +} |
| 437 | + |
| 438 | +void help_unknown_ref(const char *ref, const char *cmd, const char *error) |
| 439 | +{ |
| 440 | + int i; |
| 441 | + struct string_list suggested_refs = guess_refs(ref); |
| 442 | + |
| 443 | + fprintf_ln(stderr, _("%s: %s - %s"), cmd, ref, error); |
| 444 | + |
| 445 | + if (suggested_refs.nr > 0) { |
| 446 | + fprintf_ln(stderr, |
| 447 | + Q_("\nDid you mean this?", |
| 448 | + "\nDid you mean one of these?", |
| 449 | + suggested_refs.nr)); |
| 450 | + for (i = 0; i < suggested_refs.nr; i++) |
| 451 | + fprintf(stderr, "\t%s\n", suggested_refs.items[i].string); |
| 452 | + } |
| 453 | + |
| 454 | + string_list_clear(&suggested_refs, 0); |
| 455 | + exit(1); |
| 456 | +} |
0 commit comments