]> git.ipfire.org Git - thirdparty/git.git/blob - oidset.c
test-ref-store: remove force-create argument for create-reflog
[thirdparty/git.git] / oidset.c
1 #include "cache.h"
2 #include "oidset.h"
3
4 void oidset_init(struct oidset *set, size_t initial_size)
5 {
6 memset(&set->set, 0, sizeof(set->set));
7 if (initial_size)
8 kh_resize_oid_set(&set->set, initial_size);
9 }
10
11 int oidset_contains(const struct oidset *set, const struct object_id *oid)
12 {
13 khiter_t pos = kh_get_oid_set(&set->set, *oid);
14 return pos != kh_end(&set->set);
15 }
16
17 int oidset_insert(struct oidset *set, const struct object_id *oid)
18 {
19 int added;
20 kh_put_oid_set(&set->set, *oid, &added);
21 return !added;
22 }
23
24 int oidset_remove(struct oidset *set, const struct object_id *oid)
25 {
26 khiter_t pos = kh_get_oid_set(&set->set, *oid);
27 if (pos == kh_end(&set->set))
28 return 0;
29 kh_del_oid_set(&set->set, pos);
30 return 1;
31 }
32
33 void oidset_clear(struct oidset *set)
34 {
35 kh_release_oid_set(&set->set);
36 oidset_init(set, 0);
37 }
38
39 void oidset_parse_file(struct oidset *set, const char *path)
40 {
41 oidset_parse_file_carefully(set, path, NULL, NULL);
42 }
43
44 void oidset_parse_file_carefully(struct oidset *set, const char *path,
45 oidset_parse_tweak_fn fn, void *cbdata)
46 {
47 FILE *fp;
48 struct strbuf sb = STRBUF_INIT;
49 struct object_id oid;
50
51 fp = fopen(path, "r");
52 if (!fp)
53 die("could not open object name list: %s", path);
54 while (!strbuf_getline(&sb, fp)) {
55 const char *p;
56 const char *name;
57
58 /*
59 * Allow trailing comments, leading whitespace
60 * (including before commits), and empty or whitespace
61 * only lines.
62 */
63 name = strchr(sb.buf, '#');
64 if (name)
65 strbuf_setlen(&sb, name - sb.buf);
66 strbuf_trim(&sb);
67 if (!sb.len)
68 continue;
69
70 if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0')
71 die("invalid object name: %s", sb.buf);
72 if (fn && fn(&oid, cbdata))
73 continue;
74 oidset_insert(set, &oid);
75 }
76 if (ferror(fp))
77 die_errno("Could not read '%s'", path);
78 fclose(fp);
79 strbuf_release(&sb);
80 }