git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / oidset.c
| 1 | #include "git-compat-util.h" |
| 2 | #include "oidset.h" |
| 3 | #include "hex.h" |
| 4 | #include "strbuf.h" |
| 5 | |
| 6 | void oidset_init(struct oidset *set, size_t initial_size) |
| 7 | { |
| 8 | memset(&set->set, 0, sizeof(set->set)); |
| 9 | if (initial_size) |
| 10 | kh_resize_oid_set(&set->set, initial_size); |
| 11 | } |
| 12 | |
| 13 | int oidset_contains(const struct oidset *set, const struct object_id *oid) |
| 14 | { |
| 15 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
| 16 | return pos != kh_end(&set->set); |
| 17 | } |
| 18 | |
| 19 | bool oidset_equal(const struct oidset *a, const struct oidset *b) |
| 20 | { |
| 21 | struct oidset_iter iter; |
| 22 | struct object_id *a_oid; |
| 23 | |
| 24 | if (oidset_size(a) != oidset_size(b)) |
| 25 | return false; |
| 26 | |
| 27 | oidset_iter_init(a, &iter); |
| 28 | while ((a_oid = oidset_iter_next(&iter))) |
| 29 | if (!oidset_contains(b, a_oid)) |
| 30 | return false; |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | int oidset_insert(struct oidset *set, const struct object_id *oid) |
| 36 | { |
| 37 | int added; |
| 38 | kh_put_oid_set(&set->set, *oid, &added); |
| 39 | return !added; |
| 40 | } |
| 41 | |
| 42 | void oidset_insert_from_set(struct oidset *dest, struct oidset *src) |
| 43 | { |
| 44 | struct oidset_iter iter; |
| 45 | struct object_id *src_oid; |
| 46 | |
| 47 | oidset_iter_init(src, &iter); |
| 48 | while ((src_oid = oidset_iter_next(&iter))) |
| 49 | oidset_insert(dest, src_oid); |
| 50 | } |
| 51 | |
| 52 | int oidset_remove(struct oidset *set, const struct object_id *oid) |
| 53 | { |
| 54 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
| 55 | if (pos == kh_end(&set->set)) |
| 56 | return 0; |
| 57 | kh_del_oid_set(&set->set, pos); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | void oidset_clear(struct oidset *set) |
| 62 | { |
| 63 | kh_release_oid_set(&set->set); |
| 64 | oidset_init(set, 0); |
| 65 | } |
| 66 | |
| 67 | void oidset_parse_file(struct oidset *set, const char *path, |
| 68 | const struct git_hash_algo *algop) |
| 69 | { |
| 70 | oidset_parse_file_carefully(set, path, algop, NULL, NULL); |
| 71 | } |
| 72 | |
| 73 | static void parse_oidset_line(struct oidset *set, struct strbuf *sb, |
| 74 | const struct git_hash_algo *algop, |
| 75 | oidset_parse_tweak_fn fn, void *cbdata) |
| 76 | { |
| 77 | const char *p; |
| 78 | const char *name; |
| 79 | struct object_id oid; |
| 80 | |
| 81 | if (memchr(sb->buf, '\0', sb->len)) |
| 82 | die("invalid object name: %s", sb->buf); |
| 83 | |
| 84 | /* |
| 85 | * Allow trailing comments, leading whitespace |
| 86 | * (including before commits), and empty or whitespace |
| 87 | * only lines. |
| 88 | */ |
| 89 | name = strchr(sb->buf, '#'); |
| 90 | if (name) |
| 91 | strbuf_setlen(sb, name - sb->buf); |
| 92 | strbuf_trim(sb); |
| 93 | if (!sb->len) |
| 94 | return; |
| 95 | |
| 96 | if (parse_oid_hex_algop(sb->buf, &oid, &p, algop) || *p != '\0') |
| 97 | die("invalid object name: %s", sb->buf); |
| 98 | if (fn && fn(&oid, cbdata)) |
| 99 | return; |
| 100 | oidset_insert(set, &oid); |
| 101 | } |
| 102 | |
| 103 | void oidset_parse_file_carefully(struct oidset *set, const char *path, |
| 104 | const struct git_hash_algo *algop, |
| 105 | oidset_parse_tweak_fn fn, void *cbdata) |
| 106 | { |
| 107 | FILE *fp; |
| 108 | struct strbuf sb = STRBUF_INIT; |
| 109 | |
| 110 | fp = fopen(path, "r"); |
| 111 | if (!fp) |
| 112 | die("could not open object name list: %s", path); |
| 113 | while (!strbuf_getline(&sb, fp)) |
| 114 | parse_oidset_line(set, &sb, algop, fn, cbdata); |
| 115 | if (ferror(fp)) |
| 116 | die_errno("Could not read '%s'", path); |
| 117 | fclose(fp); |
| 118 | strbuf_release(&sb); |
| 119 | } |
| 120 | |
| 121 | void oidset_parse_buffer_carefully(struct oidset *set, const char *buf, |
| 122 | size_t size, |
| 123 | const struct git_hash_algo *algop, |
| 124 | oidset_parse_tweak_fn fn, void *cbdata) |
| 125 | { |
| 126 | struct strbuf sb = STRBUF_INIT; |
| 127 | const char *p = buf, *end; |
| 128 | |
| 129 | if (!size) |
| 130 | return; |
| 131 | end = buf + size; |
| 132 | |
| 133 | while (p < end) { |
| 134 | const char *nl = memchr(p, '\n', end - p); |
| 135 | size_t len = (nl ? nl : end) - p; |
| 136 | |
| 137 | strbuf_reset(&sb); |
| 138 | if (len && p[len - 1] == '\r') |
| 139 | len--; |
| 140 | strbuf_add(&sb, p, len); |
| 141 | parse_oidset_line(set, &sb, algop, fn, cbdata); |
| 142 | p = nl ? nl + 1 : end; |
| 143 | } |
| 144 | strbuf_release(&sb); |
| 145 | } |