midx.c 3.93 KB
Newer Older
1 2 3
#include "clar_libgit2.h"

#include <git2.h>
4
#include <git2/sys/midx.h>
5

6
#include "futils.h"
7 8 9 10 11 12 13 14
#include "midx.h"

void test_pack_midx__parse(void)
{
	git_repository *repo;
	struct git_midx_file *idx;
	struct git_midx_entry e;
	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"));
Edward Thomson committed
19
	cl_git_pass(git_midx_open(&idx, git_str_cstr(&midx_path), GIT_OID_SHA1));
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", GIT_OID_SHA1));
23
	cl_git_pass(git_midx_entry_find(&e, idx, &id, GIT_OID_SHA1_HEXSIZE));
24 25 26 27 28 29 30
	cl_assert_equal_oid(&e.sha1, &id);
	cl_assert_equal_s(
			(const char *)git_vector_get(&idx->packfile_names, e.pack_index),
			"pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx");

	git_midx_free(idx);
	git_repository_free(repo);
31
	git_str_dispose(&midx_path);
32
}
33 34 35 36 37 38 39 40 41

void test_pack_midx__lookup(void)
{
	git_repository *repo;
	git_commit *commit;
	git_oid id;

	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));

42
	cl_git_pass(git_oid__fromstr(&id, "5001298e0c09ad9c34e4249bc5801c75e9754fa5", GIT_OID_SHA1));
43
	cl_git_pass(git_commit_lookup_prefix(&commit, repo, &id, GIT_OID_SHA1_HEXSIZE));
44 45 46 47 48
	cl_assert_equal_s(git_commit_message(commit), "packed commit one\n");

	git_commit_free(commit);
	git_repository_free(repo);
}
49 50 51 52 53

void test_pack_midx__writer(void)
{
	git_repository *repo;
	git_midx_writer *w = NULL;
54 55
	git_buf midx = GIT_BUF_INIT;
	git_str expected_midx = GIT_STR_INIT, path = GIT_STR_INIT;
56 57 58

	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));

59
	cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack"));
Edward Thomson committed
60 61 62 63

#ifdef GIT_EXPERIMENTAL_SHA256
	cl_git_pass(git_midx_writer_new(&w, git_str_cstr(&path), GIT_OID_SHA1));
#else
64
	cl_git_pass(git_midx_writer_new(&w, git_str_cstr(&path)));
Edward Thomson committed
65
#endif
66 67 68 69 70 71

	cl_git_pass(git_midx_writer_add(w, "pack-d7c6adf9f61318f041845b01440d09aa7a91e1b5.idx"));
	cl_git_pass(git_midx_writer_add(w, "pack-d85f5d483273108c9d8dd0e4728ccf0b2982423a.idx"));
	cl_git_pass(git_midx_writer_add(w, "pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx"));

	cl_git_pass(git_midx_writer_dump(&midx, w));
72 73
	cl_git_pass(git_str_joinpath(&path, git_repository_path(repo), "objects/pack/multi-pack-index"));
	cl_git_pass(git_futils_readbuffer(&expected_midx, git_str_cstr(&path)));
74

75 76
	cl_assert_equal_i(midx.size, git_str_len(&expected_midx));
	cl_assert_equal_strn(midx.ptr, git_str_cstr(&expected_midx), midx.size);
77 78

	git_buf_dispose(&midx);
79 80
	git_str_dispose(&expected_midx);
	git_str_dispose(&path);
81 82 83
	git_midx_writer_free(w);
	git_repository_free(repo);
}
84 85 86 87 88 89

void test_pack_midx__odb_create(void)
{
	git_repository *repo;
	git_odb *odb;
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
90
	git_str midx = GIT_STR_INIT, expected_midx = GIT_STR_INIT, midx_path = GIT_STR_INIT;
91 92 93 94 95
	struct stat st;

	opts.bare = true;
	opts.local = GIT_CLONE_LOCAL;
	cl_git_pass(git_clone(&repo, cl_fixture("testrepo/.gitted"), "./clone.git", &opts));
96 97
	cl_git_pass(git_str_joinpath(&midx_path, git_repository_path(repo), "objects/pack/multi-pack-index"));
	cl_git_fail(p_stat(git_str_cstr(&midx_path), &st));
98 99 100 101 102

	cl_git_pass(git_repository_odb(&odb, repo));
	cl_git_pass(git_odb_write_multi_pack_index(odb));
	git_odb_free(odb);

103
	cl_git_pass(p_stat(git_str_cstr(&midx_path), &st));
104 105

	cl_git_pass(git_futils_readbuffer(&expected_midx, cl_fixture("testrepo.git/objects/pack/multi-pack-index")));
106 107 108
	cl_git_pass(git_futils_readbuffer(&midx, git_str_cstr(&midx_path)));
	cl_assert_equal_i(git_str_len(&midx), git_str_len(&expected_midx));
	cl_assert_equal_strn(git_str_cstr(&midx), git_str_cstr(&expected_midx), git_str_len(&midx));
109 110

	git_repository_free(repo);
111 112 113
	git_str_dispose(&midx);
	git_str_dispose(&midx_path);
	git_str_dispose(&expected_midx);
114 115

	cl_git_pass(git_futils_rmdir_r("./clone.git", NULL, GIT_RMDIR_REMOVE_FILES));
116
}