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 > /dev/null 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 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
316 cmp -s $testroot/stderr.expected $testroot/stderr
317 ret="$?"
318 if [ "$ret" != "0" ]; then
319 diff -u $testroot/stderr.expected $testroot/stderr
320 fi
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
330 ret="$?"
331 if [ "$ret" != "0" ]; then
332 test_done "$testroot" "$ret"
333 return 1
334 fi
335 echo -n > $testroot/stdout.expected
336 cmp -s $testroot/stdout.expected $testroot/stdout
337 ret="$?"
338 if [ "$ret" != "0" ]; then
339 diff -u $testroot/stdout.expected $testroot/stdout
340 test_done "$testroot" "$ret"
341 return 1
342 fi
344 got branch -r $testroot/repo mybranch
345 ret="$?"
346 if [ "$ret" != "0" ]; then
347 test_done "$testroot" "$ret"
348 return 1
349 fi
351 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
352 ret="$?"
353 if [ "$ret" != "0" ]; then
354 test_done "$testroot" "$ret"
355 return 1
356 fi
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
363 # are already packed
364 gotadmin pack -r $testroot/repo master > /dev/null
365 ret="$?"
366 if [ "$ret" != "0" ]; then
367 echo "gotadmin pack failed unexpectedly" >&2
368 test_done "$testroot" "$ret"
369 return 1
370 fi
372 gotadmin pack -r $testroot/repo mybranch > $testroot/stdout
373 ret="$?"
374 if [ "$ret" != "0" ]; then
375 echo "gotadmin pack failed unexpectedly" >&2
376 test_done "$testroot" "$ret"
377 return 1
378 fi
379 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
380 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
381 > $testroot/stdout
383 tree0=`got cat -r $testroot/repo $commit0 | grep ^tree | \
384 cut -d ' ' -f2`
385 excluded_ids=`got tree -r $testroot/repo -c $commit0 -R -i | \
386 cut -d ' ' -f 1`
387 excluded_ids="$excluded_ids $commit0 $tree0"
388 for id in $excluded_ids; do
389 ret=0
390 if grep -q ^$id $testroot/stdout; then
391 echo "found excluded object $id in pack file" >&2
392 ret=1
393 fi
394 if [ "$ret" = "1" ]; then
395 break
396 fi
397 done
398 if [ "$ret" = "1" ]; then
399 test_done "$testroot" "$ret"
400 return 1
401 fi
403 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
404 id0=`basename $d`
405 ret=0
406 for e in `ls $d`; do
407 obj_id=${id0}${e}
408 excluded=0
409 for id in $excluded_ids; do
410 if [ "$obj_id" = "$id" ]; then
411 excluded=1
412 break
413 fi
414 done
415 if [ "$excluded" = "1" ]; then
416 continue
417 fi
418 if grep -q ^$obj_id $testroot/stdout; then
419 continue
420 fi
421 echo "loose object $obj_id was not packed" >&2
422 ret=1
423 break
424 done
425 if [ "$ret" = "1" ]; then
426 break
427 fi
428 done
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
439 ret="$?"
440 if [ "$ret" != "0" ]; then
441 test_done "$testroot" "$ret"
442 return 1
443 fi
444 echo -n > $testroot/stdout.expected
445 cmp -s $testroot/stdout.expected $testroot/stdout
446 ret="$?"
447 if [ "$ret" != "0" ]; then
448 diff -u $testroot/stdout.expected $testroot/stdout
449 test_done "$testroot" "$ret"
450 return 1
451 fi
453 got branch -r $testroot/repo mybranch
454 ret="$?"
455 if [ "$ret" != "0" ]; then
456 test_done "$testroot" "$ret"
457 return 1
458 fi
460 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
461 ret="$?"
462 if [ "$ret" != "0" ]; then
463 test_done "$testroot" "$ret"
464 return 1
465 fi
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
472 ret="$?"
473 if [ "$ret" != "0" ]; then
474 echo "gotadmin pack failed unexpectedly" >&2
475 test_done "$testroot" "$ret"
476 return 1
477 fi
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
482 ret="$?"
483 if [ "$ret" != "0" ]; then
484 echo "gotadmin pack failed unexpectedly" >&2
485 test_done "$testroot" "$ret"
486 return 1
487 fi
488 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
489 gotadmin listpack $testroot/repo/.git/objects/pack/pack-$packname \
490 > $testroot/stdout
492 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
493 id0=`basename $d`
494 ret=0
495 for e in `ls $d`; do
496 obj_id=${id0}${e}
497 if grep -q ^$obj_id $testroot/stdout; then
498 continue
499 fi
500 echo "loose object $obj_id was not packed" >&2
501 ret=1
502 break
503 done
504 if [ "$ret" = "1" ]; then
505 break
506 fi
507 done
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
518 ret="$?"
519 if [ "$ret" != "0" ]; then
520 test_done "$testroot" "$ret"
521 return 1
522 fi
523 echo -n > $testroot/stdout.expected
524 cmp -s $testroot/stdout.expected $testroot/stdout
525 ret="$?"
526 if [ "$ret" != "0" ]; then
527 diff -u $testroot/stdout.expected $testroot/stdout
528 test_done "$testroot" "$ret"
529 return 1
530 fi
532 got branch -r $testroot/repo mybranch
533 ret="$?"
534 if [ "$ret" != "0" ]; then
535 test_done "$testroot" "$ret"
536 return 1
537 fi
539 got checkout -b mybranch $testroot/repo $testroot/wt > /dev/null
540 ret="$?"
541 if [ "$ret" != "0" ]; then
542 test_done "$testroot" "$ret"
543 return 1
544 fi
546 gotadmin pack -r $testroot/repo refs/got/worktree/ \
547 > $testroot/stdout 2> $testroot/stderr
548 ret="$?"
549 if [ "$ret" = "0" ]; then
550 echo "gotadmin pack succeeded unexpectedly" >&2
551 test_done "$testroot" "1"
552 return 1
553 fi
555 echo -n > $testroot/stdout.expected
556 cmp -s $testroot/stdout.expected $testroot/stdout
557 ret="$?"
558 if [ "$ret" != "0" ]; then
559 diff -u $testroot/stdout.expected $testroot/stdout
560 test_done "$testroot" "$ret"
561 return 1
562 fi
564 echo "gotadmin: not enough objects to pack" > $testroot/stderr.expected
565 cmp -s $testroot/stderr.expected $testroot/stderr
566 ret="$?"
567 if [ "$ret" != "0" ]; then
568 diff -u $testroot/stderr.expected $testroot/stderr
569 fi
570 test_done "$testroot" "$ret"
573 test_parseargs "$@"
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