stash: keep untracked files intact in stash -k
[alt-git.git] / sha1-array.c
blobc1cc25cd95da66aec1c695f4bba9680f5cb834f6
1 #include "cache.h"
2 #include "sha1-array.h"
3 #include "sha1-lookup.h"
5 void sha1_array_append(struct sha1_array *array, const unsigned char *sha1)
7 ALLOC_GROW(array->sha1, array->nr + 1, array->alloc);
8 hashcpy(array->sha1[array->nr++], sha1);
9 array->sorted = 0;
12 static int void_hashcmp(const void *a, const void *b)
14 return hashcmp(a, b);
17 static void sha1_array_sort(struct sha1_array *array)
19 QSORT(array->sha1, array->nr, void_hashcmp);
20 array->sorted = 1;
23 static const unsigned char *sha1_access(size_t index, void *table)
25 unsigned char (*array)[20] = table;
26 return array[index];
29 int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1)
31 if (!array->sorted)
32 sha1_array_sort(array);
33 return sha1_pos(sha1, array->sha1, array->nr, sha1_access);
36 void sha1_array_clear(struct sha1_array *array)
38 free(array->sha1);
39 array->sha1 = NULL;
40 array->nr = 0;
41 array->alloc = 0;
42 array->sorted = 0;
45 int sha1_array_for_each_unique(struct sha1_array *array,
46 for_each_sha1_fn fn,
47 void *data)
49 int i;
51 if (!array->sorted)
52 sha1_array_sort(array);
54 for (i = 0; i < array->nr; i++) {
55 int ret;
56 if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
57 continue;
58 ret = fn(array->sha1[i], data);
59 if (ret)
60 return ret;
62 return 0;