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 test_cleanup_unreferenced_loose_objects() {
20 local testroot=`test_init cleanup_unreferenced_loose_objects`
22 nloose0=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
23 cut -d ':' -f 2 | tr -d ' '`
24 if [ "$nloose0" != "8" ]; then
25 echo "unexpected number of loose objects: $nloose0" >&2
26 test_done "$testroot" "1"
30 # create a branch with some changes
31 got branch -r $testroot/repo newbranch >/dev/null
33 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
35 if [ "$ret" != "0" ]; then
36 echo "got checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
41 echo 'foo' > $testroot/wt/foo
42 (cd $testroot/wt && got add foo > /dev/null)
43 echo 'modified alpha' > $testroot/wt/alpha
44 (cd $testroot/wt && got commit -m 'newbranch commit' > /dev/null)
45 local commit1=`git_show_branch_head $testroot/repo newbranch`
46 local tree1=`got cat -r $testroot/repo $newbranch_commit | \
47 grep ^tree | cut -d ' ' -f2`
48 local alpha1=`got tree -r $testroot/repo -i -c $commit1 | \
49 grep "[0-9a-f] alpha$" | cut -d' ' -f 1`
50 local foo1=`got tree -r $testroot/repo -i -c $commit1 | \
51 grep "[0-9a-f] foo$" | cut -d' ' -f 1`
53 nloose1=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
54 cut -d ':' -f 2 | tr -d ' '`
55 if [ "$nloose1" != "12" ]; then
56 echo "unexpected number of loose objects: $nloose1" >&2
57 test_done "$testroot" "1"
62 got branch -r $testroot/repo -d newbranch >/dev/null
64 # remove worktree's base commit reference, which points at the branch
65 wt_uuid=`(cd $testroot/wt && got info | grep 'UUID:' | \
66 cut -d ':' -f 2 | tr -d ' ')`
67 got ref -r $testroot/repo -d "refs/got/worktree/base-$wt_uuid"
69 # cleanup -n should not remove any objects
70 ls -R $testroot/repo/.git/objects > $testroot/objects-before
71 gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
72 echo -n > $testroot/stdout.expected
73 cmp -s $testroot/stdout.expected $testroot/stdout
75 if [ "$ret" != "0" ]; then
76 diff -u $testroot/stdout.expected $testroot/stdout
77 test_done "$testroot" "$ret"
80 ls -R $testroot/repo/.git/objects > $testroot/objects-after
81 cmp -s $testroot/objects-before $testroot/objects-after
83 if [ "$ret" != "0" ]; then
84 diff -u $testroot/objects-before $testroot/objects-after
85 test_done "$testroot" "$ret"
89 # cleanup should remove loose objects that belonged to the branch
90 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
92 if [ "$ret" != "0" ]; then
93 echo "gotadmin cleanup failed unexpectedly" >&2
94 test_done "$testroot" "$ret"
97 echo -n > $testroot/stdout.expected
98 cmp -s $testroot/stdout.expected $testroot/stdout
100 if [ "$ret" != "0" ]; then
101 diff -u $testroot/stdout.expected $testroot/stdout
102 test_done "$testroot" "$ret"
106 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
107 cut -d ':' -f 2 | tr -d ' '`
108 if [ "$nloose2" != "$nloose0" ]; then
109 echo "unexpected number of loose objects: $nloose2" >&2
110 test_done "$testroot" "1"
114 for id in $commit1 $tree1 $alpha1 $foo1; do
115 path=`get_loose_object_path $testroot/repo $id`
116 if [ -e "$path" ]; then
117 echo "loose object $path was not purged" >&2
123 test_done "$testroot" "$ret"
126 test_cleanup_redundant_loose_objects() {
127 local testroot=`test_init cleanup_redundant_loose_objects`
129 # tags should also be packed
130 got tag -r $testroot/repo -m 1.0 1.0 >/dev/null
132 nloose0=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
133 cut -d ':' -f 2 | tr -d ' '`
134 if [ "$nloose0" != "9" ]; then
135 echo "unexpected number of loose objects: $nloose0" >&2
136 test_done "$testroot" "1"
140 # no pack files should exist yet
141 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
147 echo -n > $testroot/stdout.expected
148 cmp -s $testroot/stdout.expected $testroot/stdout
150 if [ "$ret" != "0" ]; then
151 diff -u $testroot/stdout.expected $testroot/stdout
152 test_done "$testroot" "$ret"
156 gotadmin pack -r $testroot/repo > /dev/null
158 npacked0=`gotadmin info -r $testroot/repo | grep '^packed objects:' | \
159 cut -d ':' -f 2 | tr -d ' '`
160 if [ "$npacked0" != "9" ]; then
161 echo "unexpected number of loose objects: $npacked0" >&2
162 test_done "$testroot" "1"
166 # cleanup -n should not remove any objects
167 ls -R $testroot/repo/.git/objects > $testroot/objects-before
168 gotadmin cleanup -n -q -r $testroot/repo > $testroot/stdout
169 echo -n > $testroot/stdout.expected
170 cmp -s $testroot/stdout.expected $testroot/stdout
172 if [ "$ret" != "0" ]; then
173 diff -u $testroot/stdout.expected $testroot/stdout
174 test_done "$testroot" "$ret"
177 ls -R $testroot/repo/.git/objects > $testroot/objects-after
178 cmp -s $testroot/objects-before $testroot/objects-after
180 if [ "$ret" != "0" ]; then
181 diff -u $testroot/objects-before $testroot/objects-after
182 test_done "$testroot" "$ret"
186 nloose1=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
187 cut -d ':' -f 2 | tr -d ' '`
188 if [ "$nloose1" != "$nloose0" ]; then
189 echo "unexpected number of loose objects: $nloose1" >&2
190 test_done "$testroot" "1"
194 # cleanup should remove all loose objects
195 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
197 if [ "$ret" != "0" ]; then
198 echo "gotadmin cleanup failed unexpectedly" >&2
199 test_done "$testroot" "$ret"
202 echo -n > $testroot/stdout.expected
203 cmp -s $testroot/stdout.expected $testroot/stdout
205 if [ "$ret" != "0" ]; then
206 diff -u $testroot/stdout.expected $testroot/stdout
207 test_done "$testroot" "$ret"
211 nloose2=`gotadmin info -r $testroot/repo | grep '^loose objects:' | \
212 cut -d ':' -f 2 | tr -d ' '`
213 if [ "$nloose2" != "0" ]; then
214 echo "unexpected number of loose objects: $nloose2" >&2
215 test_done "$testroot" "1"
219 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
224 echo "loose object $obj_id was not purged" >&2
228 if [ "$ret" = "1" ]; then
233 test_done "$testroot" "$ret"
237 run_test test_cleanup_unreferenced_loose_objects
238 run_test test_cleanup_redundant_loose_objects