3 # Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
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"
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
38 if [ "$ret" != "0" ]; then
39 echo "got checkout command failed unexpectedly"
40 test_done "$testroot" "$ret"
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"
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"
72 # cleanup -n should not remove any objects
73 ls -R $testroot/repo/.git/objects > $testroot/objects-before
74 gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
75 echo -n > $testroot/stdout.expected
76 cmp -s $testroot/stdout.expected $testroot/stdout
78 if [ "$ret" != "0" ]; then
79 diff -u $testroot/stdout.expected $testroot/stdout
80 test_done "$testroot" "$ret"
83 ls -R $testroot/repo/.git/objects > $testroot/objects-after
84 cmp -s $testroot/objects-before $testroot/objects-after
86 if [ "$ret" != "0" ]; then
87 diff -u $testroot/objects-before $testroot/objects-after
88 test_done "$testroot" "$ret"
92 # cleanup should remove loose objects that belonged to the branch
93 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
95 if [ "$ret" != "0" ]; then
96 echo "gotadmin cleanup failed unexpectedly" >&2
97 test_done "$testroot" "$ret"
100 echo -n > $testroot/stdout.expected
101 cmp -s $testroot/stdout.expected $testroot/stdout
103 if [ "$ret" != "0" ]; then
104 diff -u $testroot/stdout.expected $testroot/stdout
105 test_done "$testroot" "$ret"
109 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
110 cut -d ':' -f 2 | tr -d ' '`
111 if [ "$nloose2" != "$nloose0" ]; then
112 echo "unexpected number of loose objects: $nloose2" >&2
113 test_done "$testroot" "1"
117 for id in $commit1 $tree1 $alpha1 $foo1; do
118 path=`get_loose_object_path $testroot/repo $id`
119 if [ -e "$path" ]; then
120 echo "loose object $path was not purged" >&2
126 test_done "$testroot" "$ret"
129 test_cleanup_redundant_loose_objects() {
130 local testroot=`test_init cleanup_redundant_loose_objects`
132 # tags should also be packed
133 got tag -r $testroot/repo -m 1.0 1.0 >/dev/null
135 nloose0=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
136 cut -d ':' -f 2 | tr -d ' '`
137 if [ "$nloose0" != "9" ]; then
138 echo "unexpected number of loose objects: $nloose0" >&2
139 test_done "$testroot" "1"
143 # no pack files should exist yet
144 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
146 if [ "$ret" != "0" ]; then
147 test_done "$testroot" "$ret"
150 echo -n > $testroot/stdout.expected
151 cmp -s $testroot/stdout.expected $testroot/stdout
153 if [ "$ret" != "0" ]; then
154 diff -u $testroot/stdout.expected $testroot/stdout
155 test_done "$testroot" "$ret"
159 gotadmin pack -r $testroot/repo > /dev/null
161 npacked0=`gotadmin info -r $testroot/repo | grep '^packed objects:' | \
162 cut -d ':' -f 2 | tr -d ' '`
163 if [ "$npacked0" != "9" ]; then
164 echo "unexpected number of loose objects: $npacked0" >&2
165 test_done "$testroot" "1"
169 # cleanup -n should not remove any objects
170 ls -R $testroot/repo/.git/objects > $testroot/objects-before
171 gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
172 echo -n > $testroot/stdout.expected
173 cmp -s $testroot/stdout.expected $testroot/stdout
175 if [ "$ret" != "0" ]; then
176 diff -u $testroot/stdout.expected $testroot/stdout
177 test_done "$testroot" "$ret"
180 ls -R $testroot/repo/.git/objects > $testroot/objects-after
181 cmp -s $testroot/objects-before $testroot/objects-after
183 if [ "$ret" != "0" ]; then
184 diff -u $testroot/objects-before $testroot/objects-after
185 test_done "$testroot" "$ret"
189 nloose1=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
190 cut -d ':' -f 2 | tr -d ' '`
191 if [ "$nloose1" != "$nloose0" ]; then
192 echo "unexpected number of loose objects: $nloose1" >&2
193 test_done "$testroot" "1"
197 # cleanup should remove all loose objects
198 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
200 if [ "$ret" != "0" ]; then
201 echo "gotadmin cleanup failed unexpectedly" >&2
202 test_done "$testroot" "$ret"
205 echo -n > $testroot/stdout.expected
206 cmp -s $testroot/stdout.expected $testroot/stdout
208 if [ "$ret" != "0" ]; then
209 diff -u $testroot/stdout.expected $testroot/stdout
210 test_done "$testroot" "$ret"
214 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
215 cut -d ':' -f 2 | tr -d ' '`
216 if [ "$nloose2" != "0" ]; then
217 echo "unexpected number of loose objects: $nloose2" >&2
218 test_done "$testroot" "1"
222 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
227 echo "loose object $obj_id was not purged" >&2
231 if [ "$ret" = "1" ]; then
236 test_done "$testroot" "$ret"
239 test_cleanup_precious_objects() {
240 local testroot=`test_init cleanup_precious_objects`
242 # enable Git's preciousObjects extension
243 (cd $testroot/repo && git config extensions.preciousObjects true)
245 # cleanup should now refuse to purge objects
246 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
249 if [ "$ret" == "0" ]; then
250 echo "gotadmin cleanup succeeded unexpectedly" >&2
251 test_done "$testroot" "1"
255 echo -n "gotadmin: the preciousObjects Git extension is enabled; " \
256 > $testroot/stderr.expected
257 echo "this implies that objects must not be deleted" \
258 >> $testroot/stderr.expected
259 cmp -s $testroot/stderr.expected $testroot/stderr
261 if [ "$ret" != "0" ]; then
262 diff -u $testroot/stderr.expected $testroot/stderr
264 test_done "$testroot" "$ret"
267 test_cleanup_missing_pack_file() {
268 local testroot=`test_init cleanup_missing_pack_file`
270 # no pack files should exist yet
271 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
273 if [ "$ret" != "0" ]; then
274 test_done "$testroot" "$ret"
277 echo -n > $testroot/stdout.expected
278 cmp -s $testroot/stdout.expected $testroot/stdout
280 if [ "$ret" != "0" ]; then
281 diff -u $testroot/stdout.expected $testroot/stdout
282 test_done "$testroot" "$ret"
286 gotadmin pack -r $testroot/repo > $testroot/stdout
287 packname=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
288 packhash=`echo $packname | sed -e 's:^objects/pack/pack-::' \
291 # Some freshly cloned Git repositories suffer from lonely pack index
292 # files. Remove the pack file we just wrote to simulate this issue.
293 rm $testroot/repo/.git/objects/pack/pack-$packname
295 # cleanup should now refuse to purge objects
296 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
299 if [ "$ret" == "0" ]; then
300 echo "gotadmin cleanup succeeded unexpectedly" >&2
301 test_done "$testroot" "1"
305 echo -n "gotadmin: objects/pack/pack-${packhash}.idx: " \
306 > $testroot/stderr.expected
307 echo "pack index has no corresponding pack file" \
308 >> $testroot/stderr.expected
309 cmp -s $testroot/stderr.expected $testroot/stderr
311 if [ "$ret" != "0" ]; then
312 diff -u $testroot/stderr.expected $testroot/stderr
313 test_done "$testroot" "$ret"
317 gotadmin cleanup -r $testroot/repo -p -n > $testroot/stdout
319 if [ "$ret" != "0" ]; then
320 echo "gotadmin cleanup failed unexpectedly" >&2
321 test_done "$testroot" "$ret"
324 packidx_path=$testroot/repo/.git/objects/pack/pack-${packhash}.idx
325 echo "$packidx_path could be removed" > $testroot/stdout.expected
326 cmp -s $testroot/stdout.expected $testroot/stdout
328 if [ "$ret" != "0" ]; then
329 diff -u $testroot/stdout.expected $testroot/stdout
330 test_done "$testroot" "$ret"
334 gotadmin cleanup -r $testroot/repo -p > $testroot/stdout
336 if [ "$ret" != "0" ]; then
337 echo "gotadmin cleanup failed unexpectedly" >&2
338 test_done "$testroot" "$ret"
341 echo "$packidx_path removed" > $testroot/stdout.expected
342 cmp -s $testroot/stdout.expected $testroot/stdout
344 if [ "$ret" != "0" ]; then
345 diff -u $testroot/stdout.expected $testroot/stdout
346 test_done "$testroot" "$ret"
350 # cleanup should now attempt to purge objects
351 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout \
354 if [ "$ret" != "0" ]; then
355 echo "gotadmin cleanup failed unexpectedly" >&2
356 test_done "$testroot" "1"
359 test_done "$testroot" "$ret"
363 run_test test_cleanup_unreferenced_loose_objects
364 run_test test_cleanup_redundant_loose_objects
365 run_test test_cleanup_precious_objects
366 run_test test_cleanup_missing_pack_file