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_revert_basic {
20 local testroot=`test_init revert_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
23 ret="$?"
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
26 return 1
27 fi
29 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
31 echo 'R epsilon/zeta' > $testroot/stdout.expected
33 (cd $testroot/wt && got revert epsilon/zeta > $testroot/stdout)
35 cmp -s $testroot/stdout.expected $testroot/stdout
36 ret="$?"
37 if [ "$ret" != "0" ]; then
38 diff -u $testroot/stdout.expected $testroot/stdout
39 test_done "$testroot" "$ret"
40 return 1
41 fi
43 echo "zeta" > $testroot/content.expected
44 cat $testroot/wt/epsilon/zeta > $testroot/content
46 cmp -s $testroot/content.expected $testroot/content
47 ret="$?"
48 if [ "$ret" != "0" ]; then
49 diff -u $testroot/content.expected $testroot/content
50 fi
51 test_done "$testroot" "$ret"
53 }
55 function test_revert_rm {
56 local testroot=`test_init revert_rm`
58 got checkout $testroot/repo $testroot/wt > /dev/null
59 ret="$?"
60 if [ "$ret" != "0" ]; then
61 test_done "$testroot" "$ret"
62 return 1
63 fi
65 (cd $testroot/wt && got rm beta >/dev/null)
67 echo 'R beta' > $testroot/stdout.expected
69 (cd $testroot/wt && got revert beta > $testroot/stdout)
71 cmp -s $testroot/stdout.expected $testroot/stdout
72 ret="$?"
73 if [ "$ret" != "0" ]; then
74 diff -u $testroot/stdout.expected $testroot/stdout
75 test_done "$testroot" "$ret"
76 return 1
77 fi
79 echo "beta" > $testroot/content.expected
80 cat $testroot/wt/beta > $testroot/content
82 cmp -s $testroot/content.expected $testroot/content
83 ret="$?"
84 if [ "$ret" != "0" ]; then
85 diff -u $testroot/content.expected $testroot/content
86 fi
87 test_done "$testroot" "$ret"
88 }
90 function test_revert_add {
91 local testroot=`test_init revert_add`
93 got checkout $testroot/repo $testroot/wt > /dev/null
94 ret="$?"
95 if [ "$ret" != "0" ]; then
96 test_done "$testroot" "$ret"
97 return 1
98 fi
100 echo "new file" > $testroot/wt/new
101 (cd $testroot/wt && got add new >/dev/null)
103 echo 'R new' > $testroot/stdout.expected
105 (cd $testroot/wt && got revert new > $testroot/stdout)
107 cmp -s $testroot/stdout.expected $testroot/stdout
108 ret="$?"
109 if [ "$ret" != "0" ]; then
110 diff -u $testroot/stdout.expected $testroot/stdout
111 test_done "$testroot" "$ret"
112 return 1
113 fi
115 echo "new file" > $testroot/content.expected
116 cat $testroot/wt/new > $testroot/content
118 cmp -s $testroot/content.expected $testroot/content
119 ret="$?"
120 if [ "$ret" != "0" ]; then
121 diff -u $testroot/content.expected $testroot/content
122 test_done "$testroot" "$ret"
123 return 1
124 fi
126 echo '? new' > $testroot/stdout.expected
128 (cd $testroot/wt && got status > $testroot/stdout)
130 cmp -s $testroot/stdout.expected $testroot/stdout
131 ret="$?"
132 if [ "$ret" != "0" ]; then
133 diff -u $testroot/stdout.expected $testroot/stdout
134 fi
135 test_done "$testroot" "$ret"
138 function test_revert_multiple {
139 local testroot=`test_init revert_multiple`
141 got checkout $testroot/repo $testroot/wt > /dev/null
142 ret="$?"
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
145 return 1
146 fi
148 echo "modified alpha" > $testroot/wt/alpha
149 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
151 echo 'R alpha' > $testroot/stdout.expected
152 echo 'R epsilon/zeta' >> $testroot/stdout.expected
154 (cd $testroot/wt && got revert alpha epsilon/zeta > $testroot/stdout)
156 cmp -s $testroot/stdout.expected $testroot/stdout
157 ret="$?"
158 if [ "$ret" != "0" ]; then
159 diff -u $testroot/stdout.expected $testroot/stdout
160 test_done "$testroot" "$ret"
161 return 1
162 fi
164 echo "alpha" > $testroot/content.expected
165 cat $testroot/wt/alpha > $testroot/content
167 cmp -s $testroot/content.expected $testroot/content
168 ret="$?"
169 if [ "$ret" != "0" ]; then
170 diff -u $testroot/content.expected $testroot/content
171 test_done "$testroot" "$ret"
172 return 1
173 fi
175 echo "zeta" > $testroot/content.expected
176 cat $testroot/wt/epsilon/zeta > $testroot/content
178 cmp -s $testroot/content.expected $testroot/content
179 ret="$?"
180 if [ "$ret" != "0" ]; then
181 diff -u $testroot/content.expected $testroot/content
182 fi
183 test_done "$testroot" "$ret"
186 function test_revert_file_in_new_subdir {
187 local testroot=`test_init revert_file_in_new_subdir`
189 got checkout $testroot/repo $testroot/wt > /dev/null
190 ret="$?"
191 if [ "$ret" != "0" ]; then
192 test_done "$testroot" "$ret"
193 return 1
194 fi
197 mkdir -p $testroot/wt/newdir
198 echo new > $testroot/wt/newdir/new
199 (cd $testroot/wt && got add newdir/new > /dev/null)
201 (cd $testroot/wt && got revert newdir/new > $testroot/stdout)
203 echo "R newdir/new" > $testroot/stdout.expected
204 cmp -s $testroot/stdout.expected $testroot/stdout
205 ret="$?"
206 if [ "$ret" != "0" ]; then
207 diff -u $testroot/stdout.expected $testroot/stdout
208 test_done "$testroot" "$ret"
209 return 1
210 fi
212 (cd $testroot/wt && got status > $testroot/stdout)
214 echo "? newdir/new" > $testroot/stdout.expected
215 cmp -s $testroot/stdout.expected $testroot/stdout
216 ret="$?"
217 if [ "$ret" != "0" ]; then
218 diff -u $testroot/stdout.expected $testroot/stdout
219 fi
220 test_done "$testroot" "$ret"
224 function test_revert_no_arguments {
225 local testroot=`test_init revert_no_arguments`
227 got checkout $testroot/repo $testroot/wt > /dev/null
228 ret="$?"
229 if [ "$ret" != "0" ]; then
230 test_done "$testroot" "$ret"
231 return 1
232 fi
234 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
236 (cd $testroot/wt && got revert > $testroot/stdout 2> $testroot/stderr)
237 ret="$?"
238 if [ "$ret" == "0" ]; then
239 echo "revert command succeeded unexpectedly" >&2
240 test_done "$testroot" "1"
241 return 1
242 fi
244 echo -n > $testroot/stdout.expected
245 cmp -s $testroot/stdout.expected $testroot/stdout
246 ret="$?"
247 if [ "$ret" != "0" ]; then
248 diff -u $testroot/stdout.expected $testroot/stdout
249 test_done "$testroot" "$ret"
250 return 1
251 fi
253 echo "usage: got revert [-p] [-F response-script] [-R] path ..." \
254 > $testroot/stderr.expected
255 cmp -s $testroot/stderr.expected $testroot/stderr
256 ret="$?"
257 if [ "$ret" != "0" ]; then
258 diff -u $testroot/stderr.expected $testroot/stderr
259 fi
260 test_done "$testroot" "$ret"
263 function test_revert_directory {
264 local testroot=`test_init revert_directory`
266 got checkout $testroot/repo $testroot/wt > /dev/null
267 ret="$?"
268 if [ "$ret" != "0" ]; then
269 test_done "$testroot" "$ret"
270 return 1
271 fi
273 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
275 (cd $testroot/wt && got revert . > $testroot/stdout 2> $testroot/stderr)
276 ret="$?"
277 if [ "$ret" == "0" ]; then
278 echo "got revert command succeeded unexpectedly" >&2
279 test_done "$testroot" "1"
280 return 1
281 fi
282 echo "got: reverting directories requires -R option" \
283 > $testroot/stderr.expected
284 cmp -s $testroot/stderr.expected $testroot/stderr
285 ret="$?"
286 if [ "$ret" != "0" ]; then
287 diff -u $testroot/stderr.expected $testroot/stderr
288 test_done "$testroot" "$ret"
289 return 1
290 fi
292 echo -n > $testroot/stdout.expected
293 cmp -s $testroot/stdout.expected $testroot/stdout
294 ret="$?"
295 if [ "$ret" != "0" ]; then
296 diff -u $testroot/stdout.expected $testroot/stdout
297 test_done "$testroot" "$ret"
298 return 1
299 fi
301 (cd $testroot/wt && got revert -R . > $testroot/stdout)
303 echo 'R epsilon/zeta' > $testroot/stdout.expected
304 cmp -s $testroot/stdout.expected $testroot/stdout
305 ret="$?"
306 if [ "$ret" != "0" ]; then
307 diff -u $testroot/stdout.expected $testroot/stdout
308 test_done "$testroot" "$ret"
309 return 1
310 fi
312 echo "zeta" > $testroot/content.expected
313 cat $testroot/wt/epsilon/zeta > $testroot/content
315 cmp -s $testroot/content.expected $testroot/content
316 ret="$?"
317 if [ "$ret" != "0" ]; then
318 diff -u $testroot/content.expected $testroot/content
319 fi
320 test_done "$testroot" "$ret"
324 function test_revert_patch {
325 local testroot=`test_init revert_patch`
327 jot 16 > $testroot/repo/numbers
328 (cd $testroot/repo && git add numbers)
329 git_commit $testroot/repo -m "added numbers file"
330 local commit_id=`git_show_head $testroot/repo`
332 got checkout $testroot/repo $testroot/wt > /dev/null
333 ret="$?"
334 if [ "$ret" != "0" ]; then
335 test_done "$testroot" "$ret"
336 return 1
337 fi
339 sed -i -e 's/^2$/a/' $testroot/wt/numbers
340 sed -i -e 's/^7$/b/' $testroot/wt/numbers
341 sed -i -e 's/^16$/c/' $testroot/wt/numbers
343 (cd $testroot/wt && got diff > $testroot/numbers.diff)
345 # don't revert any hunks
346 printf "n\nn\nn\n" > $testroot/patchscript
347 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
348 numbers > $testroot/stdout)
349 ret="$?"
350 if [ "$ret" != "0" ]; then
351 echo "got revert command failed unexpectedly" >&2
352 test_done "$testroot" "1"
353 return 1
354 fi
355 cat > $testroot/stdout.expected <<EOF
356 -----------------------------------------------
357 @@ -1,5 +1,5 @@
359 -2
360 +a
364 -----------------------------------------------
365 M numbers (change 1 of 3)
366 revert this change? [y/n/q] n
367 -----------------------------------------------
368 @@ -4,7 +4,7 @@
372 -7
373 +b
376 10
377 -----------------------------------------------
378 M numbers (change 2 of 3)
379 revert this change? [y/n/q] n
380 -----------------------------------------------
381 @@ -13,4 +13,4 @@
382 13
383 14
384 15
385 -16
386 +c
387 -----------------------------------------------
388 M numbers (change 3 of 3)
389 revert this change? [y/n/q] n
390 EOF
391 cmp -s $testroot/stdout.expected $testroot/stdout
392 ret="$?"
393 if [ "$ret" != "0" ]; then
394 diff -u $testroot/stdout.expected $testroot/stdout
395 test_done "$testroot" "$ret"
396 return 1
397 fi
399 (cd $testroot/wt && got status > $testroot/stdout)
400 echo "M numbers" > $testroot/stdout.expected
401 cmp -s $testroot/stdout.expected $testroot/stdout
402 ret="$?"
403 if [ "$ret" != "0" ]; then
404 diff -u $testroot/stdout.expected $testroot/stdout
405 test_done "$testroot" "$ret"
406 return 1
407 fi
409 (cd $testroot/wt && got diff > $testroot/stdout)
410 cmp -s $testroot/numbers.diff $testroot/stdout
411 ret="$?"
412 if [ "$ret" != "0" ]; then
413 diff -u $testroot/numbers.diff $testroot/stdout
414 test_done "$testroot" "$ret"
415 return 1
416 fi
418 # revert middle hunk
419 printf "n\ny\nn\n" > $testroot/patchscript
420 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
421 numbers > $testroot/stdout)
423 cat > $testroot/stdout.expected <<EOF
424 -----------------------------------------------
425 @@ -1,5 +1,5 @@
427 -2
428 +a
432 -----------------------------------------------
433 M numbers (change 1 of 3)
434 revert this change? [y/n/q] n
435 -----------------------------------------------
436 @@ -4,7 +4,7 @@
440 -7
441 +b
444 10
445 -----------------------------------------------
446 M numbers (change 2 of 3)
447 revert this change? [y/n/q] y
448 -----------------------------------------------
449 @@ -13,4 +13,4 @@
450 13
451 14
452 15
453 -16
454 +c
455 -----------------------------------------------
456 M numbers (change 3 of 3)
457 revert this change? [y/n/q] n
458 EOF
459 cmp -s $testroot/stdout.expected $testroot/stdout
460 ret="$?"
461 if [ "$ret" != "0" ]; then
462 diff -u $testroot/stdout.expected $testroot/stdout
463 test_done "$testroot" "$ret"
464 return 1
465 fi
467 (cd $testroot/wt && got status > $testroot/stdout)
468 echo "M numbers" > $testroot/stdout.expected
469 cmp -s $testroot/stdout.expected $testroot/stdout
470 ret="$?"
471 if [ "$ret" != "0" ]; then
472 diff -u $testroot/stdout.expected $testroot/stdout
473 test_done "$testroot" "$ret"
474 return 1
475 fi
477 (cd $testroot/wt && got diff > $testroot/stdout)
479 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
480 echo -n 'blob - ' >> $testroot/stdout.expected
481 got tree -r $testroot/repo -i -c $commit_id \
482 | grep 'numbers$' | cut -d' ' -f 1 \
483 >> $testroot/stdout.expected
484 echo 'file + numbers' >> $testroot/stdout.expected
485 cat >> $testroot/stdout.expected <<EOF
486 --- numbers
487 +++ numbers
488 @@ -1,5 +1,5 @@
490 -2
491 +a
495 @@ -13,4 +13,4 @@
496 13
497 14
498 15
499 -16
500 +c
501 EOF
502 cmp -s $testroot/stdout.expected $testroot/stdout
503 ret="$?"
504 if [ "$ret" != "0" ]; then
505 diff -u $testroot/stdout.expected $testroot/stdout
506 test_done "$testroot" "$ret"
507 return 1
508 fi
510 # revert last hunk
511 printf "n\ny\n" > $testroot/patchscript
512 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
513 numbers > $testroot/stdout)
514 cat > $testroot/stdout.expected <<EOF
515 -----------------------------------------------
516 @@ -1,5 +1,5 @@
518 -2
519 +a
523 -----------------------------------------------
524 M numbers (change 1 of 2)
525 revert this change? [y/n/q] n
526 -----------------------------------------------
527 @@ -13,4 +13,4 @@
528 13
529 14
530 15
531 -16
532 +c
533 -----------------------------------------------
534 M numbers (change 2 of 2)
535 revert this change? [y/n/q] y
536 EOF
537 cmp -s $testroot/stdout.expected $testroot/stdout
538 ret="$?"
539 if [ "$ret" != "0" ]; then
540 diff -u $testroot/stdout.expected $testroot/stdout
541 test_done "$testroot" "$ret"
542 return 1
543 fi
545 (cd $testroot/wt && got diff > $testroot/stdout)
547 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
548 echo -n 'blob - ' >> $testroot/stdout.expected
549 got tree -r $testroot/repo -i -c $commit_id \
550 | grep 'numbers$' | cut -d' ' -f 1 \
551 >> $testroot/stdout.expected
552 echo 'file + numbers' >> $testroot/stdout.expected
553 cat >> $testroot/stdout.expected <<EOF
554 --- numbers
555 +++ numbers
556 @@ -1,5 +1,5 @@
558 -2
559 +a
563 EOF
564 cmp -s $testroot/stdout.expected $testroot/stdout
565 ret="$?"
566 if [ "$ret" != "0" ]; then
567 diff -u $testroot/stdout.expected $testroot/stdout
568 fi
569 test_done "$testroot" "$ret"
572 function test_revert_patch_added {
573 local testroot=`test_init revert_patch_added`
574 local commit_id=`git_show_head $testroot/repo`
576 got checkout $testroot/repo $testroot/wt > /dev/null
577 ret="$?"
578 if [ "$ret" != "0" ]; then
579 test_done "$testroot" "$ret"
580 return 1
581 fi
583 echo "new" > $testroot/wt/epsilon/new
584 (cd $testroot/wt && got add epsilon/new > /dev/null)
586 printf "n\n" > $testroot/patchscript
587 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
588 epsilon/new > $testroot/stdout)
590 echo "A epsilon/new" > $testroot/stdout.expected
591 echo "revert this addition? [y/n] n" >> $testroot/stdout.expected
592 cmp -s $testroot/stdout.expected $testroot/stdout
593 ret="$?"
594 if [ "$ret" != "0" ]; then
595 diff -u $testroot/stdout.expected $testroot/stdout
596 test_done "$testroot" "$ret"
597 return 1
598 fi
600 (cd $testroot/wt && got status > $testroot/stdout)
601 echo "A epsilon/new" > $testroot/stdout.expected
602 cmp -s $testroot/stdout.expected $testroot/stdout
603 ret="$?"
604 if [ "$ret" != "0" ]; then
605 diff -u $testroot/stdout.expected $testroot/stdout
606 test_done "$testroot" "$ret"
607 return 1
608 fi
610 printf "y\n" > $testroot/patchscript
611 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
612 epsilon/new > $testroot/stdout)
614 echo "A epsilon/new" > $testroot/stdout.expected
615 echo "revert this addition? [y/n] y" >> $testroot/stdout.expected
616 echo "R epsilon/new" >> $testroot/stdout.expected
617 cmp -s $testroot/stdout.expected $testroot/stdout
618 ret="$?"
619 if [ "$ret" != "0" ]; then
620 diff -u $testroot/stdout.expected $testroot/stdout
621 test_done "$testroot" "$ret"
622 return 1
623 fi
625 (cd $testroot/wt && got status > $testroot/stdout)
626 echo "? epsilon/new" > $testroot/stdout.expected
627 cmp -s $testroot/stdout.expected $testroot/stdout
628 ret="$?"
629 if [ "$ret" != "0" ]; then
630 diff -u $testroot/stdout.expected $testroot/stdout
631 fi
632 test_done "$testroot" "$ret"
635 function test_revert_patch_removed {
636 local testroot=`test_init revert_patch_removed`
637 local commit_id=`git_show_head $testroot/repo`
639 got checkout $testroot/repo $testroot/wt > /dev/null
640 ret="$?"
641 if [ "$ret" != "0" ]; then
642 test_done "$testroot" "$ret"
643 return 1
644 fi
646 (cd $testroot/wt && got rm beta > /dev/null)
648 printf "n\n" > $testroot/patchscript
649 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
650 beta > $testroot/stdout)
651 echo "D beta" > $testroot/stdout.expected
652 echo "revert this deletion? [y/n] n" >> $testroot/stdout.expected
653 cmp -s $testroot/stdout.expected $testroot/stdout
654 ret="$?"
655 if [ "$ret" != "0" ]; then
656 diff -u $testroot/stdout.expected $testroot/stdout
657 test_done "$testroot" "$ret"
658 return 1
659 fi
661 (cd $testroot/wt && got status > $testroot/stdout)
662 echo "D beta" > $testroot/stdout.expected
663 cmp -s $testroot/stdout.expected $testroot/stdout
664 ret="$?"
665 if [ "$ret" != "0" ]; then
666 diff -u $testroot/stdout.expected $testroot/stdout
667 test_done "$testroot" "$ret"
668 return 1
669 fi
671 printf "y\n" > $testroot/patchscript
672 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
673 beta > $testroot/stdout)
675 echo "D beta" > $testroot/stdout.expected
676 echo "revert this deletion? [y/n] y" >> $testroot/stdout.expected
677 echo "R beta" >> $testroot/stdout.expected
678 cmp -s $testroot/stdout.expected $testroot/stdout
679 ret="$?"
680 if [ "$ret" != "0" ]; then
681 diff -u $testroot/stdout.expected $testroot/stdout
682 test_done "$testroot" "$ret"
683 return 1
684 fi
686 (cd $testroot/wt && got status > $testroot/stdout)
687 echo -n > $testroot/stdout.expected
688 cmp -s $testroot/stdout.expected $testroot/stdout
689 ret="$?"
690 if [ "$ret" != "0" ]; then
691 diff -u $testroot/stdout.expected $testroot/stdout
692 fi
693 test_done "$testroot" "$ret"
696 run_test test_revert_basic
697 run_test test_revert_rm
698 run_test test_revert_add
699 run_test test_revert_multiple
700 run_test test_revert_file_in_new_subdir
701 run_test test_revert_no_arguments
702 run_test test_revert_directory
703 run_test test_revert_patch
704 run_test test_revert_patch_added
705 run_test test_revert_patch_removed