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 local commit="$2"
88 (cd $repo && git show --no-patch --pretty='format:%P' $commit)
89 }
91 function git_show_tree
92 {
93 local repo="$1"
94 (cd $repo && git show --no-patch --pretty='format:%T')
95 }
97 function trim_obj_id
98 {
99 let trimcount=$1
100 id=$2
102 pat=""
103 while [ trimcount -gt 0 ]; do
104 pat="[0-9a-f]$pat"
105 let trimcount--
106 done
108 echo ${id%$pat}
111 function 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 function 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 function 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 function 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 function get_blob_id
155 repo="$1"
156 tree_path="$2"
157 filename="$3"
159 got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
162 function test_init
164 local testname="$1"
165 local no_tree="$2"
166 if [ -z "$testname" ]; then
167 echo "No test name provided" >&2
168 return 1
169 fi
170 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
171 mkdir $testroot/repo
172 git_init $testroot/repo
173 if [ -z "$no_tree" ]; then
174 make_test_tree $testroot/repo
175 (cd $repo && git add .)
176 git_commit $testroot/repo -m "adding the test tree"
177 fi
178 touch $testroot/repo/.git/git-daemon-export-ok
179 echo "$testroot"
182 function test_cleanup
184 local testroot="$1"
186 (cd $testroot/repo && git fsck --strict \
187 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
188 ret="$?"
189 if [ "$ret" != "0" ]; then
190 echo -n "git fsck: "
191 cat $testroot/fsck.stderr
192 echo "git fsck failed; leaving test data in $testroot"
193 return 1
194 fi
196 rm -rf "$testroot"
199 function run_test
201 testfunc="$1"
202 echo -n "$testfunc "
203 $testfunc
206 function test_done
208 local testroot="$1"
209 local result="$2"
210 if [ "$result" == "0" ]; then
211 test_cleanup "$testroot" || return 1
212 echo "ok"
213 elif echo "$result" | grep -q "^xfail"; then
214 # expected test failure; test reproduces an unfixed bug
215 echo "$result"
216 test_cleanup "$testroot" || return 1
217 else
218 echo "test failed; leaving test data in $testroot"
219 fi