Commit 51fc5e895d7655611431a0bdb76b9fd8499a9e1a

Carlos Martín Nieto 2013-05-04T15:16:55

Make sure the ref iterator works in an repo without physical presence

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);
+}