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 # Verify that git push reports no changes to send and no error.
116 (cd $testroot/repo-clone3 && git push -q > $testroot/stdout \
117 2> $testroot/stderr)
118 ret=$?
119 if [ $ret -ne 0 ]; then
120 echo "git push failed unexpectedly" >&2
121 test_done "$testroot" "1"
122 return 1
123 fi
125 # sending to a repository should result in a new pack file
126 ls -R ${GOTD_TEST_REPO}/objects/pack > $testroot/repo-list.after
127 diff -u $testroot/repo-list.before $testroot/repo-list.after \
128 > $testroot/repo-list.diff
129 grep '^+[^+]' < $testroot/repo-list.diff > $testroot/repo-list.newlines
130 nplus=`wc -l < $testroot/repo-list.newlines | tr -d ' '`
131 if [ "$nplus" != "2" ]; then
132 echo "$nplus new files created:"
133 cat $testroot/repo-list.diff
134 test_done "$testroot" "$ret"
135 return 1
136 fi
137 egrep -q '\+pack-[a-f0-9]{40}.pack' $testroot/repo-list.newlines
138 ret=$?
139 if [ $ret -ne 0 ]; then
140 echo "new pack file not found in ${GOTD_TEST_REPO}"
141 cat $testroot/repo-list.newlines
142 test_done "$testroot" "$ret"
143 return 1
144 fi
145 egrep -q '\+pack-[a-f0-9]{40}.idx' $testroot/repo-list.newlines
146 ret=$?
147 if [ $ret -ne 0 ]; then
148 echo "new pack index not found in ${GOTD_TEST_REPO}"
149 test_done "$testroot" "$ret"
150 return 1
151 fi
153 test_done "$testroot" "$ret"
156 test_fetch_more_history() {
157 local testroot=`test_init fetch_more_history 1`
159 got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
160 ret=$?
161 if [ $ret -ne 0 ]; then
162 echo "got clone failed unexpectedly" >&2
163 test_done "$testroot" "1"
164 return 1
165 fi
167 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
168 ret=$?
169 if [ $ret -ne 0 ]; then
170 echo "got checkout failed unexpectedly" >&2
171 test_done "$testroot" "1"
172 return 1
173 fi
175 # Create some more commit history on the main branch.
176 # History needs to be deep enough to trick 'git pull' into sending
177 # a lot of 'have' lines, which triggered a bug in gotd.
178 for i in `jot 50`; do
179 echo "more alpha" >> $testroot/wt/alpha
180 (cd $testroot/wt && got commit -m 'more changes' > /dev/null)
181 done
182 got send -b main -q -r $testroot/repo-clone
183 ret=$?
184 if [ $ret -ne 0 ]; then
185 echo "got send failed unexpectedly" >&2
186 test_done "$testroot" "1"
187 return 1
188 fi
190 # create a second clone to test an incremental fetch with later
191 got clone -q -m ${GOTD_TEST_REPO_URL} $testroot/repo-clone2
192 ret=$?
193 if [ $ret -ne 0 ]; then
194 echo "got clone failed unexpectedly" >&2
195 test_done "$testroot" "1"
196 return 1
197 fi
198 # same for Git, which used to fail:
199 # fetch-pack: protocol error: bad band #69
200 # fatal: protocol error: bad pack header
201 # gotsh: unexpected 'have' packet
202 git clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone3 \
203 >$testroot/stdout 2>$testroot/stderr
204 ret=$?
205 if [ $ret -ne 0 ]; then
206 echo "git clone failed unexpectedly" >&2
207 test_done "$testroot" "1"
208 return 1
209 fi
211 # Create more commit history on the main branch
212 echo "more alpha" >> $testroot/wt/alpha
213 (cd $testroot/wt && got commit -m 'make changes' > /dev/null)
214 echo "more beta" >> $testroot/wt/beta
215 (cd $testroot/wt && got commit -m 'more changes' > /dev/null)
216 (cd $testroot/wt && got rm epsilon/zeta > /dev/null)
217 (cd $testroot/wt && got commit -m 'rm epsilon/zeta' > /dev/null)
218 got send -b main -q -r $testroot/repo-clone
219 ret=$?
220 if [ $ret -ne 0 ]; then
221 echo "got send failed unexpectedly" >&2
222 test_done "$testroot" "1"
223 return 1
224 fi
226 # Verify that the new changes can be fetched
227 got fetch -q -r $testroot/repo-clone2
228 ret=$?
229 if [ $ret -ne 0 ]; then
230 echo "got fetch failed unexpectedly" >&2
231 test_done "$testroot" "1"
232 return 1
233 fi
235 got tree -R -r $testroot/repo-clone2 > $testroot/stdout
236 cat > $testroot/stdout.expected <<EOF
237 alpha
238 beta
239 gamma/
240 gamma/delta
241 psi/
242 psi/new
243 EOF
244 cmp -s $testroot/stdout.expected $testroot/stdout
245 ret=$?
246 if [ $ret -ne 0 ]; then
247 diff -u $testroot/stdout.expected $testroot/stdout
248 test_done "$testroot" "$ret"
249 return 1
250 fi
252 # Verify that git pull works, too
253 (cd $testroot/repo-clone3 && git pull -q > $testroot/stdout \
254 2> $testroot/stderr)
255 ret=$?
256 if [ $ret -ne 0 ]; then
257 echo "git pull failed unexpectedly" >&2
258 test_done "$testroot" "1"
259 return 1
260 fi
262 test_done "$testroot" "$ret"
265 test_send_new_empty_branch() {
266 local testroot=`test_init send_new_empty_branch 1`
268 got clone -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
269 ret=$?
270 if [ $ret -ne 0 ]; then
271 echo "got clone failed unexpectedly" >&2
272 test_done "$testroot" "1"
273 return 1
274 fi
275 local commit_id=`git_show_head $testroot/repo-clone`
277 got branch -r $testroot/repo-clone -c main newbranch2 >/dev/null
278 got send -b newbranch2 -q -r $testroot/repo-clone
279 ret=$?
280 if [ $ret -ne 0 ]; then
281 echo "got send failed unexpectedly" >&2
282 test_done "$testroot" "1"
283 return 1
284 fi
286 # Verify that the send operation worked fine.
287 got clone -l ${GOTD_TEST_REPO_URL} | grep newbranch2 > $testroot/stdout
288 ret=$?
289 if [ $ret -ne 0 ]; then
290 echo "got clone -l failed unexpectedly" >&2
291 test_done "$testroot" "1"
292 return 1
293 fi
295 echo "refs/heads/newbranch2: $commit_id" > $testroot/stdout.expected
296 cmp -s $testroot/stdout.expected $testroot/stdout
297 ret=$?
298 if [ $ret -ne 0 ]; then
299 diff -u $testroot/stdout.expected $testroot/stdout
300 fi
302 test_done "$testroot" "$ret"
305 test_delete_branch() {
306 local testroot=`test_init delete_branch 1`
308 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
309 ret=$?
310 if [ $ret -ne 0 ]; then
311 echo "got clone failed unexpectedly" >&2
312 test_done "$testroot" 1
313 return 1
314 fi
316 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
317 ret=$?
318 if [ $ret -ne 0 ]; then
319 echo "got checkout failed unexpectedly" >&2
320 test_done "$testroot" 1
321 return 1
322 fi
324 (cd $testroot/wt && got branch foo) >/dev/null
325 ret=$?
326 if [ $ret -ne 0 ]; then
327 echo "got branch failed unexpectedly" >&2
328 test_done "$testroot" 1
329 return 1
330 fi
332 echo modified alpha > $testroot/wt/alpha
333 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
334 ret=$?
335 if [ $ret -ne 0 ]; then
336 echo "got commit failed unexpectedly" >&2
337 test_done "$testroot" 1
338 return 1
339 fi
341 local foo_id=`git_show_branch_head "$testroot/repo-clone" foo`
342 local main_id=`git_show_branch_head "$testroot/repo-clone" main`
343 local nb_id=`git_show_branch_head "$testroot/repo-clone" newbranch`
344 local nb2_id=`git_show_branch_head "$testroot/repo-clone" newbranch2`
345 local tag_id=`got ref -r "$testroot/repo-clone" -l refs/tags/1.0 | \
346 awk '{print $2}'`
348 if ! got send -q -r $testroot/repo-clone -b foo; then
349 echo "got send failed unexpectedly" >&2
350 test_done "$testroot" 1
351 return 1
352 fi
354 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
355 cat <<EOF >$testroot/refs.expected
356 HEAD: refs/heads/main
357 HEAD: $main_id
358 refs/heads/foo: $foo_id
359 refs/heads/main: $main_id
360 refs/heads/newbranch: $nb_id
361 refs/heads/newbranch2: $nb2_id
362 refs/tags/1.0: $tag_id
363 EOF
364 if ! cmp -s $testroot/refs.expected $testroot/refs; then
365 diff -u $testroot/refs.expected $testroot/refs
366 test_done "$testroot" 1
367 return 1
368 fi
370 (cd $testroot/repo-clone && git push -d origin foo) >/dev/null 2>&1
371 ret=$?
372 if [ $ret -ne 0 ]; then
373 echo "git push -d failed unexpectedly" >&2
374 test_done "$testroot" 1
375 return 1
376 fi
378 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
379 cat <<EOF >$testroot/refs.expected
380 HEAD: refs/heads/main
381 HEAD: $main_id
382 refs/heads/main: $main_id
383 refs/heads/newbranch: $nb_id
384 refs/heads/newbranch2: $nb2_id
385 refs/tags/1.0: $tag_id
386 EOF
387 if ! cmp -s $testroot/refs.expected $testroot/refs; then
388 diff -u $testroot/refs.expected $testroot/refs
389 test_done "$testroot" 1
390 return 1
391 fi
393 # try to delete multiple branches in one go
394 got send -r $testroot/repo-clone -d newbranch -d newbranch2 \
395 >$testroot/stdout
396 ret=$?
397 if [ $ret -ne 0 ]; then
398 echo "got send with multiple -d failed unexpectedly" >&2
399 test_done "$testroot" 1
400 return 1
401 fi
403 cat <<EOF >$testroot/stdout.expected
404 Connecting to "origin" ${GOTD_TEST_REPO_URL}
405 Server has deleted refs/heads/newbranch2
406 Server has deleted refs/heads/newbranch
407 EOF
408 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
409 diff -u $testroot/stdout.expected $testroot/stdout
410 test_done "$testroot" 1
411 return 1
412 fi
414 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
415 cat <<EOF >$testroot/refs.expected
416 HEAD: refs/heads/main
417 HEAD: $main_id
418 refs/heads/main: $main_id
419 refs/tags/1.0: $tag_id
420 EOF
421 if ! cmp -s $testroot/refs.expected $testroot/refs; then
422 diff -u $testroot/refs.expected $testroot/refs
423 test_done "$testroot" 1
424 return 1
425 fi
427 # now try again but while also updating another branch
428 # other than deleting `foo'.
430 (cd $testroot/wt && got up -b main && \
431 echo 'more alpha' > alpha && \
432 got commit -m 'edit alpha on main' && \
433 got send -q -b foo) >/dev/null
434 main_id=`git_show_branch_head "$testroot/repo-clone" main`
436 got send -r $testroot/repo-clone -d foo -b main | \
437 grep '^Server has' >$testroot/stdout
438 ret=$?
439 if [ $ret -ne 0 ]; then
440 echo "got send -d foo -b main failed unexpectedly" >&2
441 test_done "$testroot" 1
442 return 1
443 fi
445 cat <<EOF >$testroot/stdout.expected
446 Server has accepted refs/heads/main
447 Server has deleted refs/heads/foo
448 EOF
449 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
450 diff -u $testroot/stdout.expected $testroot/stdout
451 test_done "$testroot" 1
452 return 1
453 fi
455 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
456 cat <<EOF >$testroot/refs.expected
457 HEAD: refs/heads/main
458 HEAD: $main_id
459 refs/heads/main: $main_id
460 refs/tags/1.0: $tag_id
461 EOF
462 if ! cmp -s $testroot/refs.expected $testroot/refs; then
463 diff -u $testroot/refs.expected $testroot/refs
464 test_done "$testroot" 1
465 return 1
466 fi
468 test_done "$testroot" 0
471 test_rewind_branch() {
472 local testroot=`test_init rewind_branch 1`
474 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
475 ret=$?
476 if [ $ret -ne 0 ]; then
477 echo "got clone failed unexpectedly" >&2
478 test_done "$testroot" 1
479 return 1
480 fi
482 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
483 ret=$?
484 if [ $ret -ne 0 ]; then
485 echo "got checkout failed unexpectedly" >&2
486 test_done "$testroot" 1
487 return 1
488 fi
490 (cd $testroot/wt && got branch foo) >/dev/null
491 ret=$?
492 if [ $ret -ne 0 ]; then
493 echo "got branch failed unexpectedly" >&2
494 test_done "$testroot" 1
495 return 1
496 fi
498 echo modified alpha > $testroot/wt/alpha
499 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
500 ret=$?
501 if [ $ret -ne 0 ]; then
502 echo "got commit failed unexpectedly" >&2
503 test_done "$testroot" 1
504 return 1
505 fi
507 if ! got send -q -r $testroot/repo-clone -b foo; then
508 echo "got send failed unexpectedly" >&2
509 test_done "$testroot" 1
510 return 1
511 fi
513 local foo_id=`git_show_branch_head "$testroot/repo-clone" foo`
514 local main_id=`git_show_branch_head "$testroot/repo-clone" main`
515 local tag_id=`got ref -r "$testroot/repo-clone" -l refs/tags/1.0 | \
516 awk '{print $2}'`
518 (cd $testroot/wt && got update -c $main_id) >/dev/null
519 ret=$?
520 if [ $ret -ne 0 ]; then
521 echo "got update failed unexpectedly" >&2
522 test_done "$testroot" 1
523 return 1
524 fi
526 (cd $testroot/wt && got histedit -d) >/dev/null
527 ret=$?
528 if [ $ret -ne 0 ]; then
529 echo "got histedit failed unexpectedly" >&2
530 test_done "$testroot" 1
531 return 1
532 fi
534 if ! got send -q -r $testroot/repo-clone -f -b foo; then
535 echo "got send failed unexpectedly" >&2
536 test_done "$testroot" 1
537 return 1
538 fi
540 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
541 cat <<EOF >$testroot/refs.expected
542 HEAD: refs/heads/main
543 HEAD: $main_id
544 refs/heads/foo: $main_id
545 refs/heads/main: $main_id
546 refs/tags/1.0: $tag_id
547 EOF
548 if ! cmp -s $testroot/refs.expected $testroot/refs; then
549 diff -u $testroot/refs.expected $testroot/refs
550 test_done "$testroot" 1
551 return 1
552 fi
554 test_done "$testroot" 0
557 test_parseargs "$@"
558 run_test test_send_basic
559 run_test test_fetch_more_history
560 run_test test_send_new_empty_branch
561 run_test test_delete_branch
562 run_test test_rewind_branch