Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019, 2020 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 export GIT_AUTHOR_NAME="Flan Hacker"
18 export GIT_AUTHOR_EMAIL="flan_hacker@openbsd.org"
19 export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
20 export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
21 export GOT_AUTHOR="$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>"
22 export GOT_AUTHOR_8="flan_hac"
23 export GOT_LOG_DEFAULT_LIMIT=0
24 export GOT_TEST_ROOT="/tmp"
26 export MALLOC_OPTIONS=S
28 git_init()
29 {
30 git init -q "$1"
31 }
33 maybe_pack_repo()
34 {
35 local repo="$1"
36 if [ -n "$GOT_TEST_PACK" ]; then
37 (cd $repo && git repack -a -q)
38 fi
39 }
41 git_commit()
42 {
43 local repo="$1"
44 shift
45 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
46 maybe_pack_repo $repo
47 }
49 git_rm()
50 {
51 local repo="$1"
52 shift
53 (cd $repo && git rm -q "$@")
54 }
56 git_show_head()
57 {
58 local repo="$1"
59 (cd $repo && git show --no-patch --pretty='format:%H')
60 }
62 git_show_branch_head()
63 {
64 local repo="$1"
65 local branch="$2"
66 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
67 }
70 git_show_author_time()
71 {
72 local repo="$1"
73 local object="$2"
74 (cd $repo && git show --no-patch --pretty='format:%at' $object)
75 }
77 git_show_tagger_time()
78 {
79 local repo="$1"
80 local tag="$2"
81 (cd $repo && git cat-file tag $tag | grep ^tagger | \
82 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
83 }
85 git_show_parent_commit()
86 {
87 local repo="$1"
88 local commit="$2"
89 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
90 }
92 git_show_tree()
93 {
94 local repo="$1"
95 (cd $repo && git show --no-patch --pretty='format:%T')
96 }
98 trim_obj_id()
99 {
100 local trimcount=$1
101 local id=$2
103 local pat=""
104 while [ "$trimcount" -gt 0 ]; do
105 pat="[0-9a-f]$pat"
106 trimcount=$((trimcount - 1))
107 done
109 echo ${id%$pat}
112 git_commit_tree()
114 local repo="$1"
115 local msg="$2"
116 local tree="$3"
117 (cd $repo && git commit-tree -m "$msg" "$tree")
118 maybe_pack_repo $repo
121 make_test_tree()
123 repo="$1"
125 echo alpha > $repo/alpha
126 echo beta > $repo/beta
127 mkdir $repo/gamma
128 echo delta > $repo/gamma/delta
129 mkdir $repo/epsilon
130 echo zeta > $repo/epsilon/zeta
133 make_single_file_repo()
135 repo="$1"
136 file="$2"
138 mkdir $repo
139 git_init $repo
140 echo "this is file $file" > $repo/$file
141 (cd $repo && git add .)
142 git_commit $repo -m "intialize $repo with file $file"
145 get_loose_object_path()
147 local repo="$1"
148 local id="$2"
149 local id0=`trim_obj_id 38 $id`
150 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
151 echo "$repo/.git/objects/$id0/$idrest"
154 get_blob_id()
156 repo="$1"
157 tree_path="$2"
158 filename="$3"
160 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
161 cut -d' ' -f 1
164 test_init()
166 local testname="$1"
167 local no_tree="$2"
168 if [ -z "$testname" ]; then
169 echo "No test name provided" >&2
170 return 1
171 fi
172 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
173 mkdir $testroot/repo
174 git_init $testroot/repo
175 if [ -z "$no_tree" ]; then
176 make_test_tree $testroot/repo
177 (cd $repo && git add .)
178 git_commit $testroot/repo -m "adding the test tree"
179 fi
180 touch $testroot/repo/.git/git-daemon-export-ok
181 echo "$testroot"
184 test_cleanup()
186 local testroot="$1"
188 (cd $testroot/repo && git fsck --strict \
189 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
190 ret="$?"
191 if [ "$ret" != "0" ]; then
192 echo -n "git fsck: "
193 cat $testroot/fsck.stderr
194 echo "git fsck failed; leaving test data in $testroot"
195 return 1
196 fi
198 rm -rf "$testroot"
201 test_parseargs()
203 while getopts qr: flag; do
204 case $flag in
205 q) export GOT_TEST_QUIET=1
206 ;;
207 r) export GOT_TEST_ROOT=$OPTARG
208 ;;
209 ?) echo "Supported options:"
210 echo " -q: quiet mode"
211 echo " -r PATH: use PATH as test data root directory"
212 exit 2
213 ;;
214 esac
215 done
216 } >&2
218 run_test()
220 testfunc="$1"
221 if [ -z "$GOT_TEST_QUIET" ]; then
222 echo -n "$testfunc "
223 fi
224 $testfunc
227 test_done()
229 local testroot="$1"
230 local result="$2"
231 if [ "$result" == "0" ]; then
232 test_cleanup "$testroot" || return 1
233 if [ -z "$GOT_TEST_QUIET" ]; then
234 echo "ok"
235 fi
236 elif echo "$result" | grep -q "^xfail"; then
237 # expected test failure; test reproduces an unfixed bug
238 echo "$result"
239 test_cleanup "$testroot" || return 1
240 else
241 echo "test failed; leaving test data in $testroot"
242 fi