3 # Copyright (c) 2022 Omar Polo <op@openbsd.org>
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 local testroot=`test_init patch_basic`
22 got checkout $testroot/repo $testroot/wt > /dev/null
24 if [ $ret -ne 0 ]; then
25 test_done $testroot $ret
29 jot 100 > $testroot/wt/numbers
30 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
33 if [ $ret -ne 0 ]; then
34 test_done "$testroot" $ret
38 cat <<EOF > $testroot/wt/patch
43 +alpha is my favourite character
90 (cd $testroot/wt && got patch < patch) > $testroot/stdout
91 if [ $ret -ne 0 ]; then
92 test_done "$testroot" $ret
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
104 if [ $ret -ne 0 ]; then
105 diff -u $testroot/stdout.expected $testroot/stdout
106 test_done $testroot $ret
110 echo 'alpha is my favourite character' > $testroot/wt/alpha.expected
111 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
113 if [ $ret -ne 0 ]; then
114 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
115 test_done "$testroot" $ret
119 if [ -f "$testroot/wt/beta" ]; then
120 echo "beta was not deleted!" >&2
121 test_done "$testroot" 1
125 echo 'this is:' > $testroot/wt/gamma/delta.expected
126 echo 'delta' >> $testroot/wt/gamma/delta.expected
127 cmp -s $testroot/wt/gamma/delta.expected $testroot/wt/gamma/delta
129 if [ $ret -ne 0 ]; then
130 diff -u $testroot/wt/gamma/delta.expected $testroot/wt/gamma/delta
131 test_done "$testroot" $ret
135 jot 5 > $testroot/wt/eta.expected
136 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
138 if [ $ret -ne 0 ]; then
139 diff -u $testroot/wt/eta.expected $testroot/wt/eta
140 test_done "$testroot" $ret
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
148 if [ $ret -ne 0 ]; then
149 diff -u $testroot/wt/numbers.expected $testroot/wt/numbers
151 test_done $testroot $ret
154 test_patch_dont_apply() {
155 local testroot=`test_init patch_dont_apply`
157 got checkout $testroot/repo $testroot/wt > /dev/null
159 if [ $ret -ne 0 ]; then
160 test_done $testroot $ret
164 jot 100 > $testroot/wt/numbers
165 (cd $testroot/wt && got add numbers && got commit -m 'add numbers') \
168 if [ $ret -ne 0 ]; then
169 test_done $testroot $ret
173 cat <<EOF > $testroot/wt/patch
193 (cd $testroot/wt && got patch patch) > $testroot/stdout 2> /dev/null
195 if [ $ret -eq 0 ]; then # should fail
196 test_done $testroot 1
200 cat <<EOF > $testroot/stdout.expected
202 @@ -1,1 +1,2 @@ hunk failed to apply
204 @@ -1,9 +0,0 @@ hunk failed to apply
205 Files where patch failed to apply: 2
208 cmp -s $testroot/stdout.expected $testroot/stdout
210 if [ $ret -ne 0 ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
213 test_done $testroot $ret
216 test_patch_malformed() {
217 local testroot=`test_init patch_malformed`
219 got checkout $testroot/repo $testroot/wt > /dev/null
221 if [ $ret -ne 0 ]; then
222 test_done $testroot $ret
227 cat <<EOF > $testroot/wt/patch
235 echo -n > $testroot/stdout.expected
236 echo "got: malformed patch" > $testroot/stderr.expected
238 (cd $testroot/wt && got patch patch) \
242 if [ $ret -eq 0 ]; then
243 echo "got managed to apply an invalid patch"
244 test_done $testroot 1
248 cmp -s $testroot/stdout.expected $testroot/stdout
250 if [ $ret -ne 0 ]; then
251 diff -u $testroot/stdout.expected $testroot/stdout
252 test_done $testroot $ret
256 cmp -s $testroot/stderr.expected $testroot/stderr
258 if [ $ret -ne 0 ]; then
259 diff -u $testroot/stderr.expected $testroot/stderr
260 test_done $testroot $ret
264 # wrong first character
265 cat <<EOF > $testroot/wt/patch
273 (cd $testroot/wt && got patch patch) \
277 if [ $ret -eq 0 ]; then
278 echo "got managed to apply an invalid patch"
279 test_done $testroot 1
283 cmp -s $testroot/stdout.expected $testroot/stdout
285 if [ $ret -ne 0 ]; then
286 diff -u $testroot/stdout.expected $testroot/stdout
287 test_done $testroot $ret
291 cmp -s $testroot/stderr.expected $testroot/stderr
293 if [ $ret -ne 0 ]; then
294 diff -u $testroot/stderr.expected $testroot/stderr
295 test_done $testroot $ret
300 cat <<EOF > $testroot/wt/patch
301 diff --git a/alpha b/iota
307 (cd $testroot/wt && got patch patch) \
311 if [ $ret -eq 0 ]; then
312 echo "got managed to apply an invalid patch"
313 test_done $testroot 1
317 cmp -s $testroot/stdout.expected $testroot/stdout
319 if [ $ret -ne 0 ]; then
320 diff -u $testroot/stdout.expected $testroot/stdout
321 test_done $testroot $ret
325 cmp -s $testroot/stderr.expected $testroot/stderr
327 if [ $ret -ne 0 ]; then
328 diff -u $testroot/stderr.expected $testroot/stderr
329 test_done $testroot $ret
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
341 if [ $ret -ne 0 ]; then
342 test_done $testroot $ret
346 cat <<EOF > $testroot/wt/patch
353 there's no patch in here!
356 echo -n > $testroot/stdout.expected
357 echo "got: no patch found" > $testroot/stderr.expected
359 (cd $testroot/wt && got patch patch) \
363 if [ $ret -eq 0 ]; then # should fail
364 test_done $testroot 1
368 cmp -s $testroot/stdout.expected $testroot/stdout
370 if [ $ret -ne 0 ]; then
371 diff -u $testroot/stdout.expected $testroot/stdout
372 test_done $testroot $ret
376 cmp -s $testroot/stderr.expected $testroot/stderr
378 if [ $ret -ne 0 ]; then
379 diff -u $testroot/stderr.expected $testroot/stderr
380 test_done $testroot $ret
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
392 if [ $ret -ne 0 ]; then
393 test_done $testroot $ret
397 cat <<EOF > $testroot/wt/patch
405 echo "M alpha" > $testroot/stdout.expected
407 (cd $testroot/wt && got patch patch) > $testroot/stdout
409 if [ $ret -ne 0 ]; then
410 test_done $testroot $ret
414 cmp -s $testroot/stdout.expected $testroot/stdout
416 if [ $ret -ne 0 ]; then
417 diff -u $testroot/stdout.expected $testroot/stdout
418 test_done $testroot $ret
422 echo hatsuseno > $testroot/wt/alpha.expected
423 echo alpha >> $testroot/wt/alpha.expected
424 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
426 if [ $ret -ne 0 ]; then
427 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
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
437 if [ $ret -ne 0 ]; then
438 test_done $testroot $ret
442 cat <<EOF > $testroot/wt/patch
443 diff --git a/beta b/iota
444 similarity index 100%
447 diff --git a/alpha b/eta
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
462 if [ $ret -ne 0 ]; then
463 test_done $testroot $ret
467 cmp -s $testroot/stdout.expected $testroot/stdout
469 if [ $ret -ne 0 ]; then
470 diff -u $testroot/stdout.expected $testroot/stdout
471 test_done $testroot $ret
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
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
486 echo beta > $testroot/wt/iota.expected
487 cmp -s $testroot/wt/iota.expected $testroot/wt/iota
489 if [ $ret -ne 0 ]; then
490 diff -u $testroot/wt/iota.expected $testroot/wt/iota
491 test_done $testroot $ret
495 echo eta > $testroot/wt/eta.expected
496 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
498 if [ $ret -ne 0 ]; then
499 diff -u $testroot/wt/eta.expected $testroot/wt/eta
500 test_done $testroot $ret
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
512 if [ $ret -ne 0 ]; then
513 test_done $testroot $ret
517 # try to patch an obstructed file, add a versioned one, edit a
518 # non existent file and an unversioned one, and remove a
520 cat <<EOF > $testroot/wt/patch
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 \
553 if [ $ret -eq 0 ]; then
554 echo "edited a missing file" >&2
555 test_done $testroot 1
559 cat <<EOF > $testroot/stdout.expected
565 Files where patch failed to apply: 5
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
577 cmp -s $testroot/stdout.expected $testroot/stdout
579 if [ $ret -ne 0 ]; then
580 diff -u $testroot/stdout.expected $testroot/stdout
581 test_done $testroot $ret
585 cmp -s $testroot/stderr.expected $testroot/stderr
587 if [ $ret -ne 0 ]; then
588 diff -u $testroot/stderr.expected $testroot/stderr
589 test_done $testroot $ret
593 (cd $testroot/wt && got status) > $testroot/stdout
594 cat <<EOF > $testroot/stdout.expected
600 cmp -s $testroot/stdout.expected $testroot/stdout
602 if [ $ret -ne 0 ]; then
603 diff -u $testroot/stdout.expected $testroot/stdout
605 test_done $testroot $ret
609 local testroot=`test_init patch_nop`
611 got checkout $testroot/repo $testroot/wt > /dev/null
613 if [ $ret -ne 0 ]; then
614 test_done $testroot $ret
618 cat <<EOF > $testroot/wt/patch
628 diff --git a/gamma/delta b/gamma/delta.new
633 +delta updated and renamed!
636 (cd $testroot/wt && got patch -n patch)
638 if [ $ret -ne 0 ]; then
639 test_done $testroot $ret
643 # remove the patch to avoid the ? entry
644 rm $testroot/wt/patch
646 (cd $testroot/wt && got status) > $testroot/stdout
648 if [ $ret -ne 0 ]; then
649 test_done $testroot $ret
653 echo -n > $testroot/stdout.expected
654 cmp -s $testroot/stdout.expected $testroot/stdout
656 if [ $ret -ne 0 ]; then
657 diff -u $testroot/stdout.expected $testroot/stdout
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
667 if [ $ret -ne 0 ]; then
668 test_done $testroot $ret
672 chmod +x $testroot/wt/alpha
673 (cd $testroot/wt && got commit -m 'alpha executable') > /dev/null
675 if [ $ret -ne 0 ]; then
676 test_done $testroot $ret
680 cat <<EOF > $testroot/wt/patch
688 (cd $testroot/wt && got patch patch) > /dev/null
690 if [ $ret -ne 0 ]; then
691 test_done $testroot $ret
695 if [ ! -x $testroot/wt/alpha ]; then
696 echo "alpha is no more executable!" >&2
697 test_done $testroot 1
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
708 if [ $ret -ne 0 ]; then
709 test_done $testroot $ret
713 cat <<EOF > $testroot/wt/patch
715 +++ iota/kappa/lambda
720 (cd $testroot/wt && got patch patch) > $testroot/stdout
722 if [ $ret -ne 0 ]; then
723 test_done $testroot $ret
727 echo 'A iota/kappa/lambda' >> $testroot/stdout.expected
728 cmp -s $testroot/stdout.expected $testroot/stdout
730 if [ $ret -ne 0 ]; then
731 diff -u $testroot/stdout.expected $testroot/stdout
732 test_done $testroot $ret
736 if [ ! -f $testroot/wt/iota/kappa/lambda ]; then
737 echo "file not created!" >&2
738 test_done $testroot $ret
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
749 if [ $ret -ne 0 ]; then
750 test_done $testroot $ret
754 cat <<EOF > $testroot/wt/patch
777 jot 100 > $testroot/wt/numbers
778 ed -s "$testroot/wt/numbers" <<EOF
785 (cd $testroot/wt && got add numbers && got commit -m '+numbers') \
788 if [ $ret -ne 0 ]; then
789 test_done $testroot $ret
793 (cd $testroot/wt && got patch patch) > $testroot/stdout
795 if [ $ret -ne 0 ]; then
796 test_done $testroot/wt $ret
800 cat <<EOF > $testroot/stdout.expected
802 @@ -47,7 +47,7 @@ applied with offset -10
803 @@ -87,7 +87,7 @@ applied with offset 10
806 cmp -s $testroot/stdout.expected $testroot/stdout
808 if [ $ret -ne 0 ]; then
809 diff -u $testroot/stdout.expected $testroot/stdout
811 test_done $testroot $ret
814 test_patch_prefer_new_path() {
815 local testroot=`test_init patch_orig`
817 got checkout $testroot/repo $testroot/wt > /dev/null
819 if [ $ret -ne 0 ]; then
820 test_done $testroot $ret
824 cat <<EOF > $testroot/wt/patch
832 (cd $testroot/wt && got patch patch) > $testroot/stdout
834 if [ $ret -ne 0 ]; then
835 test_done $testroot $ret
839 echo 'M alpha' > $testroot/stdout.expected
840 cmp -s $testroot/stdout.expected $testroot/stdout
842 if [ $ret -ne 0 ]; then
843 diff -u $testroot/stdout.expected $testroot/stdout
845 test_done $testroot $ret
848 test_patch_no_newline() {
849 local testroot=`test_init patch_no_newline`
851 got checkout $testroot/repo $testroot/wt > /dev/null
853 if [ $ret -ne 0 ]; then
854 test_done $testroot $ret
858 cat <<EOF > $testroot/wt/patch
863 \ No newline at end of file
866 (cd $testroot/wt && got patch patch) > $testroot/stdout
868 if [ $ret -ne 0 ]; then
869 test_done $testroot $ret
873 echo "A eta" > $testroot/stdout.expected
874 cmp -s $testroot/stdout.expected $testroot/stdout
876 if [ $ret -ne 0 ]; then
877 diff -u $testroot/stdout.expected $testroot/stdout
878 test_done $testroot $ret
882 echo -n eta > $testroot/wt/eta.expected
883 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
885 if [ $ret -ne 0 ]; then
886 diff -u $testroot/wt/eta.expected $testroot/wt/eta
887 test_done $testroot $ret
891 (cd $testroot/wt && got commit -m 'add eta') > /dev/null
893 if [ $ret -ne 0 ]; then
894 test_done $testroot $ret
898 cat <<EOF > $testroot/wt/patch
903 \ No newline at end of file
905 \ No newline at end of file
908 (cd $testroot/wt && got patch patch) > $testroot/stdout
910 if [ $ret -ne 0 ]; then
911 test_done $testroot $ret
915 echo "M eta" > $testroot/stdout.expected
916 cmp -s $testroot/stdout.expected $testroot/stdout
918 if [ $ret -ne 0 ]; then
919 diff -u $testroot/stdout.expected $testroot/stdout
920 test_done $testroot $ret
924 echo -n ETA > $testroot/wt/eta.expected
925 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
927 if [ $ret -ne 0 ]; then
928 diff -u $testroot/wt/eta.expected $testroot/wt/eta
929 test_done $testroot $ret
933 (cd $testroot/wt && got commit -m 'edit eta') > /dev/null
935 if [ $ret -ne 0 ]; then
936 test_done $testroot $ret
940 cat <<EOF > $testroot/wt/patch
945 \ No newline at end of file
949 (cd $testroot/wt && got patch patch) > $testroot/stdout
951 if [ $ret -ne 0 ]; then
952 test_done $testroot $ret
956 echo "M eta" > $testroot/stdout.expected
957 cmp -s $testroot/stdout.expected $testroot/stdout
959 if [ $ret -ne 0 ]; then
960 diff -u $testroot/stdout.expected $testroot/stdout
961 test_done $testroot $ret
965 echo eta > $testroot/wt/eta.expected
966 cmp -s $testroot/wt/eta.expected $testroot/wt/eta
968 if [ $ret -ne 0 ]; then
969 diff -u $testroot/wt/eta.expected $testroot/wt/eta
971 test_done $testroot $ret
975 local testroot=`test_init patch_strip`
977 got checkout $testroot/repo $testroot/wt > /dev/null
979 if [ $ret -ne 0 ]; then
980 test_done $testroot $ret
984 cat <<EOF > $testroot/wt/patch
985 --- foo/bar/alpha.orig
992 (cd $testroot/wt && got patch -p2 patch) > $testroot/stdout
994 if [ $ret -ne 0 ]; then
995 test_done $testroot $ret
999 echo "M alpha" >> $testroot/stdout.expected
1000 cmp -s $testroot/stdout.expected $testroot/stdout
1002 if [ $ret -ne 0 ]; then
1003 diff -u $testroot/stdout.expected $testroot/stdout
1004 test_done $testroot $ret
1008 (cd $testroot/wt && got revert alpha) > /dev/null 2>&1
1010 if [ $ret -ne 0 ]; then
1011 test_done $testroot $ret
1015 (cd $testroot/wt && got patch -p3 patch) \
1018 if [ $ret -eq 0 ]; then
1019 echo "stripped more components than available!"
1020 test_done $testroot 1
1024 cat <<EOF > $testroot/stderr.expected
1025 got: can't strip 1 path-components from foo/bar/alpha: bad path
1028 cmp -s $testroot/stderr.expected $testroot/stderr
1030 if [ $ret -ne 0 ]; then
1031 diff -u $testroot/stderr.expected $testroot/stderr
1033 test_done $testroot 0
1036 test_patch_whitespace() {
1037 local testroot=`test_init patch_whitespace`
1039 got checkout $testroot/repo $testroot/wt > /dev/null
1041 if [ $ret -ne 0 ]; then
1042 test_done $testroot $ret
1048 cat <<EOF > $testroot/wt/hello.c
1054 /* the trailing whitespace is on purpose */
1055 printf("hello, world\n");$trailing
1060 (cd $testroot/wt && got add hello.c && got ci -m '+hello.c') \
1063 if [ $ret -ne 0 ]; then
1064 test_done $testroot $ret
1068 # test with a diff with various whitespace corruptions
1069 cat <<EOF > $testroot/wt/patch
1074 /* the trailing whitespace is on purpose */
1075 printf("hello, world\n");
1077 + return 5; /* always fails */
1081 (cd $testroot/wt && got patch patch) \
1082 2>$testroot/stderr >$testroot/stdout
1084 if [ $ret -ne 0 ]; then
1085 echo "failed to apply diff" >&2
1086 test_done $testroot $ret
1090 echo 'M hello.c' > $testroot/stdout.expected
1091 echo '@@ -5,5 +5,5 @@ hunk contains mangled whitespace' \
1092 >> $testroot/stdout.expected
1093 cmp -s $testroot/stdout.expected $testroot/stdout
1095 if [ $ret -ne 0 ]; then
1096 diff -u $testroot/stdout.expected $testroot/stdout
1097 test_done $testroot $ret
1101 cat <<EOF > $testroot/wt/hello.c.expected
1107 /* the trailing whitespace is on purpose */
1108 printf("hello, world\n");$trailing
1109 return 5; /* always fails */
1113 cmp -s $testroot/wt/hello.c.expected $testroot/wt/hello.c
1115 if [ $ret -ne 0 ]; then
1116 diff -u $testroot/wt/hello.c.expected $testroot/wt/hello.c
1118 test_done $testroot $ret
1121 test_patch_relative_paths() {
1122 local testroot=`test_init patch_relative_paths`
1124 got checkout $testroot/repo $testroot/wt > /dev/null
1126 if [ $ret -ne 0 ]; then
1127 test_done $testroot $ret
1131 cat <<EOF > $testroot/wt/gamma/patch
1143 (cd $testroot/wt/gamma && got patch patch) > $testroot/stdout
1145 if [ $ret -ne 0 ]; then
1146 test_done $testroot $ret
1150 echo 'M gamma/delta' > $testroot/stdout.expected
1151 echo 'A gamma/eta' >> $testroot/stdout.expected
1153 cmp -s $testroot/stdout.expected $testroot/stdout
1155 if [ $ret -ne 0 ]; then
1156 diff -u $testroot/stdout.expected $testroot/stdout
1158 test_done $testroot $ret
1161 test_patch_with_path_prefix() {
1162 local testroot=`test_init patch_with_path_prefix`
1164 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1166 if [ $ret -ne 0 ]; then
1167 test_done $testroot $ret
1171 cat <<EOF > $testroot/wt/patch
1183 (cd $testroot/wt && got patch patch) > $testroot/stdout
1185 if [ $ret -ne 0 ]; then
1186 test_done $testroot $ret
1190 echo 'M delta' > $testroot/stdout.expected
1191 echo 'A eta' >> $testroot/stdout.expected
1193 cmp -s $testroot/stdout.expected $testroot/stdout
1195 if [ $ret -ne 0 ]; then
1196 diff -u $testroot/stdout.expected $testroot/stdout
1198 test_done $testroot $ret
1201 test_patch_relpath_with_path_prefix() {
1202 local testroot=`test_init patch_relpaths_with_path_prefix`
1204 got checkout -p gamma $testroot/repo $testroot/wt > /dev/null
1206 if [ $ret -ne 0 ]; then
1207 test_done $testroot $ret
1211 mkdir -p $testroot/wt/epsilon/zeta/
1213 cat <<EOF > $testroot/wt/patch
1220 (cd $testroot/wt/epsilon/zeta && got patch -p1 $testroot/wt/patch) \
1223 if [ $ret -ne 0 ]; then
1224 test_done $testroot $ret
1228 echo 'A epsilon/zeta/theta' >> $testroot/stdout.expected
1230 cmp -s $testroot/stdout.expected $testroot/stdout
1232 if [ $ret -ne 0 ]; then
1233 diff -u $testroot/stdout.expected $testroot/stdout
1234 test_done $testroot $ret
1238 echo 'theta' > $testroot/theta.expected
1239 cmp -s $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1241 if [ $ret -ne 0 ]; then
1242 diff -u $testroot/wt/epsilon/zeta/theta $testroot/theta.expected
1244 test_done $testroot $ret
1247 test_patch_reverse() {
1248 local testroot=`test_init patch_reverse`
1250 got checkout $testroot/repo $testroot/wt > /dev/null
1252 if [ $ret -ne 0 ]; then
1253 test_done $testroot $ret
1257 cat <<EOF > $testroot/wt/patch
1262 \ No newline at end of file
1266 (cd $testroot/wt && got patch -R patch) > $testroot/stdout
1268 if [ $ret -ne 0 ]; then
1269 test_done $testroot $ret
1273 echo "M alpha" > $testroot/stdout.expected
1274 cmp -s $testroot/stdout.expected $testroot/stdout
1276 if [ $ret -ne 0 ]; then
1277 diff -u $testroot/stdout.expected $testroot/stdout
1278 test_done $testroot $ret
1282 echo -n ALPHA > $testroot/wt/alpha.expected
1283 cmp -s $testroot/wt/alpha.expected $testroot/wt/alpha
1285 if [ $ret -ne 0 ]; then
1286 diff -u $testroot/wt/alpha.expected $testroot/wt/alpha
1288 test_done $testroot $ret
1291 test_patch_merge_simple() {
1292 local testroot=`test_init patch_merge_simple`
1294 got checkout $testroot/repo $testroot/wt > /dev/null
1296 if [ $ret -ne 0 ]; then
1297 test_done $testroot $ret
1301 jot 10 > $testroot/wt/numbers
1302 chmod +x $testroot/wt/numbers
1303 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1306 if [ $ret -ne 0 ]; then
1307 test_done $testroot $ret
1311 jot 10 | sed 's/4/four/g' > $testroot/wt/numbers
1313 (cd $testroot/wt && got diff > $testroot/old.diff \
1314 && got revert numbers) >/dev/null
1316 if [ $ret -ne 0 ]; then
1317 test_done $testroot $ret
1321 jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
1322 (cd $testroot/wt && got commit -m 'edit numbers') \
1325 if [ $ret -ne 0 ]; then
1326 test_done $testroot $ret
1330 (cd $testroot/wt && got patch $testroot/old.diff) \
1333 if [ $ret -ne 0 ]; then
1334 test_done $testroot $ret
1338 echo 'G numbers' > $testroot/stdout.expected
1339 cmp -s $testroot/stdout $testroot/stdout.expected
1341 if [ $ret -ne 0 ]; then
1342 diff -u $testroot/stdout $testroot/stdout.expected
1343 test_done $testroot $ret
1347 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1348 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1350 if [ $ret -ne 0 ]; then
1351 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1352 test_done $testroot $ret
1356 test -x $testroot/wt/numbers
1358 if [ $ret -ne 0 ]; then
1359 echo "numbers lost the executable bit" >&2
1361 test_done $testroot $ret
1364 test_patch_merge_gitdiff() {
1365 local testroot=`test_init patch_merge_gitdiff`
1367 jot 10 > $testroot/repo/numbers
1368 git -C $testroot/repo add numbers && \
1369 git_commit $testroot/repo -m "nums"
1371 if [ $ret -ne 0 ]; then
1372 test_done $testroot $ret
1376 jot 10 | sed 's/4/four/g' > $testroot/repo/numbers
1377 git -C $testroot/repo diff > $testroot/old.diff
1379 if [ $ret -ne 0 ]; then
1380 test_done $testroot $ret
1385 jot 10 > $testroot/repo/numbers
1387 jot 10 | sed 's/6/six/g' > $testroot/repo/numbers
1388 git -C $testroot/repo add numbers && \
1389 git_commit $testroot/repo -m "edit"
1391 if [ $ret -ne 0 ]; then
1392 test_done $testroot $ret
1396 # now work with got:
1397 got checkout $testroot/repo $testroot/wt > /dev/null
1399 if [ $ret -ne 0 ]; then
1400 test_done $testroot $ret
1404 (cd $testroot/wt && got patch $testroot/old.diff) > $testroot/stdout
1406 if [ $ret -ne 0 ]; then
1407 test_done $testroot $ret
1411 echo 'G numbers' > $testroot/stdout.expected
1412 cmp -s $testroot/stdout $testroot/stdout.expected
1414 if [ $ret -ne 0 ]; then
1415 diff -u $testroot/stdout $testroot/stdout.expected
1417 test_done $testroot $ret
1420 test_patch_merge_base_provided() {
1421 local testroot=`test_init patch_merge_base_provided`
1423 got checkout $testroot/repo $testroot/wt >/dev/null
1425 if [ $ret -ne 0 ]; then
1426 test_done $testroot $ret
1430 jot 10 > $testroot/wt/numbers
1431 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1434 if [ $ret -ne 0 ]; then
1435 test_done $testroot $ret
1439 local commit_id=`git_show_head $testroot/repo`
1441 jot 10 | sed s/4/four/ > $testroot/wt/numbers
1443 # get rid of the metadata
1444 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
1447 jot 10 | sed s/6/six/ > $testroot/wt/numbers
1448 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
1450 if [ $ret -ne 0 ]; then
1451 test_done $testroot $ret
1455 (cd $testroot/wt && got patch -c $commit_id patch) >$testroot/stdout
1457 if [ $ret -ne 0 ]; then
1458 test_done $testroot $ret
1462 echo 'G numbers' > $testroot/stdout.expected
1463 cmp -s $testroot/stdout $testroot/stdout.expected
1465 if [ $ret -ne 0 ]; then
1466 diff -u $testroot/stdout $testroot/stdout.expected
1467 test_done $testroot $ret
1471 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
1472 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1474 if [ $ret -ne 0 ]; then
1475 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1477 test_done $testroot $ret
1480 test_patch_merge_conflict() {
1481 local testroot=`test_init patch_merge_conflict`
1483 got checkout $testroot/repo $testroot/wt > /dev/null
1485 if [ $ret -ne 0 ]; then
1486 test_done $testroot $ret
1490 jot 10 > $testroot/wt/numbers
1491 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1494 if [ $ret -ne 0 ]; then
1495 test_done $testroot $ret
1499 local commit_id=`git_show_head $testroot/repo`
1501 jot 10 | sed 's/6/six/g' > $testroot/wt/numbers
1502 echo ALPHA > $testroot/wt/alpha
1504 (cd $testroot/wt && got diff > $testroot/old.diff \
1505 && got revert alpha numbers) >/dev/null
1507 if [ $ret -ne 0 ]; then
1508 test_done $testroot $ret
1512 jot 10 | sed 's/6/3+3/g' > $testroot/wt/numbers
1513 jot -c 3 a > $testroot/wt/alpha
1514 (cd $testroot/wt && got commit -m 'edit alpha and numbers') \
1517 if [ $ret -ne 0 ]; then
1518 test_done $testroot $ret
1522 (cd $testroot/wt && got patch $testroot/old.diff) \
1523 > $testroot/stdout 2>/dev/null
1525 if [ $ret -eq 0 ]; then
1526 echo "got patch merged a diff that should conflict" >&2
1527 test_done $testroot 1
1531 echo 'C alpha' > $testroot/stdout.expected
1532 echo 'C numbers' >> $testroot/stdout.expected
1533 echo 'Files with merge conflicts: 2' >> $testroot/stdout.expected
1534 cmp -s $testroot/stdout $testroot/stdout.expected
1536 if [ $ret -ne 0 ]; then
1537 diff -u $testroot/stdout $testroot/stdout.expected
1538 test_done $testroot $ret
1542 # XXX: prefixing every line with a tab otherwise got thinks
1543 # the file has conflicts in it.
1544 cat <<-EOF > $testroot/wt/alpha.expected
1547 ||||||| commit $commit_id
1556 cat <<-EOF > $testroot/wt/numbers.expected
1564 ||||||| commit $commit_id
1575 cmp -s $testroot/wt/alpha $testroot/wt/alpha.expected
1577 if [ $ret -ne 0 ]; then
1578 diff -u $testroot/wt/alpha $testroot/wt/alpha.expected
1579 test_done $testroot $ret
1583 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1585 if [ $ret -ne 0 ]; then
1586 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1588 test_done $testroot $ret
1591 test_patch_merge_unknown_blob() {
1592 local testroot=`test_init patch_merge_unknown_blob`
1594 got checkout $testroot/repo $testroot/wt > /dev/null
1596 if [ $ret -ne 0 ]; then
1597 test_done $testroot $ret
1601 cat <<EOF > $testroot/wt/patch
1603 diff aaaabbbbccccddddeeeeffff0000111122223333 foo/bar
1605 blob - aaaabbbbccccddddeeeeffff0000111122223333
1607 blob + 0000111122223333444455556666777788889999
1617 (cd $testroot/wt/ && got patch patch) > $testroot/stdout
1619 if [ $ret -ne 0 ]; then
1620 test_done $testroot $ret
1624 echo 'M alpha' > $testroot/stdout.expected
1625 cmp -s $testroot/stdout.expected $testroot/stdout
1627 if [ $ret -ne 0 ]; then
1628 diff -u $testroot/stdout.expected $testroot/stdout
1629 test_done $testroot $ret
1633 # try again without a `diff' header
1635 cat <<EOF > $testroot/wt/patch
1637 blob - aaaabbbbccccddddeeeeffff0000111122223333
1639 blob + 0000111122223333444455556666777788889999
1649 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1652 if [ $ret -ne 0 ]; then
1653 test_done $testroot $ret
1657 echo 'M alpha' > $testroot/stdout.expected
1658 cmp -s $testroot/stdout.expected $testroot/stdout
1660 if [ $ret -ne 0 ]; then
1661 diff -u $testroot/stdout.expected $testroot/stdout
1662 test_done $testroot $ret
1666 # try again with a git-style diff
1668 cat <<EOF > $testroot/wt/patch
1669 diff --git a/alpha b/alpha
1670 index 0123456789ab..abcdef012345 100644
1678 (cd $testroot/wt && got revert alpha > /dev/null && got patch patch) \
1681 if [ $ret -ne 0 ]; then
1682 test_done $testroot $ret
1686 echo 'M alpha' > $testroot/stdout.expected
1687 cmp -s $testroot/stdout.expected $testroot/stdout
1689 if [ $ret -ne 0 ]; then
1690 diff -u $testroot/stdout.expected $testroot/stdout
1692 test_done $testroot $ret
1695 test_patch_merge_reverse() {
1696 local testroot=`test_init patch_merge_simple`
1698 got checkout $testroot/repo $testroot/wt > /dev/null
1700 if [ $ret -ne 0 ]; then
1701 test_done $testroot $ret
1705 jot 10 > $testroot/wt/numbers
1706 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1709 if [ $ret -ne 0 ]; then
1710 test_done $testroot $ret
1714 local commit_id=`git_show_head $testroot/repo`
1716 jot 10 | sed s/5/five/g > $testroot/wt/numbers
1717 (cd $testroot/wt && got diff > $testroot/wt/patch \
1718 && got commit -m 'edit numbers') > /dev/null
1720 if [ $ret -ne 0 ]; then
1721 test_done $testroot $ret
1725 jot 10 | sed -e s/5/five/g -e s/6/six/g > $testroot/wt/numbers
1726 (cd $testroot/wt && got commit -m 'edit numbers again') >/dev/null
1728 if [ $ret -ne 0 ]; then
1729 test_done $testroot $ret
1733 (cd $testroot/wt && got patch -R patch) >/dev/null 2>&1
1735 if [ $ret -eq 0 ]; then
1736 echo "unexpectedly reverted the patch" >&2
1737 test_done $testroot 1
1741 cat <<-EOF > $testroot/wt/numbers.expected
1754 >>>>>>> commit $commit_id
1761 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
1763 if [ $ret -ne 0 ]; then
1764 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
1766 test_done $testroot $ret
1769 test_patch_newfile_xbit_got_diff() {
1770 local testroot=`test_init patch_newfile_xbit`
1772 got checkout $testroot/repo $testroot/wt > /dev/null
1774 if [ $ret -ne 0 ]; then
1775 test_done $testroot $ret
1779 cat <<EOF > $testroot/wt/patch
1781 blob + abcdef0123456789abcdef012345678901234567 (mode 755)
1788 (cd $testroot/wt && got patch patch) > /dev/null
1790 if [ $ret -ne 0 ]; then
1791 test_done $testroot $ret
1795 if [ ! -x $testroot/wt/xfile ]; then
1796 echo "failed to set xbit on newfile" >&2
1797 test_done $testroot 1
1801 echo xfile > $testroot/wt/xfile.expected
1802 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1804 if [ $ret -ne 0 ]; then
1806 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1809 test_done $testroot $ret
1812 test_patch_newfile_xbit_git_diff() {
1813 local testroot=`test_init patch_newfile_xbit`
1815 got checkout $testroot/repo $testroot/wt > /dev/null
1817 if [ $ret -ne 0 ]; then
1818 test_done $testroot $ret
1822 cat <<EOF > $testroot/wt/patch
1823 diff --git a/xfile b/xfile
1824 new file mode 100755
1825 index 00000000..abcdef01
1832 (cd $testroot/wt && got patch patch) > /dev/null
1834 if [ $ret -ne 0 ]; then
1835 test_done $testroot $ret
1839 if [ ! -x $testroot/wt/xfile ]; then
1840 echo "failed to set xbit on newfile" >&2
1841 test_done $testroot 1
1845 echo xfile > $testroot/wt/xfile.expected
1846 cmp -s $testroot/wt/xfile $testroot/wt/xfile.expected
1848 if [ $ret -ne 0 ]; then
1850 diff -u $testroot/wt/xfile $testroot/wt/xfile.expected
1853 test_done $testroot $ret
1856 test_patch_umask() {
1857 local testroot=`test_init patch_umask`
1859 got checkout "$testroot/repo" "$testroot/wt" >/dev/null
1861 cat <<EOF >$testroot/wt/patch
1869 # using a subshell to avoid clobbering global umask
1870 (umask 077 && cd "$testroot/wt" && got patch <patch) >/dev/null
1872 if [ $ret -ne 0 ]; then
1873 test_done "$testroot" $ret
1877 if ! ls -l "$testroot/wt/alpha" | grep -q ^-rw-------; then
1878 echo "alpha is not 0600 after patch" >&2
1879 ls -l "$testroot/wt/alpha" >&2
1880 test_done "$testroot" 1
1884 test_done "$testroot" 0
1887 test_patch_remove_binary_file() {
1888 local testroot=`test_init patch_remove_binary_file`
1890 if ! got checkout $testroot/repo $testroot/wt >/dev/null; then
1891 test_done $testroot $ret
1895 dd if=/dev/zero of=$testroot/wt/x bs=1 count=16 2>/dev/null >&2
1896 (cd $testroot/wt && got add x && got commit -m +x) >/dev/null
1898 (cd $testroot/wt && \
1899 got branch demo && \
1902 got up -b master) >/dev/null
1904 echo 'D x' > $testroot/stdout.expected
1906 (cd $testroot/wt && got log -c demo -l 1 -p >patch)
1908 (cd $testroot/wt && got patch <patch) > $testroot/stdout
1909 if [ $? -ne 0 ]; then
1910 echo 'patch failed' >&2
1911 test_done $testroot 1
1915 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1916 diff -u $testroot/stdout.expected $testroot/stdout
1917 test_done $testroot 1
1921 # try again using a git produced diff
1922 (cd $testroot/wt && got revert x) >/dev/null
1924 git -C $testroot/repo show demo >$testroot/wt/patch
1926 (cd $testroot/wt && got patch <patch) > $testroot/stdout
1927 if [ $? -ne 0 ]; then
1928 echo 'patch failed' >&2
1929 test_done $testroot 1
1933 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1934 diff -u $testroot/stdout.expected $testroot/stdout
1935 test_done $testroot 1
1939 # try again using a diff(1) style patch
1940 (cd $testroot/wt && got revert x) >/dev/null
1942 echo "Binary files x and /dev/null differ" >$testroot/wt/patch
1943 (cd $testroot/wt && got patch <patch) >$testroot/stdout
1944 if [ $? -ne 0 ]; then
1945 echo 'patch failed' >&2
1946 test_done $testroot 1
1950 if ! cmp -s $testroot/stdout.expected $testroot/stdout; then
1951 diff -u $testroot/stdout.expected $testroot/stdout
1952 test_done $testroot 1
1956 test_done $testroot 0
1959 test_patch_commit_keywords() {
1960 local testroot=`test_init patch_commit_keywords`
1962 got checkout $testroot/repo $testroot/wt >/dev/null
1964 if [ $ret -ne 0 ]; then
1965 test_done $testroot $ret
1969 jot 10 > $testroot/wt/numbers
1970 (cd $testroot/wt && got add numbers && got commit -m +numbers) \
1973 if [ $ret -ne 0 ]; then
1974 test_done $testroot $ret
1978 jot 10 | sed s/4/four/ > $testroot/wt/numbers
1980 # get rid of the metadata
1981 (cd $testroot/wt && got diff | sed -n '/^---/,$p' > patch) \
1984 jot 10 | sed s/6/six/ > $testroot/wt/numbers
1985 (cd $testroot/wt && got commit -m 'edit numbers') >/dev/null
1987 if [ $ret -ne 0 ]; then
1988 test_done $testroot $ret
1992 (cd $testroot/wt && got patch -c :head:- patch) >$testroot/stdout
1994 if [ $ret -ne 0 ]; then
1995 test_done $testroot $ret
1999 echo 'G numbers' > $testroot/stdout.expected
2000 cmp -s $testroot/stdout $testroot/stdout.expected
2002 if [ $ret -ne 0 ]; then
2003 diff -u $testroot/stdout $testroot/stdout.expected
2004 test_done $testroot $ret
2008 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2009 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2011 if [ $ret -ne 0 ]; then
2012 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2015 (cd "$testroot/wt" && got rv numbers > /dev/null)
2017 (cd $testroot/wt && got patch -c :base:- patch) >$testroot/stdout
2019 if [ $ret -ne 0 ]; then
2020 test_done $testroot $ret
2024 echo 'G numbers' > $testroot/stdout.expected
2025 cmp -s $testroot/stdout $testroot/stdout.expected
2027 if [ $ret -ne 0 ]; then
2028 diff -u $testroot/stdout $testroot/stdout.expected
2029 test_done $testroot $ret
2033 jot 10 | sed -e s/4/four/ -e s/6/six/ > $testroot/wt/numbers.expected
2034 cmp -s $testroot/wt/numbers $testroot/wt/numbers.expected
2036 if [ $ret -ne 0 ]; then
2037 diff -u $testroot/wt/numbers $testroot/wt/numbers.expected
2040 test_done $testroot $ret
2044 run_test test_patch_basic
2045 run_test test_patch_dont_apply
2046 run_test test_patch_malformed
2047 run_test test_patch_no_patch
2048 run_test test_patch_equals_for_context
2049 run_test test_patch_rename
2050 run_test test_patch_illegal_status
2051 run_test test_patch_nop
2052 run_test test_patch_preserve_perm
2053 run_test test_patch_create_dirs
2054 run_test test_patch_with_offset
2055 run_test test_patch_prefer_new_path
2056 run_test test_patch_no_newline
2057 run_test test_patch_strip
2058 run_test test_patch_whitespace
2059 run_test test_patch_relative_paths
2060 run_test test_patch_with_path_prefix
2061 run_test test_patch_relpath_with_path_prefix
2062 run_test test_patch_reverse
2063 run_test test_patch_merge_simple
2064 run_test test_patch_merge_gitdiff
2065 run_test test_patch_merge_base_provided
2066 run_test test_patch_merge_conflict
2067 run_test test_patch_merge_unknown_blob
2068 run_test test_patch_merge_reverse
2069 run_test test_patch_newfile_xbit_got_diff
2070 run_test test_patch_newfile_xbit_git_diff
2071 run_test test_patch_umask
2072 run_test test_patch_remove_binary_file
2073 run_test test_patch_commit_keywords