blob: 91d13859106a1bc56632aeedd4030a007608a359 [file] [log] [blame]
Elijah Newrenb73ecb42023-02-24 00:09:26 +00001#include "git-compat-util.h"
Jeff King29c2bd52017-02-08 15:53:07 -05002#include "oidset.h"
Elijah Newrenb73ecb42023-02-24 00:09:26 +00003#include "hex.h"
4#include "strbuf.h"
Jeff King29c2bd52017-02-08 15:53:07 -05005
René Scharfe8c84ae62018-10-04 17:14:37 +02006void oidset_init(struct oidset *set, size_t initial_size)
7{
8 memset(&set->set, 0, sizeof(set->set));
9 if (initial_size)
Jeff King8fbb5582019-06-20 03:41:28 -040010 kh_resize_oid_set(&set->set, initial_size);
René Scharfe8c84ae62018-10-04 17:14:37 +020011}
12
Jeff King29c2bd52017-02-08 15:53:07 -050013int oidset_contains(const struct oidset *set, const struct object_id *oid)
14{
Jeff King8fbb5582019-06-20 03:41:28 -040015 khiter_t pos = kh_get_oid_set(&set->set, *oid);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020016 return pos != kh_end(&set->set);
Jeff King29c2bd52017-02-08 15:53:07 -050017}
18
19int oidset_insert(struct oidset *set, const struct object_id *oid)
20{
René Scharfe8b2f8cb2018-10-04 17:13:06 +020021 int added;
Jeff King8fbb5582019-06-20 03:41:28 -040022 kh_put_oid_set(&set->set, *oid, &added);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020023 return !added;
Jeff King29c2bd52017-02-08 15:53:07 -050024}
25
Christian Coudereaf07b72024-02-14 15:25:11 +010026void oidset_insert_from_set(struct oidset *dest, struct oidset *src)
27{
28 struct oidset_iter iter;
29 struct object_id *src_oid;
30
31 oidset_iter_init(src, &iter);
32 while ((src_oid = oidset_iter_next(&iter)))
33 oidset_insert(dest, src_oid);
34}
35
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000036int oidset_remove(struct oidset *set, const struct object_id *oid)
37{
Jeff King8fbb5582019-06-20 03:41:28 -040038 khiter_t pos = kh_get_oid_set(&set->set, *oid);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020039 if (pos == kh_end(&set->set))
40 return 0;
Jeff King8fbb5582019-06-20 03:41:28 -040041 kh_del_oid_set(&set->set, pos);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020042 return 1;
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000043}
44
Jeff King29c2bd52017-02-08 15:53:07 -050045void oidset_clear(struct oidset *set)
46{
Jeff King8fbb5582019-06-20 03:41:28 -040047 kh_release_oid_set(&set->set);
René Scharfe8b2f8cb2018-10-04 17:13:06 +020048 oidset_init(set, 0);
Jeff King29c2bd52017-02-08 15:53:07 -050049}
Barret Rhodenf93895f2019-05-15 17:44:57 -040050
51void oidset_parse_file(struct oidset *set, const char *path)
52{
Junio C Hamano610e2b92020-09-24 21:55:04 -070053 oidset_parse_file_carefully(set, path, NULL, NULL);
54}
55
56void oidset_parse_file_carefully(struct oidset *set, const char *path,
57 oidset_parse_tweak_fn fn, void *cbdata)
58{
Barret Rhodenf93895f2019-05-15 17:44:57 -040059 FILE *fp;
60 struct strbuf sb = STRBUF_INIT;
61 struct object_id oid;
62
63 fp = fopen(path, "r");
64 if (!fp)
65 die("could not open object name list: %s", path);
66 while (!strbuf_getline(&sb, fp)) {
67 const char *p;
68 const char *name;
69
70 /*
71 * Allow trailing comments, leading whitespace
72 * (including before commits), and empty or whitespace
73 * only lines.
74 */
75 name = strchr(sb.buf, '#');
76 if (name)
77 strbuf_setlen(&sb, name - sb.buf);
78 strbuf_trim(&sb);
79 if (!sb.len)
80 continue;
81
René Scharfec714d052020-11-10 12:38:27 +010082 if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0')
Barret Rhodenf93895f2019-05-15 17:44:57 -040083 die("invalid object name: %s", sb.buf);
René Scharfec714d052020-11-10 12:38:27 +010084 if (fn && fn(&oid, cbdata))
85 continue;
Barret Rhodenf93895f2019-05-15 17:44:57 -040086 oidset_insert(set, &oid);
87 }
88 if (ferror(fp))
89 die_errno("Could not read '%s'", path);
90 fclose(fp);
91 strbuf_release(&sb);
92}