1 #include "clar_libgit2.h"
3 #include "git2/odb_backend.h"
7 static git_repository
*_repo
;
9 void test_odb_foreach__cleanup(void)
12 git_repository_free(_repo
);
18 static int foreach_cb(const git_oid
*oid
, void *data
)
29 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
38 void test_odb_foreach__foreach(void)
42 cl_git_pass(git_repository_open(&_repo
, cl_fixture("testrepo.git")));
43 git_repository_odb(&_odb
, _repo
);
45 cl_git_pass(git_odb_foreach(_odb
, foreach_cb
, &nobj
));
46 cl_assert_equal_i(60 + 1640, nobj
); /* count + in-pack */
49 void test_odb_foreach__one_pack(void)
51 git_odb_backend
*backend
= NULL
;
54 cl_git_pass(git_odb_new(&_odb
));
55 cl_git_pass(git_odb_backend_one_pack(&backend
, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
56 cl_git_pass(git_odb_add_backend(_odb
, backend
, 1));
59 cl_git_pass(git_odb_foreach(_odb
, foreach_cb
, &nobj
));
60 cl_assert(nobj
== 1628);
63 static int foreach_stop_cb(const git_oid
*oid
, void *data
)
70 return (*nobj
== 1000) ? -321 : 0;
73 static int foreach_stop_first_cb(const git_oid
*oid
, void *data
)
83 static int foreach_stop_cb_positive_ret(const git_oid
*oid
, void *data
)
90 return (*nobj
== 1000) ? 321 : 0;
93 void test_odb_foreach__interrupt_foreach(void)
98 cl_git_pass(git_repository_open(&_repo
, cl_fixture("testrepo.git")));
99 git_repository_odb(&_odb
, _repo
);
101 cl_assert_equal_i(-321, git_odb_foreach(_odb
, foreach_stop_cb
, &nobj
));
102 cl_assert(nobj
== 1000);
106 cl_assert_equal_i(321, git_odb_foreach(_odb
, foreach_stop_cb_positive_ret
, &nobj
));
107 cl_assert(nobj
== 1000);
110 git_repository_free(_repo
);
112 cl_git_pass(git_repository_init(&_repo
, "onlyloose.git", true));
113 git_repository_odb(&_odb
, _repo
);
115 cl_git_pass(git_odb_write(&id
, _odb
, "", 0, GIT_OBJECT_BLOB
));
116 cl_assert_equal_i(-123, git_odb_foreach(_odb
, foreach_stop_first_cb
, &nobj
));
119 void test_odb_foreach__files_in_objects_dir(void)
121 git_repository
*repo
;
123 git_str buf
= GIT_STR_INIT
;
126 cl_fixture_sandbox("testrepo.git");
127 cl_git_pass(git_repository_open(&repo
, "testrepo.git"));
129 cl_git_pass(git_str_joinpath(&buf
, git_repository_path(repo
), "objects/somefile"));
130 cl_git_mkfile(buf
.ptr
, "");
131 git_str_dispose(&buf
);
133 cl_git_pass(git_repository_odb(&odb
, repo
));
134 cl_git_pass(git_odb_foreach(odb
, foreach_cb
, &nobj
));
135 cl_assert_equal_i(60 + 1640, nobj
); /* count + in-pack */
138 git_repository_free(repo
);
139 cl_fixture_cleanup("testrepo.git");