Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2022 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_send_basic() {
21 local testroot=`test_init send_basic 1`
23 ls -R ${GOTD_TEST_REPO}/objects/pack > $testroot/repo-list.before
25 got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
26 ret=$?
27 if [ $ret -ne 0 ]; then
28 echo "got clone failed unexpectedly" >&2
29 test_done "$testroot" "1"
30 return 1
31 fi
33 # create a second clone to test an incremental fetch with later
34 got clone -q -m ${GOTD_TEST_REPO_URL} $testroot/repo-clone2
35 ret=$?
36 if [ $ret -ne 0 ]; then
37 echo "got clone failed unexpectedly" >&2
38 test_done "$testroot" "1"
39 return 1
40 fi
41 # same for Git
42 git clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone3 \
43 >$testroot/stdout 2>$testroot/stderr
44 ret=$?
45 if [ $ret -ne 0 ]; then
46 echo "git clone failed unexpectedly" >&2
47 test_done "$testroot" "1"
48 return 1
49 fi
51 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
52 ret=$?
53 if [ $ret -ne 0 ]; then
54 echo "got checkout failed unexpectedly" >&2
55 test_done "$testroot" "1"
56 return 1
57 fi
59 mkdir $testroot/wt/psi
60 echo "new" > $testroot/wt/psi/new
61 (cd $testroot/wt && got add psi/new > /dev/null)
62 echo "more alpha" >> $testroot/wt/alpha
63 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
64 (cd $testroot/wt && got branch newbranch >/dev/null)
65 echo "even more alpha" >> $testroot/wt/alpha
66 (cd $testroot/wt && got commit -m 'more changes' > /dev/null)
67 got tag -r $testroot/repo-clone -m "tagging 1.0" 1.0 >/dev/null
69 got send -b main -b newbranch -q -r $testroot/repo-clone -t 1.0
70 ret=$?
71 if [ $ret -ne 0 ]; then
72 echo "got send failed unexpectedly" >&2
73 test_done "$testroot" "1"
74 return 1
75 fi
77 # Verify that the send operation worked fine.
78 got fetch -q -r $testroot/repo-clone2
79 ret=$?
80 if [ $ret -ne 0 ]; then
81 echo "got fetch failed unexpectedly" >&2
82 test_done "$testroot" "1"
83 return 1
84 fi
86 got tree -R -r $testroot/repo-clone2 > $testroot/stdout
87 cat > $testroot/stdout.expected <<EOF
88 alpha
89 beta
90 epsilon/
91 epsilon/zeta
92 gamma/
93 gamma/delta
94 psi/
95 psi/new
96 EOF
97 cmp -s $testroot/stdout.expected $testroot/stdout
98 ret=$?
99 if [ $ret -ne 0 ]; then
100 diff -u $testroot/stdout.expected $testroot/stdout
101 test_done "$testroot" "$ret"
102 return 1
103 fi
105 # Verify that git pull works, too
106 (cd $testroot/repo-clone3 && git pull -q > $testroot/stdout \
107 2> $testroot/stderr)
108 ret=$?
109 if [ $ret -ne 0 ]; then
110 echo "git pull failed unexpectedly" >&2
111 test_done "$testroot" "1"
112 return 1
113 fi
115 # sending to a repository should result in a new pack file
116 ls -R ${GOTD_TEST_REPO}/objects/pack > $testroot/repo-list.after
117 diff -u $testroot/repo-list.before $testroot/repo-list.after \
118 > $testroot/repo-list.diff
119 grep '^+[^+]' < $testroot/repo-list.diff > $testroot/repo-list.newlines
120 nplus=`wc -l < $testroot/repo-list.newlines | tr -d ' '`
121 if [ "$nplus" != "2" ]; then
122 echo "$nplus new files created:"
123 cat $testroot/repo-list.diff
124 test_done "$testroot" "$ret"
125 return 1
126 fi
127 egrep -q '\+pack-[a-f0-9]{40}.pack' $testroot/repo-list.newlines
128 ret=$?
129 if [ $ret -ne 0 ]; then
130 echo "new pack file not found in ${GOTD_TEST_REPO}"
131 cat $testroot/repo-list.newlines
132 test_done "$testroot" "$ret"
133 return 1
134 fi
135 egrep -q '\+pack-[a-f0-9]{40}.idx' $testroot/repo-list.newlines
136 ret=$?
137 if [ $ret -ne 0 ]; then
138 echo "new pack index not found in ${GOTD_TEST_REPO}"
139 test_done "$testroot" "$ret"
140 return 1
141 fi
143 test_done "$testroot" "$ret"
146 test_fetch_more_history() {
147 local testroot=`test_init fetch_more_history 1`
149 got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
150 ret=$?
151 if [ $ret -ne 0 ]; then
152 echo "got clone failed unexpectedly" >&2
153 test_done "$testroot" "1"
154 return 1
155 fi
157 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
158 ret=$?
159 if [ $ret -ne 0 ]; then
160 echo "got checkout failed unexpectedly" >&2
161 test_done "$testroot" "1"
162 return 1
163 fi
165 # Create some more commit history on the main branch.
166 # History needs to be deep enough to trick 'git pull' into sending
167 # a lot of 'have' lines, which triggered a bug in gotd.
168 for i in `jot 50`; do
169 echo "more alpha" >> $testroot/wt/alpha
170 (cd $testroot/wt && got commit -m 'more changes' > /dev/null)
171 done
172 got send -b main -q -r $testroot/repo-clone
173 ret=$?
174 if [ $ret -ne 0 ]; then
175 echo "got send failed unexpectedly" >&2
176 test_done "$testroot" "1"
177 return 1
178 fi
180 # create a second clone to test an incremental fetch with later
181 got clone -q -m ${GOTD_TEST_REPO_URL} $testroot/repo-clone2
182 ret=$?
183 if [ $ret -ne 0 ]; then
184 echo "got clone failed unexpectedly" >&2
185 test_done "$testroot" "1"
186 return 1
187 fi
188 # same for Git, which used to fail:
189 # fetch-pack: protocol error: bad band #69
190 # fatal: protocol error: bad pack header
191 # gotsh: unexpected 'have' packet
192 git clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone3 \
193 >$testroot/stdout 2>$testroot/stderr
194 ret=$?
195 if [ $ret -ne 0 ]; then
196 echo "git clone failed unexpectedly" >&2
197 test_done "$testroot" "1"
198 return 1
199 fi
201 # Create more commit history on the main branch
202 echo "more alpha" >> $testroot/wt/alpha
203 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
204 echo "more beta" >> $testroot/wt/beta
205 (cd $testroot/wt && got commit -m 'more changes' > /dev/null)
206 (cd $testroot/wt && got rm epsilon/zeta > /dev/null)
207 (cd $testroot/wt && got commit -m 'rm epsilon/zeta' > /dev/null)
208 got send -b main -q -r $testroot/repo-clone
209 ret=$?
210 if [ $ret -ne 0 ]; then
211 echo "got send failed unexpectedly" >&2
212 test_done "$testroot" "1"
213 return 1
214 fi
216 # Verify that the new changes can be fetched
217 got fetch -q -r $testroot/repo-clone2
218 ret=$?
219 if [ $ret -ne 0 ]; then
220 echo "got fetch failed unexpectedly" >&2
221 test_done "$testroot" "1"
222 return 1
223 fi
225 got tree -R -r $testroot/repo-clone2 > $testroot/stdout
226 cat > $testroot/stdout.expected <<EOF
227 alpha
228 beta
229 gamma/
230 gamma/delta
231 psi/
232 psi/new
233 EOF
234 cmp -s $testroot/stdout.expected $testroot/stdout
235 ret=$?
236 if [ $ret -ne 0 ]; then
237 diff -u $testroot/stdout.expected $testroot/stdout
238 test_done "$testroot" "$ret"
239 return 1
240 fi
242 # Verify that git pull works, too
243 (cd $testroot/repo-clone3 && git pull -q > $testroot/stdout \
244 2> $testroot/stderr)
245 ret=$?
246 if [ $ret -ne 0 ]; then
247 echo "git pull failed unexpectedly" >&2
248 test_done "$testroot" "1"
249 return 1
250 fi
252 test_done "$testroot" "$ret"
255 test_send_new_empty_branch() {
256 local testroot=`test_init send_new_empty_branch 1`
258 got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
259 ret=$?
260 if [ $ret -ne 0 ]; then
261 echo "got clone failed unexpectedly" >&2
262 test_done "$testroot" "1"
263 return 1
264 fi
265 local commit_id=`git_show_head $testroot/repo-clone`
267 got branch -r $testroot/repo-clone -c main newbranch2 >/dev/null
268 got send -b newbranch2 -q -r $testroot/repo-clone
269 ret=$?
270 if [ $ret -ne 0 ]; then
271 echo "got send failed unexpectedly" >&2
272 test_done "$testroot" "1"
273 return 1
274 fi
276 # Verify that the send operation worked fine.
277 got clone -l ${GOTD_TEST_REPO_URL} | grep newbranch2 > $testroot/stdout
278 ret=$?
279 if [ $ret -ne 0 ]; then
280 echo "got clone -l failed unexpectedly" >&2
281 test_done "$testroot" "1"
282 return 1
283 fi
285 echo "refs/heads/newbranch2: $commit_id" > $testroot/stdout.expected
286 cmp -s $testroot/stdout.expected $testroot/stdout
287 ret=$?
288 if [ $ret -ne 0 ]; then
289 diff -u $testroot/stdout.expected $testroot/stdout
290 fi
292 test_done "$testroot" "$ret"
295 test_delete_branch() {
296 local testroot=`test_init delete_branch 1`
298 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
299 ret=$?
300 if [ $ret -ne 0 ]; then
301 echo "got clone failed unexpectedly" >&2
302 test_done "$testroot" 1
303 return 1
304 fi
306 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
307 ret=$?
308 if [ $ret -ne 0 ]; then
309 echo "got checkout failed unexpectedly" >&2
310 test_done "$testroot" 1
311 return 1
312 fi
314 (cd $testroot/wt && got branch foo) >/dev/null
315 ret=$?
316 if [ $ret -ne 0 ]; then
317 echo "got branch failed unexpectedly" >&2
318 test_done "$testroot" 1
319 return 1
320 fi
322 echo modified alpha > $testroot/wt/alpha
323 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
324 ret=$?
325 if [ $ret -ne 0 ]; then
326 echo "got commit failed unexpectedly" >&2
327 test_done "$testroot" 1
328 return 1
329 fi
331 local foo_id=`git_show_branch_head "$testroot/repo-clone" foo`
332 local main_id=`git_show_branch_head "$testroot/repo-clone" main`
333 local nb_id=`git_show_branch_head "$testroot/repo-clone" newbranch`
334 local nb2_id=`git_show_branch_head "$testroot/repo-clone" newbranch2`
335 local tag_id=`got ref -r "$testroot/repo-clone" -l refs/tags/1.0 | \
336 awk '{print $2}'`
338 if ! got send -q -r $testroot/repo-clone -b foo; then
339 echo "got send failed unexpectedly" >&2
340 test_done "$testroot" 1
341 return 1
342 fi
344 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
345 cat <<EOF >$testroot/refs.expected
346 HEAD: refs/heads/main
347 HEAD: $main_id
348 refs/heads/foo: $foo_id
349 refs/heads/main: $main_id
350 refs/heads/newbranch: $nb_id
351 refs/heads/newbranch2: $nb2_id
352 refs/tags/1.0: $tag_id
353 EOF
354 if ! cmp -s $testroot/refs.expected $testroot/refs; then
355 diff -u $testroot/refs.expected $testroot/refs
356 test_done "$testroot" 1
357 return 1
358 fi
360 (cd $testroot/repo-clone && git push -d origin foo) >/dev/null 2>&1
361 ret=$?
362 if [ $ret -ne 0 ]; then
363 echo "git push -d failed unexpectedly" >&2
364 test_done "$testroot" 1
365 return 1
366 fi
368 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
369 cat <<EOF >$testroot/refs.expected
370 HEAD: refs/heads/main
371 HEAD: $main_id
372 refs/heads/main: $main_id
373 refs/heads/newbranch: $nb_id
374 refs/heads/newbranch2: $nb2_id
375 refs/tags/1.0: $tag_id
376 EOF
377 if ! cmp -s $testroot/refs.expected $testroot/refs; then
378 diff -u $testroot/refs.expected $testroot/refs
379 test_done "$testroot" 1
380 return 1
381 fi
383 # try to delete multiple branches in one go
384 got send -r $testroot/repo-clone -d newbranch -d newbranch2 \
385 >$testroot/stdout
386 ret=$?
387 if [ $ret -ne 0 ]; then
388 echo "got send with multiple -d failed unexpectedly" >&2
389 test_done "$testroot" 1
390 return 1
391 fi
393 cat <<EOF >$testroot/stdout.expected
394 Connecting to "origin" ${GOTD_TEST_REPO_URL}
395 Server has deleted refs/heads/newbranch2
396 Server has deleted refs/heads/newbranch
397 EOF
398 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
399 diff -u $testroot/stdout.expected $testroot/stdout
400 test_done "$testroot" 1
401 return 1
402 fi
404 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
405 cat <<EOF >$testroot/refs.expected
406 HEAD: refs/heads/main
407 HEAD: $main_id
408 refs/heads/main: $main_id
409 refs/tags/1.0: $tag_id
410 EOF
411 if ! cmp -s $testroot/refs.expected $testroot/refs; then
412 diff -u $testroot/refs.expected $testroot/refs
413 test_done "$testroot" 1
414 return 1
415 fi
417 # now try again but while also updating another branch
418 # other than deleting `foo'.
420 (cd $testroot/wt && got up -b main && \
421 echo 'more alpha' > alpha && \
422 got commit -m 'edit alpha on main' && \
423 got send -q -b foo) >/dev/null
424 main_id=`git_show_branch_head "$testroot/repo-clone" main`
426 got send -r $testroot/repo-clone -d foo -b main | \
427 grep '^Server has' >$testroot/stdout
428 ret=$?
429 if [ $ret -ne 0 ]; then
430 echo "got send -d foo -b main failed unexpectedly" >&2
431 test_done "$testroot" 1
432 return 1
433 fi
435 cat <<EOF >$testroot/stdout.expected
436 Server has accepted refs/heads/main
437 Server has deleted refs/heads/foo
438 EOF
439 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
440 diff -u $testroot/stdout.expected $testroot/stdout
441 test_done "$testroot" 1
442 return 1
443 fi
445 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
446 cat <<EOF >$testroot/refs.expected
447 HEAD: refs/heads/main
448 HEAD: $main_id
449 refs/heads/main: $main_id
450 refs/tags/1.0: $tag_id
451 EOF
452 if ! cmp -s $testroot/refs.expected $testroot/refs; then
453 diff -u $testroot/refs.expected $testroot/refs
454 test_done "$testroot" 1
455 return 1
456 fi
458 test_done "$testroot" 0
461 test_rewind_branch() {
462 local testroot=`test_init rewind_branch 1`
464 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
465 ret=$?
466 if [ $ret -ne 0 ]; then
467 echo "got clone failed unexpectedly" >&2
468 test_done "$testroot" 1
469 return 1
470 fi
472 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
473 ret=$?
474 if [ $ret -ne 0 ]; then
475 echo "got checkout failed unexpectedly" >&2
476 test_done "$testroot" 1
477 return 1
478 fi
480 (cd $testroot/wt && got branch foo) >/dev/null
481 ret=$?
482 if [ $ret -ne 0 ]; then
483 echo "got branch failed unexpectedly" >&2
484 test_done "$testroot" 1
485 return 1
486 fi
488 echo modified alpha > $testroot/wt/alpha
489 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
490 ret=$?
491 if [ $ret -ne 0 ]; then
492 echo "got commit failed unexpectedly" >&2
493 test_done "$testroot" 1
494 return 1
495 fi
497 if ! got send -q -r $testroot/repo-clone -b foo; then
498 echo "got send failed unexpectedly" >&2
499 test_done "$testroot" 1
500 return 1
501 fi
503 local foo_id=`git_show_branch_head "$testroot/repo-clone" foo`
504 local main_id=`git_show_branch_head "$testroot/repo-clone" main`
505 local tag_id=`got ref -r "$testroot/repo-clone" -l refs/tags/1.0 | \
506 awk '{print $2}'`
508 (cd $testroot/wt && got update -c $main_id) >/dev/null
509 ret=$?
510 if [ $ret -ne 0 ]; then
511 echo "got update failed unexpectedly" >&2
512 test_done "$testroot" 1
513 return 1
514 fi
516 (cd $testroot/wt && got histedit -d) >/dev/null
517 ret=$?
518 if [ $ret -ne 0 ]; then
519 echo "got histedit failed unexpectedly" >&2
520 test_done "$testroot" 1
521 return 1
522 fi
524 if ! got send -q -r $testroot/repo-clone -f -b foo; then
525 echo "got send failed unexpectedly" >&2
526 test_done "$testroot" 1
527 return 1
528 fi
530 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
531 cat <<EOF >$testroot/refs.expected
532 HEAD: refs/heads/main
533 HEAD: $main_id
534 refs/heads/foo: $main_id
535 refs/heads/main: $main_id
536 refs/tags/1.0: $tag_id
537 EOF
538 if ! cmp -s $testroot/refs.expected $testroot/refs; then
539 diff -u $testroot/refs.expected $testroot/refs
540 test_done "$testroot" 1
541 return 1
542 fi
544 test_done "$testroot" 0
547 test_parseargs "$@"
548 run_test test_send_basic
549 run_test test_fetch_more_history
550 run_test test_send_new_empty_branch
551 run_test test_delete_branch
552 run_test test_rewind_branch