make got-read-gotconfig clear its imsgbuf before exit in an error case
[got-portable.git] / regress / cmdline / patch.sh
blobe5504f42e6a08f89598536b78ff4c5ed56bfb461
1 #!/bin/sh
3 # Copyright (c) 2022 Omar Polo <op@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.
17 . ./common.sh
19 test_patch_basic() {
20 local testroot=`test_init patch_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
23 ret=$?
24 if [ $ret -ne 0 ]; then
25 test_done $testroot $ret
26 return 1
29 seq 100 > $testroot/wt/numbers
30 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
31 >/dev/null
32 ret=$?
33 if [ $ret -ne 0 ]; then
34 test_done "$testroot" $ret
35 return 1
38 cat <<EOF > $testroot/wt/patch
39 --- alpha
40 +++ alpha
41 @@ -1 +1 @@
42 -alpha
43 +alpha is my favourite character
44 --- beta
45 +++ /dev/null
46 @@ -1 +0,0 @@
47 -beta
48 --- gamma/delta
49 +++ gamma/delta
50 @@ -1 +1,2 @@
51 +this is:
52 delta
53 --- /dev/null
54 +++ eta
55 @@ -0,0 +5,5 @@
61 --- numbers
62 +++ numbers
63 @@ -3,7 +3,7 @@
68 +six
72 @@ -57,7 +57,7 @@
76 -60
77 +sixty
81 @@ -98,3 +98,6 @@
84 100
85 +101
86 +102
87 +103
88 EOF
90 (cd $testroot/wt && got patch < patch) > $testroot/stdout
91 if [ $ret -ne 0 ]; then
92 test_done "$testroot" $ret
93 return 1
96 echo 'M alpha' > $testroot/stdout.expected
97 echo 'D beta' >> $testroot/stdout.expected
98 echo 'M gamma/delta' >> $testroot/stdout.expected
99 echo 'A eta' >> $testroot/stdout.expected
100 echo 'M numbers' >> $testroot/stdout.expected
102 cmp -s $testroot/stdout.expected $testroot/stdout
103 ret=$?
104 if [ $ret -ne 0 ]; then
105 diff -u $testroot/stdout.expected $testroot/stdout
106 test_done $testroot $ret
107 return 1
110 echo 'alpha is my favourite character' > $testroot/wt/alpha.expected
111 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
112 ret=$?
113 if [ $ret -ne 0 ]; then
114 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
115 test_done "$testroot" $ret
116 return 1
119 if [ -f "$testroot/wt/beta" ]; then
120 echo "beta was not deleted!" >&2
121 test_done "$testroot" 1
122 return 1
125 echo 'this is:' > $testroot/wt/gamma/delta.expected
126 echo 'delta' >> $testroot/wt/gamma/delta.expected
127 cmp -s $testroot/wt/gamma/delta.expected $testroot/wt/gamma/delta
128 ret=$?
129 if [ $ret -ne 0 ]; then
130 diff -u $testroot/wt/gamma/delta.expected $testroot/wt/gamma/delta
131 test_done "$testroot" $ret
132 return 1
135 seq 5 > $testroot/wt/eta.expected
136 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
137 ret=$?
138 if [ $ret -ne 0 ]; then
139 diff -u $testroot/wt/eta.expected $testroot/wt/eta
140 test_done "$testroot" $ret
141 return 1
144 seq 103 | sed -e 's/^6$/six/' -e 's/60/sixty/' \
145 > $testroot/wt/numbers.expected
146 cmp -s $testroot/wt/numbers.expected $testroot/wt/numbers
147 ret=$?
148 if [ $ret -ne 0 ]; then
149 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
151 test_done $testroot $ret
154 test_patch_dont_apply() {
155 local testroot=`test_init patch_dont_apply`
157 got checkout $testroot/repo $testroot/wt > /dev/null
158 ret=$?
159 if [ $ret -ne 0 ]; then
160 test_done $testroot $ret
161 return 1
164 seq 100 > $testroot/wt/numbers
165 (cd $testroot/wt && got add numbers && got commit -m 'add numbers') \
166 >/dev/null
167 ret=$?
168 if [ $ret -ne 0 ]; then
169 test_done $testroot $ret
170 return 1
173 cat <<EOF > $testroot/wt/patch
174 --- alpha
175 +++ alpha
176 @@ -1 +1,2 @@
177 +hatsuseno
178 alpha something
179 --- numbers
180 +++ /dev/null
181 @@ -1,9 +0,0 @@
193 (cd $testroot/wt && got patch patch) > $testroot/stdout 2> /dev/null
194 ret=$?
195 if [ $ret -eq 0 ]; then # should fail
196 test_done $testroot 1
197 return 1
200 cat <<EOF > $testroot/stdout.expected
201 # alpha
202 @@ -1,1 +1,2 @@ hunk failed to apply
203 # numbers
204 @@ -1,9 +0,0 @@ hunk failed to apply
205 Files where patch failed to apply: 2
208 cmp -s $testroot/stdout.expected $testroot/stdout
209 ret=$?
210 if [ $ret -ne 0 ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
213 test_done $testroot $ret
216 test_patch_malformed() {
217 local testroot=`test_init patch_malformed`
219 got checkout $testroot/repo $testroot/wt > /dev/null
220 ret=$?
221 if [ $ret -ne 0 ]; then
222 test_done $testroot $ret
223 return 1
226 # missing "@@"
227 cat <<EOF > $testroot/wt/patch
228 --- alpha
229 +++ alpha
230 @@ -1 +1,2
231 +hatsuseno
232 alpha
235 echo -n > $testroot/stdout.expected
236 echo "got-read-patch: @@ -1 +1,2: malformed patch" \
237 > $testroot/stderr.expected
238 echo "got: malformed patch" >> $testroot/stderr.expected
240 (cd $testroot/wt && got patch patch) \
241 > $testroot/stdout \
242 2> $testroot/stderr
243 ret=$?
244 if [ $ret -eq 0 ]; then
245 echo "got managed to apply an invalid patch"
246 test_done $testroot 1
247 return 1
250 cmp -s $testroot/stdout.expected $testroot/stdout
251 ret=$?
252 if [ $ret -ne 0 ]; then
253 diff -u $testroot/stdout.expected $testroot/stdout
254 test_done $testroot $ret
255 return 1
258 echo -n > $testroot/stdout.expected
259 echo "got-read-patch: @@ -1 +1,2: malformed patch" \
260 > $testroot/stderr.expected
261 echo "got: malformed patch" >> $testroot/stderr.expected
262 cmp -s $testroot/stderr.expected $testroot/stderr
263 ret=$?
264 if [ $ret -ne 0 ]; then
265 diff -u $testroot/stderr.expected $testroot/stderr
266 test_done $testroot $ret
267 return 1
270 # wrong first character
271 cat <<EOF > $testroot/wt/patch
272 --- alpha
273 +++ alpha
274 @@ -1 +1,2 @@
275 +hatsuseno
276 alpha
279 (cd $testroot/wt && got patch patch) \
280 > $testroot/stdout \
281 2> $testroot/stderr
282 ret=$?
283 if [ $ret -eq 0 ]; then
284 echo "got managed to apply an invalid patch"
285 test_done $testroot 1
286 return 1
289 cmp -s $testroot/stdout.expected $testroot/stdout
290 ret=$?
291 if [ $ret -ne 0 ]; then
292 diff -u $testroot/stdout.expected $testroot/stdout
293 test_done $testroot $ret
294 return 1
297 echo "got-read-patch: alpha: malformed patch" \
298 > $testroot/stderr.expected
299 echo "got: malformed patch" >> $testroot/stderr.expected
301 cmp -s $testroot/stderr.expected $testroot/stderr
302 ret=$?
303 if [ $ret -ne 0 ]; then
304 diff -u $testroot/stderr.expected $testroot/stderr
305 test_done $testroot $ret
306 return 1
309 # empty hunk
310 cat <<EOF > $testroot/wt/patch
311 diff --git a/alpha b/iota
312 --- a/alpha
313 +++ b/iota
314 @@ -0,0 +0,0 @@
317 (cd $testroot/wt && got patch patch) \
318 > $testroot/stdout \
319 2> $testroot/stderr
320 ret=$?
321 if [ $ret -eq 0 ]; then
322 echo "got managed to apply an invalid patch"
323 test_done $testroot 1
324 return 1
327 cmp -s $testroot/stdout.expected $testroot/stdout
328 ret=$?
329 if [ $ret -ne 0 ]; then
330 diff -u $testroot/stdout.expected $testroot/stdout
331 test_done $testroot $ret
332 return 1
335 echo "got-read-patch: @@ -0,0 +0,0 @@: malformed patch" \
336 > $testroot/stderr.expected
337 echo "got: malformed patch" >> $testroot/stderr.expected
339 cmp -s $testroot/stderr.expected $testroot/stderr
340 ret=$?
341 if [ $ret -ne 0 ]; then
342 diff -u $testroot/stderr.expected $testroot/stderr
343 test_done $testroot $ret
344 return 1
347 test_done $testroot $ret
350 test_patch_no_patch() {
351 local testroot=`test_init patch_no_patch`
353 got checkout $testroot/repo $testroot/wt > /dev/null
354 ret=$?
355 if [ $ret -ne 0 ]; then
356 test_done $testroot $ret
357 return 1
360 cat <<EOF > $testroot/wt/patch
361 hello world!
364 some other nonsense
367 there's no patch in here!
370 echo -n > $testroot/stdout.expected
371 echo "got-read-patch: no patch found" > $testroot/stderr.expected
372 echo "got: no patch found" >> $testroot/stderr.expected
374 (cd $testroot/wt && got patch patch) \
375 > $testroot/stdout \
376 2> $testroot/stderr
377 ret=$?
378 if [ $ret -eq 0 ]; then # should fail
379 test_done $testroot 1
380 return 1
383 cmp -s $testroot/stdout.expected $testroot/stdout
384 ret=$?
385 if [ $ret -ne 0 ]; then
386 diff -u $testroot/stdout.expected $testroot/stdout
387 test_done $testroot $ret
388 return 1
391 cmp -s $testroot/stderr.expected $testroot/stderr
392 ret=$?
393 if [ $ret -ne 0 ]; then
394 diff -u $testroot/stderr.expected $testroot/stderr
395 test_done $testroot $ret
396 return 1
399 test_done $testroot $ret
402 test_patch_equals_for_context() {
403 local testroot=`test_init patch_equals_for_context`
405 got checkout $testroot/repo $testroot/wt > /dev/null
406 ret=$?
407 if [ $ret -ne 0 ]; then
408 test_done $testroot $ret
409 return 1
412 cat <<EOF > $testroot/wt/patch
413 --- alpha
414 +++ alpha
415 @@ -1 +1,2 @@
416 +hatsuseno
417 =alpha
420 echo "M alpha" > $testroot/stdout.expected
422 (cd $testroot/wt && got patch patch) > $testroot/stdout
423 ret=$?
424 if [ $ret -ne 0 ]; then
425 test_done $testroot $ret
426 return 1
429 cmp -s $testroot/stdout.expected $testroot/stdout
430 ret=$?
431 if [ $ret -ne 0 ]; then
432 diff -u $testroot/stdout.expected $testroot/stdout
433 test_done $testroot $ret
434 return 1
437 echo hatsuseno > $testroot/wt/alpha.expected
438 echo alpha >> $testroot/wt/alpha.expected
439 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
440 ret=$?
441 if [ $ret -ne 0 ]; then
442 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
444 test_done $testroot $ret
447 test_patch_rename() {
448 local testroot=`test_init patch_rename`
450 got checkout $testroot/repo $testroot/wt > /dev/null
451 ret=$?
452 if [ $ret -ne 0 ]; then
453 test_done $testroot $ret
454 return 1
457 cat <<EOF > $testroot/wt/patch
458 diff --git a/beta b/iota
459 similarity index 100%
460 rename from beta
461 rename to iota
462 diff --git a/alpha b/eta
463 --- a/alpha
464 +++ b/eta
465 @@ -1 +1 @@
466 -alpha
467 +eta
470 echo 'D beta' > $testroot/stdout.expected
471 echo 'A iota' >> $testroot/stdout.expected
472 echo 'D alpha' >> $testroot/stdout.expected
473 echo 'A eta' >> $testroot/stdout.expected
475 (cd $testroot/wt && got patch patch) > $testroot/stdout
476 ret=$?
477 if [ $ret -ne 0 ]; then
478 test_done $testroot $ret
479 return 1
482 cmp -s $testroot/stdout.expected $testroot/stdout
483 ret=$?
484 if [ $ret -ne 0 ]; then
485 diff -u $testroot/stdout.expected $testroot/stdout
486 test_done $testroot $ret
487 return 1
490 if [ -f $testroot/wt/alpha -o -f $testroot/wt/beta ]; then
491 echo "alpha or beta were not removed" >&2
492 test_done $testroot 1
493 return 1
495 if [ ! -f $testroot/wt/iota -o ! -f $testroot/wt/eta ]; then
496 echo "iota or eta were not created" >&2
497 test_done $testroot 1
498 return 1
501 echo beta > $testroot/wt/iota.expected
502 cmp -s $testroot/wt/iota.expected $testroot/wt/iota
503 ret=$?
504 if [ $ret -ne 0 ]; then
505 diff -u $testroot/wt/iota.expected $testroot/wt/iota
506 test_done $testroot $ret
507 return 1
510 echo eta > $testroot/wt/eta.expected
511 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
512 ret=$?
513 if [ $ret -ne 0 ]; then
514 diff -u $testroot/wt/eta.expected $testroot/wt/eta
515 test_done $testroot $ret
516 return 1
519 test_done $testroot $ret
522 test_patch_illegal_status() {
523 local testroot=`test_init patch_illegal_status`
525 got checkout $testroot/repo $testroot/wt > /dev/null
526 ret=$?
527 if [ $ret -ne 0 ]; then
528 test_done $testroot $ret
529 return 1
532 # try to patch an obstructed file, add a versioned one, edit a
533 # non existent file and an unversioned one, and remove a
534 # non existent file.
535 cat <<EOF > $testroot/wt/patch
536 --- alpha
537 +++ alpha
538 @@ -1 +1,2 @@
539 alpha
540 +was edited
541 --- /dev/null
542 +++ beta
543 @@ -0,0 +1 @@
544 +beta
545 --- iota
546 +++ iota
547 @@ -1 +1 @@
548 -iota
549 +IOTA
550 --- kappa
551 +++ kappa
552 @@ -1 +1 @@
553 -kappa
554 +KAPPA
555 --- lambda
556 +++ /dev/null
557 @@ -1 +0,0 @@
558 -lambda
561 echo kappa > $testroot/wt/kappa
562 rm $testroot/wt/alpha
563 mkdir $testroot/wt/alpha
565 (cd $testroot/wt && got patch patch) > $testroot/stdout \
566 2> $testroot/stderr
567 ret=$?
568 if [ $ret -eq 0 ]; then
569 echo "edited a missing file" >&2
570 test_done $testroot 1
571 return 1
574 cat <<EOF > $testroot/stdout.expected
575 # alpha
576 # beta
577 # iota
578 # kappa
579 # lambda
580 Files where patch failed to apply: 5
583 cat <<EOF > $testroot/stderr.expected
584 got: alpha: file has unexpected status
585 got: beta: file has unexpected status
586 got: iota: No such file or directory
587 got: kappa: file has unexpected status
588 got: lambda: No such file or directory
589 got: patch failed to apply
592 cmp -s $testroot/stdout.expected $testroot/stdout
593 ret=$?
594 if [ $ret -ne 0 ]; then
595 diff -u $testroot/stdout.expected $testroot/stdout
596 test_done $testroot $ret
597 return 1
600 cmp -s $testroot/stderr.expected $testroot/stderr
601 ret=$?
602 if [ $ret -ne 0 ]; then
603 diff -u $testroot/stderr.expected $testroot/stderr
604 test_done $testroot $ret
605 return 1
608 (cd $testroot/wt && got status) > $testroot/stdout
609 cat <<EOF > $testroot/stdout.expected
610 ~ alpha
611 ? kappa
612 ? patch
615 cmp -s $testroot/stdout.expected $testroot/stdout
616 ret=$?
617 if [ $ret -ne 0 ]; then
618 diff -u $testroot/stdout.expected $testroot/stdout
620 test_done $testroot $ret
623 test_patch_nop() {
624 local testroot=`test_init patch_nop`
626 got checkout $testroot/repo $testroot/wt > /dev/null
627 ret=$?
628 if [ $ret -ne 0 ]; then
629 test_done $testroot $ret
630 return 1
633 cat <<EOF > $testroot/wt/patch
634 --- alpha
635 +++ alpha
636 @@ -1 +1 @@
637 -alpha
638 +cafe alpha
639 --- beta
640 +++ /dev/null
641 @@ -1 +0,0 @@
642 -beta
643 diff --git a/gamma/delta b/gamma/delta.new
644 --- gamma/delta
645 +++ gamma/delta.new
646 @@ -1 +1 @@
647 -delta
648 +delta updated and renamed!
651 (cd $testroot/wt && got patch -n patch)
652 ret=$?
653 if [ $ret -ne 0 ]; then
654 test_done $testroot $ret
655 return 1
658 # remove the patch to avoid the ? entry
659 rm $testroot/wt/patch
661 (cd $testroot/wt && got status) > $testroot/stdout
662 ret=$?
663 if [ $ret -ne 0 ]; then
664 test_done $testroot $ret
665 return 1
668 echo -n > $testroot/stdout.expected
669 cmp -s $testroot/stdout.expected $testroot/stdout
670 ret=$?
671 if [ $ret -ne 0 ]; then
672 diff -u $testroot/stdout.expected $testroot/stdout
674 test_done $testroot $ret
677 test_patch_preserve_perm() {
678 local testroot=`test_init patch_preserve_perm`
680 got checkout $testroot/repo $testroot/wt > /dev/null
681 ret=$?
682 if [ $ret -ne 0 ]; then
683 test_done $testroot $ret
684 return 1
687 chmod +x $testroot/wt/alpha
688 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
689 ret=$?
690 if [ $ret -ne 0 ]; then
691 test_done $testroot $ret
692 return 1
695 cat <<EOF > $testroot/wt/patch
696 --- alpha
697 +++ alpha
698 @@ -1 +1,2 @@
699 alpha
700 +was edited
703 (cd $testroot/wt && got patch patch) > /dev/null
704 ret=$?
705 if [ $ret -ne 0 ]; then
706 test_done $testroot $ret
707 return 1
710 if [ ! -x $testroot/wt/alpha ]; then
711 echo "alpha is no more executable!" >&2
712 test_done $testroot 1
713 return 1
715 test_done $testroot 0
718 test_patch_create_dirs() {
719 local testroot=`test_init patch_create_dirs`
721 got checkout $testroot/repo $testroot/wt > /dev/null
722 ret=$?
723 if [ $ret -ne 0 ]; then
724 test_done $testroot $ret
725 return 1
728 cat <<EOF > $testroot/wt/patch
729 --- /dev/null
730 +++ iota/kappa/lambda
731 @@ -0,0 +1 @@
732 +lambda
735 (cd $testroot/wt && got patch patch) > $testroot/stdout
736 ret=$?
737 if [ $ret -ne 0 ]; then
738 test_done $testroot $ret
739 return 1
742 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
743 cmp -s $testroot/stdout.expected $testroot/stdout
744 ret=$?
745 if [ $ret -ne 0 ]; then
746 diff -u $testroot/stdout.expected $testroot/stdout
747 test_done $testroot $ret
748 return 1
751 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
752 echo "file not created!" >&2
753 test_done $testroot $ret
754 return 1
756 test_done $testroot 0
759 test_patch_with_offset() {
760 local testroot=`test_init patch_with_offset`
762 got checkout $testroot/repo $testroot/wt > /dev/null
763 ret=$?
764 if [ $ret -ne 0 ]; then
765 test_done $testroot $ret
766 return 1
769 cat <<EOF > $testroot/wt/patch
770 --- numbers
771 +++ numbers
772 @@ -47,7 +47,7 @@
777 +midway tru it!
781 @@ -87,7 +87,7 @@
786 +almost there!
792 seq 100 > $testroot/wt/numbers
793 ed -s "$testroot/wt/numbers" <<EOF
794 1,10d
795 50r !seq 20
800 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
801 > /dev/null
802 ret=$?
803 if [ $ret -ne 0 ]; then
804 test_done $testroot $ret
805 return 1
808 (cd $testroot/wt && got patch patch) > $testroot/stdout
809 ret=$?
810 if [ $ret -ne 0 ]; then
811 test_done $testroot/wt $ret
812 return 1
815 cat <<EOF > $testroot/stdout.expected
816 M numbers
817 @@ -47,7 +47,7 @@ applied with offset -10
818 @@ -87,7 +87,7 @@ applied with offset 10
821 cmp -s $testroot/stdout.expected $testroot/stdout
822 ret=$?
823 if [ $ret -ne 0 ]; then
824 diff -u $testroot/stdout.expected $testroot/stdout
826 test_done $testroot $ret
829 test_patch_empty_file() {
830 local testroot=`test_init patch_empty_file`
832 got checkout $testroot/repo $testroot/wt > /dev/null
833 ret=$?
834 if [ $ret -ne 0 ]; then
835 test_done $testroot $ret
836 return 1
839 echo -n > $testroot/wt/alpha
840 (cd "$testroot/wt" && got commit -m 'edit alpha' alpha) >/dev/null
842 # try a patch which re-adds file contents; should suceeed
843 cat <<EOF >$testroot/wt/patch
844 --- alpha
845 +++ alpha
846 @@ -0,0 +1 @@
847 +alpha
849 (cd $testroot/wt && got patch patch) > $testroot/stdout
850 ret=$?
851 if [ $ret -ne 0 ]; then
852 test_done $testroot $ret
853 return 1
855 echo 'M alpha' > $testroot/stdout.expected
856 cmp -s $testroot/stdout.expected $testroot/stdout
857 ret=$?
858 if [ $ret -ne 0 ]; then
859 diff -u $testroot/stdout.expected $testroot/stdout
860 test_done $testroot $ret
861 return 1
864 (cd $testroot/wt && got revert -R . > /dev/null)
866 # try a patch which edits the file; should fail
867 cat <<EOF >$testroot/wt/patch
868 --- alpha
869 +++ alpha
870 @@ -1 +1 @@
871 -alpha
872 +beta
874 (cd $testroot/wt && got patch patch) > $testroot/stdout \
875 2> $testroot/stderr
876 ret=$?
877 if [ $ret -eq 0 ]; then
878 echo "got patch succeeded unexpectedly" >&2
879 test_done $testroot $ret
880 return 1
883 echo 'got: patch failed to apply' > $testroot/stderr.expected
884 cmp -s $testroot/stderr.expected $testroot/stderr
885 ret=$?
886 if [ $ret -ne 0 ]; then
887 diff -u $testroot/stderr.expected $testroot/stderr
888 test_done $testroot $ret
889 return 1
892 (cd $testroot/wt && got revert -R . > /dev/null)
894 # try a patch which deletes lines from the file; should fail
895 cat <<EOF >$testroot/wt/patch
896 --- alpha
897 +++ alpha
898 @@ -1,2 +0 @@
899 -alpha
900 -beta
902 (cd $testroot/wt && got patch patch) > $testroot/stdout \
903 2> $testroot/stderr
904 ret=$?
905 if [ $ret -eq 0 ]; then
906 echo "got patch succeeded unexpectedly" >&2
907 test_done $testroot $ret
908 return 1
910 echo 'got: patch failed to apply' > $testroot/stderr.expected
911 cmp -s $testroot/stderr.expected $testroot/stderr
912 ret=$?
913 if [ $ret -ne 0 ]; then
914 diff -u $testroot/stderr.expected $testroot/stderr
916 test_done $testroot $ret
919 test_patch_prefer_new_path() {
920 local testroot=`test_init patch_orig`
922 got checkout $testroot/repo $testroot/wt > /dev/null
923 ret=$?
924 if [ $ret -ne 0 ]; then
925 test_done $testroot $ret
926 return 1
929 cat <<EOF > $testroot/wt/patch
930 --- alpha.orig
931 +++ alpha
932 @@ -1 +1,2 @@
933 alpha
934 +was edited
937 (cd $testroot/wt && got patch patch) > $testroot/stdout
938 ret=$?
939 if [ $ret -ne 0 ]; then
940 test_done $testroot $ret
941 return 1
944 echo 'M alpha' > $testroot/stdout.expected
945 cmp -s $testroot/stdout.expected $testroot/stdout
946 ret=$?
947 if [ $ret -ne 0 ]; then
948 diff -u $testroot/stdout.expected $testroot/stdout
950 test_done $testroot $ret
953 test_patch_no_newline() {
954 local testroot=`test_init patch_no_newline`
956 got checkout $testroot/repo $testroot/wt > /dev/null
957 ret=$?
958 if [ $ret -ne 0 ]; then
959 test_done $testroot $ret
960 return 1
963 cat <<EOF > $testroot/wt/patch
964 --- /dev/null
965 +++ eta
966 @@ -0,0 +1 @@
967 +eta
968 \ No newline at end of file
971 (cd $testroot/wt && got patch patch) > $testroot/stdout
972 ret=$?
973 if [ $ret -ne 0 ]; then
974 test_done $testroot $ret
975 return 1
978 echo "A eta" > $testroot/stdout.expected
979 cmp -s $testroot/stdout.expected $testroot/stdout
980 ret=$?
981 if [ $ret -ne 0 ]; then
982 diff -u $testroot/stdout.expected $testroot/stdout
983 test_done $testroot $ret
984 return 1
987 echo -n eta > $testroot/wt/eta.expected
988 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
989 ret=$?
990 if [ $ret -ne 0 ]; then
991 diff -u $testroot/wt/eta.expected $testroot/wt/eta
992 test_done $testroot $ret
993 return 1
996 (cd $testroot/wt && got commit -m 'add eta') > /dev/null
997 ret=$?
998 if [ $ret -ne 0 ]; then
999 test_done $testroot $ret
1000 return 1
1003 cat <<EOF > $testroot/wt/patch
1004 --- eta
1005 +++ eta
1006 @@ -1 +1 @@
1007 -eta
1008 \ No newline at end of file
1009 +ETA
1010 \ No newline at end of file
1013 (cd $testroot/wt && got patch patch) > $testroot/stdout
1014 ret=$?
1015 if [ $ret -ne 0 ]; then
1016 test_done $testroot $ret
1017 return 1
1020 echo "M eta" > $testroot/stdout.expected
1021 cmp -s $testroot/stdout.expected $testroot/stdout
1022 ret=$?
1023 if [ $ret -ne 0 ]; then
1024 diff -u $testroot/stdout.expected $testroot/stdout
1025 test_done $testroot $ret
1026 return 1
1029 echo -n ETA > $testroot/wt/eta.expected
1030 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1031 ret=$?
1032 if [ $ret -ne 0 ]; then
1033 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1034 test_done $testroot $ret
1035 return 1
1038 (cd $testroot/wt && got commit -m 'edit eta') > /dev/null
1039 ret=$?
1040 if [ $ret -ne 0 ]; then
1041 test_done $testroot $ret
1042 return 1
1045 cat <<EOF > $testroot/wt/patch
1046 --- eta
1047 +++ eta
1048 @@ -1 +1 @@
1049 -ETA
1050 \ No newline at end of file
1051 +eta
1054 (cd $testroot/wt && got patch patch) > $testroot/stdout
1055 ret=$?
1056 if [ $ret -ne 0 ]; then
1057 test_done $testroot $ret
1058 return 1
1061 echo "M eta" > $testroot/stdout.expected
1062 cmp -s $testroot/stdout.expected $testroot/stdout
1063 ret=$?
1064 if [ $ret -ne 0 ]; then
1065 diff -u $testroot/stdout.expected $testroot/stdout
1066 test_done $testroot $ret
1067 return 1
1070 echo eta > $testroot/wt/eta.expected
1071 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1072 ret=$?
1073 if [ $ret -ne 0 ]; then
1074 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1076 test_done $testroot $ret
1079 test_patch_strip() {
1080 local testroot=`test_init patch_strip`
1082 got checkout $testroot/repo $testroot/wt > /dev/null
1083 ret=$?
1084 if [ $ret -ne 0 ]; then
1085 test_done $testroot $ret
1086 return 1
1089 cat <<EOF > $testroot/wt/patch
1090 --- foo/bar/alpha.orig
1091 +++ foo/bar/alpha
1092 @@ -1 +1 @@
1093 -alpha
1094 +ALPHA
1097 (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout
1098 ret=$?
1099 if [ $ret -ne 0 ]; then
1100 test_done $testroot $ret
1101 return 1
1104 echo "M alpha" >> $testroot/stdout.expected
1105 cmp -s $testroot/stdout.expected $testroot/stdout
1106 ret=$?
1107 if [ $ret -ne 0 ]; then
1108 diff -u $testroot/stdout.expected $testroot/stdout
1109 test_done $testroot $ret
1110 return 1
1113 (cd $testroot/wt && got revert alpha) > /dev/null 2>&1
1114 ret=$?
1115 if [ $ret -ne 0 ]; then
1116 test_done $testroot $ret
1117 return 1
1120 (cd $testroot/wt && got patch -p3 patch) \
1121 2> $testroot/stderr
1122 ret=$?
1123 if [ $ret -eq 0 ]; then
1124 echo "stripped more components than available!"
1125 test_done $testroot 1
1126 return 1
1129 cat <<EOF > $testroot/stderr.expected
1130 got: can't strip 1 path-components from foo/bar/alpha: bad path
1133 cmp -s $testroot/stderr.expected $testroot/stderr
1134 ret=$?
1135 if [ $ret -ne 0 ]; then
1136 diff -u $testroot/stderr.expected $testroot/stderr
1138 test_done $testroot 0
1141 test_patch_whitespace() {
1142 local testroot=`test_init patch_whitespace`
1144 got checkout $testroot/repo $testroot/wt > /dev/null
1145 ret=$?
1146 if [ $ret -ne 0 ]; then
1147 test_done $testroot $ret
1148 return 1
1151 trailing=" "
1153 cat <<EOF > $testroot/wt/hello.c
1154 #include <stdio.h>
1157 main(void)
1159 /* the trailing whitespace is on purpose */
1160 printf("hello, world\n");$trailing
1161 return 0;
1165 (cd $testroot/wt && got add hello.c && got ci -m '+hello.c') \
1166 > /dev/null
1167 ret=$?
1168 if [ $ret -ne 0 ]; then
1169 test_done $testroot $ret
1170 return 1
1173 # test with a diff with various whitespace corruptions
1174 cat <<EOF > $testroot/wt/patch
1175 --- hello.c
1176 +++ hello.c
1177 @@ -5,5 +5,5 @@
1179 /* the trailing whitespace is on purpose */
1180 printf("hello, world\n");
1181 - return 0;
1182 + return 5; /* always fails */
1186 (cd $testroot/wt && got patch patch) \
1187 2>$testroot/stderr >$testroot/stdout
1188 ret=$?
1189 if [ $ret -ne 0 ]; then
1190 echo "failed to apply diff" >&2
1191 test_done $testroot $ret
1192 return 1
1195 echo 'M hello.c' > $testroot/stdout.expected
1196 echo '@@ -5,5 +5,5 @@ hunk contains mangled whitespace' \
1197 >> $testroot/stdout.expected
1198 cmp -s $testroot/stdout.expected $testroot/stdout
1199 ret=$?
1200 if [ $ret -ne 0 ]; then
1201 diff -u $testroot/stdout.expected $testroot/stdout
1202 test_done $testroot $ret
1203 return 1
1206 cat <<EOF > $testroot/wt/hello.c.expected
1207 #include <stdio.h>
1210 main(void)
1212 /* the trailing whitespace is on purpose */
1213 printf("hello, world\n");$trailing
1214 return 5; /* always fails */
1218 cmp -s $testroot/wt/hello.c.expected $testroot/wt/hello.c
1219 ret=$?
1220 if [ $ret -ne 0 ]; then
1221 diff -u $testroot/wt/hello.c.expected $testroot/wt/hello.c
1223 test_done $testroot $ret
1226 test_patch_relative_paths() {
1227 local testroot=`test_init patch_relative_paths`
1229 got checkout $testroot/repo $testroot/wt > /dev/null
1230 ret=$?
1231 if [ $ret -ne 0 ]; then
1232 test_done $testroot $ret
1233 return 1
1236 cat <<EOF > $testroot/wt/gamma/patch
1237 --- delta
1238 +++ delta
1239 @@ -1 +1 @@
1240 -delta
1241 +DELTA
1242 --- /dev/null
1243 +++ eta
1244 @@ -0,0 +1 @@
1245 +eta
1248 (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
1249 ret=$?
1250 if [ $ret -ne 0 ]; then
1251 test_done $testroot $ret
1252 return 1
1255 echo 'M gamma/delta' > $testroot/stdout.expected
1256 echo 'A gamma/eta' >> $testroot/stdout.expected
1258 cmp -s $testroot/stdout.expected $testroot/stdout
1259 ret=$?
1260 if [ $ret -ne 0 ]; then
1261 diff -u $testroot/stdout.expected $testroot/stdout
1263 test_done $testroot $ret
1266 test_patch_with_path_prefix() {
1267 local testroot=`test_init patch_with_path_prefix`
1269 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1270 ret=$?
1271 if [ $ret -ne 0 ]; then
1272 test_done $testroot $ret
1273 return 1
1276 cat <<EOF > $testroot/wt/patch
1277 --- delta
1278 +++ delta
1279 @@ -1 +1 @@
1280 -delta
1281 +DELTA
1282 --- /dev/null
1283 +++ eta
1284 @@ -0,0 +1 @@
1285 +eta
1288 (cd $testroot/wt && got patch patch) > $testroot/stdout
1289 ret=$?
1290 if [ $ret -ne 0 ]; then
1291 test_done $testroot $ret
1292 return 1
1295 echo 'M delta' > $testroot/stdout.expected
1296 echo 'A eta' >> $testroot/stdout.expected
1298 cmp -s $testroot/stdout.expected $testroot/stdout
1299 ret=$?
1300 if [ $ret -ne 0 ]; then
1301 diff -u $testroot/stdout.expected $testroot/stdout
1303 test_done $testroot $ret
1306 test_patch_relpath_with_path_prefix() {
1307 local testroot=`test_init patch_relpaths_with_path_prefix`
1309 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1310 ret=$?
1311 if [ $ret -ne 0 ]; then
1312 test_done $testroot $ret
1313 return 1
1316 mkdir -p $testroot/wt/epsilon/zeta/
1318 cat <<EOF > $testroot/wt/patch
1319 --- /dev/null
1320 +++ zeta/theta
1321 @@ -0,0 +1 @@
1322 +theta
1325 (cd $testroot/wt/epsilon/zeta && got patch -p1 $testroot/wt/patch) \
1326 > $testroot/stdout
1327 ret=$?
1328 if [ $ret -ne 0 ]; then
1329 test_done $testroot $ret
1330 return 1
1333 echo 'A epsilon/zeta/theta' >> $testroot/stdout.expected
1335 cmp -s $testroot/stdout.expected $testroot/stdout
1336 ret=$?
1337 if [ $ret -ne 0 ]; then
1338 diff -u $testroot/stdout.expected $testroot/stdout
1339 test_done $testroot $ret
1340 return 1
1343 echo 'theta' > $testroot/theta.expected
1344 cmp -s $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1345 ret=$?
1346 if [ $ret -ne 0 ]; then
1347 diff -u $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1349 test_done $testroot $ret
1352 test_patch_reverse() {
1353 local testroot=`test_init patch_reverse`
1355 got checkout $testroot/repo $testroot/wt > /dev/null
1356 ret=$?
1357 if [ $ret -ne 0 ]; then
1358 test_done $testroot $ret
1359 return 1
1362 cat <<EOF > $testroot/wt/patch
1363 --- alpha
1364 +++ alpha
1365 @@ -1 +1 @@
1366 -ALPHA
1367 \ No newline at end of file
1368 +alpha
1371 (cd $testroot/wt && got patch -R patch) > $testroot/stdout
1372 ret=$?
1373 if [ $ret -ne 0 ]; then
1374 test_done $testroot $ret
1375 return 1
1378 echo "M alpha" > $testroot/stdout.expected
1379 cmp -s $testroot/stdout.expected $testroot/stdout
1380 ret=$?
1381 if [ $ret -ne 0 ]; then
1382 diff -u $testroot/stdout.expected $testroot/stdout
1383 test_done $testroot $ret
1384 return 1
1387 echo -n ALPHA > $testroot/wt/alpha.expected
1388 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
1389 ret=$?
1390 if [ $ret -ne 0 ]; then
1391 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
1393 test_done $testroot $ret
1396 test_patch_merge_simple() {
1397 local testroot=`test_init patch_merge_simple`
1399 got checkout $testroot/repo $testroot/wt > /dev/null
1400 ret=$?
1401 if [ $ret -ne 0 ]; then
1402 test_done $testroot $ret
1403 return 1
1406 seq 10 > $testroot/wt/numbers
1407 chmod +x $testroot/wt/numbers
1408 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1409 > /dev/null
1410 ret=$?
1411 if [ $ret -ne 0 ]; then
1412 test_done $testroot $ret
1413 return 1
1416 seq 10 | sed 's/4/four/g' > $testroot/wt/numbers
1418 (cd $testroot/wt && got diff > $testroot/old.diff \
1419 && got revert numbers) >/dev/null
1420 ret=$?
1421 if [ $ret -ne 0 ]; then
1422 test_done $testroot $ret
1423 return 1
1426 seq 10 | sed 's/6/six/g' > $testroot/wt/numbers
1427 (cd $testroot/wt && got commit -m 'edit numbers') \
1428 > /dev/null
1429 ret=$?
1430 if [ $ret -ne 0 ]; then
1431 test_done $testroot $ret
1432 return 1
1435 (cd $testroot/wt && got patch $testroot/old.diff) \
1436 > $testroot/stdout
1437 ret=$?
1438 if [ $ret -ne 0 ]; then
1439 test_done $testroot $ret
1440 return 1
1443 echo 'G numbers' > $testroot/stdout.expected
1444 cmp -s $testroot/stdout $testroot/stdout.expected
1445 ret=$?
1446 if [ $ret -ne 0 ]; then
1447 diff -u $testroot/stdout $testroot/stdout.expected
1448 test_done $testroot $ret
1449 return 1
1452 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1453 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1454 ret=$?
1455 if [ $ret -ne 0 ]; then
1456 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1457 test_done $testroot $ret
1458 return 1
1461 test -x $testroot/wt/numbers
1462 ret=$?
1463 if [ $ret -ne 0 ]; then
1464 echo "numbers lost the executable bit" >&2
1466 test_done $testroot $ret
1469 test_patch_merge_gitdiff() {
1470 local testroot=`test_init patch_merge_gitdiff`
1472 seq 10 > $testroot/repo/numbers
1473 git -C $testroot/repo add numbers && \
1474 git_commit $testroot/repo -m "nums"
1475 ret=$?
1476 if [ $ret -ne 0 ]; then
1477 test_done $testroot $ret
1478 return 1
1481 seq 10 | sed 's/4/four/g' > $testroot/repo/numbers
1482 git -C $testroot/repo diff > $testroot/old.diff
1483 ret=$?
1484 if [ $ret -ne 0 ]; then
1485 test_done $testroot $ret
1486 return 1
1489 # restore numbers
1490 seq 10 > $testroot/repo/numbers
1492 seq 10 | sed 's/6/six/g' > $testroot/repo/numbers
1493 git -C $testroot/repo add numbers && \
1494 git_commit $testroot/repo -m "edit"
1495 ret=$?
1496 if [ $ret -ne 0 ]; then
1497 test_done $testroot $ret
1498 return 1
1501 # now work with got:
1502 got checkout $testroot/repo $testroot/wt > /dev/null
1503 ret=$?
1504 if [ $ret -ne 0 ]; then
1505 test_done $testroot $ret
1506 return 1
1509 (cd $testroot/wt && got patch $testroot/old.diff) > $testroot/stdout
1510 ret=$?
1511 if [ $ret -ne 0 ]; then
1512 test_done $testroot $ret
1513 return 1
1516 echo 'G numbers' > $testroot/stdout.expected
1517 cmp -s $testroot/stdout $testroot/stdout.expected
1518 ret=$?
1519 if [ $ret -ne 0 ]; then
1520 diff -u $testroot/stdout $testroot/stdout.expected
1522 test_done $testroot $ret
1525 test_patch_merge_base_provided() {
1526 local testroot=`test_init patch_merge_base_provided`
1528 got checkout $testroot/repo $testroot/wt >/dev/null
1529 ret=$?
1530 if [ $ret -ne 0 ]; then
1531 test_done $testroot $ret
1532 return 1
1535 seq 10 > $testroot/wt/numbers
1536 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1537 >/dev/null
1538 ret=$?
1539 if [ $ret -ne 0 ]; then
1540 test_done $testroot $ret
1541 return 1
1544 local commit_id=`git_show_head $testroot/repo`
1546 seq 10 | sed s/4/four/ > $testroot/wt/numbers
1548 # get rid of the metadata
1549 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
1550 >/dev/null
1552 seq 10 | sed s/6/six/ > $testroot/wt/numbers
1553 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
1554 ret=$?
1555 if [ $ret -ne 0 ]; then
1556 test_done $testroot $ret
1557 return 1
1560 (cd $testroot/wt && got patch -c $commit_id patch) >$testroot/stdout
1561 ret=$?
1562 if [ $ret -ne 0 ]; then
1563 test_done $testroot $ret
1564 return 1
1567 echo 'G numbers' > $testroot/stdout.expected
1568 cmp -s $testroot/stdout $testroot/stdout.expected
1569 ret=$?
1570 if [ $ret -ne 0 ]; then
1571 diff -u $testroot/stdout $testroot/stdout.expected
1572 test_done $testroot $ret
1573 return 1
1576 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1577 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1578 ret=$?
1579 if [ $ret -ne 0 ]; then
1580 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1582 test_done $testroot $ret
1585 test_patch_merge_conflict() {
1586 local testroot=`test_init patch_merge_conflict`
1588 got checkout $testroot/repo $testroot/wt > /dev/null
1589 ret=$?
1590 if [ $ret -ne 0 ]; then
1591 test_done $testroot $ret
1592 return 1
1595 local alpha_basecommit_id=`git_show_head $testroot/repo`
1597 seq 10 > $testroot/wt/numbers
1598 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1599 > /dev/null
1600 ret=$?
1601 if [ $ret -ne 0 ]; then
1602 test_done $testroot $ret
1603 return 1
1606 local commit_id=`git_show_head $testroot/repo`
1608 seq 10 | sed 's/6/six/g' > $testroot/wt/numbers
1609 echo ALPHA > $testroot/wt/alpha
1611 (cd $testroot/wt && got diff > $testroot/old.diff \
1612 && got revert alpha numbers) >/dev/null
1613 ret=$?
1614 if [ $ret -ne 0 ]; then
1615 test_done $testroot $ret
1616 return 1
1619 seq 10 | sed 's/6/3+3/g' > $testroot/wt/numbers
1620 printf '%s\n' a b c > $testroot/wt/alpha
1621 (cd $testroot/wt && got commit -m 'edit alpha and numbers') \
1622 > /dev/null
1623 ret=$?
1624 if [ $ret -ne 0 ]; then
1625 test_done $testroot $ret
1626 return 1
1629 (cd $testroot/wt && got patch $testroot/old.diff) \
1630 > $testroot/stdout 2>/dev/null
1631 ret=$?
1632 if [ $ret -eq 0 ]; then
1633 echo "got patch merged a diff that should conflict" >&2
1634 test_done $testroot 1
1635 return 1
1638 echo 'C alpha' > $testroot/stdout.expected
1639 echo 'C numbers' >> $testroot/stdout.expected
1640 echo 'Files with merge conflicts: 2' >> $testroot/stdout.expected
1641 cmp -s $testroot/stdout $testroot/stdout.expected
1642 ret=$?
1643 if [ $ret -ne 0 ]; then
1644 diff -u $testroot/stdout $testroot/stdout.expected
1645 test_done $testroot $ret
1646 return 1
1649 # XXX: prefixing every line with a tab otherwise got thinks
1650 # the file has conflicts in it.
1651 cat <<-EOF > $testroot/wt/alpha.expected
1652 <<<<<<< --- alpha
1653 ALPHA
1654 ||||||| commit $alpha_basecommit_id
1655 alpha
1656 =======
1660 >>>>>>> +++ alpha
1663 cat <<-EOF > $testroot/wt/numbers.expected
1669 <<<<<<< --- numbers
1671 ||||||| commit $commit_id
1673 =======
1675 >>>>>>> +++ numbers
1682 cmp -s $testroot/wt/alpha $testroot/wt/alpha.expected
1683 ret=$?
1684 if [ $ret -ne 0 ]; then
1685 diff -u $testroot/wt/alpha $testroot/wt/alpha.expected
1686 test_done $testroot $ret
1687 return 1
1690 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1691 ret=$?
1692 if [ $ret -ne 0 ]; then
1693 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1695 test_done $testroot $ret
1698 test_patch_merge_unknown_blob() {
1699 local testroot=`test_init patch_merge_unknown_blob`
1701 got checkout $testroot/repo $testroot/wt > /dev/null
1702 ret=$?
1703 if [ $ret -ne 0 ]; then
1704 test_done $testroot $ret
1705 return 1
1708 cat <<EOF > $testroot/wt/patch
1709 I've got a
1710 diff aaaabbbbccccddddeeeeffff0000111122223333 foo/bar
1711 with a
1712 blob - aaaabbbbccccddddeeeeffff0000111122223333
1713 and also a
1714 blob + 0000111122223333444455556666777788889999
1715 for this dummy diff
1716 --- alpha
1717 +++ alpha
1718 @@ -1 +1 @@
1719 -alpha
1720 +ALPHA
1721 will it work?
1724 (cd $testroot/wt/ && got patch patch) > $testroot/stdout
1725 ret=$?
1726 if [ $ret -ne 0 ]; then
1727 test_done $testroot $ret
1728 return 1
1731 echo 'M alpha' > $testroot/stdout.expected
1732 cmp -s $testroot/stdout.expected $testroot/stdout
1733 ret=$?
1734 if [ $ret -ne 0 ]; then
1735 diff -u $testroot/stdout.expected $testroot/stdout
1736 test_done $testroot $ret
1737 return 1
1740 # try again without a `diff' header
1742 cat <<EOF > $testroot/wt/patch
1743 I've got a
1744 blob - aaaabbbbccccddddeeeeffff0000111122223333
1745 and also a
1746 blob + 0000111122223333444455556666777788889999
1747 for this dummy diff
1748 --- alpha
1749 +++ alpha
1750 @@ -1 +1 @@
1751 -alpha
1752 +ALPHA
1753 will it work?
1756 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1757 > $testroot/stdout
1758 ret=$?
1759 if [ $ret -ne 0 ]; then
1760 test_done $testroot $ret
1761 return 1
1764 echo 'M alpha' > $testroot/stdout.expected
1765 cmp -s $testroot/stdout.expected $testroot/stdout
1766 ret=$?
1767 if [ $ret -ne 0 ]; then
1768 diff -u $testroot/stdout.expected $testroot/stdout
1769 test_done $testroot $ret
1770 return 1
1773 # try again with a git-style diff
1775 cat <<EOF > $testroot/wt/patch
1776 diff --git a/alpha b/alpha
1777 index 0123456789ab..abcdef012345 100644
1778 --- a/alpha
1779 +++ b/alpha
1780 @@ -1 +1 @@
1781 -alpha
1782 +ALPHA
1785 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1786 > $testroot/stdout
1787 ret=$?
1788 if [ $ret -ne 0 ]; then
1789 test_done $testroot $ret
1790 return 1
1793 echo 'M alpha' > $testroot/stdout.expected
1794 cmp -s $testroot/stdout.expected $testroot/stdout
1795 ret=$?
1796 if [ $ret -ne 0 ]; then
1797 diff -u $testroot/stdout.expected $testroot/stdout
1799 test_done $testroot $ret
1802 test_patch_merge_reverse() {
1803 local testroot=`test_init patch_merge_simple`
1805 got checkout $testroot/repo $testroot/wt > /dev/null
1806 ret=$?
1807 if [ $ret -ne 0 ]; then
1808 test_done $testroot $ret
1809 return 1
1812 seq 10 > $testroot/wt/numbers
1813 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1814 > /dev/null
1815 ret=$?
1816 if [ $ret -ne 0 ]; then
1817 test_done $testroot $ret
1818 return 1
1821 local commit_id=`git_show_head $testroot/repo`
1823 seq 10 | sed s/5/five/g > $testroot/wt/numbers
1824 (cd $testroot/wt && got diff > $testroot/wt/patch \
1825 && got commit -m 'edit numbers') > /dev/null
1826 ret=$?
1827 if [ $ret -ne 0 ]; then
1828 test_done $testroot $ret
1829 return 1
1832 seq 10 | sed -e s/5/five/g -e s/6/six/g > $testroot/wt/numbers
1833 (cd $testroot/wt && got commit -m 'edit numbers again') >/dev/null
1834 ret=$?
1835 if [ $ret -ne 0 ]; then
1836 test_done $testroot $ret
1837 return 1
1840 (cd $testroot/wt && got patch -R patch) >/dev/null 2>&1
1841 ret=$?
1842 if [ $ret -eq 0 ]; then
1843 echo "unexpectedly reverted the patch" >&2
1844 test_done $testroot 1
1845 return 1
1848 cat <<-EOF > $testroot/wt/numbers.expected
1853 <<<<<<< --- numbers
1856 ||||||| +++ numbers
1857 five
1858 =======
1859 five
1861 >>>>>>> commit $commit_id
1868 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1869 ret=$?
1870 if [ $ret -ne 0 ]; then
1871 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1873 test_done $testroot $ret
1876 test_patch_newfile_xbit_got_diff() {
1877 local testroot=`test_init patch_newfile_xbit`
1879 got checkout $testroot/repo $testroot/wt > /dev/null
1880 ret=$?
1881 if [ $ret -ne 0 ]; then
1882 test_done $testroot $ret
1883 return 1
1886 cat <<EOF > $testroot/wt/patch
1887 blob - /dev/null
1888 blob + abcdef0123456789abcdef012345678901234567 (mode 755)
1889 --- /dev/null
1890 +++ xfile
1891 @@ -0,0 +1,1 @@
1892 +xfile
1895 (cd $testroot/wt && got patch patch) > /dev/null
1896 ret=$?
1897 if [ $ret -ne 0 ]; then
1898 test_done $testroot $ret
1899 return 1
1902 if [ ! -x $testroot/wt/xfile ]; then
1903 echo "failed to set xbit on newfile" >&2
1904 test_done $testroot 1
1905 return 1
1908 echo xfile > $testroot/wt/xfile.expected
1909 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1910 ret=$?
1911 if [ $ret -ne 0 ]; then
1912 echo "fail"
1913 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1916 test_done $testroot $ret
1919 test_patch_newfile_xbit_git_diff() {
1920 local testroot=`test_init patch_newfile_xbit`
1922 got checkout $testroot/repo $testroot/wt > /dev/null
1923 ret=$?
1924 if [ $ret -ne 0 ]; then
1925 test_done $testroot $ret
1926 return 1
1929 cat <<EOF > $testroot/wt/patch
1930 diff --git a/xfile b/xfile
1931 new file mode 100755
1932 index 00000000..abcdef01
1933 --- /dev/null
1934 +++ b/xfile
1935 @@ -0,0 +1,1 @@
1936 +xfile
1939 (cd $testroot/wt && got patch patch) > /dev/null
1940 ret=$?
1941 if [ $ret -ne 0 ]; then
1942 test_done $testroot $ret
1943 return 1
1946 if [ ! -x $testroot/wt/xfile ]; then
1947 echo "failed to set xbit on newfile" >&2
1948 test_done $testroot 1
1949 return 1
1952 echo xfile > $testroot/wt/xfile.expected
1953 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1954 ret=$?
1955 if [ $ret -ne 0 ]; then
1956 echo "fail"
1957 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1960 test_done $testroot $ret
1963 test_patch_umask() {
1964 local testroot=`test_init patch_umask`
1966 got checkout "$testroot/repo" "$testroot/wt" >/dev/null
1968 cat <<EOF >$testroot/wt/patch
1969 --- alpha
1970 +++ alpha
1971 @@ -1 +1 @@
1972 -alpha
1973 +modified alpha
1976 # using a subshell to avoid clobbering global umask
1977 (umask 077 && cd "$testroot/wt" && got patch <patch) >/dev/null
1978 ret=$?
1979 if [ $ret -ne 0 ]; then
1980 test_done "$testroot" $ret
1981 return 1
1984 if ! ls -l "$testroot/wt/alpha" | grep -q ^-rw-------; then
1985 echo "alpha is not 0600 after patch" >&2
1986 ls -l "$testroot/wt/alpha" >&2
1987 test_done "$testroot" 1
1988 return 1
1991 test_done "$testroot" 0
1994 test_patch_remove_binary_file() {
1995 local testroot=`test_init patch_remove_binary_file`
1997 if ! got checkout $testroot/repo $testroot/wt >/dev/null; then
1998 test_done $testroot $ret
1999 return 1
2002 dd if=/dev/zero of=$testroot/wt/x bs=1 count=16 2>/dev/null >&2
2003 (cd $testroot/wt && got add x && got commit -m +x) >/dev/null
2005 (cd $testroot/wt && \
2006 got branch demo && \
2007 got rm x && \
2008 got ci -m -x &&
2009 got up -b master) >/dev/null
2011 echo 'D x' > $testroot/stdout.expected
2013 (cd $testroot/wt && got log -c demo -l 1 -p >patch)
2015 (cd $testroot/wt && got patch <patch) > $testroot/stdout
2016 if [ $? -ne 0 ]; then
2017 echo 'patch failed' >&2
2018 test_done $testroot 1
2019 return 1
2022 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
2023 diff -u $testroot/stdout.expected $testroot/stdout
2024 test_done $testroot 1
2025 return 1
2028 # try again using a git produced diff
2029 (cd $testroot/wt && got revert x) >/dev/null
2031 git -C $testroot/repo show demo >$testroot/wt/patch
2033 (cd $testroot/wt && got patch <patch) > $testroot/stdout
2034 if [ $? -ne 0 ]; then
2035 echo 'patch failed' >&2
2036 test_done $testroot 1
2037 return 1
2040 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
2041 diff -u $testroot/stdout.expected $testroot/stdout
2042 test_done $testroot 1
2043 return 1
2046 # try again using a diff(1) style patch
2047 (cd $testroot/wt && got revert x) >/dev/null
2049 echo "Binary files x and /dev/null differ" >$testroot/wt/patch
2050 (cd $testroot/wt && got patch <patch) >$testroot/stdout
2051 if [ $? -ne 0 ]; then
2052 echo 'patch failed' >&2
2053 test_done $testroot 1
2054 return 1
2057 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
2058 diff -u $testroot/stdout.expected $testroot/stdout
2059 test_done $testroot 1
2060 return 1
2063 test_done $testroot 0
2066 test_patch_commit_keywords() {
2067 local testroot=`test_init patch_commit_keywords`
2069 got checkout $testroot/repo $testroot/wt >/dev/null
2070 ret=$?
2071 if [ $ret -ne 0 ]; then
2072 test_done $testroot $ret
2073 return 1
2076 seq 10 > $testroot/wt/numbers
2077 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
2078 >/dev/null
2079 ret=$?
2080 if [ $ret -ne 0 ]; then
2081 test_done $testroot $ret
2082 return 1
2085 seq 10 | sed s/4/four/ > $testroot/wt/numbers
2087 # get rid of the metadata
2088 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
2089 >/dev/null
2091 seq 10 | sed s/6/six/ > $testroot/wt/numbers
2092 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
2093 ret=$?
2094 if [ $ret -ne 0 ]; then
2095 test_done $testroot $ret
2096 return 1
2099 (cd $testroot/wt && got patch -c :head:- patch) >$testroot/stdout
2100 ret=$?
2101 if [ $ret -ne 0 ]; then
2102 test_done $testroot $ret
2103 return 1
2106 echo 'G numbers' > $testroot/stdout.expected
2107 cmp -s $testroot/stdout $testroot/stdout.expected
2108 ret=$?
2109 if [ $ret -ne 0 ]; then
2110 diff -u $testroot/stdout $testroot/stdout.expected
2111 test_done $testroot $ret
2112 return 1
2115 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2116 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2117 ret=$?
2118 if [ $ret -ne 0 ]; then
2119 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2122 (cd "$testroot/wt" && got rv numbers > /dev/null)
2124 (cd $testroot/wt && got patch -c :base:- patch) >$testroot/stdout
2125 ret=$?
2126 if [ $ret -ne 0 ]; then
2127 test_done $testroot $ret
2128 return 1
2131 echo 'G numbers' > $testroot/stdout.expected
2132 cmp -s $testroot/stdout $testroot/stdout.expected
2133 ret=$?
2134 if [ $ret -ne 0 ]; then
2135 diff -u $testroot/stdout $testroot/stdout.expected
2136 test_done $testroot $ret
2137 return 1
2140 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2141 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2142 ret=$?
2143 if [ $ret -ne 0 ]; then
2144 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2147 test_done $testroot $ret
2150 test_parseargs "$@"
2151 run_test test_patch_basic
2152 run_test test_patch_dont_apply
2153 run_test test_patch_malformed
2154 run_test test_patch_no_patch
2155 run_test test_patch_equals_for_context
2156 run_test test_patch_rename
2157 run_test test_patch_illegal_status
2158 run_test test_patch_nop
2159 run_test test_patch_preserve_perm
2160 run_test test_patch_create_dirs
2161 run_test test_patch_with_offset
2162 run_test test_patch_empty_file
2163 run_test test_patch_prefer_new_path
2164 run_test test_patch_no_newline
2165 run_test test_patch_strip
2166 run_test test_patch_whitespace
2167 run_test test_patch_relative_paths
2168 run_test test_patch_with_path_prefix
2169 run_test test_patch_relpath_with_path_prefix
2170 run_test test_patch_reverse
2171 run_test test_patch_merge_simple
2172 run_test test_patch_merge_gitdiff
2173 run_test test_patch_merge_base_provided
2174 run_test test_patch_merge_conflict
2175 run_test test_patch_merge_unknown_blob
2176 run_test test_patch_merge_reverse
2177 run_test test_patch_newfile_xbit_got_diff
2178 run_test test_patch_newfile_xbit_git_diff
2179 run_test test_patch_umask
2180 run_test test_patch_remove_binary_file
2181 run_test test_patch_commit_keywords