worktree: move subcommand
[git] / oidset.c
1 #include "cache.h"
2 #include "oidset.h"
3
4 struct oidset_entry {
5         struct hashmap_entry hash;
6         struct object_id oid;
7 };
8
9 static int oidset_hashcmp(const void *va, const void *vb,
10                           const void *vkey)
11 {
12         const struct oidset_entry *a = va, *b = vb;
13         const struct object_id *key = vkey;
14         return oidcmp(&a->oid, key ? key : &b->oid);
15 }
16
17 int oidset_contains(const struct oidset *set, const struct object_id *oid)
18 {
19         struct hashmap_entry key;
20
21         if (!set->map.cmpfn)
22                 return 0;
23
24         hashmap_entry_init(&key, sha1hash(oid->hash));
25         return !!hashmap_get(&set->map, &key, oid);
26 }
27
28 int oidset_insert(struct oidset *set, const struct object_id *oid)
29 {
30         struct oidset_entry *entry;
31
32         if (!set->map.cmpfn)
33                 hashmap_init(&set->map, oidset_hashcmp, 0);
34
35         if (oidset_contains(set, oid))
36                 return 1;
37
38         entry = xmalloc(sizeof(*entry));
39         hashmap_entry_init(&entry->hash, sha1hash(oid->hash));
40         oidcpy(&entry->oid, oid);
41
42         hashmap_add(&set->map, entry);
43         return 0;
44 }
45
46 void oidset_clear(struct oidset *set)
47 {
48         hashmap_free(&set->map, 1);
49 }