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