]> git.proxmox.com Git - libgit2.git/blob - tests/refs/branches/delete.c
6093c7886583cd744cb0f0ba211c5c48156bc88e
[libgit2.git] / tests / refs / branches / delete.c
1 #include "clar_libgit2.h"
2 #include "refs.h"
3 #include "repo/repo_helpers.h"
4 #include "config/config_helpers.h"
5 #include "futils.h"
6 #include "reflog.h"
7
8 static git_repository *repo;
9 static git_reference *fake_remote;
10
11 void test_refs_branches_delete__initialize(void)
12 {
13 git_oid id;
14
15 repo = cl_git_sandbox_init("testrepo.git");
16
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));
19 }
20
21 void test_refs_branches_delete__cleanup(void)
22 {
23 git_reference_free(fake_remote);
24 fake_remote = NULL;
25
26 cl_git_sandbox_cleanup();
27 repo = NULL;
28 }
29
30 void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
31 {
32 git_reference *head;
33 git_reference *branch;
34
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);
39
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);
43 }
44
45 void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
46 {
47 git_reference *head;
48 git_reference *branch;
49
50 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
51 git_reference_delete(head);
52 git_reference_free(head);
53
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);
57 }
58
59 void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
60 {
61 git_reference *branch;
62
63 make_head_unborn(repo, NON_EXISTING_HEAD);
64
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);
68 }
69
70 void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
71 {
72 git_reference *head, *branch;
73
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);
78
79 /* Detach HEAD and make it target the commit that "master" points to */
80 git_repository_detach_head(repo);
81
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);
85 }
86
87 void test_refs_branches_delete__can_delete_a_local_branch(void)
88 {
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);
93 }
94
95 void test_refs_branches_delete__can_delete_a_remote_branch(void)
96 {
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);
101 }
102
103 void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
104 {
105 git_reference *branch;
106
107 assert_config_entry_existence(repo, "branch.track-local.remote", true);
108 assert_config_entry_existence(repo, "branch.track-local.merge", true);
109
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);
113
114 assert_config_entry_existence(repo, "branch.track-local.remote", false);
115 assert_config_entry_existence(repo, "branch.track-local.merge", false);
116 }
117
118 void test_refs_branches_delete__removes_reflog(void)
119 {
120 git_reference *branch;
121 git_reflog *log;
122 git_oid oidzero = {{0}};
123 git_signature *sig;
124
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);
132
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);
136
137 cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));
138
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);
143 }
144
145 void test_refs_branches_delete__removes_empty_folders(void)
146 {
147 const char *commondir = git_repository_commondir(repo);
148 git_oid commit_id;
149 git_commit *commit;
150 git_reference *branch;
151
152 git_reflog *log;
153 git_oid oidzero = {{0}};
154 git_signature *sig;
155
156 git_buf ref_folder = GIT_BUF_INIT;
157 git_buf reflog_folder = GIT_BUF_INIT;
158
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);
164
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);
172
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"));
175
176 cl_assert(git_path_exists(git_buf_cstr(&ref_folder)) == true);
177 cl_assert(git_path_exists(git_buf_cstr(&reflog_folder)) == true);
178
179 cl_git_pass(git_branch_delete(branch));
180
181 cl_assert(git_path_exists(git_buf_cstr(&ref_folder)) == false);
182 cl_assert(git_path_exists(git_buf_cstr(&reflog_folder)) == false);
183
184 git_reference_free(branch);
185 git_buf_dispose(&ref_folder);
186 git_buf_dispose(&reflog_folder);
187 }
188