delete.c 4.34 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 13 14 15 16

static git_repository *repo;
static git_reference *fake_remote;

void test_refs_branches_delete__initialize(void)
{
	git_oid id;

	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));

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

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

25
	git_repository_free(repo);
26
	repo = NULL;
27 28 29 30 31 32 33

	cl_fixture_cleanup("testrepo.git");
}

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

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

41 42 43
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_fail(git_branch_delete(branch));
	git_reference_free(branch);
44 45
}

46
void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
47 48
{
	git_reference *head;
49
	git_reference *branch;
50 51 52

	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
	git_reference_delete(head);
53
	git_reference_free(head);
54

55
	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
56
	cl_git_pass(git_branch_delete(branch));
57
	git_reference_free(branch);
58 59
}

60
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_unborn(void)
61 62 63
{
	git_reference *branch;

64
	make_head_unborn(repo, NON_EXISTING_HEAD);
65 66

	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
67
	cl_git_pass(git_branch_delete(branch));
68
	git_reference_free(branch);
69 70 71 72
}

void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
73
	git_reference *head, *branch;
74

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

	/* Detach HEAD and make it target the commit that "master" points to */
81
	git_repository_detach_head(repo, NULL, NULL);
82

83 84
	cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
	cl_git_pass(git_branch_delete(branch));
85
	git_reference_free(branch);
86 87 88 89
}

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

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

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));
113
	git_reference_free(branch);
114 115 116

	assert_config_entry_existence(repo, "branch.track-local.remote", false);
	assert_config_entry_existence(repo, "branch.track-local.merge", false);
117
}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

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);

	/* 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);
}