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