]> git.proxmox.com Git - libgit2.git/blob - tests/libgit2/odb/foreach.c
New upstream version 1.5.0+ds
[libgit2.git] / tests / libgit2 / odb / foreach.c
1 #include "clar_libgit2.h"
2 #include "odb.h"
3 #include "git2/odb_backend.h"
4 #include "pack.h"
5
6 static git_odb *_odb;
7 static git_repository *_repo;
8
9 void test_odb_foreach__cleanup(void)
10 {
11 git_odb_free(_odb);
12 git_repository_free(_repo);
13
14 _odb = NULL;
15 _repo = NULL;
16 }
17
18 static int foreach_cb(const git_oid *oid, void *data)
19 {
20 int *nobj = data;
21 (*nobj)++;
22
23 GIT_UNUSED(oid);
24
25 return 0;
26 }
27
28 /*
29 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
30 * count: 60
31 * size: 240
32 * in-pack: 1640
33 * packs: 3
34 * size-pack: 425
35 * prune-packable: 0
36 * garbage: 0
37 */
38 void test_odb_foreach__foreach(void)
39 {
40 int nobj = 0;
41
42 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
43 git_repository_odb(&_odb, _repo);
44
45 cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
46 cl_assert_equal_i(60 + 1640, nobj); /* count + in-pack */
47 }
48
49 void test_odb_foreach__one_pack(void)
50 {
51 git_odb_backend *backend = NULL;
52 int nobj = 0;
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
59 cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
60 cl_assert(nobj == 1628);
61 }
62
63 static int foreach_stop_cb(const git_oid *oid, void *data)
64 {
65 int *nobj = data;
66 (*nobj)++;
67
68 GIT_UNUSED(oid);
69
70 return (*nobj == 1000) ? -321 : 0;
71 }
72
73 static 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
83 static 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
93 void test_odb_foreach__interrupt_foreach(void)
94 {
95 int nobj = 0;
96 git_oid id;
97
98 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
99 git_repository_odb(&_odb, _repo);
100
101 cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
102 cl_assert(nobj == 1000);
103
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
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
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));
117 }
118
119 void test_odb_foreach__files_in_objects_dir(void)
120 {
121 git_repository *repo;
122 git_odb *odb;
123 git_str buf = GIT_STR_INIT;
124 int nobj = 0;
125
126 cl_fixture_sandbox("testrepo.git");
127 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
128
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);
132
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 */
136
137 git_odb_free(odb);
138 git_repository_free(repo);
139 cl_fixture_cleanup("testrepo.git");
140 }