]>
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 | ||
c3a9ad31 JH |
26 | int oidset_remove(struct oidset *set, const struct object_id *oid) |
27 | { | |
8fbb558a | 28 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
8b2f8cbc RS |
29 | if (pos == kh_end(&set->set)) |
30 | return 0; | |
8fbb558a | 31 | kh_del_oid_set(&set->set, pos); |
8b2f8cbc | 32 | return 1; |
c3a9ad31 JH |
33 | } |
34 | ||
29c2bd5f JK |
35 | void oidset_clear(struct oidset *set) |
36 | { | |
8fbb558a | 37 | kh_release_oid_set(&set->set); |
8b2f8cbc | 38 | oidset_init(set, 0); |
29c2bd5f | 39 | } |
f93895f8 BR |
40 | |
41 | void oidset_parse_file(struct oidset *set, const char *path) | |
610e2b92 JH |
42 | { |
43 | oidset_parse_file_carefully(set, path, NULL, NULL); | |
44 | } | |
45 | ||
46 | void oidset_parse_file_carefully(struct oidset *set, const char *path, | |
47 | oidset_parse_tweak_fn fn, void *cbdata) | |
f93895f8 BR |
48 | { |
49 | FILE *fp; | |
50 | struct strbuf sb = STRBUF_INIT; | |
51 | struct object_id oid; | |
52 | ||
53 | fp = fopen(path, "r"); | |
54 | if (!fp) | |
55 | die("could not open object name list: %s", path); | |
56 | while (!strbuf_getline(&sb, fp)) { | |
57 | const char *p; | |
58 | const char *name; | |
59 | ||
60 | /* | |
61 | * Allow trailing comments, leading whitespace | |
62 | * (including before commits), and empty or whitespace | |
63 | * only lines. | |
64 | */ | |
65 | name = strchr(sb.buf, '#'); | |
66 | if (name) | |
67 | strbuf_setlen(&sb, name - sb.buf); | |
68 | strbuf_trim(&sb); | |
69 | if (!sb.len) | |
70 | continue; | |
71 | ||
c714d058 | 72 | if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0') |
f93895f8 | 73 | die("invalid object name: %s", sb.buf); |
c714d058 RS |
74 | if (fn && fn(&oid, cbdata)) |
75 | continue; | |
f93895f8 BR |
76 | oidset_insert(set, &oid); |
77 | } | |
78 | if (ferror(fp)) | |
79 | die_errno("Could not read '%s'", path); | |
80 | fclose(fp); | |
81 | strbuf_release(&sb); | |
82 | } |