]> git.proxmox.com Git - libgit2.git/blame - tests/refs/branches/delete.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / refs / branches / delete.c
CommitLineData
731df570 1#include "clar_libgit2.h"
2#include "refs.h"
cd1ef822 3#include "repo/repo_helpers.h"
0b98a8a4 4#include "config/config_helpers.h"
22a2d3d5 5#include "futils.h"
ac3d33df 6#include "reflog.h"
731df570 7
8static git_repository *repo;
9static git_reference *fake_remote;
10
11void test_refs_branches_delete__initialize(void)
12{
13 git_oid id;
14
99dfa470 15 repo = cl_git_sandbox_init("testrepo.git");
731df570 16
17 cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
659cf202 18 cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
731df570 19}
20
21void test_refs_branches_delete__cleanup(void)
22{
23 git_reference_free(fake_remote);
9094d30b
SC
24 fake_remote = NULL;
25
99dfa470 26 cl_git_sandbox_cleanup();
9094d30b 27 repo = NULL;
731df570 28}
29
731df570 30void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
31{
32 git_reference *head;
1c947daa 33 git_reference *branch;
731df570 34
35 /* Ensure HEAD targets the local master branch */
36 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
a7f8065f 37 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
731df570 38 git_reference_free(head);
39
1c947daa
VM
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);
731df570 43}
44
0532e7bb 45void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
731df570 46{
47 git_reference *head;
0532e7bb 48 git_reference *branch;
731df570 49
50 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
51 git_reference_delete(head);
d00d5464 52 git_reference_free(head);
731df570 53
1c947daa 54 cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
8b05bea8 55 cl_git_pass(git_branch_delete(branch));
d00d5464 56 git_reference_free(branch);
8b05bea8 57}
58
605da51a 59void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
8b05bea8 60{
8b05bea8 61 git_reference *branch;
62
605da51a 63 make_head_unborn(repo, NON_EXISTING_HEAD);
8b05bea8 64
65 cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
0532e7bb 66 cl_git_pass(git_branch_delete(branch));
d00d5464 67 git_reference_free(branch);
731df570 68}
69
70void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
71{
209e34fa 72 git_reference *head, *branch;
731df570 73
209e34fa 74 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
ac3d33df 75 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC, git_reference_type(head));
2508cc66 76 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
731df570 77 git_reference_free(head);
209e34fa 78
79 /* Detach HEAD and make it target the commit that "master" points to */
4e498646 80 git_repository_detach_head(repo);
731df570 81
1c947daa
VM
82 cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
83 cl_git_pass(git_branch_delete(branch));
d00d5464 84 git_reference_free(branch);
731df570 85}
86
87void test_refs_branches_delete__can_delete_a_local_branch(void)
88{
1c947daa
VM
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));
d00d5464 92 git_reference_free(branch);
731df570 93}
94
95void test_refs_branches_delete__can_delete_a_remote_branch(void)
96{
1c947daa
VM
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));
d00d5464 100 git_reference_free(branch);
731df570 101}
0b98a8a4 102
103void 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));
d00d5464 112 git_reference_free(branch);
0b98a8a4 113
114 assert_config_entry_existence(repo, "branch.track-local.remote", false);
115 assert_config_entry_existence(repo, "branch.track-local.merge", false);
2508cc66 116}
48110f67
BS
117
118void 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
01d0c02d
CMN
137 cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));
138
e579e0f7 139 /* Reading a non-existent reflog creates it, but it should be empty */
48110f67
BS
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
ac3d33df
JK
145void 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
e579e0f7
MB
156 git_str ref_folder = GIT_STR_INIT;
157 git_str reflog_folder = GIT_STR_INIT;
ac3d33df
JK
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
e579e0f7
MB
173 cl_git_pass(git_str_joinpath(&ref_folder, commondir, "refs/heads/some/deep"));
174 cl_git_pass(git_str_join3(&reflog_folder, '/', commondir, GIT_REFLOG_DIR, "refs/heads/some/deep"));
ac3d33df 175
e579e0f7
MB
176 cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == true);
177 cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == true);
ac3d33df
JK
178
179 cl_git_pass(git_branch_delete(branch));
180
e579e0f7
MB
181 cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == false);
182 cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == false);
ac3d33df
JK
183
184 git_reference_free(branch);
e579e0f7
MB
185 git_str_dispose(&ref_folder);
186 git_str_dispose(&reflog_folder);
ac3d33df
JK
187}
188