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 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"
27 return 1
28 fi
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
34 ret="$?"
35 if [ "$ret" != "0" ]; then
36 echo "got checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
38 return 1
39 fi
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"
58 return 1
59 fi
61 # delete the branch
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 -1 -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
74 ret="$?"
75 if [ "$ret" != "0" ]; then
76 diff -u $testroot/stdout.expected $testroot/stdout
77 test_done "$testroot" "$ret"
78 return 1
79 fi
80 ls -1 -R $testroot/repo/.git/objects > $testroot/objects-after
81 cmp -s $testroot/objects-before $testroot/objects-after
82 ret="$?"
83 if [ "$ret" != "0" ]; then
84 diff -u $testroot/objects-before $testroot/objects-after
85 test_done "$testroot" "$ret"
86 return 1
87 fi
89 # cleanup should remove loose objects that belonged to the branch
90 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
91 ret="$?"
92 if [ "$ret" != "0" ]; then
93 echo "gotadmin cleanup failed unexpectedly" >&2
94 test_done "$testroot" "$ret"
95 return 1
96 fi
97 echo -n > $testroot/stdout.expected
98 cmp -s $testroot/stdout.expected $testroot/stdout
99 ret="$?"
100 if [ "$ret" != "0" ]; then
101 diff -u $testroot/stdout.expected $testroot/stdout
102 test_done "$testroot" "$ret"
103 return 1
104 fi
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"
111 return 1
112 fi
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
118 ret=1
119 break
120 fi
121 done
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"
137 return 1
138 fi
140 # no pack files should exist yet
141 ls $testroot/repo/.git/objects/pack/ > $testroot/stdout
142 ret="$?"
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
145 return 1
146 fi
147 echo -n > $testroot/stdout.expected
148 cmp -s $testroot/stdout.expected $testroot/stdout
149 ret="$?"
150 if [ "$ret" != "0" ]; then
151 diff -u $testroot/stdout.expected $testroot/stdout
152 test_done "$testroot" "$ret"
153 return 1
154 fi
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"
163 return 1
164 fi
166 # cleanup -n should not remove any objects
167 ls -1 -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
171 ret="$?"
172 if [ "$ret" != "0" ]; then
173 diff -u $testroot/stdout.expected $testroot/stdout
174 test_done "$testroot" "$ret"
175 return 1
176 fi
177 ls -1 -R $testroot/repo/.git/objects > $testroot/objects-after
178 cmp -s $testroot/objects-before $testroot/objects-after
179 ret="$?"
180 if [ "$ret" != "0" ]; then
181 diff -u $testroot/objects-before $testroot/objects-after
182 test_done "$testroot" "$ret"
183 return 1
184 fi
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"
191 return 1
192 fi
194 # cleanup should remove all loose objects
195 gotadmin cleanup -q -r $testroot/repo > $testroot/stdout
196 ret="$?"
197 if [ "$ret" != "0" ]; then
198 echo "gotadmin cleanup failed unexpectedly" >&2
199 test_done "$testroot" "$ret"
200 return 1
201 fi
202 echo -n > $testroot/stdout.expected
203 cmp -s $testroot/stdout.expected $testroot/stdout
204 ret="$?"
205 if [ "$ret" != "0" ]; then
206 diff -u $testroot/stdout.expected $testroot/stdout
207 test_done "$testroot" "$ret"
208 return 1
209 fi
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"
216 return 1
217 fi
219 for d in $testroot/repo/.git/objects/[0-9a-f][0-9a-f]; do
220 id0=`basename $d`
221 ret=0
222 for e in `ls $d`; do
223 obj_id=${id0}${e}
224 echo "loose object $obj_id was not purged" >&2
225 ret=1
226 break
227 done
228 if [ "$ret" == "1" ]; then
229 break
230 fi
231 done
233 test_done "$testroot" "$ret"
236 test_parseargs "$@"
237 run_test test_cleanup_unreferenced_loose_objects
238 run_test test_cleanup_redundant_loose_objects