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 refs/heads/foo: $foo_id
358 refs/heads/main: $main_id
359 refs/heads/newbranch: $nb_id
360 refs/heads/newbranch2: $nb2_id
361 refs/tags/1.0: $tag_id
362 EOF
363 if ! cmp -s $testroot/refs.expected $testroot/refs; then
364 diff -u $testroot/refs.expected $testroot/refs
365 test_done "$testroot" 1
366 return 1
367 fi
369 (cd $testroot/repo-clone && git push -d origin foo) >/dev/null 2>&1
370 ret=$?
371 if [ $ret -ne 0 ]; then
372 echo "git push -d failed unexpectedly" >&2
373 test_done "$testroot" 1
374 return 1
375 fi
377 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
378 cat <<EOF >$testroot/refs.expected
379 HEAD: refs/heads/main
380 refs/heads/main: $main_id
381 refs/heads/newbranch: $nb_id
382 refs/heads/newbranch2: $nb2_id
383 refs/tags/1.0: $tag_id
384 EOF
385 if ! cmp -s $testroot/refs.expected $testroot/refs; then
386 diff -u $testroot/refs.expected $testroot/refs
387 test_done "$testroot" 1
388 return 1
389 fi
391 # try to delete multiple branches in one go
392 got send -r $testroot/repo-clone -d newbranch -d newbranch2 \
393 >$testroot/stdout
394 ret=$?
395 if [ $ret -ne 0 ]; then
396 echo "got send with multiple -d failed unexpectedly" >&2
397 test_done "$testroot" 1
398 return 1
399 fi
401 cat <<EOF >$testroot/stdout.expected
402 Connecting to "origin" ${GOTD_TEST_REPO_URL}
403 Server has deleted refs/heads/newbranch2
404 Server has deleted refs/heads/newbranch
405 EOF
406 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
407 diff -u $testroot/stdout.expected $testroot/stdout
408 test_done "$testroot" 1
409 return 1
410 fi
412 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
413 cat <<EOF >$testroot/refs.expected
414 HEAD: refs/heads/main
415 refs/heads/main: $main_id
416 refs/tags/1.0: $tag_id
417 EOF
418 if ! cmp -s $testroot/refs.expected $testroot/refs; then
419 diff -u $testroot/refs.expected $testroot/refs
420 test_done "$testroot" 1
421 return 1
422 fi
424 # now try again but while also updating another branch
425 # other than deleting `foo'.
427 (cd $testroot/wt && got up -b main && \
428 echo 'more alpha' > alpha && \
429 got commit -m 'edit alpha on main' && \
430 got send -q -b foo) >/dev/null
431 main_id=`git_show_branch_head "$testroot/repo-clone" main`
433 got send -r $testroot/repo-clone -d foo -b main | \
434 grep '^Server has' >$testroot/stdout
435 ret=$?
436 if [ $ret -ne 0 ]; then
437 echo "got send -d foo -b main failed unexpectedly" >&2
438 test_done "$testroot" 1
439 return 1
440 fi
442 cat <<EOF >$testroot/stdout.expected
443 Server has accepted refs/heads/main
444 Server has deleted refs/heads/foo
445 EOF
446 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
447 diff -u $testroot/stdout.expected $testroot/stdout
448 test_done "$testroot" 1
449 return 1
450 fi
452 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
453 cat <<EOF >$testroot/refs.expected
454 HEAD: refs/heads/main
455 refs/heads/main: $main_id
456 refs/tags/1.0: $tag_id
457 EOF
458 if ! cmp -s $testroot/refs.expected $testroot/refs; then
459 diff -u $testroot/refs.expected $testroot/refs
460 test_done "$testroot" 1
461 return 1
462 fi
464 test_done "$testroot" 0
467 test_rewind_branch() {
468 local testroot=`test_init rewind_branch 1`
470 got clone -a -q ${GOTD_TEST_REPO_URL} $testroot/repo-clone
471 ret=$?
472 if [ $ret -ne 0 ]; then
473 echo "got clone failed unexpectedly" >&2
474 test_done "$testroot" 1
475 return 1
476 fi
478 got checkout -q $testroot/repo-clone $testroot/wt >/dev/null
479 ret=$?
480 if [ $ret -ne 0 ]; then
481 echo "got checkout failed unexpectedly" >&2
482 test_done "$testroot" 1
483 return 1
484 fi
486 (cd $testroot/wt && got branch foo) >/dev/null
487 ret=$?
488 if [ $ret -ne 0 ]; then
489 echo "got branch failed unexpectedly" >&2
490 test_done "$testroot" 1
491 return 1
492 fi
494 echo modified alpha > $testroot/wt/alpha
495 (cd $testroot/wt && got commit -m 'edit alpha') >/dev/null
496 ret=$?
497 if [ $ret -ne 0 ]; then
498 echo "got commit failed unexpectedly" >&2
499 test_done "$testroot" 1
500 return 1
501 fi
503 if ! got send -q -r $testroot/repo-clone -b foo; then
504 echo "got send failed unexpectedly" >&2
505 test_done "$testroot" 1
506 return 1
507 fi
509 local foo_id=`git_show_branch_head "$testroot/repo-clone" foo`
510 local main_id=`git_show_branch_head "$testroot/repo-clone" main`
511 local tag_id=`got ref -r "$testroot/repo-clone" -l refs/tags/1.0 | \
512 awk '{print $2}'`
514 (cd $testroot/wt && got update -c $main_id) >/dev/null
515 ret=$?
516 if [ $ret -ne 0 ]; then
517 echo "got update failed unexpectedly" >&2
518 test_done "$testroot" 1
519 return 1
520 fi
522 (cd $testroot/wt && got histedit -d) >/dev/null
523 ret=$?
524 if [ $ret -ne 0 ]; then
525 echo "got histedit failed unexpectedly" >&2
526 test_done "$testroot" 1
527 return 1
528 fi
530 if ! got send -q -r $testroot/repo-clone -f -b foo; then
531 echo "got send failed unexpectedly" >&2
532 test_done "$testroot" 1
533 return 1
534 fi
536 got fetch -q -r $testroot/repo-clone -l >$testroot/refs
537 cat <<EOF >$testroot/refs.expected
538 HEAD: refs/heads/main
539 refs/heads/foo: $main_id
540 refs/heads/main: $main_id
541 refs/tags/1.0: $tag_id
542 EOF
543 if ! cmp -s $testroot/refs.expected $testroot/refs; then
544 diff -u $testroot/refs.expected $testroot/refs
545 test_done "$testroot" 1
546 return 1
547 fi
549 test_done "$testroot" 0
552 test_parseargs "$@"
553 run_test test_send_basic
554 run_test test_fetch_more_history
555 run_test test_send_new_empty_branch
556 run_test test_delete_branch
557 run_test test_rewind_branch