interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t4107-apply-ignore-whitespace.sh

194 lines · 4053 bytes Raw Blame History
1#!/bin/sh
2#
3# Copyright (c) 2009 Giuseppe Bilotta
4#
5
6test_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
13cat > patch1.patch <<\EOF
14diff --git a/main.c b/main.c
15new 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+}
41EOF
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
50sed -e 's/Z/ /g' > patch2.patch <<\EOF
51diff --git a/main.c b/main.c
52--- a/main.c
53+++ b/main.c
54@@ -10,6 +10,8 @@
55Z print_int(func(i)); /* stuff */
56Z }
57Z
58+ printf("\n");
59+
60Z return 0;
61Z}
62Z
63EOF
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
70sed -e 's/Z/ /g' > patch3.patch <<\EOF
71diff --git a/main.c b/main.c
72--- a/main.c
73+++ b/main.c
74@@ -10,3 +10,4 @@
75Z for (i = 0; i < 10; i++) {
76Z print_int(func(i));Z
77+ /* stuff */
78Z }
79EOF
80
81# This patch will fail even if whitespace differences are being ignored,
82# because of the missing EOL at EOF.
83
84sed -e 's/Z/ /g' > patch4.patch <<\EOF
85diff --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+ };
92EOF
93
94# This patch will fail unless whitespace differences are being ignored.
95
96sed -e 's/Z/ /g' > patch5.patch <<\EOF
97diff --git a/main.c b/main.c
98--- a/main.c
99+++ b/main.c
100@@ -2,2 +2,3 @@
101Z void print_int(int num);
102+ /* a comment */
103Z int func(int num);
104EOF
105
106# And this is how the final output should be. Patches introduce
107# HTs but the original SP indents are mostly kept.
108
109sed -e 's/T/ /g' > main.c.final <<\EOF
110#include <stdio.h>
111
112void print_int(int num);
113int func(int num);
114
115int main() {
116 int i;
117
118 for (i = 0; i < 10; i++) {
119 print_int(func(i)); /* stuff */
120 }
121
122Tprintf("\n");
123
124 return 0;
125}
126
127int func(int num) {
128 return num * num;
129}
130
131void print_int(int num) {
132 printf("%d", num);
133}
134EOF
135
136test_expect_success 'file creation' '
137 git apply patch1.patch
138'
139
140test_expect_success 'patch2 fails (retab)' '
141 test_must_fail git apply patch2.patch
142'
143
144test_expect_success 'patch2 applies with --ignore-whitespace' '
145 git apply --ignore-whitespace patch2.patch
146'
147
148test_expect_success 'patch2 reverse applies with --ignore-space-change' '
149 git apply -R --ignore-space-change patch2.patch
150'
151
152git config apply.ignorewhitespace change
153
154test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
155 git apply patch2.patch &&
156 test_cmp main.c.final main.c
157'
158
159test_expect_success 'patch3 fails (missing string at EOL)' '
160 test_must_fail git apply patch3.patch
161'
162
163test_expect_success 'patch4 fails (missing EOL at EOF)' '
164 test_must_fail git apply patch4.patch
165'
166
167test_expect_success 'patch5 fails (leading whitespace differences matter)' '
168 test_must_fail git apply patch5.patch
169'
170
171test_expect_success 're-create file (with --ignore-whitespace)' '
172 rm -f main.c &&
173 git apply patch1.patch
174'
175
176test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
177 test_must_fail git apply --no-ignore-whitespace patch5.patch
178'
179
180test_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
194test_done