blob: 71ef4132d153b7be4b4f3d4ebfb1a85ee4bfa9ab [file] [log] [blame]
Junio C Hamano4be60962006-09-17 01:04:24 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
Martin Ågren289218d2020-08-06 22:08:54 +02006test_description='git apply boundary tests'
Junio C Hamano4be60962006-09-17 01:04:24 -07007
Junio C Hamano4be60962006-09-17 01:04:24 -07008. ./test-lib.sh
9
10L="c d e f g h i j k l m n o p q r s t u v w x"
11
12test_expect_success setup '
Martin Ågren289218d2020-08-06 22:08:54 +020013 test_write_lines b $L y >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070014 cat victim >original &&
15 git update-index --add victim &&
16
Jeff King53350a32015-03-20 06:12:37 -040017 # add to the head
Martin Ågren289218d2020-08-06 22:08:54 +020018 test_write_lines a b $L y >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070019 cat victim >add-a-expect &&
20 git diff victim >add-a-patch.with &&
21 git diff --unified=0 >add-a-patch.without &&
22
Jeff King53350a32015-03-20 06:12:37 -040023 # insert at line two
Martin Ågren289218d2020-08-06 22:08:54 +020024 test_write_lines b a $L y >victim &&
Junio C Hamanoed0f47a2008-08-30 13:20:31 -070025 cat victim >insert-a-expect &&
26 git diff victim >insert-a-patch.with &&
27 git diff --unified=0 >insert-a-patch.without &&
28
Jeff King53350a32015-03-20 06:12:37 -040029 # modify at the head
Martin Ågren289218d2020-08-06 22:08:54 +020030 test_write_lines a $L y >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070031 cat victim >mod-a-expect &&
32 git diff victim >mod-a-patch.with &&
33 git diff --unified=0 >mod-a-patch.without &&
34
Jeff King53350a32015-03-20 06:12:37 -040035 # remove from the head
Martin Ågren289218d2020-08-06 22:08:54 +020036 test_write_lines $L y >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070037 cat victim >del-a-expect &&
Jeff King53350a32015-03-20 06:12:37 -040038 git diff victim >del-a-patch.with &&
Junio C Hamano4be60962006-09-17 01:04:24 -070039 git diff --unified=0 >del-a-patch.without &&
40
Jeff King53350a32015-03-20 06:12:37 -040041 # add to the tail
Martin Ågren289218d2020-08-06 22:08:54 +020042 test_write_lines b $L y z >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070043 cat victim >add-z-expect &&
44 git diff victim >add-z-patch.with &&
45 git diff --unified=0 >add-z-patch.without &&
46
Jeff King53350a32015-03-20 06:12:37 -040047 # modify at the tail
Martin Ågren289218d2020-08-06 22:08:54 +020048 test_write_lines b $L z >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070049 cat victim >mod-z-expect &&
50 git diff victim >mod-z-patch.with &&
51 git diff --unified=0 >mod-z-patch.without &&
52
Jeff King53350a32015-03-20 06:12:37 -040053 # remove from the tail
Martin Ågren289218d2020-08-06 22:08:54 +020054 test_write_lines b $L >victim &&
Junio C Hamano4be60962006-09-17 01:04:24 -070055 cat victim >del-z-expect &&
Jeff King8fb26872015-03-20 06:06:15 -040056 git diff victim >del-z-patch.with &&
57 git diff --unified=0 >del-z-patch.without
Junio C Hamano4be60962006-09-17 01:04:24 -070058
Jeff King53350a32015-03-20 06:12:37 -040059 # done
Junio C Hamano4be60962006-09-17 01:04:24 -070060'
61
62for with in with without
63do
64 case "$with" in
65 with) u= ;;
Martin Ågren289218d2020-08-06 22:08:54 +020066 without) u=--unidiff-zero ;;
Junio C Hamano4be60962006-09-17 01:04:24 -070067 esac
Junio C Hamanoed0f47a2008-08-30 13:20:31 -070068 for kind in add-a add-z insert-a mod-a mod-z del-a del-z
Junio C Hamano4be60962006-09-17 01:04:24 -070069 do
70 test_expect_success "apply $kind-patch $with context" '
71 cat original >victim &&
72 git update-index victim &&
Martin Ågren289218d2020-08-06 22:08:54 +020073 git apply --index $u "$kind-patch.$with" &&
74 test_cmp "$kind-expect" victim
Junio C Hamano4be60962006-09-17 01:04:24 -070075 '
76 done
77done
78
Junio C Hamanoed0f47a2008-08-30 13:20:31 -070079for kind in add-a add-z insert-a mod-a mod-z del-a del-z
Junio C Hamano4be60962006-09-17 01:04:24 -070080do
81 rm -f $kind-ng.without
82 sed -e "s/^diff --git /diff /" \
83 -e '/^index /d' \
84 <$kind-patch.without >$kind-ng.without
85 test_expect_success "apply non-git $kind-patch without context" '
86 cat original >victim &&
87 git update-index victim &&
Martin Ågren289218d2020-08-06 22:08:54 +020088 git apply --unidiff-zero --index "$kind-ng.without" &&
89 test_cmp "$kind-expect" victim
Junio C Hamano4be60962006-09-17 01:04:24 -070090 '
91done
92
Junio C Hamanoee5a3172008-04-06 19:21:45 -070093test_expect_success 'two lines' '
Junio C Hamanoee5a3172008-04-06 19:21:45 -070094 >file &&
95 git add file &&
96 echo aaa >file &&
97 git diff >patch &&
98 git add file &&
99 echo bbb >file &&
100 git add file &&
101 test_must_fail git apply --check patch
Junio C Hamanoee5a3172008-04-06 19:21:45 -0700102'
103
Björn Gustavsson24ff4d52010-03-06 15:30:29 +0100104test_expect_success 'apply patch with 3 context lines matching at end' '
Martin Ågren289218d2020-08-06 22:08:54 +0200105 test_write_lines a b c d >file &&
Björn Gustavsson24ff4d52010-03-06 15:30:29 +0100106 git add file &&
107 echo e >>file &&
108 git diff >patch &&
109 >file &&
110 test_must_fail git apply patch
111'
112
Junio C Hamano4be60962006-09-17 01:04:24 -0700113test_done