]>
git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/delete.c
6093c7886583cd744cb0f0ba211c5c48156bc88e
1 #include "clar_libgit2.h"
3 #include "repo/repo_helpers.h"
4 #include "config/config_helpers.h"
8 static git_repository
*repo
;
9 static git_reference
*fake_remote
;
11 void test_refs_branches_delete__initialize(void)
15 repo
= cl_git_sandbox_init("testrepo.git");
17 cl_git_pass(git_oid_fromstr(&id
, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
18 cl_git_pass(git_reference_create(&fake_remote
, repo
, "refs/remotes/nulltoken/master", &id
, 0, NULL
));
21 void test_refs_branches_delete__cleanup(void)
23 git_reference_free(fake_remote
);
26 cl_git_sandbox_cleanup();
30 void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
33 git_reference
*branch
;
35 /* Ensure HEAD targets the local master branch */
36 cl_git_pass(git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
));
37 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
38 git_reference_free(head
);
40 cl_git_pass(git_branch_lookup(&branch
, repo
, "master", GIT_BRANCH_LOCAL
));
41 cl_git_fail(git_branch_delete(branch
));
42 git_reference_free(branch
);
45 void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
48 git_reference
*branch
;
50 cl_git_pass(git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
));
51 git_reference_delete(head
);
52 git_reference_free(head
);
54 cl_git_pass(git_branch_lookup(&branch
, repo
, "br2", GIT_BRANCH_LOCAL
));
55 cl_git_pass(git_branch_delete(branch
));
56 git_reference_free(branch
);
59 void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
61 git_reference
*branch
;
63 make_head_unborn(repo
, NON_EXISTING_HEAD
);
65 cl_git_pass(git_branch_lookup(&branch
, repo
, "br2", GIT_BRANCH_LOCAL
));
66 cl_git_pass(git_branch_delete(branch
));
67 git_reference_free(branch
);
70 void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
72 git_reference
*head
, *branch
;
74 cl_git_pass(git_reference_lookup(&head
, repo
, GIT_HEAD_FILE
));
75 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
76 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
77 git_reference_free(head
);
79 /* Detach HEAD and make it target the commit that "master" points to */
80 git_repository_detach_head(repo
);
82 cl_git_pass(git_branch_lookup(&branch
, repo
, "master", GIT_BRANCH_LOCAL
));
83 cl_git_pass(git_branch_delete(branch
));
84 git_reference_free(branch
);
87 void test_refs_branches_delete__can_delete_a_local_branch(void)
89 git_reference
*branch
;
90 cl_git_pass(git_branch_lookup(&branch
, repo
, "br2", GIT_BRANCH_LOCAL
));
91 cl_git_pass(git_branch_delete(branch
));
92 git_reference_free(branch
);
95 void test_refs_branches_delete__can_delete_a_remote_branch(void)
97 git_reference
*branch
;
98 cl_git_pass(git_branch_lookup(&branch
, repo
, "nulltoken/master", GIT_BRANCH_REMOTE
));
99 cl_git_pass(git_branch_delete(branch
));
100 git_reference_free(branch
);
103 void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
105 git_reference
*branch
;
107 assert_config_entry_existence(repo
, "branch.track-local.remote", true);
108 assert_config_entry_existence(repo
, "branch.track-local.merge", true);
110 cl_git_pass(git_branch_lookup(&branch
, repo
, "track-local", GIT_BRANCH_LOCAL
));
111 cl_git_pass(git_branch_delete(branch
));
112 git_reference_free(branch
);
114 assert_config_entry_existence(repo
, "branch.track-local.remote", false);
115 assert_config_entry_existence(repo
, "branch.track-local.merge", false);
118 void test_refs_branches_delete__removes_reflog(void)
120 git_reference
*branch
;
122 git_oid oidzero
= {{0}};
125 /* Ensure the reflog has at least one entry */
126 cl_git_pass(git_signature_now(&sig
, "Me", "user@example.com"));
127 cl_git_pass(git_reflog_read(&log
, repo
, "refs/heads/track-local"));
128 cl_git_pass(git_reflog_append(log
, &oidzero
, sig
, "message"));
129 cl_assert(git_reflog_entrycount(log
) > 0);
130 git_signature_free(sig
);
131 git_reflog_free(log
);
133 cl_git_pass(git_branch_lookup(&branch
, repo
, "track-local", GIT_BRANCH_LOCAL
));
134 cl_git_pass(git_branch_delete(branch
));
135 git_reference_free(branch
);
137 cl_assert_equal_i(false, git_reference_has_log(repo
, "refs/heads/track-local"));
139 /* Reading a nonexistant reflog creates it, but it should be empty */
140 cl_git_pass(git_reflog_read(&log
, repo
, "refs/heads/track-local"));
141 cl_assert_equal_i(0, git_reflog_entrycount(log
));
142 git_reflog_free(log
);
145 void test_refs_branches_delete__removes_empty_folders(void)
147 const char *commondir
= git_repository_commondir(repo
);
150 git_reference
*branch
;
153 git_oid oidzero
= {{0}};
156 git_buf ref_folder
= GIT_BUF_INIT
;
157 git_buf reflog_folder
= GIT_BUF_INIT
;
159 /* Create a new branch with a nested name */
160 cl_git_pass(git_oid_fromstr(&commit_id
, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
161 cl_git_pass(git_commit_lookup(&commit
, repo
, &commit_id
));
162 cl_git_pass(git_branch_create(&branch
, repo
, "some/deep/ref", commit
, 0));
163 git_commit_free(commit
);
165 /* Ensure the reflog has at least one entry */
166 cl_git_pass(git_signature_now(&sig
, "Me", "user@example.com"));
167 cl_git_pass(git_reflog_read(&log
, repo
, "refs/heads/some/deep/ref"));
168 cl_git_pass(git_reflog_append(log
, &oidzero
, sig
, "message"));
169 cl_assert(git_reflog_entrycount(log
) > 0);
170 git_signature_free(sig
);
171 git_reflog_free(log
);
173 cl_git_pass(git_buf_joinpath(&ref_folder
, commondir
, "refs/heads/some/deep"));
174 cl_git_pass(git_buf_join3(&reflog_folder
, '/', commondir
, GIT_REFLOG_DIR
, "refs/heads/some/deep"));
176 cl_assert(git_path_exists(git_buf_cstr(&ref_folder
)) == true);
177 cl_assert(git_path_exists(git_buf_cstr(&reflog_folder
)) == true);
179 cl_git_pass(git_branch_delete(branch
));
181 cl_assert(git_path_exists(git_buf_cstr(&ref_folder
)) == false);
182 cl_assert(git_path_exists(git_buf_cstr(&reflog_folder
)) == false);
184 git_reference_free(branch
);
185 git_buf_dispose(&ref_folder
);
186 git_buf_dispose(&reflog_folder
);