archive: handle commits with an empty tree
[git/jnareb-git.git] / t / t4107-apply-ignore-whitespace.sh
blobb04fc8fc12238c3326306ed0e055b7d67bf950b4
1 #!/bin/sh
3 # Copyright (c) 2009 Giuseppe Bilotta
6 test_description='git-apply --ignore-whitespace.
9 . ./test-lib.sh
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
16 new file mode 100644
17 --- /dev/null
18 +++ b/main.c
19 @@ -0,0 +1,22 @@
20 +#include <stdio.h>
22 +void print_int(int num);
23 +int func(int num);
25 +int main() {
26 + int i;
28 + for (i = 0; i < 10; i++) {
29 + print_int(func(i)); /* stuff */
30 + }
32 + return 0;
35 +int func(int num) {
36 + return num * num;
39 +void print_int(int num) {
40 + printf("%d", num);
42 EOF
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
53 --- a/main.c
54 +++ b/main.c
55 @@ -10,6 +10,8 @@
56 Z print_int(func(i)); /* stuff */
57 Z }
59 + printf("\n");
61 Z return 0;
64 EOF
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.
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
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
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
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
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);
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
111 #include <stdio.h>
113 void print_int(int num);
114 T/* a comment */
115 int func(int num);
117 int main() {
118 int i;
120 for (i = 0; i < 10; i++) {
121 print_int(func(i)); /* stuff */
124 Tprintf("\n");
126 return 0;
129 int func(int num) {
130 return num * num;
133 void print_int(int num) {
134 printf("%d", num);
138 test_expect_success 'file creation' '
139 git apply patch1.patch
142 test_expect_success 'patch2 fails (retab)' '
143 test_must_fail git apply patch2.patch
146 test_expect_success 'patch2 applies with --ignore-whitespace' '
147 git apply --ignore-whitespace patch2.patch
150 test_expect_success 'patch2 reverse applies with --ignore-space-change' '
151 git apply -R --ignore-space-change patch2.patch
154 git config apply.ignorewhitespace change
156 test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
157 git apply patch2.patch
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 applies (leading whitespace)' '
169 git apply patch5.patch
172 test_expect_success 'patches do not mangle whitespace' '
173 test_cmp main.c main.c.final
176 test_expect_success 're-create file (with --ignore-whitespace)' '
177 rm -f main.c &&
178 git apply patch1.patch
181 test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
182 test_must_fail git apply --no-ignore-whitespace patch5.patch
185 test_done