]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4107-apply-ignore-whitespace.sh
Merge branch 'wb/fsmonitor-bitmap-fix'
[thirdparty/git.git] / t / t4107-apply-ignore-whitespace.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Giuseppe Bilotta
4 #
5
6 test_description='git-apply --ignore-whitespace.
7
8 '
9 . ./test-lib.sh
10
11 # This primes main.c file that indents without using HT at all.
12 # Various patches with HT and other spaces are attempted in the test.
13
14 cat > patch1.patch <<\EOF
15 diff --git a/main.c b/main.c
16 new file mode 100644
17 --- /dev/null
18 +++ b/main.c
19 @@ -0,0 +1,22 @@
20 +#include <stdio.h>
21 +
22 +void print_int(int num);
23 +int func(int num);
24 +
25 +int main() {
26 + int i;
27 +
28 + for (i = 0; i < 10; i++) {
29 + print_int(func(i)); /* stuff */
30 + }
31 +
32 + return 0;
33 +}
34 +
35 +int func(int num) {
36 + return num * num;
37 +}
38 +
39 +void print_int(int num) {
40 + printf("%d", num);
41 +}
42 EOF
43
44 # Since whitespace is very significant and we want to prevent whitespace
45 # mangling when creating this test from a patch, we protect 'fixable'
46 # whitespace by replacing spaces with Z and replacing them at patch
47 # creation time, hence the sed trick.
48
49 # This patch will fail unless whitespace differences are being ignored
50
51 sed -e 's/Z/ /g' > patch2.patch <<\EOF
52 diff --git a/main.c b/main.c
53 --- a/main.c
54 +++ b/main.c
55 @@ -10,6 +10,8 @@
56 Z print_int(func(i)); /* stuff */
57 Z }
58 Z
59 + printf("\n");
60 +
61 Z return 0;
62 Z}
63 Z
64 EOF
65
66 # This patch will fail even if whitespace differences are being ignored,
67 # because of the missing string at EOL. TODO: this testcase should be
68 # improved by creating a line that has the same hash with and without
69 # the final string.
70
71 sed -e 's/Z/ /g' > patch3.patch <<\EOF
72 diff --git a/main.c b/main.c
73 --- a/main.c
74 +++ b/main.c
75 @@ -10,3 +10,4 @@
76 Z for (i = 0; i < 10; i++) {
77 Z print_int(func(i));Z
78 + /* stuff */
79 Z }
80 EOF
81
82 # This patch will fail even if whitespace differences are being ignored,
83 # because of the missing EOL at EOF.
84
85 sed -e 's/Z/ /g' > patch4.patch <<\EOF
86 diff --git a/main.c b/main.c
87 --- a/main.c
88 +++ b/main.c
89 @@ -21,1 +21,1 @@
90 - };Z
91 \ No newline at end of file
92 + };
93 EOF
94
95 # This patch will fail unless whitespace differences are being ignored.
96
97 sed -e 's/Z/ /g' > patch5.patch <<\EOF
98 diff --git a/main.c b/main.c
99 --- a/main.c
100 +++ b/main.c
101 @@ -2,2 +2,3 @@
102 Z void print_int(int num);
103 + /* a comment */
104 Z int func(int num);
105 EOF
106
107 # And this is how the final output should be. Patches introduce
108 # HTs but the original SP indents are mostly kept.
109
110 sed -e 's/T/ /g' > main.c.final <<\EOF
111 #include <stdio.h>
112
113 void print_int(int num);
114 int func(int num);
115
116 int main() {
117 int i;
118
119 for (i = 0; i < 10; i++) {
120 print_int(func(i)); /* stuff */
121 }
122
123 Tprintf("\n");
124
125 return 0;
126 }
127
128 int func(int num) {
129 return num * num;
130 }
131
132 void print_int(int num) {
133 printf("%d", num);
134 }
135 EOF
136
137 test_expect_success 'file creation' '
138 git apply patch1.patch
139 '
140
141 test_expect_success 'patch2 fails (retab)' '
142 test_must_fail git apply patch2.patch
143 '
144
145 test_expect_success 'patch2 applies with --ignore-whitespace' '
146 git apply --ignore-whitespace patch2.patch
147 '
148
149 test_expect_success 'patch2 reverse applies with --ignore-space-change' '
150 git apply -R --ignore-space-change patch2.patch
151 '
152
153 git config apply.ignorewhitespace change
154
155 test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
156 git apply patch2.patch &&
157 test_cmp main.c.final main.c
158 '
159
160 test_expect_success 'patch3 fails (missing string at EOL)' '
161 test_must_fail git apply patch3.patch
162 '
163
164 test_expect_success 'patch4 fails (missing EOL at EOF)' '
165 test_must_fail git apply patch4.patch
166 '
167
168 test_expect_success 'patch5 fails (leading whitespace differences matter)' '
169 test_must_fail git apply patch5.patch
170 '
171
172 test_expect_success 're-create file (with --ignore-whitespace)' '
173 rm -f main.c &&
174 git apply patch1.patch
175 '
176
177 test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
178 test_must_fail git apply --no-ignore-whitespace patch5.patch
179 '
180
181 test_expect_success 'apply --ignore-space-change --inaccurate-eof' '
182 echo 1 >file &&
183 git apply --ignore-space-change --inaccurate-eof <<-\EOF &&
184 diff --git a/file b/file
185 --- a/file
186 +++ b/file
187 @@ -1 +1 @@
188 -1
189 +2
190 EOF
191 printf 2 >expect &&
192 test_cmp expect file
193 '
194
195 test_done