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 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
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 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
142 fi
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
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 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
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 Files where patch failed to apply: 2
206 EOF
208 cmp -s $testroot/stdout.expected $testroot/stdout
209 ret=$?
210 if [ $ret -ne 0 ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
212 fi
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
224 fi
226 # missing "@@"
227 cat <<EOF > $testroot/wt/patch
228 --- alpha
229 +++ alpha
230 @@ -1 +1,2
231 +hatsuseno
232 alpha
233 EOF
235 echo -n > $testroot/stdout.expected
236 echo "got: malformed patch" > $testroot/stderr.expected
238 (cd $testroot/wt && got patch patch) \
239 > $testroot/stdout \
240 2> $testroot/stderr
241 ret=$?
242 if [ $ret -eq 0 ]; then
243 echo "got managed to apply an invalid patch"
244 test_done $testroot 1
245 return 1
246 fi
248 cmp -s $testroot/stdout.expected $testroot/stdout
249 ret=$?
250 if [ $ret -ne 0 ]; then
251 diff -u $testroot/stdout.expected $testroot/stdout
252 test_done $testroot $ret
253 return 1
254 fi
256 cmp -s $testroot/stderr.expected $testroot/stderr
257 ret=$?
258 if [ $ret -ne 0 ]; then
259 diff -u $testroot/stderr.expected $testroot/stderr
260 test_done $testroot $ret
261 return 1
262 fi
264 # wrong first character
265 cat <<EOF > $testroot/wt/patch
266 --- alpha
267 +++ alpha
268 @@ -1 +1,2 @@
269 +hatsuseno
270 alpha
271 EOF
273 (cd $testroot/wt && got patch patch) \
274 > $testroot/stdout \
275 2> $testroot/stderr
276 ret=$?
277 if [ $ret -eq 0 ]; then
278 echo "got managed to apply an invalid patch"
279 test_done $testroot 1
280 return 1
281 fi
283 cmp -s $testroot/stdout.expected $testroot/stdout
284 ret=$?
285 if [ $ret -ne 0 ]; then
286 diff -u $testroot/stdout.expected $testroot/stdout
287 test_done $testroot $ret
288 return 1
289 fi
291 cmp -s $testroot/stderr.expected $testroot/stderr
292 ret=$?
293 if [ $ret -ne 0 ]; then
294 diff -u $testroot/stderr.expected $testroot/stderr
295 test_done $testroot $ret
296 return 1
297 fi
299 # empty hunk
300 cat <<EOF > $testroot/wt/patch
301 diff --git a/alpha b/iota
302 --- a/alpha
303 +++ b/iota
304 @@ -0,0 +0,0 @@
305 EOF
307 (cd $testroot/wt && got patch patch) \
308 > $testroot/stdout \
309 2> $testroot/stderr
310 ret=$?
311 if [ $ret -eq 0 ]; then
312 echo "got managed to apply an invalid patch"
313 test_done $testroot 1
314 return 1
315 fi
317 cmp -s $testroot/stdout.expected $testroot/stdout
318 ret=$?
319 if [ $ret -ne 0 ]; then
320 diff -u $testroot/stdout.expected $testroot/stdout
321 test_done $testroot $ret
322 return 1
323 fi
325 cmp -s $testroot/stderr.expected $testroot/stderr
326 ret=$?
327 if [ $ret -ne 0 ]; then
328 diff -u $testroot/stderr.expected $testroot/stderr
329 test_done $testroot $ret
330 return 1
331 fi
333 test_done $testroot $ret
336 test_patch_no_patch() {
337 local testroot=`test_init patch_no_patch`
339 got checkout $testroot/repo $testroot/wt > /dev/null
340 ret=$?
341 if [ $ret -ne 0 ]; then
342 test_done $testroot $ret
343 return 1
344 fi
346 cat <<EOF > $testroot/wt/patch
347 hello world!
348 ...
350 some other nonsense
351 ...
353 there's no patch in here!
354 EOF
356 echo -n > $testroot/stdout.expected
357 echo "got: no patch found" > $testroot/stderr.expected
359 (cd $testroot/wt && got patch patch) \
360 > $testroot/stdout \
361 2> $testroot/stderr
362 ret=$?
363 if [ $ret -eq 0 ]; then # should fail
364 test_done $testroot 1
365 return 1
366 fi
368 cmp -s $testroot/stdout.expected $testroot/stdout
369 ret=$?
370 if [ $ret -ne 0 ]; then
371 diff -u $testroot/stdout.expected $testroot/stdout
372 test_done $testroot $ret
373 return 1
374 fi
376 cmp -s $testroot/stderr.expected $testroot/stderr
377 ret=$?
378 if [ $ret -ne 0 ]; then
379 diff -u $testroot/stderr.expected $testroot/stderr
380 test_done $testroot $ret
381 return 1
382 fi
384 test_done $testroot $ret
387 test_patch_equals_for_context() {
388 local testroot=`test_init patch_equals_for_context`
390 got checkout $testroot/repo $testroot/wt > /dev/null
391 ret=$?
392 if [ $ret -ne 0 ]; then
393 test_done $testroot $ret
394 return 1
395 fi
397 cat <<EOF > $testroot/wt/patch
398 --- alpha
399 +++ alpha
400 @@ -1 +1,2 @@
401 +hatsuseno
402 =alpha
403 EOF
405 echo "M alpha" > $testroot/stdout.expected
407 (cd $testroot/wt && got patch patch) > $testroot/stdout
408 ret=$?
409 if [ $ret -ne 0 ]; then
410 test_done $testroot $ret
411 return 1
412 fi
414 cmp -s $testroot/stdout.expected $testroot/stdout
415 ret=$?
416 if [ $ret -ne 0 ]; then
417 diff -u $testroot/stdout.expected $testroot/stdout
418 test_done $testroot $ret
419 return 1
420 fi
422 echo hatsuseno > $testroot/wt/alpha.expected
423 echo alpha >> $testroot/wt/alpha.expected
424 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
425 ret=$?
426 if [ $ret -ne 0 ]; then
427 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
428 fi
429 test_done $testroot $ret
432 test_patch_rename() {
433 local testroot=`test_init patch_rename`
435 got checkout $testroot/repo $testroot/wt > /dev/null
436 ret=$?
437 if [ $ret -ne 0 ]; then
438 test_done $testroot $ret
439 return 1
440 fi
442 cat <<EOF > $testroot/wt/patch
443 diff --git a/beta b/iota
444 similarity index 100%
445 rename from beta
446 rename to iota
447 diff --git a/alpha b/eta
448 --- a/alpha
449 +++ b/eta
450 @@ -1 +1 @@
451 -alpha
452 +eta
453 EOF
455 echo 'D beta' > $testroot/stdout.expected
456 echo 'A iota' >> $testroot/stdout.expected
457 echo 'D alpha' >> $testroot/stdout.expected
458 echo 'A eta' >> $testroot/stdout.expected
460 (cd $testroot/wt && got patch patch) > $testroot/stdout
461 ret=$?
462 if [ $ret -ne 0 ]; then
463 test_done $testroot $ret
464 return 1
465 fi
467 cmp -s $testroot/stdout.expected $testroot/stdout
468 ret=$?
469 if [ $ret -ne 0 ]; then
470 diff -u $testroot/stdout.expected $testroot/stdout
471 test_done $testroot $ret
472 return 1
473 fi
475 if [ -f $testroot/wt/alpha -o -f $testroot/wt/beta ]; then
476 echo "alpha or beta were not removed" >&2
477 test_done $testroot 1
478 return 1
479 fi
480 if [ ! -f $testroot/wt/iota -o ! -f $testroot/wt/eta ]; then
481 echo "iota or eta were not created" >&2
482 test_done $testroot 1
483 return 1
484 fi
486 echo beta > $testroot/wt/iota.expected
487 cmp -s $testroot/wt/iota.expected $testroot/wt/iota
488 ret=$?
489 if [ $ret -ne 0 ]; then
490 diff -u $testroot/wt/iota.expected $testroot/wt/iota
491 test_done $testroot $ret
492 return 1
493 fi
495 echo eta > $testroot/wt/eta.expected
496 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
497 ret=$?
498 if [ $ret -ne 0 ]; then
499 diff -u $testroot/wt/eta.expected $testroot/wt/eta
500 test_done $testroot $ret
501 return 1
502 fi
504 test_done $testroot $ret
507 test_patch_illegal_status() {
508 local testroot=`test_init patch_illegal_status`
510 got checkout $testroot/repo $testroot/wt > /dev/null
511 ret=$?
512 if [ $ret -ne 0 ]; then
513 test_done $testroot $ret
514 return 1
515 fi
517 # try to patch an obstructed file, add a versioned one, edit a
518 # non existent file and an unversioned one, and remove a
519 # non existent file.
520 cat <<EOF > $testroot/wt/patch
521 --- alpha
522 +++ alpha
523 @@ -1 +1,2 @@
524 alpha
525 +was edited
526 --- /dev/null
527 +++ beta
528 @@ -0,0 +1 @@
529 +beta
530 --- iota
531 +++ iota
532 @@ -1 +1 @@
533 -iota
534 +IOTA
535 --- kappa
536 +++ kappa
537 @@ -1 +1 @@
538 -kappa
539 +KAPPA
540 --- lambda
541 +++ /dev/null
542 @@ -1 +0,0 @@
543 -lambda
544 EOF
546 echo kappa > $testroot/wt/kappa
547 rm $testroot/wt/alpha
548 mkdir $testroot/wt/alpha
550 (cd $testroot/wt && got patch patch) > $testroot/stdout \
551 2> $testroot/stderr
552 ret=$?
553 if [ $ret -eq 0 ]; then
554 echo "edited a missing file" >&2
555 test_done $testroot 1
556 return 1
557 fi
559 cat <<EOF > $testroot/stdout.expected
560 # alpha
561 # beta
562 # iota
563 # kappa
564 # lambda
565 Files where patch failed to apply: 5
566 EOF
568 cat <<EOF > $testroot/stderr.expected
569 got: alpha: file has unexpected status
570 got: beta: file has unexpected status
571 got: iota: No such file or directory
572 got: kappa: file has unexpected status
573 got: lambda: No such file or directory
574 got: patch failed to apply
575 EOF
577 cmp -s $testroot/stdout.expected $testroot/stdout
578 ret=$?
579 if [ $ret -ne 0 ]; then
580 diff -u $testroot/stdout.expected $testroot/stdout
581 test_done $testroot $ret
582 return 1
583 fi
585 cmp -s $testroot/stderr.expected $testroot/stderr
586 ret=$?
587 if [ $ret -ne 0 ]; then
588 diff -u $testroot/stderr.expected $testroot/stderr
589 test_done $testroot $ret
590 return 1
591 fi
593 (cd $testroot/wt && got status) > $testroot/stdout
594 cat <<EOF > $testroot/stdout.expected
595 ~ alpha
596 ? kappa
597 ? patch
598 EOF
600 cmp -s $testroot/stdout.expected $testroot/stdout
601 ret=$?
602 if [ $ret -ne 0 ]; then
603 diff -u $testroot/stdout.expected $testroot/stdout
604 fi
605 test_done $testroot $ret
608 test_patch_nop() {
609 local testroot=`test_init patch_nop`
611 got checkout $testroot/repo $testroot/wt > /dev/null
612 ret=$?
613 if [ $ret -ne 0 ]; then
614 test_done $testroot $ret
615 return 1
616 fi
618 cat <<EOF > $testroot/wt/patch
619 --- alpha
620 +++ alpha
621 @@ -1 +1 @@
622 -alpha
623 +cafe alpha
624 --- beta
625 +++ /dev/null
626 @@ -1 +0,0 @@
627 -beta
628 diff --git a/gamma/delta b/gamma/delta.new
629 --- gamma/delta
630 +++ gamma/delta.new
631 @@ -1 +1 @@
632 -delta
633 +delta updated and renamed!
634 EOF
636 (cd $testroot/wt && got patch -n patch)
637 ret=$?
638 if [ $ret -ne 0 ]; then
639 test_done $testroot $ret
640 return 1
641 fi
643 # remove the patch to avoid the ? entry
644 rm $testroot/wt/patch
646 (cd $testroot/wt && got status) > $testroot/stdout
647 ret=$?
648 if [ $ret -ne 0 ]; then
649 test_done $testroot $ret
650 return 1
651 fi
653 echo -n > $testroot/stdout.expected
654 cmp -s $testroot/stdout.expected $testroot/stdout
655 ret=$?
656 if [ $ret -ne 0 ]; then
657 diff -u $testroot/stdout.expected $testroot/stdout
658 fi
659 test_done $testroot $ret
662 test_patch_preserve_perm() {
663 local testroot=`test_init patch_preserve_perm`
665 got checkout $testroot/repo $testroot/wt > /dev/null
666 ret=$?
667 if [ $ret -ne 0 ]; then
668 test_done $testroot $ret
669 return 1
670 fi
672 chmod +x $testroot/wt/alpha
673 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
674 ret=$?
675 if [ $ret -ne 0 ]; then
676 test_done $testroot $ret
677 return 1
678 fi
680 cat <<EOF > $testroot/wt/patch
681 --- alpha
682 +++ alpha
683 @@ -1 +1,2 @@
684 alpha
685 +was edited
686 EOF
688 (cd $testroot/wt && got patch patch) > /dev/null
689 ret=$?
690 if [ $ret -ne 0 ]; then
691 test_done $testroot $ret
692 return 1
693 fi
695 if [ ! -x $testroot/wt/alpha ]; then
696 echo "alpha is no more executable!" >&2
697 test_done $testroot 1
698 return 1
699 fi
700 test_done $testroot 0
703 test_patch_create_dirs() {
704 local testroot=`test_init patch_create_dirs`
706 got checkout $testroot/repo $testroot/wt > /dev/null
707 ret=$?
708 if [ $ret -ne 0 ]; then
709 test_done $testroot $ret
710 return 1
711 fi
713 cat <<EOF > $testroot/wt/patch
714 --- /dev/null
715 +++ iota/kappa/lambda
716 @@ -0,0 +1 @@
717 +lambda
718 EOF
720 (cd $testroot/wt && got patch patch) > $testroot/stdout
721 ret=$?
722 if [ $ret -ne 0 ]; then
723 test_done $testroot $ret
724 return 1
725 fi
727 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
728 cmp -s $testroot/stdout.expected $testroot/stdout
729 ret=$?
730 if [ $ret -ne 0 ]; then
731 diff -u $testroot/stdout.expected $testroot/stdout
732 test_done $testroot $ret
733 return 1
734 fi
736 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
737 echo "file not created!" >&2
738 test_done $testroot $ret
739 return 1
740 fi
741 test_done $testroot 0
744 test_patch_with_offset() {
745 local testroot=`test_init patch_with_offset`
747 got checkout $testroot/repo $testroot/wt > /dev/null
748 ret=$?
749 if [ $ret -ne 0 ]; then
750 test_done $testroot $ret
751 return 1
752 fi
754 cat <<EOF > $testroot/wt/patch
755 --- numbers
756 +++ numbers
757 @@ -47,7 +47,7 @@
758 47
759 48
760 49
761 -50
762 +midway tru it!
763 51
764 52
765 53
766 @@ -87,7 +87,7 @@
767 87
768 88
769 89
770 -90
771 +almost there!
772 91
773 92
774 93
775 EOF
777 seq 100 > $testroot/wt/numbers
778 ed -s "$testroot/wt/numbers" <<EOF
779 1,10d
780 50r !seq 20
783 EOF
785 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
786 > /dev/null
787 ret=$?
788 if [ $ret -ne 0 ]; then
789 test_done $testroot $ret
790 return 1
791 fi
793 (cd $testroot/wt && got patch patch) > $testroot/stdout
794 ret=$?
795 if [ $ret -ne 0 ]; then
796 test_done $testroot/wt $ret
797 return 1
798 fi
800 cat <<EOF > $testroot/stdout.expected
801 M numbers
802 @@ -47,7 +47,7 @@ applied with offset -10
803 @@ -87,7 +87,7 @@ applied with offset 10
804 EOF
806 cmp -s $testroot/stdout.expected $testroot/stdout
807 ret=$?
808 if [ $ret -ne 0 ]; then
809 diff -u $testroot/stdout.expected $testroot/stdout
810 fi
811 test_done $testroot $ret
814 test_patch_empty_file() {
815 local testroot=`test_init patch_empty_file`
817 got checkout $testroot/repo $testroot/wt > /dev/null
818 ret=$?
819 if [ $ret -ne 0 ]; then
820 test_done $testroot $ret
821 return 1
822 fi
824 echo -n > $testroot/wt/alpha
825 (cd "$testroot/wt" && got commit -m 'edit alpha' alpha) >/dev/null
827 # try a patch which re-adds file contents; should suceeed
828 cat <<EOF >$testroot/wt/patch
829 --- alpha
830 +++ alpha
831 @@ -0,0 +1 @@
832 +alpha
833 EOF
834 (cd $testroot/wt && got patch patch) > $testroot/stdout
835 ret=$?
836 if [ $ret -ne 0 ]; then
837 test_done $testroot $ret
838 return 1
839 fi
840 echo 'M alpha' > $testroot/stdout.expected
841 cmp -s $testroot/stdout.expected $testroot/stdout
842 ret=$?
843 if [ $ret -ne 0 ]; then
844 diff -u $testroot/stdout.expected $testroot/stdout
845 test_done $testroot $ret
846 return 1
847 fi
849 (cd $testroot/wt && got revert -R . > /dev/null)
851 # try a patch which edits the file; should fail
852 cat <<EOF >$testroot/wt/patch
853 --- alpha
854 +++ alpha
855 @@ -1 +1 @@
856 -alpha
857 +beta
858 EOF
859 (cd $testroot/wt && got patch patch) > $testroot/stdout \
860 2> $testroot/stderr
861 ret=$?
862 if [ $ret -eq 0 ]; then
863 echo "got patch suceeded unexpectedly" >&2
864 test_done $testroot $ret
865 return 1
866 fi
868 echo 'got: patch failed to apply' > $testroot/stderr.expected
869 cmp -s $testroot/stderr.expected $testroot/stderr
870 ret=$?
871 if [ $ret -ne 0 ]; then
872 diff -u $testroot/stderr.expected $testroot/stderr
873 test_done $testroot $ret
874 return 1
875 fi
877 (cd $testroot/wt && got revert -R . > /dev/null)
879 # try a patch which deletes lines from the file; should fail
880 cat <<EOF >$testroot/wt/patch
881 --- alpha
882 +++ alpha
883 @@ -1,2 +0 @@
884 -alpha
885 -beta
886 EOF
887 (cd $testroot/wt && got patch patch) > $testroot/stdout \
888 2> $testroot/stderr
889 ret=$?
890 if [ $ret -eq 0 ]; then
891 echo "got patch suceeded unexpectedly" >&2
892 test_done $testroot $ret
893 return 1
894 fi
895 echo 'got: patch failed to apply' > $testroot/stderr.expected
896 cmp -s $testroot/stderr.expected $testroot/stderr
897 ret=$?
898 if [ $ret -ne 0 ]; then
899 diff -u $testroot/stderr.expected $testroot/stderr
900 fi
901 test_done $testroot $ret
904 test_patch_prefer_new_path() {
905 local testroot=`test_init patch_orig`
907 got checkout $testroot/repo $testroot/wt > /dev/null
908 ret=$?
909 if [ $ret -ne 0 ]; then
910 test_done $testroot $ret
911 return 1
912 fi
914 cat <<EOF > $testroot/wt/patch
915 --- alpha.orig
916 +++ alpha
917 @@ -1 +1,2 @@
918 alpha
919 +was edited
920 EOF
922 (cd $testroot/wt && got patch patch) > $testroot/stdout
923 ret=$?
924 if [ $ret -ne 0 ]; then
925 test_done $testroot $ret
926 return 1
927 fi
929 echo 'M alpha' > $testroot/stdout.expected
930 cmp -s $testroot/stdout.expected $testroot/stdout
931 ret=$?
932 if [ $ret -ne 0 ]; then
933 diff -u $testroot/stdout.expected $testroot/stdout
934 fi
935 test_done $testroot $ret
938 test_patch_no_newline() {
939 local testroot=`test_init patch_no_newline`
941 got checkout $testroot/repo $testroot/wt > /dev/null
942 ret=$?
943 if [ $ret -ne 0 ]; then
944 test_done $testroot $ret
945 return 1
946 fi
948 cat <<EOF > $testroot/wt/patch
949 --- /dev/null
950 +++ eta
951 @@ -0,0 +1 @@
952 +eta
953 \ No newline at end of file
954 EOF
956 (cd $testroot/wt && got patch patch) > $testroot/stdout
957 ret=$?
958 if [ $ret -ne 0 ]; then
959 test_done $testroot $ret
960 return 1
961 fi
963 echo "A eta" > $testroot/stdout.expected
964 cmp -s $testroot/stdout.expected $testroot/stdout
965 ret=$?
966 if [ $ret -ne 0 ]; then
967 diff -u $testroot/stdout.expected $testroot/stdout
968 test_done $testroot $ret
969 return 1
970 fi
972 echo -n eta > $testroot/wt/eta.expected
973 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
974 ret=$?
975 if [ $ret -ne 0 ]; then
976 diff -u $testroot/wt/eta.expected $testroot/wt/eta
977 test_done $testroot $ret
978 return 1
979 fi
981 (cd $testroot/wt && got commit -m 'add eta') > /dev/null
982 ret=$?
983 if [ $ret -ne 0 ]; then
984 test_done $testroot $ret
985 return 1
986 fi
988 cat <<EOF > $testroot/wt/patch
989 --- eta
990 +++ eta
991 @@ -1 +1 @@
992 -eta
993 \ No newline at end of file
994 +ETA
995 \ No newline at end of file
996 EOF
998 (cd $testroot/wt && got patch patch) > $testroot/stdout
999 ret=$?
1000 if [ $ret -ne 0 ]; then
1001 test_done $testroot $ret
1002 return 1
1005 echo "M eta" > $testroot/stdout.expected
1006 cmp -s $testroot/stdout.expected $testroot/stdout
1007 ret=$?
1008 if [ $ret -ne 0 ]; then
1009 diff -u $testroot/stdout.expected $testroot/stdout
1010 test_done $testroot $ret
1011 return 1
1014 echo -n ETA > $testroot/wt/eta.expected
1015 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1016 ret=$?
1017 if [ $ret -ne 0 ]; then
1018 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1019 test_done $testroot $ret
1020 return 1
1023 (cd $testroot/wt && got commit -m 'edit eta') > /dev/null
1024 ret=$?
1025 if [ $ret -ne 0 ]; then
1026 test_done $testroot $ret
1027 return 1
1030 cat <<EOF > $testroot/wt/patch
1031 --- eta
1032 +++ eta
1033 @@ -1 +1 @@
1034 -ETA
1035 \ No newline at end of file
1036 +eta
1037 EOF
1039 (cd $testroot/wt && got patch patch) > $testroot/stdout
1040 ret=$?
1041 if [ $ret -ne 0 ]; then
1042 test_done $testroot $ret
1043 return 1
1046 echo "M eta" > $testroot/stdout.expected
1047 cmp -s $testroot/stdout.expected $testroot/stdout
1048 ret=$?
1049 if [ $ret -ne 0 ]; then
1050 diff -u $testroot/stdout.expected $testroot/stdout
1051 test_done $testroot $ret
1052 return 1
1055 echo eta > $testroot/wt/eta.expected
1056 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
1057 ret=$?
1058 if [ $ret -ne 0 ]; then
1059 diff -u $testroot/wt/eta.expected $testroot/wt/eta
1061 test_done $testroot $ret
1064 test_patch_strip() {
1065 local testroot=`test_init patch_strip`
1067 got checkout $testroot/repo $testroot/wt > /dev/null
1068 ret=$?
1069 if [ $ret -ne 0 ]; then
1070 test_done $testroot $ret
1071 return 1
1074 cat <<EOF > $testroot/wt/patch
1075 --- foo/bar/alpha.orig
1076 +++ foo/bar/alpha
1077 @@ -1 +1 @@
1078 -alpha
1079 +ALPHA
1080 EOF
1082 (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout
1083 ret=$?
1084 if [ $ret -ne 0 ]; then
1085 test_done $testroot $ret
1086 return 1
1089 echo "M alpha" >> $testroot/stdout.expected
1090 cmp -s $testroot/stdout.expected $testroot/stdout
1091 ret=$?
1092 if [ $ret -ne 0 ]; then
1093 diff -u $testroot/stdout.expected $testroot/stdout
1094 test_done $testroot $ret
1095 return 1
1098 (cd $testroot/wt && got revert alpha) > /dev/null 2>&1
1099 ret=$?
1100 if [ $ret -ne 0 ]; then
1101 test_done $testroot $ret
1102 return 1
1105 (cd $testroot/wt && got patch -p3 patch) \
1106 2> $testroot/stderr
1107 ret=$?
1108 if [ $ret -eq 0 ]; then
1109 echo "stripped more components than available!"
1110 test_done $testroot 1
1111 return 1
1114 cat <<EOF > $testroot/stderr.expected
1115 got: can't strip 1 path-components from foo/bar/alpha: bad path
1116 EOF
1118 cmp -s $testroot/stderr.expected $testroot/stderr
1119 ret=$?
1120 if [ $ret -ne 0 ]; then
1121 diff -u $testroot/stderr.expected $testroot/stderr
1123 test_done $testroot 0
1126 test_patch_whitespace() {
1127 local testroot=`test_init patch_whitespace`
1129 got checkout $testroot/repo $testroot/wt > /dev/null
1130 ret=$?
1131 if [ $ret -ne 0 ]; then
1132 test_done $testroot $ret
1133 return 1
1136 trailing=" "
1138 cat <<EOF > $testroot/wt/hello.c
1139 #include <stdio.h>
1141 int
1142 main(void)
1144 /* the trailing whitespace is on purpose */
1145 printf("hello, world\n");$trailing
1146 return 0;
1148 EOF
1150 (cd $testroot/wt && got add hello.c && got ci -m '+hello.c') \
1151 > /dev/null
1152 ret=$?
1153 if [ $ret -ne 0 ]; then
1154 test_done $testroot $ret
1155 return 1
1158 # test with a diff with various whitespace corruptions
1159 cat <<EOF > $testroot/wt/patch
1160 --- hello.c
1161 +++ hello.c
1162 @@ -5,5 +5,5 @@
1164 /* the trailing whitespace is on purpose */
1165 printf("hello, world\n");
1166 - return 0;
1167 + return 5; /* always fails */
1169 EOF
1171 (cd $testroot/wt && got patch patch) \
1172 2>$testroot/stderr >$testroot/stdout
1173 ret=$?
1174 if [ $ret -ne 0 ]; then
1175 echo "failed to apply diff" >&2
1176 test_done $testroot $ret
1177 return 1
1180 echo 'M hello.c' > $testroot/stdout.expected
1181 echo '@@ -5,5 +5,5 @@ hunk contains mangled whitespace' \
1182 >> $testroot/stdout.expected
1183 cmp -s $testroot/stdout.expected $testroot/stdout
1184 ret=$?
1185 if [ $ret -ne 0 ]; then
1186 diff -u $testroot/stdout.expected $testroot/stdout
1187 test_done $testroot $ret
1188 return 1
1191 cat <<EOF > $testroot/wt/hello.c.expected
1192 #include <stdio.h>
1194 int
1195 main(void)
1197 /* the trailing whitespace is on purpose */
1198 printf("hello, world\n");$trailing
1199 return 5; /* always fails */
1201 EOF
1203 cmp -s $testroot/wt/hello.c.expected $testroot/wt/hello.c
1204 ret=$?
1205 if [ $ret -ne 0 ]; then
1206 diff -u $testroot/wt/hello.c.expected $testroot/wt/hello.c
1208 test_done $testroot $ret
1211 test_patch_relative_paths() {
1212 local testroot=`test_init patch_relative_paths`
1214 got checkout $testroot/repo $testroot/wt > /dev/null
1215 ret=$?
1216 if [ $ret -ne 0 ]; then
1217 test_done $testroot $ret
1218 return 1
1221 cat <<EOF > $testroot/wt/gamma/patch
1222 --- delta
1223 +++ delta
1224 @@ -1 +1 @@
1225 -delta
1226 +DELTA
1227 --- /dev/null
1228 +++ eta
1229 @@ -0,0 +1 @@
1230 +eta
1231 EOF
1233 (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
1234 ret=$?
1235 if [ $ret -ne 0 ]; then
1236 test_done $testroot $ret
1237 return 1
1240 echo 'M gamma/delta' > $testroot/stdout.expected
1241 echo 'A gamma/eta' >> $testroot/stdout.expected
1243 cmp -s $testroot/stdout.expected $testroot/stdout
1244 ret=$?
1245 if [ $ret -ne 0 ]; then
1246 diff -u $testroot/stdout.expected $testroot/stdout
1248 test_done $testroot $ret
1251 test_patch_with_path_prefix() {
1252 local testroot=`test_init patch_with_path_prefix`
1254 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1255 ret=$?
1256 if [ $ret -ne 0 ]; then
1257 test_done $testroot $ret
1258 return 1
1261 cat <<EOF > $testroot/wt/patch
1262 --- delta
1263 +++ delta
1264 @@ -1 +1 @@
1265 -delta
1266 +DELTA
1267 --- /dev/null
1268 +++ eta
1269 @@ -0,0 +1 @@
1270 +eta
1271 EOF
1273 (cd $testroot/wt && got patch patch) > $testroot/stdout
1274 ret=$?
1275 if [ $ret -ne 0 ]; then
1276 test_done $testroot $ret
1277 return 1
1280 echo 'M delta' > $testroot/stdout.expected
1281 echo 'A eta' >> $testroot/stdout.expected
1283 cmp -s $testroot/stdout.expected $testroot/stdout
1284 ret=$?
1285 if [ $ret -ne 0 ]; then
1286 diff -u $testroot/stdout.expected $testroot/stdout
1288 test_done $testroot $ret
1291 test_patch_relpath_with_path_prefix() {
1292 local testroot=`test_init patch_relpaths_with_path_prefix`
1294 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1295 ret=$?
1296 if [ $ret -ne 0 ]; then
1297 test_done $testroot $ret
1298 return 1
1301 mkdir -p $testroot/wt/epsilon/zeta/
1303 cat <<EOF > $testroot/wt/patch
1304 --- /dev/null
1305 +++ zeta/theta
1306 @@ -0,0 +1 @@
1307 +theta
1308 EOF
1310 (cd $testroot/wt/epsilon/zeta && got patch -p1 $testroot/wt/patch) \
1311 > $testroot/stdout
1312 ret=$?
1313 if [ $ret -ne 0 ]; then
1314 test_done $testroot $ret
1315 return 1
1318 echo 'A epsilon/zeta/theta' >> $testroot/stdout.expected
1320 cmp -s $testroot/stdout.expected $testroot/stdout
1321 ret=$?
1322 if [ $ret -ne 0 ]; then
1323 diff -u $testroot/stdout.expected $testroot/stdout
1324 test_done $testroot $ret
1325 return 1
1328 echo 'theta' > $testroot/theta.expected
1329 cmp -s $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1330 ret=$?
1331 if [ $ret -ne 0 ]; then
1332 diff -u $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1334 test_done $testroot $ret
1337 test_patch_reverse() {
1338 local testroot=`test_init patch_reverse`
1340 got checkout $testroot/repo $testroot/wt > /dev/null
1341 ret=$?
1342 if [ $ret -ne 0 ]; then
1343 test_done $testroot $ret
1344 return 1
1347 cat <<EOF > $testroot/wt/patch
1348 --- alpha
1349 +++ alpha
1350 @@ -1 +1 @@
1351 -ALPHA
1352 \ No newline at end of file
1353 +alpha
1354 EOF
1356 (cd $testroot/wt && got patch -R patch) > $testroot/stdout
1357 ret=$?
1358 if [ $ret -ne 0 ]; then
1359 test_done $testroot $ret
1360 return 1
1363 echo "M alpha" > $testroot/stdout.expected
1364 cmp -s $testroot/stdout.expected $testroot/stdout
1365 ret=$?
1366 if [ $ret -ne 0 ]; then
1367 diff -u $testroot/stdout.expected $testroot/stdout
1368 test_done $testroot $ret
1369 return 1
1372 echo -n ALPHA > $testroot/wt/alpha.expected
1373 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
1374 ret=$?
1375 if [ $ret -ne 0 ]; then
1376 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
1378 test_done $testroot $ret
1381 test_patch_merge_simple() {
1382 local testroot=`test_init patch_merge_simple`
1384 got checkout $testroot/repo $testroot/wt > /dev/null
1385 ret=$?
1386 if [ $ret -ne 0 ]; then
1387 test_done $testroot $ret
1388 return 1
1391 seq 10 > $testroot/wt/numbers
1392 chmod +x $testroot/wt/numbers
1393 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1394 > /dev/null
1395 ret=$?
1396 if [ $ret -ne 0 ]; then
1397 test_done $testroot $ret
1398 return 1
1401 seq 10 | sed 's/4/four/g' > $testroot/wt/numbers
1403 (cd $testroot/wt && got diff > $testroot/old.diff \
1404 && got revert numbers) >/dev/null
1405 ret=$?
1406 if [ $ret -ne 0 ]; then
1407 test_done $testroot $ret
1408 return 1
1411 seq 10 | sed 's/6/six/g' > $testroot/wt/numbers
1412 (cd $testroot/wt && got commit -m 'edit numbers') \
1413 > /dev/null
1414 ret=$?
1415 if [ $ret -ne 0 ]; then
1416 test_done $testroot $ret
1417 return 1
1420 (cd $testroot/wt && got patch $testroot/old.diff) \
1421 > $testroot/stdout
1422 ret=$?
1423 if [ $ret -ne 0 ]; then
1424 test_done $testroot $ret
1425 return 1
1428 echo 'G numbers' > $testroot/stdout.expected
1429 cmp -s $testroot/stdout $testroot/stdout.expected
1430 ret=$?
1431 if [ $ret -ne 0 ]; then
1432 diff -u $testroot/stdout $testroot/stdout.expected
1433 test_done $testroot $ret
1434 return 1
1437 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1438 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1439 ret=$?
1440 if [ $ret -ne 0 ]; then
1441 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1442 test_done $testroot $ret
1443 return 1
1446 test -x $testroot/wt/numbers
1447 ret=$?
1448 if [ $ret -ne 0 ]; then
1449 echo "numbers lost the executable bit" >&2
1451 test_done $testroot $ret
1454 test_patch_merge_gitdiff() {
1455 local testroot=`test_init patch_merge_gitdiff`
1457 seq 10 > $testroot/repo/numbers
1458 git -C $testroot/repo add numbers && \
1459 git_commit $testroot/repo -m "nums"
1460 ret=$?
1461 if [ $ret -ne 0 ]; then
1462 test_done $testroot $ret
1463 return 1
1466 seq 10 | sed 's/4/four/g' > $testroot/repo/numbers
1467 git -C $testroot/repo diff > $testroot/old.diff
1468 ret=$?
1469 if [ $ret -ne 0 ]; then
1470 test_done $testroot $ret
1471 return 1
1474 # restore numbers
1475 seq 10 > $testroot/repo/numbers
1477 seq 10 | sed 's/6/six/g' > $testroot/repo/numbers
1478 git -C $testroot/repo add numbers && \
1479 git_commit $testroot/repo -m "edit"
1480 ret=$?
1481 if [ $ret -ne 0 ]; then
1482 test_done $testroot $ret
1483 return 1
1486 # now work with got:
1487 got checkout $testroot/repo $testroot/wt > /dev/null
1488 ret=$?
1489 if [ $ret -ne 0 ]; then
1490 test_done $testroot $ret
1491 return 1
1494 (cd $testroot/wt && got patch $testroot/old.diff) > $testroot/stdout
1495 ret=$?
1496 if [ $ret -ne 0 ]; then
1497 test_done $testroot $ret
1498 return 1
1501 echo 'G numbers' > $testroot/stdout.expected
1502 cmp -s $testroot/stdout $testroot/stdout.expected
1503 ret=$?
1504 if [ $ret -ne 0 ]; then
1505 diff -u $testroot/stdout $testroot/stdout.expected
1507 test_done $testroot $ret
1510 test_patch_merge_base_provided() {
1511 local testroot=`test_init patch_merge_base_provided`
1513 got checkout $testroot/repo $testroot/wt >/dev/null
1514 ret=$?
1515 if [ $ret -ne 0 ]; then
1516 test_done $testroot $ret
1517 return 1
1520 seq 10 > $testroot/wt/numbers
1521 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1522 >/dev/null
1523 ret=$?
1524 if [ $ret -ne 0 ]; then
1525 test_done $testroot $ret
1526 return 1
1529 local commit_id=`git_show_head $testroot/repo`
1531 seq 10 | sed s/4/four/ > $testroot/wt/numbers
1533 # get rid of the metadata
1534 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
1535 >/dev/null
1537 seq 10 | sed s/6/six/ > $testroot/wt/numbers
1538 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
1539 ret=$?
1540 if [ $ret -ne 0 ]; then
1541 test_done $testroot $ret
1542 return 1
1545 (cd $testroot/wt && got patch -c $commit_id patch) >$testroot/stdout
1546 ret=$?
1547 if [ $ret -ne 0 ]; then
1548 test_done $testroot $ret
1549 return 1
1552 echo 'G numbers' > $testroot/stdout.expected
1553 cmp -s $testroot/stdout $testroot/stdout.expected
1554 ret=$?
1555 if [ $ret -ne 0 ]; then
1556 diff -u $testroot/stdout $testroot/stdout.expected
1557 test_done $testroot $ret
1558 return 1
1561 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1562 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1563 ret=$?
1564 if [ $ret -ne 0 ]; then
1565 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1567 test_done $testroot $ret
1570 test_patch_merge_conflict() {
1571 local testroot=`test_init patch_merge_conflict`
1573 got checkout $testroot/repo $testroot/wt > /dev/null
1574 ret=$?
1575 if [ $ret -ne 0 ]; then
1576 test_done $testroot $ret
1577 return 1
1580 seq 10 > $testroot/wt/numbers
1581 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1582 > /dev/null
1583 ret=$?
1584 if [ $ret -ne 0 ]; then
1585 test_done $testroot $ret
1586 return 1
1589 local commit_id=`git_show_head $testroot/repo`
1591 seq 10 | sed 's/6/six/g' > $testroot/wt/numbers
1592 echo ALPHA > $testroot/wt/alpha
1594 (cd $testroot/wt && got diff > $testroot/old.diff \
1595 && got revert alpha numbers) >/dev/null
1596 ret=$?
1597 if [ $ret -ne 0 ]; then
1598 test_done $testroot $ret
1599 return 1
1602 seq 10 | sed 's/6/3+3/g' > $testroot/wt/numbers
1603 printf '%s\n' a b c > $testroot/wt/alpha
1604 (cd $testroot/wt && got commit -m 'edit alpha and numbers') \
1605 > /dev/null
1606 ret=$?
1607 if [ $ret -ne 0 ]; then
1608 test_done $testroot $ret
1609 return 1
1612 (cd $testroot/wt && got patch $testroot/old.diff) \
1613 > $testroot/stdout 2>/dev/null
1614 ret=$?
1615 if [ $ret -eq 0 ]; then
1616 echo "got patch merged a diff that should conflict" >&2
1617 test_done $testroot 1
1618 return 1
1621 echo 'C alpha' > $testroot/stdout.expected
1622 echo 'C numbers' >> $testroot/stdout.expected
1623 echo 'Files with merge conflicts: 2' >> $testroot/stdout.expected
1624 cmp -s $testroot/stdout $testroot/stdout.expected
1625 ret=$?
1626 if [ $ret -ne 0 ]; then
1627 diff -u $testroot/stdout $testroot/stdout.expected
1628 test_done $testroot $ret
1629 return 1
1632 # XXX: prefixing every line with a tab otherwise got thinks
1633 # the file has conflicts in it.
1634 cat <<-EOF > $testroot/wt/alpha.expected
1635 <<<<<<< --- alpha
1636 ALPHA
1637 ||||||| commit $commit_id
1638 alpha
1639 =======
1643 >>>>>>> +++ alpha
1644 EOF
1646 cat <<-EOF > $testroot/wt/numbers.expected
1652 <<<<<<< --- numbers
1653 six
1654 ||||||| commit $commit_id
1656 =======
1657 3+3
1658 >>>>>>> +++ numbers
1663 EOF
1665 cmp -s $testroot/wt/alpha $testroot/wt/alpha.expected
1666 ret=$?
1667 if [ $ret -ne 0 ]; then
1668 diff -u $testroot/wt/alpha $testroot/wt/alpha.expected
1669 test_done $testroot $ret
1670 return 1
1673 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1674 ret=$?
1675 if [ $ret -ne 0 ]; then
1676 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1678 test_done $testroot $ret
1681 test_patch_merge_unknown_blob() {
1682 local testroot=`test_init patch_merge_unknown_blob`
1684 got checkout $testroot/repo $testroot/wt > /dev/null
1685 ret=$?
1686 if [ $ret -ne 0 ]; then
1687 test_done $testroot $ret
1688 return 1
1691 cat <<EOF > $testroot/wt/patch
1692 I've got a
1693 diff aaaabbbbccccddddeeeeffff0000111122223333 foo/bar
1694 with a
1695 blob - aaaabbbbccccddddeeeeffff0000111122223333
1696 and also a
1697 blob + 0000111122223333444455556666777788889999
1698 for this dummy diff
1699 --- alpha
1700 +++ alpha
1701 @@ -1 +1 @@
1702 -alpha
1703 +ALPHA
1704 will it work?
1705 EOF
1707 (cd $testroot/wt/ && got patch patch) > $testroot/stdout
1708 ret=$?
1709 if [ $ret -ne 0 ]; then
1710 test_done $testroot $ret
1711 return 1
1714 echo 'M alpha' > $testroot/stdout.expected
1715 cmp -s $testroot/stdout.expected $testroot/stdout
1716 ret=$?
1717 if [ $ret -ne 0 ]; then
1718 diff -u $testroot/stdout.expected $testroot/stdout
1719 test_done $testroot $ret
1720 return 1
1723 # try again without a `diff' header
1725 cat <<EOF > $testroot/wt/patch
1726 I've got a
1727 blob - aaaabbbbccccddddeeeeffff0000111122223333
1728 and also a
1729 blob + 0000111122223333444455556666777788889999
1730 for this dummy diff
1731 --- alpha
1732 +++ alpha
1733 @@ -1 +1 @@
1734 -alpha
1735 +ALPHA
1736 will it work?
1737 EOF
1739 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1740 > $testroot/stdout
1741 ret=$?
1742 if [ $ret -ne 0 ]; then
1743 test_done $testroot $ret
1744 return 1
1747 echo 'M alpha' > $testroot/stdout.expected
1748 cmp -s $testroot/stdout.expected $testroot/stdout
1749 ret=$?
1750 if [ $ret -ne 0 ]; then
1751 diff -u $testroot/stdout.expected $testroot/stdout
1752 test_done $testroot $ret
1753 return 1
1756 # try again with a git-style diff
1758 cat <<EOF > $testroot/wt/patch
1759 diff --git a/alpha b/alpha
1760 index 0123456789ab..abcdef012345 100644
1761 --- a/alpha
1762 +++ b/alpha
1763 @@ -1 +1 @@
1764 -alpha
1765 +ALPHA
1766 EOF
1768 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1769 > $testroot/stdout
1770 ret=$?
1771 if [ $ret -ne 0 ]; then
1772 test_done $testroot $ret
1773 return 1
1776 echo 'M alpha' > $testroot/stdout.expected
1777 cmp -s $testroot/stdout.expected $testroot/stdout
1778 ret=$?
1779 if [ $ret -ne 0 ]; then
1780 diff -u $testroot/stdout.expected $testroot/stdout
1782 test_done $testroot $ret
1785 test_patch_merge_reverse() {
1786 local testroot=`test_init patch_merge_simple`
1788 got checkout $testroot/repo $testroot/wt > /dev/null
1789 ret=$?
1790 if [ $ret -ne 0 ]; then
1791 test_done $testroot $ret
1792 return 1
1795 seq 10 > $testroot/wt/numbers
1796 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1797 > /dev/null
1798 ret=$?
1799 if [ $ret -ne 0 ]; then
1800 test_done $testroot $ret
1801 return 1
1804 local commit_id=`git_show_head $testroot/repo`
1806 seq 10 | sed s/5/five/g > $testroot/wt/numbers
1807 (cd $testroot/wt && got diff > $testroot/wt/patch \
1808 && got commit -m 'edit numbers') > /dev/null
1809 ret=$?
1810 if [ $ret -ne 0 ]; then
1811 test_done $testroot $ret
1812 return 1
1815 seq 10 | sed -e s/5/five/g -e s/6/six/g > $testroot/wt/numbers
1816 (cd $testroot/wt && got commit -m 'edit numbers again') >/dev/null
1817 ret=$?
1818 if [ $ret -ne 0 ]; then
1819 test_done $testroot $ret
1820 return 1
1823 (cd $testroot/wt && got patch -R patch) >/dev/null 2>&1
1824 ret=$?
1825 if [ $ret -eq 0 ]; then
1826 echo "unexpectedly reverted the patch" >&2
1827 test_done $testroot 1
1828 return 1
1831 cat <<-EOF > $testroot/wt/numbers.expected
1836 <<<<<<< --- numbers
1839 ||||||| +++ numbers
1840 five
1841 =======
1842 five
1843 six
1844 >>>>>>> commit $commit_id
1849 EOF
1851 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1852 ret=$?
1853 if [ $ret -ne 0 ]; then
1854 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1856 test_done $testroot $ret
1859 test_patch_newfile_xbit_got_diff() {
1860 local testroot=`test_init patch_newfile_xbit`
1862 got checkout $testroot/repo $testroot/wt > /dev/null
1863 ret=$?
1864 if [ $ret -ne 0 ]; then
1865 test_done $testroot $ret
1866 return 1
1869 cat <<EOF > $testroot/wt/patch
1870 blob - /dev/null
1871 blob + abcdef0123456789abcdef012345678901234567 (mode 755)
1872 --- /dev/null
1873 +++ xfile
1874 @@ -0,0 +1,1 @@
1875 +xfile
1876 EOF
1878 (cd $testroot/wt && got patch patch) > /dev/null
1879 ret=$?
1880 if [ $ret -ne 0 ]; then
1881 test_done $testroot $ret
1882 return 1
1885 if [ ! -x $testroot/wt/xfile ]; then
1886 echo "failed to set xbit on newfile" >&2
1887 test_done $testroot 1
1888 return 1
1891 echo xfile > $testroot/wt/xfile.expected
1892 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1893 ret=$?
1894 if [ $ret -ne 0 ]; then
1895 echo "fail"
1896 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1899 test_done $testroot $ret
1902 test_patch_newfile_xbit_git_diff() {
1903 local testroot=`test_init patch_newfile_xbit`
1905 got checkout $testroot/repo $testroot/wt > /dev/null
1906 ret=$?
1907 if [ $ret -ne 0 ]; then
1908 test_done $testroot $ret
1909 return 1
1912 cat <<EOF > $testroot/wt/patch
1913 diff --git a/xfile b/xfile
1914 new file mode 100755
1915 index 00000000..abcdef01
1916 --- /dev/null
1917 +++ b/xfile
1918 @@ -0,0 +1,1 @@
1919 +xfile
1920 EOF
1922 (cd $testroot/wt && got patch patch) > /dev/null
1923 ret=$?
1924 if [ $ret -ne 0 ]; then
1925 test_done $testroot $ret
1926 return 1
1929 if [ ! -x $testroot/wt/xfile ]; then
1930 echo "failed to set xbit on newfile" >&2
1931 test_done $testroot 1
1932 return 1
1935 echo xfile > $testroot/wt/xfile.expected
1936 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1937 ret=$?
1938 if [ $ret -ne 0 ]; then
1939 echo "fail"
1940 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1943 test_done $testroot $ret
1946 test_patch_umask() {
1947 local testroot=`test_init patch_umask`
1949 got checkout "$testroot/repo" "$testroot/wt" >/dev/null
1951 cat <<EOF >$testroot/wt/patch
1952 --- alpha
1953 +++ alpha
1954 @@ -1 +1 @@
1955 -alpha
1956 +modified alpha
1957 EOF
1959 # using a subshell to avoid clobbering global umask
1960 (umask 077 && cd "$testroot/wt" && got patch <patch) >/dev/null
1961 ret=$?
1962 if [ $ret -ne 0 ]; then
1963 test_done "$testroot" $ret
1964 return 1
1967 if ! ls -l "$testroot/wt/alpha" | grep -q ^-rw-------; then
1968 echo "alpha is not 0600 after patch" >&2
1969 ls -l "$testroot/wt/alpha" >&2
1970 test_done "$testroot" 1
1971 return 1
1974 test_done "$testroot" 0
1977 test_patch_remove_binary_file() {
1978 local testroot=`test_init patch_remove_binary_file`
1980 if ! got checkout $testroot/repo $testroot/wt >/dev/null; then
1981 test_done $testroot $ret
1982 return 1
1985 dd if=/dev/zero of=$testroot/wt/x bs=1 count=16 2>/dev/null >&2
1986 (cd $testroot/wt && got add x && got commit -m +x) >/dev/null
1988 (cd $testroot/wt && \
1989 got branch demo && \
1990 got rm x && \
1991 got ci -m -x &&
1992 got up -b master) >/dev/null
1994 echo 'D x' > $testroot/stdout.expected
1996 (cd $testroot/wt && got log -c demo -l 1 -p >patch)
1998 (cd $testroot/wt && got patch <patch) > $testroot/stdout
1999 if [ $? -ne 0 ]; then
2000 echo 'patch failed' >&2
2001 test_done $testroot 1
2002 return 1
2005 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
2006 diff -u $testroot/stdout.expected $testroot/stdout
2007 test_done $testroot 1
2008 return 1
2011 # try again using a git produced diff
2012 (cd $testroot/wt && got revert x) >/dev/null
2014 git -C $testroot/repo show demo >$testroot/wt/patch
2016 (cd $testroot/wt && got patch <patch) > $testroot/stdout
2017 if [ $? -ne 0 ]; then
2018 echo 'patch failed' >&2
2019 test_done $testroot 1
2020 return 1
2023 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
2024 diff -u $testroot/stdout.expected $testroot/stdout
2025 test_done $testroot 1
2026 return 1
2029 # try again using a diff(1) style patch
2030 (cd $testroot/wt && got revert x) >/dev/null
2032 echo "Binary files x and /dev/null differ" >$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 test_done $testroot 0
2049 test_patch_commit_keywords() {
2050 local testroot=`test_init patch_commit_keywords`
2052 got checkout $testroot/repo $testroot/wt >/dev/null
2053 ret=$?
2054 if [ $ret -ne 0 ]; then
2055 test_done $testroot $ret
2056 return 1
2059 seq 10 > $testroot/wt/numbers
2060 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
2061 >/dev/null
2062 ret=$?
2063 if [ $ret -ne 0 ]; then
2064 test_done $testroot $ret
2065 return 1
2068 seq 10 | sed s/4/four/ > $testroot/wt/numbers
2070 # get rid of the metadata
2071 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
2072 >/dev/null
2074 seq 10 | sed s/6/six/ > $testroot/wt/numbers
2075 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
2076 ret=$?
2077 if [ $ret -ne 0 ]; then
2078 test_done $testroot $ret
2079 return 1
2082 (cd $testroot/wt && got patch -c :head:- patch) >$testroot/stdout
2083 ret=$?
2084 if [ $ret -ne 0 ]; then
2085 test_done $testroot $ret
2086 return 1
2089 echo 'G numbers' > $testroot/stdout.expected
2090 cmp -s $testroot/stdout $testroot/stdout.expected
2091 ret=$?
2092 if [ $ret -ne 0 ]; then
2093 diff -u $testroot/stdout $testroot/stdout.expected
2094 test_done $testroot $ret
2095 return 1
2098 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2099 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2100 ret=$?
2101 if [ $ret -ne 0 ]; then
2102 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2105 (cd "$testroot/wt" && got rv numbers > /dev/null)
2107 (cd $testroot/wt && got patch -c :base:- patch) >$testroot/stdout
2108 ret=$?
2109 if [ $ret -ne 0 ]; then
2110 test_done $testroot $ret
2111 return 1
2114 echo 'G numbers' > $testroot/stdout.expected
2115 cmp -s $testroot/stdout $testroot/stdout.expected
2116 ret=$?
2117 if [ $ret -ne 0 ]; then
2118 diff -u $testroot/stdout $testroot/stdout.expected
2119 test_done $testroot $ret
2120 return 1
2123 seq 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2124 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2125 ret=$?
2126 if [ $ret -ne 0 ]; then
2127 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2130 test_done $testroot $ret
2133 test_parseargs "$@"
2134 run_test test_patch_basic
2135 run_test test_patch_dont_apply
2136 run_test test_patch_malformed
2137 run_test test_patch_no_patch
2138 run_test test_patch_equals_for_context
2139 run_test test_patch_rename
2140 run_test test_patch_illegal_status
2141 run_test test_patch_nop
2142 run_test test_patch_preserve_perm
2143 run_test test_patch_create_dirs
2144 run_test test_patch_with_offset
2145 run_test test_patch_empty_file
2146 run_test test_patch_prefer_new_path
2147 run_test test_patch_no_newline
2148 run_test test_patch_strip
2149 run_test test_patch_whitespace
2150 run_test test_patch_relative_paths
2151 run_test test_patch_with_path_prefix
2152 run_test test_patch_relpath_with_path_prefix
2153 run_test test_patch_reverse
2154 run_test test_patch_merge_simple
2155 run_test test_patch_merge_gitdiff
2156 run_test test_patch_merge_base_provided
2157 run_test test_patch_merge_conflict
2158 run_test test_patch_merge_unknown_blob
2159 run_test test_patch_merge_reverse
2160 run_test test_patch_newfile_xbit_got_diff
2161 run_test test_patch_newfile_xbit_git_diff
2162 run_test test_patch_umask
2163 run_test test_patch_remove_binary_file
2164 run_test test_patch_commit_keywords