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
25 export MALLOC_OPTIONS=S
27 git_init()
28 {
29 git init -q "$1"
30 }
32 maybe_pack_repo()
33 {
34 local repo="$1"
35 if [ -n "$GOT_TEST_PACK" ]; then
36 (cd $repo && git repack -a -q)
37 fi
38 }
40 git_commit()
41 {
42 local repo="$1"
43 shift
44 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
45 maybe_pack_repo $repo
46 }
48 git_rm()
49 {
50 local repo="$1"
51 shift
52 (cd $repo && git rm -q "$@")
53 }
55 git_show_head()
56 {
57 local repo="$1"
58 (cd $repo && git show --no-patch --pretty='format:%H')
59 }
61 git_show_branch_head()
62 {
63 local repo="$1"
64 local branch="$2"
65 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
66 }
69 git_show_author_time()
70 {
71 local repo="$1"
72 local object="$2"
73 (cd $repo && git show --no-patch --pretty='format:%at' $object)
74 }
76 git_show_tagger_time()
77 {
78 local repo="$1"
79 local tag="$2"
80 (cd $repo && git cat-file tag $tag | grep ^tagger | \
81 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
82 }
84 git_show_parent_commit()
85 {
86 local repo="$1"
87 local commit="$2"
88 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
89 }
91 git_show_tree()
92 {
93 local repo="$1"
94 (cd $repo && git show --no-patch --pretty='format:%T')
95 }
97 trim_obj_id()
98 {
99 local trimcount=$1
100 local id=$2
102 local pat=""
103 while [ "$trimcount" -gt 0 ]; do
104 pat="[0-9a-f]$pat"
105 trimcount=$((trimcount - 1))
106 done
108 echo ${id%$pat}
111 git_commit_tree()
113 local repo="$1"
114 local msg="$2"
115 local tree="$3"
116 (cd $repo && git commit-tree -m "$msg" "$tree")
117 maybe_pack_repo $repo
120 make_test_tree()
122 repo="$1"
124 echo alpha > $repo/alpha
125 echo beta > $repo/beta
126 mkdir $repo/gamma
127 echo delta > $repo/gamma/delta
128 mkdir $repo/epsilon
129 echo zeta > $repo/epsilon/zeta
132 make_single_file_repo()
134 repo="$1"
135 file="$2"
137 mkdir $repo
138 git_init $repo
139 echo "this is file $file" > $repo/$file
140 (cd $repo && git add .)
141 git_commit $repo -m "intialize $repo with file $file"
144 get_loose_object_path()
146 local repo="$1"
147 local id="$2"
148 local id0=`trim_obj_id 38 $id`
149 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
150 echo "$repo/.git/objects/$id0/$idrest"
153 get_blob_id()
155 repo="$1"
156 tree_path="$2"
157 filename="$3"
159 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
160 cut -d' ' -f 1
163 test_init()
165 local testname="$1"
166 local no_tree="$2"
167 if [ -z "$testname" ]; then
168 echo "No test name provided" >&2
169 return 1
170 fi
171 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
172 mkdir $testroot/repo
173 git_init $testroot/repo
174 if [ -z "$no_tree" ]; then
175 make_test_tree $testroot/repo
176 (cd $repo && git add .)
177 git_commit $testroot/repo -m "adding the test tree"
178 fi
179 touch $testroot/repo/.git/git-daemon-export-ok
180 echo "$testroot"
183 test_cleanup()
185 local testroot="$1"
187 (cd $testroot/repo && git fsck --strict \
188 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
189 ret="$?"
190 if [ "$ret" != "0" ]; then
191 echo -n "git fsck: "
192 cat $testroot/fsck.stderr
193 echo "git fsck failed; leaving test data in $testroot"
194 return 1
195 fi
197 rm -rf "$testroot"
200 test_parseargs()
202 args=`getopt q $*`
203 if [ $? -ne 0 ]; then
204 echo "Supported options:"
205 echo " -q: quiet mode"
206 exit 2
207 fi
208 set -- $args
209 while [ $# -ne 0 ]; do
210 case "$1"
211 in
212 -q)
213 export GOT_TEST_QUIET=1; shift;;
214 --)
215 shift; break;;
216 esac
217 done
220 run_test()
222 testfunc="$1"
223 if [ -z "$GOT_TEST_QUIET" ]; then
224 echo -n "$testfunc "
225 fi
226 $testfunc
229 test_done()
231 local testroot="$1"
232 local result="$2"
233 if [ "$result" == "0" ]; then
234 test_cleanup "$testroot" || return 1
235 if [ -z "$GOT_TEST_QUIET" ]; then
236 echo "ok"
237 fi
238 elif echo "$result" | grep -q "^xfail"; then
239 # expected test failure; test reproduces an unfixed bug
240 echo "$result"
241 test_cleanup "$testroot" || return 1
242 else
243 echo "test failed; leaving test data in $testroot"
244 fi