]> git.proxmox.com Git - libgit2.git/blob - tests/refs/pack.c
1c1cd51cba54b3304b7773da2155f77e41fe8040
[libgit2.git] / tests / refs / pack.c
1 #include "clar_libgit2.h"
2
3 #include "futils.h"
4 #include "git2/reflog.h"
5 #include "git2/refdb.h"
6 #include "reflog.h"
7 #include "refs.h"
8 #include "ref_helpers.h"
9
10 static const char *loose_tag_ref_name = "refs/tags/e90810b";
11
12 static git_repository *g_repo;
13
14 void test_refs_pack__initialize(void)
15 {
16 g_repo = cl_git_sandbox_init("testrepo");
17 }
18
19 void test_refs_pack__cleanup(void)
20 {
21 cl_git_sandbox_cleanup();
22 }
23
24 static void packall(void)
25 {
26 git_refdb *refdb;
27
28 cl_git_pass(git_repository_refdb(&refdb, g_repo));
29 cl_git_pass(git_refdb_compress(refdb));
30 git_refdb_free(refdb);
31 }
32
33 void test_refs_pack__empty(void)
34 {
35 /* create a packfile for an empty folder */
36 git_str temp_path = GIT_STR_INIT;
37
38 cl_git_pass(git_str_join_n(&temp_path, '/', 3, git_repository_path(g_repo), GIT_REFS_HEADS_DIR, "empty_dir"));
39 cl_git_pass(git_futils_mkdir_r(temp_path.ptr, GIT_REFS_DIR_MODE));
40 git_str_dispose(&temp_path);
41
42 packall();
43 }
44
45 void test_refs_pack__loose(void)
46 {
47 /* create a packfile from all the loose refs in a repo */
48 git_reference *reference;
49 git_str temp_path = GIT_STR_INIT;
50
51 /* Ensure a known loose ref can be looked up */
52 cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
53 cl_assert(reference_is_packed(reference) == 0);
54 cl_assert_equal_s(reference->name, loose_tag_ref_name);
55 git_reference_free(reference);
56
57 /*
58 * We are now trying to pack also a loose reference
59 * called `points_to_blob`, to make sure we can properly
60 * pack weak tags
61 */
62 packall();
63
64 /* Ensure the packed-refs file exists */
65 cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), GIT_PACKEDREFS_FILE));
66 cl_assert(git_fs_path_exists(temp_path.ptr));
67
68 /* Ensure the known ref can still be looked up but is now packed */
69 cl_git_pass(git_reference_lookup(&reference, g_repo, loose_tag_ref_name));
70 cl_assert(reference_is_packed(reference));
71 cl_assert_equal_s(reference->name, loose_tag_ref_name);
72
73 /* Ensure the known ref has been removed from the loose folder structure */
74 cl_git_pass(git_str_joinpath(&temp_path, git_repository_path(g_repo), loose_tag_ref_name));
75 cl_assert(!git_fs_path_exists(temp_path.ptr));
76
77 git_reference_free(reference);
78 git_str_dispose(&temp_path);
79 }
80
81 void test_refs_pack__symbolic(void)
82 {
83 /* create a packfile from loose refs skipping symbolic refs */
84 int i;
85 git_oid head;
86 git_reference *ref;
87 char name[128];
88
89 cl_git_pass(git_reference_name_to_id(&head, g_repo, "HEAD"));
90
91 /* make a bunch of references */
92
93 for (i = 0; i < 100; ++i) {
94 p_snprintf(name, sizeof(name), "refs/heads/symbolic-%03d", i);
95 cl_git_pass(git_reference_symbolic_create(
96 &ref, g_repo, name, "refs/heads/master", 0, NULL));
97 git_reference_free(ref);
98
99 p_snprintf(name, sizeof(name), "refs/heads/direct-%03d", i);
100 cl_git_pass(git_reference_create(&ref, g_repo, name, &head, 0, NULL));
101 git_reference_free(ref);
102 }
103
104 packall();
105 }