blob: 2d0ab76fb569be21dacc68e66098a4814191325b [file] [log] [blame]
Jeff King29c2bd52017-02-08 15:53:07 -05001#include "cache.h"
2#include "oidset.h"
3
René Scharfe8c84ae62018-10-04 17:14:37 +02004void oidset_init(struct oidset *set, size_t initial_size)
5{
6 memset(&set->set, 0, sizeof(set->set));
7 if (initial_size)
Jeff King8fbb5582019-06-20 03:41:28 -04008 kh_resize_oid_set(&set->set, initial_size);
René Scharfe8c84ae62018-10-04 17:14:37 +02009}
10
Jeff King29c2bd52017-02-08 15:53:07 -050011int oidset_contains(const struct oidset *set, const struct object_id *oid)
12{
Jeff King8fbb5582019-06-20 03:41:28 -040013 khiter_t pos = kh_get_oid_set(&set->set, *oid);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020014 return pos != kh_end(&set->set);
Jeff King29c2bd52017-02-08 15:53:07 -050015}
16
17int oidset_insert(struct oidset *set, const struct object_id *oid)
18{
René Scharfe8b2f8cb2018-10-04 17:13:06 +020019 int added;
Jeff King8fbb5582019-06-20 03:41:28 -040020 kh_put_oid_set(&set->set, *oid, &added);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020021 return !added;
Jeff King29c2bd52017-02-08 15:53:07 -050022}
23
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000024int oidset_remove(struct oidset *set, const struct object_id *oid)
25{
Jeff King8fbb5582019-06-20 03:41:28 -040026 khiter_t pos = kh_get_oid_set(&set->set, *oid);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020027 if (pos == kh_end(&set->set))
28 return 0;
Jeff King8fbb5582019-06-20 03:41:28 -040029 kh_del_oid_set(&set->set, pos);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020030 return 1;
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000031}
32
Jeff King29c2bd52017-02-08 15:53:07 -050033void oidset_clear(struct oidset *set)
34{
Jeff King8fbb5582019-06-20 03:41:28 -040035 kh_release_oid_set(&set->set);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020036 oidset_init(set, 0);
Jeff King29c2bd52017-02-08 15:53:07 -050037}
Barret Rhodenf93895f2019-05-15 17:44:57 -040038
Taylor Blauf4781062020-04-13 22:04:22 -060039int oidset_size(struct oidset *set)
40{
41 return kh_size(&set->set);
42}
43
Barret Rhodenf93895f2019-05-15 17:44:57 -040044void oidset_parse_file(struct oidset *set, const char *path)
45{
Junio C Hamano610e2b92020-09-24 21:55:04 -070046 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)
51{
Barret Rhodenf93895f2019-05-15 17:44:57 -040052 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
Junio C Hamano610e2b92020-09-24 21:55:04 -070075 if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0' ||
76 (fn && fn(&oid, cbdata)))
Barret Rhodenf93895f2019-05-15 17:44:57 -040077 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}