Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2023 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 . ../cmdline/common.sh
18 . ./common.sh
20 test_create_protected_branch() {
21 local testroot=`test_init create_protected_branch 1`
23 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
24 ret=$?
25 if [ $ret -ne 0 ]; then
26 echo "got clone failed unexpectedly" >&2
27 test_done "$testroot" 1
28 return 1
29 fi
31 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
32 ret=$?
33 if [ $ret -ne 0 ]; then
34 echo "got checkout failed unexpectedly" >&2
35 test_done "$testroot" 1
36 return 1
37 fi
39 (cd $testroot/wt && got branch foo) >/dev/null
40 ret=$?
41 if [ $ret -ne 0 ]; then
42 echo "got branch failed unexpectedly" >&2
43 test_done "$testroot" 1
44 return 1
45 fi
47 echo modified alpha > $testroot/wt/alpha
48 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
49 ret=$?
50 if [ $ret -ne 0 ]; then
51 echo "got commit failed unexpectedly" >&2
52 test_done "$testroot" 1
53 return 1
54 fi
55 local commit_id=`git_show_branch_head $testroot/repo-clone foo`
57 # Creating a new branch should succeed.
58 got send -q -r $testroot/repo-clone -b foo 2> $testroot/stderr
59 ret=$?
60 if [ $ret -ne 0 ]; then
61 echo "got send failed unexpectedly" >&2
62 test_done "$testroot" 1
63 return 1
64 fi
66 # Verify that the send operation worked fine.
67 got clone -l ${GOTD_TEST_REPO_URL} | grep foo > $testroot/stdout
68 ret=$?
69 if [ $ret -ne 0 ]; then
70 echo "got clone -l failed unexpectedly" >&2
71 test_done "$testroot" "1"
72 return 1
73 fi
75 echo "refs/heads/foo: $commit_id" > $testroot/stdout.expected
76 cmp -s $testroot/stdout.expected $testroot/stdout
77 ret=$?
78 if [ $ret -ne 0 ]; then
79 diff -u $testroot/stdout.expected $testroot/stdout
80 fi
82 test_done "$testroot" $ret
83 }
85 test_modify_protected_tag_namespace() {
86 local testroot=`test_init modify_protected_tag_namespace`
88 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
89 ret=$?
90 if [ $ret -ne 0 ]; then
91 echo "got clone failed unexpectedly" >&2
92 test_done "$testroot" 1
93 return 1
94 fi
96 got tag -r $testroot/repo-clone -m "1.0" 1.0 >/dev/null
97 ret=$?
98 if [ $ret -ne 0 ]; then
99 echo "got tag failed unexpectedly" >&2
100 test_done "$testroot" 1
101 return 1
102 fi
104 # Creating a new tag should succeed.
105 got send -q -r $testroot/repo-clone -t 1.0 2> $testroot/stderr
106 ret=$?
107 if [ $ret -ne 0 ]; then
108 echo "got send failed unexpectedly" >&2
109 test_done "$testroot" 1
110 return 1
111 fi
113 got ref -r $testroot/repo-clone -d refs/tags/1.0 > /dev/null
114 got tag -r $testroot/repo-clone -m "another 1.0" 1.0 >/dev/null
115 ret=$?
116 if [ $ret -ne 0 ]; then
117 echo "got tag failed unexpectedly" >&2
118 test_done "$testroot" 1
119 return 1
120 fi
122 # Overwriting an existing tag should fail.
123 got send -q -f -r $testroot/repo-clone -t 1.0 2> $testroot/stderr
124 ret=$?
125 if [ $ret == 0 ]; then
126 echo "got send succeeded unexpectedly" >&2
127 test_done "$testroot" 1
128 return 1
129 fi
131 if ! egrep -q '(gotsh|got-send-pack): refs/tags/: reference namespace is protected' \
132 $testroot/stderr; then
133 echo -n "error message unexpected or missing: " >&2
134 cat $testroot/stderr >&2
135 test_done "$testroot" 1
136 return 1
137 fi
139 # Deleting an existing tag should fail.
140 # 'got send' cannot even do this so we use 'git push'.
141 (cd $testroot/repo-clone && git push -q -d origin refs/tags/1.0 \
142 2> $testroot/stderr)
143 ret=$?
144 if [ $ret -eq 0 ]; then
145 echo "git push -d succeeded unexpectedly" >&2
146 test_done "$testroot" 1
147 return 1
148 fi
150 if ! egrep -q '(fatal: remote error|gotsh): refs/tags/: reference namespace is protected' \
151 $testroot/stderr; then
152 echo -n "error message unexpected or missing: " >&2
153 cat $testroot/stderr >&2
154 test_done "$testroot" 1
155 return 1
156 fi
158 test_done "$testroot" 0
161 test_delete_protected_branch() {
162 local testroot=`test_init delete_protected_branch`
164 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
165 ret=$?
166 if [ $ret -ne 0 ]; then
167 echo "got clone failed unexpectedly" >&2
168 test_done "$testroot" 1
169 return 1
170 fi
172 if got send -q -r $testroot/repo-clone -d main 2> $testroot/stderr; then
173 echo "got send succeeded unexpectedly" >&2
174 test_done "$testroot" 1
175 return 1
176 fi
178 if ! egrep -q '(gotsh|got-send-pack): refs/heads/main: reference is protected' \
179 $testroot/stderr; then
180 echo -n "error message unexpected or missing: " >&2
181 cat $testroot/stderr >&2
182 test_done "$testroot" 1
183 return 1
184 fi
186 test_done "$testroot" 0
189 test_modify_protected_branch() {
190 local testroot=`test_init modify_protected_branch`
192 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
193 ret=$?
194 if [ $ret -ne 0 ]; then
195 echo "got clone failed unexpectedly" >&2
196 test_done "$testroot" 1
197 return 1
198 fi
200 got checkout $testroot/repo-clone $testroot/wt >/dev/null
202 for i in 1 2 3; do
203 echo "more alpha" >> $testroot/wt/alpha
204 (cd $testroot/wt && got commit -m "more" >/dev/null)
205 done
206 local commit_id=`git_show_head $testroot/repo-clone`
207 local parent_commit_id=`git_show_parent_commit $testroot/repo-clone \
208 "$commit_id"`
210 # Modifying the branch by adding new commits on top should succeed.
211 got send -q -r $testroot/repo-clone 2> $testroot/stderr
212 ret=$?
213 if [ $ret -ne 0 ]; then
214 echo "got send failed unexpectedly" >&2
215 test_done "$testroot" 1
216 return 1
217 fi
219 # Verify that the send operation worked fine.
220 got clone -l ${GOTD_TEST_REPO_URL} | grep main > $testroot/stdout
221 ret=$?
222 if [ $ret -ne 0 ]; then
223 echo "got clone -l failed unexpectedly" >&2
224 test_done "$testroot" "1"
225 return 1
226 fi
228 echo "HEAD: refs/heads/main" > $testroot/stdout.expected
229 echo "refs/heads/main: $commit_id" >> $testroot/stdout.expected
230 cmp -s $testroot/stdout.expected $testroot/stdout
231 ret=$?
232 if [ $ret -ne 0 ]; then
233 diff -u $testroot/stdout.expected $testroot/stdout
234 test_done "$testroot" $ret
235 return 1
236 fi
238 # Attempt to remove the tip commit
239 (cd $testroot/wt && got update -c "$parent_commit_id" >/dev/null)
240 (cd $testroot/wt && got histedit -d >/dev/null)
241 ret=$?
242 if [ $ret -ne 0 ]; then
243 echo "got histedit failed unexpectedly" >&2
244 test_done "$testroot" 1
245 return 1
246 fi
248 # The client should reject sending without -f.
249 got send -q -r $testroot/repo-clone 2> $testroot/stderr
250 ret=$?
251 if [ $ret -eq 0 ]; then
252 echo "got send succeeded unexpectedly" >&2
253 test_done "$testroot" 1
254 return 1
255 fi
257 echo 'got: refs/heads/main: fetch and rebase required' \
258 >> $testroot/stderr.expected
259 if ! cmp -s $testroot/stderr.expected $testroot/stderr; then
260 diff -u $testroot/stderr.expected $testroot/stderr
261 test_done "$testroot" 1
262 return 1
263 fi
265 # Try again with -f.
266 got send -q -r $testroot/repo-clone -f 2> $testroot/stderr
267 ret=$?
268 if [ $ret -eq 0 ]; then
269 echo "got send succeeded unexpectedly" >&2
270 test_done "$testroot" 1
271 return 1
272 fi
274 if ! egrep -q '(gotsh|got-send-pack): refs/heads/main: reference is protected' \
275 $testroot/stderr; then
276 echo -n "error message unexpected or missing: " >&2
277 cat $testroot/stderr >&2
278 test_done "$testroot" 1
279 return 1
280 fi
282 # Verify that the send -f operation did not have any effect.
283 got clone -l ${GOTD_TEST_REPO_URL} | grep main > $testroot/stdout
284 ret=$?
285 if [ $ret -ne 0 ]; then
286 echo "got clone -l failed unexpectedly" >&2
287 test_done "$testroot" "1"
288 return 1
289 fi
291 echo "HEAD: refs/heads/main" > $testroot/stdout.expected
292 echo "refs/heads/main: $commit_id" >> $testroot/stdout.expected
293 cmp -s $testroot/stdout.expected $testroot/stdout
294 ret=$?
295 if [ $ret -ne 0 ]; then
296 diff -u $testroot/stdout.expected $testroot/stdout
297 fi
299 test_done "$testroot" $ret
302 test_parseargs "$@"
303 run_test test_create_protected_branch
304 run_test test_modify_protected_tag_namespace
305 run_test test_delete_protected_branch
306 run_test test_modify_protected_branch