3 # Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 # disable automatic packing for these tests
20 export GOT_TEST_PACK=""
22 test_pack_all_loose_objects() {
23 local testroot=`test_init pack_all_loose_objects`
25 # tags should also be packed
26 got tag -r $testroot/repo -m 1.0 1.0 >/dev/null
28 # no pack files should exist yet
29 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
31 if [ $ret -ne 0 ]; then
32 test_done "$testroot" "$ret"
35 echo -n > $testroot/stdout.expected
36 cmp -s $testroot/stdout.expected $testroot/stdout
38 if [ $ret -ne 0 ]; then
39 diff -u $testroot/stdout.expected $testroot/stdout
40 test_done "$testroot" "$ret"
44 gotadmin pack -r $testroot/repo > $testroot/stdout
46 if [ $ret -ne 0 ]; then
47 echo "gotadmin pack failed unexpectedly" >&2
48 test_done "$testroot" "$ret"
51 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
52 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
55 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
60 if grep -q ^$obj_id $testroot/stdout; then
63 echo "loose object $obj_id was not packed" >&2
67 if [ $ret -eq 1 ]; then
72 test_done "$testroot" "$ret"
76 local testroot=`test_init pack_exclude`
77 local commit0=`git_show_head $testroot/repo`
79 # no pack files should exist yet
80 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
82 if [ $ret -ne 0 ]; then
83 test_done "$testroot" "$ret"
86 echo -n > $testroot/stdout.expected
87 cmp -s $testroot/stdout.expected $testroot/stdout
89 if [ $ret -ne 0 ]; then
90 diff -u $testroot/stdout.expected $testroot/stdout
91 test_done "$testroot" "$ret"
95 got branch -r $testroot/repo mybranch
97 if [ $ret -ne 0 ]; then
98 test_done "$testroot" "$ret"
102 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
104 if [ $ret -ne 0 ]; then
105 test_done "$testroot" "$ret"
109 echo a new line >> $testroot/wt/alpha
110 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
112 gotadmin pack -r $testroot/repo -x master > $testroot/stdout
114 if [ $ret -ne 0 ]; then
115 echo "gotadmin pack failed unexpectedly" >&2
116 test_done "$testroot" "$ret"
119 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
120 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
123 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
125 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
127 excluded_ids="$excluded_ids $commit0 $tree0"
128 for id in $excluded_ids; do
130 if grep -q ^$id $testroot/stdout; then
131 echo "found excluded object $id in pack file" >&2
134 if [ $ret -eq 1 ]; then
138 if [ $ret -eq 1 ]; then
139 test_done "$testroot" "$ret"
143 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
149 for id in $excluded_ids; do
150 if [ "$obj_id" = "$id" ]; then
155 if [ "$excluded" = "1" ]; then
158 if grep -q ^$obj_id $testroot/stdout; then
161 echo "loose object $obj_id was not packed" >&2
165 if [ $ret -eq 1 ]; then
170 test_done "$testroot" "$ret"
173 test_pack_exclude_tag() {
174 local testroot=`test_init pack_exclude_tag`
175 local commit0=`git_show_head $testroot/repo`
177 # no pack files should exist yet
178 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
180 if [ $ret -ne 0 ]; then
181 test_done "$testroot" "$ret"
184 echo -n > $testroot/stdout.expected
185 cmp -s $testroot/stdout.expected $testroot/stdout
187 if [ $ret -ne 0 ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
193 got tag -r $testroot/repo -m 1.0 -c master 1.0 > /dev/null
195 if [ $ret -ne 0 ]; then
196 test_done "$testroot" "$ret"
200 got branch -r $testroot/repo mybranch
202 if [ $ret -ne 0 ]; then
203 test_done "$testroot" "$ret"
207 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
209 if [ $ret -ne 0 ]; then
210 test_done "$testroot" "$ret"
214 echo a new line >> $testroot/wt/alpha
215 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
217 gotadmin pack -r $testroot/repo -x refs/tags/1.0 > $testroot/stdout
219 if [ $ret -ne 0 ]; then
220 echo "gotadmin pack failed unexpectedly" >&2
221 test_done "$testroot" "$ret"
224 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
225 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
228 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
230 tag0=`got tag -l -r $testroot/repo | grep ^tag | cut -d ' ' -f3`
231 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
233 excluded_ids="$excluded_ids $commit0 $tree0 $tag0"
234 for id in $excluded_ids; do
236 if grep -q ^$id $testroot/stdout; then
237 echo "found excluded object $id in pack file" >&2
240 if [ $ret -eq 1 ]; then
244 if [ $ret -eq 1 ]; then
245 test_done "$testroot" "$ret"
249 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
255 for id in $excluded_ids; do
256 if [ "$obj_id" = "$id" ]; then
261 if [ "$excluded" = "1" ]; then
264 if grep -q ^$obj_id $testroot/stdout; then
267 echo "loose object $obj_id was not packed" >&2
271 if [ $ret -eq 1 ]; then
276 test_done "$testroot" "$ret"
279 test_pack_include() {
280 local testroot=`test_init pack_include`
281 local commit0=`git_show_head $testroot/repo`
283 # no pack files should exist yet
284 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
286 if [ $ret -ne 0 ]; then
287 test_done "$testroot" "$ret"
290 echo -n > $testroot/stdout.expected
291 cmp -s $testroot/stdout.expected $testroot/stdout
293 if [ $ret -ne 0 ]; then
294 diff -u $testroot/stdout.expected $testroot/stdout
295 test_done "$testroot" "$ret"
299 got branch -r $testroot/repo mybranch
301 if [ $ret -ne 0 ]; then
302 test_done "$testroot" "$ret"
306 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
308 if [ $ret -ne 0 ]; then
309 test_done "$testroot" "$ret"
313 echo a new line >> $testroot/wt/alpha
314 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
315 local commit1=`git_show_branch_head $testroot/repo mybranch`
317 gotadmin pack -r $testroot/repo master > $testroot/stdout
319 if [ $ret -ne 0 ]; then
320 echo "gotadmin pack failed unexpectedly" >&2
321 test_done "$testroot" "$ret"
324 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
325 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
328 tree1=`got cat -r $testroot/repo $commit1 | grep ^tree | \
330 alpha1=`got tree -r $testroot/repo -i -c $commit1 | \
331 grep "[0-9a-f] alpha$" | cut -d' ' -f 1`
332 excluded_ids="$alpha1 $commit1 $tree1"
333 for id in $excluded_ids; do
335 if grep -q ^$id $testroot/stdout; then
336 echo "found excluded object $id in pack file" >&2
339 if [ $ret -eq 1 ]; then
343 if [ $ret -eq 1 ]; then
344 test_done "$testroot" "$ret"
348 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
350 included_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
352 included_ids="$included_ids $commit0 $tree0"
353 for obj_id in $included_ids; do
354 for id in $excluded_ids; do
355 if [ "$obj_id" = "$id" ]; then
360 if [ "$excluded" = "1" ]; then
363 if grep -q ^$obj_id $testroot/stdout; then
366 echo "included object $obj_id was not packed" >&2
371 test_done "$testroot" "$ret"
374 test_pack_ambiguous_arg() {
375 local testroot=`test_init pack_ambiguous_arg`
376 local commit0=`git_show_head $testroot/repo`
378 # no pack files should exist yet
379 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
381 if [ $ret -ne 0 ]; then
382 test_done "$testroot" "$ret"
385 echo -n > $testroot/stdout.expected
386 cmp -s $testroot/stdout.expected $testroot/stdout
388 if [ $ret -ne 0 ]; then
389 diff -u $testroot/stdout.expected $testroot/stdout
390 test_done "$testroot" "$ret"
394 got branch -r $testroot/repo mybranch
396 if [ $ret -ne 0 ]; then
397 test_done "$testroot" "$ret"
401 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
403 if [ $ret -ne 0 ]; then
404 test_done "$testroot" "$ret"
408 echo a new line >> $testroot/wt/alpha
409 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
410 local commit1=`git_show_branch_head $testroot/repo mybranch`
412 gotadmin pack -r $testroot/repo -x master master \
413 > /dev/null 2> $testroot/stderr
415 if [ $ret -eq 0 ]; then
416 echo "gotadmin pack succeeded unexpectedly" >&2
417 test_done "$testroot" "1"
421 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
422 cmp -s $testroot/stderr.expected $testroot/stderr
424 if [ $ret -ne 0 ]; then
425 diff -u $testroot/stderr.expected $testroot/stderr
427 test_done "$testroot" "$ret"
430 test_pack_loose_only() {
431 local testroot=`test_init pack_loose_only`
432 local commit0=`git_show_head $testroot/repo`
434 # no pack files should exist yet
435 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
437 if [ $ret -ne 0 ]; then
438 test_done "$testroot" "$ret"
441 echo -n > $testroot/stdout.expected
442 cmp -s $testroot/stdout.expected $testroot/stdout
444 if [ $ret -ne 0 ]; then
445 diff -u $testroot/stdout.expected $testroot/stdout
446 test_done "$testroot" "$ret"
450 got branch -r $testroot/repo mybranch
452 if [ $ret -ne 0 ]; then
453 test_done "$testroot" "$ret"
457 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
459 if [ $ret -ne 0 ]; then
460 test_done "$testroot" "$ret"
464 echo a new line >> $testroot/wt/alpha
465 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
467 # pack objects belonging to the 'master' branch; its objects
468 # should then be excluded while packing 'mybranch' since they
470 gotadmin pack -r $testroot/repo master > /dev/null
472 if [ $ret -ne 0 ]; then
473 echo "gotadmin pack failed unexpectedly" >&2
474 test_done "$testroot" "$ret"
478 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
480 if [ $ret -ne 0 ]; then
481 echo "gotadmin pack failed unexpectedly" >&2
482 test_done "$testroot" "$ret"
485 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
486 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
489 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
491 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
493 excluded_ids="$excluded_ids $commit0 $tree0"
494 for id in $excluded_ids; do
496 if grep -q ^$id $testroot/stdout; then
497 echo "found excluded object $id in pack file" >&2
500 if [ $ret -eq 1 ]; then
504 if [ $ret -eq 1 ]; then
505 test_done "$testroot" "$ret"
509 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
515 for id in $excluded_ids; do
516 if [ "$obj_id" = "$id" ]; then
521 if [ "$excluded" = "1" ]; then
524 if grep -q ^$obj_id $testroot/stdout; then
527 echo "loose object $obj_id was not packed" >&2
531 if [ $ret -eq 1 ]; then
536 test_done "$testroot" "$ret"
539 test_pack_all_objects() {
540 local testroot=`test_init pack_all_objects`
541 local commit0=`git_show_head $testroot/repo`
543 # no pack files should exist yet
544 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
546 if [ $ret -ne 0 ]; then
547 test_done "$testroot" "$ret"
550 echo -n > $testroot/stdout.expected
551 cmp -s $testroot/stdout.expected $testroot/stdout
553 if [ $ret -ne 0 ]; then
554 diff -u $testroot/stdout.expected $testroot/stdout
555 test_done "$testroot" "$ret"
559 got branch -r $testroot/repo mybranch
561 if [ $ret -ne 0 ]; then
562 test_done "$testroot" "$ret"
566 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
568 if [ $ret -ne 0 ]; then
569 test_done "$testroot" "$ret"
573 echo a new line >> $testroot/wt/alpha
574 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
576 # pack objects belonging to the 'master' branch
577 gotadmin pack -r $testroot/repo master > /dev/null
579 if [ $ret -ne 0 ]; then
580 echo "gotadmin pack failed unexpectedly" >&2
581 test_done "$testroot" "$ret"
585 # pack mybranch, including already packed objects on the
586 # 'master' branch which are reachable from mybranch
587 gotadmin pack -r $testroot/repo -a mybranch > $testroot/stdout
589 if [ $ret -ne 0 ]; then
590 echo "gotadmin pack failed unexpectedly" >&2
591 test_done "$testroot" "$ret"
594 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
595 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
598 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
603 if grep -q ^$obj_id $testroot/stdout; then
606 echo "loose object $obj_id was not packed" >&2
610 if [ $ret -eq 1 ]; then
615 test_done "$testroot" "$ret"
618 test_pack_bad_ref() {
619 local testroot=`test_init pack_bad_ref`
620 local commit0=`git_show_head $testroot/repo`
622 # no pack files should exist yet
623 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
625 if [ $ret -ne 0 ]; then
626 test_done "$testroot" "$ret"
629 echo -n > $testroot/stdout.expected
630 cmp -s $testroot/stdout.expected $testroot/stdout
632 if [ $ret -ne 0 ]; then
633 diff -u $testroot/stdout.expected $testroot/stdout
634 test_done "$testroot" "$ret"
638 got branch -r $testroot/repo mybranch
640 if [ $ret -ne 0 ]; then
641 test_done "$testroot" "$ret"
645 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
647 if [ $ret -ne 0 ]; then
648 test_done "$testroot" "$ret"
652 gotadmin pack -r $testroot/repo refs/got/worktree/ \
653 > $testroot/stdout 2> $testroot/stderr
655 if [ $ret -eq 0 ]; then
656 echo "gotadmin pack succeeded unexpectedly" >&2
657 test_done "$testroot" "1"
661 echo -n > $testroot/stdout.expected
662 cmp -s $testroot/stdout.expected $testroot/stdout
664 if [ $ret -ne 0 ]; then
665 diff -u $testroot/stdout.expected $testroot/stdout
666 test_done "$testroot" "$ret"
670 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
671 cmp -s $testroot/stderr.expected $testroot/stderr
673 if [ $ret -ne 0 ]; then
674 diff -u $testroot/stderr.expected $testroot/stderr
676 test_done "$testroot" "$ret"
680 run_test test_pack_all_loose_objects
681 run_test test_pack_exclude
682 run_test test_pack_exclude_tag
683 run_test test_pack_include
684 run_test test_pack_ambiguous_arg
685 run_test test_pack_loose_only
686 run_test test_pack_all_objects
687 run_test test_pack_bad_ref