]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/delete.c
1 #include "clar_libgit2.h"
4 #include "git2/reflog.h"
5 #include "git2/refdb.h"
7 #include "ref_helpers.h"
9 static const char *packed_test_head_name
= "refs/heads/packed-test";
10 static const char *current_master_tip
= "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
12 static git_repository
*g_repo
;
16 void test_refs_delete__initialize(void)
18 g_repo
= cl_git_sandbox_init("testrepo");
21 void test_refs_delete__cleanup(void)
23 cl_git_sandbox_cleanup();
28 void test_refs_delete__packed_loose(void)
30 // deleting a ref which is both packed and loose should remove both tracks in the filesystem
31 git_reference
*looked_up_ref
, *another_looked_up_ref
;
32 git_buf temp_path
= GIT_BUF_INIT
;
34 /* Ensure the loose reference exists on the file system */
35 cl_git_pass(git_buf_joinpath(&temp_path
, git_repository_path(g_repo
), packed_test_head_name
));
36 cl_assert(git_path_exists(temp_path
.ptr
));
38 /* Lookup the reference */
39 cl_git_pass(git_reference_lookup(&looked_up_ref
, g_repo
, packed_test_head_name
));
41 /* Ensure it's the loose version that has been found */
42 cl_assert(reference_is_packed(looked_up_ref
) == 0);
44 /* Now that the reference is deleted... */
45 cl_git_pass(git_reference_delete(looked_up_ref
));
46 git_reference_free(looked_up_ref
);
48 /* Looking up the reference once again should not retrieve it */
49 cl_git_fail(git_reference_lookup(&another_looked_up_ref
, g_repo
, packed_test_head_name
));
51 /* Ensure the loose reference doesn't exist any longer on the file system */
52 cl_assert(!git_path_exists(temp_path
.ptr
));
54 git_reference_free(another_looked_up_ref
);
55 git_buf_free(&temp_path
);
58 void test_refs_delete__packed_only(void)
60 // can delete a just packed reference
64 const char *new_ref
= "refs/heads/new_ref";
66 git_oid_fromstr(&id
, current_master_tip
);
68 /* Create and write the new object id reference */
69 cl_git_pass(git_reference_create(&ref
, g_repo
, new_ref
, &id
, 0, NULL
, NULL
));
70 git_reference_free(ref
);
72 /* Lookup the reference */
73 cl_git_pass(git_reference_lookup(&ref
, g_repo
, new_ref
));
75 /* Ensure it's a loose reference */
76 cl_assert(reference_is_packed(ref
) == 0);
78 /* Pack all existing references */
79 cl_git_pass(git_repository_refdb(&refdb
, g_repo
));
80 cl_git_pass(git_refdb_compress(refdb
));
82 /* Reload the reference from disk */
83 git_reference_free(ref
);
84 cl_git_pass(git_reference_lookup(&ref
, g_repo
, new_ref
));
86 /* Ensure it's a packed reference */
87 cl_assert(reference_is_packed(ref
) == 1);
89 /* This should pass */
90 cl_git_pass(git_reference_delete(ref
));
91 git_reference_free(ref
);
92 git_refdb_free(refdb
);