Commit f7416509c065b908e6d06ec11b31379b3bc51f90

Marijan Šuflaj 2019-01-20T20:15:31

Fix odb foreach to also close on positive error code In include/git2/odb.h it states that callback can also return positive value which should break looping. Implementations of git_odb_foreach() and pack_backend__foreach() did not respect that.

diff --git a/src/odb.c b/src/odb.c
index b6833e4..c0cd0ef 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -1260,7 +1260,7 @@ int git_odb_foreach(git_odb *db, git_odb_foreach_cb cb, void *payload)
 	git_vector_foreach(&db->backends, i, internal) {
 		git_odb_backend *b = internal->backend;
 		int error = b->foreach(b, cb, payload);
-		if (error < 0)
+		if (error != 0)
 			return error;
 	}
 
diff --git a/src/odb_pack.c b/src/odb_pack.c
index c2c5e79..6bdedec 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -478,7 +478,7 @@ static int pack_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb c
 		return error;
 
 	git_vector_foreach(&backend->packs, i, p) {
-		if ((error = git_pack_foreach_entry(p, cb, data)) < 0)
+		if ((error = git_pack_foreach_entry(p, cb, data)) != 0)
 			return error;
 	}
 
diff --git a/tests/odb/foreach.c b/tests/odb/foreach.c
index 3e44f10..7a45a57 100644
--- a/tests/odb/foreach.c
+++ b/tests/odb/foreach.c
@@ -81,6 +81,16 @@ static int foreach_stop_first_cb(const git_oid *oid, void *data)
 	return -123;
 }
 
+static int foreach_stop_cb_positive_ret(const git_oid *oid, void *data)
+{
+	int *nobj = data;
+	(*nobj)++;
+
+	GIT_UNUSED(oid);
+
+	return (*nobj == 1000) ? 321 : 0;
+}
+
 void test_odb_foreach__interrupt_foreach(void)
 {
 	int nobj = 0;
@@ -92,6 +102,11 @@ void test_odb_foreach__interrupt_foreach(void)
 	cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
 	cl_assert(nobj == 1000);
 
+	nobj = 0;
+
+	cl_assert_equal_i(321, git_odb_foreach(_odb, foreach_stop_cb_positive_ret, &nobj));
+	cl_assert(nobj == 1000);
+
 	git_odb_free(_odb);
 	git_repository_free(_repo);