Blame


1 0e673013 2019-01-02 stsp #!/bin/sh
2 0e673013 2019-01-02 stsp #
3 5aa81393 2020-01-06 stsp # Copyright (c) 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 0e673013 2019-01-02 stsp #
5 0e673013 2019-01-02 stsp # Permission to use, copy, modify, and distribute this software for any
6 0e673013 2019-01-02 stsp # purpose with or without fee is hereby granted, provided that the above
7 0e673013 2019-01-02 stsp # copyright notice and this permission notice appear in all copies.
8 0e673013 2019-01-02 stsp #
9 0e673013 2019-01-02 stsp # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 0e673013 2019-01-02 stsp # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 0e673013 2019-01-02 stsp # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 0e673013 2019-01-02 stsp # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 0e673013 2019-01-02 stsp # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 0e673013 2019-01-02 stsp # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 0e673013 2019-01-02 stsp # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 0e673013 2019-01-02 stsp
17 0b2899f8 2019-08-18 stsp export GIT_AUTHOR_NAME="Flan Hacker"
18 0b2899f8 2019-08-18 stsp export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
19 0b2899f8 2019-08-18 stsp export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
20 0b2899f8 2019-08-18 stsp export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
21 0b2899f8 2019-08-18 stsp export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
22 82f6abb8 2019-08-14 stsp export GOT_AUTHOR_8="flan_hac"
23 e600f124 2021-03-21 stsp export GOT_AUTHOR_11="flan_hacker"
24 b1ebc001 2019-08-13 stsp export GOT_LOG_DEFAULT_LIMIT=0
25 11f4fa81 2020-10-01 stsp export GOT_TEST_ROOT="/tmp"
26 c3754a5b 2019-05-10 stsp
27 7b67836a 2019-05-10 stsp export MALLOC_OPTIONS=S
28 7b67836a 2019-05-10 stsp
29 fa37079f 2021-10-09 thomas date()
30 fa37079f 2021-10-09 thomas {
31 fa37079f 2021-10-09 thomas DATECMD="date"
32 1ff9fea4 2021-11-23 thomas [ "$PLATFORM" != "linux" ] && DATECMD="gdate"
33 fa37079f 2021-10-09 thomas
34 c206b220 2021-10-09 thomas command "$DATECMD" "$@"
35 fa37079f 2021-10-09 thomas }
36 fa37079f 2021-10-09 thomas
37 fa37079f 2021-10-09 thomas ln()
38 fa37079f 2021-10-09 thomas {
39 fa37079f 2021-10-09 thomas LNCMD="ln"
40 1ff9fea4 2021-11-23 thomas [ "$PLATFORM" != "linux" ] && LNCMD="gln"
41 fa37079f 2021-10-09 thomas
42 c206b220 2021-10-09 thomas command "$LNCMD" "$@"
43 fa37079f 2021-10-09 thomas }
44 fa37079f 2021-10-09 thomas
45 c206b220 2021-10-09 thomas sed()
46 c206b220 2021-10-09 thomas {
47 c206b220 2021-10-09 thomas SEDCMD="sed"
48 186c23b6 2022-03-08 thomas
49 186c23b6 2022-03-08 thomas # On non-linux systems, the sed command can happily accept "-i ''" as
50 186c23b6 2022-03-08 thomas # a valid command to not save backup files for in-place edits.
51 186c23b6 2022-03-08 thomas # However, on linux, "-i ''" would be treated as "-i" with a blank
52 186c23b6 2022-03-08 thomas # argument, and hence, no file to edit in-place, which is an error.
53 186c23b6 2022-03-08 thomas #
54 186c23b6 2022-03-08 thomas # Therefore, scan the argument list and remove "-i ''", replacing it
55 186c23b6 2022-03-08 thomas # with just "-i".
56 1ff9fea4 2021-11-23 thomas [ "$PLATFORM" = "linux" ] && {
57 c206b220 2021-10-09 thomas set -- "$@"
58 186c23b6 2022-03-08 thomas i=1
59 186c23b6 2022-03-08 thomas while [ "$i" -le "$#" ]; do
60 186c23b6 2022-03-08 thomas m=$((i + 1))
61 186c23b6 2022-03-08 thomas [ "${!i}" = "-i" ] && [ -z "${!m}" ] && {
62 186c23b6 2022-03-08 thomas shift 2
63 186c23b6 2022-03-08 thomas command "$SEDCMD" -i "$@"
64 186c23b6 2022-03-08 thomas return
65 186c23b6 2022-03-08 thomas }
66 186c23b6 2022-03-08 thomas i=$((i + 1))
67 186c23b6 2022-03-08 thomas done
68 c206b220 2021-10-09 thomas }
69 c206b220 2021-10-09 thomas command "$SEDCMD" "$@"
70 c206b220 2021-10-09 thomas }
71 fa37079f 2021-10-09 thomas
72 fa37079f 2021-10-09 thomas
73 f6cae3ed 2020-09-13 naddy git_init()
74 0e673013 2019-01-02 stsp {
75 0b2899f8 2019-08-18 stsp git init -q "$1"
76 f60607c8 2021-09-30 thomas
77 f60607c8 2021-09-30 thomas # Switch the default branch to match our test expectations if needed.
78 f60607c8 2021-09-30 thomas # Only need to change HEAD since 'git init' did not create any refs.
79 f60607c8 2021-09-30 thomas # Relying on implementation details of 'git init' is no problem for us.
80 f60607c8 2021-09-30 thomas # We want to be alerted when Git changes fundamental assumptions such
81 f60607c8 2021-09-30 thomas # as what an empty repository looks like and where the default branch
82 f60607c8 2021-09-30 thomas # is set. In such cases Got's own tooling might well need to change
83 f60607c8 2021-09-30 thomas # its behaviour, too, and our tests should fail.
84 f60607c8 2021-09-30 thomas # TODO: Update all tests to assume 'main' instead of 'master' and
85 f60607c8 2021-09-30 thomas # switch to main here, to match Got's own default.
86 f60607c8 2021-09-30 thomas echo "ref: refs/heads/master" > "$1/.git/HEAD"
87 0e673013 2019-01-02 stsp }
88 0e673013 2019-01-02 stsp
89 f6cae3ed 2020-09-13 naddy maybe_pack_repo()
90 b32c4525 2020-01-05 stsp {
91 b32c4525 2020-01-05 stsp local repo="$1"
92 b32c4525 2020-01-05 stsp if [ -n "$GOT_TEST_PACK" ]; then
93 b32c4525 2020-01-05 stsp (cd $repo && git repack -a -q)
94 b32c4525 2020-01-05 stsp fi
95 b32c4525 2020-01-05 stsp }
96 b32c4525 2020-01-05 stsp
97 f6cae3ed 2020-09-13 naddy git_commit()
98 0e673013 2019-01-02 stsp {
99 0e673013 2019-01-02 stsp local repo="$1"
100 0e673013 2019-01-02 stsp shift
101 0b2899f8 2019-08-18 stsp (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
102 b32c4525 2020-01-05 stsp maybe_pack_repo $repo
103 0e673013 2019-01-02 stsp }
104 0e673013 2019-01-02 stsp
105 f6cae3ed 2020-09-13 naddy git_rm()
106 512f0d0e 2019-01-02 stsp {
107 512f0d0e 2019-01-02 stsp local repo="$1"
108 512f0d0e 2019-01-02 stsp shift
109 512f0d0e 2019-01-02 stsp (cd $repo && git rm -q "$@")
110 512f0d0e 2019-01-02 stsp }
111 512f0d0e 2019-01-02 stsp
112 f6cae3ed 2020-09-13 naddy git_show_head()
113 9c4b8182 2019-01-02 stsp {
114 9c4b8182 2019-01-02 stsp local repo="$1"
115 9c4b8182 2019-01-02 stsp (cd $repo && git show --no-patch --pretty='format:%H')
116 9c4b8182 2019-01-02 stsp }
117 9c4b8182 2019-01-02 stsp
118 f6cae3ed 2020-09-13 naddy git_show_branch_head()
119 db32465d 2020-02-07 stsp {
120 db32465d 2020-02-07 stsp local repo="$1"
121 db32465d 2020-02-07 stsp local branch="$2"
122 db32465d 2020-02-07 stsp (cd $repo && git show --no-patch --pretty='format:%H' $branch)
123 db32465d 2020-02-07 stsp }
124 db32465d 2020-02-07 stsp
125 db32465d 2020-02-07 stsp
126 f6cae3ed 2020-09-13 naddy git_show_author_time()
127 bcb49d15 2019-08-14 stsp {
128 bcb49d15 2019-08-14 stsp local repo="$1"
129 01073a5d 2019-08-22 stsp local object="$2"
130 01073a5d 2019-08-22 stsp (cd $repo && git show --no-patch --pretty='format:%at' $object)
131 bcb49d15 2019-08-14 stsp }
132 bcb49d15 2019-08-14 stsp
133 f6cae3ed 2020-09-13 naddy git_show_tagger_time()
134 8e7bd50a 2019-08-22 stsp {
135 8e7bd50a 2019-08-22 stsp local repo="$1"
136 8e7bd50a 2019-08-22 stsp local tag="$2"
137 8e7bd50a 2019-08-22 stsp (cd $repo && git cat-file tag $tag | grep ^tagger | \
138 8e7bd50a 2019-08-22 stsp sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
139 8e7bd50a 2019-08-22 stsp }
140 8e7bd50a 2019-08-22 stsp
141 f6cae3ed 2020-09-13 naddy git_show_parent_commit()
142 818c7501 2019-07-11 stsp {
143 818c7501 2019-07-11 stsp local repo="$1"
144 70551d57 2020-04-24 stsp local commit="$2"
145 70551d57 2020-04-24 stsp (cd $repo && git show --no-patch --pretty='format:%P' $commit)
146 818c7501 2019-07-11 stsp }
147 818c7501 2019-07-11 stsp
148 f6cae3ed 2020-09-13 naddy git_show_tree()
149 03415a1a 2019-06-02 stsp {
150 03415a1a 2019-06-02 stsp local repo="$1"
151 03415a1a 2019-06-02 stsp (cd $repo && git show --no-patch --pretty='format:%T')
152 03415a1a 2019-06-02 stsp }
153 03415a1a 2019-06-02 stsp
154 f6cae3ed 2020-09-13 naddy trim_obj_id()
155 818c7501 2019-07-11 stsp {
156 9439a990 2020-09-16 naddy local trimcount=$1
157 9439a990 2020-09-16 naddy local id=$2
158 818c7501 2019-07-11 stsp
159 9439a990 2020-09-16 naddy local pat=""
160 9439a990 2020-09-16 naddy while [ "$trimcount" -gt 0 ]; do
161 818c7501 2019-07-11 stsp pat="[0-9a-f]$pat"
162 9439a990 2020-09-16 naddy trimcount=$((trimcount - 1))
163 818c7501 2019-07-11 stsp done
164 818c7501 2019-07-11 stsp
165 818c7501 2019-07-11 stsp echo ${id%$pat}
166 818c7501 2019-07-11 stsp }
167 818c7501 2019-07-11 stsp
168 f6cae3ed 2020-09-13 naddy git_commit_tree()
169 03415a1a 2019-06-02 stsp {
170 03415a1a 2019-06-02 stsp local repo="$1"
171 03415a1a 2019-06-02 stsp local msg="$2"
172 03415a1a 2019-06-02 stsp local tree="$3"
173 03415a1a 2019-06-02 stsp (cd $repo && git commit-tree -m "$msg" "$tree")
174 03415a1a 2019-06-02 stsp }
175 03415a1a 2019-06-02 stsp
176 f0fd0aaf 2021-09-14 stsp git_fsck()
177 f0fd0aaf 2021-09-14 stsp {
178 f0fd0aaf 2021-09-14 stsp local testroot="$1"
179 f0fd0aaf 2021-09-14 stsp local repo="$2"
180 f0fd0aaf 2021-09-14 stsp
181 f0fd0aaf 2021-09-14 stsp (cd $repo && git fsck --strict \
182 f0fd0aaf 2021-09-14 stsp > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
183 fc414659 2022-04-16 thomas ret=$?
184 fc414659 2022-04-16 thomas if [ $ret -ne 0 ]; then
185 f0fd0aaf 2021-09-14 stsp echo -n "git fsck: "
186 f0fd0aaf 2021-09-14 stsp cat $testroot/fsck.stderr
187 f0fd0aaf 2021-09-14 stsp echo "git fsck failed; leaving test data in $testroot"
188 f0fd0aaf 2021-09-14 stsp return 1
189 f0fd0aaf 2021-09-14 stsp fi
190 f0fd0aaf 2021-09-14 stsp
191 f0fd0aaf 2021-09-14 stsp return 0
192 f0fd0aaf 2021-09-14 stsp }
193 f0fd0aaf 2021-09-14 stsp
194 f6cae3ed 2020-09-13 naddy make_test_tree()
195 0e673013 2019-01-02 stsp {
196 0e673013 2019-01-02 stsp repo="$1"
197 0e673013 2019-01-02 stsp
198 0e673013 2019-01-02 stsp echo alpha > $repo/alpha
199 0e673013 2019-01-02 stsp echo beta > $repo/beta
200 0e673013 2019-01-02 stsp mkdir $repo/gamma
201 0e673013 2019-01-02 stsp echo delta > $repo/gamma/delta
202 0e673013 2019-01-02 stsp mkdir $repo/epsilon
203 0e673013 2019-01-02 stsp echo zeta > $repo/epsilon/zeta
204 0e673013 2019-01-02 stsp }
205 0e673013 2019-01-02 stsp
206 f6cae3ed 2020-09-13 naddy make_single_file_repo()
207 e7303626 2020-05-14 stsp {
208 e7303626 2020-05-14 stsp repo="$1"
209 e7303626 2020-05-14 stsp file="$2"
210 e7303626 2020-05-14 stsp
211 e7303626 2020-05-14 stsp mkdir $repo
212 e7303626 2020-05-14 stsp git_init $repo
213 e7303626 2020-05-14 stsp echo "this is file $file" > $repo/$file
214 e7303626 2020-05-14 stsp (cd $repo && git add .)
215 e7303626 2020-05-14 stsp git_commit $repo -m "intialize $repo with file $file"
216 e7303626 2020-05-14 stsp }
217 e7303626 2020-05-14 stsp
218 f6cae3ed 2020-09-13 naddy get_loose_object_path()
219 e7303626 2020-05-14 stsp {
220 e7303626 2020-05-14 stsp local repo="$1"
221 e7303626 2020-05-14 stsp local id="$2"
222 e7303626 2020-05-14 stsp local id0=`trim_obj_id 38 $id`
223 e7303626 2020-05-14 stsp local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
224 e7303626 2020-05-14 stsp echo "$repo/.git/objects/$id0/$idrest"
225 e7303626 2020-05-14 stsp }
226 e7303626 2020-05-14 stsp
227 f6cae3ed 2020-09-13 naddy get_blob_id()
228 3ce1b845 2019-07-15 stsp {
229 3ce1b845 2019-07-15 stsp repo="$1"
230 3ce1b845 2019-07-15 stsp tree_path="$2"
231 3ce1b845 2019-07-15 stsp filename="$3"
232 3ce1b845 2019-07-15 stsp
233 e8863bdc 2020-07-23 stsp got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
234 e8863bdc 2020-07-23 stsp cut -d' ' -f 1
235 3ce1b845 2019-07-15 stsp }
236 3ce1b845 2019-07-15 stsp
237 f6cae3ed 2020-09-13 naddy test_init()
238 0e673013 2019-01-02 stsp {
239 0e673013 2019-01-02 stsp local testname="$1"
240 4a1ddfc2 2019-01-12 stsp local no_tree="$2"
241 0e673013 2019-01-02 stsp if [ -z "$testname" ]; then
242 0e673013 2019-01-02 stsp echo "No test name provided" >&2
243 0e673013 2019-01-02 stsp return 1
244 0e673013 2019-01-02 stsp fi
245 e5a14fe3 2020-10-01 stsp local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
246 0e673013 2019-01-02 stsp mkdir $testroot/repo
247 0e673013 2019-01-02 stsp git_init $testroot/repo
248 4a1ddfc2 2019-01-12 stsp if [ -z "$no_tree" ]; then
249 4a1ddfc2 2019-01-12 stsp make_test_tree $testroot/repo
250 3ce1b845 2019-07-15 stsp (cd $repo && git add .)
251 4a1ddfc2 2019-01-12 stsp git_commit $testroot/repo -m "adding the test tree"
252 4a1ddfc2 2019-01-12 stsp fi
253 c8c71e6e 2020-03-21 stsp touch $testroot/repo/.git/git-daemon-export-ok
254 0e673013 2019-01-02 stsp echo "$testroot"
255 0e673013 2019-01-02 stsp }
256 0e673013 2019-01-02 stsp
257 f6cae3ed 2020-09-13 naddy test_cleanup()
258 0e673013 2019-01-02 stsp {
259 0e673013 2019-01-02 stsp local testroot="$1"
260 fe7842f5 2019-07-14 stsp
261 f0fd0aaf 2021-09-14 stsp git_fsck $testroot $testroot/repo
262 fc414659 2022-04-16 thomas ret=$?
263 fc414659 2022-04-16 thomas if [ $ret -ne 0 ]; then
264 f0fd0aaf 2021-09-14 stsp return $ret
265 fe7842f5 2019-07-14 stsp fi
266 fe7842f5 2019-07-14 stsp
267 0e673013 2019-01-02 stsp rm -rf "$testroot"
268 0e673013 2019-01-02 stsp }
269 0e673013 2019-01-02 stsp
270 f6cae3ed 2020-09-13 naddy test_parseargs()
271 7fb414ae 2020-08-08 stsp {
272 6c8da0c6 2020-10-03 naddy while getopts qr: flag; do
273 6c8da0c6 2020-10-03 naddy case $flag in
274 6c8da0c6 2020-10-03 naddy q) export GOT_TEST_QUIET=1
275 6c8da0c6 2020-10-03 naddy ;;
276 6c8da0c6 2020-10-03 naddy r) export GOT_TEST_ROOT=$OPTARG
277 6c8da0c6 2020-10-03 naddy ;;
278 6c8da0c6 2020-10-03 naddy ?) echo "Supported options:"
279 6c8da0c6 2020-10-03 naddy echo " -q: quiet mode"
280 6c8da0c6 2020-10-03 naddy echo " -r PATH: use PATH as test data root directory"
281 6c8da0c6 2020-10-03 naddy exit 2
282 6c8da0c6 2020-10-03 naddy ;;
283 7fb414ae 2020-08-08 stsp esac
284 7fb414ae 2020-08-08 stsp done
285 6c8da0c6 2020-10-03 naddy } >&2
286 7fb414ae 2020-08-08 stsp
287 f6cae3ed 2020-09-13 naddy run_test()
288 0e673013 2019-01-02 stsp {
289 0e673013 2019-01-02 stsp testfunc="$1"
290 7fb414ae 2020-08-08 stsp if [ -z "$GOT_TEST_QUIET" ]; then
291 7fb414ae 2020-08-08 stsp echo -n "$testfunc "
292 7fb414ae 2020-08-08 stsp fi
293 0e673013 2019-01-02 stsp $testfunc
294 0e673013 2019-01-02 stsp }
295 0e673013 2019-01-02 stsp
296 f6cae3ed 2020-09-13 naddy test_done()
297 0e673013 2019-01-02 stsp {
298 0e673013 2019-01-02 stsp local testroot="$1"
299 0e673013 2019-01-02 stsp local result="$2"
300 54c39596 2020-12-28 stsp if [ "$result" = "0" ]; then
301 fe7842f5 2019-07-14 stsp test_cleanup "$testroot" || return 1
302 7fb414ae 2020-08-08 stsp if [ -z "$GOT_TEST_QUIET" ]; then
303 7fb414ae 2020-08-08 stsp echo "ok"
304 7fb414ae 2020-08-08 stsp fi
305 3941b73a 2019-05-14 stsp elif echo "$result" | grep -q "^xfail"; then
306 3941b73a 2019-05-14 stsp # expected test failure; test reproduces an unfixed bug
307 3941b73a 2019-05-14 stsp echo "$result"
308 17201126 2019-07-14 stsp test_cleanup "$testroot" || return 1
309 0e673013 2019-01-02 stsp else
310 0e673013 2019-01-02 stsp echo "test failed; leaving test data in $testroot"
311 0e673013 2019-01-02 stsp fi
312 0e673013 2019-01-02 stsp }