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>"
19 export MALLOC_OPTIONS=S
21 function git_init
22 {
23 git init -q "$@"
24 }
26 function git_commit
27 {
28 local repo="$1"
29 shift
30 (cd $repo && git commit -q -a "$@")
31 }
33 function git_rm
34 {
35 local repo="$1"
36 shift
37 (cd $repo && git rm -q "$@")
38 }
40 function git_show_head
41 {
42 local repo="$1"
43 (cd $repo && git show --no-patch --pretty='format:%H')
44 }
46 function git_show_parent_commit
47 {
48 local repo="$1"
49 (cd $repo && git show --no-patch --pretty='format:%P')
50 }
52 function git_show_tree
53 {
54 local repo="$1"
55 (cd $repo && git show --no-patch --pretty='format:%T')
56 }
58 function trim_obj_id
59 {
60 let trimcount=$1
61 id=$2
63 pat=""
64 while [ trimcount -gt 0 ]; do
65 pat="[0-9a-f]$pat"
66 let trimcount--
67 done
69 echo ${id%$pat}
70 }
72 function git_commit_tree
73 {
74 local repo="$1"
75 local msg="$2"
76 local tree="$3"
77 (cd $repo && git commit-tree -m "$msg" "$tree")
78 }
80 function make_test_tree
81 {
82 repo="$1"
84 echo alpha > $repo/alpha
85 echo beta > $repo/beta
86 mkdir $repo/gamma
87 echo delta > $repo/gamma/delta
88 mkdir $repo/epsilon
89 echo zeta > $repo/epsilon/zeta
90 }
92 function get_blob_id
93 {
94 repo="$1"
95 tree_path="$2"
96 filename="$3"
98 got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
99 }
101 function test_init
103 local testname="$1"
104 local no_tree="$2"
105 if [ -z "$testname" ]; then
106 echo "No test name provided" >&2
107 return 1
108 fi
109 local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
110 mkdir $testroot/repo
111 git_init $testroot/repo
112 if [ -z "$no_tree" ]; then
113 make_test_tree $testroot/repo
114 (cd $repo && git add .)
115 git_commit $testroot/repo -m "adding the test tree"
116 fi
117 echo "$testroot"
120 function test_cleanup
122 local testroot="$1"
124 (cd $testroot/repo && git fsck --strict \
125 > $testroot/fsck.stdout 2> $testroot/fsck.stderr)
126 ret="$?"
127 if [ "$ret" != "0" ]; then
128 echo -n "git fsck: "
129 cat $testroot/fsck.stderr
130 echo "git fsck failed; leaving test data in $testroot"
131 return 1
132 fi
134 rm -rf "$testroot"
137 function run_test
139 testfunc="$1"
140 echo -n "$testfunc "
141 $testfunc
144 function test_done
146 local testroot="$1"
147 local result="$2"
148 if [ "$result" == "0" ]; then
149 test_cleanup "$testroot" || return 1
150 echo "ok"
151 elif echo "$result" | grep -q "^xfail"; then
152 # expected test failure; test reproduces an unfixed bug
153 echo "$result"
154 test_cleanup "$testroot" || return 1
155 else
156 echo "test failed; leaving test data in $testroot"
157 fi