]>
Commit | Line | Data |
---|---|---|
f1ad25f6 | 1 | #include "clar_libgit2.h" |
2 | #include "refs.h" | |
3 | ||
4 | git_repository *repo; | |
5 | ||
6 | void test_repo_head__initialize(void) | |
7 | { | |
8 | repo = cl_git_sandbox_init("testrepo.git"); | |
9 | } | |
10 | ||
11 | void test_repo_head__cleanup(void) | |
12 | { | |
13 | cl_git_sandbox_cleanup(); | |
14 | } | |
15 | ||
16 | void test_repo_head__head_detached(void) | |
17 | { | |
18 | git_reference *ref; | |
19 | git_oid oid; | |
20 | ||
21 | cl_assert(git_repository_head_detached(repo) == 0); | |
22 | ||
23 | /* detach the HEAD */ | |
24 | git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"); | |
25 | cl_git_pass(git_reference_create_oid(&ref, repo, "HEAD", &oid, 1)); | |
26 | cl_assert(git_repository_head_detached(repo) == 1); | |
27 | git_reference_free(ref); | |
28 | ||
29 | /* take the reop back to it's original state */ | |
30 | cl_git_pass(git_reference_create_symbolic(&ref, repo, "HEAD", "refs/heads/master", 1)); | |
31 | cl_assert(git_repository_head_detached(repo) == 0); | |
32 | ||
33 | git_reference_free(ref); | |
34 | } | |
35 | ||
36 | void test_repo_head__head_orphan(void) | |
37 | { | |
38 | git_reference *ref; | |
39 | ||
40 | cl_assert(git_repository_head_orphan(repo) == 0); | |
41 | ||
42 | /* orphan HEAD */ | |
43 | cl_git_pass(git_reference_create_symbolic(&ref, repo, "HEAD", "refs/heads/orphan", 1)); | |
44 | cl_assert(git_repository_head_orphan(repo) == 1); | |
45 | git_reference_free(ref); | |
46 | ||
47 | /* take the reop back to it's original state */ | |
48 | cl_git_pass(git_reference_create_symbolic(&ref, repo, "HEAD", "refs/heads/master", 1)); | |
49 | cl_assert(git_repository_head_orphan(repo) == 0); | |
50 | ||
51 | git_reference_free(ref); | |
52 | } | |
3f4c3072 | 53 | |
44af67a8 | 54 | void test_repo_head__set_head_Attaches_HEAD_to_un_unborn_branch_when_the_branch_doesnt_exist(void) |
55 | { | |
56 | git_reference *head; | |
57 | ||
58 | cl_git_pass(git_repository_set_head(repo, "refs/heads/doesnt/exist/yet")); | |
59 | ||
60 | cl_assert_equal_i(false, git_repository_head_detached(repo)); | |
61 | ||
62 | cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo)); | |
63 | } | |
64 | ||
65 | void test_repo_head__set_head_Returns_ENOTFOUND_when_the_reference_doesnt_exist(void) | |
66 | { | |
67 | cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head(repo, "refs/tags/doesnt/exist/yet")); | |
68 | } | |
69 | ||
70 | void test_repo_head__set_head_Fails_when_the_reference_points_to_a_non_commitish(void) | |
71 | { | |
72 | cl_git_fail(git_repository_set_head(repo, "refs/tags/point_to_blob")); | |
73 | } | |
74 | ||
75 | void test_repo_head__set_head_Attaches_HEAD_when_the_reference_points_to_a_branch(void) | |
76 | { | |
77 | git_reference *head; | |
78 | ||
79 | cl_git_pass(git_repository_set_head(repo, "refs/heads/br2")); | |
80 | ||
81 | cl_assert_equal_i(false, git_repository_head_detached(repo)); | |
82 | ||
83 | cl_git_pass(git_repository_head(&head, repo)); | |
84 | cl_assert_equal_s("refs/heads/br2", git_reference_name(head)); | |
85 | ||
86 | git_reference_free(head); | |
87 | } | |
88 | ||
3f4c3072 | 89 | static void assert_head_is_correctly_detached(void) |
90 | { | |
91 | git_reference *head; | |
92 | git_object *commit; | |
93 | ||
94 | cl_assert_equal_i(true, git_repository_head_detached(repo)); | |
95 | ||
96 | cl_git_pass(git_repository_head(&head, repo)); | |
97 | ||
98 | cl_git_pass(git_object_lookup(&commit, repo, git_reference_oid(head), GIT_OBJ_COMMIT)); | |
99 | ||
100 | git_object_free(commit); | |
101 | git_reference_free(head); | |
102 | } | |
103 | ||
44af67a8 | 104 | void test_repo_head__set_head_Detaches_HEAD_when_the_reference_doesnt_point_to_a_branch(void) |
105 | { | |
106 | cl_git_pass(git_repository_set_head(repo, "refs/tags/test")); | |
107 | ||
108 | cl_assert_equal_i(true, git_repository_head_detached(repo)); | |
109 | ||
110 | assert_head_is_correctly_detached(); | |
111 | } | |
112 | ||
4ebe38bd | 113 | void test_repo_head__set_head_detached_Return_ENOTFOUND_when_the_object_doesnt_exist(void) |
114 | { | |
115 | git_oid oid; | |
116 | ||
117 | cl_git_pass(git_oid_fromstr(&oid, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef")); | |
118 | ||
119 | cl_assert_equal_i(GIT_ENOTFOUND, git_repository_set_head_detached(repo, &oid)); | |
120 | } | |
121 | ||
122 | void test_repo_head__set_head_detached_Fails_when_the_object_isnt_a_commitish(void) | |
123 | { | |
124 | git_object *blob; | |
125 | ||
126 | cl_git_pass(git_revparse_single(&blob, repo, "point_to_blob")); | |
127 | ||
128 | cl_git_fail(git_repository_set_head_detached(repo, git_object_id(blob))); | |
129 | ||
130 | git_object_free(blob); | |
131 | } | |
132 | ||
133 | void test_repo_head__set_head_detached_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) | |
134 | { | |
135 | git_object *tag; | |
136 | ||
137 | cl_git_pass(git_revparse_single(&tag, repo, "tags/test")); | |
138 | cl_assert_equal_i(GIT_OBJ_TAG, git_object_type(tag)); | |
139 | ||
140 | cl_git_pass(git_repository_set_head_detached(repo, git_object_id(tag))); | |
141 | ||
142 | assert_head_is_correctly_detached(); | |
143 | ||
144 | git_object_free(tag); | |
145 | } | |
146 | ||
3f4c3072 | 147 | void test_repo_head__detach_head_Detaches_HEAD_and_make_it_point_to_the_peeled_commit(void) |
148 | { | |
149 | cl_assert_equal_i(false, git_repository_head_detached(repo)); | |
150 | ||
151 | cl_git_pass(git_repository_detach_head(repo)); | |
152 | ||
153 | assert_head_is_correctly_detached(); | |
154 | } | |
155 | ||
156 | void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void) | |
157 | { | |
158 | git_reference *head; | |
159 | ||
160 | cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, "refs/tags/point_to_blob", 1)); | |
161 | ||
162 | cl_git_fail(git_repository_detach_head(repo)); | |
163 | ||
164 | git_reference_free(head); | |
165 | } |