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" != "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" != "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" != "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" = "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" != "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" != "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" != "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" != "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" != "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" = "1" ]; then
135 break
136 fi
137 done
138 if [ "$ret" = "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" = "1" ]; then
166 break
167 fi
168 done
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
179 ret="$?"
180 if [ "$ret" != "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" != "0" ]; then
188 diff -u $testroot/stdout.expected $testroot/stdout
189 test_done "$testroot" "$ret"
190 return 1
191 fi
193 got branch -r $testroot/repo mybranch
194 ret="$?"
195 if [ "$ret" != "0" ]; then
196 test_done "$testroot" "$ret"
197 return 1
198 fi
200 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
201 ret="$?"
202 if [ "$ret" != "0" ]; then
203 test_done "$testroot" "$ret"
204 return 1
205 fi
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
212 ret="$?"
213 if [ "$ret" != "0" ]; then
214 echo "gotadmin pack failed unexpectedly" >&2
215 test_done "$testroot" "$ret"
216 return 1
217 fi
218 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
219 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
220 > $testroot/stdout
222 tree1=`got cat -r $testroot/repo $commit1 | grep ^tree | \
223 cut -d ' ' -f2`
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
228 ret=0
229 if grep -q ^$id $testroot/stdout; then
230 echo "found excluded object $id in pack file" >&2
231 ret=1
232 fi
233 if [ "$ret" = "1" ]; then
234 break
235 fi
236 done
237 if [ "$ret" = "1" ]; then
238 test_done "$testroot" "$ret"
239 return 1
240 fi
242 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
243 cut -d ' ' -f2`
244 included_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
245 cut -d ' ' -f 1`
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
250 excluded=1
251 break
252 fi
253 done
254 if [ "$excluded" = "1" ]; then
255 continue
256 fi
257 if grep -q ^$obj_id $testroot/stdout; then
258 continue
259 fi
260 echo "included object $obj_id was not packed" >&2
261 ret=1
262 break
263 done
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
274 ret="$?"
275 if [ "$ret" != "0" ]; then
276 test_done "$testroot" "$ret"
277 return 1
278 fi
279 echo -n > $testroot/stdout.expected
280 cmp -s $testroot/stdout.expected $testroot/stdout
281 ret="$?"
282 if [ "$ret" != "0" ]; then
283 diff -u $testroot/stdout.expected $testroot/stdout
284 test_done "$testroot" "$ret"
285 return 1
286 fi
288 got branch -r $testroot/repo mybranch
289 ret="$?"
290 if [ "$ret" != "0" ]; then
291 test_done "$testroot" "$ret"
292 return 1
293 fi
295 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
296 ret="$?"
297 if [ "$ret" != "0" ]; then
298 test_done "$testroot" "$ret"
299 return 1
300 fi
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
308 ret="$?"
309 if [ "$ret" = "0" ]; then
310 echo "gotadmin pack succeeded unexpectedly" >&2
311 test_done "$testroot" "1"
312 return 1
313 fi
315 printf "\rpacking 1 reference\n" > $testroot/stdout.expected
316 cmp -s $testroot/stdout.expected $testroot/stdout
317 ret="$?"
318 if [ "$ret" != "0" ]; then
319 diff -u $testroot/stdout.expected $testroot/stdout
320 test_done "$testroot" "$ret"
321 return 1
322 fi
324 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
325 cmp -s $testroot/stderr.expected $testroot/stderr
326 ret="$?"
327 if [ "$ret" != "0" ]; then
328 diff -u $testroot/stderr.expected $testroot/stderr
329 fi
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
339 ret="$?"
340 if [ "$ret" != "0" ]; then
341 test_done "$testroot" "$ret"
342 return 1
343 fi
344 echo -n > $testroot/stdout.expected
345 cmp -s $testroot/stdout.expected $testroot/stdout
346 ret="$?"
347 if [ "$ret" != "0" ]; then
348 diff -u $testroot/stdout.expected $testroot/stdout
349 test_done "$testroot" "$ret"
350 return 1
351 fi
353 got branch -r $testroot/repo mybranch
354 ret="$?"
355 if [ "$ret" != "0" ]; then
356 test_done "$testroot" "$ret"
357 return 1
358 fi
360 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
361 ret="$?"
362 if [ "$ret" != "0" ]; then
363 test_done "$testroot" "$ret"
364 return 1
365 fi
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
372 # are already packed
373 gotadmin pack -r $testroot/repo master > /dev/null
374 ret="$?"
375 if [ "$ret" != "0" ]; then
376 echo "gotadmin pack failed unexpectedly" >&2
377 test_done "$testroot" "$ret"
378 return 1
379 fi
381 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
382 ret="$?"
383 if [ "$ret" != "0" ]; then
384 echo "gotadmin pack failed unexpectedly" >&2
385 test_done "$testroot" "$ret"
386 return 1
387 fi
388 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
389 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
390 > $testroot/stdout
392 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
393 cut -d ' ' -f2`
394 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
395 cut -d ' ' -f 1`
396 excluded_ids="$excluded_ids $commit0 $tree0"
397 for id in $excluded_ids; do
398 ret=0
399 if grep -q ^$id $testroot/stdout; then
400 echo "found excluded object $id in pack file" >&2
401 ret=1
402 fi
403 if [ "$ret" = "1" ]; then
404 break
405 fi
406 done
407 if [ "$ret" = "1" ]; then
408 test_done "$testroot" "$ret"
409 return 1
410 fi
412 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
413 id0=`basename $d`
414 ret=0
415 for e in `ls $d`; do
416 obj_id=${id0}${e}
417 excluded=0
418 for id in $excluded_ids; do
419 if [ "$obj_id" = "$id" ]; then
420 excluded=1
421 break
422 fi
423 done
424 if [ "$excluded" = "1" ]; then
425 continue
426 fi
427 if grep -q ^$obj_id $testroot/stdout; then
428 continue
429 fi
430 echo "loose object $obj_id was not packed" >&2
431 ret=1
432 break
433 done
434 if [ "$ret" = "1" ]; then
435 break
436 fi
437 done
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
448 ret="$?"
449 if [ "$ret" != "0" ]; then
450 test_done "$testroot" "$ret"
451 return 1
452 fi
453 echo -n > $testroot/stdout.expected
454 cmp -s $testroot/stdout.expected $testroot/stdout
455 ret="$?"
456 if [ "$ret" != "0" ]; then
457 diff -u $testroot/stdout.expected $testroot/stdout
458 test_done "$testroot" "$ret"
459 return 1
460 fi
462 got branch -r $testroot/repo mybranch
463 ret="$?"
464 if [ "$ret" != "0" ]; then
465 test_done "$testroot" "$ret"
466 return 1
467 fi
469 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
470 ret="$?"
471 if [ "$ret" != "0" ]; then
472 test_done "$testroot" "$ret"
473 return 1
474 fi
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
481 ret="$?"
482 if [ "$ret" != "0" ]; then
483 echo "gotadmin pack failed unexpectedly" >&2
484 test_done "$testroot" "$ret"
485 return 1
486 fi
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
491 ret="$?"
492 if [ "$ret" != "0" ]; then
493 echo "gotadmin pack failed unexpectedly" >&2
494 test_done "$testroot" "$ret"
495 return 1
496 fi
497 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
498 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
499 > $testroot/stdout
501 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
502 id0=`basename $d`
503 ret=0
504 for e in `ls $d`; do
505 obj_id=${id0}${e}
506 if grep -q ^$obj_id $testroot/stdout; then
507 continue
508 fi
509 echo "loose object $obj_id was not packed" >&2
510 ret=1
511 break
512 done
513 if [ "$ret" = "1" ]; then
514 break
515 fi
516 done
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
527 ret="$?"
528 if [ "$ret" != "0" ]; then
529 test_done "$testroot" "$ret"
530 return 1
531 fi
532 echo -n > $testroot/stdout.expected
533 cmp -s $testroot/stdout.expected $testroot/stdout
534 ret="$?"
535 if [ "$ret" != "0" ]; then
536 diff -u $testroot/stdout.expected $testroot/stdout
537 test_done "$testroot" "$ret"
538 return 1
539 fi
541 got branch -r $testroot/repo mybranch
542 ret="$?"
543 if [ "$ret" != "0" ]; then
544 test_done "$testroot" "$ret"
545 return 1
546 fi
548 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
549 ret="$?"
550 if [ "$ret" != "0" ]; then
551 test_done "$testroot" "$ret"
552 return 1
553 fi
555 gotadmin pack -r $testroot/repo refs/got/worktree/ \
556 > $testroot/stdout 2> $testroot/stderr
557 ret="$?"
558 if [ "$ret" = "0" ]; then
559 echo "gotadmin pack succeeded unexpectedly" >&2
560 test_done "$testroot" "1"
561 return 1
562 fi
564 echo -n > $testroot/stdout.expected
565 cmp -s $testroot/stdout.expected $testroot/stdout
566 ret="$?"
567 if [ "$ret" != "0" ]; then
568 diff -u $testroot/stdout.expected $testroot/stdout
569 test_done "$testroot" "$ret"
570 return 1
571 fi
573 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
574 cmp -s $testroot/stderr.expected $testroot/stderr
575 ret="$?"
576 if [ "$ret" != "0" ]; then
577 diff -u $testroot/stderr.expected $testroot/stderr
578 fi
579 test_done "$testroot" "$ret"
582 test_parseargs "$@"
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