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