3 test_description='blob conversion via gitattributes'
9 cat <<EOF >"$TEST_ROOT/rot13.sh"
12 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
13 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
15 chmod +x "$TEST_ROOT/rot13.sh"
17 generate_random_characters () {
20 test-genrandom some-seed $LEN |
21 perl -pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
25 perl -e 'print -s $ARGV[0]' "$1"
29 rm -f rot13-filter.log &&
33 # Compare two files and ensure that `clean` and `smudge` respectively are
34 # called at least once if specified in the `expect` file. The actual
35 # invocation count is not relevant because their number can vary.
36 # c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
40 for FILE in "$expect" "$actual"
42 sort "$FILE" | uniq -c |
43 sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp" &&
44 mv "$FILE.tmp" "$FILE" || return
46 test_cmp "$expect" "$actual"
49 # Compare two files but exclude all `clean` invocations because Git can
50 # call `clean` zero or more times.
51 # c.f. http://public-inbox.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
52 test_cmp_exclude_clean () {
55 for FILE in "$expect" "$actual"
57 grep -v "IN: clean" "$FILE" >"$FILE.tmp" &&
58 mv "$FILE.tmp" "$FILE"
60 test_cmp "$expect" "$actual"
63 # Check that the contents of two files are equal and that their rot13 version
64 # is equal to the committed content.
65 test_cmp_committed_rot13 () {
67 "$TEST_ROOT/rot13.sh" <"$1" >expected &&
68 git cat-file blob :"$2" >actual &&
69 test_cmp expected actual
72 test_expect_success setup '
73 git config filter.rot13.smudge ./rot13.sh &&
74 git config filter.rot13.clean ./rot13.sh &&
77 echo "*.t filter=rot13"
82 echo a b c d e f g h i j k l m
83 echo n o p q r s t u v w x y z
89 git add test test.t test.i &&
90 rm -f test test.t test.i &&
91 git checkout -- test test.t test.i &&
93 echo "content-test2" >test2.o &&
94 echo "content-test3 - filename with special characters" >"test3 '\''sq'\'',\$x=.o"
97 script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
99 test_expect_success check '
101 test_cmp test.o test &&
102 test_cmp test.o test.t &&
104 # ident should be stripped in the repository
105 git diff --raw --exit-code :test :test.i &&
106 id=$(git rev-parse --verify :test) &&
107 embedded=$(sed -ne "$script" test.i) &&
108 test "z$id" = "z$embedded" &&
110 git cat-file blob :test.t >test.r &&
112 ./rot13.sh <test.o >test.t &&
113 test_cmp test.r test.t
116 # If an expanded ident ever gets into the repository, we want to make sure that
117 # it is collapsed before being expanded again on checkout
118 test_expect_success expanded_in_repo '
120 echo "File with expanded keywords"
123 echo "\$Id: 0000000000000000000000000000000000000000 \$"
124 echo "\$Id: NoSpaceAtEnd\$"
125 echo "\$Id:NoSpaceAtFront \$"
126 echo "\$Id:NoSpaceAtEitherEnd\$"
127 echo "\$Id: NoTerminatingSymbol"
128 echo "\$Id: Foreign Commit With Spaces \$"
129 } >expanded-keywords.0 &&
132 cat expanded-keywords.0 &&
133 printf "\$Id: NoTerminatingSymbolAtEOF"
134 } >expanded-keywords &&
135 cat expanded-keywords >expanded-keywords-crlf &&
136 git add expanded-keywords expanded-keywords-crlf &&
137 git commit -m "File with keywords expanded" &&
138 id=$(git rev-parse --verify :expanded-keywords) &&
141 echo "File with expanded keywords"
148 echo "\$Id: NoTerminatingSymbol"
149 echo "\$Id: Foreign Commit With Spaces \$"
150 } >expected-output.0 &&
152 cat expected-output.0 &&
153 printf "\$Id: NoTerminatingSymbolAtEOF"
154 } >expected-output &&
156 append_cr <expected-output.0 &&
157 printf "\$Id: NoTerminatingSymbolAtEOF"
158 } >expected-output-crlf &&
160 echo "expanded-keywords ident"
161 echo "expanded-keywords-crlf ident text eol=crlf"
162 } >>.gitattributes &&
164 rm -f expanded-keywords expanded-keywords-crlf &&
166 git checkout -- expanded-keywords &&
167 test_cmp expanded-keywords expected-output &&
169 git checkout -- expanded-keywords-crlf &&
170 test_cmp expanded-keywords-crlf expected-output-crlf
173 # The use of %f in a filter definition is expanded to the path to
174 # the filename being smudged or cleaned. It must be shell escaped.
175 # First, set up some interesting file names and pet them in
177 test_expect_success 'filter shell-escaped filenames' '
178 cat >argc.sh <<-EOF &&
183 normal=name-no-magic &&
184 special="name with '\''sq'\'' and \$x" &&
185 echo some test text >"$normal" &&
186 echo some test text >"$special" &&
187 git add "$normal" "$special" &&
188 git commit -q -m "add files" &&
189 echo "name* filter=argc" >.gitattributes &&
191 # delete the files and check them out again, using a smudge filter
192 # that will count the args and echo the command-line back to us
193 test_config filter.argc.smudge "sh ./argc.sh %f" &&
194 rm "$normal" "$special" &&
195 git checkout -- "$normal" "$special" &&
197 # make sure argc.sh counted the right number of args
198 echo "argc: 1 $normal" >expect &&
199 test_cmp expect "$normal" &&
200 echo "argc: 1 $special" >expect &&
201 test_cmp expect "$special" &&
203 # do the same thing, but with more args in the filter expression
204 test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" &&
205 rm "$normal" "$special" &&
206 git checkout -- "$normal" "$special" &&
208 # make sure argc.sh counted the right number of args
209 echo "argc: 2 $normal --my-extra-arg" >expect &&
210 test_cmp expect "$normal" &&
211 echo "argc: 2 $special --my-extra-arg" >expect &&
212 test_cmp expect "$special" &&
216 test_expect_success 'required filter should filter data' '
217 test_config filter.required.smudge ./rot13.sh &&
218 test_config filter.required.clean ./rot13.sh &&
219 test_config filter.required.required true &&
221 echo "*.r filter=required" >.gitattributes &&
223 cat test.o >test.r &&
227 git checkout -- test.r &&
228 test_cmp test.o test.r &&
230 ./rot13.sh <test.o >expected &&
231 git cat-file blob :test.r >actual &&
232 test_cmp expected actual
235 test_expect_success 'required filter smudge failure' '
236 test_config filter.failsmudge.smudge false &&
237 test_config filter.failsmudge.clean cat &&
238 test_config filter.failsmudge.required true &&
240 echo "*.fs filter=failsmudge" >.gitattributes &&
242 echo test >test.fs &&
245 test_must_fail git checkout -- test.fs
248 test_expect_success 'required filter clean failure' '
249 test_config filter.failclean.smudge cat &&
250 test_config filter.failclean.clean false &&
251 test_config filter.failclean.required true &&
253 echo "*.fc filter=failclean" >.gitattributes &&
255 echo test >test.fc &&
256 test_must_fail git add test.fc
259 test_expect_success 'filtering large input to small output should use little memory' '
260 test_config filter.devnull.clean "cat >/dev/null" &&
261 test_config filter.devnull.required true &&
262 for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB &&
263 echo "30MB filter=devnull" >.gitattributes &&
264 GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
267 test_expect_success 'filter that does not read is fine' '
268 test-genrandom foo $((128 * 1024 + 1)) >big &&
269 echo "big filter=epipe" >.gitattributes &&
270 test_config filter.epipe.clean "echo xyzzy" &&
272 git cat-file blob :big >actual &&
273 echo xyzzy >expect &&
274 test_cmp expect actual
277 test_expect_success EXPENSIVE 'filter large file' '
278 test_config filter.largefile.smudge cat &&
279 test_config filter.largefile.clean cat &&
280 for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB &&
281 echo "2GB filter=largefile" >.gitattributes &&
283 test_must_be_empty err &&
285 git checkout -- 2GB 2>err &&
286 test_must_be_empty err
289 test_expect_success "filter: clean empty file" '
290 test_config filter.in-repo-header.clean "echo cleaned && cat" &&
291 test_config filter.in-repo-header.smudge "sed 1d" &&
293 echo "empty-in-worktree filter=in-repo-header" >>.gitattributes &&
294 >empty-in-worktree &&
296 echo cleaned >expected &&
297 git add empty-in-worktree &&
298 git show :empty-in-worktree >actual &&
299 test_cmp expected actual
302 test_expect_success "filter: smudge empty file" '
303 test_config filter.empty-in-repo.clean "cat >/dev/null" &&
304 test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
306 echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
307 echo dead data walking >empty-in-repo &&
308 git add empty-in-repo &&
310 echo smudged >expected &&
311 git checkout-index --prefix=filtered- empty-in-repo &&
312 test_cmp expected filtered-empty-in-repo
315 test_expect_success 'disable filter with empty override' '
316 test_config_global filter.disable.smudge false &&
317 test_config_global filter.disable.clean false &&
318 test_config filter.disable.smudge false &&
319 test_config filter.disable.clean false &&
321 echo "*.disable filter=disable" >.gitattributes &&
323 echo test >test.disable &&
324 git -c filter.disable.clean= add test.disable 2>err &&
325 test_must_be_empty err &&
326 rm -f test.disable &&
327 git -c filter.disable.smudge= checkout -- test.disable 2>err &&
328 test_must_be_empty err
331 test_expect_success 'diff does not reuse worktree files that need cleaning' '
332 test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
333 echo "file filter=counter" >.gitattributes &&
334 test_commit one file &&
335 test_commit two file &&
338 git diff-tree -p HEAD &&
339 test_line_count = 0 count
342 test_expect_success PERL 'required process filter should filter data' '
343 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
344 test_config_global filter.protocol.required true &&
351 echo "git-stderr.log" >.gitignore &&
352 echo "*.r filter=protocol" >.gitattributes &&
354 git commit . -m "test commit 1" &&
355 git branch empty-branch &&
357 cp "$TEST_ROOT/test.o" test.r &&
358 cp "$TEST_ROOT/test2.o" test2.r &&
360 cp "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r" &&
363 S=$(file_size test.r) &&
364 S2=$(file_size test2.r) &&
365 S3=$(file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
368 cat >expected.log <<-EOF &&
370 init handshake complete
371 IN: clean test.r $S [OK] -- OUT: $S . [OK]
372 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
373 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
374 IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
377 test_cmp_count expected.log rot13-filter.log &&
379 filter_git commit . -m "test commit 2" &&
380 cat >expected.log <<-EOF &&
382 init handshake complete
383 IN: clean test.r $S [OK] -- OUT: $S . [OK]
384 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
385 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
386 IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
387 IN: clean test.r $S [OK] -- OUT: $S . [OK]
388 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
389 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
390 IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
393 test_cmp_count expected.log rot13-filter.log &&
395 rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
397 filter_git checkout --quiet --no-progress . &&
398 cat >expected.log <<-EOF &&
400 init handshake complete
401 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
402 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
405 test_cmp_exclude_clean expected.log rot13-filter.log &&
407 filter_git checkout --quiet --no-progress empty-branch &&
408 cat >expected.log <<-EOF &&
410 init handshake complete
411 IN: clean test.r $S [OK] -- OUT: $S . [OK]
414 test_cmp_exclude_clean expected.log rot13-filter.log &&
416 filter_git checkout --quiet --no-progress master &&
417 cat >expected.log <<-EOF &&
419 init handshake complete
420 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
421 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
422 IN: smudge test4-empty.r 0 [OK] -- OUT: 0 [OK]
423 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
426 test_cmp_exclude_clean expected.log rot13-filter.log &&
428 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
429 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
430 test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r"
434 test_expect_success PERL 'required process filter takes precedence' '
435 test_config_global filter.protocol.clean false &&
436 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean" &&
437 test_config_global filter.protocol.required true &&
444 echo "*.r filter=protocol" >.gitattributes &&
445 cp "$TEST_ROOT/test.o" test.r &&
446 S=$(file_size test.r) &&
448 # Check that the process filter is invoked here
450 cat >expected.log <<-EOF &&
452 init handshake complete
453 IN: clean test.r $S [OK] -- OUT: $S . [OK]
456 test_cmp_count expected.log rot13-filter.log
460 test_expect_success PERL 'required process filter should be used only for "clean" operation only' '
461 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean" &&
468 echo "*.r filter=protocol" >.gitattributes &&
469 cp "$TEST_ROOT/test.o" test.r &&
470 S=$(file_size test.r) &&
473 cat >expected.log <<-EOF &&
475 init handshake complete
476 IN: clean test.r $S [OK] -- OUT: $S . [OK]
479 test_cmp_count expected.log rot13-filter.log &&
483 filter_git checkout --quiet --no-progress . &&
484 # If the filter would be used for "smudge", too, we would see
485 # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
486 cat >expected.log <<-EOF &&
488 init handshake complete
491 test_cmp_exclude_clean expected.log rot13-filter.log
495 test_expect_success PERL 'required process filter should process multiple packets' '
496 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
497 test_config_global filter.protocol.required true &&
505 # Generate data requiring 1, 2, 3 packets
506 S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
507 generate_random_characters $(($S )) 1pkt_1__.file &&
508 generate_random_characters $(($S +1)) 2pkt_1+1.file &&
509 generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
510 generate_random_characters $(($S*2 )) 2pkt_2__.file &&
511 generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
513 for FILE in "$TEST_ROOT"/*.file
516 "$TEST_ROOT/rot13.sh" <"$FILE" >"$FILE.rot13"
519 echo "*.file filter=protocol" >.gitattributes &&
520 filter_git add *.file .gitattributes &&
521 cat >expected.log <<-EOF &&
523 init handshake complete
524 IN: clean 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
525 IN: clean 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
526 IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
527 IN: clean 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
528 IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
531 test_cmp_count expected.log rot13-filter.log &&
535 filter_git checkout --quiet --no-progress -- *.file &&
536 cat >expected.log <<-EOF &&
538 init handshake complete
539 IN: smudge 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
540 IN: smudge 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
541 IN: smudge 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
542 IN: smudge 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
543 IN: smudge 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
546 test_cmp_exclude_clean expected.log rot13-filter.log &&
550 test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE
555 test_expect_success PERL 'required process filter with clean error should fail' '
556 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
557 test_config_global filter.protocol.required true &&
564 echo "*.r filter=protocol" >.gitattributes &&
566 cp "$TEST_ROOT/test.o" test.r &&
567 echo "this is going to fail" >clean-write-fail.r &&
568 echo "content-test3-subdir" >test3.r &&
570 test_must_fail git add .
574 test_expect_success PERL 'process filter should restart after unexpected write failure' '
575 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
582 echo "*.r filter=protocol" >.gitattributes &&
584 cp "$TEST_ROOT/test.o" test.r &&
585 cp "$TEST_ROOT/test2.o" test2.r &&
586 echo "this is going to fail" >smudge-write-fail.o &&
587 cp smudge-write-fail.o smudge-write-fail.r &&
589 S=$(file_size test.r) &&
590 S2=$(file_size test2.r) &&
591 SF=$(file_size smudge-write-fail.r) &&
596 rm -f rot13-filter.log &&
597 git checkout --quiet --no-progress . 2>git-stderr.log &&
599 grep "smudge write error at" git-stderr.log &&
600 grep "error: external filter" git-stderr.log &&
602 cat >expected.log <<-EOF &&
604 init handshake complete
605 IN: smudge smudge-write-fail.r $SF [OK] -- OUT: $SF [WRITE FAIL]
607 init handshake complete
608 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
609 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
612 test_cmp_exclude_clean expected.log rot13-filter.log &&
614 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
615 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
618 ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
619 "$TEST_ROOT/rot13.sh" <smudge-write-fail.o >expected &&
620 git cat-file blob :smudge-write-fail.r >actual &&
621 test_cmp expected actual
625 test_expect_success PERL 'process filter should not be restarted if it signals an error' '
626 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
633 echo "*.r filter=protocol" >.gitattributes &&
635 cp "$TEST_ROOT/test.o" test.r &&
636 cp "$TEST_ROOT/test2.o" test2.r &&
637 echo "this will cause an error" >error.o &&
638 cp error.o error.r &&
640 S=$(file_size test.r) &&
641 S2=$(file_size test2.r) &&
642 SE=$(file_size error.r) &&
647 filter_git checkout --quiet --no-progress . &&
648 cat >expected.log <<-EOF &&
650 init handshake complete
651 IN: smudge error.r $SE [OK] -- OUT: 0 [ERROR]
652 IN: smudge test.r $S [OK] -- OUT: $S . [OK]
653 IN: smudge test2.r $S2 [OK] -- OUT: $S2 . [OK]
656 test_cmp_exclude_clean expected.log rot13-filter.log &&
658 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
659 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
660 test_cmp error.o error.r
664 test_expect_success PERL 'process filter abort stops processing of all further files' '
665 test_config_global filter.protocol.process "$TEST_DIRECTORY/t0021/rot13-filter.pl clean smudge" &&
672 echo "*.r filter=protocol" >.gitattributes &&
674 cp "$TEST_ROOT/test.o" test.r &&
675 cp "$TEST_ROOT/test2.o" test2.r &&
676 echo "error this blob and all future blobs" >abort.o &&
677 cp abort.o abort.r &&
679 SA=$(file_size abort.r) &&
684 # Note: This test assumes that Git filters files in alphabetical
685 # order ("abort.r" before "test.r").
686 filter_git checkout --quiet --no-progress . &&
687 cat >expected.log <<-EOF &&
689 init handshake complete
690 IN: smudge abort.r $SA [OK] -- OUT: 0 [ABORT]
693 test_cmp_exclude_clean expected.log rot13-filter.log &&
695 test_cmp "$TEST_ROOT/test.o" test.r &&
696 test_cmp "$TEST_ROOT/test2.o" test2.r &&
697 test_cmp abort.o abort.r
701 test_expect_success PERL 'invalid process filter must fail (and not hang!)' '
702 test_config_global filter.protocol.process cat &&
703 test_config_global filter.protocol.required true &&
710 echo "*.r filter=protocol" >.gitattributes &&
712 cp "$TEST_ROOT/test.o" test.r &&
713 test_must_fail git add . 2>git-stderr.log &&
714 grep "does not support filter protocol version" git-stderr.log