Merge branch 'po-id' of github.com:bagasme/git-po
[alt-git.git] / oidset.c
blob5aac633c1f405580447001dfae114660e6120e90
1 #include "cache.h"
2 #include "oidset.h"
4 void oidset_init(struct oidset *set, size_t initial_size)
6 memset(&set->set, 0, sizeof(set->set));
7 if (initial_size)
8 kh_resize_oid_set(&set->set, initial_size);
11 int oidset_contains(const struct oidset *set, const struct object_id *oid)
13 khiter_t pos = kh_get_oid_set(&set->set, *oid);
14 return pos != kh_end(&set->set);
17 int oidset_insert(struct oidset *set, const struct object_id *oid)
19 int added;
20 kh_put_oid_set(&set->set, *oid, &added);
21 return !added;
24 int oidset_remove(struct oidset *set, const struct object_id *oid)
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;
33 void oidset_clear(struct oidset *set)
35 kh_release_oid_set(&set->set);
36 oidset_init(set, 0);
39 int oidset_size(struct oidset *set)
41 return kh_size(&set->set);
44 void oidset_parse_file(struct oidset *set, const char *path)
46 oidset_parse_file_carefully(set, path, NULL, NULL);
49 void oidset_parse_file_carefully(struct oidset *set, const char *path,
50 oidset_parse_tweak_fn fn, void *cbdata)
52 FILE *fp;
53 struct strbuf sb = STRBUF_INIT;
54 struct object_id oid;
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;
64 * Allow trailing comments, leading whitespace
65 * (including before commits), and empty or whitespace
66 * only lines.
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;
75 if (parse_oid_hex(sb.buf, &oid, &p) || *p != '\0')
76 die("invalid object name: %s", sb.buf);
77 if (fn && fn(&oid, cbdata))
78 continue;
79 oidset_insert(set, &oid);
81 if (ferror(fp))
82 die_errno("Could not read '%s'", path);
83 fclose(fp);
84 strbuf_release(&sb);