]> git.proxmox.com Git - libgit2.git/blame - tests/libgit2/refs/revparse.c
Merge https://salsa.debian.org/debian/libgit2 into proxmox/bullseye
[libgit2.git] / tests / libgit2 / refs / revparse.c
CommitLineData
ac250c56
BS
1#include "clar_libgit2.h"
2
3#include "git2/revparse.h"
35bed94f 4#include "refs.h"
5#include "path.h"
ac250c56
BS
6
7static git_repository *g_repo;
2ebc3c66 8static git_object *g_obj;
ac250c56 9
387d01b8 10/* Helpers */
e841c533 11static void test_object_and_ref_inrepo(
12 const char *spec,
13 const char *expected_oid,
14 const char *expected_refname,
15 git_repository *repo,
16 bool assert_reference_retrieval)
ac250c56 17{
e28dd29b 18 char objstr[64] = {0};
2ebc3c66 19 git_object *obj = NULL;
e841c533 20 git_reference *ref = NULL;
1decf88b 21 int error;
27ee8483 22
e841c533 23 error = git_revparse_ext(&obj, &ref, repo, spec);
27ee8483 24
1decf88b 25 if (expected_oid != NULL) {
753e17b0 26 cl_git_pass(error);
2ebc3c66 27 git_oid_fmt(objstr, git_object_id(obj));
1decf88b 28 cl_assert_equal_s(objstr, expected_oid);
29 } else
753e17b0 30 cl_git_fail(error);
2ebc3c66 31
e841c533 32 if (assert_reference_retrieval) {
33 if (expected_refname == NULL)
34 cl_assert(NULL == ref);
35 else
36 cl_assert_equal_s(expected_refname, git_reference_name(ref));
37 }
38
2ebc3c66 39 git_object_free(obj);
e841c533 40 git_reference_free(ref);
41}
42
43static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo)
44{
45 test_object_and_ref_inrepo(spec, expected_oid, NULL, repo, false);
1decf88b 46}
ac250c56 47
8480eef7
BS
48static void test_id_inrepo(
49 const char *spec,
50 const char *expected_left,
51 const char *expected_right,
c25aa7cd 52 git_revspec_t expected_flags,
8480eef7
BS
53 git_repository *repo)
54{
cbda09d0
VM
55 git_revspec revspec;
56 int error = git_revparse(&revspec, repo, spec);
8480eef7
BS
57
58 if (expected_left) {
59 char str[64] = {0};
60 cl_assert_equal_i(0, error);
cbda09d0 61 git_oid_fmt(str, git_object_id(revspec.from));
8480eef7 62 cl_assert_equal_s(str, expected_left);
cbda09d0 63 git_object_free(revspec.from);
8480eef7
BS
64 } else {
65 cl_assert_equal_i(GIT_ENOTFOUND, error);
66 }
67
68 if (expected_right) {
69 char str[64] = {0};
cbda09d0 70 git_oid_fmt(str, git_object_id(revspec.to));
8480eef7 71 cl_assert_equal_s(str, expected_right);
cbda09d0 72 git_object_free(revspec.to);
8480eef7
BS
73 }
74
75 if (expected_flags)
cbda09d0 76 cl_assert_equal_i(expected_flags, revspec.flags);
8480eef7
BS
77}
78
35bed94f 79static void test_object(const char *spec, const char *expected_oid)
80{
81 test_object_inrepo(spec, expected_oid, g_repo);
82}
83
e841c533 84static void test_object_and_ref(const char *spec, const char *expected_oid, const char *expected_refname)
85{
86 test_object_and_ref_inrepo(spec, expected_oid, expected_refname, g_repo, true);
87}
88
b208d900
GP
89static void test_rangelike(const char *rangelike,
90 const char *expected_left,
91 const char *expected_right,
c25aa7cd 92 git_revspec_t expected_revparseflags)
b208d900
GP
93{
94 char objstr[64] = {0};
cbda09d0 95 git_revspec revspec;
b208d900
GP
96 int error;
97
cbda09d0 98 error = git_revparse(&revspec, g_repo, rangelike);
b208d900
GP
99
100 if (expected_left != NULL) {
101 cl_assert_equal_i(0, error);
cbda09d0
VM
102 cl_assert_equal_i(revspec.flags, expected_revparseflags);
103 git_oid_fmt(objstr, git_object_id(revspec.from));
b208d900 104 cl_assert_equal_s(objstr, expected_left);
cbda09d0 105 git_oid_fmt(objstr, git_object_id(revspec.to));
b208d900
GP
106 cl_assert_equal_s(objstr, expected_right);
107 } else
108 cl_assert(error != 0);
299a224b 109
cbda09d0
VM
110 git_object_free(revspec.from);
111 git_object_free(revspec.to);
b208d900
GP
112}
113
114
8480eef7
BS
115static void test_id(
116 const char *spec,
117 const char *expected_left,
118 const char *expected_right,
c25aa7cd 119 git_revspec_t expected_flags)
8480eef7
BS
120{
121 test_id_inrepo(spec, expected_left, expected_right, expected_flags, g_repo);
122}
123
8b107dc5
WB
124static void test_invalid_revspec(const char* invalid_spec)
125{
126 git_revspec revspec;
127
128 cl_assert_equal_i(
129 GIT_EINVALIDSPEC, git_revparse(&revspec, g_repo, invalid_spec));
130}
131
ac250c56
BS
132void test_refs_revparse__initialize(void)
133{
34922eee 134 cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
ac250c56
BS
135}
136
137void test_refs_revparse__cleanup(void)
138{
34922eee 139 git_repository_free(g_repo);
ac250c56
BS
140}
141
9d7bdf71
BS
142void test_refs_revparse__nonexistant_object(void)
143{
ce9e8e11 144 test_object("this-does-not-exist", NULL);
3e82d6c6 145 test_object("this-does-not-exist^1", NULL);
146 test_object("this-does-not-exist~2", NULL);
ce9e8e11 147}
1decf88b 148
2ebc3c66 149static void assert_invalid_single_spec(const char *invalid_spec)
ce9e8e11 150{
cc146626 151 cl_assert_equal_i(
2ebc3c66 152 GIT_EINVALIDSPEC, git_revparse_single(&g_obj, g_repo, invalid_spec));
cc146626 153}
279b45b0 154
cc146626 155void test_refs_revparse__invalid_reference_name(void)
156{
2ebc3c66
BS
157 assert_invalid_single_spec("this doesn't make sense");
158 assert_invalid_single_spec("Inv@{id");
159 assert_invalid_single_spec("");
9d7bdf71
BS
160}
161
ac250c56
BS
162void test_refs_revparse__shas(void)
163{
e28dd29b 164 test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd");
165 test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
ac250c56
BS
166}
167
168void test_refs_revparse__head(void)
169{
e28dd29b 170 test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
d448392e 171 test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
172 test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
173 test_object("master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
ac250c56
BS
174}
175
176void test_refs_revparse__full_refs(void)
177{
e28dd29b 178 test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
179 test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d");
180 test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
ac250c56
BS
181}
182
183void test_refs_revparse__partial_refs(void)
184{
e28dd29b 185 test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08");
186 test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
187 test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f");
ac250c56
BS
188}
189
190void test_refs_revparse__describe_output(void)
191{
e28dd29b 192 test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
193 test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
ac250c56
BS
194}
195
023c6f69
BS
196void test_refs_revparse__nth_parent(void)
197{
2ebc3c66
BS
198 assert_invalid_single_spec("be3563a^-1");
199 assert_invalid_single_spec("^");
200 assert_invalid_single_spec("be3563a^{tree}^");
201 assert_invalid_single_spec("point_to_blob^{blob}^");
202 assert_invalid_single_spec("this doesn't make sense^1");
d448392e 203
e28dd29b 204 test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
205 test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
206 test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
207 test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
d448392e 208 test_object("be3563a^^", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
e28dd29b 209 test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
210 test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
d448392e 211 test_object("be3563a^{commit}^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
0e7af9e7 212
1decf88b 213 test_object("be3563a^42", NULL);
9d7bdf71
BS
214}
215
216void test_refs_revparse__not_tag(void)
217{
e28dd29b 218 test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
219 test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
d448392e 220 test_object("master^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
221 test_object("master^{tree}^{}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
222 test_object("e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
223 test_object("tags/e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
224 test_object("e908^{}", "e90810b8df3e80c413d903f631643c716887138d");
9d7bdf71
BS
225}
226
227void test_refs_revparse__to_type(void)
228{
2ebc3c66 229 assert_invalid_single_spec("wrapped_tag^{trip}");
cc146626 230 test_object("point_to_blob^{commit}", NULL);
231 cl_assert_equal_i(
753e17b0 232 GIT_EPEEL, git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
d448392e 233
e28dd29b 234 test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
235 test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
236 test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
d448392e 237 test_object("master^{commit}^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
023c6f69
BS
238}
239
38533d5a
BS
240void test_refs_revparse__linear_history(void)
241{
2ebc3c66 242 assert_invalid_single_spec("~");
cc146626 243 test_object("foo~bar", NULL);
244
2ebc3c66
BS
245 assert_invalid_single_spec("master~bar");
246 assert_invalid_single_spec("master~-1");
247 assert_invalid_single_spec("master~0bar");
248 assert_invalid_single_spec("this doesn't make sense~2");
249 assert_invalid_single_spec("be3563a^{tree}~");
250 assert_invalid_single_spec("point_to_blob^{blob}~");
eb6bc45f 251
e28dd29b 252 test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
253 test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
254 test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a");
255 test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
d448392e 256 test_object("master~~", "9fd738e8f7967c078dceed8190330fc8648ee56a");
38533d5a
BS
257}
258
259void test_refs_revparse__chaining(void)
260{
2ebc3c66
BS
261 assert_invalid_single_spec("master@{0}@{0}");
262 assert_invalid_single_spec("@{u}@{-1}");
263 assert_invalid_single_spec("@{-1}@{-1}");
264 assert_invalid_single_spec("@{-3}@{0}");
d448392e 265
266 test_object("master@{0}~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
267 test_object("@{u}@{0}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
268 test_object("@{-1}@{0}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
269 test_object("@{-4}@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
e28dd29b 270 test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
271 test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
272 test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
d448392e 273 test_object("master^^2^", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
e28dd29b 274 test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479");
d448392e 275 test_object("master^^1^2^1", NULL);
38533d5a
BS
276}
277
3cd90893 278void test_refs_revparse__upstream(void)
279{
2ebc3c66
BS
280 assert_invalid_single_spec("e90810b@{u}");
281 assert_invalid_single_spec("refs/tags/e90810b@{u}");
cc146626 282 test_object("refs/heads/e90810b@{u}", NULL);
3cd90893 283
284 test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
285 test_object("@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
286 test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
287 test_object("heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
288 test_object("refs/heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
289}
290
3d78ab64 291void test_refs_revparse__ordinal(void)
023c6f69 292{
2ebc3c66 293 assert_invalid_single_spec("master@{-2}");
e579e0f7 294
d448392e 295 /* TODO: make the test below actually fail
2ebc3c66 296 * cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{1a}"));
d448392e 297 */
a51bdbcf 298
1decf88b 299 test_object("nope@{0}", NULL);
300 test_object("master@{31415}", NULL);
3d78ab64 301 test_object("@{1000}", NULL);
b8460c20 302 test_object("@{2}", NULL);
08ac23a5 303
e28dd29b 304 test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
305 test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
3d78ab64 306
307 test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
308 test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
309 test_object("heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
310 test_object("refs/heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
311}
312
313void test_refs_revparse__previous_head(void)
314{
2ebc3c66
BS
315 assert_invalid_single_spec("@{-xyz}");
316 assert_invalid_single_spec("@{-0}");
317 assert_invalid_single_spec("@{-1b}");
3d78ab64 318
319 test_object("@{-42}", NULL);
320
321 test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
322 test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
886f183a
BS
323}
324
35bed94f 325static void create_fake_stash_reference_and_reflog(git_repository *repo)
326{
d00d5464 327 git_reference *master, *new_master;
e579e0f7 328 git_str log_path = GIT_STR_INIT;
35bed94f 329
e579e0f7 330 git_str_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
35bed94f 331
e579e0f7 332 cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&log_path)));
35bed94f 333
334 cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
659cf202 335 cl_git_pass(git_reference_rename(&new_master, master, "refs/fakestash", 0, NULL));
d00d5464 336 git_reference_free(master);
35bed94f 337
e579e0f7 338 cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&log_path)));
35bed94f 339
e579e0f7 340 git_str_dispose(&log_path);
d00d5464 341 git_reference_free(new_master);
35bed94f 342}
343
344void test_refs_revparse__reflog_of_a_ref_under_refs(void)
345{
346 git_repository *repo = cl_git_sandbox_init("testrepo.git");
347
348 test_object_inrepo("refs/fakestash", NULL, repo);
349
350 create_fake_stash_reference_and_reflog(repo);
351
352 /*
353 * $ git reflog -1 refs/fakestash
354 * a65fedf refs/fakestash@{0}: commit: checking in
355 *
356 * $ git reflog -1 refs/fakestash@{0}
357 * a65fedf refs/fakestash@{0}: commit: checking in
358 *
359 * $ git reflog -1 fakestash
360 * a65fedf fakestash@{0}: commit: checking in
361 *
362 * $ git reflog -1 fakestash@{0}
363 * a65fedf fakestash@{0}: commit: checking in
364 */
365 test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
366 test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
367 test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
368 test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
369
370 cl_git_sandbox_cleanup();
371}
372
886f183a
BS
373void test_refs_revparse__revwalk(void)
374{
cc146626 375 test_object("master^{/not found in any commit}", NULL);
376 test_object("master^{/merge}", NULL);
2ebc3c66 377 assert_invalid_single_spec("master^{/((}");
886f183a 378
e28dd29b 379 test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
380 test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
381 test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
382 test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a");
886f183a
BS
383}
384
385void test_refs_revparse__date(void)
386{
494ae940 387 /*
388 * $ git reflog HEAD --date=iso
389 * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master
390 * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in
391 * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2
392 * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit:
393 * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes
394 *
395 * $ git reflog HEAD --date=raw
396 * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master
397 * a4a7dce HEAD@{1335806617 -0900}: commit: checking in
398 * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2
399 * a65fedf HEAD@{1335806603 -0900}: commit:
400 * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
401 */
494ae940 402 test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
403 test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
404 test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
405
406 /*
407 * $ git reflog master --date=iso
408 * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in
409 * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src...
410 *
411 * $ git reflog master --date=raw
412 * a65fedf master@{1335806603 -0800}: commit: checking in
413 * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso
414 */
415
416
417 /*
e579e0f7
MB
418 * $ git rev-parse "master@{2012-04-30 17:22:42 +0000}"
419 * warning: log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800
420 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
494ae940 421 */
e579e0f7
MB
422 test_object("master@{2012-04-30 17:22:42 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
423 test_object("master@{2012-04-30 09:22:42 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
494ae940 424
425 /*
426 * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
427 * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg
428 */
429 test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
430 test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
431
432 /*
433 * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}"
434 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
435 */
436 test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
437
438 /*
439 * $ git reflog -1 master@{2012-05-03}
440 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
441 */
442 test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
443
444 /*
445 * $ git reflog -1 "master@{1335806603}"
446 * a65fedf
447 *
448 * $ git reflog -1 "master@{1335806602}"
449 * be3563a
450 */
451 test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
452 test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
e579e0f7
MB
453
454 /*
455 * $ git rev-parse "with-empty-log@{2 days ago}" --
456 * fatal: log for refs/heads/with-empty-log is empty
457 */
458 test_object("with-empty-log@{2 days ago}", NULL);
459}
460
461void test_refs_revparse__invalid_date(void)
462{
463 /*
464 * $ git rev-parse HEAD@{} --
465 * fatal: bad revision 'HEAD@{}'
466 *
467 * $ git rev-parse HEAD@{NEITHER_INTEGER_NOR_DATETIME} --
468 * fatal: bad revision 'HEAD@{NEITHER_INTEGER_NOR_DATETIME}'
469 */
470 test_object("HEAD@{}", NULL);
471 test_object("HEAD@{NEITHER_INTEGER_NOR_DATETIME}", NULL);
023c6f69 472}
244d2f6b
BS
473
474void test_refs_revparse__colon(void)
475{
2ebc3c66
BS
476 assert_invalid_single_spec(":/");
477 assert_invalid_single_spec("point_to_blob:readme.txt");
478 cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); /* Not implemented */
e28dd29b 479
1decf88b 480 test_object(":/not found in any commit", NULL);
481 test_object("subtrees:ab/42.txt", NULL);
482 test_object("subtrees:ab/4.txt/nope", NULL);
483 test_object("subtrees:nope", NULL);
484 test_object("test/master^1:branch_file.txt", NULL);
5b68ba7e 485
bb89cf94 486 /* From tags */
487 test_object("test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
488 test_object("tags/test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
489 test_object("e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
490 test_object("tags/e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
491
492 /* From commits */
493 test_object("a65f:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
494
495 /* From trees */
496 test_object("a65f^{tree}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
497 test_object("944c:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
498
499 /* Retrieving trees */
faaa7c51 500 test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
501 test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12");
502 test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3");
bb89cf94 503 test_object("subtrees:ab/", "f1425cef211cc08caa31e7b545ffb232acb098c3");
faaa7c51 504
bb89cf94 505 /* Retrieving blobs */
e28dd29b 506 test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f");
507 test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b");
508 test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
509 test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
510 test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f");
511 test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
512 test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
0d23c62c 513 test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
d448392e 514 test_object("test/master@{1}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
244d2f6b 515}
d1b7921a 516
517void test_refs_revparse__disambiguation(void)
518{
519 /*
520 * $ git show e90810b
521 * tag e90810b
522 * Tagger: Vicent Marti <tanoku@gmail.com>
523 * Date: Thu Aug 12 03:59:17 2010 +0200
524 *
525 * This is a very simple tag.
526 *
527 * commit e90810b8df3e80c413d903f631643c716887138d
528 * Author: Vicent Marti <tanoku@gmail.com>
529 * Date: Thu Aug 5 18:42:20 2010 +0200
530 *
531 * Test commit 2
532 *
533 * diff --git a/readme.txt b/readme.txt
534 * index 6336846..0266163 100644
535 * --- a/readme.txt
536 * +++ b/readme.txt
537 * @@ -1 +1,2 @@
538 * Testing a readme.txt
539 * +Now we add a single line here
540 *
541 * $ git show-ref e90810b
542 * 7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b
543 *
544 */
545 test_object("e90810b", "7b4384978d2493e851f9cca7858815fac9b10980");
546
547 /*
548 * $ git show e90810
549 * commit e90810b8df3e80c413d903f631643c716887138d
550 * Author: Vicent Marti <tanoku@gmail.com>
551 * Date: Thu Aug 5 18:42:20 2010 +0200
552 *
553 * Test commit 2
554 *
555 * diff --git a/readme.txt b/readme.txt
556 * index 6336846..0266163 100644
557 * --- a/readme.txt
558 * +++ b/readme.txt
559 * @@ -1 +1,2 @@
560 * Testing a readme.txt
561 * +Now we add a single line here
562 */
563 test_object("e90810", "e90810b8df3e80c413d903f631643c716887138d");
564}
c9de8611
BS
565
566void test_refs_revparse__a_too_short_objectid_returns_EAMBIGUOUS(void)
567{
cc146626 568 cl_assert_equal_i(
2ebc3c66 569 GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "e90"));
c9de8611 570}
5912d74c 571
209f9b67 572/*
573 * $ echo "aabqhq" | git hash-object -t blob --stdin
574 * dea509d0b3cb8ee0650f6ca210bc83f4678851ba
e579e0f7 575 *
209f9b67 576 * $ echo "aaazvc" | git hash-object -t blob --stdin
577 * dea509d097ce692e167dfc6a48a7a280cc5e877e
578 */
579void test_refs_revparse__a_not_precise_enough_objectid_returns_EAMBIGUOUS(void)
580{
581 git_repository *repo;
582 git_index *index;
583 git_object *obj;
584
585 repo = cl_git_sandbox_init("testrepo");
586
587 cl_git_mkfile("testrepo/one.txt", "aabqhq\n");
588 cl_git_mkfile("testrepo/two.txt", "aaazvc\n");
e579e0f7 589
209f9b67 590 cl_git_pass(git_repository_index(&index, repo));
591 cl_git_pass(git_index_add_bypath(index, "one.txt"));
592 cl_git_pass(git_index_add_bypath(index, "two.txt"));
e579e0f7 593
209f9b67 594 cl_git_fail_with(git_revparse_single(&obj, repo, "dea509d0"), GIT_EAMBIGUOUS);
595
596 cl_git_pass(git_revparse_single(&obj, repo, "dea509d09"));
597
598 git_object_free(obj);
599 git_index_free(index);
600 cl_git_sandbox_cleanup();
601}
602
5912d74c 603void test_refs_revparse__issue_994(void)
604{
605 git_repository *repo;
606 git_reference *head, *with_at;
607 git_object *target;
e579e0f7 608
5912d74c 609 repo = cl_git_sandbox_init("testrepo.git");
610
611 cl_assert_equal_i(GIT_ENOTFOUND,
2ebc3c66 612 git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
5912d74c 613
614 cl_assert_equal_i(GIT_ENOTFOUND,
2ebc3c66 615 git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
5912d74c 616
617
618 cl_git_pass(git_repository_head(&head, repo));
2508cc66 619 cl_git_pass(git_reference_create(
5912d74c 620 &with_at,
621 repo,
622 "refs/remotes/origin/bim_with_3d@11296",
2508cc66 623 git_reference_target(head),
0b28217b 624 0,
659cf202 625 NULL));
5912d74c 626
2ebc3c66 627 cl_git_pass(git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
5912d74c 628 git_object_free(target);
629
2ebc3c66 630 cl_git_pass(git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
5912d74c 631 git_object_free(target);
632
633 git_reference_free(with_at);
634 git_reference_free(head);
635 cl_git_sandbox_cleanup();
636}
545b479a 637
638/**
639 * $ git rev-parse blah-7-gc47800c
640 * c47800c7266a2be04c571c04d5a6614691ea99bd
1fed6b07 641 *
545b479a 642 * $ git rev-parse HEAD~3
643 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
1fed6b07 644 *
545b479a 645 * $ git branch blah-7-gc47800c HEAD~3
1fed6b07 646 *
545b479a 647 * $ git rev-parse blah-7-gc47800c
648 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
649 */
650void test_refs_revparse__try_to_retrieve_branch_before_described_tag(void)
651{
652 git_repository *repo;
653 git_reference *branch;
654 git_object *target;
655 char sha[GIT_OID_HEXSZ + 1];
656
657 repo = cl_git_sandbox_init("testrepo.git");
658
659 test_object_inrepo("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);
660
2ebc3c66 661 cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
6bfb990d 662 cl_git_pass(git_branch_create(&branch, repo, "blah-7-gc47800c", (git_commit *)target, 0));
545b479a 663
664 git_oid_tostr(sha, GIT_OID_HEXSZ + 1, git_object_id(target));
665
666 test_object_inrepo("blah-7-gc47800c", sha, repo);
667
668 git_reference_free(branch);
669 git_object_free(target);
670 cl_git_sandbox_cleanup();
671}
0e8e5a61 672
673/**
674 * $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
675 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
1fed6b07 676 *
0e8e5a61 677 * $ git rev-parse HEAD~3
678 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
1fed6b07 679 *
0e8e5a61 680 * $ git branch a65fedf39aefe402d3bb6e24df4d4f5fe4547750 HEAD~3
1fed6b07 681 *
0e8e5a61 682 * $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
683 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
1fed6b07 684 *
0e8e5a61 685 * $ git rev-parse heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750
686 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
687 */
688void test_refs_revparse__try_to_retrieve_sha_before_branch(void)
689{
690 git_repository *repo;
691 git_reference *branch;
692 git_object *target;
693 char sha[GIT_OID_HEXSZ + 1];
694
695 repo = cl_git_sandbox_init("testrepo.git");
696
697 test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
698
2ebc3c66 699 cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
6bfb990d 700 cl_git_pass(git_branch_create(&branch, repo, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", (git_commit *)target, 0));
0e8e5a61 701
702 git_oid_tostr(sha, GIT_OID_HEXSZ + 1, git_object_id(target));
703
704 test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
705 test_object_inrepo("heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750", sha, repo);
706
707 git_reference_free(branch);
708 git_object_free(target);
709 cl_git_sandbox_cleanup();
710}
711
712/**
713 * $ git rev-parse c47800
714 * c47800c7266a2be04c571c04d5a6614691ea99bd
1fed6b07 715 *
0e8e5a61 716 * $ git rev-parse HEAD~3
717 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
1fed6b07 718 *
0e8e5a61 719 * $ git branch c47800 HEAD~3
1fed6b07 720 *
0e8e5a61 721 * $ git rev-parse c47800
722 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
723 */
724void test_refs_revparse__try_to_retrieve_branch_before_abbrev_sha(void)
725{
726 git_repository *repo;
727 git_reference *branch;
728 git_object *target;
729 char sha[GIT_OID_HEXSZ + 1];
730
731 repo = cl_git_sandbox_init("testrepo.git");
732
733 test_object_inrepo("c47800", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);
734
2ebc3c66 735 cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
6bfb990d 736 cl_git_pass(git_branch_create(&branch, repo, "c47800", (git_commit *)target, 0));
0e8e5a61 737
738 git_oid_tostr(sha, GIT_OID_HEXSZ + 1, git_object_id(target));
739
740 test_object_inrepo("c47800", sha, repo);
741
742 git_reference_free(branch);
743 git_object_free(target);
744 cl_git_sandbox_cleanup();
745}
b208d900
GP
746
747
748void test_refs_revparse__range(void)
749{
2ebc3c66
BS
750 assert_invalid_single_spec("be3563a^1..be3563a");
751
b208d900
GP
752 test_rangelike("be3563a^1..be3563a",
753 "9fd738e8f7967c078dceed8190330fc8648ee56a",
754 "be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
c25aa7cd 755 GIT_REVSPEC_RANGE);
b208d900
GP
756
757 test_rangelike("be3563a^1...be3563a",
758 "9fd738e8f7967c078dceed8190330fc8648ee56a",
759 "be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
c25aa7cd 760 GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
b208d900
GP
761
762 test_rangelike("be3563a^1.be3563a", NULL, NULL, 0);
763}
8480eef7 764
8480eef7
BS
765void test_refs_revparse__parses_range_operator(void)
766{
c25aa7cd 767 test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
36c2dfed
VM
768 test_id("HEAD~3..HEAD",
769 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
770 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
c25aa7cd 771 GIT_REVSPEC_RANGE);
36c2dfed
VM
772
773 test_id("HEAD~3...HEAD",
774 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
775 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
c25aa7cd 776 GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
8b107dc5
WB
777
778 test_id("HEAD~3..",
779 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
780 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
c25aa7cd 781 GIT_REVSPEC_RANGE);
8b107dc5
WB
782
783 test_id("HEAD~3...",
784 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
785 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
c25aa7cd 786 GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
8b107dc5
WB
787
788 test_id("..HEAD~3",
789 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
790 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
c25aa7cd 791 GIT_REVSPEC_RANGE);
8b107dc5
WB
792
793 test_id("...HEAD~3",
794 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
795 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
c25aa7cd 796 GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
8b107dc5
WB
797
798 test_id("...",
799 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
800 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
c25aa7cd 801 GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
8b107dc5
WB
802
803 test_invalid_revspec("..");
8480eef7 804}
e841c533 805
806void test_refs_revparse__ext_retrieves_both_the_reference_and_its_target(void)
807{
808 test_object_and_ref(
809 "master@{upstream}",
810 "be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
811 "refs/remotes/test/master");
812
813 test_object_and_ref(
814 "@{-1}",
815 "a4a7dce85cf63874e984719f4fdd239f5145052f",
816 "refs/heads/br2");
817}
818
f672cd2a 819void test_refs_revparse__ext_can_expand_short_reference_names(void)
e841c533 820{
821 test_object_and_ref(
822 "master",
823 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
f672cd2a 824 "refs/heads/master");
80fd31fa 825
826 test_object_and_ref(
827 "HEAD",
828 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
829 "refs/heads/master");
830
831 test_object_and_ref(
832 "tags/test",
833 "b25fa35b38051e4ae45d4222e795f9df2e43f1d1",
834 "refs/tags/test");
835}
836
837void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_revision(void)
838{
839 test_object_and_ref(
840 "HEAD~3",
841 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
842 NULL);
843
844 test_object_and_ref(
845 "HEAD~0",
846 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
847 NULL);
848
849 test_object_and_ref(
850 "HEAD^0",
851 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
852 NULL);
853
854 test_object_and_ref(
855 "@{-1}@{0}",
856 "a4a7dce85cf63874e984719f4fdd239f5145052f",
857 NULL);
858}
859
860void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_tree_content(void)
861{
862 test_object_and_ref(
863 "tags/test:readme.txt",
864 "0266163a49e280c4f5ed1e08facd36a2bd716bcf",
865 NULL);
e841c533 866}
a2f8d1ae
VM
867
868void test_refs_revparse__uneven_sizes(void)
869{
870 test_object("a65fedf39aefe402d3bb6e24df4d4f5fe454775",
871 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
872
873 test_object("a65fedf39aefe402d3bb6e24df4d4f5fe45477",
874 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
875
876 test_object("a65fedf39aefe402d3bb6e24df4d4f5fe4547",
877 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
878
879 test_object("a65fedf39aefe402d3bb6e24df4d",
880 "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
881}
e579e0f7
MB
882
883void test_refs_revparse__parses_at_head(void)
884{
885 test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
886 test_id("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
887 test_id("@", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
888}