3 test_description
='blob conversion via gitattributes'
10 write_script
<<\EOF
"$TEST_ROOT/rot13.sh"
12 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
13 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
16 write_script rot13-filter.pl
"$PERL_PATH" \
17 <"$TEST_DIRECTORY"/t0021
/rot13-filter.pl
19 generate_random_characters
() {
22 test-genrandom some-seed
$LEN |
23 perl
-pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
27 perl
-e 'print -s $ARGV[0]' "$1"
31 rm -f rot13-filter.log
&&
32 git
"$@" 2>git-stderr.log
&&
36 # Compare two files and ensure that `clean` and `smudge` respectively are
37 # called at least once if specified in the `expect` file. The actual
38 # invocation count is not relevant because their number can vary.
39 # c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
43 for FILE
in "$expect" "$actual"
45 sort "$FILE" |
uniq -c |
46 sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp" &&
47 mv "$FILE.tmp" "$FILE" ||
return
49 test_cmp
"$expect" "$actual"
52 # Compare two files but exclude all `clean` invocations because Git can
53 # call `clean` zero or more times.
54 # c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
55 test_cmp_exclude_clean
() {
58 for FILE
in "$expect" "$actual"
60 grep -v "IN: clean" "$FILE" >"$FILE.tmp" &&
61 mv "$FILE.tmp" "$FILE"
63 test_cmp
"$expect" "$actual"
66 # Check that the contents of two files are equal and that their rot13 version
67 # is equal to the committed content.
68 test_cmp_committed_rot13
() {
70 rot13.sh
<"$1" >expected
&&
71 git cat-file blob
:"$2" >actual
&&
72 test_cmp expected actual
75 test_expect_success setup
'
76 git config filter.rot13.smudge ./rot13.sh &&
77 git config filter.rot13.clean ./rot13.sh &&
80 echo "*.t filter=rot13"
85 echo a b c d e f g h i j k l m
86 echo n o p q r s t u v w x y z
92 git add test test.t test.i &&
93 rm -f test test.t test.i &&
94 git checkout -- test test.t test.i &&
96 echo "content-test2" >test2.o &&
97 echo "content-test3 - filename with special characters" >"test3 '\''sq'\'',\$x.o"
100 script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
102 test_expect_success check
'
104 test_cmp test.o test &&
105 test_cmp test.o test.t &&
107 # ident should be stripped in the repository
108 git diff --raw --exit-code :test :test.i &&
109 id=$(git rev-parse --verify :test) &&
110 embedded=$(sed -ne "$script" test.i) &&
111 test "z$id" = "z$embedded" &&
113 git cat-file blob :test.t >test.r &&
115 ./rot13.sh <test.o >test.t &&
116 test_cmp test.r test.t
119 # If an expanded ident ever gets into the repository, we want to make sure that
120 # it is collapsed before being expanded again on checkout
121 test_expect_success expanded_in_repo
'
123 echo "File with expanded keywords"
126 echo "\$Id: 0000000000000000000000000000000000000000 \$"
127 echo "\$Id: NoSpaceAtEnd\$"
128 echo "\$Id:NoSpaceAtFront \$"
129 echo "\$Id:NoSpaceAtEitherEnd\$"
130 echo "\$Id: NoTerminatingSymbol"
131 echo "\$Id: Foreign Commit With Spaces \$"
132 } >expanded-keywords.0 &&
135 cat expanded-keywords.0 &&
136 printf "\$Id: NoTerminatingSymbolAtEOF"
137 } >expanded-keywords &&
138 cat expanded-keywords >expanded-keywords-crlf &&
139 git add expanded-keywords expanded-keywords-crlf &&
140 git commit -m "File with keywords expanded" &&
141 id=$(git rev-parse --verify :expanded-keywords) &&
144 echo "File with expanded keywords"
151 echo "\$Id: NoTerminatingSymbol"
152 echo "\$Id: Foreign Commit With Spaces \$"
153 } >expected-output.0 &&
155 cat expected-output.0 &&
156 printf "\$Id: NoTerminatingSymbolAtEOF"
157 } >expected-output &&
159 append_cr <expected-output.0 &&
160 printf "\$Id: NoTerminatingSymbolAtEOF"
161 } >expected-output-crlf &&
163 echo "expanded-keywords ident"
164 echo "expanded-keywords-crlf ident text eol=crlf"
165 } >>.gitattributes &&
167 rm -f expanded-keywords expanded-keywords-crlf &&
169 git checkout -- expanded-keywords &&
170 test_cmp expanded-keywords expected-output &&
172 git checkout -- expanded-keywords-crlf &&
173 test_cmp expanded-keywords-crlf expected-output-crlf
176 # The use of %f in a filter definition is expanded to the path to
177 # the filename being smudged or cleaned. It must be shell escaped.
178 # First, set up some interesting file names and pet them in
180 test_expect_success
'filter shell-escaped filenames' '
181 cat >argc.sh <<-EOF &&
186 normal=name-no-magic &&
187 special="name with '\''sq'\'' and \$x" &&
188 echo some test text >"$normal" &&
189 echo some test text >"$special" &&
190 git add "$normal" "$special" &&
191 git commit -q -m "add files" &&
192 echo "name* filter=argc" >.gitattributes &&
194 # delete the files and check them out again, using a smudge filter
195 # that will count the args and echo the command-line back to us
196 test_config filter.argc.smudge "sh ./argc.sh %f" &&
197 rm "$normal" "$special" &&
198 git checkout -- "$normal" "$special" &&
200 # make sure argc.sh counted the right number of args
201 echo "argc: 1 $normal" >expect &&
202 test_cmp expect "$normal" &&
203 echo "argc: 1 $special" >expect &&
204 test_cmp expect "$special" &&
206 # do the same thing, but with more args in the filter expression
207 test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" &&
208 rm "$normal" "$special" &&
209 git checkout -- "$normal" "$special" &&
211 # make sure argc.sh counted the right number of args
212 echo "argc: 2 $normal --my-extra-arg" >expect &&
213 test_cmp expect "$normal" &&
214 echo "argc: 2 $special --my-extra-arg" >expect &&
215 test_cmp expect "$special" &&
219 test_expect_success
'required filter should filter data' '
220 test_config filter.required.smudge ./rot13.sh &&
221 test_config filter.required.clean ./rot13.sh &&
222 test_config filter.required.required true &&
224 echo "*.r filter=required" >.gitattributes &&
226 cat test.o >test.r &&
230 git checkout -- test.r &&
231 test_cmp test.o test.r &&
233 ./rot13.sh <test.o >expected &&
234 git cat-file blob :test.r >actual &&
235 test_cmp expected actual
238 test_expect_success
'required filter smudge failure' '
239 test_config filter.failsmudge.smudge false &&
240 test_config filter.failsmudge.clean cat &&
241 test_config filter.failsmudge.required true &&
243 echo "*.fs filter=failsmudge" >.gitattributes &&
245 echo test >test.fs &&
248 test_must_fail git checkout -- test.fs
251 test_expect_success
'required filter clean failure' '
252 test_config filter.failclean.smudge cat &&
253 test_config filter.failclean.clean false &&
254 test_config filter.failclean.required true &&
256 echo "*.fc filter=failclean" >.gitattributes &&
258 echo test >test.fc &&
259 test_must_fail git add test.fc
262 test_expect_success
'filtering large input to small output should use little memory' '
263 test_config filter.devnull.clean "cat >/dev/null" &&
264 test_config filter.devnull.required true &&
265 for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB &&
266 echo "30MB filter=devnull" >.gitattributes &&
267 GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
270 test_expect_success
'filter that does not read is fine' '
271 test-genrandom foo $((128 * 1024 + 1)) >big &&
272 echo "big filter=epipe" >.gitattributes &&
273 test_config filter.epipe.clean "echo xyzzy" &&
275 git cat-file blob :big >actual &&
276 echo xyzzy >expect &&
277 test_cmp expect actual
280 test_expect_success EXPENSIVE
'filter large file' '
281 test_config filter.largefile.smudge cat &&
282 test_config filter.largefile.clean cat &&
283 for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB &&
284 echo "2GB filter=largefile" >.gitattributes &&
286 test_must_be_empty err &&
288 git checkout -- 2GB 2>err &&
289 test_must_be_empty err
292 test_expect_success
"filter: clean empty file" '
293 test_config filter.in-repo-header.clean "echo cleaned && cat" &&
294 test_config filter.in-repo-header.smudge "sed 1d" &&
296 echo "empty-in-worktree filter=in-repo-header" >>.gitattributes &&
297 >empty-in-worktree &&
299 echo cleaned >expected &&
300 git add empty-in-worktree &&
301 git show :empty-in-worktree >actual &&
302 test_cmp expected actual
305 test_expect_success
"filter: smudge empty file" '
306 test_config filter.empty-in-repo.clean "cat >/dev/null" &&
307 test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
309 echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
310 echo dead data walking >empty-in-repo &&
311 git add empty-in-repo &&
313 echo smudged >expected &&
314 git checkout-index --prefix=filtered- empty-in-repo &&
315 test_cmp expected filtered-empty-in-repo
318 test_expect_success
'disable filter with empty override' '
319 test_config_global filter.disable.smudge false &&
320 test_config_global filter.disable.clean false &&
321 test_config filter.disable.smudge false &&
322 test_config filter.disable.clean false &&
324 echo "*.disable filter=disable" >.gitattributes &&
326 echo test >test.disable &&
327 git -c filter.disable.clean= add test.disable 2>err &&
328 test_must_be_empty err &&
329 rm -f test.disable &&
330 git -c filter.disable.smudge= checkout -- test.disable 2>err &&
331 test_must_be_empty err
334 test_expect_success
'diff does not reuse worktree files that need cleaning' '
335 test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
336 echo "file filter=counter" >.gitattributes &&
337 test_commit one file &&
338 test_commit two file &&
341 git diff-tree -p HEAD &&
342 test_line_count = 0 count
345 test_expect_success PERL
'required process filter should filter data' '
346 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
347 test_config_global filter.protocol.required true &&
354 echo "git-stderr.log" >.gitignore &&
355 echo "*.r filter=protocol" >.gitattributes &&
357 git commit . -m "test commit 1" &&
358 git branch empty-branch &&
360 cp "$TEST_ROOT/test.o" test.r &&
361 cp "$TEST_ROOT/test2.o" test2.r &&
363 cp "$TEST_ROOT/test3 '\''sq'\'',\$x.o" "testsubdir/test3 '\''sq'\'',\$x.r" &&
366 S=$(file_size test.r) &&
367 S2=$(file_size test2.r) &&
368 S3=$(file_size "testsubdir/test3 '\''sq'\'',\$x.r") &&
371 cat >expected.log <<-EOF &&
373 init handshake complete
374 IN: clean test.r $S [OK] -- OUT: $S . [OK]
375 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
376 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
377 IN: clean testsubdir/test3 '\''sq'\'',\$x.r $S3 [OK] -- OUT: $S3 . [OK]
380 test_cmp_count expected.log rot13-filter.log &&
382 filter_git commit . -m "test commit 2" &&
383 cat >expected.log <<-EOF &&
385 init handshake complete
386 IN: clean test.r $S [OK] -- OUT: $S . [OK]
387 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
388 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
389 IN: clean testsubdir/test3 '\''sq'\'',\$x.r $S3 [OK] -- OUT: $S3 . [OK]
390 IN: clean test.r $S [OK] -- OUT: $S . [OK]
391 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
392 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
393 IN: clean testsubdir/test3 '\''sq'\'',\$x.r $S3 [OK] -- OUT: $S3 . [OK]
396 test_cmp_count expected.log rot13-filter.log &&
398 rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x.r" &&
400 filter_git checkout --quiet --no-progress . &&
401 cat >expected.log <<-EOF &&
403 init handshake complete
404 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
405 IN: smudge testsubdir/test3 '\''sq'\'',\$x.r $S3 [OK] -- OUT: $S3 . [OK]
408 test_cmp_exclude_clean expected.log rot13-filter.log &&
410 filter_git checkout --quiet --no-progress empty-branch &&
411 cat >expected.log <<-EOF &&
413 init handshake complete
414 IN: clean test.r $S [OK] -- OUT: $S . [OK]
417 test_cmp_exclude_clean expected.log rot13-filter.log &&
419 filter_git checkout --quiet --no-progress master &&
420 cat >expected.log <<-EOF &&
422 init handshake complete
423 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
424 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
425 IN: smudge test4-empty.r 0 [OK] -- OUT: 0 [OK]
426 IN: smudge testsubdir/test3 '\''sq'\'',\$x.r $S3 [OK] -- OUT: $S3 . [OK]
429 test_cmp_exclude_clean expected.log rot13-filter.log &&
431 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
432 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
433 test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x.o" "testsubdir/test3 '\''sq'\'',\$x.r"
437 test_expect_success PERL
'required process filter takes precedence' '
438 test_config_global filter.protocol.clean false &&
439 test_config_global filter.protocol.process "rot13-filter.pl clean" &&
440 test_config_global filter.protocol.required true &&
447 echo "*.r filter=protocol" >.gitattributes &&
448 cp "$TEST_ROOT/test.o" test.r &&
449 S=$(file_size test.r) &&
451 # Check that the process filter is invoked here
453 cat >expected.log <<-EOF &&
455 init handshake complete
456 IN: clean test.r $S [OK] -- OUT: $S . [OK]
459 test_cmp_count expected.log rot13-filter.log
463 test_expect_success PERL
'required process filter should be used only for "clean" operation only' '
464 test_config_global filter.protocol.process "rot13-filter.pl clean" &&
471 echo "*.r filter=protocol" >.gitattributes &&
472 cp "$TEST_ROOT/test.o" test.r &&
473 S=$(file_size test.r) &&
476 cat >expected.log <<-EOF &&
478 init handshake complete
479 IN: clean test.r $S [OK] -- OUT: $S . [OK]
482 test_cmp_count expected.log rot13-filter.log &&
486 filter_git checkout --quiet --no-progress . &&
487 # If the filter would be used for "smudge", too, we would see
488 # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
489 cat >expected.log <<-EOF &&
491 init handshake complete
494 test_cmp_exclude_clean expected.log rot13-filter.log
498 test_expect_success PERL
'required process filter should process multiple packets' '
499 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
500 test_config_global filter.protocol.required true &&
508 # Generate data requiring 1, 2, 3 packets
509 S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
510 generate_random_characters $(($S )) 1pkt_1__.file &&
511 generate_random_characters $(($S +1)) 2pkt_1+1.file &&
512 generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
513 generate_random_characters $(($S*2 )) 2pkt_2__.file &&
514 generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
516 for FILE in "$TEST_ROOT"/*.file
519 rot13.sh <"$FILE" >"$FILE.rot13"
522 echo "*.file filter=protocol" >.gitattributes &&
523 filter_git add *.file .gitattributes &&
524 cat >expected.log <<-EOF &&
526 init handshake complete
527 IN: clean 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
528 IN: clean 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
529 IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
530 IN: clean 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
531 IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
534 test_cmp_count expected.log rot13-filter.log &&
538 filter_git checkout --quiet --no-progress -- *.file &&
539 cat >expected.log <<-EOF &&
541 init handshake complete
542 IN: smudge 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
543 IN: smudge 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
544 IN: smudge 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
545 IN: smudge 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
546 IN: smudge 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
549 test_cmp_exclude_clean expected.log rot13-filter.log &&
553 test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE
558 test_expect_success PERL
'required process filter with clean error should fail' '
559 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
560 test_config_global filter.protocol.required true &&
567 echo "*.r filter=protocol" >.gitattributes &&
569 cp "$TEST_ROOT/test.o" test.r &&
570 echo "this is going to fail" >clean-write-fail.r &&
571 echo "content-test3-subdir" >test3.r &&
573 test_must_fail git add .
577 test_expect_success PERL
'process filter should restart after unexpected write failure' '
578 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
585 echo "*.r filter=protocol" >.gitattributes &&
587 cp "$TEST_ROOT/test.o" test.r &&
588 cp "$TEST_ROOT/test2.o" test2.r &&
589 echo "this is going to fail" >smudge-write-fail.o &&
590 cp smudge-write-fail.o smudge-write-fail.r &&
592 S=$(file_size test.r) &&
593 S2=$(file_size test2.r) &&
594 SF=$(file_size smudge-write-fail.r) &&
599 rm -f rot13-filter.log &&
600 git checkout --quiet --no-progress . 2>git-stderr.log &&
602 grep "smudge write error at" git-stderr.log &&
603 grep "error: external filter" git-stderr.log &&
605 cat >expected.log <<-EOF &&
607 init handshake complete
608 IN: smudge smudge-write-fail.r $SF [OK] -- OUT: $SF [WRITE FAIL]
610 init handshake complete
611 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
612 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
615 test_cmp_exclude_clean expected.log rot13-filter.log &&
617 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
618 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
621 ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
622 rot13.sh <smudge-write-fail.o >expected &&
623 git cat-file blob :smudge-write-fail.r >actual &&
624 test_cmp expected actual
628 test_expect_success PERL
'process filter should not be restarted if it signals an error' '
629 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
636 echo "*.r filter=protocol" >.gitattributes &&
638 cp "$TEST_ROOT/test.o" test.r &&
639 cp "$TEST_ROOT/test2.o" test2.r &&
640 echo "this will cause an error" >error.o &&
641 cp error.o error.r &&
643 S=$(file_size test.r) &&
644 S2=$(file_size test2.r) &&
645 SE=$(file_size error.r) &&
650 filter_git checkout --quiet --no-progress . &&
651 cat >expected.log <<-EOF &&
653 init handshake complete
654 IN: smudge error.r $SE [OK] -- OUT: 0 [ERROR]
655 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
656 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
659 test_cmp_exclude_clean expected.log rot13-filter.log &&
661 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
662 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
663 test_cmp error.o error.r
667 test_expect_success PERL
'process filter abort stops processing of all further files' '
668 test_config_global filter.protocol.process "rot13-filter.pl clean smudge" &&
675 echo "*.r filter=protocol" >.gitattributes &&
677 cp "$TEST_ROOT/test.o" test.r &&
678 cp "$TEST_ROOT/test2.o" test2.r &&
679 echo "error this blob and all future blobs" >abort.o &&
680 cp abort.o abort.r &&
682 SA=$(file_size abort.r) &&
687 # Note: This test assumes that Git filters files in alphabetical
688 # order ("abort.r" before "test.r").
689 filter_git checkout --quiet --no-progress . &&
690 cat >expected.log <<-EOF &&
692 init handshake complete
693 IN: smudge abort.r $SA [OK] -- OUT: 0 [ABORT]
696 test_cmp_exclude_clean expected.log rot13-filter.log &&
698 test_cmp "$TEST_ROOT/test.o" test.r &&
699 test_cmp "$TEST_ROOT/test2.o" test2.r &&
700 test_cmp abort.o abort.r
704 test_expect_success PERL
'invalid process filter must fail (and not hang!)' '
705 test_config_global filter.protocol.process cat &&
706 test_config_global filter.protocol.required true &&
713 echo "*.r filter=protocol" >.gitattributes &&
715 cp "$TEST_ROOT/test.o" test.r &&
716 test_must_fail git add . 2>git-stderr.log &&
717 grep "does not support filter protocol version" git-stderr.log