Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 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 GOT_AUTHOR="Flan Hacker <flan_hacker@openbsd.org>"
18 export GOT_AUTHOR_8="flan_hac"
19 export GOT_LOG_DEFAULT_LIMIT=0
21 export MALLOC_OPTIONS=S
23 function git_init
24 {
25 git init -q "$@"
26 }
28 function git_commit
29 {
30 local repo="$1"
31 shift
32 (cd $repo && git commit -q -a "$@")
33 }
35 function git_rm
36 {
37 local repo="$1"
38 shift
39 (cd $repo && git rm -q "$@")
40 }
42 function git_show_head
43 {
44 local repo="$1"
45 (cd $repo && git show --no-patch --pretty='format:%H')
46 }
48 function git_show_author_time
49 {
50 local repo="$1"
51 (cd $repo && git show --no-patch --pretty='format:%at')
52 }
54 function git_show_parent_commit
55 {
56 local repo="$1"
57 (cd $repo && git show --no-patch --pretty='format:%P')
58 }
60 function git_show_tree
61 {
62 local repo="$1"
63 (cd $repo && git show --no-patch --pretty='format:%T')
64 }
66 function trim_obj_id
67 {
68 let trimcount=$1
69 id=$2
71 pat=""
72 while [ trimcount -gt 0 ]; do
73 pat="[0-9a-f]$pat"
74 let trimcount--
75 done
77 echo ${id%$pat}
78 }
80 function git_commit_tree
81 {
82 local repo="$1"
83 local msg="$2"
84 local tree="$3"
85 (cd $repo && git commit-tree -m "$msg" "$tree")
86 }
88 function make_test_tree
89 {
90 repo="$1"
92 echo alpha > $repo/alpha
93 echo beta > $repo/beta
94 mkdir $repo/gamma
95 echo delta > $repo/gamma/delta
96 mkdir $repo/epsilon
97 echo zeta > $repo/epsilon/zeta
98 }
100 function get_blob_id
102 repo="$1"
103 tree_path="$2"
104 filename="$3"
106 got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
109 function test_init
111 local testname="$1"
112 local no_tree="$2"
113 if [ -z "$testname" ]; then
114 echo "No test name provided" >&2
115 return 1
116 fi
117 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
118 mkdir $testroot/repo
119 git_init $testroot/repo
120 if [ -z "$no_tree" ]; then
121 make_test_tree $testroot/repo
122 (cd $repo && git add .)
123 git_commit $testroot/repo -m "adding the test tree"
124 fi
125 echo "$testroot"
128 function test_cleanup
130 local testroot="$1"
132 (cd $testroot/repo && git fsck --strict \
133 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
134 ret="$?"
135 if [ "$ret" != "0" ]; then
136 echo -n "git fsck: "
137 cat $testroot/fsck.stderr
138 echo "git fsck failed; leaving test data in $testroot"
139 return 1
140 fi
142 rm -rf "$testroot"
145 function run_test
147 testfunc="$1"
148 echo -n "$testfunc "
149 $testfunc
152 function test_done
154 local testroot="$1"
155 local result="$2"
156 if [ "$result" == "0" ]; then
157 test_cleanup "$testroot" || return 1
158 echo "ok"
159 elif echo "$result" | grep -q "^xfail"; then
160 # expected test failure; test reproduces an unfixed bug
161 echo "$result"
162 test_cleanup "$testroot" || return 1
163 else
164 echo "test failed; leaving test data in $testroot"
165 fi