mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-23 20:23:51 +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.
93 lines
2.0 KiB
C
93 lines
2.0 KiB
C
#include "clar_libgit2.h"
|
|
#include "refs.h"
|
|
|
|
static git_repository *repo;
|
|
static git_reference *fake_remote;
|
|
|
|
void test_refs_foreachglob__initialize(void)
|
|
{
|
|
git_oid id;
|
|
|
|
cl_fixture_sandbox("testrepo.git");
|
|
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
|
|
|
|
cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
|
|
cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0));
|
|
}
|
|
|
|
void test_refs_foreachglob__cleanup(void)
|
|
{
|
|
git_reference_free(fake_remote);
|
|
git_repository_free(repo);
|
|
|
|
cl_fixture_cleanup("testrepo.git");
|
|
}
|
|
|
|
static int count_cb(const char *reference_name, void *payload)
|
|
{
|
|
int *count = (int *)payload;
|
|
|
|
GIT_UNUSED(reference_name);
|
|
|
|
(*count)++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void assert_retrieval(const char *glob, unsigned int flags, int expected_count)
|
|
{
|
|
int count = 0;
|
|
|
|
cl_git_pass(git_reference_foreach_glob(repo, glob, flags, count_cb, &count));
|
|
|
|
cl_assert_equal_i(expected_count, count);
|
|
}
|
|
|
|
void test_refs_foreachglob__retrieve_all_refs(void)
|
|
{
|
|
/* 7 heads (including one packed head) + 1 note + 2 remotes + 6 tags */
|
|
assert_retrieval("*", GIT_REF_LISTALL, 17);
|
|
}
|
|
|
|
void test_refs_foreachglob__retrieve_remote_branches(void)
|
|
{
|
|
assert_retrieval("refs/remotes/*", GIT_REF_LISTALL, 2);
|
|
}
|
|
|
|
void test_refs_foreachglob__retrieve_local_branches(void)
|
|
{
|
|
assert_retrieval("refs/heads/*", GIT_REF_LISTALL, 8);
|
|
}
|
|
|
|
void test_refs_foreachglob__retrieve_partially_named_references(void)
|
|
{
|
|
/*
|
|
* refs/heads/packed-test, refs/heads/test
|
|
* refs/remotes/test/master, refs/tags/test
|
|
*/
|
|
|
|
assert_retrieval("*test*", GIT_REF_LISTALL, 4);
|
|
}
|
|
|
|
|
|
static int interrupt_cb(const char *reference_name, void *payload)
|
|
{
|
|
int *count = (int *)payload;
|
|
|
|
GIT_UNUSED(reference_name);
|
|
|
|
(*count)++;
|
|
|
|
return (*count == 11);
|
|
}
|
|
|
|
void test_refs_foreachglob__can_cancel(void)
|
|
{
|
|
int count = 0;
|
|
|
|
cl_assert_equal_i(GIT_EUSER, git_reference_foreach_glob(
|
|
repo, "*", GIT_REF_LISTALL, interrupt_cb, &count) );
|
|
|
|
cl_assert_equal_i(11, count);
|
|
}
|