]> git.proxmox.com Git - libgit2.git/blob - tests/revwalk/basic.c
2c8d885e24d9f1bd5b779a3b7c817b5b8c885a64
[libgit2.git] / tests / revwalk / basic.c
1 #include "clar_libgit2.h"
2
3 /*
4 * a4a7dce [0] Merge branch 'master' into br2
5 |\
6 | * 9fd738e [1] a fourth commit
7 | * 4a202b3 [2] a third commit
8 * | c47800c [3] branch commit one
9 |/
10 * 5b5b025 [5] another commit
11 * 8496071 [4] testing
12 */
13 static const char *commit_head = "a4a7dce85cf63874e984719f4fdd239f5145052f";
14
15 static const char *commit_ids[] = {
16 "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
17 "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
18 "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
19 "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
20 "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
21 "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
22 };
23
24 /* Careful: there are two possible topological sorts */
25 static const int commit_sorting_topo[][6] = {
26 {0, 1, 2, 3, 5, 4}, {0, 3, 1, 2, 5, 4}
27 };
28
29 static const int commit_sorting_time[][6] = {
30 {0, 3, 1, 2, 5, 4}
31 };
32
33 static const int commit_sorting_topo_reverse[][6] = {
34 {4, 5, 3, 2, 1, 0}, {4, 5, 2, 1, 3, 0}
35 };
36
37 static const int commit_sorting_time_reverse[][6] = {
38 {4, 5, 2, 1, 3, 0}
39 };
40
41 /* This is specified unsorted, so both combinations are possible */
42 static const int commit_sorting_segment[][6] = {
43 {1, 2, -1, -1, -1, -1}, {2, 1, -1, -1, -1, -1}
44 };
45
46 #define commit_count 6
47 static const int result_bytes = 24;
48
49
50 static int get_commit_index(git_oid *raw_oid)
51 {
52 int i;
53 char oid[GIT_OID_HEXSZ];
54
55 git_oid_fmt(oid, raw_oid);
56
57 for (i = 0; i < commit_count; ++i)
58 if (memcmp(oid, commit_ids[i], GIT_OID_HEXSZ) == 0)
59 return i;
60
61 return -1;
62 }
63
64 static int test_walk_only(git_revwalk *walk,
65 const int possible_results[][commit_count], int results_count)
66 {
67 git_oid oid;
68 int i;
69 int result_array[commit_count];
70
71 for (i = 0; i < commit_count; ++i)
72 result_array[i] = -1;
73
74 i = 0;
75 while (git_revwalk_next(&oid, walk) == 0) {
76 result_array[i++] = get_commit_index(&oid);
77 /*{
78 char str[GIT_OID_HEXSZ+1];
79 git_oid_fmt(str, &oid);
80 str[GIT_OID_HEXSZ] = 0;
81 printf(" %d) %s\n", i, str);
82 }*/
83 }
84
85 for (i = 0; i < results_count; ++i)
86 if (memcmp(possible_results[i],
87 result_array, result_bytes) == 0)
88 return 0;
89
90 return GIT_ERROR;
91 }
92
93 static int test_walk(git_revwalk *walk, const git_oid *root,
94 int flags, const int possible_results[][6], int results_count)
95 {
96 git_revwalk_sorting(walk, flags);
97 git_revwalk_push(walk, root);
98
99 return test_walk_only(walk, possible_results, results_count);
100 }
101
102 static git_repository *_repo = NULL;
103 static git_revwalk *_walk = NULL;
104 static const char *_fixture = NULL;
105
106 void test_revwalk_basic__initialize(void)
107 {
108 }
109
110 void test_revwalk_basic__cleanup(void)
111 {
112 git_revwalk_free(_walk);
113
114 if (_fixture)
115 cl_git_sandbox_cleanup();
116 else
117 git_repository_free(_repo);
118
119 _fixture = NULL;
120 _repo = NULL;
121 _walk = NULL;
122 }
123
124 static void revwalk_basic_setup_walk(const char *fixture)
125 {
126 if (fixture) {
127 _fixture = fixture;
128 _repo = cl_git_sandbox_init(fixture);
129 } else {
130 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
131 }
132
133 cl_git_pass(git_revwalk_new(&_walk, _repo));
134 }
135
136 void test_revwalk_basic__sorting_modes(void)
137 {
138 git_oid id;
139
140 revwalk_basic_setup_walk(NULL);
141
142 git_oid_fromstr(&id, commit_head);
143
144 cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME, commit_sorting_time, 1));
145 cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL, commit_sorting_topo, 2));
146 cl_git_pass(test_walk(_walk, &id, GIT_SORT_TIME | GIT_SORT_REVERSE, commit_sorting_time_reverse, 1));
147 cl_git_pass(test_walk(_walk, &id, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE, commit_sorting_topo_reverse, 2));
148 }
149
150 void test_revwalk_basic__glob_heads(void)
151 {
152 int i = 0;
153 git_oid oid;
154
155 revwalk_basic_setup_walk(NULL);
156
157 cl_git_pass(git_revwalk_push_glob(_walk, "heads"));
158
159 while (git_revwalk_next(&oid, _walk) == 0)
160 i++;
161
162 /* git log --branches --oneline | wc -l => 14 */
163 cl_assert_equal_i(i, 14);
164 }
165
166 void test_revwalk_basic__glob_heads_with_invalid(void)
167 {
168 int i;
169 git_oid oid;
170
171 revwalk_basic_setup_walk("testrepo");
172
173 cl_git_mkfile("testrepo/.git/refs/heads/garbage", "not-a-ref");
174 cl_git_pass(git_revwalk_push_glob(_walk, "heads"));
175
176 for (i = 0; !git_revwalk_next(&oid, _walk); ++i)
177 /* walking */;
178
179 /* git log --branches --oneline | wc -l => 16 */
180 cl_assert_equal_i(20, i);
181 }
182
183 void test_revwalk_basic__push_head(void)
184 {
185 int i = 0;
186 git_oid oid;
187
188 revwalk_basic_setup_walk(NULL);
189
190 cl_git_pass(git_revwalk_push_head(_walk));
191
192 while (git_revwalk_next(&oid, _walk) == 0) {
193 i++;
194 }
195
196 /* git log HEAD --oneline | wc -l => 7 */
197 cl_assert_equal_i(i, 7);
198 }
199
200 void test_revwalk_basic__sorted_after_reset(void)
201 {
202 int i = 0;
203 git_oid oid;
204
205 revwalk_basic_setup_walk(NULL);
206
207 git_oid_fromstr(&oid, commit_head);
208
209 /* push, sort, and test the walk */
210 cl_git_pass(git_revwalk_push(_walk, &oid));
211 git_revwalk_sorting(_walk, GIT_SORT_TIME);
212
213 cl_git_pass(test_walk_only(_walk, commit_sorting_time, 2));
214
215 /* reset, push, and test again - we should see all entries */
216 git_revwalk_reset(_walk);
217 cl_git_pass(git_revwalk_push(_walk, &oid));
218
219 while (git_revwalk_next(&oid, _walk) == 0)
220 i++;
221
222 cl_assert_equal_i(i, commit_count);
223 }
224
225 void test_revwalk_basic__push_head_hide_ref(void)
226 {
227 int i = 0;
228 git_oid oid;
229
230 revwalk_basic_setup_walk(NULL);
231
232 cl_git_pass(git_revwalk_push_head(_walk));
233 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
234
235 while (git_revwalk_next(&oid, _walk) == 0) {
236 i++;
237 }
238
239 /* git log HEAD --oneline --not refs/heads/packed-test | wc -l => 4 */
240 cl_assert_equal_i(i, 4);
241 }
242
243 void test_revwalk_basic__push_head_hide_ref_nobase(void)
244 {
245 int i = 0;
246 git_oid oid;
247
248 revwalk_basic_setup_walk(NULL);
249
250 cl_git_pass(git_revwalk_push_head(_walk));
251 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed"));
252
253 while (git_revwalk_next(&oid, _walk) == 0) {
254 i++;
255 }
256
257 /* git log HEAD --oneline --not refs/heads/packed | wc -l => 7 */
258 cl_assert_equal_i(i, 7);
259 }
260
261 /*
262 * $ git rev-list HEAD 5b5b02 ^refs/heads/packed-test
263 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
264 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
265 * c47800c7266a2be04c571c04d5a6614691ea99bd
266 * 9fd738e8f7967c078dceed8190330fc8648ee56a
267
268 * $ git log HEAD 5b5b02 --oneline --not refs/heads/packed-test | wc -l => 4
269 * a65fedf
270 * be3563a Merge branch 'br2'
271 * c47800c branch commit one
272 * 9fd738e a fourth commit
273 */
274 void test_revwalk_basic__multiple_push_1(void)
275 {
276 int i = 0;
277 git_oid oid;
278
279 revwalk_basic_setup_walk(NULL);
280
281 cl_git_pass(git_revwalk_push_head(_walk));
282
283 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
284
285 cl_git_pass(git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
286 cl_git_pass(git_revwalk_push(_walk, &oid));
287
288 while (git_revwalk_next(&oid, _walk) == 0)
289 i++;
290
291 cl_assert_equal_i(i, 4);
292 }
293
294 /*
295 * Difference between test_revwalk_basic__multiple_push_1 and
296 * test_revwalk_basic__multiple_push_2 is in the order reference
297 * refs/heads/packed-test and commit 5b5b02 are pushed.
298 * revwalk should return same commits in both the tests.
299
300 * $ git rev-list 5b5b02 HEAD ^refs/heads/packed-test
301 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
302 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
303 * c47800c7266a2be04c571c04d5a6614691ea99bd
304 * 9fd738e8f7967c078dceed8190330fc8648ee56a
305
306 * $ git log 5b5b02 HEAD --oneline --not refs/heads/packed-test | wc -l => 4
307 * a65fedf
308 * be3563a Merge branch 'br2'
309 * c47800c branch commit one
310 * 9fd738e a fourth commit
311 */
312 void test_revwalk_basic__multiple_push_2(void)
313 {
314 int i = 0;
315 git_oid oid;
316
317 revwalk_basic_setup_walk(NULL);
318
319 cl_git_pass(git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
320 cl_git_pass(git_revwalk_push(_walk, &oid));
321
322 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/packed-test"));
323
324 cl_git_pass(git_revwalk_push_head(_walk));
325
326 while (git_revwalk_next(&oid, _walk) == 0)
327 i++;
328
329 cl_assert_equal_i(i, 4);
330 }
331
332 void test_revwalk_basic__disallow_non_commit(void)
333 {
334 git_oid oid;
335
336 revwalk_basic_setup_walk(NULL);
337
338 cl_git_pass(git_oid_fromstr(&oid, "521d87c1ec3aef9824daf6d96cc0ae3710766d91"));
339 cl_git_fail(git_revwalk_push(_walk, &oid));
340 }
341
342 void test_revwalk_basic__hide_then_push(void)
343 {
344 git_oid oid;
345 int i = 0;
346
347 revwalk_basic_setup_walk(NULL);
348 cl_git_pass(git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
349
350 cl_git_pass(git_revwalk_hide(_walk, &oid));
351 cl_git_pass(git_revwalk_push(_walk, &oid));
352
353 while (git_revwalk_next(&oid, _walk) == 0)
354 i++;
355
356 cl_assert_equal_i(i, 0);
357 }
358
359 void test_revwalk_basic__topo_crash(void)
360 {
361 git_oid oid;
362 git_oid_fromstr(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
363
364 revwalk_basic_setup_walk(NULL);
365 git_revwalk_sorting(_walk, GIT_SORT_TOPOLOGICAL);
366
367 cl_git_pass(git_revwalk_push(_walk, &oid));
368 cl_git_pass(git_revwalk_hide(_walk, &oid));
369
370 git_revwalk_next(&oid, _walk);
371 }
372
373 void test_revwalk_basic__from_new_to_old(void)
374 {
375 git_oid from_oid, to_oid, oid;
376 int i = 0;
377
378 revwalk_basic_setup_walk(NULL);
379 git_revwalk_sorting(_walk, GIT_SORT_TIME);
380
381 cl_git_pass(git_oid_fromstr(&to_oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
382 cl_git_pass(git_oid_fromstr(&from_oid, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
383
384 cl_git_pass(git_revwalk_push(_walk, &to_oid));
385 cl_git_pass(git_revwalk_hide(_walk, &from_oid));
386
387 while (git_revwalk_next(&oid, _walk) == 0)
388 i++;
389
390 cl_assert_equal_i(i, 0);
391 }
392
393 void test_revwalk_basic__push_range(void)
394 {
395 revwalk_basic_setup_walk(NULL);
396
397 git_revwalk_reset(_walk);
398 git_revwalk_sorting(_walk, 0);
399 cl_git_pass(git_revwalk_push_range(_walk, "9fd738e~2..9fd738e"));
400 cl_git_pass(test_walk_only(_walk, commit_sorting_segment, 2));
401 }
402
403 void test_revwalk_basic__push_range_merge_base(void)
404 {
405 revwalk_basic_setup_walk(NULL);
406
407 git_revwalk_reset(_walk);
408 git_revwalk_sorting(_walk, 0);
409 cl_git_fail_with(GIT_EINVALIDSPEC, git_revwalk_push_range(_walk, "HEAD...HEAD~2"));
410 }
411
412 void test_revwalk_basic__push_range_no_range(void)
413 {
414 revwalk_basic_setup_walk(NULL);
415
416 git_revwalk_reset(_walk);
417 git_revwalk_sorting(_walk, 0);
418 cl_git_fail_with(GIT_EINVALIDSPEC, git_revwalk_push_range(_walk, "HEAD"));
419 }
420
421 void test_revwalk_basic__push_mixed(void)
422 {
423 git_oid oid;
424 int i = 0;
425
426 revwalk_basic_setup_walk(NULL);
427
428 git_revwalk_reset(_walk);
429 git_revwalk_sorting(_walk, 0);
430 cl_git_pass(git_revwalk_push_glob(_walk, "tags"));
431
432 while (git_revwalk_next(&oid, _walk) == 0) {
433 i++;
434 }
435
436 /* git rev-list --count --glob=tags #=> 9 */
437 cl_assert_equal_i(9, i);
438 }
439
440 void test_revwalk_basic__push_all(void)
441 {
442 git_oid oid;
443 int i = 0;
444
445 revwalk_basic_setup_walk(NULL);
446
447 git_revwalk_reset(_walk);
448 git_revwalk_sorting(_walk, 0);
449 cl_git_pass(git_revwalk_push_glob(_walk, "*"));
450
451 while (git_revwalk_next(&oid, _walk) == 0) {
452 i++;
453 }
454
455 /* git rev-list --count --all #=> 15 */
456 cl_assert_equal_i(15, i);
457 }
458
459 /*
460 * $ git rev-list br2 master e908
461 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
462 * e90810b8df3e80c413d903f631643c716887138d
463 * 6dcf9bf7541ee10456529833502442f385010c3d
464 * a4a7dce85cf63874e984719f4fdd239f5145052f
465 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
466 * c47800c7266a2be04c571c04d5a6614691ea99bd
467 * 9fd738e8f7967c078dceed8190330fc8648ee56a
468 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
469 * 5b5b025afb0b4c913b4c338a42934a3863bf3644
470 * 8496071c1b46c854b31185ea97743be6a8774479
471 */
472
473 void test_revwalk_basic__mimic_git_rev_list(void)
474 {
475 git_oid oid;
476
477 revwalk_basic_setup_walk(NULL);
478 git_revwalk_sorting(_walk, GIT_SORT_TIME);
479
480 cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/br2"));
481 cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/master"));
482 cl_git_pass(git_oid_fromstr(&oid, "e90810b8df3e80c413d903f631643c716887138d"));
483 cl_git_pass(git_revwalk_push(_walk, &oid));
484
485 cl_git_pass(git_revwalk_next(&oid, _walk));
486 cl_assert(!git_oid_streq(&oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
487
488 cl_git_pass(git_revwalk_next(&oid, _walk));
489 cl_assert(!git_oid_streq(&oid, "e90810b8df3e80c413d903f631643c716887138d"));
490
491 cl_git_pass(git_revwalk_next(&oid, _walk));
492 cl_assert(!git_oid_streq(&oid, "6dcf9bf7541ee10456529833502442f385010c3d"));
493
494 cl_git_pass(git_revwalk_next(&oid, _walk));
495 cl_assert(!git_oid_streq(&oid, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
496
497 cl_git_pass(git_revwalk_next(&oid, _walk));
498 cl_assert(!git_oid_streq(&oid, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
499
500 cl_git_pass(git_revwalk_next(&oid, _walk));
501 cl_assert(!git_oid_streq(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
502
503 cl_git_pass(git_revwalk_next(&oid, _walk));
504 cl_assert(!git_oid_streq(&oid, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
505
506 cl_git_pass(git_revwalk_next(&oid, _walk));
507 cl_assert(!git_oid_streq(&oid, "4a202b346bb0fb0db7eff3cffeb3c70babbd2045"));
508
509 cl_git_pass(git_revwalk_next(&oid, _walk));
510 cl_assert(!git_oid_streq(&oid, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
511
512 cl_git_pass(git_revwalk_next(&oid, _walk));
513 cl_assert(!git_oid_streq(&oid, "8496071c1b46c854b31185ea97743be6a8774479"));
514
515 cl_git_fail_with(git_revwalk_next(&oid, _walk), GIT_ITEROVER);
516 }
517
518 void test_revwalk_basic__big_timestamp(void)
519 {
520 git_reference *head;
521 git_commit *tip;
522 git_signature *sig;
523 git_tree *tree;
524 git_oid id;
525 int error;
526
527 revwalk_basic_setup_walk("testrepo.git");
528
529 cl_git_pass(git_repository_head(&head, _repo));
530 cl_git_pass(git_reference_peel((git_object **) &tip, head, GIT_OBJECT_COMMIT));
531
532 /* Commit with a far-ahead timestamp, we should be able to parse it in the revwalk */
533 cl_git_pass(git_signature_new(&sig, "Joe", "joe@example.com", INT64_C(2399662595), 0));
534 cl_git_pass(git_commit_tree(&tree, tip));
535
536 cl_git_pass(git_commit_create(&id, _repo, "HEAD", sig, sig, NULL, "some message", tree, 1,
537 (const git_commit **)&tip));
538
539 cl_git_pass(git_revwalk_push_head(_walk));
540
541 while ((error = git_revwalk_next(&id, _walk)) == 0) {
542 /* nothing */
543 }
544
545 cl_assert_equal_i(GIT_ITEROVER, error);
546
547 git_tree_free(tree);
548 git_commit_free(tip);
549 git_reference_free(head);
550 git_signature_free(sig);
551
552 }
553
554 /* Ensure that we correctly hide a commit that is (timewise) older
555 * than the commits that we are showing.
556 *
557 * % git rev-list 8e73b76..bd75801
558 * bd758010071961f28336333bc41e9c64c9a64866
559 */
560 void test_revwalk_basic__old_hidden_commit_one(void)
561 {
562 git_oid new_id, old_id, oid;
563
564 revwalk_basic_setup_walk("testrepo.git");
565
566 cl_git_pass(git_oid_fromstr(&new_id, "bd758010071961f28336333bc41e9c64c9a64866"));
567 cl_git_pass(git_revwalk_push(_walk, &new_id));
568
569 cl_git_pass(git_oid_fromstr(&old_id, "8e73b769e97678d684b809b163bebdae2911720f"));
570 cl_git_pass(git_revwalk_hide(_walk, &old_id));
571
572 cl_git_pass(git_revwalk_next(&oid, _walk));
573 cl_assert(!git_oid_streq(&oid, "bd758010071961f28336333bc41e9c64c9a64866"));
574
575 cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
576 }
577
578 /* Ensure that we correctly hide a commit that is (timewise) older
579 * than the commits that we are showing.
580 *
581 * % git rev-list bd75801 ^b91e763
582 * bd758010071961f28336333bc41e9c64c9a64866
583 */
584 void test_revwalk_basic__old_hidden_commit_two(void)
585 {
586 git_oid new_id, old_id, oid;
587
588 revwalk_basic_setup_walk("testrepo.git");
589
590 cl_git_pass(git_oid_fromstr(&new_id, "bd758010071961f28336333bc41e9c64c9a64866"));
591 cl_git_pass(git_revwalk_push(_walk, &new_id));
592
593 cl_git_pass(git_oid_fromstr(&old_id, "b91e763008b10db366442469339f90a2b8400d0a"));
594 cl_git_pass(git_revwalk_hide(_walk, &old_id));
595
596 cl_git_pass(git_revwalk_next(&oid, _walk));
597 cl_assert(!git_oid_streq(&oid, "bd758010071961f28336333bc41e9c64c9a64866"));
598
599 cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
600 }
601
602 /*
603 * Ensure that we correctly hide all parent commits of a newer
604 * commit when first hiding older commits.
605 *
606 * % git rev-list D ^B ^A ^E
607 * 790ba0facf6fd103699a5c40cd19dad277ff49cd
608 * b82cee5004151ae0c4f82b69fb71b87477664b6f
609 */
610 void test_revwalk_basic__newer_hidden_commit_hides_old_commits(void)
611 {
612 git_oid oid;
613
614 revwalk_basic_setup_walk("revwalk.git");
615
616 cl_git_pass(git_revwalk_push_ref(_walk, "refs/heads/D"));
617 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/B"));
618 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/A"));
619 cl_git_pass(git_revwalk_hide_ref(_walk, "refs/heads/E"));
620
621 cl_git_pass(git_revwalk_next(&oid, _walk));
622 cl_assert(git_oid_streq(&oid, "b82cee5004151ae0c4f82b69fb71b87477664b6f"));
623 cl_git_pass(git_revwalk_next(&oid, _walk));
624 cl_assert(git_oid_streq(&oid, "790ba0facf6fd103699a5c40cd19dad277ff49cd"));
625
626 cl_git_fail_with(GIT_ITEROVER, git_revwalk_next(&oid, _walk));
627 }