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_cleanup_unreferenced_loose_objects() {
23 local testroot=`test_init cleanup_unreferenced_loose_objects`
25 nloose0=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
26 cut -d ':' -f 2 | tr -d ' '`
27 if [ "$nloose0" != "8" ]; then
28 echo "unexpected number of loose objects: $nloose0" >&2
29 test_done "$testroot" "1"
30 return 1
31 fi
33 # create a branch with some changes
34 got branch -r $testroot/repo newbranch >/dev/null
36 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
37 ret=$?
38 if [ $ret -ne 0 ]; then
39 echo "got checkout command failed unexpectedly"
40 test_done "$testroot" "$ret"
41 return 1
42 fi
44 echo 'foo' > $testroot/wt/foo
45 (cd $testroot/wt && got add foo > /dev/null)
46 echo 'modified alpha' > $testroot/wt/alpha
47 (cd $testroot/wt && got commit -m 'newbranch commit' > /dev/null)
48 local commit1=`git_show_branch_head $testroot/repo newbranch`
49 local tree1=`got cat -r $testroot/repo $newbranch_commit | \
50 grep ^tree | cut -d ' ' -f2`
51 local alpha1=`got tree -r $testroot/repo -i -c $commit1 | \
52 grep "[0-9a-f] alpha$" | cut -d' ' -f 1`
53 local foo1=`got tree -r $testroot/repo -i -c $commit1 | \
54 grep "[0-9a-f] foo$" | cut -d' ' -f 1`
56 nloose1=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
57 cut -d ':' -f 2 | tr -d ' '`
58 if [ "$nloose1" != "12" ]; then
59 echo "unexpected number of loose objects: $nloose1" >&2
60 test_done "$testroot" "1"
61 return 1
62 fi
64 # delete the branch
65 got branch -r $testroot/repo -d newbranch >/dev/null
67 # remove worktree's base commit reference, which points at the branch
68 wt_uuid=`(cd $testroot/wt && got info | grep 'UUID:' | \
69 cut -d ':' -f 2 | tr -d ' ')`
70 got ref -r $testroot/repo -d "refs/got/worktree/base-$wt_uuid" \
71 > /dev/null
73 # cleanup -n should not remove any objects
74 ls -R $testroot/repo/.git/objects > $testroot/objects-before
75 gotadmin cleanup -a -n -q -r $testroot/repo > $testroot/stdout
76 echo -n > $testroot/stdout.expected
77 cmp -s $testroot/stdout.expected $testroot/stdout
78 ret=$?
79 if [ $ret -ne 0 ]; then
80 diff -u $testroot/stdout.expected $testroot/stdout
81 test_done "$testroot" "$ret"
82 return 1
83 fi
84 ls -R $testroot/repo/.git/objects > $testroot/objects-after
85 cmp -s $testroot/objects-before $testroot/objects-after
86 ret=$?
87 if [ $ret -ne 0 ]; then
88 diff -u $testroot/objects-before $testroot/objects-after
89 test_done "$testroot" "$ret"
90 return 1
91 fi
93 # cleanup should remove loose objects that belonged to the branch
94 gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout
95 ret=$?
96 if [ $ret -ne 0 ]; then
97 echo "gotadmin cleanup failed unexpectedly" >&2
98 test_done "$testroot" "$ret"
99 return 1
100 fi
101 echo -n > $testroot/stdout.expected
102 cmp -s $testroot/stdout.expected $testroot/stdout
103 ret=$?
104 if [ $ret -ne 0 ]; then
105 diff -u $testroot/stdout.expected $testroot/stdout
106 test_done "$testroot" "$ret"
107 return 1
108 fi
110 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
111 cut -d ':' -f 2 | tr -d ' '`
112 if [ "$nloose2" != "$nloose0" ]; then
113 echo "unexpected number of loose objects: $nloose2" >&2
114 test_done "$testroot" "1"
115 return 1
116 fi
118 for id in $commit1 $tree1 $alpha1 $foo1; do
119 path=`get_loose_object_path $testroot/repo $id`
120 if [ -e "$path" ]; then
121 echo "loose object $path was not purged" >&2
122 ret=1
123 break
124 fi
125 done
127 test_done "$testroot" "$ret"
130 test_cleanup_redundant_loose_objects() {
131 local testroot=`test_init cleanup_redundant_loose_objects`
133 # tags should also be packed
134 got tag -r $testroot/repo -m 1.0 1.0 >/dev/null
136 nloose0=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
137 cut -d ':' -f 2 | tr -d ' '`
138 if [ "$nloose0" != "9" ]; then
139 echo "unexpected number of loose objects: $nloose0" >&2
140 test_done "$testroot" "1"
141 return 1
142 fi
144 # no pack files should exist yet
145 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
146 ret=$?
147 if [ $ret -ne 0 ]; then
148 test_done "$testroot" "$ret"
149 return 1
150 fi
151 echo -n > $testroot/stdout.expected
152 cmp -s $testroot/stdout.expected $testroot/stdout
153 ret=$?
154 if [ $ret -ne 0 ]; then
155 diff -u $testroot/stdout.expected $testroot/stdout
156 test_done "$testroot" "$ret"
157 return 1
158 fi
160 gotadmin pack -r $testroot/repo > /dev/null
162 npacked0=`gotadmin info -r $testroot/repo | grep '^packed objects:' | \
163 cut -d ':' -f 2 | tr -d ' '`
164 if [ "$npacked0" != "9" ]; then
165 echo "unexpected number of loose objects: $npacked0" >&2
166 test_done "$testroot" "1"
167 return 1
168 fi
170 # cleanup -n should not remove any objects
171 ls -R $testroot/repo/.git/objects > $testroot/objects-before
172 gotadmin cleanup -a -n -q -r $testroot/repo > $testroot/stdout
173 echo -n > $testroot/stdout.expected
174 cmp -s $testroot/stdout.expected $testroot/stdout
175 ret=$?
176 if [ $ret -ne 0 ]; then
177 diff -u $testroot/stdout.expected $testroot/stdout
178 test_done "$testroot" "$ret"
179 return 1
180 fi
181 ls -R $testroot/repo/.git/objects > $testroot/objects-after
182 cmp -s $testroot/objects-before $testroot/objects-after
183 ret=$?
184 if [ $ret -ne 0 ]; then
185 diff -u $testroot/objects-before $testroot/objects-after
186 test_done "$testroot" "$ret"
187 return 1
188 fi
190 nloose1=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
191 cut -d ':' -f 2 | tr -d ' '`
192 if [ "$nloose1" != "$nloose0" ]; then
193 echo "unexpected number of loose objects: $nloose1" >&2
194 test_done "$testroot" "1"
195 return 1
196 fi
198 # cleanup should remove all loose objects
199 gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout
200 ret=$?
201 if [ $ret -ne 0 ]; then
202 echo "gotadmin cleanup failed unexpectedly" >&2
203 test_done "$testroot" "$ret"
204 return 1
205 fi
206 echo -n > $testroot/stdout.expected
207 cmp -s $testroot/stdout.expected $testroot/stdout
208 ret=$?
209 if [ $ret -ne 0 ]; then
210 diff -u $testroot/stdout.expected $testroot/stdout
211 test_done "$testroot" "$ret"
212 return 1
213 fi
215 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
216 cut -d ':' -f 2 | tr -d ' '`
217 if [ "$nloose2" != "0" ]; then
218 echo "unexpected number of loose objects: $nloose2" >&2
219 test_done "$testroot" "1"
220 return 1
221 fi
223 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
224 id0=`basename $d`
225 ret=0
226 for e in `ls $d`; do
227 obj_id=${id0}${e}
228 echo "loose object $obj_id was not purged" >&2
229 ret=1
230 break
231 done
232 if [ $ret -eq 1 ]; then
233 break
234 fi
235 done
237 test_done "$testroot" "$ret"
240 test_cleanup_redundant_pack_files() {
241 local testroot=`test_init cleanup_redundant_pack_files`
243 # no pack files should exist yet
245 n=$(gotadmin info -r "$testroot/repo" | awk '/^pack files/{print $3}')
246 if [ "$n" -ne 0 ]; then
247 echo "expected no pack file to exists, $n found" >&2
248 test_done "$testroot" 1
249 return 1
250 fi
252 # create a redundant pack with an associated .keep file
253 hash=$(gotadmin pack -a -r "$testroot/repo" \
254 | awk '/^Indexed/{print $2}')
255 kpack="$testroot/repo/.git/objects/pack/pack-$hash"
256 touch "${kpack%.pack}.keep"
258 # create a few pack files with different objects
259 for i in `seq 5`; do
260 echo "alpha $i" > $testroot/repo/alpha
261 git_commit "$testroot/repo" -m "edit #$i"
262 gotadmin pack -r "$testroot/repo" >/dev/null
263 done
265 # create two packs with all the objects
266 gotadmin pack -a -r "$testroot/repo" >/dev/null
267 gotadmin pack -a -r "$testroot/repo" >/dev/null
269 # create another one with unreachable objects
270 git -C "$testroot/repo" checkout -q -b tempbranch
271 echo "modified alpha on tempbranch" >$testroot/repo/alpha
272 git_commit "$testroot/repo" -m "edit alpha on tempbranch"
273 gotadmin pack -a -r "$testroot/repo" >/dev/null
274 git -C "$testroot/repo" checkout -q master
275 (cd "$testroot/repo" && got branch -d tempbranch) >/dev/null
277 gotadmin cleanup -a -q -r "$testroot/repo"
278 n=$(gotadmin info -r "$testroot/repo" | awk '/^pack files/{print $3}')
279 if [ "$n" -ne 2 ]; then
280 echo "expected 2 pack files left, $n found instead" >&2
281 test_done "$testroot" 1
282 return 1
283 fi
285 if [ ! -f "$kpack" ]; then
286 echo "$kpack disappeared unexpectedly" >&2
287 test_done "$testroot" 1
288 return 1
289 fi
291 if [ ! -f "${kpack%.pack}.keep" ]; then
292 echo "${kpack%.pack}.keep disappeared unexpectedly" >&2
293 test_done "$testroot" 1
294 return 1
295 fi
297 # create one more non-redundant pack
298 for i in `seq 5`; do
299 echo "alpha again $i" > $testroot/repo/alpha
300 git_commit "$testroot/repo" -m "edit $i"
301 done
302 gotadmin pack -r "$testroot/repo" >/dev/null
304 gotadmin cleanup -a -q -r "$testroot/repo"
306 n=$(gotadmin info -r "$testroot/repo" | awk '/^pack files/{print $3}')
307 if [ "$n" -ne 3 ]; then
308 echo "expected 3 pack files left, $n found instead" >&2
309 test_done "$testroot" 1
310 return 1
311 fi
313 # remove the .keep file
314 rm "${kpack%.pack}.keep"
316 # create some commits on a separate branch
317 git -C "$testroot/repo" checkout -q -b newbranch
319 for i in `seq 5`; do
320 echo "alpha $i" > $testroot/repo/alpha
321 git_commit "$testroot/repo" -m "edit #$i"
322 gotadmin pack -r "$testroot/repo" >/dev/null
323 done
325 gotadmin pack -a -x master -r "$testroot/repo" >/dev/null
327 gotadmin cleanup -a -q -r "$testroot/repo"
328 n=$(gotadmin info -r "$testroot/repo" | awk '/^pack files/{print $3}')
329 if [ "$n" -ne 3 ]; then
330 echo "expected 3 pack files left, $n found instead" >&2
331 test_done "$testroot" 1
332 return 1
333 fi
335 test_done "$testroot" 0
338 test_cleanup_precious_objects() {
339 local testroot=`test_init cleanup_precious_objects`
341 # enable Git's preciousObjects extension
342 git -C $testroot/repo config extensions.preciousObjects true
344 # cleanup should now refuse to purge objects
345 gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
346 2> $testroot/stderr
347 ret=$?
348 if [ $ret -eq 0 ]; then
349 echo "gotadmin cleanup succeeded unexpectedly" >&2
350 test_done "$testroot" "1"
351 return 1
352 fi
354 echo -n "gotadmin: the preciousObjects Git extension is enabled; " \
355 > $testroot/stderr.expected
356 echo "this implies that objects must not be deleted" \
357 >> $testroot/stderr.expected
358 cmp -s $testroot/stderr.expected $testroot/stderr
359 ret=$?
360 if [ $ret -ne 0 ]; then
361 diff -u $testroot/stderr.expected $testroot/stderr
362 fi
363 test_done "$testroot" "$ret"
366 test_cleanup_missing_pack_file() {
367 local testroot=`test_init cleanup_missing_pack_file`
369 # no pack files should exist yet
370 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
371 ret=$?
372 if [ $ret -ne 0 ]; then
373 test_done "$testroot" "$ret"
374 return 1
375 fi
376 echo -n > $testroot/stdout.expected
377 cmp -s $testroot/stdout.expected $testroot/stdout
378 ret=$?
379 if [ $ret -ne 0 ]; then
380 diff -u $testroot/stdout.expected $testroot/stdout
381 test_done "$testroot" "$ret"
382 return 1
383 fi
385 gotadmin pack -r $testroot/repo > $testroot/stdout
386 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
387 packhash=`echo $packname | sed -e 's:^objects/pack/pack-::' \
388 -e 's/.pack$//'`
390 # Some freshly cloned Git repositories suffer from lonely pack index
391 # files. Remove the pack file we just wrote to simulate this issue.
392 rm -f $testroot/repo/.git/objects/pack/pack-$packname
394 # cleanup should now refuse to purge objects
395 gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
396 2> $testroot/stderr
397 ret=$?
398 if [ $ret -eq 0 ]; then
399 echo "gotadmin cleanup succeeded unexpectedly" >&2
400 test_done "$testroot" "1"
401 return 1
402 fi
404 echo -n "gotadmin: objects/pack/pack-${packhash}.idx: " \
405 > $testroot/stderr.expected
406 echo -n "pack index has no corresponding pack file; pack file must " \
407 >> $testroot/stderr.expected
408 echo "be restored or 'gotadmin cleanup -p' must be run" \
409 >> $testroot/stderr.expected
410 cmp -s $testroot/stderr.expected $testroot/stderr
411 ret=$?
412 if [ $ret -ne 0 ]; then
413 diff -u $testroot/stderr.expected $testroot/stderr
414 test_done "$testroot" "$ret"
415 return 1
416 fi
418 gotadmin cleanup -a -r $testroot/repo -p -n > $testroot/stdout
419 ret=$?
420 if [ $ret -ne 0 ]; then
421 echo "gotadmin cleanup failed unexpectedly" >&2
422 test_done "$testroot" "$ret"
423 return 1
424 fi
425 packidx_path=$testroot/repo/.git/objects/pack/pack-${packhash}.idx
426 echo "$packidx_path could be removed" > $testroot/stdout.expected
427 cmp -s $testroot/stdout.expected $testroot/stdout
428 ret=$?
429 if [ $ret -ne 0 ]; then
430 diff -u $testroot/stdout.expected $testroot/stdout
431 test_done "$testroot" "$ret"
432 return 1
433 fi
435 gotadmin cleanup -a -r $testroot/repo -p > $testroot/stdout
436 ret=$?
437 if [ $ret -ne 0 ]; then
438 echo "gotadmin cleanup failed unexpectedly" >&2
439 test_done "$testroot" "$ret"
440 return 1
441 fi
442 echo "$packidx_path removed" > $testroot/stdout.expected
443 cmp -s $testroot/stdout.expected $testroot/stdout
444 ret=$?
445 if [ $ret -ne 0 ]; then
446 diff -u $testroot/stdout.expected $testroot/stdout
447 test_done "$testroot" "$ret"
448 return 1
449 fi
451 # cleanup should now attempt to purge objects
452 gotadmin cleanup -a -q -r $testroot/repo > $testroot/stdout \
453 2> $testroot/stderr
454 ret=$?
455 if [ $ret -ne 0 ]; then
456 echo "gotadmin cleanup failed unexpectedly" >&2
457 test_done "$testroot" "1"
458 return 1
459 fi
460 test_done "$testroot" "$ret"
463 test_parseargs "$@"
464 run_test test_cleanup_unreferenced_loose_objects
465 run_test test_cleanup_redundant_loose_objects
466 run_test test_cleanup_redundant_pack_files
467 run_test test_cleanup_precious_objects
468 run_test test_cleanup_missing_pack_file