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