|
| 1 | +#define USE_THE_REPOSITORY_VARIABLE |
| 2 | + |
| 3 | +#include "git-compat-util.h" |
| 4 | +#include "branch-suggestions.h" |
| 5 | +#include "refs.h" |
| 6 | +#include "string-list.h" |
| 7 | +#include "levenshtein.h" |
| 8 | +#include "gettext.h" |
| 9 | +#include "repository.h" |
| 10 | +#include "strbuf.h" |
| 11 | + |
| 12 | +#define SIMILARITY_FLOOR 7 |
| 13 | +#define SIMILAR_ENOUGH(x) ((x) < SIMILARITY_FLOOR) |
| 14 | +#define MAX_SUGGESTIONS 5 |
| 15 | + |
| 16 | +struct branch_suggestion_cb { |
| 17 | + const char *attempted_name; |
| 18 | + struct string_list *suggestions; |
| 19 | +}; |
| 20 | + |
| 21 | +static int collect_branch_cb(const char *refname, const char *referent UNUSED, |
| 22 | + const struct object_id *oid UNUSED, |
| 23 | + int flags UNUSED, void *cb_data) |
| 24 | +{ |
| 25 | + struct branch_suggestion_cb *cb = cb_data; |
| 26 | + const char *branch_name; |
| 27 | + |
| 28 | + /* Since we're using refs_for_each_ref_in with "refs/heads/", |
| 29 | + * the refname might already be stripped or might still have the prefix */ |
| 30 | + if (starts_with(refname, "refs/heads/")) { |
| 31 | + branch_name = refname + strlen("refs/heads/"); |
| 32 | + } else { |
| 33 | + branch_name = refname; |
| 34 | + } |
| 35 | + |
| 36 | + /* Skip the attempted name itself */ |
| 37 | + if (!strcmp(branch_name, cb->attempted_name)) |
| 38 | + return 0; |
| 39 | + |
| 40 | + string_list_append(cb->suggestions, branch_name); |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +void suggest_similar_branch_names(const char *attempted_name) |
| 45 | +{ |
| 46 | + struct string_list branches = STRING_LIST_INIT_DUP; |
| 47 | + struct branch_suggestion_cb cb_data; |
| 48 | + size_t i; |
| 49 | + int best_similarity = INT_MAX; |
| 50 | + int suggestion_count = 0; |
| 51 | + |
| 52 | + cb_data.attempted_name = attempted_name; |
| 53 | + cb_data.suggestions = &branches; |
| 54 | + |
| 55 | + /* Collect all local branch names */ |
| 56 | + refs_for_each_ref_in(get_main_ref_store(the_repository), "refs/heads/", |
| 57 | + collect_branch_cb, &cb_data); |
| 58 | + |
| 59 | + if (!branches.nr) |
| 60 | + goto cleanup; |
| 61 | + |
| 62 | + /* Calculate Levenshtein distances */ |
| 63 | + for (i = 0; i < branches.nr; i++) { |
| 64 | + const char *branch_name = branches.items[i].string; |
| 65 | + int distance; |
| 66 | + |
| 67 | + /* Give prefix matches a very good score */ |
| 68 | + if (starts_with(branch_name, attempted_name)) { |
| 69 | + distance = 0; |
| 70 | + } else { |
| 71 | + distance = levenshtein(attempted_name, branch_name, 0, 2, 1, 3); |
| 72 | + } |
| 73 | + |
| 74 | + branches.items[i].util = (void *)(intptr_t)distance; |
| 75 | + |
| 76 | + if (distance < best_similarity) |
| 77 | + best_similarity = distance; |
| 78 | + } |
| 79 | + |
| 80 | + /* Only show suggestions if they're similar enough */ |
| 81 | + if (!SIMILAR_ENOUGH(best_similarity)) |
| 82 | + goto cleanup; |
| 83 | + |
| 84 | + /* Count and display similar branches */ |
| 85 | + for (i = 0; i < branches.nr && suggestion_count < MAX_SUGGESTIONS; i++) { |
| 86 | + int distance = (int)(intptr_t)branches.items[i].util; |
| 87 | + |
| 88 | + if (distance <= best_similarity && SIMILAR_ENOUGH(distance)) { |
| 89 | + if (suggestion_count == 0) { |
| 90 | + fprintf(stderr, "%s\n", _("hint: Did you mean one of these?")); |
| 91 | + } |
| 92 | + fprintf(stderr, "hint: %s\n", branches.items[i].string); |
| 93 | + suggestion_count++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +cleanup: |
| 98 | + string_list_clear(&branches, 0); |
| 99 | +} |
0 commit comments