3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
19 function test_revert_basic {
20 local testroot=`test_init revert_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
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
37 if [ "$ret" != "0" ]; then
38 diff -u $testroot/stdout.expected $testroot/stdout
39 test_done "$testroot" "$ret"
43 echo "zeta" > $testroot/content.expected
44 cat $testroot/wt/epsilon/zeta > $testroot/content
46 cmp -s $testroot/content.expected $testroot/content
48 if [ "$ret" != "0" ]; then
49 diff -u $testroot/content.expected $testroot/content
51 test_done "$testroot" "$ret"
55 function test_revert_rm {
56 local testroot=`test_init revert_rm`
58 got checkout $testroot/repo $testroot/wt > /dev/null
60 if [ "$ret" != "0" ]; then
61 test_done "$testroot" "$ret"
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
73 if [ "$ret" != "0" ]; then
74 diff -u $testroot/stdout.expected $testroot/stdout
75 test_done "$testroot" "$ret"
79 echo "beta" > $testroot/content.expected
80 cat $testroot/wt/beta > $testroot/content
82 cmp -s $testroot/content.expected $testroot/content
84 if [ "$ret" != "0" ]; then
85 diff -u $testroot/content.expected $testroot/content
87 test_done "$testroot" "$ret"
90 function test_revert_add {
91 local testroot=`test_init revert_add`
93 got checkout $testroot/repo $testroot/wt > /dev/null
95 if [ "$ret" != "0" ]; then
96 test_done "$testroot" "$ret"
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
109 if [ "$ret" != "0" ]; then
110 diff -u $testroot/stdout.expected $testroot/stdout
111 test_done "$testroot" "$ret"
115 echo "new file" > $testroot/content.expected
116 cat $testroot/wt/new > $testroot/content
118 cmp -s $testroot/content.expected $testroot/content
120 if [ "$ret" != "0" ]; then
121 diff -u $testroot/content.expected $testroot/content
122 test_done "$testroot" "$ret"
126 echo '? new' > $testroot/stdout.expected
128 (cd $testroot/wt && got status > $testroot/stdout)
130 cmp -s $testroot/stdout.expected $testroot/stdout
132 if [ "$ret" != "0" ]; then
133 diff -u $testroot/stdout.expected $testroot/stdout
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
143 if [ "$ret" != "0" ]; then
144 test_done "$testroot" "$ret"
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
158 if [ "$ret" != "0" ]; then
159 diff -u $testroot/stdout.expected $testroot/stdout
160 test_done "$testroot" "$ret"
164 echo "alpha" > $testroot/content.expected
165 cat $testroot/wt/alpha > $testroot/content
167 cmp -s $testroot/content.expected $testroot/content
169 if [ "$ret" != "0" ]; then
170 diff -u $testroot/content.expected $testroot/content
171 test_done "$testroot" "$ret"
175 echo "zeta" > $testroot/content.expected
176 cat $testroot/wt/epsilon/zeta > $testroot/content
178 cmp -s $testroot/content.expected $testroot/content
180 if [ "$ret" != "0" ]; then
181 diff -u $testroot/content.expected $testroot/content
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
191 if [ "$ret" != "0" ]; then
192 test_done "$testroot" "$ret"
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
206 if [ "$ret" != "0" ]; then
207 diff -u $testroot/stdout.expected $testroot/stdout
208 test_done "$testroot" "$ret"
212 (cd $testroot/wt && got status > $testroot/stdout)
214 echo "? newdir/new" > $testroot/stdout.expected
215 cmp -s $testroot/stdout.expected $testroot/stdout
217 if [ "$ret" != "0" ]; then
218 diff -u $testroot/stdout.expected $testroot/stdout
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
229 if [ "$ret" != "0" ]; then
230 test_done "$testroot" "$ret"
234 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
236 (cd $testroot/wt && got revert > $testroot/stdout 2> $testroot/stderr)
238 if [ "$ret" == "0" ]; then
239 echo "revert command succeeded unexpectedly" >&2
240 test_done "$testroot" "1"
244 echo -n > $testroot/stdout.expected
245 cmp -s $testroot/stdout.expected $testroot/stdout
247 if [ "$ret" != "0" ]; then
248 diff -u $testroot/stdout.expected $testroot/stdout
249 test_done "$testroot" "$ret"
253 echo "usage: got revert [-p] [-F response-script] [-R] path ..." \
254 > $testroot/stderr.expected
255 cmp -s $testroot/stderr.expected $testroot/stderr
257 if [ "$ret" != "0" ]; then
258 diff -u $testroot/stderr.expected $testroot/stderr
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
268 if [ "$ret" != "0" ]; then
269 test_done "$testroot" "$ret"
273 echo "modified epsilon/zeta" > $testroot/wt/epsilon/zeta
275 (cd $testroot/wt && got revert . > $testroot/stdout 2> $testroot/stderr)
277 if [ "$ret" == "0" ]; then
278 echo "got revert command succeeded unexpectedly" >&2
279 test_done "$testroot" "1"
282 echo "got: reverting directories requires -R option" \
283 > $testroot/stderr.expected
284 cmp -s $testroot/stderr.expected $testroot/stderr
286 if [ "$ret" != "0" ]; then
287 diff -u $testroot/stderr.expected $testroot/stderr
288 test_done "$testroot" "$ret"
292 echo -n > $testroot/stdout.expected
293 cmp -s $testroot/stdout.expected $testroot/stdout
295 if [ "$ret" != "0" ]; then
296 diff -u $testroot/stdout.expected $testroot/stdout
297 test_done "$testroot" "$ret"
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
306 if [ "$ret" != "0" ]; then
307 diff -u $testroot/stdout.expected $testroot/stdout
308 test_done "$testroot" "$ret"
312 echo "zeta" > $testroot/content.expected
313 cat $testroot/wt/epsilon/zeta > $testroot/content
315 cmp -s $testroot/content.expected $testroot/content
317 if [ "$ret" != "0" ]; then
318 diff -u $testroot/content.expected $testroot/content
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
334 if [ "$ret" != "0" ]; then
335 test_done "$testroot" "$ret"
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)
350 if [ "$ret" != "0" ]; then
351 echo "got revert command failed unexpectedly" >&2
352 test_done "$testroot" "1"
355 cat > $testroot/stdout.expected <<EOF
356 -----------------------------------------------
364 -----------------------------------------------
365 M numbers (change 1 of 3)
366 revert this change? [y/n/q] n
367 -----------------------------------------------
377 -----------------------------------------------
378 M numbers (change 2 of 3)
379 revert this change? [y/n/q] n
380 -----------------------------------------------
387 -----------------------------------------------
388 M numbers (change 3 of 3)
389 revert this change? [y/n/q] n
391 cmp -s $testroot/stdout.expected $testroot/stdout
393 if [ "$ret" != "0" ]; then
394 diff -u $testroot/stdout.expected $testroot/stdout
395 test_done "$testroot" "$ret"
399 (cd $testroot/wt && got status > $testroot/stdout)
400 echo "M numbers" > $testroot/stdout.expected
401 cmp -s $testroot/stdout.expected $testroot/stdout
403 if [ "$ret" != "0" ]; then
404 diff -u $testroot/stdout.expected $testroot/stdout
405 test_done "$testroot" "$ret"
409 (cd $testroot/wt && got diff > $testroot/stdout)
410 cmp -s $testroot/numbers.diff $testroot/stdout
412 if [ "$ret" != "0" ]; then
413 diff -u $testroot/numbers.diff $testroot/stdout
414 test_done "$testroot" "$ret"
419 printf "y\nn\nn\n" > $testroot/patchscript
420 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
421 numbers > $testroot/stdout)
423 if [ "$ret" != "0" ]; then
424 echo "got revert command failed unexpectedly" >&2
425 test_done "$testroot" "1"
428 cat > $testroot/stdout.expected <<EOF
429 -----------------------------------------------
437 -----------------------------------------------
438 M numbers (change 1 of 3)
439 revert this change? [y/n/q] y
440 -----------------------------------------------
450 -----------------------------------------------
451 M numbers (change 2 of 3)
452 revert this change? [y/n/q] n
453 -----------------------------------------------
460 -----------------------------------------------
461 M numbers (change 3 of 3)
462 revert this change? [y/n/q] n
464 cmp -s $testroot/stdout.expected $testroot/stdout
466 if [ "$ret" != "0" ]; then
467 diff -u $testroot/stdout.expected $testroot/stdout
468 test_done "$testroot" "$ret"
472 (cd $testroot/wt && got status > $testroot/stdout)
473 echo "M numbers" > $testroot/stdout.expected
474 cmp -s $testroot/stdout.expected $testroot/stdout
476 if [ "$ret" != "0" ]; then
477 diff -u $testroot/stdout.expected $testroot/stdout
478 test_done "$testroot" "$ret"
482 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
483 echo -n 'blob - ' >> $testroot/stdout.expected
484 got tree -r $testroot/repo -i -c $commit_id \
485 | grep 'numbers$' | cut -d' ' -f 1 \
486 >> $testroot/stdout.expected
487 echo 'file + numbers' >> $testroot/stdout.expected
488 cat >> $testroot/stdout.expected <<EOF
507 (cd $testroot/wt && got diff > $testroot/stdout)
508 cmp -s $testroot/stdout.expected $testroot/stdout
510 if [ "$ret" != "0" ]; then
511 diff -u $testroot/stdout.expected $testroot/stdout
512 test_done "$testroot" "$ret"
516 # put first hunk back
517 sed -i -e 's/^2$/a/' $testroot/wt/numbers
520 printf "n\ny\nn\n" > $testroot/patchscript
521 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
522 numbers > $testroot/stdout)
524 cat > $testroot/stdout.expected <<EOF
525 -----------------------------------------------
533 -----------------------------------------------
534 M numbers (change 1 of 3)
535 revert this change? [y/n/q] n
536 -----------------------------------------------
546 -----------------------------------------------
547 M numbers (change 2 of 3)
548 revert this change? [y/n/q] y
549 -----------------------------------------------
556 -----------------------------------------------
557 M numbers (change 3 of 3)
558 revert this change? [y/n/q] n
560 cmp -s $testroot/stdout.expected $testroot/stdout
562 if [ "$ret" != "0" ]; then
563 diff -u $testroot/stdout.expected $testroot/stdout
564 test_done "$testroot" "$ret"
568 (cd $testroot/wt && got status > $testroot/stdout)
569 echo "M numbers" > $testroot/stdout.expected
570 cmp -s $testroot/stdout.expected $testroot/stdout
572 if [ "$ret" != "0" ]; then
573 diff -u $testroot/stdout.expected $testroot/stdout
574 test_done "$testroot" "$ret"
578 (cd $testroot/wt && got diff > $testroot/stdout)
580 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
581 echo -n 'blob - ' >> $testroot/stdout.expected
582 got tree -r $testroot/repo -i -c $commit_id \
583 | grep 'numbers$' | cut -d' ' -f 1 \
584 >> $testroot/stdout.expected
585 echo 'file + numbers' >> $testroot/stdout.expected
586 cat >> $testroot/stdout.expected <<EOF
603 cmp -s $testroot/stdout.expected $testroot/stdout
605 if [ "$ret" != "0" ]; then
606 diff -u $testroot/stdout.expected $testroot/stdout
607 test_done "$testroot" "$ret"
612 printf "n\ny\n" > $testroot/patchscript
613 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
614 numbers > $testroot/stdout)
615 cat > $testroot/stdout.expected <<EOF
616 -----------------------------------------------
624 -----------------------------------------------
625 M numbers (change 1 of 2)
626 revert this change? [y/n/q] n
627 -----------------------------------------------
634 -----------------------------------------------
635 M numbers (change 2 of 2)
636 revert this change? [y/n/q] y
638 cmp -s $testroot/stdout.expected $testroot/stdout
640 if [ "$ret" != "0" ]; then
641 diff -u $testroot/stdout.expected $testroot/stdout
642 test_done "$testroot" "$ret"
646 (cd $testroot/wt && got diff > $testroot/stdout)
648 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
649 echo -n 'blob - ' >> $testroot/stdout.expected
650 got tree -r $testroot/repo -i -c $commit_id \
651 | grep 'numbers$' | cut -d' ' -f 1 \
652 >> $testroot/stdout.expected
653 echo 'file + numbers' >> $testroot/stdout.expected
654 cat >> $testroot/stdout.expected <<EOF
665 cmp -s $testroot/stdout.expected $testroot/stdout
667 if [ "$ret" != "0" ]; then
668 diff -u $testroot/stdout.expected $testroot/stdout
670 test_done "$testroot" "$ret"
673 function test_revert_patch_added {
674 local testroot=`test_init revert_patch_added`
675 local commit_id=`git_show_head $testroot/repo`
677 got checkout $testroot/repo $testroot/wt > /dev/null
679 if [ "$ret" != "0" ]; then
680 test_done "$testroot" "$ret"
684 echo "new" > $testroot/wt/epsilon/new
685 (cd $testroot/wt && got add epsilon/new > /dev/null)
687 printf "n\n" > $testroot/patchscript
688 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
689 epsilon/new > $testroot/stdout)
691 echo "A epsilon/new" > $testroot/stdout.expected
692 echo "revert this addition? [y/n] n" >> $testroot/stdout.expected
693 cmp -s $testroot/stdout.expected $testroot/stdout
695 if [ "$ret" != "0" ]; then
696 diff -u $testroot/stdout.expected $testroot/stdout
697 test_done "$testroot" "$ret"
701 (cd $testroot/wt && got status > $testroot/stdout)
702 echo "A epsilon/new" > $testroot/stdout.expected
703 cmp -s $testroot/stdout.expected $testroot/stdout
705 if [ "$ret" != "0" ]; then
706 diff -u $testroot/stdout.expected $testroot/stdout
707 test_done "$testroot" "$ret"
711 printf "y\n" > $testroot/patchscript
712 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
713 epsilon/new > $testroot/stdout)
715 echo "A epsilon/new" > $testroot/stdout.expected
716 echo "revert this addition? [y/n] y" >> $testroot/stdout.expected
717 echo "R epsilon/new" >> $testroot/stdout.expected
718 cmp -s $testroot/stdout.expected $testroot/stdout
720 if [ "$ret" != "0" ]; then
721 diff -u $testroot/stdout.expected $testroot/stdout
722 test_done "$testroot" "$ret"
726 (cd $testroot/wt && got status > $testroot/stdout)
727 echo "? epsilon/new" > $testroot/stdout.expected
728 cmp -s $testroot/stdout.expected $testroot/stdout
730 if [ "$ret" != "0" ]; then
731 diff -u $testroot/stdout.expected $testroot/stdout
733 test_done "$testroot" "$ret"
736 function test_revert_patch_removed {
737 local testroot=`test_init revert_patch_removed`
738 local commit_id=`git_show_head $testroot/repo`
740 got checkout $testroot/repo $testroot/wt > /dev/null
742 if [ "$ret" != "0" ]; then
743 test_done "$testroot" "$ret"
747 (cd $testroot/wt && got rm beta > /dev/null)
749 printf "n\n" > $testroot/patchscript
750 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
751 beta > $testroot/stdout)
752 echo "D beta" > $testroot/stdout.expected
753 echo "revert this deletion? [y/n] n" >> $testroot/stdout.expected
754 cmp -s $testroot/stdout.expected $testroot/stdout
756 if [ "$ret" != "0" ]; then
757 diff -u $testroot/stdout.expected $testroot/stdout
758 test_done "$testroot" "$ret"
762 (cd $testroot/wt && got status > $testroot/stdout)
763 echo "D beta" > $testroot/stdout.expected
764 cmp -s $testroot/stdout.expected $testroot/stdout
766 if [ "$ret" != "0" ]; then
767 diff -u $testroot/stdout.expected $testroot/stdout
768 test_done "$testroot" "$ret"
772 printf "y\n" > $testroot/patchscript
773 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
774 beta > $testroot/stdout)
776 echo "D beta" > $testroot/stdout.expected
777 echo "revert this deletion? [y/n] y" >> $testroot/stdout.expected
778 echo "R beta" >> $testroot/stdout.expected
779 cmp -s $testroot/stdout.expected $testroot/stdout
781 if [ "$ret" != "0" ]; then
782 diff -u $testroot/stdout.expected $testroot/stdout
783 test_done "$testroot" "$ret"
787 (cd $testroot/wt && got status > $testroot/stdout)
788 echo -n > $testroot/stdout.expected
789 cmp -s $testroot/stdout.expected $testroot/stdout
791 if [ "$ret" != "0" ]; then
792 diff -u $testroot/stdout.expected $testroot/stdout
794 test_done "$testroot" "$ret"
797 function test_revert_patch_one_change {
798 local testroot=`test_init revert_patch_one_change`
800 jot 16 > $testroot/repo/numbers
801 (cd $testroot/repo && git add numbers)
802 git_commit $testroot/repo -m "added numbers file"
803 local commit_id=`git_show_head $testroot/repo`
805 got checkout $testroot/repo $testroot/wt > /dev/null
807 if [ "$ret" != "0" ]; then
808 test_done "$testroot" "$ret"
812 # Ensure file size is changed. Avoids race condition causing test
813 # failures where 'got revert' does not see changes to revert if
814 # timestamps and size in stat info remain unchanged.
815 sed -i -e 's/^2$/aa/' $testroot/wt/numbers
817 # revert change with -p
818 printf "y\n" > $testroot/patchscript
819 (cd $testroot/wt && got revert -F $testroot/patchscript -p \
820 numbers > $testroot/stdout)
822 if [ "$ret" != "0" ]; then
823 echo "got revert command failed unexpectedly" >&2
824 test_done "$testroot" "1"
827 cat > $testroot/stdout.expected <<EOF
828 -----------------------------------------------
836 -----------------------------------------------
837 M numbers (change 1 of 1)
838 revert this change? [y/n/q] y
841 if [ "$ret" != "0" ]; then
842 echo "got revert command failed unexpectedly" >&2
843 test_done "$testroot" "1"
847 cmp -s $testroot/stdout.expected $testroot/stdout
849 if [ "$ret" != "0" ]; then
850 diff -u $testroot/stdout.expected $testroot/stdout
851 test_done "$testroot" "$ret"
855 (cd $testroot/wt && got status > $testroot/stdout)
856 echo -n > $testroot/stdout.expected
857 cmp -s $testroot/stdout.expected $testroot/stdout
859 if [ "$ret" != "0" ]; then
860 diff -u $testroot/stdout.expected $testroot/stdout
861 test_done "$testroot" "$ret"
865 (cd $testroot/wt && got diff > $testroot/stdout)
866 echo -n > $testroot/stdout.expected
867 cmp -s $testroot/stdout.expected $testroot/stdout
869 if [ "$ret" != "0" ]; then
870 diff -u $testroot/stdout.expected $testroot/stdout
872 test_done "$testroot" "$ret"
875 run_test test_revert_basic
876 run_test test_revert_rm
877 run_test test_revert_add
878 run_test test_revert_multiple
879 run_test test_revert_file_in_new_subdir
880 run_test test_revert_no_arguments
881 run_test test_revert_directory
882 run_test test_revert_patch
883 run_test test_revert_patch_added
884 run_test test_revert_patch_removed
885 run_test test_revert_patch_one_change