Add iterator support to the testdb backend
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/tests-clar/refdb/testdb.c b/tests-clar/refdb/testdb.c
index 627254e..eee60ac 100644
--- a/tests-clar/refdb/testdb.c
+++ b/tests-clar/refdb/testdb.c
@@ -112,6 +112,49 @@ static int refdb_test_backend__lookup(
return GIT_ENOTFOUND;
}
+typedef struct {
+ git_reference_iterator parent;
+ size_t i;
+} refdb_test_iter;
+
+static int refdb_test_backend__iterator(git_reference_iterator **out, git_refdb_backend *_backend)
+{
+ refdb_test_iter *iter;
+
+ GIT_UNUSED(_backend);
+
+ iter = git__malloc(sizeof(refdb_test_iter));
+ GITERR_CHECK_ALLOC(iter);
+
+ iter->parent.backend = _backend;
+ iter->i = 0;
+
+ *out = (git_reference_iterator *) iter;
+
+ return 0;
+}
+
+static int refdb_test_backend__next(const char **name, git_reference_iterator *_iter)
+{
+ refdb_test_entry *entry;
+ refdb_test_backend *backend = (refdb_test_backend *) _iter->backend;
+ refdb_test_iter *iter = (refdb_test_iter *) _iter;
+
+ entry = git_vector_get(&backend->refs, iter->i);
+ if (!entry)
+ return GIT_ITEROVER;
+
+ *name = entry->name;
+ iter->i++;
+
+ return 0;
+}
+
+static void refdb_test_backend__iterator_free(git_reference_iterator *iter)
+{
+ git__free(iter);
+}
+
static int refdb_test_backend__foreach(
git_refdb_backend *_backend,
unsigned int list_flags,
@@ -200,6 +243,9 @@ int refdb_backend_test(
backend->parent.exists = &refdb_test_backend__exists;
backend->parent.lookup = &refdb_test_backend__lookup;
+ backend->parent.iterator = &refdb_test_backend__iterator;
+ backend->parent.next = &refdb_test_backend__next;
+ backend->parent.iterator_free = &refdb_test_backend__iterator_free;
backend->parent.foreach = &refdb_test_backend__foreach;
backend->parent.write = &refdb_test_backend__write;
backend->parent.delete = &refdb_test_backend__delete;