Coverage Report

Created: 2024-09-16 06:12

/src/git/oidset.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "oidset.h"
3
#include "hex.h"
4
#include "strbuf.h"
5
6
void oidset_init(struct oidset *set, size_t initial_size)
7
97.8k
{
8
97.8k
  memset(&set->set, 0, sizeof(set->set));
9
97.8k
  if (initial_size)
10
0
    kh_resize_oid_set(&set->set, initial_size);
11
97.8k
}
12
13
int oidset_contains(const struct oidset *set, const struct object_id *oid)
14
0
{
15
0
  khiter_t pos = kh_get_oid_set(&set->set, *oid);
16
0
  return pos != kh_end(&set->set);
17
0
}
18
19
int oidset_insert(struct oidset *set, const struct object_id *oid)
20
0
{
21
0
  int added;
22
0
  kh_put_oid_set(&set->set, *oid, &added);
23
0
  return !added;
24
0
}
25
26
void oidset_insert_from_set(struct oidset *dest, struct oidset *src)
27
0
{
28
0
  struct oidset_iter iter;
29
0
  struct object_id *src_oid;
30
31
0
  oidset_iter_init(src, &iter);
32
0
  while ((src_oid = oidset_iter_next(&iter)))
33
0
    oidset_insert(dest, src_oid);
34
0
}
35
36
int oidset_remove(struct oidset *set, const struct object_id *oid)
37
0
{
38
0
  khiter_t pos = kh_get_oid_set(&set->set, *oid);
39
0
  if (pos == kh_end(&set->set))
40
0
    return 0;
41
0
  kh_del_oid_set(&set->set, pos);
42
0
  return 1;
43
0
}
44
45
void oidset_clear(struct oidset *set)
46
47.5k
{
47
47.5k
  kh_release_oid_set(&set->set);
48
47.5k
  oidset_init(set, 0);
49
47.5k
}
50
51
void oidset_parse_file(struct oidset *set, const char *path,
52
           const struct git_hash_algo *algop)
53
0
{
54
0
  oidset_parse_file_carefully(set, path, algop, NULL, NULL);
55
0
}
56
57
void oidset_parse_file_carefully(struct oidset *set, const char *path,
58
         const struct git_hash_algo *algop,
59
         oidset_parse_tweak_fn fn, void *cbdata)
60
0
{
61
0
  FILE *fp;
62
0
  struct strbuf sb = STRBUF_INIT;
63
0
  struct object_id oid;
64
65
0
  fp = fopen(path, "r");
66
0
  if (!fp)
67
0
    die("could not open object name list: %s", path);
68
0
  while (!strbuf_getline(&sb, fp)) {
69
0
    const char *p;
70
0
    const char *name;
71
72
    /*
73
     * Allow trailing comments, leading whitespace
74
     * (including before commits), and empty or whitespace
75
     * only lines.
76
     */
77
0
    name = strchr(sb.buf, '#');
78
0
    if (name)
79
0
      strbuf_setlen(&sb, name - sb.buf);
80
0
    strbuf_trim(&sb);
81
0
    if (!sb.len)
82
0
      continue;
83
84
0
    if (parse_oid_hex_algop(sb.buf, &oid, &p, algop) || *p != '\0')
85
0
      die("invalid object name: %s", sb.buf);
86
0
    if (fn && fn(&oid, cbdata))
87
0
      continue;
88
0
    oidset_insert(set, &oid);
89
0
  }
90
0
  if (ferror(fp))
91
0
    die_errno("Could not read '%s'", path);
92
0
  fclose(fp);
93
0
  strbuf_release(&sb);
94
0
}