delete.c 4.31 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 6 7 8 9 10 11 12

static git_repository *repo;
static git_reference *fake_remote;

void test_refs_branches_delete__initialize(void)
{
	git_oid id;

13
	repo = cl_git_sandbox_init("testrepo.git");
14 15

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

void test_refs_branches_delete__cleanup(void)
{
	git_reference_free(fake_remote);
22 23
	fake_remote = NULL;

24
	cl_git_sandbox_cleanup();
25
	repo = NULL;
26 27 28 29 30
}

void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
{
	git_reference *head;
31
	git_reference *branch;
32 33 34

	/* Ensure HEAD targets the local master branch */
	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
35
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
36 37
	git_reference_free(head);

38 39 40
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_fail(git_branch_delete(branch));
	git_reference_free(branch);
41 42
}

43
void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
44 45
{
	git_reference *head;
46
	git_reference *branch;
47 48 49

	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
	git_reference_delete(head);
50
	git_reference_free(head);
51

52
	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
53
	cl_git_pass(git_branch_delete(branch));
54
	git_reference_free(branch);
55 56
}

57
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
58 59 60
{
	git_reference *branch;

61
	make_head_unborn(repo, NON_EXISTING_HEAD);
62 63

	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
64
	cl_git_pass(git_branch_delete(branch));
65
	git_reference_free(branch);
66 67 68 69
}

void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
70
	git_reference *head, *branch;
71

72 73
	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
74
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
75
	git_reference_free(head);
76 77

	/* Detach HEAD and make it target the commit that "master" points to */
78
	git_repository_detach_head(repo);
79

80 81
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
82
	git_reference_free(branch);
83 84 85 86
}

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

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

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));
110
	git_reference_free(branch);
111 112 113

	assert_config_entry_existence(repo, "branch.track-local.remote", false);
	assert_config_entry_existence(repo, "branch.track-local.merge", false);
114
}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

void test_refs_branches_delete__removes_reflog(void)
{
	git_reference *branch;
	git_reflog *log;
	git_oid oidzero = {{0}};
	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);

135 136
	cl_assert_equal_i(false, git_reference_has_log(repo, "refs/heads/track-local"));

137 138 139 140 141 142
	/* Reading a nonexistant reflog creates it, but it should be empty */
	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);
}