mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-03 11:20:59 +00:00

This updates all the `foreach()` type functions across the library that take callbacks from the user to have a consistent behavior. The rules are: * A callback terminates the loop by returning any non-zero value * Once the callback returns non-zero, it will not be called again (i.e. the loop stops all iteration regardless of state) * If the callback returns non-zero, the parent fn returns GIT_EUSER * Although the parent returns GIT_EUSER, no error will be set in the library and `giterr_last()` will return NULL if called. This commit makes those changes across the library and adds tests for most of the iteration APIs to make sure that they follow the above rules.
55 lines
946 B
C
55 lines
946 B
C
#include "clar_libgit2.h"
|
|
#include "odb.h"
|
|
#include "git2/odb_backend.h"
|
|
#include "pack.h"
|
|
|
|
static git_odb *_odb;
|
|
static git_repository *_repo;
|
|
static int nobj;
|
|
|
|
void test_odb_foreach__initialize(void)
|
|
{
|
|
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
|
|
git_repository_odb(&_odb, _repo);
|
|
}
|
|
|
|
void test_odb_foreach__cleanup(void)
|
|
{
|
|
git_odb_free(_odb);
|
|
git_repository_free(_repo);
|
|
}
|
|
|
|
static int foreach_cb(git_oid *oid, void *data)
|
|
{
|
|
GIT_UNUSED(data);
|
|
GIT_UNUSED(oid);
|
|
|
|
nobj++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void test_odb_foreach__foreach(void)
|
|
{
|
|
nobj = 0;
|
|
cl_git_pass(git_odb_foreach(_odb, foreach_cb, NULL));
|
|
cl_assert(nobj == 1683);
|
|
}
|
|
|
|
static int foreach_stop_cb(git_oid *oid, void *data)
|
|
{
|
|
GIT_UNUSED(data);
|
|
GIT_UNUSED(oid);
|
|
|
|
nobj++;
|
|
|
|
return (nobj == 1000);
|
|
}
|
|
|
|
void test_odb_foreach__interrupt_foreach(void)
|
|
{
|
|
nobj = 0;
|
|
cl_assert_equal_i(GIT_EUSER, git_odb_foreach(_odb, foreach_stop_cb, NULL));
|
|
cl_assert(nobj == 1000);
|
|
}
|