]> git.proxmox.com Git - libgit2.git/blob - tests/pack/midx.c
9dd949363e6200a8d5c7b219b283773eec07191a
[libgit2.git] / tests / pack / midx.c
1 #include "clar_libgit2.h"
2
3 #include <git2.h>
4 #include <git2/sys/midx.h>
5
6 #include "futils.h"
7 #include "midx.h"
8
9 void test_pack_midx__parse(void)
10 {
11 git_repository *repo;
12 struct git_midx_file *idx;
13 struct git_midx_entry e;
14 git_oid id;
15 git_str midx_path = GIT_STR_INIT;
16
17 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
18 cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index"));
19 cl_git_pass(git_midx_open(&idx, git_str_cstr(&midx_path)));
20 cl_assert_equal_i(git_midx_needs_refresh(idx, git_str_cstr(&midx_path)), 0);
21
22 cl_git_pass(git_oid_fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5"));
23 cl_git_pass(git_midx_entry_find(&e, idx, &id, GIT_OID_HEXSZ));
24 cl_assert_equal_oid(&e.sha1, &id);
25 cl_assert_equal_s(
26 (const char *)git_vector_get(&idx->packfile_names, e.pack_index),
27 "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx");
28
29 git_midx_free(idx);
30 git_repository_free(repo);
31 git_str_dispose(&midx_path);
32 }
33
34 void test_pack_midx__lookup(void)
35 {
36 git_repository *repo;
37 git_commit *commit;
38 git_oid id;
39
40 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
41
42 cl_git_pass(git_oid_fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5"));
43 cl_git_pass(git_commit_lookup_prefix(&commit, repo, &id, GIT_OID_HEXSZ));
44 cl_assert_equal_s(git_commit_message(commit), "packed commit one\n");
45
46 git_commit_free(commit);
47 git_repository_free(repo);
48 }
49
50 void test_pack_midx__writer(void)
51 {
52 git_repository *repo;
53 git_midx_writer *w = NULL;
54 git_buf midx = GIT_BUF_INIT;
55 git_str expected_midx = GIT_STR_INIT, path = GIT_STR_INIT;
56
57 cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));
58
59 cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack"));
60 cl_git_pass(git_midx_writer_new(&w, git_str_cstr(&path)));
61
62 cl_git_pass(git_midx_writer_add(w, "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx"));
63 cl_git_pass(git_midx_writer_add(w, "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.idx"));
64 cl_git_pass(git_midx_writer_add(w, "pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx"));
65
66 cl_git_pass(git_midx_writer_dump(&midx, w));
67 cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack/multi-pack-index"));
68 cl_git_pass(git_futils_readbuffer(&expected_midx, git_str_cstr(&path)));
69
70 cl_assert_equal_i(midx.size, git_str_len(&expected_midx));
71 cl_assert_equal_strn(midx.ptr, git_str_cstr(&expected_midx), midx.size);
72
73 git_buf_dispose(&midx);
74 git_str_dispose(&expected_midx);
75 git_str_dispose(&path);
76 git_midx_writer_free(w);
77 git_repository_free(repo);
78 }
79
80 void test_pack_midx__odb_create(void)
81 {
82 git_repository *repo;
83 git_odb *odb;
84 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
85 git_str midx = GIT_STR_INIT, expected_midx = GIT_STR_INIT, midx_path = GIT_STR_INIT;
86 struct stat st;
87
88 opts.bare = true;
89 opts.local = GIT_CLONE_LOCAL;
90 cl_git_pass(git_clone(&repo, cl_fixture("testrepo/.gitted"), "./clone.git", &opts));
91 cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index"));
92 cl_git_fail(p_stat(git_str_cstr(&midx_path), &st));
93
94 cl_git_pass(git_repository_odb(&odb, repo));
95 cl_git_pass(git_odb_write_multi_pack_index(odb));
96 git_odb_free(odb);
97
98 cl_git_pass(p_stat(git_str_cstr(&midx_path), &st));
99
100 cl_git_pass(git_futils_readbuffer(&expected_midx, cl_fixture("testrepo.git/objects/pack/multi-pack-index")));
101 cl_git_pass(git_futils_readbuffer(&midx, git_str_cstr(&midx_path)));
102 cl_assert_equal_i(git_str_len(&midx), git_str_len(&expected_midx));
103 cl_assert_equal_strn(git_str_cstr(&midx), git_str_cstr(&expected_midx), git_str_len(&midx));
104
105 git_repository_free(repo);
106 git_str_dispose(&midx);
107 git_str_dispose(&midx_path);
108 git_str_dispose(&expected_midx);
109
110 cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
111 }