delete.c 3.29 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));
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 53

	cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
	git_reference_delete(head);

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

void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
{
	git_reference *branch;

62
	make_head_orphaned(repo, NON_EXISTING_HEAD);
63 64

	cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
65
	cl_git_pass(git_branch_delete(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 78

	/* Detach HEAD and make it target the commit that "master" points to */
	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 83 84 85
}

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

void test_refs_branches_delete__can_delete_a_remote_branch(void)
{
93 94 95
	git_reference *branch;
	cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
	cl_git_pass(git_branch_delete(branch));
96
}
97 98 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));

	assert_config_entry_existence(repo, "branch.track-local.remote", false);
	assert_config_entry_existence(repo, "branch.track-local.merge", false);
110
}