summaryrefslogtreecommitdiff
path: root/t/t4107-apply-ignore-whitespace.sh
blob: ac72eeaf27a9b925d801052fb3d051189c0a832a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
#
# Copyright (c) 2009 Giuseppe Bilotta
#

test_description='git-apply --ignore-whitespace.

'
. ./test-lib.sh

# This primes main.c file that indents without using HT at all.
# Various patches with HT and other spaces are attempted in the test.

cat > patch1.patch <<\EOF
diff --git a/main.c b/main.c
new file mode 100644
--- /dev/null
+++ b/main.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+void print_int(int num);
+int func(int num);
+
+int main() {
+       int i;
+
+       for (i = 0; i < 10; i++) {
+               print_int(func(i)); /* stuff */
+       }
+
+       return 0;
+}
+
+int func(int num) {
+       return num * num;
+}
+
+void print_int(int num) {
+       printf("%d", num);
+}
EOF

# Since whitespace is very significant and we want to prevent whitespace
# mangling when creating this test from a patch, we protect 'fixable'
# whitespace by replacing spaces with Z and replacing them at patch
# creation time, hence the sed trick.

# This patch will fail unless whitespace differences are being ignored

sed -e 's/Z/ /g' > patch2.patch <<\EOF
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,6 +10,8 @@
Z		print_int(func(i)); /* stuff */
Z	}
Z
+	printf("\n");
+
Z	return 0;
Z}
Z
EOF

# This patch will fail even if whitespace differences are being ignored,
# because of the missing string at EOL. TODO: this testcase should be
# improved by creating a line that has the same hash with and without
# the final string.

sed -e 's/Z/ /g' > patch3.patch <<\EOF
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,3 +10,4 @@
Z	for (i = 0; i < 10; i++) {
Z		print_int(func(i));Z
+		/* stuff */
Z	}
EOF

# This patch will fail even if whitespace differences are being ignored,
# because of the missing EOL at EOF.

sed -e 's/Z/ /g' > patch4.patch <<\EOF
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -21,1 +21,1 @@
-	};Z
\ No newline at end of file
+	};
EOF

# This patch will fail unless whitespace differences are being ignored.

sed -e 's/Z/ /g' > patch5.patch <<\EOF
diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -2,2 +2,3 @@
Z	void print_int(int num);
+	/* a comment */
Z	int func(int num);
EOF

# And this is how the final output should be.  Patches introduce
# HTs but the original SP indents are mostly kept.

sed -e 's/T/	/g' > main.c.final <<\EOF
#include <stdio.h>

void print_int(int num);
int func(int num);

int main() {
       int i;

       for (i = 0; i < 10; i++) {
               print_int(func(i)); /* stuff */
       }

Tprintf("\n");

       return 0;
}

int func(int num) {
       return num * num;
}

void print_int(int num) {
       printf("%d", num);
}
EOF

test_expect_success 'file creation' '
	git apply patch1.patch
'

test_expect_success 'patch2 fails (retab)' '
	test_must_fail git apply patch2.patch
'

test_expect_success 'patch2 applies with --ignore-whitespace' '
	git apply --ignore-whitespace patch2.patch
'

test_expect_success 'patch2 reverse applies with --ignore-space-change' '
	git apply -R --ignore-space-change patch2.patch
'

git config apply.ignorewhitespace change

test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
	git apply patch2.patch &&
	test_cmp main.c.final main.c
'

test_expect_success 'patch3 fails (missing string at EOL)' '
	test_must_fail git apply patch3.patch
'

test_expect_success 'patch4 fails (missing EOL at EOF)' '
	test_must_fail git apply patch4.patch
'

test_expect_success 'patch5 fails (leading whitespace differences matter)' '
	test_must_fail git apply patch5.patch
'

test_expect_success 're-create file (with --ignore-whitespace)' '
	rm -f main.c &&
	git apply patch1.patch
'

test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
	test_must_fail git apply --no-ignore-whitespace patch5.patch
'

test_expect_success 'apply --ignore-space-change --inaccurate-eof' '
	echo 1 >file &&
	git apply --ignore-space-change --inaccurate-eof <<-\EOF &&
	diff --git a/file b/file
	--- a/file
	+++ b/file
	@@ -1 +1 @@
	-1
	+2
	EOF
	printf 2 >expect &&
	test_cmp expect file
'

test_done