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" != "0" ]; then
32 test_done "$testroot" "$ret"
35 echo -n > $testroot/stdout.expected
36 cmp -s $testroot/stdout.expected $testroot/stdout
38 if [ "$ret" != "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" != "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" = "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" != "0" ]; then
83 test_done "$testroot" "$ret"
86 echo -n > $testroot/stdout.expected
87 cmp -s $testroot/stdout.expected $testroot/stdout
89 if [ "$ret" != "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" != "0" ]; then
98 test_done "$testroot" "$ret"
102 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
104 if [ "$ret" != "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" != "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" = "1" ]; then
138 if [ "$ret" = "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" = "1" ]; then
170 test_done "$testroot" "$ret"
173 test_pack_include() {
174 local testroot=`test_init pack_include`
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" != "0" ]; then
181 test_done "$testroot" "$ret"
184 echo -n > $testroot/stdout.expected
185 cmp -s $testroot/stdout.expected $testroot/stdout
187 if [ "$ret" != "0" ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
193 got branch -r $testroot/repo mybranch
195 if [ "$ret" != "0" ]; then
196 test_done "$testroot" "$ret"
200 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
202 if [ "$ret" != "0" ]; then
203 test_done "$testroot" "$ret"
207 echo a new line >> $testroot/wt/alpha
208 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
209 local commit1=`git_show_branch_head $testroot/repo mybranch`
211 gotadmin pack -r $testroot/repo master > $testroot/stdout
213 if [ "$ret" != "0" ]; then
214 echo "gotadmin pack failed unexpectedly" >&2
215 test_done "$testroot" "$ret"
218 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
219 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
222 tree1=`got cat -r $testroot/repo $commit1 | grep ^tree | \
224 alpha1=`got tree -r $testroot/repo -i -c $commit1 | \
225 grep "[0-9a-f] alpha$" | cut -d' ' -f 1`
226 excluded_ids="$alpha1 $commit1 $tree1"
227 for id in $excluded_ids; do
229 if grep -q ^$id $testroot/stdout; then
230 echo "found excluded object $id in pack file" >&2
233 if [ "$ret" = "1" ]; then
237 if [ "$ret" = "1" ]; then
238 test_done "$testroot" "$ret"
242 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
244 included_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
246 included_ids="$included_ids $commit0 $tree0"
247 for obj_id in $included_ids; do
248 for id in $excluded_ids; do
249 if [ "$obj_id" = "$id" ]; then
254 if [ "$excluded" = "1" ]; then
257 if grep -q ^$obj_id $testroot/stdout; then
260 echo "included object $obj_id was not packed" >&2
265 test_done "$testroot" "$ret"
268 test_pack_ambiguous_arg() {
269 local testroot=`test_init pack_ambiguous_arg`
270 local commit0=`git_show_head $testroot/repo`
272 # no pack files should exist yet
273 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
275 if [ "$ret" != "0" ]; then
276 test_done "$testroot" "$ret"
279 echo -n > $testroot/stdout.expected
280 cmp -s $testroot/stdout.expected $testroot/stdout
282 if [ "$ret" != "0" ]; then
283 diff -u $testroot/stdout.expected $testroot/stdout
284 test_done "$testroot" "$ret"
288 got branch -r $testroot/repo mybranch
290 if [ "$ret" != "0" ]; then
291 test_done "$testroot" "$ret"
295 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
297 if [ "$ret" != "0" ]; then
298 test_done "$testroot" "$ret"
302 echo a new line >> $testroot/wt/alpha
303 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
304 local commit1=`git_show_branch_head $testroot/repo mybranch`
306 gotadmin pack -r $testroot/repo -x master master \
307 > $testroot/stdout 2> $testroot/stderr
309 if [ "$ret" = "0" ]; then
310 echo "gotadmin pack succeeded unexpectedly" >&2
311 test_done "$testroot" "1"
315 printf "\rpacking 1 reference\n" > $testroot/stdout.expected
316 cmp -s $testroot/stdout.expected $testroot/stdout
318 if [ "$ret" != "0" ]; then
319 diff -u $testroot/stdout.expected $testroot/stdout
320 test_done "$testroot" "$ret"
324 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
325 cmp -s $testroot/stderr.expected $testroot/stderr
327 if [ "$ret" != "0" ]; then
328 diff -u $testroot/stderr.expected $testroot/stderr
330 test_done "$testroot" "$ret"
333 test_pack_loose_only() {
334 local testroot=`test_init pack_loose_only`
335 local commit0=`git_show_head $testroot/repo`
337 # no pack files should exist yet
338 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
340 if [ "$ret" != "0" ]; then
341 test_done "$testroot" "$ret"
344 echo -n > $testroot/stdout.expected
345 cmp -s $testroot/stdout.expected $testroot/stdout
347 if [ "$ret" != "0" ]; then
348 diff -u $testroot/stdout.expected $testroot/stdout
349 test_done "$testroot" "$ret"
353 got branch -r $testroot/repo mybranch
355 if [ "$ret" != "0" ]; then
356 test_done "$testroot" "$ret"
360 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
362 if [ "$ret" != "0" ]; then
363 test_done "$testroot" "$ret"
367 echo a new line >> $testroot/wt/alpha
368 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
370 # pack objects belonging to the 'master' branch; its objects
371 # should then be excluded while packing 'mybranch' since they
373 gotadmin pack -r $testroot/repo master > /dev/null
375 if [ "$ret" != "0" ]; then
376 echo "gotadmin pack failed unexpectedly" >&2
377 test_done "$testroot" "$ret"
381 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
383 if [ "$ret" != "0" ]; then
384 echo "gotadmin pack failed unexpectedly" >&2
385 test_done "$testroot" "$ret"
388 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
389 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
392 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
394 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
396 excluded_ids="$excluded_ids $commit0 $tree0"
397 for id in $excluded_ids; do
399 if grep -q ^$id $testroot/stdout; then
400 echo "found excluded object $id in pack file" >&2
403 if [ "$ret" = "1" ]; then
407 if [ "$ret" = "1" ]; then
408 test_done "$testroot" "$ret"
412 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
418 for id in $excluded_ids; do
419 if [ "$obj_id" = "$id" ]; then
424 if [ "$excluded" = "1" ]; then
427 if grep -q ^$obj_id $testroot/stdout; then
430 echo "loose object $obj_id was not packed" >&2
434 if [ "$ret" = "1" ]; then
439 test_done "$testroot" "$ret"
442 test_pack_all_objects() {
443 local testroot=`test_init pack_all_objects`
444 local commit0=`git_show_head $testroot/repo`
446 # no pack files should exist yet
447 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
449 if [ "$ret" != "0" ]; then
450 test_done "$testroot" "$ret"
453 echo -n > $testroot/stdout.expected
454 cmp -s $testroot/stdout.expected $testroot/stdout
456 if [ "$ret" != "0" ]; then
457 diff -u $testroot/stdout.expected $testroot/stdout
458 test_done "$testroot" "$ret"
462 got branch -r $testroot/repo mybranch
464 if [ "$ret" != "0" ]; then
465 test_done "$testroot" "$ret"
469 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
471 if [ "$ret" != "0" ]; then
472 test_done "$testroot" "$ret"
476 echo a new line >> $testroot/wt/alpha
477 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
479 # pack objects belonging to the 'master' branch
480 gotadmin pack -r $testroot/repo master > /dev/null
482 if [ "$ret" != "0" ]; then
483 echo "gotadmin pack failed unexpectedly" >&2
484 test_done "$testroot" "$ret"
488 # pack mybranch, including already packed objects on the
489 # 'master' branch which are reachable from mybranch
490 gotadmin pack -r $testroot/repo -a mybranch > $testroot/stdout
492 if [ "$ret" != "0" ]; then
493 echo "gotadmin pack failed unexpectedly" >&2
494 test_done "$testroot" "$ret"
497 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
498 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
501 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
506 if grep -q ^$obj_id $testroot/stdout; then
509 echo "loose object $obj_id was not packed" >&2
513 if [ "$ret" = "1" ]; then
518 test_done "$testroot" "$ret"
521 test_pack_bad_ref() {
522 local testroot=`test_init pack_bad_ref`
523 local commit0=`git_show_head $testroot/repo`
525 # no pack files should exist yet
526 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
528 if [ "$ret" != "0" ]; then
529 test_done "$testroot" "$ret"
532 echo -n > $testroot/stdout.expected
533 cmp -s $testroot/stdout.expected $testroot/stdout
535 if [ "$ret" != "0" ]; then
536 diff -u $testroot/stdout.expected $testroot/stdout
537 test_done "$testroot" "$ret"
541 got branch -r $testroot/repo mybranch
543 if [ "$ret" != "0" ]; then
544 test_done "$testroot" "$ret"
548 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
550 if [ "$ret" != "0" ]; then
551 test_done "$testroot" "$ret"
555 gotadmin pack -r $testroot/repo refs/got/worktree/ \
556 > $testroot/stdout 2> $testroot/stderr
558 if [ "$ret" = "0" ]; then
559 echo "gotadmin pack succeeded unexpectedly" >&2
560 test_done "$testroot" "1"
564 echo -n > $testroot/stdout.expected
565 cmp -s $testroot/stdout.expected $testroot/stdout
567 if [ "$ret" != "0" ]; then
568 diff -u $testroot/stdout.expected $testroot/stdout
569 test_done "$testroot" "$ret"
573 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
574 cmp -s $testroot/stderr.expected $testroot/stderr
576 if [ "$ret" != "0" ]; then
577 diff -u $testroot/stderr.expected $testroot/stderr
579 test_done "$testroot" "$ret"
583 run_test test_pack_all_loose_objects
584 run_test test_pack_exclude
585 run_test test_pack_include
586 run_test test_pack_ambiguous_arg
587 run_test test_pack_loose_only
588 run_test test_pack_all_objects
589 run_test test_pack_bad_ref