head.c 4.72 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "refs.h"
3
#include "repo_helpers.h"
4
#include "posix.h"
5
#include "git2/annotated_commit.h"
6

7
static const char *g_email = "foo@example.com";
nulltoken committed
8
static git_repository *repo;
9 10 11 12

void test_repo_head__initialize(void)
{
	repo = cl_git_sandbox_init("testrepo.git");
13
	cl_git_pass(git_repository_set_ident(repo, "Foo Bar", g_email));
14 15 16 17 18 19 20
}

void test_repo_head__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

21
void test_repo_head__unborn_head(void)
22 23 24
{
	git_reference *ref;

nulltoken committed
25
	cl_git_pass(git_repository_head_detached(repo));
26

27
	make_head_unborn(repo, NON_EXISTING_HEAD);
28

29
	cl_assert(git_repository_head_unborn(repo) == 1);
30

31 32

	/* take the repo back to it's original state */
33
	cl_git_pass(git_reference_symbolic_create(&ref, repo, "HEAD", "refs/heads/master", 1, NULL));
34
	cl_assert(git_repository_head_unborn(repo) == 0);
35 36 37

	git_reference_free(ref);
}
38

39 40 41 42
void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_doesnt_exist(void)
{
	git_reference *head;

43
	cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet"));
44 45 46

	cl_assert_equal_i(false, git_repository_head_detached(repo));

47
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo));
48 49 50 51
}

void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void)
{
52
	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet"));
53 54 55 56
}

void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void)
{
57
	cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob"));
58 59 60 61 62 63
}

void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void)
{
	git_reference *head;

64
	cl_git_pass(git_repository_set_head(repo, "refs/heads/br2"));
65 66 67 68 69 70 71 72 73

	cl_assert_equal_i(false, git_repository_head_detached(repo));

	cl_git_pass(git_repository_head(&head, repo));
	cl_assert_equal_s("refs/heads/br2", git_reference_name(head));

	git_reference_free(head);
}

74 75 76 77 78 79 80 81 82
static void assert_head_is_correctly_detached(void)
{
	git_reference *head;
	git_object *commit;

	cl_assert_equal_i(true, git_repository_head_detached(repo));

	cl_git_pass(git_repository_head(&head, repo));

83
	cl_git_pass(git_object_lookup(&commit, repo, git_reference_target(head), GIT_OBJ_COMMIT));
84 85 86 87 88

	git_object_free(commit);
	git_reference_free(head);
}

89 90
void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void)
{
91
	cl_git_pass(git_repository_set_head(repo, "refs/tags/test"));
92 93 94 95 96 97

	cl_assert_equal_i(true, git_repository_head_detached(repo));

	assert_head_is_correctly_detached();
}

98 99 100 101 102 103
void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_exist(void)
{
	git_oid oid;

	cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));

104
	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid));
105 106 107 108 109 110 111 112
}

void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void)
{
	git_object *blob;

	cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob"));

113
	cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob)));
114 115 116 117 118 119 120 121 122 123 124

	git_object_free(blob);
}

void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void)
{
	git_object *tag;

	cl_git_pass(git_revparse_single(&tag, repo, "tags/test"));
	cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag));

125
	cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag)));
126 127 128 129 130 131

	assert_head_is_correctly_detached();

	git_object_free(tag);
}

132 133 134 135
void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void)
{
	cl_assert_equal_i(false, git_repository_head_detached(repo));

136
	cl_git_pass(git_repository_detach_head(repo));
137 138 139 140 141 142 143 144

	assert_head_is_correctly_detached();
}

void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void)
{
	git_reference *head;

145
	cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1, NULL));
146

147
	cl_git_fail(git_repository_detach_head(repo));
148 149 150

	git_reference_free(head);
}
151

152
void test_repo_head__detaching_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
153
{
154
	make_head_unborn(repo, NON_EXISTING_HEAD);
155

156
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_detach_head(repo));
157 158
}

159
void test_repo_head__retrieving_an_unborn_branch_returns_GIT_EUNBORNBRANCH(void)
160 161 162
{
	git_reference *head;

163
	make_head_unborn(repo, NON_EXISTING_HEAD);
164

165
	cl_assert_equal_i(GIT_EUNBORNBRANCH, git_repository_head(&head, repo));
166
}
167

168 169 170 171 172 173 174 175 176
void test_repo_head__retrieving_a_missing_head_returns_GIT_ENOTFOUND(void)
{
	git_reference *head;

	delete_head(repo);

	cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo));
}

177
void test_repo_head__can_tell_if_an_unborn_head_is_detached(void)
178
{
179
	make_head_unborn(repo, NON_EXISTING_HEAD);
180 181 182

	cl_assert_equal_i(false, git_repository_head_detached(repo));
}