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