status: contextually notify user about an initial commit
[git/debian.git] / sha1-array.c
blob7d646ab5b8cf80e47c6c1f339affeaa76a41d2d2
1 #include "cache.h"
2 #include "sha1-array.h"
3 #include "sha1-lookup.h"
5 void oid_array_append(struct oid_array *array, const struct object_id *oid)
7 ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
8 oidcpy(&array->oid[array->nr++], oid);
9 array->sorted = 0;
12 static int void_hashcmp(const void *a, const void *b)
14 return oidcmp(a, b);
17 static void oid_array_sort(struct oid_array *array)
19 QSORT(array->oid, array->nr, void_hashcmp);
20 array->sorted = 1;
23 static const unsigned char *sha1_access(size_t index, void *table)
25 struct object_id *array = table;
26 return array[index].hash;
29 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
31 if (!array->sorted)
32 oid_array_sort(array);
33 return sha1_pos(oid->hash, array->oid, array->nr, sha1_access);
36 void oid_array_clear(struct oid_array *array)
38 free(array->oid);
39 array->oid = NULL;
40 array->nr = 0;
41 array->alloc = 0;
42 array->sorted = 0;
45 int oid_array_for_each_unique(struct oid_array *array,
46 for_each_oid_fn fn,
47 void *data)
49 int i;
51 if (!array->sorted)
52 oid_array_sort(array);
54 for (i = 0; i < array->nr; i++) {
55 int ret;
56 if (i > 0 && !oidcmp(array->oid + i, array->oid + i - 1))
57 continue;
58 ret = fn(array->oid + i, data);
59 if (ret)
60 return ret;
62 return 0;