Coverage Report

Created: 2024-09-08 06:23

/src/git/oid-array.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "oid-array.h"
5
#include "hash-lookup.h"
6
7
void oid_array_append(struct oid_array *array, const struct object_id *oid)
8
0
{
9
0
  ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
10
0
  oidcpy(&array->oid[array->nr++], oid);
11
0
  if (!oid->algo)
12
0
    oid_set_algo(&array->oid[array->nr - 1], the_hash_algo);
13
0
  array->sorted = 0;
14
0
}
15
16
static int void_hashcmp(const void *va, const void *vb)
17
0
{
18
0
  const struct object_id *a = va, *b = vb;
19
0
  int ret;
20
0
  if (a->algo == b->algo)
21
0
    ret = oidcmp(a, b);
22
0
  else
23
0
    ret = a->algo > b->algo ? 1 : -1;
24
0
  return ret;
25
0
}
26
27
void oid_array_sort(struct oid_array *array)
28
0
{
29
0
  if (array->sorted)
30
0
    return;
31
0
  QSORT(array->oid, array->nr, void_hashcmp);
32
0
  array->sorted = 1;
33
0
}
34
35
static const struct object_id *oid_access(size_t index, const void *table)
36
0
{
37
0
  const struct object_id *array = table;
38
0
  return &array[index];
39
0
}
40
41
int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
42
0
{
43
0
  oid_array_sort(array);
44
0
  return oid_pos(oid, array->oid, array->nr, oid_access);
45
0
}
46
47
void oid_array_clear(struct oid_array *array)
48
0
{
49
0
  FREE_AND_NULL(array->oid);
50
0
  array->nr = 0;
51
0
  array->alloc = 0;
52
0
  array->sorted = 0;
53
0
}
54
55
56
int oid_array_for_each(struct oid_array *array,
57
           for_each_oid_fn fn,
58
           void *data)
59
0
{
60
0
  size_t i;
61
62
  /* No oid_array_sort() here! See oid-array.h */
63
64
0
  for (i = 0; i < array->nr; i++) {
65
0
    int ret = fn(array->oid + i, data);
66
0
    if (ret)
67
0
      return ret;
68
0
  }
69
0
  return 0;
70
0
}
71
72
int oid_array_for_each_unique(struct oid_array *array,
73
            for_each_oid_fn fn,
74
            void *data)
75
0
{
76
0
  size_t i;
77
78
0
  oid_array_sort(array);
79
80
0
  for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) {
81
0
    int ret = fn(array->oid + i, data);
82
0
    if (ret)
83
0
      return ret;
84
0
  }
85
0
  return 0;
86
0
}
87
88
void oid_array_filter(struct oid_array *array,
89
          for_each_oid_fn want,
90
          void *cb_data)
91
0
{
92
0
  size_t nr = array->nr, src, dst;
93
0
  struct object_id *oids = array->oid;
94
95
0
  for (src = dst = 0; src < nr; src++) {
96
0
    if (want(&oids[src], cb_data)) {
97
0
      if (src != dst)
98
0
        oidcpy(&oids[dst], &oids[src]);
99
0
      dst++;
100
0
    }
101
0
  }
102
0
  array->nr = dst;
103
0
}