Make sure the ref iterator works in an repo without physical presence
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
diff --git a/src/refdb.c b/src/refdb.c
index 73882e8..269234b 100644
--- a/src/refdb.c
+++ b/src/refdb.c
@@ -128,7 +128,11 @@ int git_refdb_iterator(git_reference_iterator **out, git_refdb *db)
{
git_reference_iterator *iter;
- /* FIXME: don't segfault when there is no backends */
+ if (!db->backend || !db->backend->iterator) {
+ giterr_set(GITERR_REFERENCE, "This backend doesn't support iterators");
+ return -1;
+ }
+
if (db->backend->iterator(&iter, db->backend) < 0) {
git__free(iter);
return -1;
diff --git a/tests-clar/refs/iterator.c b/tests-clar/refs/iterator.c
index aef0453..d5555c6 100644
--- a/tests-clar/refs/iterator.c
+++ b/tests-clar/refs/iterator.c
@@ -74,3 +74,21 @@ void test_refs_iterator__list(void)
}
git_vector_free(&output);
}
+
+void test_refs_iterator__empty(void)
+{
+ git_reference_iterator *iter;
+ git_odb *odb;
+ const char *name;
+ git_repository *empty;
+
+ cl_git_pass(git_odb_new(&odb));
+ cl_git_pass(git_repository_wrap_odb(&empty, odb));
+
+ cl_git_pass(git_reference_iterator_new(&iter, empty));
+ cl_assert_equal_i(GIT_ITEROVER, git_reference_next(&name, iter));
+
+ git_reference_iterator_free(iter);
+ git_odb_free(odb);
+ git_repository_free(empty);
+}