]>
Commit | Line | Data |
---|---|---|
b73ecb48 | 1 | #include "git-compat-util.h" |
29c2bd5f | 2 | #include "oidset.h" |
b73ecb48 EN |
3 | #include "hex.h" |
4 | #include "strbuf.h" | |
29c2bd5f | 5 | |
8c84ae65 RS |
6 | void oidset_init(struct oidset *set, size_t initial_size) |
7 | { | |
8 | memset(&set->set, 0, sizeof(set->set)); | |
9 | if (initial_size) | |
8fbb558a | 10 | kh_resize_oid_set(&set->set, initial_size); |
8c84ae65 RS |
11 | } |
12 | ||
29c2bd5f JK |
13 | int oidset_contains(const struct oidset *set, const struct object_id *oid) |
14 | { | |
8fbb558a | 15 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
8b2f8cbc | 16 | return pos != kh_end(&set->set); |
29c2bd5f JK |
17 | } |
18 | ||
19 | int oidset_insert(struct oidset *set, const struct object_id *oid) | |
20 | { | |
8b2f8cbc | 21 | int added; |
8fbb558a | 22 | kh_put_oid_set(&set->set, *oid, &added); |
8b2f8cbc | 23 | return !added; |
29c2bd5f JK |
24 | } |
25 | ||
eaf07b7d CC |
26 | void oidset_insert_from_set(struct oidset *dest, struct oidset *src) |
27 | { | |
28 | struct oidset_iter iter; | |
29 | struct object_id *src_oid; | |
30 | ||
31 | oidset_iter_init(src, &iter); | |
32 | while ((src_oid = oidset_iter_next(&iter))) | |
33 | oidset_insert(dest, src_oid); | |
34 | } | |
35 | ||
c3a9ad31 JH |
36 | int oidset_remove(struct oidset *set, const struct object_id *oid) |
37 | { | |
8fbb558a | 38 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
8b2f8cbc RS |
39 | if (pos == kh_end(&set->set)) |
40 | return 0; | |
8fbb558a | 41 | kh_del_oid_set(&set->set, pos); |
8b2f8cbc | 42 | return 1; |
c3a9ad31 JH |
43 | } |
44 | ||
29c2bd5f JK |
45 | void oidset_clear(struct oidset *set) |
46 | { | |
8fbb558a | 47 | kh_release_oid_set(&set->set); |
8b2f8cbc | 48 | oidset_init(set, 0); |
29c2bd5f | 49 | } |
f93895f8 BR |
50 | |
51 | void oidset_parse_file(struct oidset *set, const char *path) | |
610e2b92 JH |
52 | { |
53 | oidset_parse_file_carefully(set, path, NULL, NULL); | |
54 | } | |
55 | ||
56 | void oidset_parse_file_carefully(struct oidset *set, const char *path, | |
57 | oidset_parse_tweak_fn fn, void *cbdata) | |
f93895f8 BR |
58 | { |
59 | FILE *fp; | |
60 | struct strbuf sb = STRBUF_INIT; | |
61 | struct object_id oid; | |
62 | ||
63 | fp = fopen(path, "r"); | |
64 | if (!fp) | |
65 | die("could not open object name list: %s", path); | |
66 | while (!strbuf_getline(&sb, fp)) { | |
67 | const char *p; | |
68 | const char *name; | |
69 | ||
70 | /* | |
71 | * Allow trailing comments, leading whitespace | |
72 | * (including before commits), and empty or whitespace | |
73 | * only lines. | |
74 | */ | |
75 | name = strchr(sb.buf, '#'); | |
76 | if (name) | |
77 | strbuf_setlen(&sb, name - sb.buf); | |
78 | strbuf_trim(&sb); | |
79 | if (!sb.len) | |
80 | continue; | |
81 | ||
c714d058 | 82 | if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0') |
f93895f8 | 83 | die("invalid object name: %s", sb.buf); |
c714d058 RS |
84 | if (fn && fn(&oid, cbdata)) |
85 | continue; | |
f93895f8 BR |
86 | oidset_insert(set, &oid); |
87 | } | |
88 | if (ferror(fp)) | |
89 | die_errno("Could not read '%s'", path); | |
90 | fclose(fp); | |
91 | strbuf_release(&sb); | |
92 | } |