]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/odb/foreach.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / odb / foreach.c
CommitLineData
521aedad
CMN
1#include "clar_libgit2.h"
2#include "odb.h"
3#include "git2/odb_backend.h"
4#include "pack.h"
5
6static git_odb *_odb;
7static git_repository *_repo;
521aedad 8
521aedad
CMN
9void test_odb_foreach__cleanup(void)
10{
11 git_odb_free(_odb);
12 git_repository_free(_repo);
81f73a87
VM
13
14 _odb = NULL;
15 _repo = NULL;
521aedad
CMN
16}
17
c3fb7d04 18static int foreach_cb(const git_oid *oid, void *data)
521aedad 19{
25e0b157
RB
20 int *nobj = data;
21 (*nobj)++;
521aedad 22
25e0b157 23 GIT_UNUSED(oid);
521aedad
CMN
24
25 return 0;
26}
27
1d733b57 28/*
83e1efbf 29 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
565fb8dc
ET
30 * count: 60
31 * size: 240
1d733b57 32 * in-pack: 1640
33 * packs: 3
34 * size-pack: 425
35 * prune-packable: 0
36 * garbage: 0
37 */
521aedad
CMN
38void test_odb_foreach__foreach(void)
39{
25e0b157
RB
40 int nobj = 0;
41
507523c3
CMN
42 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
43 git_repository_odb(&_odb, _repo);
44
25e0b157 45 cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
565fb8dc 46 cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
521aedad 47}
507523c3
CMN
48
49void test_odb_foreach__one_pack(void)
50{
51 git_odb_backend *backend = NULL;
25e0b157 52 int nobj = 0;
507523c3
CMN
53
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));
57 _repo = NULL;
58
25e0b157 59 cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
507523c3
CMN
60 cl_assert(nobj == 1628);
61}
5dca2010 62
c3fb7d04 63static int foreach_stop_cb(const git_oid *oid, void *data)
5dca2010 64{
25e0b157
RB
65 int *nobj = data;
66 (*nobj)++;
5dca2010 67
25e0b157 68 GIT_UNUSED(oid);
5dca2010 69
25e0b157 70 return (*nobj == 1000) ? -321 : 0;
5dca2010
RB
71}
72
8da44047
CMN
73static int foreach_stop_first_cb(const git_oid *oid, void *data)
74{
75 int *nobj = data;
76 (*nobj)++;
77
78 GIT_UNUSED(oid);
79
80 return -123;
81}
82
ac3d33df
JK
83static int foreach_stop_cb_positive_ret(const git_oid *oid, void *data)
84{
85 int *nobj = data;
86 (*nobj)++;
87
88 GIT_UNUSED(oid);
89
90 return (*nobj == 1000) ? 321 : 0;
91}
92
5dca2010
RB
93void test_odb_foreach__interrupt_foreach(void)
94{
25e0b157 95 int nobj = 0;
8da44047 96 git_oid id;
25e0b157 97
81f73a87
VM
98 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
99 git_repository_odb(&_odb, _repo);
100
25e0b157 101 cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
5dca2010 102 cl_assert(nobj == 1000);
8da44047 103
ac3d33df
JK
104 nobj = 0;
105
106 cl_assert_equal_i(321, git_odb_foreach(_odb, foreach_stop_cb_positive_ret, &nobj));
107 cl_assert(nobj == 1000);
108
8da44047
CMN
109 git_odb_free(_odb);
110 git_repository_free(_repo);
111
112 cl_git_pass(git_repository_init(&_repo, "onlyloose.git", true));
113 git_repository_odb(&_odb, _repo);
114
ac3d33df 115 cl_git_pass(git_odb_write(&id, _odb, "", 0, GIT_OBJECT_BLOB));
8da44047 116 cl_assert_equal_i(-123, git_odb_foreach(_odb, foreach_stop_first_cb, &nobj));
5dca2010 117}
ee311907
CMN
118
119void test_odb_foreach__files_in_objects_dir(void)
120{
121 git_repository *repo;
122 git_odb *odb;
e579e0f7 123 git_str buf = GIT_STR_INIT;
7629ea5d 124 int nobj = 0;
ee311907
CMN
125
126 cl_fixture_sandbox("testrepo.git");
127 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
128
e579e0f7 129 cl_git_pass(git_str_joinpath(&buf, git_repository_path(repo), "objects/somefile"));
ee311907 130 cl_git_mkfile(buf.ptr, "");
e579e0f7 131 git_str_dispose(&buf);
ee311907
CMN
132
133 cl_git_pass(git_repository_odb(&odb, repo));
134 cl_git_pass(git_odb_foreach(odb, foreach_cb, &nobj));
565fb8dc 135 cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
ee311907
CMN
136
137 git_odb_free(odb);
138 git_repository_free(repo);
139 cl_fixture_cleanup("testrepo.git");
140}