]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0028-working-tree-encoding.sh
clone: allow "--bare" with "-o"
[thirdparty/git.git] / t / t0028-working-tree-encoding.sh
1 #!/bin/sh
2
3 test_description='working-tree-encoding conversion via gitattributes'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY/lib-encoding.sh"
10
11 GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING
12
13 test_expect_success 'setup test files' '
14 git config core.eol lf &&
15
16 text="hallo there!\ncan you read me?" &&
17 echo "*.utf16 text working-tree-encoding=utf-16" >.gitattributes &&
18 echo "*.utf16lebom text working-tree-encoding=UTF-16LE-BOM" >>.gitattributes &&
19 printf "$text" >test.utf8.raw &&
20 printf "$text" | write_utf16 >test.utf16.raw &&
21 printf "$text" | write_utf32 >test.utf32.raw &&
22 printf "\377\376" >test.utf16lebom.raw &&
23 printf "$text" | iconv -f UTF-8 -t UTF-16LE >>test.utf16lebom.raw &&
24
25 # Line ending tests
26 printf "one\ntwo\nthree\n" >lf.utf8.raw &&
27 printf "one\r\ntwo\r\nthree\r\n" >crlf.utf8.raw &&
28
29 # BOM tests
30 printf "\0a\0b\0c" >nobom.utf16be.raw &&
31 printf "a\0b\0c\0" >nobom.utf16le.raw &&
32 printf "\376\377\0a\0b\0c" >bebom.utf16be.raw &&
33 printf "\377\376a\0b\0c\0" >lebom.utf16le.raw &&
34 printf "\0\0\0a\0\0\0b\0\0\0c" >nobom.utf32be.raw &&
35 printf "a\0\0\0b\0\0\0c\0\0\0" >nobom.utf32le.raw &&
36 printf "\0\0\376\377\0\0\0a\0\0\0b\0\0\0c" >bebom.utf32be.raw &&
37 printf "\377\376\0\0a\0\0\0b\0\0\0c\0\0\0" >lebom.utf32le.raw &&
38
39 # Add only UTF-16 file, we will add the UTF-32 file later
40 cp test.utf16.raw test.utf16 &&
41 cp test.utf32.raw test.utf32 &&
42 cp test.utf16lebom.raw test.utf16lebom &&
43 git add .gitattributes test.utf16 test.utf16lebom &&
44 git commit -m initial
45 '
46
47 test_expect_success 'ensure UTF-8 is stored in Git' '
48 test_when_finished "rm -f test.utf16.git" &&
49
50 git cat-file -p :test.utf16 >test.utf16.git &&
51 test_cmp_bin test.utf8.raw test.utf16.git
52 '
53
54 test_expect_success 're-encode to UTF-16 on checkout' '
55 test_when_finished "rm -f test.utf16.raw" &&
56
57 rm test.utf16 &&
58 git checkout test.utf16 &&
59 test_cmp_bin test.utf16.raw test.utf16
60 '
61
62 test_expect_success 're-encode to UTF-16-LE-BOM on checkout' '
63 rm test.utf16lebom &&
64 git checkout test.utf16lebom &&
65 test_cmp_bin test.utf16lebom.raw test.utf16lebom
66 '
67
68 test_expect_success 'check $GIT_DIR/info/attributes support' '
69 test_when_finished "rm -f test.utf32.git" &&
70 test_when_finished "git reset --hard HEAD" &&
71
72 echo "*.utf32 text working-tree-encoding=utf-32" >.git/info/attributes &&
73 git add test.utf32 &&
74
75 git cat-file -p :test.utf32 >test.utf32.git &&
76 test_cmp_bin test.utf8.raw test.utf32.git
77 '
78
79 for i in 16 32
80 do
81 test_expect_success "check prohibited UTF-${i} BOM" '
82 test_when_finished "git reset --hard HEAD" &&
83
84 echo "*.utf${i}be text working-tree-encoding=utf-${i}be" >>.gitattributes &&
85 echo "*.utf${i}le text working-tree-encoding=utf-${i}LE" >>.gitattributes &&
86
87 # Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian)
88 # but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).
89 # In these cases the BOM is prohibited.
90 cp bebom.utf${i}be.raw bebom.utf${i}be &&
91 test_must_fail git add bebom.utf${i}be 2>err.out &&
92 test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
93 test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out &&
94
95 cp lebom.utf${i}le.raw lebom.utf${i}be &&
96 test_must_fail git add lebom.utf${i}be 2>err.out &&
97 test_i18ngrep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
98 test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out &&
99
100 cp bebom.utf${i}be.raw bebom.utf${i}le &&
101 test_must_fail git add bebom.utf${i}le 2>err.out &&
102 test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out &&
103 test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out &&
104
105 cp lebom.utf${i}le.raw lebom.utf${i}le &&
106 test_must_fail git add lebom.utf${i}le 2>err.out &&
107 test_i18ngrep "fatal: BOM is prohibited .* utf-${i}LE" err.out &&
108 test_i18ngrep "use UTF-${i} as working-tree-encoding" err.out
109 '
110
111 test_expect_success "check required UTF-${i} BOM" '
112 test_when_finished "git reset --hard HEAD" &&
113
114 echo "*.utf${i} text working-tree-encoding=utf-${i}" >>.gitattributes &&
115
116 cp nobom.utf${i}be.raw nobom.utf${i} &&
117 test_must_fail git add nobom.utf${i} 2>err.out &&
118 test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out &&
119 test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out &&
120
121 cp nobom.utf${i}le.raw nobom.utf${i} &&
122 test_must_fail git add nobom.utf${i} 2>err.out &&
123 test_i18ngrep "fatal: BOM is required .* utf-${i}" err.out &&
124 test_i18ngrep "use UTF-${i}BE or UTF-${i}LE" err.out
125 '
126
127 test_expect_success "eol conversion for UTF-${i} encoded files on checkout" '
128 test_when_finished "rm -f crlf.utf${i}.raw lf.utf${i}.raw" &&
129 test_when_finished "git reset --hard HEAD^" &&
130
131 cat lf.utf8.raw | write_utf${i} >lf.utf${i}.raw &&
132 cat crlf.utf8.raw | write_utf${i} >crlf.utf${i}.raw &&
133 cp crlf.utf${i}.raw eol.utf${i} &&
134
135 cat >expectIndexLF <<-EOF &&
136 i/lf w/-text attr/text eol.utf${i}
137 EOF
138
139 git add eol.utf${i} &&
140 git commit -m eol &&
141
142 # UTF-${i} with CRLF (Windows line endings)
143 rm eol.utf${i} &&
144 git -c core.eol=crlf checkout eol.utf${i} &&
145 test_cmp_bin crlf.utf${i}.raw eol.utf${i} &&
146
147 # Although the file has CRLF in the working tree,
148 # ensure LF in the index
149 git ls-files --eol eol.utf${i} >actual &&
150 test_cmp expectIndexLF actual &&
151
152 # UTF-${i} with LF (Unix line endings)
153 rm eol.utf${i} &&
154 git -c core.eol=lf checkout eol.utf${i} &&
155 test_cmp_bin lf.utf${i}.raw eol.utf${i} &&
156
157 # The file LF in the working tree, ensure LF in the index
158 git ls-files --eol eol.utf${i} >actual &&
159 test_cmp expectIndexLF actual
160 '
161 done
162
163 test_expect_success 'check unsupported encodings' '
164 test_when_finished "git reset --hard HEAD" &&
165
166 echo "*.set text working-tree-encoding" >.gitattributes &&
167 printf "set" >t.set &&
168 test_must_fail git add t.set 2>err.out &&
169 test_i18ngrep "true/false are no valid working-tree-encodings" err.out &&
170
171 echo "*.unset text -working-tree-encoding" >.gitattributes &&
172 printf "unset" >t.unset &&
173 git add t.unset &&
174
175 echo "*.empty text working-tree-encoding=" >.gitattributes &&
176 printf "empty" >t.empty &&
177 git add t.empty &&
178
179 echo "*.garbage text working-tree-encoding=garbage" >.gitattributes &&
180 printf "garbage" >t.garbage &&
181 test_must_fail git add t.garbage 2>err.out &&
182 test_i18ngrep "failed to encode" err.out
183 '
184
185 test_expect_success 'error if encoding round trip is not the same during refresh' '
186 BEFORE_STATE=$(git rev-parse HEAD) &&
187 test_when_finished "git reset --hard $BEFORE_STATE" &&
188
189 # Add and commit a UTF-16 file but skip the "working-tree-encoding"
190 # filter. Consequently, the in-repo representation is UTF-16 and not
191 # UTF-8. This simulates a Git version that has no working tree encoding
192 # support.
193 echo "*.utf16le text working-tree-encoding=utf-16le" >.gitattributes &&
194 echo "hallo" >nonsense.utf16le &&
195 TEST_HASH=$(git hash-object --no-filters -w nonsense.utf16le) &&
196 git update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16le &&
197 COMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m "plain commit" $(git write-tree)) &&
198 git update-ref refs/heads/main $COMMIT &&
199
200 test_must_fail git checkout HEAD^ 2>err.out &&
201 test_i18ngrep "error: .* overwritten by checkout:" err.out
202 '
203
204 test_expect_success 'error if encoding garbage is already in Git' '
205 BEFORE_STATE=$(git rev-parse HEAD) &&
206 test_when_finished "git reset --hard $BEFORE_STATE" &&
207
208 # Skip the UTF-16 filter for the added file
209 # This simulates a Git version that has no checkoutEncoding support
210 cp nobom.utf16be.raw nonsense.utf16 &&
211 TEST_HASH=$(git hash-object --no-filters -w nonsense.utf16) &&
212 git update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16 &&
213 COMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m "plain commit" $(git write-tree)) &&
214 git update-ref refs/heads/main $COMMIT &&
215
216 git diff 2>err.out &&
217 test_i18ngrep "error: BOM is required" err.out
218 '
219
220 test_lazy_prereq ICONV_SHIFT_JIS '
221 iconv -f UTF-8 -t SHIFT-JIS </dev/null
222 '
223
224 test_expect_success ICONV_SHIFT_JIS 'check roundtrip encoding' '
225 test_when_finished "rm -f roundtrip.shift roundtrip.utf16" &&
226 test_when_finished "git reset --hard HEAD" &&
227
228 text="hallo there!\nroundtrip test here!" &&
229 printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift &&
230 printf "$text" | write_utf16 >roundtrip.utf16 &&
231 echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes &&
232
233 # SHIFT-JIS encoded files are round-trip checked by default...
234 GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 |
235 grep "Checking roundtrip encoding for SHIFT-JIS" &&
236 git reset &&
237
238 # ... unless we overwrite the Git config!
239 ! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage \
240 add .gitattributes roundtrip.shift 2>&1 |
241 grep "Checking roundtrip encoding for SHIFT-JIS" &&
242 git reset &&
243
244 # UTF-16 encoded files should not be round-trip checked by default...
245 ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 |
246 grep "Checking roundtrip encoding for UTF-16" &&
247 git reset &&
248
249 # ... unless we tell Git to check it!
250 GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-16, UTF-32" \
251 add roundtrip.utf16 2>&1 |
252 grep "Checking roundtrip encoding for utf-16" &&
253 git reset &&
254
255 # ... unless we tell Git to check it!
256 # (here we also check that the casing of the encoding is irrelevant)
257 GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-32, utf-16" \
258 add roundtrip.utf16 2>&1 |
259 grep "Checking roundtrip encoding for utf-16" &&
260 git reset
261 '
262
263 # $1: checkout encoding
264 # $2: test string
265 # $3: binary test string in checkout encoding
266 test_commit_utf8_checkout_other () {
267 encoding="$1"
268 orig_string="$2"
269 expect_bytes="$3"
270
271 test_expect_success "Commit UTF-8, checkout $encoding" '
272 test_when_finished "git checkout HEAD -- .gitattributes" &&
273
274 test_ext="commit_utf8_checkout_$encoding" &&
275 test_file="test.$test_ext" &&
276
277 # Commit as UTF-8
278 echo "*.$test_ext text working-tree-encoding=UTF-8" >.gitattributes &&
279 printf "$orig_string" >$test_file &&
280 git add $test_file &&
281 git commit -m "Test data" &&
282
283 # Checkout in tested encoding
284 rm $test_file &&
285 echo "*.$test_ext text working-tree-encoding=$encoding" >.gitattributes &&
286 git checkout HEAD -- $test_file &&
287
288 # Test
289 printf $expect_bytes >$test_file.raw &&
290 test_cmp_bin $test_file.raw $test_file
291 '
292 }
293
294 test_commit_utf8_checkout_other "UTF-8" "Test Тест" "\124\145\163\164\040\320\242\320\265\321\201\321\202"
295 test_commit_utf8_checkout_other "UTF-16LE" "Test Тест" "\124\000\145\000\163\000\164\000\040\000\042\004\065\004\101\004\102\004"
296 test_commit_utf8_checkout_other "UTF-16BE" "Test Тест" "\000\124\000\145\000\163\000\164\000\040\004\042\004\065\004\101\004\102"
297 test_commit_utf8_checkout_other "UTF-16LE-BOM" "Test Тест" "\377\376\124\000\145\000\163\000\164\000\040\000\042\004\065\004\101\004\102\004"
298 test_commit_utf8_checkout_other "UTF-16BE-BOM" "Test Тест" "\376\377\000\124\000\145\000\163\000\164\000\040\004\042\004\065\004\101\004\102"
299 test_commit_utf8_checkout_other "UTF-32LE" "Test Тест" "\124\000\000\000\145\000\000\000\163\000\000\000\164\000\000\000\040\000\000\000\042\004\000\000\065\004\000\000\101\004\000\000\102\004\000\000"
300 test_commit_utf8_checkout_other "UTF-32BE" "Test Тест" "\000\000\000\124\000\000\000\145\000\000\000\163\000\000\000\164\000\000\000\040\000\000\004\042\000\000\004\065\000\000\004\101\000\000\004\102"
301
302 test_done