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_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 -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 branch -r $testroot/repo mybranch
195 if [ $ret -ne 0 ]; then
196 test_done "$testroot" "$ret"
200 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
202 if [ $ret -ne 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 -ne 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 -eq 1 ]; then
237 if [ $ret -eq 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 -ne 0 ]; then
276 test_done "$testroot" "$ret"
279 echo -n > $testroot/stdout.expected
280 cmp -s $testroot/stdout.expected $testroot/stdout
282 if [ $ret -ne 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 -ne 0 ]; then
291 test_done "$testroot" "$ret"
295 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
297 if [ $ret -ne 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 > /dev/null 2> $testroot/stderr
309 if [ $ret -eq 0 ]; then
310 echo "gotadmin pack succeeded unexpectedly" >&2
311 test_done "$testroot" "1"
315 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
316 cmp -s $testroot/stderr.expected $testroot/stderr
318 if [ $ret -ne 0 ]; then
319 diff -u $testroot/stderr.expected $testroot/stderr
321 test_done "$testroot" "$ret"
324 test_pack_loose_only() {
325 local testroot=`test_init pack_loose_only`
326 local commit0=`git_show_head $testroot/repo`
328 # no pack files should exist yet
329 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
331 if [ $ret -ne 0 ]; then
332 test_done "$testroot" "$ret"
335 echo -n > $testroot/stdout.expected
336 cmp -s $testroot/stdout.expected $testroot/stdout
338 if [ $ret -ne 0 ]; then
339 diff -u $testroot/stdout.expected $testroot/stdout
340 test_done "$testroot" "$ret"
344 got branch -r $testroot/repo mybranch
346 if [ $ret -ne 0 ]; then
347 test_done "$testroot" "$ret"
351 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
353 if [ $ret -ne 0 ]; then
354 test_done "$testroot" "$ret"
358 echo a new line >> $testroot/wt/alpha
359 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
361 # pack objects belonging to the 'master' branch; its objects
362 # should then be excluded while packing 'mybranch' since they
364 gotadmin pack -r $testroot/repo master > /dev/null
366 if [ $ret -ne 0 ]; then
367 echo "gotadmin pack failed unexpectedly" >&2
368 test_done "$testroot" "$ret"
372 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
374 if [ $ret -ne 0 ]; then
375 echo "gotadmin pack failed unexpectedly" >&2
376 test_done "$testroot" "$ret"
379 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
380 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
383 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
385 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
387 excluded_ids="$excluded_ids $commit0 $tree0"
388 for id in $excluded_ids; do
390 if grep -q ^$id $testroot/stdout; then
391 echo "found excluded object $id in pack file" >&2
394 if [ $ret -eq 1 ]; then
398 if [ $ret -eq 1 ]; then
399 test_done "$testroot" "$ret"
403 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
409 for id in $excluded_ids; do
410 if [ "$obj_id" = "$id" ]; then
415 if [ "$excluded" = "1" ]; then
418 if grep -q ^$obj_id $testroot/stdout; then
421 echo "loose object $obj_id was not packed" >&2
425 if [ $ret -eq 1 ]; then
430 test_done "$testroot" "$ret"
433 test_pack_all_objects() {
434 local testroot=`test_init pack_all_objects`
435 local commit0=`git_show_head $testroot/repo`
437 # no pack files should exist yet
438 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
440 if [ $ret -ne 0 ]; then
441 test_done "$testroot" "$ret"
444 echo -n > $testroot/stdout.expected
445 cmp -s $testroot/stdout.expected $testroot/stdout
447 if [ $ret -ne 0 ]; then
448 diff -u $testroot/stdout.expected $testroot/stdout
449 test_done "$testroot" "$ret"
453 got branch -r $testroot/repo mybranch
455 if [ $ret -ne 0 ]; then
456 test_done "$testroot" "$ret"
460 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
462 if [ $ret -ne 0 ]; then
463 test_done "$testroot" "$ret"
467 echo a new line >> $testroot/wt/alpha
468 (cd $testroot/wt && got commit -m "edit alpha" >/dev/null)
470 # pack objects belonging to the 'master' branch
471 gotadmin pack -r $testroot/repo master > /dev/null
473 if [ $ret -ne 0 ]; then
474 echo "gotadmin pack failed unexpectedly" >&2
475 test_done "$testroot" "$ret"
479 # pack mybranch, including already packed objects on the
480 # 'master' branch which are reachable from mybranch
481 gotadmin pack -r $testroot/repo -a mybranch > $testroot/stdout
483 if [ $ret -ne 0 ]; then
484 echo "gotadmin pack failed unexpectedly" >&2
485 test_done "$testroot" "$ret"
488 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
489 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
492 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
497 if grep -q ^$obj_id $testroot/stdout; then
500 echo "loose object $obj_id was not packed" >&2
504 if [ $ret -eq 1 ]; then
509 test_done "$testroot" "$ret"
512 test_pack_bad_ref() {
513 local testroot=`test_init pack_bad_ref`
514 local commit0=`git_show_head $testroot/repo`
516 # no pack files should exist yet
517 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
519 if [ $ret -ne 0 ]; then
520 test_done "$testroot" "$ret"
523 echo -n > $testroot/stdout.expected
524 cmp -s $testroot/stdout.expected $testroot/stdout
526 if [ $ret -ne 0 ]; then
527 diff -u $testroot/stdout.expected $testroot/stdout
528 test_done "$testroot" "$ret"
532 got branch -r $testroot/repo mybranch
534 if [ $ret -ne 0 ]; then
535 test_done "$testroot" "$ret"
539 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
541 if [ $ret -ne 0 ]; then
542 test_done "$testroot" "$ret"
546 gotadmin pack -r $testroot/repo refs/got/worktree/ \
547 > $testroot/stdout 2> $testroot/stderr
549 if [ $ret -eq 0 ]; then
550 echo "gotadmin pack succeeded unexpectedly" >&2
551 test_done "$testroot" "1"
555 echo -n > $testroot/stdout.expected
556 cmp -s $testroot/stdout.expected $testroot/stdout
558 if [ $ret -ne 0 ]; then
559 diff -u $testroot/stdout.expected $testroot/stdout
560 test_done "$testroot" "$ret"
564 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
565 cmp -s $testroot/stderr.expected $testroot/stderr
567 if [ $ret -ne 0 ]; then
568 diff -u $testroot/stderr.expected $testroot/stderr
570 test_done "$testroot" "$ret"
574 run_test test_pack_all_loose_objects
575 run_test test_pack_exclude
576 run_test test_pack_include
577 run_test test_pack_ambiguous_arg
578 run_test test_pack_loose_only
579 run_test test_pack_all_objects
580 run_test test_pack_bad_ref