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 "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 -----------------------------------------------
432 -----------------------------------------------
433 M numbers (change 1 of 3)
434 revert this change? [y/n/q] n
435 -----------------------------------------------
445 -----------------------------------------------
446 M numbers (change 2 of 3)
447 revert this change? [y/n/q] y
448 -----------------------------------------------
455 -----------------------------------------------
456 M numbers (change 3 of 3)
457 revert this change? [y/n/q] n
459 cmp -s $testroot/stdout.expected $testroot/stdout
461 if [ "$ret" != "0" ]; then
462 diff -u $testroot/stdout.expected $testroot/stdout
463 test_done "$testroot" "$ret"
467 (cd $testroot/wt && got status > $testroot/stdout)
468 echo "M numbers" > $testroot/stdout.expected
469 cmp -s $testroot/stdout.expected $testroot/stdout
471 if [ "$ret" != "0" ]; then
472 diff -u $testroot/stdout.expected $testroot/stdout
473 test_done "$testroot" "$ret"
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
502 cmp -s $testroot/stdout.expected $testroot/stdout
504 if [ "$ret" != "0" ]; then
505 diff -u $testroot/stdout.expected $testroot/stdout
506 test_done "$testroot" "$ret"
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 -----------------------------------------------
523 -----------------------------------------------
524 M numbers (change 1 of 2)
525 revert this change? [y/n/q] n
526 -----------------------------------------------
533 -----------------------------------------------
534 M numbers (change 2 of 2)
535 revert this change? [y/n/q] y
537 cmp -s $testroot/stdout.expected $testroot/stdout
539 if [ "$ret" != "0" ]; then
540 diff -u $testroot/stdout.expected $testroot/stdout
541 test_done "$testroot" "$ret"
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
564 cmp -s $testroot/stdout.expected $testroot/stdout
566 if [ "$ret" != "0" ]; then
567 diff -u $testroot/stdout.expected $testroot/stdout
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
578 if [ "$ret" != "0" ]; then
579 test_done "$testroot" "$ret"
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
594 if [ "$ret" != "0" ]; then
595 diff -u $testroot/stdout.expected $testroot/stdout
596 test_done "$testroot" "$ret"
600 (cd $testroot/wt && got status > $testroot/stdout)
601 echo "A epsilon/new" > $testroot/stdout.expected
602 cmp -s $testroot/stdout.expected $testroot/stdout
604 if [ "$ret" != "0" ]; then
605 diff -u $testroot/stdout.expected $testroot/stdout
606 test_done "$testroot" "$ret"
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
619 if [ "$ret" != "0" ]; then
620 diff -u $testroot/stdout.expected $testroot/stdout
621 test_done "$testroot" "$ret"
625 (cd $testroot/wt && got status > $testroot/stdout)
626 echo "? epsilon/new" > $testroot/stdout.expected
627 cmp -s $testroot/stdout.expected $testroot/stdout
629 if [ "$ret" != "0" ]; then
630 diff -u $testroot/stdout.expected $testroot/stdout
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
641 if [ "$ret" != "0" ]; then
642 test_done "$testroot" "$ret"
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
655 if [ "$ret" != "0" ]; then
656 diff -u $testroot/stdout.expected $testroot/stdout
657 test_done "$testroot" "$ret"
661 (cd $testroot/wt && got status > $testroot/stdout)
662 echo "D beta" > $testroot/stdout.expected
663 cmp -s $testroot/stdout.expected $testroot/stdout
665 if [ "$ret" != "0" ]; then
666 diff -u $testroot/stdout.expected $testroot/stdout
667 test_done "$testroot" "$ret"
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
680 if [ "$ret" != "0" ]; then
681 diff -u $testroot/stdout.expected $testroot/stdout
682 test_done "$testroot" "$ret"
686 (cd $testroot/wt && got status > $testroot/stdout)
687 echo -n > $testroot/stdout.expected
688 cmp -s $testroot/stdout.expected $testroot/stdout
690 if [ "$ret" != "0" ]; then
691 diff -u $testroot/stdout.expected $testroot/stdout
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