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 | | 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 | 0 | { |
77 | 0 | const char *p; |
78 | 0 | const char *name; |
79 | 0 | struct object_id oid; |
80 | |
|
81 | 0 | if (memchr(sb->buf, '\0', sb->len)) |
82 | 0 | 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 | 0 | name = strchr(sb->buf, '#'); |
90 | 0 | if (name) |
91 | 0 | strbuf_setlen(sb, name - sb->buf); |
92 | 0 | strbuf_trim(sb); |
93 | 0 | if (!sb->len) |
94 | 0 | return; |
95 | | |
96 | 0 | if (parse_oid_hex_algop(sb->buf, &oid, &p, algop) || *p != '\0') |
97 | 0 | die("invalid object name: %s", sb->buf); |
98 | 0 | if (fn && fn(&oid, cbdata)) |
99 | 0 | return; |
100 | 0 | oidset_insert(set, &oid); |
101 | 0 | } |
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 | 0 | { |
107 | 0 | FILE *fp; |
108 | 0 | struct strbuf sb = STRBUF_INIT; |
109 | |
|
110 | 0 | fp = fopen(path, "r"); |
111 | 0 | if (!fp) |
112 | 0 | die("could not open object name list: %s", path); |
113 | 0 | while (!strbuf_getline(&sb, fp)) |
114 | 0 | parse_oidset_line(set, &sb, algop, fn, cbdata); |
115 | 0 | if (ferror(fp)) |
116 | 0 | die_errno("Could not read '%s'", path); |
117 | 0 | fclose(fp); |
118 | 0 | strbuf_release(&sb); |
119 | 0 | } |
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 | 0 | { |
126 | 0 | struct strbuf sb = STRBUF_INIT; |
127 | 0 | const char *p = buf, *end; |
128 | |
|
129 | 0 | if (!size) |
130 | 0 | return; |
131 | 0 | end = buf + size; |
132 | |
|
133 | 0 | while (p < end) { |
134 | 0 | const char *nl = memchr(p, '\n', end - p); |
135 | 0 | size_t len = (nl ? nl : end) - p; |
136 | |
|
137 | 0 | strbuf_reset(&sb); |
138 | 0 | if (len && p[len - 1] == '\r') |
139 | 0 | len--; |
140 | 0 | strbuf_add(&sb, p, len); |
141 | 0 | parse_oidset_line(set, &sb, algop, fn, cbdata); |
142 | 0 | p = nl ? nl + 1 : end; |
143 | 0 | } |
144 | 0 | strbuf_release(&sb); |
145 | 0 | } |