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 function git_init
28 {
29 git init -q "$1"
30 }
32 function 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 function 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 function git_rm
49 {
50 local repo="$1"
51 shift
52 (cd $repo && git rm -q "$@")
53 }
55 function git_show_head
56 {
57 local repo="$1"
58 (cd $repo && git show --no-patch --pretty='format:%H')
59 }
61 function 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 function 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 function 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 function git_show_parent_commit
85 {
86 local repo="$1"
87 (cd $repo && git show --no-patch --pretty='format:%P')
88 }
90 function git_show_tree
91 {
92 local repo="$1"
93 (cd $repo && git show --no-patch --pretty='format:%T')
94 }
96 function trim_obj_id
97 {
98 let trimcount=$1
99 id=$2
101 pat=""
102 while [ trimcount -gt 0 ]; do
103 pat="[0-9a-f]$pat"
104 let trimcount--
105 done
107 echo ${id%$pat}
110 function git_commit_tree
112 local repo="$1"
113 local msg="$2"
114 local tree="$3"
115 (cd $repo && git commit-tree -m "$msg" "$tree")
116 maybe_pack_repo $repo
119 function make_test_tree
121 repo="$1"
123 echo alpha > $repo/alpha
124 echo beta > $repo/beta
125 mkdir $repo/gamma
126 echo delta > $repo/gamma/delta
127 mkdir $repo/epsilon
128 echo zeta > $repo/epsilon/zeta
131 function get_blob_id
133 repo="$1"
134 tree_path="$2"
135 filename="$3"
137 got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
140 function test_init
142 local testname="$1"
143 local no_tree="$2"
144 if [ -z "$testname" ]; then
145 echo "No test name provided" >&2
146 return 1
147 fi
148 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
149 mkdir $testroot/repo
150 git_init $testroot/repo
151 if [ -z "$no_tree" ]; then
152 make_test_tree $testroot/repo
153 (cd $repo && git add .)
154 git_commit $testroot/repo -m "adding the test tree"
155 fi
156 echo "$testroot"
159 function test_cleanup
161 local testroot="$1"
163 (cd $testroot/repo && git fsck --strict \
164 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
165 ret="$?"
166 if [ "$ret" != "0" ]; then
167 echo -n "git fsck: "
168 cat $testroot/fsck.stderr
169 echo "git fsck failed; leaving test data in $testroot"
170 return 1
171 fi
173 rm -rf "$testroot"
176 function run_test
178 testfunc="$1"
179 echo -n "$testfunc "
180 $testfunc
183 function test_done
185 local testroot="$1"
186 local result="$2"
187 if [ "$result" == "0" ]; then
188 test_cleanup "$testroot" || return 1
189 echo "ok"
190 elif echo "$result" | grep -q "^xfail"; then
191 # expected test failure; test reproduces an unfixed bug
192 echo "$result"
193 test_cleanup "$testroot" || return 1
194 else
195 echo "test failed; leaving test data in $testroot"
196 fi