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_AUTHOR_11="flan_hacker"
24 export GOT_LOG_DEFAULT_LIMIT=0
25 export GOT_TEST_ROOT="/tmp"
27 export MALLOC_OPTIONS=S
29 git_init()
30 {
31 git init -q "$1"
32 }
34 maybe_pack_repo()
35 {
36 local repo="$1"
37 if [ -n "$GOT_TEST_PACK" ]; then
38 (cd $repo && git repack -a -q)
39 fi
40 }
42 git_commit()
43 {
44 local repo="$1"
45 shift
46 (cd $repo && git commit --author="$GOT_AUTHOR" -q -a "$@")
47 maybe_pack_repo $repo
48 }
50 git_rm()
51 {
52 local repo="$1"
53 shift
54 (cd $repo && git rm -q "$@")
55 }
57 git_show_head()
58 {
59 local repo="$1"
60 (cd $repo && git show --no-patch --pretty='format:%H')
61 }
63 git_show_branch_head()
64 {
65 local repo="$1"
66 local branch="$2"
67 (cd $repo && git show --no-patch --pretty='format:%H' $branch)
68 }
71 git_show_author_time()
72 {
73 local repo="$1"
74 local object="$2"
75 (cd $repo && git show --no-patch --pretty='format:%at' $object)
76 }
78 git_show_tagger_time()
79 {
80 local repo="$1"
81 local tag="$2"
82 (cd $repo && git cat-file tag $tag | grep ^tagger | \
83 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
84 }
86 git_show_parent_commit()
87 {
88 local repo="$1"
89 local commit="$2"
90 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
91 }
93 git_show_tree()
94 {
95 local repo="$1"
96 (cd $repo && git show --no-patch --pretty='format:%T')
97 }
99 trim_obj_id()
101 local trimcount=$1
102 local id=$2
104 local pat=""
105 while [ "$trimcount" -gt 0 ]; do
106 pat="[0-9a-f]$pat"
107 trimcount=$((trimcount - 1))
108 done
110 echo ${id%$pat}
113 git_commit_tree()
115 local repo="$1"
116 local msg="$2"
117 local tree="$3"
118 (cd $repo && git commit-tree -m "$msg" "$tree")
119 maybe_pack_repo $repo
122 make_test_tree()
124 repo="$1"
126 echo alpha > $repo/alpha
127 echo beta > $repo/beta
128 mkdir $repo/gamma
129 echo delta > $repo/gamma/delta
130 mkdir $repo/epsilon
131 echo zeta > $repo/epsilon/zeta
134 make_single_file_repo()
136 repo="$1"
137 file="$2"
139 mkdir $repo
140 git_init $repo
141 echo "this is file $file" > $repo/$file
142 (cd $repo && git add .)
143 git_commit $repo -m "intialize $repo with file $file"
146 get_loose_object_path()
148 local repo="$1"
149 local id="$2"
150 local id0=`trim_obj_id 38 $id`
151 local idrest=`echo ${id#[0-9a-f][0-9a-f]}`
152 echo "$repo/.git/objects/$id0/$idrest"
155 get_blob_id()
157 repo="$1"
158 tree_path="$2"
159 filename="$3"
161 got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
162 cut -d' ' -f 1
165 test_init()
167 local testname="$1"
168 local no_tree="$2"
169 if [ -z "$testname" ]; then
170 echo "No test name provided" >&2
171 return 1
172 fi
173 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
174 mkdir $testroot/repo
175 git_init $testroot/repo
176 if [ -z "$no_tree" ]; then
177 make_test_tree $testroot/repo
178 (cd $repo && git add .)
179 git_commit $testroot/repo -m "adding the test tree"
180 fi
181 touch $testroot/repo/.git/git-daemon-export-ok
182 echo "$testroot"
185 test_cleanup()
187 local testroot="$1"
189 (cd $testroot/repo && git fsck --strict \
190 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
191 ret="$?"
192 if [ "$ret" != "0" ]; then
193 echo -n "git fsck: "
194 cat $testroot/fsck.stderr
195 echo "git fsck failed; leaving test data in $testroot"
196 return 1
197 fi
199 rm -rf "$testroot"
202 test_parseargs()
204 while getopts qr: flag; do
205 case $flag in
206 q) export GOT_TEST_QUIET=1
207 ;;
208 r) export GOT_TEST_ROOT=$OPTARG
209 ;;
210 ?) echo "Supported options:"
211 echo " -q: quiet mode"
212 echo " -r PATH: use PATH as test data root directory"
213 exit 2
214 ;;
215 esac
216 done
217 } >&2
219 run_test()
221 testfunc="$1"
222 if [ -z "$GOT_TEST_QUIET" ]; then
223 echo -n "$testfunc "
224 fi
225 $testfunc
228 test_done()
230 local testroot="$1"
231 local result="$2"
232 if [ "$result" = "0" ]; then
233 test_cleanup "$testroot" || return 1
234 if [ -z "$GOT_TEST_QUIET" ]; then
235 echo "ok"
236 fi
237 elif echo "$result" | grep -q "^xfail"; then
238 # expected test failure; test reproduces an unfixed bug
239 echo "$result"
240 test_cleanup "$testroot" || return 1
241 else
242 echo "test failed; leaving test data in $testroot"
243 fi