Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2022 Omar Polo <op@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 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
27 fi
29 jot 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
36 fi
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 @@
56 +1
57 +2
58 +3
59 +4
60 +5
61 --- numbers
62 +++ numbers
63 @@ -3,7 +3,7 @@
64 3
65 4
66 5
67 -6
68 +six
69 7
70 8
71 9
72 @@ -57,7 +57,7 @@
73 57
74 58
75 59
76 -60
77 +sixty
78 61
79 62
80 63
81 @@ -98,3 +98,6 @@
82 98
83 99
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
94 fi
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
108 fi
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
117 fi
119 if [ -f "$testroot/wt/beta" ]; then
120 echo "beta was not deleted!" >&2
121 test_done "$testroot" 1
122 return 1
123 fi
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
133 fi
135 jot 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
142 fi
144 jot 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
150 fi
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
162 fi
164 jot 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
171 fi
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 @@
182 -1
183 -2
184 -3
185 -4
186 -5
187 -6
188 -7
189 -8
190 -9
191 EOF
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
198 fi
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 EOF
207 cmp -s $testroot/stdout.expected $testroot/stdout
208 ret=$?
209 if [ $ret -ne 0 ]; then
210 diff -u $testroot/stdout.expected $testroot/stdout
211 fi
212 test_done $testroot $ret
215 test_patch_malformed() {
216 local testroot=`test_init patch_malformed`
218 got checkout $testroot/repo $testroot/wt > /dev/null
219 ret=$?
220 if [ $ret -ne 0 ]; then
221 test_done $testroot $ret
222 return 1
223 fi
225 # missing "@@"
226 cat <<EOF > $testroot/wt/patch
227 --- alpha
228 +++ alpha
229 @@ -1 +1,2
230 +hatsuseno
231 alpha
232 EOF
234 echo -n > $testroot/stdout.expected
235 echo "got: malformed patch" > $testroot/stderr.expected
237 (cd $testroot/wt && got patch patch) \
238 > $testroot/stdout \
239 2> $testroot/stderr
240 ret=$?
241 if [ $ret -eq 0 ]; then
242 echo "got managed to apply an invalid patch"
243 test_done $testroot 1
244 return 1
245 fi
247 cmp -s $testroot/stdout.expected $testroot/stdout
248 ret=$?
249 if [ $ret -ne 0 ]; then
250 diff -u $testroot/stdout.expected $testroot/stdout
251 test_done $testroot $ret
252 return 1
253 fi
255 cmp -s $testroot/stderr.expected $testroot/stderr
256 ret=$?
257 if [ $ret -ne 0 ]; then
258 diff -u $testroot/stderr.expected $testroot/stderr
259 test_done $testroot $ret
260 return 1
261 fi
263 # wrong first character
264 cat <<EOF > $testroot/wt/patch
265 --- alpha
266 +++ alpha
267 @@ -1 +1,2 @@
268 +hatsuseno
269 alpha
270 EOF
272 (cd $testroot/wt && got patch patch) \
273 > $testroot/stdout \
274 2> $testroot/stderr
275 ret=$?
276 if [ $ret -eq 0 ]; then
277 echo "got managed to apply an invalid patch"
278 test_done $testroot 1
279 return 1
280 fi
282 cmp -s $testroot/stdout.expected $testroot/stdout
283 ret=$?
284 if [ $ret -ne 0 ]; then
285 diff -u $testroot/stdout.expected $testroot/stdout
286 test_done $testroot $ret
287 return 1
288 fi
290 cmp -s $testroot/stderr.expected $testroot/stderr
291 ret=$?
292 if [ $ret -ne 0 ]; then
293 diff -u $testroot/stderr.expected $testroot/stderr
294 test_done $testroot $ret
295 return 1
296 fi
298 # empty hunk
299 cat <<EOF > $testroot/wt/patch
300 diff --git a/alpha b/iota
301 --- a/alpha
302 +++ b/iota
303 @@ -0,0 +0,0 @@
304 EOF
306 (cd $testroot/wt && got patch patch) \
307 > $testroot/stdout \
308 2> $testroot/stderr
309 ret=$?
310 if [ $ret -eq 0 ]; then
311 echo "got managed to apply an invalid patch"
312 test_done $testroot 1
313 return 1
314 fi
316 cmp -s $testroot/stdout.expected $testroot/stdout
317 ret=$?
318 if [ $ret -ne 0 ]; then
319 diff -u $testroot/stdout.expected $testroot/stdout
320 test_done $testroot $ret
321 return 1
322 fi
324 cmp -s $testroot/stderr.expected $testroot/stderr
325 ret=$?
326 if [ $ret -ne 0 ]; then
327 diff -u $testroot/stderr.expected $testroot/stderr
328 test_done $testroot $ret
329 return 1
330 fi
332 test_done $testroot $ret
335 test_patch_no_patch() {
336 local testroot=`test_init patch_no_patch`
338 got checkout $testroot/repo $testroot/wt > /dev/null
339 ret=$?
340 if [ $ret -ne 0 ]; then
341 test_done $testroot $ret
342 return 1
343 fi
345 cat <<EOF > $testroot/wt/patch
346 hello world!
347 ...
349 some other nonsense
350 ...
352 there's no patch in here!
353 EOF
355 echo -n > $testroot/stdout.expected
356 echo "got: no patch found" > $testroot/stderr.expected
358 (cd $testroot/wt && got patch patch) \
359 > $testroot/stdout \
360 2> $testroot/stderr
361 ret=$?
362 if [ $ret -eq 0 ]; then # should fail
363 test_done $testroot 1
364 return 1
365 fi
367 cmp -s $testroot/stdout.expected $testroot/stdout
368 ret=$?
369 if [ $ret -ne 0 ]; then
370 diff -u $testroot/stdout.expected $testroot/stdout
371 test_done $testroot $ret
372 return 1
373 fi
375 cmp -s $testroot/stderr.expected $testroot/stderr
376 ret=$?
377 if [ $ret -ne 0 ]; then
378 diff -u $testroot/stderr.expected $testroot/stderr
379 test_done $testroot $ret
380 return 1
381 fi
383 test_done $testroot $ret
386 test_patch_equals_for_context() {
387 local testroot=`test_init patch_equals_for_context`
389 got checkout $testroot/repo $testroot/wt > /dev/null
390 ret=$?
391 if [ $ret -ne 0 ]; then
392 test_done $testroot $ret
393 return 1
394 fi
396 cat <<EOF > $testroot/wt/patch
397 --- alpha
398 +++ alpha
399 @@ -1 +1,2 @@
400 +hatsuseno
401 =alpha
402 EOF
404 echo "M alpha" > $testroot/stdout.expected
406 (cd $testroot/wt && got patch patch) > $testroot/stdout
407 ret=$?
408 if [ $ret -ne 0 ]; then
409 test_done $testroot $ret
410 return 1
411 fi
413 cmp -s $testroot/stdout.expected $testroot/stdout
414 ret=$?
415 if [ $ret -ne 0 ]; then
416 diff -u $testroot/stdout.expected $testroot/stdout
417 test_done $testroot $ret
418 return 1
419 fi
421 echo hatsuseno > $testroot/wt/alpha.expected
422 echo alpha >> $testroot/wt/alpha.expected
423 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
424 ret=$?
425 if [ $ret -ne 0 ]; then
426 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
427 fi
428 test_done $testroot $ret
431 test_patch_rename() {
432 local testroot=`test_init patch_rename`
434 got checkout $testroot/repo $testroot/wt > /dev/null
435 ret=$?
436 if [ $ret -ne 0 ]; then
437 test_done $testroot $ret
438 return 1
439 fi
441 cat <<EOF > $testroot/wt/patch
442 diff --git a/beta b/iota
443 similarity index 100%
444 rename from beta
445 rename to iota
446 diff --git a/alpha b/eta
447 --- a/alpha
448 +++ b/eta
449 @@ -1 +1 @@
450 -alpha
451 +eta
452 EOF
454 echo 'D beta' > $testroot/stdout.expected
455 echo 'A iota' >> $testroot/stdout.expected
456 echo 'D alpha' >> $testroot/stdout.expected
457 echo 'A eta' >> $testroot/stdout.expected
459 (cd $testroot/wt && got patch patch) > $testroot/stdout
460 ret=$?
461 if [ $ret -ne 0 ]; then
462 test_done $testroot $ret
463 return 1
464 fi
466 cmp -s $testroot/stdout.expected $testroot/stdout
467 ret=$?
468 if [ $ret -ne 0 ]; then
469 diff -u $testroot/stdout.expected $testroot/stdout
470 test_done $testroot $ret
471 return 1
472 fi
474 if [ -f $testroot/wt/alpha -o -f $testroot/wt/beta ]; then
475 echo "alpha or beta were not removed" >&2
476 test_done $testroot 1
477 return 1
478 fi
479 if [ ! -f $testroot/wt/iota -o ! -f $testroot/wt/eta ]; then
480 echo "iota or eta were not created" >&2
481 test_done $testroot 1
482 return 1
483 fi
485 echo beta > $testroot/wt/iota.expected
486 cmp -s $testroot/wt/iota.expected $testroot/wt/iota
487 ret=$?
488 if [ $ret -ne 0 ]; then
489 diff -u $testroot/wt/iota.expected $testroot/wt/iota
490 test_done $testroot $ret
491 return 1
492 fi
494 echo eta > $testroot/wt/eta.expected
495 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
496 ret=$?
497 if [ $ret -ne 0 ]; then
498 diff -u $testroot/wt/eta.expected $testroot/wt/eta
499 test_done $testroot $ret
500 return 1
501 fi
503 test_done $testroot $ret
506 test_patch_illegal_status() {
507 local testroot=`test_init patch_illegal_status`
509 got checkout $testroot/repo $testroot/wt > /dev/null
510 ret=$?
511 if [ $ret -ne 0 ]; then
512 test_done $testroot $ret
513 return 1
514 fi
516 # try to patch an obstructed file, add a versioned one, edit a
517 # non existent file and an unversioned one, and remove a
518 # non existent file.
519 cat <<EOF > $testroot/wt/patch
520 --- alpha
521 +++ alpha
522 @@ -1 +1,2 @@
523 alpha
524 +was edited
525 --- /dev/null
526 +++ beta
527 @@ -0,0 +1 @@
528 +beta
529 --- iota
530 +++ iota
531 @@ -1 +1 @@
532 -iota
533 +IOTA
534 --- kappa
535 +++ kappa
536 @@ -1 +1 @@
537 -kappa
538 +KAPPA
539 --- lambda
540 +++ /dev/null
541 @@ -1 +0,0 @@
542 -lambda
543 EOF
545 echo kappa > $testroot/wt/kappa
546 rm $testroot/wt/alpha
547 mkdir $testroot/wt/alpha
549 (cd $testroot/wt && got patch patch) > $testroot/stdout \
550 2> $testroot/stderr
551 ret=$?
552 if [ $ret -eq 0 ]; then
553 echo "edited a missing file" >&2
554 test_done $testroot 1
555 return 1
556 fi
558 cat <<EOF > $testroot/stdout.expected
559 # alpha
560 # beta
561 # iota
562 # kappa
563 # lambda
564 EOF
566 cat <<EOF > $testroot/stderr.expected
567 got: alpha: file has unexpected status
568 got: beta: file has unexpected status
569 got: iota: No such file or directory
570 got: kappa: file has unexpected status
571 got: lambda: No such file or directory
572 got: patch failed to apply
573 EOF
575 cmp -s $testroot/stdout.expected $testroot/stdout
576 ret=$?
577 if [ $ret -ne 0 ]; then
578 diff -u $testroot/stdout.expected $testroot/stdout
579 test_done $testroot $ret
580 return 1
581 fi
583 cmp -s $testroot/stderr.expected $testroot/stderr
584 ret=$?
585 if [ $ret -ne 0 ]; then
586 diff -u $testroot/stderr.expected $testroot/stderr
587 test_done $testroot $ret
588 return 1
589 fi
591 (cd $testroot/wt && got status) > $testroot/stdout
592 cat <<EOF > $testroot/stdout.expected
593 ~ alpha
594 ? kappa
595 ? patch
596 EOF
598 cmp -s $testroot/stdout.expected $testroot/stdout
599 ret=$?
600 if [ $ret -ne 0 ]; then
601 diff -u $testroot/stdout.expected $testroot/stdout
602 fi
603 test_done $testroot $ret
606 test_patch_nop() {
607 local testroot=`test_init patch_nop`
609 got checkout $testroot/repo $testroot/wt > /dev/null
610 ret=$?
611 if [ $ret -ne 0 ]; then
612 test_done $testroot $ret
613 return 1
614 fi
616 cat <<EOF > $testroot/wt/patch
617 --- alpha
618 +++ alpha
619 @@ -1 +1 @@
620 -alpha
621 +cafe alpha
622 --- beta
623 +++ /dev/null
624 @@ -1 +0,0 @@
625 -beta
626 diff --git a/gamma/delta b/gamma/delta.new
627 --- gamma/delta
628 +++ gamma/delta.new
629 @@ -1 +1 @@
630 -delta
631 +delta updated and renamed!
632 EOF
634 (cd $testroot/wt && got patch -n patch)
635 ret=$?
636 if [ $ret -ne 0 ]; then
637 test_done $testroot $ret
638 return 1
639 fi
641 # remove the patch to avoid the ? entry
642 rm $testroot/wt/patch
644 (cd $testroot/wt && got status) > $testroot/stdout
645 ret=$?
646 if [ $ret -ne 0 ]; then
647 test_done $testroot $ret
648 return 1
649 fi
651 echo -n > $testroot/stdout.expected
652 cmp -s $testroot/stdout.expected $testroot/stdout
653 ret=$?
654 if [ $ret -ne 0 ]; then
655 diff -u $testroot/stdout.expected $testroot/stdout
656 fi
657 test_done $testroot $ret
660 test_patch_preserve_perm() {
661 local testroot=`test_init patch_preserve_perm`
663 got checkout $testroot/repo $testroot/wt > /dev/null
664 ret=$?
665 if [ $ret -ne 0 ]; then
666 test_done $testroot $ret
667 return 1
668 fi
670 chmod +x $testroot/wt/alpha
671 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
672 ret=$?
673 if [ $ret -ne 0 ]; then
674 test_done $testroot $ret
675 return 1
676 fi
678 cat <<EOF > $testroot/wt/patch
679 --- alpha
680 +++ alpha
681 @@ -1 +1,2 @@
682 alpha
683 +was edited
684 EOF
686 (cd $testroot/wt && got patch patch) > /dev/null
687 ret=$?
688 if [ $ret -ne 0 ]; then
689 test_done $testroot $ret
690 return 1
691 fi
693 if [ ! -x $testroot/wt/alpha ]; then
694 echo "alpha is no more executable!" >&2
695 test_done $testroot 1
696 return 1
697 fi
698 test_done $testroot 0
701 test_patch_create_dirs() {
702 local testroot=`test_init patch_create_dirs`
704 got checkout $testroot/repo $testroot/wt > /dev/null
705 ret=$?
706 if [ $ret -ne 0 ]; then
707 test_done $testroot $ret
708 return 1
709 fi
711 cat <<EOF > $testroot/wt/patch
712 --- /dev/null
713 +++ iota/kappa/lambda
714 @@ -0,0 +1 @@
715 +lambda
716 EOF
718 (cd $testroot/wt && got patch patch) > $testroot/stdout
719 ret=$?
720 if [ $ret -ne 0 ]; then
721 test_done $testroot $ret
722 return 1
723 fi
725 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
726 cmp -s $testroot/stdout.expected $testroot/stdout
727 ret=$?
728 if [ $ret -ne 0 ]; then
729 diff -u $testroot/stdout.expected $testroot/stdout
730 test_done $testroot $ret
731 return 1
732 fi
734 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
735 echo "file not created!" >&2
736 test_done $testroot $ret
737 return 1
738 fi
739 test_done $testroot 0
742 test_patch_with_offset() {
743 local testroot=`test_init patch_with_offset`
745 got checkout $testroot/repo $testroot/wt > /dev/null
746 ret=$?
747 if [ $ret -ne 0 ]; then
748 test_done $testroot $ret
749 return 1
750 fi
752 cat <<EOF > $testroot/wt/patch
753 --- numbers
754 +++ numbers
755 @@ -47,7 +47,7 @@
756 47
757 48
758 49
759 -50
760 +midway tru it!
761 51
762 52
763 53
764 @@ -87,7 +87,7 @@
765 87
766 88
767 89
768 -90
769 +almost there!
770 91
771 92
772 93
773 EOF
775 jot 100 > $testroot/wt/numbers
776 ed -s "$testroot/wt/numbers" <<EOF
777 1,10d
778 50r !jot 20
781 EOF
783 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
784 > /dev/null
785 ret=$?
786 if [ $ret -ne 0 ]; then
787 test_done $testroot $ret
788 return 1
789 fi
791 (cd $testroot/wt && got patch patch) > $testroot/stdout
792 ret=$?
793 if [ $ret -ne 0 ]; then
794 test_done $testroot/wt $ret
795 return 1
796 fi
798 cat <<EOF > $testroot/stdout.expected
799 M numbers
800 @@ -47,7 +47,7 @@ applied with offset -10
801 @@ -87,7 +87,7 @@ applied with offset 10
802 EOF
804 cmp -s $testroot/stdout.expected $testroot/stdout
805 ret=$?
806 if [ $ret -ne 0 ]; then
807 diff -u $testroot/stdout.expected $testroot/stdout
808 fi
809 test_done $testroot $ret
812 test_patch_prefer_new_path() {
813 local testroot=`test_init patch_orig`
815 got checkout $testroot/repo $testroot/wt > /dev/null
816 ret=$?
817 if [ $ret -ne 0 ]; then
818 test_done $testroot $ret
819 return 1
820 fi
822 cat <<EOF > $testroot/wt/patch
823 --- alpha.orig
824 +++ alpha
825 @@ -1 +1,2 @@
826 alpha
827 +was edited
828 EOF
830 (cd $testroot/wt && got patch patch) > $testroot/stdout
831 ret=$?
832 if [ $ret -ne 0 ]; then
833 test_done $testroot $ret
834 return 1
835 fi
837 echo 'M alpha' > $testroot/stdout.expected
838 cmp -s $testroot/stdout.expected $testroot/stdout
839 ret=$?
840 if [ $ret -ne 0 ]; then
841 diff -u $testroot/stdout.expected $testroot/stdout
842 fi
843 test_done $testroot $ret
846 test_patch_no_newline() {
847 local testroot=`test_init patch_no_newline`
849 got checkout $testroot/repo $testroot/wt > /dev/null
850 ret=$?
851 if [ $ret -ne 0 ]; then
852 test_done $testroot $ret
853 return 1
854 fi
856 cat <<EOF > $testroot/wt/patch
857 --- /dev/null
858 +++ eta
859 @@ -0,0 +1 @@
860 +eta
861 \ No newline at end of file
862 EOF
864 (cd $testroot/wt && got patch patch) > $testroot/stdout
865 ret=$?
866 if [ $ret -ne 0 ]; then
867 test_done $testroot $ret
868 return 1
869 fi
871 echo "A eta" > $testroot/stdout.expected
872 cmp -s $testroot/stdout.expected $testroot/stdout
873 ret=$?
874 if [ $ret -ne 0 ]; then
875 diff -u $testroot/stdout.expected $testroot/stdout
876 test_done $testroot $ret
877 return 1
878 fi
880 echo -n eta > $testroot/wt/eta.expected
881 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
882 ret=$?
883 if [ $ret -ne 0 ]; then
884 diff -u $testroot/wt/eta.expected $testroot/wt/eta
885 test_done $testroot $ret
886 return 1
887 fi
889 (cd $testroot/wt && got commit -m 'add eta') > /dev/null
890 ret=$?
891 if [ $ret -ne 0 ]; then
892 test_done $testroot $ret
893 return 1
894 fi
896 cat <<EOF > $testroot/wt/patch
897 --- eta
898 +++ eta
899 @@ -1 +1 @@
900 -eta
901 \ No newline at end of file
902 +ETA
903 \ No newline at end of file
904 EOF
906 (cd $testroot/wt && got patch patch) > $testroot/stdout
907 ret=$?
908 if [ $ret -ne 0 ]; then
909 test_done $testroot $ret
910 return 1
911 fi
913 echo "M eta" > $testroot/stdout.expected
914 cmp -s $testroot/stdout.expected $testroot/stdout
915 ret=$?
916 if [ $ret -ne 0 ]; then
917 diff -u $testroot/stdout.expected $testroot/stdout
918 test_done $testroot $ret
919 return 1
920 fi
922 echo -n ETA > $testroot/wt/eta.expected
923 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
924 ret=$?
925 if [ $ret -ne 0 ]; then
926 diff -u $testroot/wt/eta.expected $testroot/wt/eta
927 test_done $testroot $ret
928 return 1
929 fi
931 (cd $testroot/wt && got commit -m 'edit eta') > /dev/null
932 ret=$?
933 if [ $ret -ne 0 ]; then
934 test_done $testroot $ret
935 return 1
936 fi
938 cat <<EOF > $testroot/wt/patch
939 --- eta
940 +++ eta
941 @@ -1 +1 @@
942 -ETA
943 \ No newline at end of file
944 +eta
945 EOF
947 (cd $testroot/wt && got patch patch) > $testroot/stdout
948 ret=$?
949 if [ $ret -ne 0 ]; then
950 test_done $testroot $ret
951 return 1
952 fi
954 echo "M eta" > $testroot/stdout.expected
955 cmp -s $testroot/stdout.expected $testroot/stdout
956 ret=$?
957 if [ $ret -ne 0 ]; then
958 diff -u $testroot/stdout.expected $testroot/stdout
959 test_done $testroot $ret
960 return 1
961 fi
963 echo eta > $testroot/wt/eta.expected
964 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
965 ret=$?
966 if [ $ret -ne 0 ]; then
967 diff -u $testroot/wt/eta.expected $testroot/wt/eta
968 fi
969 test_done $testroot $ret
972 test_patch_strip() {
973 local testroot=`test_init patch_strip`
975 got checkout $testroot/repo $testroot/wt > /dev/null
976 ret=$?
977 if [ $ret -ne 0 ]; then
978 test_done $testroot $ret
979 return 1
980 fi
982 cat <<EOF > $testroot/wt/patch
983 --- foo/bar/alpha.orig
984 +++ foo/bar/alpha
985 @@ -1 +1 @@
986 -alpha
987 +ALPHA
988 EOF
990 (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout
991 ret=$?
992 if [ $ret -ne 0 ]; then
993 test_done $testroot $ret
994 return 1
995 fi
997 echo "M alpha" >> $testroot/stdout.expected
998 cmp -s $testroot/stdout.expected $testroot/stdout
999 ret=$?
1000 if [ $ret -ne 0 ]; then
1001 diff -u $testroot/stdout.expected $testroot/stdout
1002 test_done $testroot $ret
1003 return 1
1006 (cd $testroot/wt && got revert alpha) > /dev/null 2>&1
1007 ret=$?
1008 if [ $ret -ne 0 ]; then
1009 test_done $testroot $ret
1010 return 1
1013 (cd $testroot/wt && got patch -p3 patch) \
1014 2> $testroot/stderr
1015 ret=$?
1016 if [ $ret -eq 0 ]; then
1017 echo "stripped more components than available!"
1018 test_done $testroot 1
1019 return 1
1022 cat <<EOF > $testroot/stderr.expected
1023 got: can't strip 1 path-components from foo/bar/alpha: bad path
1024 EOF
1026 cmp -s $testroot/stderr.expected $testroot/stderr
1027 ret=$?
1028 if [ $ret -ne 0 ]; then
1029 diff -u $testroot/stderr.expected $testroot/stderr
1031 test_done $testroot 0
1034 test_patch_whitespace() {
1035 local testroot=`test_init patch_whitespace`
1037 got checkout $testroot/repo $testroot/wt > /dev/null
1038 ret=$?
1039 if [ $ret -ne 0 ]; then
1040 test_done $testroot $ret
1041 return 1
1044 trailing=" "
1046 cat <<EOF > $testroot/wt/hello.c
1047 #include <stdio.h>
1049 int
1050 main(void)
1052 /* the trailing whitespace is on purpose */
1053 printf("hello, world\n");$trailing
1054 return 0;
1056 EOF
1058 (cd $testroot/wt && got add hello.c && got ci -m '+hello.c') \
1059 > /dev/null
1060 ret=$?
1061 if [ $ret -ne 0 ]; then
1062 test_done $testroot $ret
1063 return 1
1066 # test with a diff with various whitespace corruptions
1067 cat <<EOF > $testroot/wt/patch
1068 --- hello.c
1069 +++ hello.c
1070 @@ -5,5 +5,5 @@
1072 /* the trailing whitespace is on purpose */
1073 printf("hello, world\n");
1074 - return 0;
1075 + return 5; /* always fails */
1077 EOF
1079 (cd $testroot/wt && got patch patch) \
1080 2>$testroot/stderr >$testroot/stdout
1081 ret=$?
1082 if [ $ret -ne 0 ]; then
1083 echo "failed to apply diff" >&2
1084 test_done $testroot $ret
1085 return 1
1088 echo 'M hello.c' > $testroot/stdout.expected
1089 echo '@@ -5,5 +5,5 @@ hunk contains mangled whitespace' \
1090 >> $testroot/stdout.expected
1091 cmp -s $testroot/stdout.expected $testroot/stdout
1092 ret=$?
1093 if [ $ret -ne 0 ]; then
1094 diff -u $testroot/stdout.expected $testroot/stdout
1095 test_done $testroot $ret
1096 return 1
1099 cat <<EOF > $testroot/wt/hello.c.expected
1100 #include <stdio.h>
1102 int
1103 main(void)
1105 /* the trailing whitespace is on purpose */
1106 printf("hello, world\n");$trailing
1107 return 5; /* always fails */
1109 EOF
1111 cmp -s $testroot/wt/hello.c.expected $testroot/wt/hello.c
1112 ret=$?
1113 if [ $ret -ne 0 ]; then
1114 diff -u $testroot/wt/hello.c.expected $testroot/wt/hello.c
1116 test_done $testroot $ret
1119 test_patch_relative_paths() {
1120 local testroot=`test_init patch_relative_paths`
1122 got checkout $testroot/repo $testroot/wt > /dev/null
1123 ret=$?
1124 if [ $ret -ne 0 ]; then
1125 test_done $testroot $ret
1126 return 1
1129 cat <<EOF > $testroot/wt/gamma/patch
1130 --- delta
1131 +++ delta
1132 @@ -1 +1 @@
1133 -delta
1134 +DELTA
1135 --- /dev/null
1136 +++ eta
1137 @@ -0,0 +1 @@
1138 +eta
1139 EOF
1141 (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
1142 ret=$?
1143 if [ $ret -ne 0 ]; then
1144 test_done $testroot $ret
1145 return 1
1148 echo 'M gamma/delta' > $testroot/stdout.expected
1149 echo 'A gamma/eta' >> $testroot/stdout.expected
1151 cmp -s $testroot/stdout.expected $testroot/stdout
1152 ret=$?
1153 if [ $ret -ne 0 ]; then
1154 diff -u $testroot/stdout.expected $testroot/stdout
1156 test_done $testroot $ret
1159 test_patch_with_path_prefix() {
1160 local testroot=`test_init patch_with_path_prefix`
1162 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1163 ret=$?
1164 if [ $ret -ne 0 ]; then
1165 test_done $testroot $ret
1166 return 1
1169 cat <<EOF > $testroot/wt/patch
1170 --- delta
1171 +++ delta
1172 @@ -1 +1 @@
1173 -delta
1174 +DELTA
1175 --- /dev/null
1176 +++ eta
1177 @@ -0,0 +1 @@
1178 +eta
1179 EOF
1181 (cd $testroot/wt && got patch patch) > $testroot/stdout
1182 ret=$?
1183 if [ $ret -ne 0 ]; then
1184 test_done $testroot $ret
1185 return 1
1188 echo 'M delta' > $testroot/stdout.expected
1189 echo 'A eta' >> $testroot/stdout.expected
1191 cmp -s $testroot/stdout.expected $testroot/stdout
1192 ret=$?
1193 if [ $ret -ne 0 ]; then
1194 diff -u $testroot/stdout.expected $testroot/stdout
1196 test_done $testroot $ret
1199 test_patch_relpath_with_path_prefix() {
1200 local testroot=`test_init patch_relpaths_with_path_prefix`
1202 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1203 ret=$?
1204 if [ $ret -ne 0 ]; then
1205 test_done $testroot $ret
1206 return 1
1209 mkdir -p $testroot/wt/epsilon/zeta/
1211 cat <<EOF > $testroot/wt/patch
1212 --- /dev/null
1213 +++ zeta/theta
1214 @@ -0,0 +1 @@
1215 +theta
1216 EOF
1218 (cd $testroot/wt/epsilon/zeta && got patch -p1 $testroot/wt/patch) \
1219 > $testroot/stdout
1220 ret=$?
1221 if [ $ret -ne 0 ]; then
1222 test_done $testroot $ret
1223 return 1
1226 echo 'A epsilon/zeta/theta' >> $testroot/stdout.expected
1228 cmp -s $testroot/stdout.expected $testroot/stdout
1229 ret=$?
1230 if [ $ret -ne 0 ]; then
1231 diff -u $testroot/stdout.expected $testroot/stdout
1232 test_done $testroot $ret
1233 return 1
1236 echo 'theta' > $testroot/theta.expected
1237 cmp -s $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1238 ret=$?
1239 if [ $ret -ne 0 ]; then
1240 diff -u $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1242 test_done $testroot $ret
1245 test_patch_reverse() {
1246 local testroot=`test_init patch_reverse`
1248 got checkout $testroot/repo $testroot/wt > /dev/null
1249 ret=$?
1250 if [ $ret -ne 0 ]; then
1251 test_done $testroot $ret
1252 return 1
1255 cat <<EOF > $testroot/wt/patch
1256 --- alpha
1257 +++ alpha
1258 @@ -1 +1 @@
1259 -ALPHA
1260 \ No newline at end of file
1261 +alpha
1262 EOF
1264 (cd $testroot/wt && got patch -R patch) > $testroot/stdout
1265 ret=$?
1266 if [ $ret -ne 0 ]; then
1267 test_done $testroot $ret
1268 return 1
1271 echo "M alpha" > $testroot/stdout.expected
1272 cmp -s $testroot/stdout.expected $testroot/stdout
1273 ret=$?
1274 if [ $ret -ne 0 ]; then
1275 diff -u $testroot/stdout.expected $testroot/stdout
1276 test_done $testroot $ret
1277 return 1
1280 echo -n ALPHA > $testroot/wt/alpha.expected
1281 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
1282 ret=$?
1283 if [ $ret -ne 0 ]; then
1284 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
1286 test_done $testroot $ret
1289 test_patch_merge_simple() {
1290 local testroot=`test_init patch_merge_simple`
1292 got checkout $testroot/repo $testroot/wt > /dev/null
1293 ret=$?
1294 if [ $ret -ne 0 ]; then
1295 test_done $testroot $ret
1296 return 1
1299 jot 10 > $testroot/wt/numbers
1300 chmod +x $testroot/wt/numbers
1301 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1302 > /dev/null
1303 ret=$?
1304 if [ $ret -ne 0 ]; then
1305 test_done $testroot $ret
1306 return 1
1309 jot 10 | sed 's/4/four/g' > $testroot/wt/numbers
1311 (cd $testroot/wt && got diff > $testroot/old.diff \
1312 && got revert numbers) >/dev/null
1313 ret=$?
1314 if [ $ret -ne 0 ]; then
1315 test_done $testroot $ret
1316 return 1
1319 jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
1320 (cd $testroot/wt && got commit -m 'edit numbers') \
1321 > /dev/null
1322 ret=$?
1323 if [ $ret -ne 0 ]; then
1324 test_done $testroot $ret
1325 return 1
1328 (cd $testroot/wt && got patch $testroot/old.diff) \
1329 > $testroot/stdout
1330 ret=$?
1331 if [ $ret -ne 0 ]; then
1332 test_done $testroot $ret
1333 return 1
1336 echo 'G numbers' > $testroot/stdout.expected
1337 cmp -s $testroot/stdout $testroot/stdout.expected
1338 ret=$?
1339 if [ $ret -ne 0 ]; then
1340 diff -u $testroot/stdout $testroot/stdout.expected
1341 test_done $testroot $ret
1342 return 1
1345 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1346 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1347 ret=$?
1348 if [ $ret -ne 0 ]; then
1349 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1350 test_done $testroot $ret
1351 return 1
1354 test -x $testroot/wt/numbers
1355 ret=$?
1356 if [ $ret -ne 0 ]; then
1357 echo "numbers lost the executable bit" >&2
1359 test_done $testroot $ret
1362 test_patch_merge_gitdiff() {
1363 local testroot=`test_init patch_merge_gitdiff`
1365 jot 10 > $testroot/repo/numbers
1366 (cd $testroot/repo && git add numbers && \
1367 git_commit $testroot/repo -m "nums")
1368 ret=$?
1369 if [ $ret -ne 0 ]; then
1370 test_done $testroot $ret
1371 return 1
1374 jot 10 | sed 's/4/four/g' > $testroot/repo/numbers
1375 (cd $testroot/repo && git diff > $testroot/old.diff)
1376 ret=$?
1377 if [ $ret -ne 0 ]; then
1378 test_done $testroot $ret
1379 return 1
1382 # restore numbers
1383 jot 10 > $testroot/repo/numbers
1385 jot 10 | sed 's/6/six/g' > $testroot/repo/numbers
1386 (cd $testroot/repo && git add numbers && \
1387 git_commit $testroot/repo -m "edit")
1388 ret=$?
1389 if [ $ret -ne 0 ]; then
1390 test_done $testroot $ret
1391 return 1
1394 # now work with got:
1395 got checkout $testroot/repo $testroot/wt > /dev/null
1396 ret=$?
1397 if [ $ret -ne 0 ]; then
1398 test_done $testroot $ret
1399 return 1
1402 (cd $testroot/wt && got patch $testroot/old.diff) > $testroot/stdout
1403 ret=$?
1404 if [ $ret -ne 0 ]; then
1405 test_done $testroot $ret
1406 return 1
1409 echo 'G numbers' > $testroot/stdout.expected
1410 cmp -s $testroot/stdout $testroot/stdout.expected
1411 ret=$?
1412 if [ $ret -ne 0 ]; then
1413 diff -u $testroot/stdout $testroot/stdout.expected
1415 test_done $testroot $ret
1418 test_patch_merge_base_provided() {
1419 local testroot=`test_init patch_merge_base_provided`
1421 got checkout $testroot/repo $testroot/wt >/dev/null
1422 ret=$?
1423 if [ $ret -ne 0 ]; then
1424 test_done $testroot $ret
1425 return 1
1428 jot 10 > $testroot/wt/numbers
1429 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1430 >/dev/null
1431 ret=$?
1432 if [ $ret -ne 0 ]; then
1433 test_done $testroot $ret
1434 return 1
1437 local commit_id=`git_show_head $testroot/repo`
1439 jot 10 | sed s/4/four/ > $testroot/wt/numbers
1441 # get rid of the metadata
1442 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
1443 >/dev/null
1445 jot 10 | sed s/6/six/ > $testroot/wt/numbers
1446 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
1447 ret=$?
1448 if [ $ret -ne 0 ]; then
1449 test_done $testroot $ret
1450 return 1
1453 (cd $testroot/wt && got patch -c $commit_id patch) >$testroot/stdout
1454 ret=$?
1455 if [ $ret -ne 0 ]; then
1456 test_done $testroot $ret
1457 return 1
1460 echo 'G numbers' > $testroot/stdout.expected
1461 cmp -s $testroot/stdout $testroot/stdout.expected
1462 ret=$?
1463 if [ $ret -ne 0 ]; then
1464 diff -u $testroot/stdout $testroot/stdout.expected
1465 test_done $testroot $ret
1466 return 1
1469 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1470 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1471 ret=$?
1472 if [ $ret -ne 0 ]; then
1473 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1475 test_done $testroot $ret
1478 test_patch_merge_conflict() {
1479 local testroot=`test_init patch_merge_conflict`
1481 got checkout $testroot/repo $testroot/wt > /dev/null
1482 ret=$?
1483 if [ $ret -ne 0 ]; then
1484 test_done $testroot $ret
1485 return 1
1488 jot 10 > $testroot/wt/numbers
1489 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1490 > /dev/null
1491 ret=$?
1492 if [ $ret -ne 0 ]; then
1493 test_done $testroot $ret
1494 return 1
1497 local commit_id=`git_show_head $testroot/repo`
1499 jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
1500 echo ALPHA > $testroot/wt/alpha
1502 (cd $testroot/wt && got diff > $testroot/old.diff \
1503 && got revert alpha numbers) >/dev/null
1504 ret=$?
1505 if [ $ret -ne 0 ]; then
1506 test_done $testroot $ret
1507 return 1
1510 jot 10 | sed 's/6/3+3/g' > $testroot/wt/numbers
1511 jot -c 3 a > $testroot/wt/alpha
1512 (cd $testroot/wt && got commit -m 'edit alpha and numbers') \
1513 > /dev/null
1514 ret=$?
1515 if [ $ret -ne 0 ]; then
1516 test_done $testroot $ret
1517 return 1
1520 (cd $testroot/wt && got patch $testroot/old.diff) \
1521 > $testroot/stdout 2>/dev/null
1522 ret=$?
1523 if [ $ret -eq 0 ]; then
1524 echo "got patch merged a diff that should conflict" >&2
1525 test_done $testroot 0
1526 return 1
1529 echo 'C alpha' > $testroot/stdout.expected
1530 echo 'C numbers' >> $testroot/stdout.expected
1531 cmp -s $testroot/stdout $testroot/stdout.expected
1532 ret=$?
1533 if [ $ret -ne 0 ]; then
1534 diff -u $testroot/stdout $testroot/stdout.expected
1535 test_done $testroot $ret
1536 return 1
1539 # XXX: prefixing every line with a tab otherwise got thinks
1540 # the file has conflicts in it.
1541 cat <<-EOF > $testroot/wt/alpha.expected
1542 <<<<<<< --- alpha
1543 ALPHA
1544 ||||||| commit $commit_id
1545 alpha
1546 =======
1550 >>>>>>> +++ alpha
1551 EOF
1553 cat <<-EOF > $testroot/wt/numbers.expected
1559 <<<<<<< --- numbers
1560 six
1561 ||||||| commit $commit_id
1563 =======
1564 3+3
1565 >>>>>>> +++ numbers
1570 EOF
1572 cmp -s $testroot/wt/alpha $testroot/wt/alpha.expected
1573 ret=$?
1574 if [ $ret -ne 0 ]; then
1575 diff -u $testroot/wt/alpha $testroot/wt/alpha.expected
1576 test_done $testroot $ret
1577 return 1
1580 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1581 ret=$?
1582 if [ $ret -ne 0 ]; then
1583 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1585 test_done $testroot $ret
1588 test_patch_merge_unknown_blob() {
1589 local testroot=`test_init patch_merge_unknown_blob`
1591 got checkout $testroot/repo $testroot/wt > /dev/null
1592 ret=$?
1593 if [ $ret -ne 0 ]; then
1594 test_done $testroot $ret
1595 return 1
1598 cat <<EOF > $testroot/wt/patch
1599 I've got a
1600 diff aaaabbbbccccddddeeeeffff0000111122223333 foo/bar
1601 with a
1602 blob - aaaabbbbccccddddeeeeffff0000111122223333
1603 and also a
1604 blob + 0000111122223333444455556666777788889999
1605 for this dummy diff
1606 --- alpha
1607 +++ alpha
1608 @@ -1 +1 @@
1609 -alpha
1610 +ALPHA
1611 will it work?
1612 EOF
1614 (cd $testroot/wt/ && got patch patch) > $testroot/stdout
1615 ret=$?
1616 if [ $ret -ne 0 ]; then
1617 test_done $testroot $ret
1618 return 1
1621 echo 'M alpha' > $testroot/stdout.expected
1622 cmp -s $testroot/stdout.expected $testroot/stdout
1623 ret=$?
1624 if [ $ret -ne 0 ]; then
1625 diff -u $testroot/stdout.expected $testroot/stdout
1626 test_done $testroot $ret
1627 return 1
1630 # try again without a `diff' header
1632 cat <<EOF > $testroot/wt/patch
1633 I've got a
1634 blob - aaaabbbbccccddddeeeeffff0000111122223333
1635 and also a
1636 blob + 0000111122223333444455556666777788889999
1637 for this dummy diff
1638 --- alpha
1639 +++ alpha
1640 @@ -1 +1 @@
1641 -alpha
1642 +ALPHA
1643 will it work?
1644 EOF
1646 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1647 > $testroot/stdout
1648 ret=$?
1649 if [ $ret -ne 0 ]; then
1650 test_done $testroot $ret
1651 return 1
1654 echo 'M alpha' > $testroot/stdout.expected
1655 cmp -s $testroot/stdout.expected $testroot/stdout
1656 ret=$?
1657 if [ $ret -ne 0 ]; then
1658 diff -u $testroot/stdout.expected $testroot/stdout
1659 test_done $testroot $ret
1660 return 1
1663 # try again with a git-style diff
1665 cat <<EOF > $testroot/wt/patch
1666 diff --git a/alpha b/alpha
1667 index 0123456789ab..abcdef012345 100644
1668 --- a/alpha
1669 +++ b/alpha
1670 @@ -1 +1 @@
1671 -alpha
1672 +ALPHA
1673 EOF
1675 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1676 > $testroot/stdout
1677 ret=$?
1678 if [ $ret -ne 0 ]; then
1679 test_done $testroot $ret
1680 return 1
1683 echo 'M alpha' > $testroot/stdout.expected
1684 cmp -s $testroot/stdout.expected $testroot/stdout
1685 ret=$?
1686 if [ $ret -ne 0 ]; then
1687 diff -u $testroot/stdout.expected $testroot/stdout
1689 test_done $testroot $ret
1692 test_patch_merge_reverse() {
1693 local testroot=`test_init patch_merge_simple`
1695 got checkout $testroot/repo $testroot/wt > /dev/null
1696 ret=$?
1697 if [ $ret -ne 0 ]; then
1698 test_done $testroot $ret
1699 return 1
1702 jot 10 > $testroot/wt/numbers
1703 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1704 > /dev/null
1705 ret=$?
1706 if [ $ret -ne 0 ]; then
1707 test_done $testroot $ret
1708 return 1
1711 local commit_id=`git_show_head $testroot/repo`
1713 jot 10 | sed s/5/five/g > $testroot/wt/numbers
1714 (cd $testroot/wt && got diff > $testroot/wt/patch \
1715 && got commit -m 'edit numbers') > /dev/null
1716 ret=$?
1717 if [ $ret -ne 0 ]; then
1718 test_done $testroot $ret
1719 return 1
1722 jot 10 | sed -e s/5/five/g -e s/6/six/g > $testroot/wt/numbers
1723 (cd $testroot/wt && got commit -m 'edit numbers again') >/dev/null
1724 ret=$?
1725 if [ $ret -ne 0 ]; then
1726 test_done $testroot $ret
1727 return 1
1730 (cd $testroot/wt && got patch -R patch) >/dev/null 2>&1
1731 ret=$?
1732 if [ $ret -eq 0 ]; then
1733 echo "unexpectedly reverted the patch" >&2
1734 test_done $testroot 1
1735 return 1
1738 cat <<-EOF > $testroot/wt/numbers.expected
1743 <<<<<<< --- numbers
1746 ||||||| +++ numbers
1747 five
1748 =======
1749 five
1750 six
1751 >>>>>>> commit $commit_id
1756 EOF
1758 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1759 ret=$?
1760 if [ $ret -ne 0 ]; then
1761 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1763 test_done $testroot $ret
1766 test_patch_newfile_xbit_got_diff() {
1767 local testroot=`test_init patch_newfile_xbit`
1769 got checkout $testroot/repo $testroot/wt > /dev/null
1770 ret=$?
1771 if [ $ret -ne 0 ]; then
1772 test_done $testroot $ret
1773 return 1
1776 cat <<EOF > $testroot/wt/patch
1777 blob - /dev/null
1778 blob + abcdef0123456789abcdef012345678901234567 (mode 755)
1779 --- /dev/null
1780 +++ xfile
1781 @@ -0,0 +1,1 @@
1782 +xfile
1783 EOF
1785 (cd $testroot/wt && got patch patch) > /dev/null
1786 ret=$?
1787 if [ $ret -ne 0 ]; then
1788 test_done $testroot $ret
1789 return 1
1792 if [ ! -x $testroot/wt/xfile ]; then
1793 echo "failed to set xbit on newfile" >&2
1794 test_done $testroot 1
1795 return 1
1798 echo xfile > $testroot/wt/xfile.expected
1799 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1800 ret=$?
1801 if [ $ret -ne 0 ]; then
1802 echo "fail"
1803 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1806 test_done $testroot $ret
1809 test_patch_newfile_xbit_git_diff() {
1810 local testroot=`test_init patch_newfile_xbit`
1812 got checkout $testroot/repo $testroot/wt > /dev/null
1813 ret=$?
1814 if [ $ret -ne 0 ]; then
1815 test_done $testroot $ret
1816 return 1
1819 cat <<EOF > $testroot/wt/patch
1820 diff --git a/xfile b/xfile
1821 new file mode 100755
1822 index 00000000..abcdef01
1823 --- /dev/null
1824 +++ b/xfile
1825 @@ -0,0 +1,1 @@
1826 +xfile
1827 EOF
1829 (cd $testroot/wt && got patch patch) > /dev/null
1830 ret=$?
1831 if [ $ret -ne 0 ]; then
1832 test_done $testroot $ret
1833 return 1
1836 if [ ! -x $testroot/wt/xfile ]; then
1837 echo "failed to set xbit on newfile" >&2
1838 test_done $testroot 1
1839 return 1
1842 echo xfile > $testroot/wt/xfile.expected
1843 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1844 ret=$?
1845 if [ $ret -ne 0 ]; then
1846 echo "fail"
1847 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1850 test_done $testroot $ret
1853 test_patch_umask() {
1854 local testroot=`test_init patch_umask`
1856 got checkout "$testroot/repo" "$testroot/wt" >/dev/null
1858 cat <<EOF >$testroot/wt/patch
1859 --- alpha
1860 +++ alpha
1861 @@ -1 +1 @@
1862 -alpha
1863 +modified alpha
1864 EOF
1866 # using a subshell to avoid clobbering global umask
1867 (umask 077 && cd "$testroot/wt" && got patch <patch) >/dev/null
1868 ret=$?
1869 if [ $ret -ne 0 ]; then
1870 test_done "$testroot" $ret
1871 return 1
1874 if ! ls -l "$testroot/wt/alpha" | grep -q ^-rw-------; then
1875 echo "alpha is not 0600 after patch" >&2
1876 ls -l "$testroot/wt/alpha" >&2
1877 test_done "$testroot" 1
1878 return 1
1881 test_done "$testroot" 0
1884 test_patch_remove_binary_file() {
1885 local testroot=`test_init patch_remove_binary_file`
1887 if ! got checkout $testroot/repo $testroot/wt >/dev/null; then
1888 test_done $testroot $ret
1889 return 1
1892 dd if=/dev/zero of=$testroot/wt/x bs=1 count=16 2>/dev/null >&2
1893 (cd $testroot/wt && got add x && got commit -m +x) >/dev/null
1895 (cd $testroot/wt && \
1896 got branch demo && \
1897 got rm x && \
1898 got ci -m -x &&
1899 got up -b master) >/dev/null
1901 echo 'D x' > $testroot/stdout.expected
1903 (cd $testroot/wt && got log -c demo -l 1 -p >patch)
1905 (cd $testroot/wt && got patch <patch) > $testroot/stdout
1906 if [ $? -ne 0 ]; then
1907 echo 'patch failed' >&2
1908 test_done $testroot 1
1909 return 1
1912 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1913 diff -u $testroot/stdout.expected $testroot/stdout
1914 test_done $testroot 1
1915 return 1
1918 # try again using a git produced diff
1919 (cd $testroot/wt && got revert x) >/dev/null
1921 (cd $testroot/repo && git show demo) >$testroot/wt/patch
1923 (cd $testroot/wt && got patch <patch) > $testroot/stdout
1924 if [ $? -ne 0 ]; then
1925 echo 'patch failed' >&2
1926 test_done $testroot 1
1927 return 1
1930 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1931 diff -u $testroot/stdout.expected $testroot/stdout
1932 test_done $testroot 1
1933 return 1
1936 # try again using a diff(1) style patch
1937 (cd $testroot/wt && got revert x) >/dev/null
1939 echo "Binary files x and /dev/null differ" >$testroot/wt/patch
1940 (cd $testroot/wt && got patch <patch) >$testroot/stdout
1941 if [ $? -ne 0 ]; then
1942 echo 'patch failed' >&2
1943 test_done $testroot 1
1944 return 1
1947 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1948 diff -u $testroot/stdout.expected $testroot/stdout
1949 test_done $testroot 1
1950 return 1
1953 test_done $testroot 0
1956 test_parseargs "$@"
1957 run_test test_patch_basic
1958 run_test test_patch_dont_apply
1959 run_test test_patch_malformed
1960 run_test test_patch_no_patch
1961 run_test test_patch_equals_for_context
1962 run_test test_patch_rename
1963 run_test test_patch_illegal_status
1964 run_test test_patch_nop
1965 run_test test_patch_preserve_perm
1966 run_test test_patch_create_dirs
1967 run_test test_patch_with_offset
1968 run_test test_patch_prefer_new_path
1969 run_test test_patch_no_newline
1970 run_test test_patch_strip
1971 run_test test_patch_whitespace
1972 run_test test_patch_relative_paths
1973 run_test test_patch_with_path_prefix
1974 run_test test_patch_relpath_with_path_prefix
1975 run_test test_patch_reverse
1976 run_test test_patch_merge_simple
1977 run_test test_patch_merge_gitdiff
1978 run_test test_patch_merge_base_provided
1979 run_test test_patch_merge_conflict
1980 run_test test_patch_merge_unknown_blob
1981 run_test test_patch_merge_reverse
1982 run_test test_patch_newfile_xbit_got_diff
1983 run_test test_patch_newfile_xbit_git_diff
1984 run_test test_patch_umask
1985 run_test test_patch_remove_binary_file