Line | Count | Source |
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 | 0 | { |
8 | 0 | memset(&set->set, 0, sizeof(set->set)); |
9 | 0 | if (initial_size) |
10 | 0 | kh_resize_oid_set(&set->set, initial_size); |
11 | 0 | } |
12 | | |
13 | | int oidset_contains(const struct oidset *set, const struct object_id *oid) |
14 | 0 | { |
15 | 0 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
16 | 0 | return pos != kh_end(&set->set); |
17 | 0 | } |
18 | | |
19 | | bool oidset_equal(const struct oidset *a, const struct oidset *b) |
20 | 0 | { |
21 | 0 | struct oidset_iter iter; |
22 | 0 | struct object_id *a_oid; |
23 | |
|
24 | 0 | if (oidset_size(a) != oidset_size(b)) |
25 | 0 | return false; |
26 | | |
27 | 0 | oidset_iter_init(a, &iter); |
28 | 0 | while ((a_oid = oidset_iter_next(&iter))) |
29 | 0 | if (!oidset_contains(b, a_oid)) |
30 | 0 | return false; |
31 | | |
32 | 0 | return true; |
33 | 0 | } |
34 | | |
35 | | int oidset_insert(struct oidset *set, const struct object_id *oid) |
36 | 0 | { |
37 | 0 | int added; |
38 | 0 | kh_put_oid_set(&set->set, *oid, &added); |
39 | 0 | return !added; |
40 | 0 | } |
41 | | |
42 | | void oidset_insert_from_set(struct oidset *dest, struct oidset *src) |
43 | 0 | { |
44 | 0 | struct oidset_iter iter; |
45 | 0 | struct object_id *src_oid; |
46 | |
|
47 | 0 | oidset_iter_init(src, &iter); |
48 | 0 | while ((src_oid = oidset_iter_next(&iter))) |
49 | 0 | oidset_insert(dest, src_oid); |
50 | 0 | } |
51 | | |
52 | | int oidset_remove(struct oidset *set, const struct object_id *oid) |
53 | 0 | { |
54 | 0 | khiter_t pos = kh_get_oid_set(&set->set, *oid); |
55 | 0 | if (pos == kh_end(&set->set)) |
56 | 0 | return 0; |
57 | 0 | kh_del_oid_set(&set->set, pos); |
58 | 0 | return 1; |
59 | 0 | } |
60 | | |
61 | | void oidset_clear(struct oidset *set) |
62 | 0 | { |
63 | 0 | kh_release_oid_set(&set->set); |
64 | 0 | oidset_init(set, 0); |
65 | 0 | } |
66 | | |
67 | | void oidset_parse_file(struct oidset *set, const char *path, |
68 | | const struct git_hash_algo *algop) |
69 | 0 | { |
70 | 0 | oidset_parse_file_carefully(set, path, algop, NULL, NULL); |
71 | 0 | } |
72 | | |
73 | | void oidset_parse_file_carefully(struct oidset *set, const char *path, |
74 | | const struct git_hash_algo *algop, |
75 | | oidset_parse_tweak_fn fn, void *cbdata) |
76 | 0 | { |
77 | 0 | FILE *fp; |
78 | 0 | struct strbuf sb = STRBUF_INIT; |
79 | 0 | struct object_id oid; |
80 | |
|
81 | 0 | fp = fopen(path, "r"); |
82 | 0 | if (!fp) |
83 | 0 | die("could not open object name list: %s", path); |
84 | 0 | while (!strbuf_getline(&sb, fp)) { |
85 | 0 | const char *p; |
86 | 0 | const char *name; |
87 | | |
88 | | /* |
89 | | * Allow trailing comments, leading whitespace |
90 | | * (including before commits), and empty or whitespace |
91 | | * only lines. |
92 | | */ |
93 | 0 | name = strchr(sb.buf, '#'); |
94 | 0 | if (name) |
95 | 0 | strbuf_setlen(&sb, name - sb.buf); |
96 | 0 | strbuf_trim(&sb); |
97 | 0 | if (!sb.len) |
98 | 0 | continue; |
99 | | |
100 | 0 | if (parse_oid_hex_algop(sb.buf, &oid, &p, algop) || *p != '\0') |
101 | 0 | die("invalid object name: %s", sb.buf); |
102 | 0 | if (fn && fn(&oid, cbdata)) |
103 | 0 | continue; |
104 | 0 | oidset_insert(set, &oid); |
105 | 0 | } |
106 | 0 | if (ferror(fp)) |
107 | 0 | die_errno("Could not read '%s'", path); |
108 | 0 | fclose(fp); |
109 | 0 | strbuf_release(&sb); |
110 | 0 | } |