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 -r $testroot/repo -x master master \
413 > /dev/null 2> $testroot/stderr
414 ret=$?
415 if [ $ret -eq 0 ]; then
416 echo "gotadmin pack succeeded unexpectedly" >&2
417 test_done "$testroot" "1"
418 return 1
419 fi
421 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
422 cmp -s $testroot/stderr.expected $testroot/stderr
423 ret=$?
424 if [ $ret -ne 0 ]; then
425 diff -u $testroot/stderr.expected $testroot/stderr
426 fi
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
436 ret=$?
437 if [ $ret -ne 0 ]; then
438 test_done "$testroot" "$ret"
439 return 1
440 fi
441 echo -n > $testroot/stdout.expected
442 cmp -s $testroot/stdout.expected $testroot/stdout
443 ret=$?
444 if [ $ret -ne 0 ]; then
445 diff -u $testroot/stdout.expected $testroot/stdout
446 test_done "$testroot" "$ret"
447 return 1
448 fi
450 got branch -r $testroot/repo mybranch
451 ret=$?
452 if [ $ret -ne 0 ]; then
453 test_done "$testroot" "$ret"
454 return 1
455 fi
457 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
458 ret=$?
459 if [ $ret -ne 0 ]; then
460 test_done "$testroot" "$ret"
461 return 1
462 fi
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
469 # are already packed
470 gotadmin pack -r $testroot/repo master > /dev/null
471 ret=$?
472 if [ $ret -ne 0 ]; then
473 echo "gotadmin pack failed unexpectedly" >&2
474 test_done "$testroot" "$ret"
475 return 1
476 fi
478 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
479 ret=$?
480 if [ $ret -ne 0 ]; then
481 echo "gotadmin pack failed unexpectedly" >&2
482 test_done "$testroot" "$ret"
483 return 1
484 fi
485 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
486 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
487 > $testroot/stdout
489 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
490 cut -d ' ' -f2`
491 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
492 cut -d ' ' -f 1`
493 excluded_ids="$excluded_ids $commit0 $tree0"
494 for id in $excluded_ids; do
495 ret=0
496 if grep -q ^$id $testroot/stdout; then
497 echo "found excluded object $id in pack file" >&2
498 ret=1
499 fi
500 if [ $ret -eq 1 ]; then
501 break
502 fi
503 done
504 if [ $ret -eq 1 ]; then
505 test_done "$testroot" "$ret"
506 return 1
507 fi
509 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
510 id0=`basename $d`
511 ret=0
512 for e in `ls $d`; do
513 obj_id=${id0}${e}
514 excluded=0
515 for id in $excluded_ids; do
516 if [ "$obj_id" = "$id" ]; then
517 excluded=1
518 break
519 fi
520 done
521 if [ "$excluded" = "1" ]; then
522 continue
523 fi
524 if grep -q ^$obj_id $testroot/stdout; then
525 continue
526 fi
527 echo "loose object $obj_id was not packed" >&2
528 ret=1
529 break
530 done
531 if [ $ret -eq 1 ]; then
532 break
533 fi
534 done
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
545 ret=$?
546 if [ $ret -ne 0 ]; then
547 test_done "$testroot" "$ret"
548 return 1
549 fi
550 echo -n > $testroot/stdout.expected
551 cmp -s $testroot/stdout.expected $testroot/stdout
552 ret=$?
553 if [ $ret -ne 0 ]; then
554 diff -u $testroot/stdout.expected $testroot/stdout
555 test_done "$testroot" "$ret"
556 return 1
557 fi
559 got branch -r $testroot/repo mybranch
560 ret=$?
561 if [ $ret -ne 0 ]; then
562 test_done "$testroot" "$ret"
563 return 1
564 fi
566 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
567 ret=$?
568 if [ $ret -ne 0 ]; then
569 test_done "$testroot" "$ret"
570 return 1
571 fi
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
578 ret=$?
579 if [ $ret -ne 0 ]; then
580 echo "gotadmin pack failed unexpectedly" >&2
581 test_done "$testroot" "$ret"
582 return 1
583 fi
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
588 ret=$?
589 if [ $ret -ne 0 ]; then
590 echo "gotadmin pack failed unexpectedly" >&2
591 test_done "$testroot" "$ret"
592 return 1
593 fi
594 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
595 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
596 > $testroot/stdout
598 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
599 id0=`basename $d`
600 ret=0
601 for e in `ls $d`; do
602 obj_id=${id0}${e}
603 if grep -q ^$obj_id $testroot/stdout; then
604 continue
605 fi
606 echo "loose object $obj_id was not packed" >&2
607 ret=1
608 break
609 done
610 if [ $ret -eq 1 ]; then
611 break
612 fi
613 done
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
624 ret=$?
625 if [ $ret -ne 0 ]; then
626 test_done "$testroot" "$ret"
627 return 1
628 fi
629 echo -n > $testroot/stdout.expected
630 cmp -s $testroot/stdout.expected $testroot/stdout
631 ret=$?
632 if [ $ret -ne 0 ]; then
633 diff -u $testroot/stdout.expected $testroot/stdout
634 test_done "$testroot" "$ret"
635 return 1
636 fi
638 got branch -r $testroot/repo mybranch
639 ret=$?
640 if [ $ret -ne 0 ]; then
641 test_done "$testroot" "$ret"
642 return 1
643 fi
645 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
646 ret=$?
647 if [ $ret -ne 0 ]; then
648 test_done "$testroot" "$ret"
649 return 1
650 fi
652 gotadmin pack -r $testroot/repo refs/got/worktree/ \
653 > $testroot/stdout 2> $testroot/stderr
654 ret=$?
655 if [ $ret -eq 0 ]; then
656 echo "gotadmin pack succeeded unexpectedly" >&2
657 test_done "$testroot" "1"
658 return 1
659 fi
661 echo -n > $testroot/stdout.expected
662 cmp -s $testroot/stdout.expected $testroot/stdout
663 ret=$?
664 if [ $ret -ne 0 ]; then
665 diff -u $testroot/stdout.expected $testroot/stdout
666 test_done "$testroot" "$ret"
667 return 1
668 fi
670 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
671 cmp -s $testroot/stderr.expected $testroot/stderr
672 ret=$?
673 if [ $ret -ne 0 ]; then
674 diff -u $testroot/stderr.expected $testroot/stderr
675 fi
676 test_done "$testroot" "$ret"
679 test_parseargs "$@"
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