Merge branch 'cl/config-regexp-docfix' into maint-2.46
[git/gitster.git] / t / t9802-git-p4-filetype.sh
blob1bc48305b017897a143f3d3b660c506e8f1f423e
1 #!/bin/sh
3 test_description='git p4 filetype tests'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./lib-git-p4.sh
8 test_expect_success 'start p4d' '
9 start_p4d
13 # This series of tests checks newline handling Both p4 and
14 # git store newlines as \n, and have options to choose how
15 # newlines appear in checked-out files.
17 test_expect_success 'p4 client newlines, unix' '
19 cd "$cli" &&
20 p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
21 printf "unix\ncrlf\n" >f-unix &&
22 printf "unix\r\ncrlf\r\n" >f-unix-as-crlf &&
23 p4 add -t text f-unix &&
24 p4 submit -d f-unix &&
26 # LineEnd: unix; should be no change after sync
27 cp f-unix f-unix-orig &&
28 p4 sync -f &&
29 test_cmp f-unix-orig f-unix &&
31 # make sure stored in repo as unix newlines
32 # use sed to eat python-appended newline
33 p4 -G print //depot/f-unix | marshal_dump data 2 |\
34 sed \$d >f-unix-p4-print &&
35 test_cmp f-unix-orig f-unix-p4-print &&
37 # switch to win, make sure lf -> crlf
38 p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
39 p4 sync -f &&
40 test_cmp f-unix-as-crlf f-unix
44 test_expect_success 'p4 client newlines, win' '
46 cd "$cli" &&
47 p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
48 printf "win\r\ncrlf\r\n" >f-win &&
49 printf "win\ncrlf\n" >f-win-as-lf &&
50 p4 add -t text f-win &&
51 p4 submit -d f-win &&
53 # LineEnd: win; should be no change after sync
54 cp f-win f-win-orig &&
55 p4 sync -f &&
56 test_cmp f-win-orig f-win &&
58 # make sure stored in repo as unix newlines
59 # use sed to eat python-appened newline
60 p4 -G print //depot/f-win | marshal_dump data 2 |\
61 sed \$d >f-win-p4-print &&
62 test_cmp f-win-as-lf f-win-p4-print &&
64 # switch to unix, make sure lf -> crlf
65 p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
66 p4 sync -f &&
67 test_cmp f-win-as-lf f-win
71 test_expect_success 'ensure blobs store only lf newlines' '
72 test_when_finished cleanup_git &&
74 cd "$git" &&
75 git init &&
76 git p4 sync //depot@all &&
78 # verify the files in .git are stored only with newlines
79 o=$(git ls-tree p4/master -- f-unix | cut -f1 | cut -d\ -f3) &&
80 git cat-file blob $o >f-unix-blob &&
81 test_cmp "$cli"/f-unix-orig f-unix-blob &&
83 o=$(git ls-tree p4/master -- f-win | cut -f1 | cut -d\ -f3) &&
84 git cat-file blob $o >f-win-blob &&
85 test_cmp "$cli"/f-win-as-lf f-win-blob &&
87 rm f-unix-blob f-win-blob
91 test_expect_success 'gitattributes setting eol=lf produces lf newlines' '
92 test_when_finished cleanup_git &&
94 # checkout the files and make sure core.eol works as planned
95 cd "$git" &&
96 git init &&
97 echo "* eol=lf" >.gitattributes &&
98 git p4 sync //depot@all &&
99 git checkout -b master p4/master &&
100 test_cmp "$cli"/f-unix-orig f-unix &&
101 test_cmp "$cli"/f-win-as-lf f-win
105 test_expect_success 'gitattributes setting eol=crlf produces crlf newlines' '
106 test_when_finished cleanup_git &&
108 # checkout the files and make sure core.eol works as planned
109 cd "$git" &&
110 git init &&
111 echo "* eol=crlf" >.gitattributes &&
112 git p4 sync //depot@all &&
113 git checkout -b master p4/master &&
114 test_cmp "$cli"/f-unix-as-crlf f-unix &&
115 test_cmp "$cli"/f-win-orig f-win
119 test_expect_success 'crlf cleanup' '
121 cd "$cli" &&
122 rm f-unix-orig f-unix-as-crlf &&
123 rm f-win-orig f-win-as-lf &&
124 p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
125 p4 sync -f
129 test_expect_success 'utf-16 file create' '
131 cd "$cli" &&
133 # p4 saves this verbatim
134 printf "three\nline\ntext\n" >f-ascii &&
135 p4 add -t text f-ascii &&
137 # p4 adds \377\376 header
138 cp f-ascii f-ascii-as-utf16 &&
139 p4 add -t utf16 f-ascii-as-utf16 &&
141 # p4 saves this exactly as iconv produced it
142 printf "three\nline\ntext\n" | iconv -f ascii -t utf-16 >f-utf16 &&
143 p4 add -t utf16 f-utf16 &&
145 # this also is unchanged
146 cp f-utf16 f-utf16-as-text &&
147 p4 add -t text f-utf16-as-text &&
149 p4 submit -d "f files" &&
151 # force update of client files
152 p4 sync -f
156 test_expect_success 'utf-16 file test' '
157 test_when_finished cleanup_git &&
158 git p4 clone --dest="$git" //depot@all &&
160 cd "$git" &&
162 test_cmp "$cli/f-ascii" f-ascii &&
163 test_cmp "$cli/f-ascii-as-utf16" f-ascii-as-utf16 &&
164 test_cmp "$cli/f-utf16" f-utf16 &&
165 test_cmp "$cli/f-utf16-as-text" f-utf16-as-text
169 test_expect_success 'keyword file create' '
171 cd "$cli" &&
173 printf "id\n\$Id\$\n\$Author\$\ntext\n" >k-text-k &&
174 p4 add -t text+k k-text-k &&
176 cp k-text-k k-text-ko &&
177 p4 add -t text+ko k-text-ko &&
179 iconv -f ascii -t utf-16 <k-text-k >k-utf16-k &&
180 p4 add -t utf16+k k-utf16-k &&
182 cp k-utf16-k k-utf16-ko &&
183 p4 add -t utf16+ko k-utf16-ko &&
185 p4 submit -d "k files" &&
186 p4 sync -f
190 build_smush() {
191 cat >k_smush.py <<-\EOF &&
192 import re, sys
193 sys.stdout.write(re.sub(r'(?i)\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', sys.stdin.read()))
195 cat >ko_smush.py <<-\EOF
196 import re, sys
197 sys.stdout.write(re.sub(r'(?i)\$(Id|Header):[^$]*\$', r'$\1$', sys.stdin.read()))
201 test_expect_success 'keyword file test' '
202 build_smush &&
203 test_when_finished rm -f k_smush.py ko_smush.py &&
204 test_when_finished cleanup_git &&
205 git p4 clone --dest="$git" //depot@all &&
207 cd "$git" &&
209 # text, ensure unexpanded
210 "$PYTHON_PATH" "$TRASH_DIRECTORY/k_smush.py" <"$cli/k-text-k" >cli-k-text-k-smush &&
211 test_cmp cli-k-text-k-smush k-text-k &&
212 "$PYTHON_PATH" "$TRASH_DIRECTORY/ko_smush.py" <"$cli/k-text-ko" >cli-k-text-ko-smush &&
213 test_cmp cli-k-text-ko-smush k-text-ko &&
215 # utf16, even though p4 expands keywords, git p4 does not
216 # try to undo that
217 test_cmp "$cli/k-utf16-k" k-utf16-k &&
218 test_cmp "$cli/k-utf16-ko" k-utf16-ko
222 build_gendouble() {
223 cat >gendouble.py <<-\EOF
224 import sys
225 import struct
227 s = struct.pack(b">LL18s",
228 0x00051607, # AppleDouble
229 0x00020000, # version 2
230 b"" # pad to 26 bytes
232 getattr(sys.stdout, 'buffer', sys.stdout).write(s)
236 test_expect_success 'ignore apple' '
237 test_when_finished rm -f gendouble.py &&
238 build_gendouble &&
240 cd "$cli" &&
241 test-tool genrandom apple 1024 >double.png &&
242 "$PYTHON_PATH" "$TRASH_DIRECTORY/gendouble.py" >%double.png &&
243 p4 add -t apple double.png &&
244 p4 submit -d appledouble
245 ) &&
246 test_when_finished cleanup_git &&
247 git p4 clone --dest="$git" //depot@all &&
249 cd "$git" &&
250 test ! -f double.png
254 test_expect_success SYMLINKS 'create p4 symlink' '
255 cd "$cli" &&
256 ln -s symlink-target symlink &&
257 p4 add symlink &&
258 p4 submit -d "add symlink"
261 test_expect_success SYMLINKS 'ensure p4 symlink parsed correctly' '
262 test_when_finished cleanup_git &&
263 git p4 clone --dest="$git" //depot@all &&
265 cd "$git" &&
266 test -L symlink &&
267 test $(test_readlink symlink) = symlink-target
271 test_expect_success SYMLINKS 'empty symlink target' '
273 # first create the file as a file
274 cd "$cli" &&
275 >empty-symlink &&
276 p4 add empty-symlink &&
277 p4 submit -d "add empty-symlink as a file"
278 ) &&
280 # now change it to be a symlink to "target1"
281 cd "$cli" &&
282 p4 edit empty-symlink &&
283 p4 reopen -t symlink empty-symlink &&
284 rm empty-symlink &&
285 ln -s target1 empty-symlink &&
286 p4 add empty-symlink &&
287 p4 submit -d "make empty-symlink point to target1"
288 ) &&
290 # Hack the p4 depot to make the symlink point to nothing;
291 # this should not happen in reality, but shows up
292 # in p4 repos in the wild.
294 # The sed expression changes this:
295 # @@
296 # text
297 # @target1
299 # to this:
300 # @@
301 # text
302 # @@
304 # Note that newer Perforce versions started to store files
305 # compressed in directories. The case statement handles both
306 # old and new layout.
307 cd "$db/depot" &&
308 case "$(echo empty-symlink*)" in
309 empty-symlink,v)
310 sed "/@target1/{; s/target1/@/; n; d; }" \
311 empty-symlink,v >empty-symlink,v.tmp &&
312 mv empty-symlink,v.tmp empty-symlink,v;;
313 empty-symlink,d)
314 path="empty-symlink,d/$(ls empty-symlink,d/ | tail -n1)" &&
315 rm "$path" &&
316 gzip </dev/null >"$path";;
318 BUG "unhandled p4d layout";;
319 esac
320 ) &&
322 # Make sure symlink really is empty. Asking
323 # p4 to sync here will make it generate errors.
324 cd "$cli" &&
325 p4 print -q //depot/empty-symlink#2 >out &&
326 test_must_be_empty out
327 ) &&
328 test_when_finished cleanup_git &&
330 # make sure git p4 handles it without error
331 git p4 clone --dest="$git" //depot@all &&
333 # fix the symlink, make it point to "target2"
335 cd "$cli" &&
336 p4 open empty-symlink &&
337 rm empty-symlink &&
338 ln -s target2 empty-symlink &&
339 p4 submit -d "make empty-symlink point to target2"
340 ) &&
341 cleanup_git &&
342 git p4 clone --dest="$git" //depot@all &&
344 cd "$git" &&
345 test $(test_readlink empty-symlink) = target2
349 test_expect_success SYMLINKS 'utf-8 with and without BOM in text file' '
351 cd "$cli" &&
353 # some utf8 content
354 echo some tǣxt >utf8-nobom-test &&
356 # same utf8 content as before but with bom
357 echo some tǣxt | sed '\''s/^/\xef\xbb\xbf/'\'' >utf8-bom-test &&
359 # bom only
360 dd bs=1 count=3 if=utf8-bom-test of=utf8-bom-empty-test &&
362 p4 add utf8-nobom-test utf8-bom-test utf8-bom-empty-test &&
363 p4 submit -d "add utf8 test files"
364 ) &&
365 test_when_finished cleanup_git &&
367 git p4 clone --dest="$git" //depot@all &&
369 cd "$git" &&
370 git checkout refs/remotes/p4/master &&
372 echo some tǣxt >utf8-nobom-check &&
373 test_cmp utf8-nobom-check utf8-nobom-test &&
375 echo some tǣxt | sed '\''s/^/\xef\xbb\xbf/'\'' >utf8-bom-check &&
376 test_cmp utf8-bom-check utf8-bom-test &&
378 dd bs=1 count=3 if=utf8-bom-check of=utf8-bom-empty-check &&
379 test_cmp utf8-bom-empty-check utf8-bom-empty-test
383 test_done