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" \
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
79 if [ "$ret" != "0" ]; then
80 diff -u $testroot/stdout.expected
$testroot/stdout
81 test_done
"$testroot" "$ret"
84 ls -R $testroot/repo
/.git
/objects
> $testroot/objects-after
85 cmp -s $testroot/objects-before
$testroot/objects-after
87 if [ "$ret" != "0" ]; then
88 diff -u $testroot/objects-before
$testroot/objects-after
89 test_done
"$testroot" "$ret"
93 # cleanup should remove loose objects that belonged to the branch
94 gotadmin cleanup
-a -q -r $testroot/repo
> $testroot/stdout
96 if [ "$ret" != "0" ]; then
97 echo "gotadmin cleanup failed unexpectedly" >&2
98 test_done
"$testroot" "$ret"
101 echo -n > $testroot/stdout.expected
102 cmp -s $testroot/stdout.expected
$testroot/stdout
104 if [ "$ret" != "0" ]; then
105 diff -u $testroot/stdout.expected
$testroot/stdout
106 test_done
"$testroot" "$ret"
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"
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
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"
144 # no pack files should exist yet
145 ls $testroot/repo
/.git
/objects
/pack
/ > $testroot/stdout
147 if [ "$ret" != "0" ]; then
148 test_done
"$testroot" "$ret"
151 echo -n > $testroot/stdout.expected
152 cmp -s $testroot/stdout.expected
$testroot/stdout
154 if [ "$ret" != "0" ]; then
155 diff -u $testroot/stdout.expected
$testroot/stdout
156 test_done
"$testroot" "$ret"
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"
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
176 if [ "$ret" != "0" ]; then
177 diff -u $testroot/stdout.expected
$testroot/stdout
178 test_done
"$testroot" "$ret"
181 ls -R $testroot/repo
/.git
/objects
> $testroot/objects-after
182 cmp -s $testroot/objects-before
$testroot/objects-after
184 if [ "$ret" != "0" ]; then
185 diff -u $testroot/objects-before
$testroot/objects-after
186 test_done
"$testroot" "$ret"
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"
198 # cleanup should remove all loose objects
199 gotadmin cleanup
-a -q -r $testroot/repo
> $testroot/stdout
201 if [ "$ret" != "0" ]; then
202 echo "gotadmin cleanup failed unexpectedly" >&2
203 test_done
"$testroot" "$ret"
206 echo -n > $testroot/stdout.expected
207 cmp -s $testroot/stdout.expected
$testroot/stdout
209 if [ "$ret" != "0" ]; then
210 diff -u $testroot/stdout.expected
$testroot/stdout
211 test_done
"$testroot" "$ret"
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"
223 for d
in $testroot/repo
/.git
/objects
/[0-9a-f][0-9a-f]; do
228 echo "loose object $obj_id was not purged" >&2
232 if [ "$ret" = "1" ]; then
237 test_done
"$testroot" "$ret"
240 test_cleanup_precious_objects
() {
241 local testroot
=`test_init cleanup_precious_objects`
243 # enable Git's preciousObjects extension
244 (cd $testroot/repo
&& git config extensions.preciousObjects true
)
246 # cleanup should now refuse to purge objects
247 gotadmin cleanup
-a -q -r $testroot/repo
> $testroot/stdout \
250 if [ "$ret" = "0" ]; then
251 echo "gotadmin cleanup succeeded unexpectedly" >&2
252 test_done
"$testroot" "1"
256 echo -n "gotadmin: the preciousObjects Git extension is enabled; " \
257 > $testroot/stderr.expected
258 echo "this implies that objects must not be deleted" \
259 >> $testroot/stderr.expected
260 cmp -s $testroot/stderr.expected
$testroot/stderr
262 if [ "$ret" != "0" ]; then
263 diff -u $testroot/stderr.expected
$testroot/stderr
265 test_done
"$testroot" "$ret"
268 test_cleanup_missing_pack_file
() {
269 local testroot
=`test_init cleanup_missing_pack_file`
271 # no pack files should exist yet
272 ls $testroot/repo
/.git
/objects
/pack
/ > $testroot/stdout
274 if [ "$ret" != "0" ]; then
275 test_done
"$testroot" "$ret"
278 echo -n > $testroot/stdout.expected
279 cmp -s $testroot/stdout.expected
$testroot/stdout
281 if [ "$ret" != "0" ]; then
282 diff -u $testroot/stdout.expected
$testroot/stdout
283 test_done
"$testroot" "$ret"
287 gotadmin pack
-r $testroot/repo
> $testroot/stdout
288 packname
=`grep ^Wrote $testroot/stdout | cut -d ' ' -f2`
289 packhash
=`echo $packname | sed -e 's:^objects/pack/pack-::' \
292 # Some freshly cloned Git repositories suffer from lonely pack index
293 # files. Remove the pack file we just wrote to simulate this issue.
294 rm $testroot/repo
/.git
/objects
/pack
/pack-
$packname
296 # cleanup should now refuse to purge objects
297 gotadmin cleanup
-a -q -r $testroot/repo
> $testroot/stdout \
300 if [ "$ret" = "0" ]; then
301 echo "gotadmin cleanup succeeded unexpectedly" >&2
302 test_done
"$testroot" "1"
306 echo -n "gotadmin: objects/pack/pack-${packhash}.idx: " \
307 > $testroot/stderr.expected
308 echo -n "pack index has no corresponding pack file; pack file must " \
309 >> $testroot/stderr.expected
310 echo "be restored or 'gotadmin cleanup -p' must be run" \
311 >> $testroot/stderr.expected
312 cmp -s $testroot/stderr.expected
$testroot/stderr
314 if [ "$ret" != "0" ]; then
315 diff -u $testroot/stderr.expected
$testroot/stderr
316 test_done
"$testroot" "$ret"
320 gotadmin cleanup
-a -r $testroot/repo
-p -n > $testroot/stdout
322 if [ "$ret" != "0" ]; then
323 echo "gotadmin cleanup failed unexpectedly" >&2
324 test_done
"$testroot" "$ret"
327 packidx_path
=$testroot/repo
/.git
/objects
/pack
/pack-
${packhash}.idx
328 echo "$packidx_path could be removed" > $testroot/stdout.expected
329 cmp -s $testroot/stdout.expected
$testroot/stdout
331 if [ "$ret" != "0" ]; then
332 diff -u $testroot/stdout.expected
$testroot/stdout
333 test_done
"$testroot" "$ret"
337 gotadmin cleanup
-a -r $testroot/repo
-p > $testroot/stdout
339 if [ "$ret" != "0" ]; then
340 echo "gotadmin cleanup failed unexpectedly" >&2
341 test_done
"$testroot" "$ret"
344 echo "$packidx_path removed" > $testroot/stdout.expected
345 cmp -s $testroot/stdout.expected
$testroot/stdout
347 if [ "$ret" != "0" ]; then
348 diff -u $testroot/stdout.expected
$testroot/stdout
349 test_done
"$testroot" "$ret"
353 # cleanup should now attempt to purge objects
354 gotadmin cleanup
-a -q -r $testroot/repo
> $testroot/stdout \
357 if [ "$ret" != "0" ]; then
358 echo "gotadmin cleanup failed unexpectedly" >&2
359 test_done
"$testroot" "1"
362 test_done
"$testroot" "$ret"
366 run_test test_cleanup_unreferenced_loose_objects
367 run_test test_cleanup_redundant_loose_objects
368 run_test test_cleanup_precious_objects
369 run_test test_cleanup_missing_pack_file