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_unstage_basic {
20 local testroot=`test_init unstage_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 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)
40 ret="$?"
41 if [ "$ret" != "0" ]; then
42 echo "got unstage command failed unexpectedly" >&2
43 test_done "$testroot" "1"
44 return 1
45 fi
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
51 ret="$?"
52 if [ "$ret" != "0" ]; then
53 diff -u $testroot/stdout.expected $testroot/stdout
54 test_done "$testroot" "$ret"
55 return 1
56 fi
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
63 ret="$?"
64 if [ "$ret" != "0" ]; then
65 diff -u $testroot/stdout.expected $testroot/stdout
66 fi
67 test_done "$testroot" "$ret"
68 }
70 function test_unstage_unversioned {
71 local testroot=`test_init unstage_unversioned`
73 got checkout $testroot/repo $testroot/wt > /dev/null
74 ret="$?"
75 if [ "$ret" != "0" ]; then
76 test_done "$testroot" "$ret"
77 return 1
78 fi
80 echo "modified file" > $testroot/wt/alpha
81 (cd $testroot/wt && got rm beta > /dev/null)
82 echo "new file" > $testroot/wt/foo
83 (cd $testroot/wt && got add foo > /dev/null)
85 echo ' M alpha' > $testroot/stdout.expected
86 echo ' D beta' >> $testroot/stdout.expected
87 echo ' A foo' >> $testroot/stdout.expected
88 (cd $testroot/wt && got stage > /dev/null)
90 touch $testroot/wt/unversioned-file
92 (cd $testroot/wt && got status > $testroot/stdout)
93 echo ' M alpha' > $testroot/stdout.expected
94 echo ' D beta' >> $testroot/stdout.expected
95 echo ' A foo' >> $testroot/stdout.expected
96 echo "? unversioned-file" >> $testroot/stdout.expected
97 cmp -s $testroot/stdout.expected $testroot/stdout
98 ret="$?"
99 if [ "$ret" != "0" ]; then
100 diff -u $testroot/stdout.expected $testroot/stdout
101 test_done "$testroot" "$ret"
102 return 1
103 fi
105 (cd $testroot/wt && got unstage > $testroot/stdout)
106 ret="$?"
107 if [ "$ret" != "0" ]; then
108 echo "got unstage command failed unexpectedly" >&2
109 test_done "$testroot" "1"
110 return 1
111 fi
113 echo 'G alpha' > $testroot/stdout.expected
114 echo 'D beta' >> $testroot/stdout.expected
115 echo 'G foo' >> $testroot/stdout.expected
116 cmp -s $testroot/stdout.expected $testroot/stdout
117 ret="$?"
118 if [ "$ret" != "0" ]; then
119 diff -u $testroot/stdout.expected $testroot/stdout
120 test_done "$testroot" "$ret"
121 return 1
122 fi
124 (cd $testroot/wt && got stage > /dev/null)
126 # unstaging an unversioned path is a no-op
127 (cd $testroot/wt && got unstage unversioned > $testroot/stdout)
128 ret="$?"
129 if [ "$ret" != "0" ]; then
130 echo "got unstage command failed unexpectedly" >&2
131 test_done "$testroot" "$ret"
132 return 1
133 fi
135 echo ' M alpha' > $testroot/stdout.expected
136 echo ' D beta' >> $testroot/stdout.expected
137 echo ' A foo' >> $testroot/stdout.expected
138 echo "? unversioned-file" >> $testroot/stdout.expected
139 (cd $testroot/wt && got status > $testroot/stdout)
140 cmp -s $testroot/stdout.expected $testroot/stdout
141 ret="$?"
142 if [ "$ret" != "0" ]; then
143 diff -u $testroot/stdout.expected $testroot/stdout
144 fi
145 test_done "$testroot" "$ret"
148 function test_unstage_nonexistent {
149 local testroot=`test_init unstage_nonexistent`
151 got checkout $testroot/repo $testroot/wt > /dev/null
152 ret="$?"
153 if [ "$ret" != "0" ]; then
154 test_done "$testroot" "$ret"
155 return 1
156 fi
158 echo "modified file" > $testroot/wt/alpha
159 (cd $testroot/wt && got rm beta > /dev/null)
160 echo "new file" > $testroot/wt/foo
161 (cd $testroot/wt && got add foo > /dev/null)
163 echo ' M alpha' > $testroot/stdout.expected
164 echo ' D beta' >> $testroot/stdout.expected
165 echo ' A foo' >> $testroot/stdout.expected
166 (cd $testroot/wt && got stage > /dev/null)
168 # unstaging a non-existent file is a no-op
169 (cd $testroot/wt && got unstage nonexistent-file > $testroot/stdout)
170 ret="$?"
171 if [ "$ret" != "0" ]; then
172 echo "got unstage command failed unexpectedly" >&2
173 test_done "$testroot" "1"
174 return 1
175 fi
177 echo -n > $testroot/stdout.expected
178 cmp -s $testroot/stdout.expected $testroot/stdout
179 ret="$?"
180 if [ "$ret" != "0" ]; then
181 diff -u $testroot/stdout.expected $testroot/stdout
182 fi
183 test_done "$testroot" "$ret"
186 function test_unstage_patch {
187 local testroot=`test_init unstage_patch`
189 jot 16 > $testroot/repo/numbers
190 (cd $testroot/repo && git add numbers)
191 git_commit $testroot/repo -m "added numbers file"
192 local commit_id=`git_show_head $testroot/repo`
194 got checkout $testroot/repo $testroot/wt > /dev/null
195 ret="$?"
196 if [ "$ret" != "0" ]; then
197 test_done "$testroot" "$ret"
198 return 1
199 fi
201 sed -i -e 's/^2$/a/' $testroot/wt/numbers
202 sed -i -e 's/^7$/b/' $testroot/wt/numbers
203 sed -i -e 's/^16$/c/' $testroot/wt/numbers
205 (cd $testroot/wt && got stage > /dev/null)
206 ret="$?"
207 if [ "$ret" != "0" ]; then
208 echo "got stage command failed unexpectedly" >&2
209 test_done "$testroot" "1"
210 return 1
211 fi
213 # don't unstage any hunks
214 printf "n\nn\nn\n" > $testroot/patchscript
215 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
216 numbers > $testroot/stdout)
217 ret="$?"
218 if [ "$ret" != "0" ]; then
219 echo "got unstage command failed unexpectedly" >&2
220 test_done "$testroot" "1"
221 return 1
222 fi
223 cat > $testroot/stdout.expected <<EOF
224 -----------------------------------------------
225 @@ -1,5 +1,5 @@
227 -2
228 +a
232 -----------------------------------------------
233 M numbers (change 1 of 3)
234 unstage this change? [y/n/q] n
235 -----------------------------------------------
236 @@ -4,7 +4,7 @@
240 -7
241 +b
244 10
245 -----------------------------------------------
246 M numbers (change 2 of 3)
247 unstage this change? [y/n/q] n
248 -----------------------------------------------
249 @@ -13,4 +13,4 @@
250 13
251 14
252 15
253 -16
254 +c
255 -----------------------------------------------
256 M numbers (change 3 of 3)
257 unstage this change? [y/n/q] n
258 EOF
259 cmp -s $testroot/stdout.expected $testroot/stdout
260 ret="$?"
261 if [ "$ret" != "0" ]; then
262 diff -u $testroot/stdout.expected $testroot/stdout
263 test_done "$testroot" "$ret"
264 return 1
265 fi
267 (cd $testroot/wt && got status > $testroot/stdout)
268 echo " M numbers" > $testroot/stdout.expected
269 cmp -s $testroot/stdout.expected $testroot/stdout
270 ret="$?"
271 if [ "$ret" != "0" ]; then
272 diff -u $testroot/stdout.expected $testroot/stdout
273 test_done "$testroot" "$ret"
274 return 1
275 fi
277 # unstage middle hunk
278 printf "n\ny\nn\n" > $testroot/patchscript
279 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
280 numbers > $testroot/stdout)
282 cat > $testroot/stdout.expected <<EOF
283 -----------------------------------------------
284 @@ -1,5 +1,5 @@
286 -2
287 +a
291 -----------------------------------------------
292 M numbers (change 1 of 3)
293 unstage this change? [y/n/q] n
294 -----------------------------------------------
295 @@ -4,7 +4,7 @@
299 -7
300 +b
303 10
304 -----------------------------------------------
305 M numbers (change 2 of 3)
306 unstage this change? [y/n/q] y
307 -----------------------------------------------
308 @@ -13,4 +13,4 @@
309 13
310 14
311 15
312 -16
313 +c
314 -----------------------------------------------
315 M numbers (change 3 of 3)
316 unstage this change? [y/n/q] n
317 G numbers
318 EOF
319 cmp -s $testroot/stdout.expected $testroot/stdout
320 ret="$?"
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stdout.expected $testroot/stdout
323 test_done "$testroot" "$ret"
324 return 1
325 fi
327 (cd $testroot/wt && got status > $testroot/stdout)
328 echo "MM numbers" > $testroot/stdout.expected
329 cmp -s $testroot/stdout.expected $testroot/stdout
330 ret="$?"
331 if [ "$ret" != "0" ]; then
332 diff -u $testroot/stdout.expected $testroot/stdout
333 test_done "$testroot" "$ret"
334 return 1
335 fi
337 (cd $testroot/wt && got diff -s > $testroot/stdout)
339 echo "diff $commit_id $testroot/wt (staged changes)" \
340 > $testroot/stdout.expected
341 echo -n 'blob - ' >> $testroot/stdout.expected
342 got tree -r $testroot/repo -i -c $commit_id \
343 | grep 'numbers$' | cut -d' ' -f 1 \
344 >> $testroot/stdout.expected
345 echo -n 'blob + ' >> $testroot/stdout.expected
346 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
347 >> $testroot/stdout.expected
348 cat >> $testroot/stdout.expected <<EOF
349 --- numbers
350 +++ numbers
351 @@ -1,5 +1,5 @@
353 -2
354 +a
358 @@ -13,4 +13,4 @@
359 13
360 14
361 15
362 -16
363 +c
364 EOF
365 cmp -s $testroot/stdout.expected $testroot/stdout
366 ret="$?"
367 if [ "$ret" != "0" ]; then
368 diff -u $testroot/stdout.expected $testroot/stdout
369 test_done "$testroot" "$ret"
370 return 1
371 fi
373 (cd $testroot/wt && got diff > $testroot/stdout)
374 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
375 echo -n 'blob - ' >> $testroot/stdout.expected
376 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
377 tr -d '\n' >> $testroot/stdout.expected
378 echo " (staged)" >> $testroot/stdout.expected
379 echo "file + numbers" >> $testroot/stdout.expected
380 cat >> $testroot/stdout.expected <<EOF
381 --- numbers
382 +++ numbers
383 @@ -4,7 +4,7 @@ a
387 -7
388 +b
391 10
392 EOF
393 cmp -s $testroot/stdout.expected $testroot/stdout
394 ret="$?"
395 if [ "$ret" != "0" ]; then
396 diff -u $testroot/stdout.expected $testroot/stdout
397 test_done "$testroot" "$ret"
398 return 1
399 fi
401 (cd $testroot/wt && got stage >/dev/null)
402 ret="$?"
403 if [ "$ret" != "0" ]; then
404 echo "got stage command failed unexpectedly" >&2
405 test_done "$testroot" "1"
406 return 1
407 fi
409 (cd $testroot/wt && got status > $testroot/stdout)
410 echo " M numbers" > $testroot/stdout.expected
411 cmp -s $testroot/stdout.expected $testroot/stdout
412 ret="$?"
413 if [ "$ret" != "0" ]; then
414 diff -u $testroot/stdout.expected $testroot/stdout
415 test_done "$testroot" "$ret"
416 return 1
417 fi
419 # unstage last hunk
420 printf "n\nn\ny\n" > $testroot/patchscript
421 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
422 numbers > $testroot/stdout)
424 cat > $testroot/stdout.expected <<EOF
425 -----------------------------------------------
426 @@ -1,5 +1,5 @@
428 -2
429 +a
433 -----------------------------------------------
434 M numbers (change 1 of 3)
435 unstage this change? [y/n/q] n
436 -----------------------------------------------
437 @@ -4,7 +4,7 @@
441 -7
442 +b
445 10
446 -----------------------------------------------
447 M numbers (change 2 of 3)
448 unstage this change? [y/n/q] n
449 -----------------------------------------------
450 @@ -13,4 +13,4 @@
451 13
452 14
453 15
454 -16
455 +c
456 -----------------------------------------------
457 M numbers (change 3 of 3)
458 unstage this change? [y/n/q] y
459 G numbers
460 EOF
461 cmp -s $testroot/stdout.expected $testroot/stdout
462 ret="$?"
463 if [ "$ret" != "0" ]; then
464 diff -u $testroot/stdout.expected $testroot/stdout
465 test_done "$testroot" "$ret"
466 return 1
467 fi
469 (cd $testroot/wt && got status > $testroot/stdout)
470 echo "MM numbers" > $testroot/stdout.expected
471 cmp -s $testroot/stdout.expected $testroot/stdout
472 ret="$?"
473 if [ "$ret" != "0" ]; then
474 diff -u $testroot/stdout.expected $testroot/stdout
475 test_done "$testroot" "$ret"
476 return 1
477 fi
479 (cd $testroot/wt && got diff -s > $testroot/stdout)
481 echo "diff $commit_id $testroot/wt (staged changes)" \
482 > $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 -n 'blob + ' >> $testroot/stdout.expected
488 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
489 >> $testroot/stdout.expected
490 cat >> $testroot/stdout.expected <<EOF
491 --- numbers
492 +++ numbers
493 @@ -1,10 +1,10 @@
495 -2
496 +a
501 -7
502 +b
505 10
506 EOF
507 cmp -s $testroot/stdout.expected $testroot/stdout
508 ret="$?"
509 if [ "$ret" != "0" ]; then
510 diff -u $testroot/stdout.expected $testroot/stdout
511 test_done "$testroot" "$ret"
512 return 1
513 fi
515 (cd $testroot/wt && got diff > $testroot/stdout)
516 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
517 echo -n 'blob - ' >> $testroot/stdout.expected
518 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
519 tr -d '\n' >> $testroot/stdout.expected
520 echo " (staged)" >> $testroot/stdout.expected
521 echo "file + numbers" >> $testroot/stdout.expected
522 cat >> $testroot/stdout.expected <<EOF
523 --- numbers
524 +++ numbers
525 @@ -13,4 +13,4 @@ b
526 13
527 14
528 15
529 -16
530 +c
531 EOF
532 cmp -s $testroot/stdout.expected $testroot/stdout
533 ret="$?"
534 if [ "$ret" != "0" ]; then
535 diff -u $testroot/stdout.expected $testroot/stdout
536 test_done "$testroot" "$ret"
537 return 1
538 fi
540 (cd $testroot/wt && got stage >/dev/null)
541 ret="$?"
542 if [ "$ret" != "0" ]; then
543 echo "got stage command failed unexpectedly" >&2
544 test_done "$testroot" "1"
545 return 1
546 fi
548 (cd $testroot/wt && got status > $testroot/stdout)
549 echo " M numbers" > $testroot/stdout.expected
550 cmp -s $testroot/stdout.expected $testroot/stdout
551 ret="$?"
552 if [ "$ret" != "0" ]; then
553 diff -u $testroot/stdout.expected $testroot/stdout
554 test_done "$testroot" "$ret"
555 return 1
556 fi
558 # unstage all hunks
559 printf "y\ny\ny\n" > $testroot/patchscript
560 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
561 numbers > $testroot/stdout)
563 cat > $testroot/stdout.expected <<EOF
564 -----------------------------------------------
565 @@ -1,5 +1,5 @@
567 -2
568 +a
572 -----------------------------------------------
573 M numbers (change 1 of 3)
574 unstage this change? [y/n/q] y
575 -----------------------------------------------
576 @@ -4,7 +4,7 @@
580 -7
581 +b
584 10
585 -----------------------------------------------
586 M numbers (change 2 of 3)
587 unstage this change? [y/n/q] y
588 -----------------------------------------------
589 @@ -13,4 +13,4 @@
590 13
591 14
592 15
593 -16
594 +c
595 -----------------------------------------------
596 M numbers (change 3 of 3)
597 unstage this change? [y/n/q] y
598 G numbers
599 EOF
600 cmp -s $testroot/stdout.expected $testroot/stdout
601 ret="$?"
602 if [ "$ret" != "0" ]; then
603 diff -u $testroot/stdout.expected $testroot/stdout
604 test_done "$testroot" "$ret"
605 return 1
606 fi
608 (cd $testroot/wt && got status > $testroot/stdout)
609 echo "M numbers" > $testroot/stdout.expected
610 cmp -s $testroot/stdout.expected $testroot/stdout
611 ret="$?"
612 if [ "$ret" != "0" ]; then
613 diff -u $testroot/stdout.expected $testroot/stdout
614 test_done "$testroot" "$ret"
615 return 1
616 fi
618 (cd $testroot/wt && got diff -s > $testroot/stdout)
619 echo -n > $testroot/stdout.expected
620 cmp -s $testroot/stdout.expected $testroot/stdout
621 ret="$?"
622 if [ "$ret" != "0" ]; then
623 diff -u $testroot/stdout.expected $testroot/stdout
624 test_done "$testroot" "$ret"
625 return 1
626 fi
628 (cd $testroot/wt && got diff > $testroot/stdout)
630 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
631 echo -n 'blob - ' >> $testroot/stdout.expected
632 got tree -r $testroot/repo -i -c $commit_id \
633 | grep 'numbers$' | cut -d' ' -f 1 \
634 >> $testroot/stdout.expected
635 echo 'file + numbers' >> $testroot/stdout.expected
636 cat >> $testroot/stdout.expected <<EOF
637 --- numbers
638 +++ numbers
639 @@ -1,10 +1,10 @@
641 -2
642 +a
647 -7
648 +b
651 10
652 @@ -13,4 +13,4 @@
653 13
654 14
655 15
656 -16
657 +c
658 EOF
659 cmp -s $testroot/stdout.expected $testroot/stdout
660 ret="$?"
661 if [ "$ret" != "0" ]; then
662 diff -u $testroot/stdout.expected $testroot/stdout
663 fi
664 test_done "$testroot" "$ret"
668 function test_unstage_patch_added {
669 local testroot=`test_init unstage_patch_added`
670 local commit_id=`git_show_head $testroot/repo`
672 got checkout $testroot/repo $testroot/wt > /dev/null
673 ret="$?"
674 if [ "$ret" != "0" ]; then
675 test_done "$testroot" "$ret"
676 return 1
677 fi
679 echo "new" > $testroot/wt/epsilon/new
680 (cd $testroot/wt && got add epsilon/new > /dev/null)
682 (cd $testroot/wt && got stage > /dev/null)
684 printf "y\n" > $testroot/patchscript
685 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
686 epsilon/new > $testroot/stdout)
688 echo "A epsilon/new" > $testroot/stdout.expected
689 echo "unstage this addition? [y/n] y" >> $testroot/stdout.expected
690 echo "G epsilon/new" >> $testroot/stdout.expected
691 cmp -s $testroot/stdout.expected $testroot/stdout
692 ret="$?"
693 if [ "$ret" != "0" ]; then
694 diff -u $testroot/stdout.expected $testroot/stdout
695 test_done "$testroot" "$ret"
696 return 1
697 fi
699 (cd $testroot/wt && got status > $testroot/stdout)
700 echo "A epsilon/new" > $testroot/stdout.expected
701 cmp -s $testroot/stdout.expected $testroot/stdout
702 ret="$?"
703 if [ "$ret" != "0" ]; then
704 diff -u $testroot/stdout.expected $testroot/stdout
705 test_done "$testroot" "$ret"
706 return 1
707 fi
709 (cd $testroot/wt && got diff -s > $testroot/stdout)
710 echo -n > $testroot/stdout.expected
711 cmp -s $testroot/stdout.expected $testroot/stdout
712 ret="$?"
713 if [ "$ret" != "0" ]; then
714 diff -u $testroot/stdout.expected $testroot/stdout
715 test_done "$testroot" "$ret"
716 return 1
717 fi
719 (cd $testroot/wt && got diff > $testroot/stdout)
721 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
722 echo 'blob - /dev/null' >> $testroot/stdout.expected
723 echo 'file + epsilon/new' >> $testroot/stdout.expected
724 echo "--- epsilon/new" >> $testroot/stdout.expected
725 echo "+++ epsilon/new" >> $testroot/stdout.expected
726 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
727 echo "+new" >> $testroot/stdout.expected
728 cmp -s $testroot/stdout.expected $testroot/stdout
729 ret="$?"
730 if [ "$ret" != "0" ]; then
731 diff -u $testroot/stdout.expected $testroot/stdout
732 fi
733 test_done "$testroot" "$ret"
736 function test_unstage_patch_removed {
737 local testroot=`test_init unstage_patch_removed`
738 local commit_id=`git_show_head $testroot/repo`
740 got checkout $testroot/repo $testroot/wt > /dev/null
741 ret="$?"
742 if [ "$ret" != "0" ]; then
743 test_done "$testroot" "$ret"
744 return 1
745 fi
747 (cd $testroot/wt && got rm beta > /dev/null)
748 (cd $testroot/wt && got stage > /dev/null)
750 printf "y\n" > $testroot/patchscript
751 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
752 beta > $testroot/stdout)
754 echo "D beta" > $testroot/stdout.expected
755 echo "unstage this deletion? [y/n] y" >> $testroot/stdout.expected
756 echo "D beta" >> $testroot/stdout.expected
757 cmp -s $testroot/stdout.expected $testroot/stdout
758 ret="$?"
759 if [ "$ret" != "0" ]; then
760 diff -u $testroot/stdout.expected $testroot/stdout
761 test_done "$testroot" "$ret"
762 return 1
763 fi
765 (cd $testroot/wt && got status > $testroot/stdout)
766 echo "D beta" > $testroot/stdout.expected
767 cmp -s $testroot/stdout.expected $testroot/stdout
768 ret="$?"
769 if [ "$ret" != "0" ]; then
770 diff -u $testroot/stdout.expected $testroot/stdout
771 test_done "$testroot" "$ret"
772 return 1
773 fi
775 (cd $testroot/wt && got diff -s > $testroot/stdout)
776 echo -n > $testroot/stdout.expected
777 cmp -s $testroot/stdout.expected $testroot/stdout
778 ret="$?"
779 if [ "$ret" != "0" ]; then
780 diff -u $testroot/stdout.expected $testroot/stdout
781 test_done "$testroot" "$ret"
782 return 1
783 fi
785 (cd $testroot/wt && got diff > $testroot/stdout)
787 echo "diff $commit_id $testroot/wt" \
788 > $testroot/stdout.expected
789 echo -n 'blob - ' >> $testroot/stdout.expected
790 got tree -r $testroot/repo -i | grep 'beta$' | cut -d' ' -f 1 \
791 >> $testroot/stdout.expected
792 echo 'file + /dev/null' >> $testroot/stdout.expected
793 echo "--- beta" >> $testroot/stdout.expected
794 echo "+++ beta" >> $testroot/stdout.expected
795 echo "@@ -1 +0,0 @@" >> $testroot/stdout.expected
796 echo "-beta" >> $testroot/stdout.expected
797 cmp -s $testroot/stdout.expected $testroot/stdout
798 ret="$?"
799 if [ "$ret" != "0" ]; then
800 diff -u $testroot/stdout.expected $testroot/stdout
801 fi
802 test_done "$testroot" "$ret"
805 function test_unstage_patch_quit {
806 local testroot=`test_init unstage_patch_quit`
808 jot 16 > $testroot/repo/numbers
809 echo zzz > $testroot/repo/zzz
810 (cd $testroot/repo && git add numbers zzz)
811 git_commit $testroot/repo -m "added files"
812 local commit_id=`git_show_head $testroot/repo`
814 got checkout $testroot/repo $testroot/wt > /dev/null
815 ret="$?"
816 if [ "$ret" != "0" ]; then
817 test_done "$testroot" "$ret"
818 return 1
819 fi
821 sed -i -e 's/^2$/a/' $testroot/wt/numbers
822 sed -i -e 's/^7$/b/' $testroot/wt/numbers
823 sed -i -e 's/^16$/c/' $testroot/wt/numbers
824 (cd $testroot/wt && got rm zzz > /dev/null)
825 (cd $testroot/wt && got stage > /dev/null)
827 # unstage first hunk and quit; and don't pass a path argument to
828 # ensure that we don't skip asking about the 'zzz' file after 'quit'
829 printf "y\nq\nn\n" > $testroot/patchscript
830 (cd $testroot/wt && got unstage -F $testroot/patchscript -p \
831 > $testroot/stdout)
832 ret="$?"
833 if [ "$ret" != "0" ]; then
834 echo "got unstage command failed unexpectedly" >&2
835 test_done "$testroot" "1"
836 return 1
837 fi
838 cat > $testroot/stdout.expected <<EOF
839 -----------------------------------------------
840 @@ -1,5 +1,5 @@
842 -2
843 +a
847 -----------------------------------------------
848 M numbers (change 1 of 3)
849 unstage this change? [y/n/q] y
850 -----------------------------------------------
851 @@ -4,7 +4,7 @@
855 -7
856 +b
859 10
860 -----------------------------------------------
861 M numbers (change 2 of 3)
862 unstage this change? [y/n/q] q
863 G numbers
864 D zzz
865 unstage this deletion? [y/n] n
866 EOF
867 cmp -s $testroot/stdout.expected $testroot/stdout
868 ret="$?"
869 if [ "$ret" != "0" ]; then
870 diff -u $testroot/stdout.expected $testroot/stdout
871 test_done "$testroot" "$ret"
872 return 1
873 fi
875 (cd $testroot/wt && got status > $testroot/stdout)
876 echo "MM numbers" > $testroot/stdout.expected
877 echo " D zzz" >> $testroot/stdout.expected
878 cmp -s $testroot/stdout.expected $testroot/stdout
879 ret="$?"
880 if [ "$ret" != "0" ]; then
881 diff -u $testroot/stdout.expected $testroot/stdout
882 test_done "$testroot" "$ret"
883 return 1
884 fi
886 (cd $testroot/wt && got diff > $testroot/stdout)
888 echo "diff $commit_id $testroot/wt" > $testroot/stdout.expected
889 echo -n 'blob - ' >> $testroot/stdout.expected
890 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 | \
891 tr -d '\n' >> $testroot/stdout.expected
892 echo " (staged)" >> $testroot/stdout.expected
893 echo "file + numbers" >> $testroot/stdout.expected
894 echo "--- numbers" >> $testroot/stdout.expected
895 echo "+++ numbers" >> $testroot/stdout.expected
896 echo "@@ -1,5 +1,5 @@" >> $testroot/stdout.expected
897 echo " 1" >> $testroot/stdout.expected
898 echo "-2" >> $testroot/stdout.expected
899 echo "+a" >> $testroot/stdout.expected
900 echo " 3" >> $testroot/stdout.expected
901 echo " 4" >> $testroot/stdout.expected
902 echo " 5" >> $testroot/stdout.expected
903 cmp -s $testroot/stdout.expected $testroot/stdout
904 ret="$?"
905 if [ "$ret" != "0" ]; then
906 diff -u $testroot/stdout.expected $testroot/stdout
907 test_done "$testroot" "$ret"
908 return 1
909 fi
911 (cd $testroot/wt && got diff -s > $testroot/stdout)
912 echo "diff $commit_id $testroot/wt (staged changes)" \
913 > $testroot/stdout.expected
914 echo -n 'blob - ' >> $testroot/stdout.expected
915 got tree -r $testroot/repo -i -c $commit_id \
916 | grep 'numbers$' | cut -d' ' -f 1 \
917 >> $testroot/stdout.expected
918 echo -n 'blob + ' >> $testroot/stdout.expected
919 (cd $testroot/wt && got stage -l numbers) | cut -d' ' -f 1 \
920 >> $testroot/stdout.expected
921 cat >> $testroot/stdout.expected <<EOF
922 --- numbers
923 +++ numbers
924 @@ -4,7 +4,7 @@
928 -7
929 +b
932 10
933 @@ -13,4 +13,4 @@
934 13
935 14
936 15
937 -16
938 +c
939 EOF
940 echo -n 'blob - ' >> $testroot/stdout.expected
941 got tree -r $testroot/repo -i | grep 'zzz$' | cut -d' ' -f 1 \
942 >> $testroot/stdout.expected
943 echo 'blob + /dev/null' >> $testroot/stdout.expected
944 echo "--- zzz" >> $testroot/stdout.expected
945 echo "+++ /dev/null" >> $testroot/stdout.expected
946 echo "@@ -1 +0,0 @@" >> $testroot/stdout.expected
947 echo "-zzz" >> $testroot/stdout.expected
948 cmp -s $testroot/stdout.expected $testroot/stdout
949 ret="$?"
950 if [ "$ret" != "0" ]; then
951 diff -u $testroot/stdout.expected $testroot/stdout
952 fi
953 test_done "$testroot" "$ret"
956 run_test test_unstage_basic
957 run_test test_unstage_unversioned
958 run_test test_unstage_nonexistent
959 run_test test_unstage_patch
960 run_test test_unstage_patch_added
961 run_test test_unstage_patch_removed
962 run_test test_unstage_patch_quit