]> git.proxmox.com Git - libgit2.git/blob - tests/odb/foreach.c
Merge pull request #3171 from libgit2/cmn/link-fallback
[libgit2.git] / tests / odb / foreach.c
1 #include "clar_libgit2.h"
2 #include "odb.h"
3 #include "git2/odb_backend.h"
4 #include "pack.h"
5 #include "buffer.h"
6
7 static git_odb *_odb;
8 static git_repository *_repo;
9
10 void test_odb_foreach__cleanup(void)
11 {
12 git_odb_free(_odb);
13 git_repository_free(_repo);
14
15 _odb = NULL;
16 _repo = NULL;
17 }
18
19 static int foreach_cb(const git_oid *oid, void *data)
20 {
21 int *nobj = data;
22 (*nobj)++;
23
24 GIT_UNUSED(oid);
25
26 return 0;
27 }
28
29 /*
30 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
31 * count: 47
32 * size: 4
33 * in-pack: 1640
34 * packs: 3
35 * size-pack: 425
36 * prune-packable: 0
37 * garbage: 0
38 */
39 void test_odb_foreach__foreach(void)
40 {
41 int nobj = 0;
42
43 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
44 git_repository_odb(&_odb, _repo);
45
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 void test_odb_foreach__one_pack(void)
51 {
52 git_odb_backend *backend = NULL;
53 int nobj = 0;
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
60 cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
61 cl_assert(nobj == 1628);
62 }
63
64 static int foreach_stop_cb(const git_oid *oid, void *data)
65 {
66 int *nobj = data;
67 (*nobj)++;
68
69 GIT_UNUSED(oid);
70
71 return (*nobj == 1000) ? -321 : 0;
72 }
73
74 static 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
84 void test_odb_foreach__interrupt_foreach(void)
85 {
86 int nobj = 0;
87 git_oid id;
88
89 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
90 git_repository_odb(&_odb, _repo);
91
92 cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
93 cl_assert(nobj == 1000);
94
95 git_odb_free(_odb);
96 git_repository_free(_repo);
97
98 cl_git_pass(git_repository_init(&_repo, "onlyloose.git", true));
99 git_repository_odb(&_odb, _repo);
100
101 cl_git_pass(git_odb_write(&id, _odb, "", 0, GIT_OBJ_BLOB));
102 cl_assert_equal_i(-123, git_odb_foreach(_odb, foreach_stop_first_cb, &nobj));
103 }
104
105 void test_odb_foreach__files_in_objects_dir(void)
106 {
107 git_repository *repo;
108 git_odb *odb;
109 git_buf buf = GIT_BUF_INIT;
110 int nobj = 0;
111
112 cl_fixture_sandbox("testrepo.git");
113 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
114
115 cl_git_pass(git_buf_printf(&buf, "%s/objects/somefile", git_repository_path(repo)));
116 cl_git_mkfile(buf.ptr, "");
117 git_buf_free(&buf);
118
119 cl_git_pass(git_repository_odb(&odb, repo));
120 cl_git_pass(git_odb_foreach(odb, foreach_cb, &nobj));
121 cl_assert_equal_i(47 + 1640, nobj); /* count + in-pack */
122
123 git_odb_free(odb);
124 git_repository_free(repo);
125 cl_fixture_cleanup("testrepo.git");
126 }