3 # Copyright (c) 2009 Giuseppe Bilotta
6 test_description='git-apply --ignore-whitespace.
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.
14 cat > patch1.patch <<\EOF
15 diff --git a/main.c b/main.c
22 +void print_int(int num);
28 + for (i = 0; i < 10; i++) {
29 + print_int(func(i)); /* stuff */
39 +void print_int(int num) {
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.
49 # This patch will fail unless whitespace differences are being ignored
51 sed -e 's/Z/ /g' > patch2.patch <<\EOF
52 diff --git a/main.c b/main.c
56 Z print_int(func(i)); /* stuff */
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
71 sed -e 's/Z/ /g' > patch3.patch <<\EOF
72 diff --git a/main.c b/main.c
76 Z for (i = 0; i < 10; i++) {
77 Z print_int(func(i));Z
82 # This patch will fail even if whitespace differences are being ignored,
83 # because of the missing EOL at EOF.
85 sed -e 's/Z/ /g' > patch4.patch <<\EOF
86 diff --git a/main.c b/main.c
91 \ No newline at end of file
95 # This patch will fail unless whitespace differences are being ignored.
97 sed -e 's/Z/ /g' > patch5.patch <<\EOF
98 diff --git a/main.c b/main.c
102 Z void print_int(int num);
107 # And this is how the final output should be. Patches introduce
108 # HTs but the original SP indents are mostly kept.
110 sed -e 's/T/ /g' > main.c.final <<\EOF
113 void print_int(int num);
119 for (i = 0; i < 10; i++) {
120 print_int(func(i)); /* stuff */
132 void print_int(int num) {
137 test_expect_success 'file creation' '
138 git apply patch1.patch
141 test_expect_success 'patch2 fails (retab)' '
142 test_must_fail git apply patch2.patch
145 test_expect_success 'patch2 applies with --ignore-whitespace' '
146 git apply --ignore-whitespace patch2.patch
149 test_expect_success 'patch2 reverse applies with --ignore-space-change' '
150 git apply -R --ignore-space-change patch2.patch
153 git config apply.ignorewhitespace change
155 test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
156 git apply patch2.patch &&
157 test_cmp main.c.final main.c
160 test_expect_success 'patch3 fails (missing string at EOL)' '
161 test_must_fail git apply patch3.patch
164 test_expect_success 'patch4 fails (missing EOL at EOF)' '
165 test_must_fail git apply patch4.patch
168 test_expect_success 'patch5 fails (leading whitespace differences matter)' '
169 test_must_fail git apply patch5.patch
172 test_expect_success 're-create file (with --ignore-whitespace)' '
174 git apply patch1.patch
177 test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
178 test_must_fail git apply --no-ignore-whitespace patch5.patch