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