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_author_time
62 {
63 local repo="$1"
64 local object="$2"
65 (cd $repo && git show --no-patch --pretty='format:%at' $object)
66 }
68 function git_show_tagger_time
69 {
70 local repo="$1"
71 local tag="$2"
72 (cd $repo && git cat-file tag $tag | grep ^tagger | \
73 sed -e "s/^tagger $GOT_AUTHOR//" | cut -d' ' -f2)
74 }
76 function git_show_parent_commit
77 {
78 local repo="$1"
79 (cd $repo && git show --no-patch --pretty='format:%P')
80 }
82 function git_show_tree
83 {
84 local repo="$1"
85 (cd $repo && git show --no-patch --pretty='format:%T')
86 }
88 function trim_obj_id
89 {
90 let trimcount=$1
91 id=$2
93 pat=""
94 while [ trimcount -gt 0 ]; do
95 pat="[0-9a-f]$pat"
96 let trimcount--
97 done
99 echo ${id%$pat}
102 function git_commit_tree
104 local repo="$1"
105 local msg="$2"
106 local tree="$3"
107 (cd $repo && git commit-tree -m "$msg" "$tree")
108 maybe_pack_repo $repo
111 function make_test_tree
113 repo="$1"
115 echo alpha > $repo/alpha
116 echo beta > $repo/beta
117 mkdir $repo/gamma
118 echo delta > $repo/gamma/delta
119 mkdir $repo/epsilon
120 echo zeta > $repo/epsilon/zeta
123 function get_blob_id
125 repo="$1"
126 tree_path="$2"
127 filename="$3"
129 got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
132 function test_init
134 local testname="$1"
135 local no_tree="$2"
136 if [ -z "$testname" ]; then
137 echo "No test name provided" >&2
138 return 1
139 fi
140 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
141 mkdir $testroot/repo
142 git_init $testroot/repo
143 if [ -z "$no_tree" ]; then
144 make_test_tree $testroot/repo
145 (cd $repo && git add .)
146 git_commit $testroot/repo -m "adding the test tree"
147 fi
148 echo "$testroot"
151 function test_cleanup
153 local testroot="$1"
155 (cd $testroot/repo && git fsck --strict \
156 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
157 ret="$?"
158 if [ "$ret" != "0" ]; then
159 echo -n "git fsck: "
160 cat $testroot/fsck.stderr
161 echo "git fsck failed; leaving test data in $testroot"
162 return 1
163 fi
165 rm -rf "$testroot"
168 function run_test
170 testfunc="$1"
171 echo -n "$testfunc "
172 $testfunc
175 function test_done
177 local testroot="$1"
178 local result="$2"
179 if [ "$result" == "0" ]; then
180 test_cleanup "$testroot" || return 1
181 echo "ok"
182 elif echo "$result" | grep -q "^xfail"; then
183 # expected test failure; test reproduces an unfixed bug
184 echo "$result"
185 test_cleanup "$testroot" || return 1
186 else
187 echo "test failed; leaving test data in $testroot"
188 fi