foreach.c 2.71 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "pack.h"
5
#include "buffer.h"
6 7 8 9 10 11 12 13

static git_odb *_odb;
static git_repository *_repo;

void test_odb_foreach__cleanup(void)
{
	git_odb_free(_odb);
	git_repository_free(_repo);
14 15 16

	_odb = NULL;
	_repo = NULL;
17 18
}

19
static int foreach_cb(const git_oid *oid, void *data)
20
{
21 22
	int *nobj = data;
	(*nobj)++;
23

24
	GIT_UNUSED(oid);
25 26 27 28

	return 0;
}

29
/*
30
 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
31 32
 * count: 47
 * size: 4
33 34 35 36 37 38
 * in-pack: 1640
 * packs: 3
 * size-pack: 425
 * prune-packable: 0
 * garbage: 0
 */
39 40
void test_odb_foreach__foreach(void)
{
41 42
	int nobj = 0;

43 44 45
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	git_repository_odb(&_odb, _repo);

46
	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
47
	cl_assert_equal_i(47 + 1640, nobj); /* count + in-pack */
48
}
49 50 51 52

void test_odb_foreach__one_pack(void)
{
	git_odb_backend *backend = NULL;
53
	int nobj = 0;
54 55 56 57 58 59

	cl_git_pass(git_odb_new(&_odb));
	cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
	cl_git_pass(git_odb_add_backend(_odb, backend, 1));
	_repo = NULL;

60
	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
61 62
	cl_assert(nobj == 1628);
}
63

64
static int foreach_stop_cb(const git_oid *oid, void *data)
65
{
66 67
	int *nobj = data;
	(*nobj)++;
68

69
	GIT_UNUSED(oid);
70

71
	return (*nobj == 1000) ? -321 : 0;
72 73
}

74 75 76 77 78 79 80 81 82 83
static int foreach_stop_first_cb(const git_oid *oid, void *data)
{
	int *nobj = data;
	(*nobj)++;

	GIT_UNUSED(oid);

	return -123;
}

84 85
void test_odb_foreach__interrupt_foreach(void)
{
86
	int nobj = 0;
87
	git_oid id;
88

89 90 91
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	git_repository_odb(&_odb, _repo);

92
	cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
93
	cl_assert(nobj == 1000);
94 95 96 97 98 99 100 101 102

	git_odb_free(_odb);
	git_repository_free(_repo);

	cl_git_pass(git_repository_init(&_repo, "onlyloose.git", true));
	git_repository_odb(&_odb, _repo);

	cl_git_pass(git_odb_write(&id, _odb, "", 0, GIT_OBJ_BLOB));
	cl_assert_equal_i(-123, git_odb_foreach(_odb, foreach_stop_first_cb, &nobj));
103
}
104 105 106 107 108 109

void test_odb_foreach__files_in_objects_dir(void)
{
	git_repository *repo;
	git_odb *odb;
	git_buf buf = GIT_BUF_INIT;
110
	int nobj = 0;
111 112 113 114 115 116

	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));

	cl_git_pass(git_buf_printf(&buf, "%s/objects/somefile", git_repository_path(repo)));
	cl_git_mkfile(buf.ptr, "");
117
	git_buf_free(&buf);
118 119 120 121 122 123 124 125 126

	cl_git_pass(git_repository_odb(&odb, repo));
	cl_git_pass(git_odb_foreach(odb, foreach_cb, &nobj));
	cl_assert_equal_i(47 + 1640, nobj); /* count + in-pack */

	git_odb_free(odb);
	git_repository_free(repo);
	cl_fixture_cleanup("testrepo.git");
}