]> git.proxmox.com Git - libgit2.git/blame - tests/refs/branches/delete.c
Improve handling of fake home directory
[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"
731df570 5
6static git_repository *repo;
7static git_reference *fake_remote;
8
9void test_refs_branches_delete__initialize(void)
10{
11 git_oid id;
12
13 cl_fixture_sandbox("testrepo.git");
14 cl_git_pass(git_repository_open(&repo, "testrepo.git"));
15
16 cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
0b28217b 17 cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL, NULL));
731df570 18}
19
20void test_refs_branches_delete__cleanup(void)
21{
22 git_reference_free(fake_remote);
9094d30b
SC
23 fake_remote = NULL;
24
731df570 25 git_repository_free(repo);
9094d30b 26 repo = NULL;
731df570 27
28 cl_fixture_cleanup("testrepo.git");
29}
30
731df570 31void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
32{
33 git_reference *head;
1c947daa 34 git_reference *branch;
731df570 35
36 /* Ensure HEAD targets the local master branch */
37 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
a7f8065f 38 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
731df570 39 git_reference_free(head);
40
1c947daa
VM
41 cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
42 cl_git_fail(git_branch_delete(branch));
43 git_reference_free(branch);
731df570 44}
45
0532e7bb 46void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
731df570 47{
48 git_reference *head;
0532e7bb 49 git_reference *branch;
731df570 50
51 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
52 git_reference_delete(head);
d00d5464 53 git_reference_free(head);
731df570 54
1c947daa 55 cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
8b05bea8 56 cl_git_pass(git_branch_delete(branch));
d00d5464 57 git_reference_free(branch);
8b05bea8 58}
59
605da51a 60void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
8b05bea8 61{
8b05bea8 62 git_reference *branch;
63
605da51a 64 make_head_unborn(repo, NON_EXISTING_HEAD);
8b05bea8 65
66 cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
0532e7bb 67 cl_git_pass(git_branch_delete(branch));
d00d5464 68 git_reference_free(branch);
731df570 69}
70
71void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
72{
209e34fa 73 git_reference *head, *branch;
731df570 74
209e34fa 75 cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
76 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
2508cc66 77 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
731df570 78 git_reference_free(head);
209e34fa 79
80 /* Detach HEAD and make it target the commit that "master" points to */
010cec3a 81 git_repository_detach_head(repo, NULL, NULL);
731df570 82
1c947daa
VM
83 cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
84 cl_git_pass(git_branch_delete(branch));
d00d5464 85 git_reference_free(branch);
731df570 86}
87
88void test_refs_branches_delete__can_delete_a_local_branch(void)
89{
1c947daa
VM
90 git_reference *branch;
91 cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
92 cl_git_pass(git_branch_delete(branch));
d00d5464 93 git_reference_free(branch);
731df570 94}
95
96void test_refs_branches_delete__can_delete_a_remote_branch(void)
97{
1c947daa
VM
98 git_reference *branch;
99 cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
100 cl_git_pass(git_branch_delete(branch));
d00d5464 101 git_reference_free(branch);
731df570 102}
0b98a8a4 103
104void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
105{
106 git_reference *branch;
107
108 assert_config_entry_existence(repo, "branch.track-local.remote", true);
109 assert_config_entry_existence(repo, "branch.track-local.merge", true);
110
111 cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
112 cl_git_pass(git_branch_delete(branch));
d00d5464 113 git_reference_free(branch);
0b98a8a4 114
115 assert_config_entry_existence(repo, "branch.track-local.remote", false);
116 assert_config_entry_existence(repo, "branch.track-local.merge", false);
2508cc66 117}
48110f67
BS
118
119void test_refs_branches_delete__removes_reflog(void)
120{
121 git_reference *branch;
122 git_reflog *log;
123 git_oid oidzero = {{0}};
124 git_signature *sig;
125
126 /* Ensure the reflog has at least one entry */
127 cl_git_pass(git_signature_now(&sig, "Me", "user@example.com"));
128 cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
129 cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
130 cl_assert(git_reflog_entrycount(log) > 0);
131 git_signature_free(sig);
132 git_reflog_free(log);
133
134 cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
135 cl_git_pass(git_branch_delete(branch));
136 git_reference_free(branch);
137
138 /* Reading a nonexistant reflog creates it, but it should be empty */
139 cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
140 cl_assert_equal_i(0, git_reflog_entrycount(log));
141 git_reflog_free(log);
142}
143