delete.c 5.94 KB
Newer Older
1 2
#include "clar_libgit2.h"
#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 9 10 11 12 13 14

static git_repository *repo;
static git_reference *fake_remote;

void test_refs_branches_delete__initialize(void)
{
	git_oid id;

15
	repo = cl_git_sandbox_init("testrepo.git");
16

17
	cl_git_pass(git_oid__fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
18
	cl_git_pass(git_reference_create(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0, NULL));
19 20 21 22 23
}

void test_refs_branches_delete__cleanup(void)
{
	git_reference_free(fake_remote);
24 25
	fake_remote = NULL;

26
	cl_git_sandbox_cleanup();
27
	repo = NULL;
28 29 30 31 32
}

void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
{
	git_reference *head;
33
	git_reference *branch;
34 35 36

	/* Ensure HEAD targets the local master branch */
	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 39
	git_reference_free(head);

40 41 42
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_fail(git_branch_delete(branch));
	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 51

	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
	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 62
{
	git_reference *branch;

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 71
}

void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
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 83
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
84
	git_reference_free(branch);
85 86 87 88
}

void test_refs_branches_delete__can_delete_a_local_branch(void)
{
89 90 91
	git_reference *branch;
	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
92
	git_reference_free(branch);
93 94 95 96
}

void test_refs_branches_delete__can_delete_a_remote_branch(void)
{
97 98 99
	git_reference *branch;
	cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
	cl_git_pass(git_branch_delete(branch));
100
	git_reference_free(branch);
101
}
102 103 104 105 106 107 108 109 110 111

void test_refs_branches_delete__deleting_a_branch_removes_related_configuration_data(void)
{
	git_reference *branch;

	assert_config_entry_existence(repo, "branch.track-local.remote", true);
	assert_config_entry_existence(repo, "branch.track-local.merge", true);

	cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
112
	git_reference_free(branch);
113 114 115

	assert_config_entry_existence(repo, "branch.track-local.remote", false);
	assert_config_entry_existence(repo, "branch.track-local.merge", false);
116
}
117 118 119 120 121

void test_refs_branches_delete__removes_reflog(void)
{
	git_reference *branch;
	git_reflog *log;
122
	git_oid oidzero = GIT_OID_SHA1_ZERO;
123 124 125 126 127 128 129 130 131 132 133 134 135 136
	git_signature *sig;

	/* Ensure the reflog has at least one entry */
	cl_git_pass(git_signature_now(&sig, "Me", "user@example.com"));
	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
	cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
	cl_assert(git_reflog_entrycount(log) > 0);
	git_signature_free(sig);
	git_reflog_free(log);

	cl_git_pass(git_branch_lookup(&branch, repo, "track-local", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
	git_reference_free(branch);

137 138
	cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));

Dimitris Apostolou committed
139
	/* Reading a non-existent reflog creates it, but it should be empty */
140 141 142 143 144
	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/track-local"));
	cl_assert_equal_i(0, git_reflog_entrycount(log));
	git_reflog_free(log);
}

145 146 147 148 149 150 151 152
void test_refs_branches_delete__removes_empty_folders(void)
{
	const char *commondir = git_repository_commondir(repo);
	git_oid commit_id;
	git_commit *commit;
	git_reference *branch;

	git_reflog *log;
153
	git_oid oidzero = GIT_OID_SHA1_ZERO;
154 155
	git_signature *sig;

156 157
	git_str ref_folder = GIT_STR_INIT;
	git_str reflog_folder = GIT_STR_INIT;
158 159

	/* Create a new branch with a nested name */
160
	cl_git_pass(git_oid__fromstr(&commit_id, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1));
161 162 163 164 165 166 167 168 169 170 171 172
	cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
	cl_git_pass(git_branch_create(&branch, repo, "some/deep/ref", commit, 0));
	git_commit_free(commit);

	/* Ensure the reflog has at least one entry */
	cl_git_pass(git_signature_now(&sig, "Me", "user@example.com"));
	cl_git_pass(git_reflog_read(&log, repo, "refs/heads/some/deep/ref"));
	cl_git_pass(git_reflog_append(log, &oidzero, sig, "message"));
	cl_assert(git_reflog_entrycount(log) > 0);
	git_signature_free(sig);
	git_reflog_free(log);

173 174
	cl_git_pass(git_str_joinpath(&ref_folder, commondir, "refs/heads/some/deep"));
	cl_git_pass(git_str_join3(&reflog_folder, '/', commondir, GIT_REFLOG_DIR, "refs/heads/some/deep"));
175

176 177
	cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == true);
	cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == true);
178 179 180

	cl_git_pass(git_branch_delete(branch));

181 182
	cl_assert(git_fs_path_exists(git_str_cstr(&ref_folder)) == false);
	cl_assert(git_fs_path_exists(git_str_cstr(&reflog_folder)) == false);
183 184

	git_reference_free(branch);
185 186
	git_str_dispose(&ref_folder);
	git_str_dispose(&reflog_folder);
187 188
}