]> git.proxmox.com Git - libgit2.git/blame - tests-clar/refs/revparse.c
revparse: enhance refs/<name> coverage
[libgit2.git] / tests-clar / refs / revparse.c
CommitLineData
ac250c56
BS
1#include "clar_libgit2.h"
2
3#include "git2/revparse.h"
35bed94f 4#include "buffer.h"
5#include "refs.h"
6#include "path.h"
ac250c56
BS
7
8static git_repository *g_repo;
9static git_object *g_obj;
763b8381 10static char g_orig_tz[16] = {0};
ac250c56
BS
11
12
13
387d01b8 14/* Helpers */
35bed94f 15static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo)
ac250c56 16{
e28dd29b 17 char objstr[64] = {0};
1decf88b 18 git_object *obj = NULL;
19 int error;
27ee8483 20
35bed94f 21 error = git_revparse_single(&obj, repo, spec);
27ee8483 22
1decf88b 23 if (expected_oid != NULL) {
24 cl_assert_equal_i(0, error);
25 git_oid_fmt(objstr, git_object_id(obj));
26 cl_assert_equal_s(objstr, expected_oid);
27 } else
28 cl_assert_equal_i(GIT_ENOTFOUND, error);
ac250c56 29
1decf88b 30 git_object_free(obj);
31}
ac250c56 32
35bed94f 33static void test_object(const char *spec, const char *expected_oid)
34{
35 test_object_inrepo(spec, expected_oid, g_repo);
36}
37
ac250c56
BS
38void test_refs_revparse__initialize(void)
39{
e28dd29b 40 char *tz = cl_getenv("TZ");
41 if (tz)
42 strcpy(g_orig_tz, tz);
43 cl_setenv("TZ", "UTC");
34922eee 44
45 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
ac250c56
BS
46}
47
48void test_refs_revparse__cleanup(void)
49{
34922eee 50 git_repository_free(g_repo);
e28dd29b 51 g_obj = NULL;
52 cl_setenv("TZ", g_orig_tz);
ac250c56
BS
53}
54
9d7bdf71
BS
55void test_refs_revparse__nonexistant_object(void)
56{
1decf88b 57 test_object("this doesn't exist", NULL);
58
e28dd29b 59 cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist^1"));
60 cl_git_fail(git_revparse_single(&g_obj, g_repo, "this doesn't exist~2"));
9d7bdf71
BS
61}
62
ac250c56
BS
63void test_refs_revparse__shas(void)
64{
e28dd29b 65 test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd");
66 test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
ac250c56
BS
67}
68
69void test_refs_revparse__head(void)
70{
e28dd29b 71 test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
ac250c56
BS
72}
73
74void test_refs_revparse__full_refs(void)
75{
e28dd29b 76 test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
77 test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d");
78 test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
ac250c56
BS
79}
80
81void test_refs_revparse__partial_refs(void)
82{
e28dd29b 83 test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08");
84 test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
85 test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f");
ac250c56
BS
86}
87
88void test_refs_revparse__describe_output(void)
89{
e28dd29b 90 test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
91 test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
ac250c56
BS
92}
93
023c6f69
BS
94void test_refs_revparse__nth_parent(void)
95{
e28dd29b 96 test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
97 test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
98 test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
99 test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
100 test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
101 test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
0e7af9e7 102
1decf88b 103 test_object("be3563a^42", NULL);
9d7bdf71
BS
104}
105
106void test_refs_revparse__not_tag(void)
107{
e28dd29b 108 test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
109 test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
9d7bdf71
BS
110}
111
112void test_refs_revparse__to_type(void)
113{
e28dd29b 114 test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
115 test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
116 test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
7149a625 117
e28dd29b 118 cl_git_fail(git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
023c6f69
BS
119}
120
38533d5a
BS
121void test_refs_revparse__linear_history(void)
122{
e28dd29b 123 cl_git_fail(git_revparse_single(&g_obj, g_repo, "foo~bar"));
124 cl_git_fail(git_revparse_single(&g_obj, g_repo, "master~bar"));
eb6bc45f 125
e28dd29b 126 test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
127 test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
128 test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a");
129 test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
38533d5a
BS
130}
131
132void test_refs_revparse__chaining(void)
133{
e28dd29b 134 test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
135 test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
136 test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
137 test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479");
38533d5a
BS
138}
139
023c6f69
BS
140void test_refs_revparse__reflog(void)
141{
e28dd29b 142 cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-xyz}"));
143 cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{-0}"));
cab65c2b 144 cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{-2}"));
e28dd29b 145 cl_git_fail(git_revparse_single(&g_obj, g_repo, "@{1000}"));
a51bdbcf 146
1decf88b 147 test_object("nope@{0}", NULL);
148 test_object("master@{31415}", NULL);
08ac23a5 149
e28dd29b 150 test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
151 test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
152 test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
153 test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
154 test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
155 test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
156 test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
157 test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
886f183a
BS
158}
159
35bed94f 160static void create_fake_stash_reference_and_reflog(git_repository *repo)
161{
162 git_reference *master;
163 git_buf log_path = GIT_BUF_INIT;
164
165 git_buf_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
166
167 cl_assert_equal_i(false, git_path_isfile(git_buf_cstr(&log_path)));
168
169 cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
170 cl_git_pass(git_reference_rename(master, "refs/fakestash", 0));
171
172 cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&log_path)));
173
174 git_buf_free(&log_path);
175 git_reference_free(master);
176}
177
178void test_refs_revparse__reflog_of_a_ref_under_refs(void)
179{
180 git_repository *repo = cl_git_sandbox_init("testrepo.git");
181
182 test_object_inrepo("refs/fakestash", NULL, repo);
183
184 create_fake_stash_reference_and_reflog(repo);
185
186 /*
187 * $ git reflog -1 refs/fakestash
188 * a65fedf refs/fakestash@{0}: commit: checking in
189 *
190 * $ git reflog -1 refs/fakestash@{0}
191 * a65fedf refs/fakestash@{0}: commit: checking in
192 *
193 * $ git reflog -1 fakestash
194 * a65fedf fakestash@{0}: commit: checking in
195 *
196 * $ git reflog -1 fakestash@{0}
197 * a65fedf fakestash@{0}: commit: checking in
198 */
199 test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
200 test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
201 test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
202 test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
203
204 cl_git_sandbox_cleanup();
205}
206
886f183a
BS
207void test_refs_revparse__revwalk(void)
208{
e28dd29b 209 cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/not found in any commit}"));
210 cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/merge}"));
211 cl_git_fail(git_revparse_single(&g_obj, g_repo, "master^{/((}"));
886f183a 212
e28dd29b 213 test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
214 test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
215 test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
216 test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a");
886f183a
BS
217}
218
219void test_refs_revparse__date(void)
220{
494ae940 221 /*
222 * $ git reflog HEAD --date=iso
223 * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master
224 * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in
225 * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2
226 * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit:
227 * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes
228 *
229 * $ git reflog HEAD --date=raw
230 * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master
231 * a4a7dce HEAD@{1335806617 -0900}: commit: checking in
232 * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2
233 * a65fedf HEAD@{1335806603 -0900}: commit:
234 * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
235 */
1decf88b 236 test_object("HEAD@{10 years ago}", NULL);
a346992f 237
494ae940 238 test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
239 test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
240 test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
241
242 /*
243 * $ git reflog master --date=iso
244 * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in
245 * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src...
246 *
247 * $ git reflog master --date=raw
248 * a65fedf master@{1335806603 -0800}: commit: checking in
249 * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso
250 */
251
252
253 /*
254 * $ git reflog -1 "master@{2012-04-30 17:22:42 +0000}"
255 * warning: Log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800.
256 */
1decf88b 257 test_object("master@{2012-04-30 17:22:42 +0000}", NULL);
258 test_object("master@{2012-04-30 09:22:42 -0800}", NULL);
494ae940 259
260 /*
261 * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
262 * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg
263 */
264 test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
265 test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
266
267 /*
268 * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}"
269 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
270 */
271 test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
272
273 /*
274 * $ git reflog -1 master@{2012-05-03}
275 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
276 */
277 test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
278
279 /*
280 * $ git reflog -1 "master@{1335806603}"
281 * a65fedf
282 *
283 * $ git reflog -1 "master@{1335806602}"
284 * be3563a
285 */
286 test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
287 test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
023c6f69 288}
244d2f6b
BS
289
290void test_refs_revparse__colon(void)
291{
e28dd29b 292 cl_git_fail(git_revparse_single(&g_obj, g_repo, ":/"));
e28dd29b 293 cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README"));
e28dd29b 294
1decf88b 295 test_object(":/not found in any commit", NULL);
296 test_object("subtrees:ab/42.txt", NULL);
297 test_object("subtrees:ab/4.txt/nope", NULL);
298 test_object("subtrees:nope", NULL);
299 test_object("test/master^1:branch_file.txt", NULL);
5b68ba7e 300
faaa7c51 301 /* Trees */
302 test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
303 test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12");
304 test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3");
305
306 /* Blobs */
e28dd29b 307 test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f");
308 test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b");
309 test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
310 test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
311 test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f");
312 test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
313 test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
0d23c62c 314 test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
244d2f6b 315}