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 -q -r $testroot/repo -x master master 2> $testroot/stderr
414 if [ $ret -eq 0 ]; then
415 echo "gotadmin pack succeeded unexpectedly" >&2
416 test_done "$testroot" "1"
420 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
421 cmp -s $testroot/stderr.expected $testroot/stderr
423 if [ $ret -ne 0 ]; then
424 diff -u $testroot/stderr.expected $testroot/stderr
426 test_done "$testroot" "$ret"
429 test_pack_loose_only() {
430 local testroot=`test_init pack_loose_only`
431 local commit0=`git_show_head $testroot/repo`
433 # no pack files should exist yet
434 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
436 if [ $ret -ne 0 ]; then
437 test_done "$testroot" "$ret"
440 echo -n > $testroot/stdout.expected
441 cmp -s $testroot/stdout.expected $testroot/stdout
443 if [ $ret -ne 0 ]; then
444 diff -u $testroot/stdout.expected $testroot/stdout
445 test_done "$testroot" "$ret"
449 got branch -r $testroot/repo mybranch
451 if [ $ret -ne 0 ]; then
452 test_done "$testroot" "$ret"
456 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
458 if [ $ret -ne 0 ]; then
459 test_done "$testroot" "$ret"
463 echo a new line >> $testroot/wt/alpha
464 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
466 # pack objects belonging to the 'master' branch; its objects
467 # should then be excluded while packing 'mybranch' since they
469 gotadmin pack -q -r $testroot/repo master
471 if [ $ret -ne 0 ]; then
472 echo "gotadmin pack failed unexpectedly" >&2
473 test_done "$testroot" "$ret"
477 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
479 if [ $ret -ne 0 ]; then
480 echo "gotadmin pack failed unexpectedly" >&2
481 test_done "$testroot" "$ret"
484 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
485 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
488 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
490 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
492 excluded_ids="$excluded_ids $commit0 $tree0"
493 for id in $excluded_ids; do
495 if grep -q ^$id $testroot/stdout; then
496 echo "found excluded object $id in pack file" >&2
499 if [ $ret -eq 1 ]; then
503 if [ $ret -eq 1 ]; then
504 test_done "$testroot" "$ret"
508 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
514 for id in $excluded_ids; do
515 if [ "$obj_id" = "$id" ]; then
520 if [ "$excluded" = "1" ]; then
523 if grep -q ^$obj_id $testroot/stdout; then
526 echo "loose object $obj_id was not packed" >&2
530 if [ $ret -eq 1 ]; then
535 test_done "$testroot" "$ret"
538 test_pack_all_objects() {
539 local testroot=`test_init pack_all_objects`
540 local commit0=`git_show_head $testroot/repo`
542 # no pack files should exist yet
543 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
545 if [ $ret -ne 0 ]; then
546 test_done "$testroot" "$ret"
549 echo -n > $testroot/stdout.expected
550 cmp -s $testroot/stdout.expected $testroot/stdout
552 if [ $ret -ne 0 ]; then
553 diff -u $testroot/stdout.expected $testroot/stdout
554 test_done "$testroot" "$ret"
558 got branch -r $testroot/repo mybranch
560 if [ $ret -ne 0 ]; then
561 test_done "$testroot" "$ret"
565 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
567 if [ $ret -ne 0 ]; then
568 test_done "$testroot" "$ret"
572 echo a new line >> $testroot/wt/alpha
573 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
575 # pack objects belonging to the 'master' branch
576 gotadmin pack -q -r $testroot/repo master
578 if [ $ret -ne 0 ]; then
579 echo "gotadmin pack failed unexpectedly" >&2
580 test_done "$testroot" "$ret"
584 # pack mybranch, including already packed objects on the
585 # 'master' branch which are reachable from mybranch
586 gotadmin pack -r $testroot/repo -a mybranch > $testroot/stdout
588 if [ $ret -ne 0 ]; then
589 echo "gotadmin pack failed unexpectedly" >&2
590 test_done "$testroot" "$ret"
593 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
594 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
597 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
602 if grep -q ^$obj_id $testroot/stdout; then
605 echo "loose object $obj_id was not packed" >&2
609 if [ $ret -eq 1 ]; then
614 test_done "$testroot" "$ret"
617 test_pack_bad_ref() {
618 local testroot=`test_init pack_bad_ref`
619 local commit0=`git_show_head $testroot/repo`
621 # no pack files should exist yet
622 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
624 if [ $ret -ne 0 ]; then
625 test_done "$testroot" "$ret"
628 echo -n > $testroot/stdout.expected
629 cmp -s $testroot/stdout.expected $testroot/stdout
631 if [ $ret -ne 0 ]; then
632 diff -u $testroot/stdout.expected $testroot/stdout
633 test_done "$testroot" "$ret"
637 got branch -r $testroot/repo mybranch
639 if [ $ret -ne 0 ]; then
640 test_done "$testroot" "$ret"
644 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
646 if [ $ret -ne 0 ]; then
647 test_done "$testroot" "$ret"
651 gotadmin pack -r $testroot/repo refs/got/worktree/ \
652 > $testroot/stdout 2> $testroot/stderr
654 if [ $ret -eq 0 ]; then
655 echo "gotadmin pack succeeded unexpectedly" >&2
656 test_done "$testroot" "1"
660 echo -n > $testroot/stdout.expected
661 cmp -s $testroot/stdout.expected $testroot/stdout
663 if [ $ret -ne 0 ]; then
664 diff -u $testroot/stdout.expected $testroot/stdout
665 test_done "$testroot" "$ret"
669 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
670 cmp -s $testroot/stderr.expected $testroot/stderr
672 if [ $ret -ne 0 ]; then
673 diff -u $testroot/stderr.expected $testroot/stderr
675 test_done "$testroot" "$ret"
679 run_test test_pack_all_loose_objects
680 run_test test_pack_exclude
681 run_test test_pack_exclude_tag
682 run_test test_pack_include
683 run_test test_pack_ambiguous_arg
684 run_test test_pack_loose_only
685 run_test test_pack_all_objects
686 run_test test_pack_bad_ref