Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 #
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.
8 #
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.
17 . ./common.sh
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
30 ret=$?
31 if [ $ret -ne 0 ]; then
32 test_done "$testroot" "$ret"
33 return 1
34 fi
35 echo -n > $testroot/stdout.expected
36 cmp -s $testroot/stdout.expected $testroot/stdout
37 ret=$?
38 if [ $ret -ne 0 ]; then
39 diff -u $testroot/stdout.expected $testroot/stdout
40 test_done "$testroot" "$ret"
41 return 1
42 fi
44 gotadmin pack -r $testroot/repo > $testroot/stdout
45 ret=$?
46 if [ $ret -ne 0 ]; then
47 echo "gotadmin pack failed unexpectedly" >&2
48 test_done "$testroot" "$ret"
49 return 1
50 fi
51 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
52 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
53 > $testroot/stdout
55 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
56 id0=`basename $d`
57 ret=0
58 for e in `ls $d`; do
59 obj_id=${id0}${e}
60 if grep -q ^$obj_id $testroot/stdout; then
61 continue
62 fi
63 echo "loose object $obj_id was not packed" >&2
64 ret=1
65 break
66 done
67 if [ $ret -eq 1 ]; then
68 break
69 fi
70 done
72 test_done "$testroot" "$ret"
73 }
75 test_pack_exclude() {
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
81 ret=$?
82 if [ $ret -ne 0 ]; then
83 test_done "$testroot" "$ret"
84 return 1
85 fi
86 echo -n > $testroot/stdout.expected
87 cmp -s $testroot/stdout.expected $testroot/stdout
88 ret=$?
89 if [ $ret -ne 0 ]; then
90 diff -u $testroot/stdout.expected $testroot/stdout
91 test_done "$testroot" "$ret"
92 return 1
93 fi
95 got branch -r $testroot/repo mybranch
96 ret=$?
97 if [ $ret -ne 0 ]; then
98 test_done "$testroot" "$ret"
99 return 1
100 fi
102 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
103 ret=$?
104 if [ $ret -ne 0 ]; then
105 test_done "$testroot" "$ret"
106 return 1
107 fi
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
113 ret=$?
114 if [ $ret -ne 0 ]; then
115 echo "gotadmin pack failed unexpectedly" >&2
116 test_done "$testroot" "$ret"
117 return 1
118 fi
119 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
120 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
121 > $testroot/stdout
123 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
124 cut -d ' ' -f2`
125 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
126 cut -d ' ' -f 1`
127 excluded_ids="$excluded_ids $commit0 $tree0"
128 for id in $excluded_ids; do
129 ret=0
130 if grep -q ^$id $testroot/stdout; then
131 echo "found excluded object $id in pack file" >&2
132 ret=1
133 fi
134 if [ $ret -eq 1 ]; then
135 break
136 fi
137 done
138 if [ $ret -eq 1 ]; then
139 test_done "$testroot" "$ret"
140 return 1
141 fi
143 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
144 id0=`basename $d`
145 ret=0
146 for e in `ls $d`; do
147 obj_id=${id0}${e}
148 excluded=0
149 for id in $excluded_ids; do
150 if [ "$obj_id" = "$id" ]; then
151 excluded=1
152 break
153 fi
154 done
155 if [ "$excluded" = "1" ]; then
156 continue
157 fi
158 if grep -q ^$obj_id $testroot/stdout; then
159 continue
160 fi
161 echo "loose object $obj_id was not packed" >&2
162 ret=1
163 break
164 done
165 if [ $ret -eq 1 ]; then
166 break
167 fi
168 done
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
179 ret=$?
180 if [ $ret -ne 0 ]; then
181 test_done "$testroot" "$ret"
182 return 1
183 fi
184 echo -n > $testroot/stdout.expected
185 cmp -s $testroot/stdout.expected $testroot/stdout
186 ret=$?
187 if [ $ret -ne 0 ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
190 return 1
191 fi
193 got tag -r $testroot/repo -m 1.0 -c master 1.0 > /dev/null
194 ret=$?
195 if [ $ret -ne 0 ]; then
196 test_done "$testroot" "$ret"
197 return 1
198 fi
200 got branch -r $testroot/repo mybranch
201 ret=$?
202 if [ $ret -ne 0 ]; then
203 test_done "$testroot" "$ret"
204 return 1
205 fi
207 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
208 ret=$?
209 if [ $ret -ne 0 ]; then
210 test_done "$testroot" "$ret"
211 return 1
212 fi
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
218 ret=$?
219 if [ $ret -ne 0 ]; then
220 echo "gotadmin pack failed unexpectedly" >&2
221 test_done "$testroot" "$ret"
222 return 1
223 fi
224 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
225 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
226 > $testroot/stdout
228 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
229 cut -d ' ' -f2`
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 | \
232 cut -d ' ' -f 1`
233 excluded_ids="$excluded_ids $commit0 $tree0 $tag0"
234 for id in $excluded_ids; do
235 ret=0
236 if grep -q ^$id $testroot/stdout; then
237 echo "found excluded object $id in pack file" >&2
238 ret=1
239 fi
240 if [ $ret -eq 1 ]; then
241 break
242 fi
243 done
244 if [ $ret -eq 1 ]; then
245 test_done "$testroot" "$ret"
246 return 1
247 fi
249 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
250 id0=`basename $d`
251 ret=0
252 for e in `ls $d`; do
253 obj_id=${id0}${e}
254 excluded=0
255 for id in $excluded_ids; do
256 if [ "$obj_id" = "$id" ]; then
257 excluded=1
258 break
259 fi
260 done
261 if [ "$excluded" = "1" ]; then
262 continue
263 fi
264 if grep -q ^$obj_id $testroot/stdout; then
265 continue
266 fi
267 echo "loose object $obj_id was not packed" >&2
268 ret=1
269 break
270 done
271 if [ $ret -eq 1 ]; then
272 break
273 fi
274 done
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
285 ret=$?
286 if [ $ret -ne 0 ]; then
287 test_done "$testroot" "$ret"
288 return 1
289 fi
290 echo -n > $testroot/stdout.expected
291 cmp -s $testroot/stdout.expected $testroot/stdout
292 ret=$?
293 if [ $ret -ne 0 ]; then
294 diff -u $testroot/stdout.expected $testroot/stdout
295 test_done "$testroot" "$ret"
296 return 1
297 fi
299 got branch -r $testroot/repo mybranch
300 ret=$?
301 if [ $ret -ne 0 ]; then
302 test_done "$testroot" "$ret"
303 return 1
304 fi
306 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
307 ret=$?
308 if [ $ret -ne 0 ]; then
309 test_done "$testroot" "$ret"
310 return 1
311 fi
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
318 ret=$?
319 if [ $ret -ne 0 ]; then
320 echo "gotadmin pack failed unexpectedly" >&2
321 test_done "$testroot" "$ret"
322 return 1
323 fi
324 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
325 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
326 > $testroot/stdout
328 tree1=`got cat -r $testroot/repo $commit1 | grep ^tree | \
329 cut -d ' ' -f2`
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
334 ret=0
335 if grep -q ^$id $testroot/stdout; then
336 echo "found excluded object $id in pack file" >&2
337 ret=1
338 fi
339 if [ $ret -eq 1 ]; then
340 break
341 fi
342 done
343 if [ $ret -eq 1 ]; then
344 test_done "$testroot" "$ret"
345 return 1
346 fi
348 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
349 cut -d ' ' -f2`
350 included_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
351 cut -d ' ' -f 1`
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
356 excluded=1
357 break
358 fi
359 done
360 if [ "$excluded" = "1" ]; then
361 continue
362 fi
363 if grep -q ^$obj_id $testroot/stdout; then
364 continue
365 fi
366 echo "included object $obj_id was not packed" >&2
367 ret=1
368 break
369 done
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
380 ret=$?
381 if [ $ret -ne 0 ]; then
382 test_done "$testroot" "$ret"
383 return 1
384 fi
385 echo -n > $testroot/stdout.expected
386 cmp -s $testroot/stdout.expected $testroot/stdout
387 ret=$?
388 if [ $ret -ne 0 ]; then
389 diff -u $testroot/stdout.expected $testroot/stdout
390 test_done "$testroot" "$ret"
391 return 1
392 fi
394 got branch -r $testroot/repo mybranch
395 ret=$?
396 if [ $ret -ne 0 ]; then
397 test_done "$testroot" "$ret"
398 return 1
399 fi
401 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
402 ret=$?
403 if [ $ret -ne 0 ]; then
404 test_done "$testroot" "$ret"
405 return 1
406 fi
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
413 ret=$?
414 if [ $ret -eq 0 ]; then
415 echo "gotadmin pack succeeded unexpectedly" >&2
416 test_done "$testroot" "1"
417 return 1
418 fi
420 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
421 cmp -s $testroot/stderr.expected $testroot/stderr
422 ret=$?
423 if [ $ret -ne 0 ]; then
424 diff -u $testroot/stderr.expected $testroot/stderr
425 fi
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
435 ret=$?
436 if [ $ret -ne 0 ]; then
437 test_done "$testroot" "$ret"
438 return 1
439 fi
440 echo -n > $testroot/stdout.expected
441 cmp -s $testroot/stdout.expected $testroot/stdout
442 ret=$?
443 if [ $ret -ne 0 ]; then
444 diff -u $testroot/stdout.expected $testroot/stdout
445 test_done "$testroot" "$ret"
446 return 1
447 fi
449 got branch -r $testroot/repo mybranch
450 ret=$?
451 if [ $ret -ne 0 ]; then
452 test_done "$testroot" "$ret"
453 return 1
454 fi
456 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
457 ret=$?
458 if [ $ret -ne 0 ]; then
459 test_done "$testroot" "$ret"
460 return 1
461 fi
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
468 # are already packed
469 gotadmin pack -q -r $testroot/repo master
470 ret=$?
471 if [ $ret -ne 0 ]; then
472 echo "gotadmin pack failed unexpectedly" >&2
473 test_done "$testroot" "$ret"
474 return 1
475 fi
477 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
478 ret=$?
479 if [ $ret -ne 0 ]; then
480 echo "gotadmin pack failed unexpectedly" >&2
481 test_done "$testroot" "$ret"
482 return 1
483 fi
484 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
485 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
486 > $testroot/stdout
488 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
489 cut -d ' ' -f2`
490 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
491 cut -d ' ' -f 1`
492 excluded_ids="$excluded_ids $commit0 $tree0"
493 for id in $excluded_ids; do
494 ret=0
495 if grep -q ^$id $testroot/stdout; then
496 echo "found excluded object $id in pack file" >&2
497 ret=1
498 fi
499 if [ $ret -eq 1 ]; then
500 break
501 fi
502 done
503 if [ $ret -eq 1 ]; then
504 test_done "$testroot" "$ret"
505 return 1
506 fi
508 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
509 id0=`basename $d`
510 ret=0
511 for e in `ls $d`; do
512 obj_id=${id0}${e}
513 excluded=0
514 for id in $excluded_ids; do
515 if [ "$obj_id" = "$id" ]; then
516 excluded=1
517 break
518 fi
519 done
520 if [ "$excluded" = "1" ]; then
521 continue
522 fi
523 if grep -q ^$obj_id $testroot/stdout; then
524 continue
525 fi
526 echo "loose object $obj_id was not packed" >&2
527 ret=1
528 break
529 done
530 if [ $ret -eq 1 ]; then
531 break
532 fi
533 done
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
544 ret=$?
545 if [ $ret -ne 0 ]; then
546 test_done "$testroot" "$ret"
547 return 1
548 fi
549 echo -n > $testroot/stdout.expected
550 cmp -s $testroot/stdout.expected $testroot/stdout
551 ret=$?
552 if [ $ret -ne 0 ]; then
553 diff -u $testroot/stdout.expected $testroot/stdout
554 test_done "$testroot" "$ret"
555 return 1
556 fi
558 got branch -r $testroot/repo mybranch
559 ret=$?
560 if [ $ret -ne 0 ]; then
561 test_done "$testroot" "$ret"
562 return 1
563 fi
565 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
566 ret=$?
567 if [ $ret -ne 0 ]; then
568 test_done "$testroot" "$ret"
569 return 1
570 fi
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
577 ret=$?
578 if [ $ret -ne 0 ]; then
579 echo "gotadmin pack failed unexpectedly" >&2
580 test_done "$testroot" "$ret"
581 return 1
582 fi
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
587 ret=$?
588 if [ $ret -ne 0 ]; then
589 echo "gotadmin pack failed unexpectedly" >&2
590 test_done "$testroot" "$ret"
591 return 1
592 fi
593 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
594 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
595 > $testroot/stdout
597 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
598 id0=`basename $d`
599 ret=0
600 for e in `ls $d`; do
601 obj_id=${id0}${e}
602 if grep -q ^$obj_id $testroot/stdout; then
603 continue
604 fi
605 echo "loose object $obj_id was not packed" >&2
606 ret=1
607 break
608 done
609 if [ $ret -eq 1 ]; then
610 break
611 fi
612 done
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
623 ret=$?
624 if [ $ret -ne 0 ]; then
625 test_done "$testroot" "$ret"
626 return 1
627 fi
628 echo -n > $testroot/stdout.expected
629 cmp -s $testroot/stdout.expected $testroot/stdout
630 ret=$?
631 if [ $ret -ne 0 ]; then
632 diff -u $testroot/stdout.expected $testroot/stdout
633 test_done "$testroot" "$ret"
634 return 1
635 fi
637 got branch -r $testroot/repo mybranch
638 ret=$?
639 if [ $ret -ne 0 ]; then
640 test_done "$testroot" "$ret"
641 return 1
642 fi
644 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
645 ret=$?
646 if [ $ret -ne 0 ]; then
647 test_done "$testroot" "$ret"
648 return 1
649 fi
651 gotadmin pack -r $testroot/repo refs/got/worktree/ \
652 > $testroot/stdout 2> $testroot/stderr
653 ret=$?
654 if [ $ret -eq 0 ]; then
655 echo "gotadmin pack succeeded unexpectedly" >&2
656 test_done "$testroot" "1"
657 return 1
658 fi
660 echo -n > $testroot/stdout.expected
661 cmp -s $testroot/stdout.expected $testroot/stdout
662 ret=$?
663 if [ $ret -ne 0 ]; then
664 diff -u $testroot/stdout.expected $testroot/stdout
665 test_done "$testroot" "$ret"
666 return 1
667 fi
669 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
670 cmp -s $testroot/stderr.expected $testroot/stderr
671 ret=$?
672 if [ $ret -ne 0 ]; then
673 diff -u $testroot/stderr.expected $testroot/stderr
674 fi
675 test_done "$testroot" "$ret"
678 test_parseargs "$@"
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