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 . ./common.sh
19 function test_ref_create {
20 local testroot=`test_init ref_create`
21 local commit_id=`git_show_head $testroot/repo`
23 # Create a ref based on a commit ID
24 got ref -r $testroot/repo refs/heads/commitref $commit_id
25 ret="$?"
26 if [ "$ret" != "0" ]; then
27 echo "got ref command failed unexpectedly"
28 test_done "$testroot" "$ret"
29 return 1
30 fi
32 # Create a ref based on repository's HEAD reference
33 got ref -r $testroot/repo refs/heads/newref HEAD
34 ret="$?"
35 if [ "$ret" != "0" ]; then
36 echo "got ref command failed unexpectedly"
37 test_done "$testroot" "$ret"
38 return 1
39 fi
41 # Ensure that Git recognizes the ref Got has created
42 (cd $testroot/repo && git checkout -q newref)
43 ret="$?"
44 if [ "$ret" != "0" ]; then
45 echo "git checkout command failed unexpectedly"
46 test_done "$testroot" "$ret"
47 return 1
48 fi
50 # Ensure Got recognizes the new ref
51 got checkout -b newref $testroot/repo $testroot/wt >/dev/null
52 ret="$?"
53 if [ "$ret" != "0" ]; then
54 echo "got checkout command failed unexpectedly"
55 test_done "$testroot" "$ret"
56 return 1
57 fi
59 # Create a head ref based on another specific ref
60 (cd $testroot/wt && got ref refs/heads/anotherref refs/heads/master)
61 ret="$?"
62 if [ "$ret" != "0" ]; then
63 test_done "$testroot" "$ret"
64 return 1
65 fi
67 (cd $testroot/repo && git checkout -q anotherref)
68 ret="$?"
69 if [ "$ret" != "0" ]; then
70 echo "git checkout command failed unexpectedly"
71 test_done "$testroot" "$ret"
72 fi
74 # Create a symbolic ref
75 (cd $testroot/wt && got ref -s refs/heads/symbolicref refs/heads/master)
76 ret="$?"
77 if [ "$ret" != "0" ]; then
78 test_done "$testroot" "$ret"
79 return 1
80 fi
82 (cd $testroot/repo && git checkout -q symbolicref)
83 ret="$?"
84 if [ "$ret" != "0" ]; then
85 echo "git checkout command failed unexpectedly"
86 test_done "$testroot" "$ret"
87 return 1
88 fi
90 # Attempt to create a symbolic ref pointing at a non-reference
91 (cd $testroot/wt && got ref -s refs/heads/symbolicref $commit_id \
92 2> $testroot/stderr)
93 ret="$?"
94 if [ "$ret" == "0" ]; then
95 echo "git ref command succeeded unexpectedly"
96 test_done "$testroot" "1"
97 return 1
98 fi
100 echo "got: reference $commit_id not found" > $testroot/stderr.expected
101 cmp -s $testroot/stderr $testroot/stderr.expected
102 ret="$?"
103 if [ "$ret" != "0" ]; then
104 diff -u $testroot/stderr.expected $testroot/stderr
105 test_done "$testroot" "$ret"
106 return 1
107 fi
109 # Change HEAD
110 got ref -r $testroot/repo -s HEAD refs/heads/newref
111 ret="$?"
112 if [ "$ret" != "0" ]; then
113 echo "got ref command failed unexpectedly"
114 test_done "$testroot" "$ret"
115 return 1
116 fi
118 # Ensure that Git recognizes the ref Got has created
119 (cd $testroot/repo && git checkout -q HEAD)
120 ret="$?"
121 if [ "$ret" != "0" ]; then
122 echo "git checkout command failed unexpectedly"
123 test_done "$testroot" "$ret"
124 return 1
125 fi
127 # Ensure Got recognizes the new ref
128 (cd $testroot/wt && got update -b HEAD >/dev/null)
129 ret="$?"
130 if [ "$ret" != "0" ]; then
131 echo "got checkout command failed unexpectedly"
132 test_done "$testroot" "$ret"
133 return 1
134 fi
135 got ref -r $testroot/repo -l > $testroot/stdout
136 echo "HEAD: refs/heads/newref" > $testroot/stdout.expected
137 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
138 cat $testroot/wt/.got/uuid | tr -d '\n' >> $testroot/stdout.expected
139 echo ": $commit_id" >> $testroot/stdout.expected
140 echo "refs/heads/anotherref: $commit_id" >> $testroot/stdout.expected
141 echo "refs/heads/commitref: $commit_id" >> $testroot/stdout.expected
142 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
143 echo "refs/heads/newref: $commit_id" >> $testroot/stdout.expected
144 echo "refs/heads/symbolicref: refs/heads/master" \
145 >> $testroot/stdout.expected
146 cmp -s $testroot/stdout $testroot/stdout.expected
147 ret="$?"
148 if [ "$ret" != "0" ]; then
149 diff -u $testroot/stdout.expected $testroot/stdout
150 fi
151 test_done "$testroot" "$ret"
154 function test_ref_delete {
155 local testroot=`test_init ref_delete`
156 local commit_id=`git_show_head $testroot/repo`
158 for b in ref1 ref2 ref3; do
159 got ref -r $testroot/repo refs/heads/$b refs/heads/master
160 ret="$?"
161 if [ "$ret" != "0" ]; then
162 echo "got ref command failed unexpectedly"
163 test_done "$testroot" "$ret"
164 return 1
165 fi
166 done
168 got ref -d refs/heads/ref2 -r $testroot/repo > $testroot/stdout
169 ret="$?"
170 if [ "$ret" != "0" ]; then
171 echo "got ref command failed unexpectedly"
172 test_done "$testroot" "$ret"
173 return 1
174 fi
176 got ref -l -r $testroot/repo > $testroot/stdout
177 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
178 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
179 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
180 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
181 cmp -s $testroot/stdout $testroot/stdout.expected
182 ret="$?"
183 if [ "$ret" != "0" ]; then
184 diff -u $testroot/stdout.expected $testroot/stdout
185 test_done "$testroot" "$ret"
186 return 1
187 fi
189 got ref -d refs/heads/bogus_ref_name -r $testroot/repo \
190 > $testroot/stdout 2> $testroot/stderr
191 ret="$?"
192 if [ "$ret" == "0" ]; then
193 echo "got ref succeeded unexpectedly"
194 test_done "$testroot" "$ret"
195 return 1
196 fi
198 echo "got: reference refs/heads/bogus_ref_name not found" \
199 > $testroot/stderr.expected
200 cmp -s $testroot/stderr $testroot/stderr.expected
201 ret="$?"
202 if [ "$ret" != "0" ]; then
203 diff -u $testroot/stderr.expected $testroot/stderr
204 fi
205 test_done "$testroot" "$ret"
208 run_test test_ref_create
209 run_test test_ref_delete