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_unstage_basic {
20 local testroot=`test_init unstage_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ "$ret" != "0" ]; then
25 test_done "$testroot" "$ret"
29 echo "modified file" > $testroot/wt/alpha
30 (cd $testroot/wt && got rm beta > /dev/null)
31 echo "new file" > $testroot/wt/foo
32 (cd $testroot/wt && got add foo > /dev/null)
34 echo ' M alpha' > $testroot/stdout.expected
35 echo ' D beta' >> $testroot/stdout.expected
36 echo ' A foo' >> $testroot/stdout.expected
37 (cd $testroot/wt && got stage alpha beta foo > /dev/null)
39 (cd $testroot/wt && got unstage > $testroot/stdout)
41 if [ "$ret" != "0" ]; then
42 echo "got stage command succeeded unexpectedly" >&2
43 test_done "$testroot" "1"
47 echo 'G alpha' > $testroot/stdout.expected
48 echo 'D beta' >> $testroot/stdout.expected
49 echo 'G foo' >> $testroot/stdout.expected
50 cmp -s $testroot/stdout.expected $testroot/stdout
52 if [ "$ret" != "0" ]; then
53 diff -u $testroot/stdout.expected $testroot/stdout
54 test_done "$testroot" "$ret"
58 echo 'M alpha' > $testroot/stdout.expected
59 echo 'D beta' >> $testroot/stdout.expected
60 echo 'A foo' >> $testroot/stdout.expected
61 (cd $testroot/wt && got status > $testroot/stdout)
62 cmp -s $testroot/stdout.expected $testroot/stdout
64 if [ "$ret" != "0" ]; then
65 diff -u $testroot/stdout.expected $testroot/stdout
67 test_done "$testroot" "$ret"
70 function test_unstage_patch {
71 local testroot=`test_init unstage_patch`
73 jot 16 > $testroot/repo/numbers
74 (cd $testroot/repo && git add numbers)
75 git_commit $testroot/repo -m "added numbers file"
76 local commit_id=`git_show_head $testroot/repo`
78 got checkout $testroot/repo $testroot/wt > /dev/null
80 if [ "$ret" != "0" ]; then
81 test_done "$testroot" "$ret"
85 sed -i -e 's/^2$/a/' $testroot/wt/numbers
86 sed -i -e 's/^7$/b/' $testroot/wt/numbers
87 sed -i -e 's/^16$/c/' $testroot/wt/numbers
89 (cd $testroot/wt && got stage > /dev/null)
91 if [ "$ret" != "0" ]; then
92 echo "got stage command failed unexpectedly" >&2
93 test_done "$testroot" "1"
97 # don't unstage any hunks
98 printf "n\nn\nn\n" > $testroot/patchscript
99 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
100 numbers > $testroot/stdout)
102 if [ "$ret" != "0" ]; then
103 echo "got stage command failed unexpectedly" >&2
104 test_done "$testroot" "1"
107 cat > $testroot/stdout.expected <<EOF
108 -----------------------------------------------
116 -----------------------------------------------
117 M numbers (change 1 of 3)
118 unstage this change? [y/n/q] n
119 -----------------------------------------------
129 -----------------------------------------------
130 M numbers (change 2 of 3)
131 unstage this change? [y/n/q] n
132 -----------------------------------------------
139 -----------------------------------------------
140 M numbers (change 3 of 3)
141 unstage this change? [y/n/q] n
143 cmp -s $testroot/stdout.expected $testroot/stdout
145 if [ "$ret" != "0" ]; then
146 diff -u $testroot/stdout.expected $testroot/stdout
147 test_done "$testroot" "$ret"
151 (cd $testroot/wt && got status > $testroot/stdout)
152 echo " M numbers" > $testroot/stdout.expected
153 cmp -s $testroot/stdout.expected $testroot/stdout
155 if [ "$ret" != "0" ]; then
156 diff -u $testroot/stdout.expected $testroot/stdout
157 test_done "$testroot" "$ret"
161 # unstage middle hunk
162 printf "n\ny\nn\n" > $testroot/patchscript
163 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
164 numbers > $testroot/stdout)
166 cat > $testroot/stdout.expected <<EOF
167 -----------------------------------------------
175 -----------------------------------------------
176 M numbers (change 1 of 3)
177 unstage this change? [y/n/q] n
178 -----------------------------------------------
188 -----------------------------------------------
189 M numbers (change 2 of 3)
190 unstage this change? [y/n/q] y
191 -----------------------------------------------
198 -----------------------------------------------
199 M numbers (change 3 of 3)
200 unstage this change? [y/n/q] n
203 cmp -s $testroot/stdout.expected $testroot/stdout
205 if [ "$ret" != "0" ]; then
206 diff -u $testroot/stdout.expected $testroot/stdout
207 test_done "$testroot" "$ret"
211 (cd $testroot/wt && got status > $testroot/stdout)
212 echo "MM numbers" > $testroot/stdout.expected
213 cmp -s $testroot/stdout.expected $testroot/stdout
215 if [ "$ret" != "0" ]; then
216 diff -u $testroot/stdout.expected $testroot/stdout
217 test_done "$testroot" "$ret"
221 (cd $testroot/wt && got diff -s > $testroot/stdout)
223 echo "diff $commit_id $testroot/wt (staged changes)" \
224 > $testroot/stdout.expected
225 echo -n 'blob - ' >> $testroot/stdout.expected
226 got tree -r $testroot/repo -i -c $commit_id \
227 | grep 'numbers$' | cut -d' ' -f 1 \
228 >> $testroot/stdout.expected
229 echo -n 'blob + ' >> $testroot/stdout.expected
230 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
231 >> $testroot/stdout.expected
232 cat >> $testroot/stdout.expected <<EOF
249 cmp -s $testroot/stdout.expected $testroot/stdout
251 if [ "$ret" != "0" ]; then
252 diff -u $testroot/stdout.expected $testroot/stdout
253 test_done "$testroot" "$ret"
257 (cd $testroot/wt && got diff > $testroot/stdout)
258 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
259 echo -n 'blob - ' >> $testroot/stdout.expected
260 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
261 >> $testroot/stdout.expected
262 echo "file + numbers" >> $testroot/stdout.expected
263 cat >> $testroot/stdout.expected <<EOF
276 cmp -s $testroot/stdout.expected $testroot/stdout
278 if [ "$ret" != "0" ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
284 (cd $testroot/wt && got stage >/dev/null)
286 if [ "$ret" != "0" ]; then
287 echo "got stage command failed unexpectedly" >&2
288 test_done "$testroot" "1"
292 (cd $testroot/wt && got status > $testroot/stdout)
293 echo " M numbers" > $testroot/stdout.expected
294 cmp -s $testroot/stdout.expected $testroot/stdout
296 if [ "$ret" != "0" ]; then
297 diff -u $testroot/stdout.expected $testroot/stdout
298 test_done "$testroot" "$ret"
303 printf "n\nn\ny\n" > $testroot/patchscript
304 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
305 numbers > $testroot/stdout)
307 cat > $testroot/stdout.expected <<EOF
308 -----------------------------------------------
316 -----------------------------------------------
317 M numbers (change 1 of 3)
318 unstage this change? [y/n/q] n
319 -----------------------------------------------
329 -----------------------------------------------
330 M numbers (change 2 of 3)
331 unstage this change? [y/n/q] n
332 -----------------------------------------------
339 -----------------------------------------------
340 M numbers (change 3 of 3)
341 unstage this change? [y/n/q] y
344 cmp -s $testroot/stdout.expected $testroot/stdout
346 if [ "$ret" != "0" ]; then
347 diff -u $testroot/stdout.expected $testroot/stdout
348 test_done "$testroot" "$ret"
352 (cd $testroot/wt && got status > $testroot/stdout)
353 echo "MM numbers" > $testroot/stdout.expected
354 cmp -s $testroot/stdout.expected $testroot/stdout
356 if [ "$ret" != "0" ]; then
357 diff -u $testroot/stdout.expected $testroot/stdout
358 test_done "$testroot" "$ret"
362 (cd $testroot/wt && got diff -s > $testroot/stdout)
364 echo "diff $commit_id $testroot/wt (staged changes)" \
365 > $testroot/stdout.expected
366 echo -n 'blob - ' >> $testroot/stdout.expected
367 got tree -r $testroot/repo -i -c $commit_id \
368 | grep 'numbers$' | cut -d' ' -f 1 \
369 >> $testroot/stdout.expected
370 echo -n 'blob + ' >> $testroot/stdout.expected
371 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
372 >> $testroot/stdout.expected
373 cat >> $testroot/stdout.expected <<EOF
390 cmp -s $testroot/stdout.expected $testroot/stdout
392 if [ "$ret" != "0" ]; then
393 diff -u $testroot/stdout.expected $testroot/stdout
394 test_done "$testroot" "$ret"
398 (cd $testroot/wt && got diff > $testroot/stdout)
399 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
400 echo -n 'blob - ' >> $testroot/stdout.expected
401 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
402 >> $testroot/stdout.expected
403 echo "file + numbers" >> $testroot/stdout.expected
404 cat >> $testroot/stdout.expected <<EOF
414 cmp -s $testroot/stdout.expected $testroot/stdout
416 if [ "$ret" != "0" ]; then
417 diff -u $testroot/stdout.expected $testroot/stdout
418 test_done "$testroot" "$ret"
422 (cd $testroot/wt && got stage >/dev/null)
424 if [ "$ret" != "0" ]; then
425 echo "got stage command failed unexpectedly" >&2
426 test_done "$testroot" "1"
430 (cd $testroot/wt && got status > $testroot/stdout)
431 echo " M numbers" > $testroot/stdout.expected
432 cmp -s $testroot/stdout.expected $testroot/stdout
434 if [ "$ret" != "0" ]; then
435 diff -u $testroot/stdout.expected $testroot/stdout
436 test_done "$testroot" "$ret"
441 printf "y\ny\ny\n" > $testroot/patchscript
442 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
443 numbers > $testroot/stdout)
445 cat > $testroot/stdout.expected <<EOF
446 -----------------------------------------------
454 -----------------------------------------------
455 M numbers (change 1 of 3)
456 unstage this change? [y/n/q] y
457 -----------------------------------------------
467 -----------------------------------------------
468 M numbers (change 2 of 3)
469 unstage this change? [y/n/q] y
470 -----------------------------------------------
477 -----------------------------------------------
478 M numbers (change 3 of 3)
479 unstage this change? [y/n/q] y
482 cmp -s $testroot/stdout.expected $testroot/stdout
484 if [ "$ret" != "0" ]; then
485 diff -u $testroot/stdout.expected $testroot/stdout
486 test_done "$testroot" "$ret"
490 (cd $testroot/wt && got status > $testroot/stdout)
491 echo "M numbers" > $testroot/stdout.expected
492 cmp -s $testroot/stdout.expected $testroot/stdout
494 if [ "$ret" != "0" ]; then
495 diff -u $testroot/stdout.expected $testroot/stdout
496 test_done "$testroot" "$ret"
500 (cd $testroot/wt && got diff -s > $testroot/stdout)
501 echo -n > $testroot/stdout.expected
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"
510 (cd $testroot/wt && got diff > $testroot/stdout)
512 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
513 echo -n 'blob - ' >> $testroot/stdout.expected
514 got tree -r $testroot/repo -i -c $commit_id \
515 | grep 'numbers$' | cut -d' ' -f 1 \
516 >> $testroot/stdout.expected
517 echo 'file + numbers' >> $testroot/stdout.expected
518 cat >> $testroot/stdout.expected <<EOF
541 cmp -s $testroot/stdout.expected $testroot/stdout
543 if [ "$ret" != "0" ]; then
544 diff -u $testroot/stdout.expected $testroot/stdout
546 test_done "$testroot" "$ret"
550 function test_unstage_patch_added {
551 local testroot=`test_init unstage_patch_added`
552 local commit_id=`git_show_head $testroot/repo`
554 got checkout $testroot/repo $testroot/wt > /dev/null
556 if [ "$ret" != "0" ]; then
557 test_done "$testroot" "$ret"
561 echo "new" > $testroot/wt/epsilon/new
562 (cd $testroot/wt && got add epsilon/new > /dev/null)
564 (cd $testroot/wt && got stage > /dev/null)
566 printf "y\n" > $testroot/patchscript
567 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
568 epsilon/new > $testroot/stdout)
570 echo "A epsilon/new" > $testroot/stdout.expected
571 echo "unstage this addition? [y/n] y" >> $testroot/stdout.expected
572 echo "G epsilon/new" >> $testroot/stdout.expected
573 cmp -s $testroot/stdout.expected $testroot/stdout
575 if [ "$ret" != "0" ]; then
576 diff -u $testroot/stdout.expected $testroot/stdout
577 test_done "$testroot" "$ret"
581 (cd $testroot/wt && got status > $testroot/stdout)
582 echo "A epsilon/new" > $testroot/stdout.expected
583 cmp -s $testroot/stdout.expected $testroot/stdout
585 if [ "$ret" != "0" ]; then
586 diff -u $testroot/stdout.expected $testroot/stdout
587 test_done "$testroot" "$ret"
591 (cd $testroot/wt && got diff -s > $testroot/stdout)
592 echo -n > $testroot/stdout.expected
593 cmp -s $testroot/stdout.expected $testroot/stdout
595 if [ "$ret" != "0" ]; then
596 diff -u $testroot/stdout.expected $testroot/stdout
597 test_done "$testroot" "$ret"
601 (cd $testroot/wt && got diff > $testroot/stdout)
603 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
604 echo 'blob - /dev/null' >> $testroot/stdout.expected
605 echo 'file + epsilon/new' >> $testroot/stdout.expected
606 echo "--- epsilon/new" >> $testroot/stdout.expected
607 echo "+++ epsilon/new" >> $testroot/stdout.expected
608 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
609 echo "+new" >> $testroot/stdout.expected
610 cmp -s $testroot/stdout.expected $testroot/stdout
612 if [ "$ret" != "0" ]; then
613 diff -u $testroot/stdout.expected $testroot/stdout
615 test_done "$testroot" "$ret"
618 function test_unstage_patch_removed {
619 local testroot=`test_init unstage_patch_removed`
620 local commit_id=`git_show_head $testroot/repo`
622 got checkout $testroot/repo $testroot/wt > /dev/null
624 if [ "$ret" != "0" ]; then
625 test_done "$testroot" "$ret"
629 (cd $testroot/wt && got rm beta > /dev/null)
630 (cd $testroot/wt && got stage > /dev/null)
632 printf "y\n" > $testroot/patchscript
633 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
634 beta > $testroot/stdout)
636 echo "D beta" > $testroot/stdout.expected
637 echo "unstage this deletion? [y/n] y" >> $testroot/stdout.expected
638 echo "D beta" >> $testroot/stdout.expected
639 cmp -s $testroot/stdout.expected $testroot/stdout
641 if [ "$ret" != "0" ]; then
642 diff -u $testroot/stdout.expected $testroot/stdout
643 test_done "$testroot" "$ret"
647 (cd $testroot/wt && got status > $testroot/stdout)
648 echo "D beta" > $testroot/stdout.expected
649 cmp -s $testroot/stdout.expected $testroot/stdout
651 if [ "$ret" != "0" ]; then
652 diff -u $testroot/stdout.expected $testroot/stdout
653 test_done "$testroot" "$ret"
657 (cd $testroot/wt && got diff -s > $testroot/stdout)
658 echo -n > $testroot/stdout.expected
659 cmp -s $testroot/stdout.expected $testroot/stdout
661 if [ "$ret" != "0" ]; then
662 diff -u $testroot/stdout.expected $testroot/stdout
663 test_done "$testroot" "$ret"
667 (cd $testroot/wt && got diff > $testroot/stdout)
669 echo "diff $commit_id $testroot/wt" \
670 > $testroot/stdout.expected
671 echo -n 'blob - ' >> $testroot/stdout.expected
672 got tree -r $testroot/repo -i | grep 'beta$' | cut -d' ' -f 1 \
673 >> $testroot/stdout.expected
674 echo 'file + /dev/null' >> $testroot/stdout.expected
675 echo "--- beta" >> $testroot/stdout.expected
676 echo "+++ beta" >> $testroot/stdout.expected
677 echo "@@ -1 +0,0 @@" >> $testroot/stdout.expected
678 echo "-beta" >> $testroot/stdout.expected
679 cmp -s $testroot/stdout.expected $testroot/stdout
681 if [ "$ret" != "0" ]; then
682 diff -u $testroot/stdout.expected $testroot/stdout
684 test_done "$testroot" "$ret"
687 function test_unstage_patch_quit {
688 local testroot=`test_init unstage_patch_quit`
690 jot 16 > $testroot/repo/numbers
691 echo zzz > $testroot/repo/zzz
692 (cd $testroot/repo && git add numbers zzz)
693 git_commit $testroot/repo -m "added files"
694 local commit_id=`git_show_head $testroot/repo`
696 got checkout $testroot/repo $testroot/wt > /dev/null
698 if [ "$ret" != "0" ]; then
699 test_done "$testroot" "$ret"
703 sed -i -e 's/^2$/a/' $testroot/wt/numbers
704 sed -i -e 's/^7$/b/' $testroot/wt/numbers
705 sed -i -e 's/^16$/c/' $testroot/wt/numbers
706 (cd $testroot/wt && got rm zzz > /dev/null)
707 (cd $testroot/wt && got stage > /dev/null)
709 # unstage first hunk and quit; and don't pass a path argument to
710 # ensure that we don't skip asking about the 'zzz' file after 'quit'
711 printf "y\nq\nn\n" > $testroot/patchscript
712 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
715 if [ "$ret" != "0" ]; then
716 echo "got stage command failed unexpectedly" >&2
717 test_done "$testroot" "1"
720 cat > $testroot/stdout.expected <<EOF
721 -----------------------------------------------
729 -----------------------------------------------
730 M numbers (change 1 of 3)
731 unstage this change? [y/n/q] y
732 -----------------------------------------------
742 -----------------------------------------------
743 M numbers (change 2 of 3)
744 unstage this change? [y/n/q] q
747 unstage this deletion? [y/n] n
749 cmp -s $testroot/stdout.expected $testroot/stdout
751 if [ "$ret" != "0" ]; then
752 diff -u $testroot/stdout.expected $testroot/stdout
753 test_done "$testroot" "$ret"
757 (cd $testroot/wt && got status > $testroot/stdout)
758 echo "MM numbers" > $testroot/stdout.expected
759 echo " D zzz" >> $testroot/stdout.expected
760 cmp -s $testroot/stdout.expected $testroot/stdout
762 if [ "$ret" != "0" ]; then
763 diff -u $testroot/stdout.expected $testroot/stdout
764 test_done "$testroot" "$ret"
768 (cd $testroot/wt && got diff > $testroot/stdout)
770 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
771 echo -n 'blob - ' >> $testroot/stdout.expected
772 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
773 >> $testroot/stdout.expected
774 echo "file + numbers" >> $testroot/stdout.expected
775 echo "--- numbers" >> $testroot/stdout.expected
776 echo "+++ numbers" >> $testroot/stdout.expected
777 echo "@@ -1,5 +1,5 @@" >> $testroot/stdout.expected
778 echo " 1" >> $testroot/stdout.expected
779 echo "-2" >> $testroot/stdout.expected
780 echo "+a" >> $testroot/stdout.expected
781 echo " 3" >> $testroot/stdout.expected
782 echo " 4" >> $testroot/stdout.expected
783 echo " 5" >> $testroot/stdout.expected
784 cmp -s $testroot/stdout.expected $testroot/stdout
786 if [ "$ret" != "0" ]; then
787 diff -u $testroot/stdout.expected $testroot/stdout
788 test_done "$testroot" "$ret"
792 (cd $testroot/wt && got diff -s > $testroot/stdout)
793 echo "diff $commit_id $testroot/wt (staged changes)" \
794 > $testroot/stdout.expected
795 echo -n 'blob - ' >> $testroot/stdout.expected
796 got tree -r $testroot/repo -i -c $commit_id \
797 | grep 'numbers$' | cut -d' ' -f 1 \
798 >> $testroot/stdout.expected
799 echo -n 'blob + ' >> $testroot/stdout.expected
800 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
801 >> $testroot/stdout.expected
802 cat >> $testroot/stdout.expected <<EOF
821 echo -n 'blob - ' >> $testroot/stdout.expected
822 got tree -r $testroot/repo -i | grep 'zzz$' | cut -d' ' -f 1 \
823 >> $testroot/stdout.expected
824 echo 'blob + /dev/null' >> $testroot/stdout.expected
825 echo "--- zzz" >> $testroot/stdout.expected
826 echo "+++ /dev/null" >> $testroot/stdout.expected
827 echo "@@ -1 +0,0 @@" >> $testroot/stdout.expected
828 echo "-zzz" >> $testroot/stdout.expected
829 cmp -s $testroot/stdout.expected $testroot/stdout
831 if [ "$ret" != "0" ]; then
832 diff -u $testroot/stdout.expected $testroot/stdout
834 test_done "$testroot" "$ret"
837 run_test test_unstage_basic
838 run_test test_unstage_patch
839 run_test test_unstage_patch_added
840 run_test test_unstage_patch_removed
841 run_test test_unstage_patch_quit