The eleventh batch
[git/gitster.git] / t / t0021-conversion.sh
blob9e6c6ee0d44c0353dbf7dc10fe9c24a4421693a3
1 #!/bin/sh
3 test_description='blob conversion via gitattributes'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-terminal.sh
12 PATH=$PWD:$PATH
13 TEST_ROOT="$(pwd)"
15 write_script <<\EOF "$TEST_ROOT/rot13.sh"
16 tr \
17 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
18 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
19 EOF
21 generate_random_characters () {
22 LEN=$1
23 NAME=$2
24 test-tool genrandom some-seed $LEN |
25 perl -pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
28 filter_git () {
29 rm -f *.log &&
30 git "$@"
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. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
37 test_cmp_count () {
38 expect=$1
39 actual=$2
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 done &&
45 test_cmp "$expect.tmp" "$actual.tmp" &&
46 rm "$expect.tmp" "$actual.tmp"
49 # Compare two files but exclude all `clean` invocations because Git can
50 # call `clean` zero or more times.
51 # c.f. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
52 test_cmp_exclude_clean () {
53 expect=$1
54 actual=$2
55 for FILE in "$expect" "$actual"
57 grep -v "IN: clean" "$FILE" >"$FILE.tmp"
58 done &&
59 test_cmp "$expect.tmp" "$actual.tmp" &&
60 rm "$expect.tmp" "$actual.tmp"
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 () {
66 test_cmp "$1" "$2" &&
67 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" &&
78 echo "*.i ident"
79 } >.gitattributes &&
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 &&
84 echo '\''$Id$'\''
85 } >test &&
86 cat test >test.t &&
87 cat test >test.o &&
88 cat test >test.i &&
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 '
119 cat >expanded-keywords.0 <<-\EOF &&
120 File with expanded keywords
121 $Id$
122 $Id:$
123 $Id: 0000000000000000000000000000000000000000 $
124 $Id: NoSpaceAtEnd$
125 $Id:NoSpaceAtFront $
126 $Id:NoSpaceAtEitherEnd$
127 $Id: NoTerminatingSymbol
128 $Id: Foreign Commit With Spaces $
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) &&
140 cat >expected-output.0 <<-EOF &&
141 File with expanded keywords
142 \$Id: $id \$
143 \$Id: $id \$
144 \$Id: $id \$
145 \$Id: $id \$
146 \$Id: $id \$
147 \$Id: $id \$
148 \$Id: NoTerminatingSymbol
149 \$Id: Foreign Commit With Spaces \$
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 expected-output expanded-keywords &&
169 git checkout -- expanded-keywords-crlf &&
170 test_cmp expected-output-crlf expanded-keywords-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
176 # .gitattributes.
177 test_expect_success 'filter shell-escaped filenames' '
178 cat >argc.sh <<-EOF &&
179 #!$SHELL_PATH
180 cat >/dev/null
181 echo argc: \$# "\$@"
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 &&
224 git add test.r &&
226 rm -f 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 &&
243 git add test.fs &&
244 rm -f 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 'required filter with absent clean field' '
260 test_config filter.absentclean.smudge cat &&
261 test_config filter.absentclean.required true &&
263 echo "*.ac filter=absentclean" >.gitattributes &&
265 echo test >test.ac &&
266 test_must_fail git add test.ac 2>stderr &&
267 test_grep "fatal: test.ac: clean filter .absentclean. failed" stderr
270 test_expect_success 'required filter with absent smudge field' '
271 test_config filter.absentsmudge.clean cat &&
272 test_config filter.absentsmudge.required true &&
274 echo "*.as filter=absentsmudge" >.gitattributes &&
276 echo test >test.as &&
277 git add test.as &&
278 rm -f test.as &&
279 test_must_fail git checkout -- test.as 2>stderr &&
280 test_grep "fatal: test.as: smudge filter absentsmudge failed" stderr
283 test_expect_success 'filtering large input to small output should use little memory' '
284 test_config filter.devnull.clean "cat >/dev/null" &&
285 test_config filter.devnull.required true &&
286 for i in $(test_seq 1 30); do printf "%1048576d" 1 || return 1; done >30MB &&
287 echo "30MB filter=devnull" >.gitattributes &&
288 GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
291 test_expect_success 'filter that does not read is fine' '
292 test-tool genrandom foo $((128 * 1024 + 1)) >big &&
293 echo "big filter=epipe" >.gitattributes &&
294 test_config filter.epipe.clean "echo xyzzy" &&
295 git add big &&
296 git cat-file blob :big >actual &&
297 echo xyzzy >expect &&
298 test_cmp expect actual
301 test_expect_success EXPENSIVE 'filter large file' '
302 test_config filter.largefile.smudge cat &&
303 test_config filter.largefile.clean cat &&
304 for i in $(test_seq 1 2048); do printf "%1048576d" 1 || return 1; done >2GB &&
305 echo "2GB filter=largefile" >.gitattributes &&
306 git add 2GB 2>err &&
307 test_must_be_empty err &&
308 rm -f 2GB &&
309 git checkout -- 2GB 2>err &&
310 test_must_be_empty err
313 test_expect_success "filter: clean empty file" '
314 test_config filter.in-repo-header.clean "echo cleaned && cat" &&
315 test_config filter.in-repo-header.smudge "sed 1d" &&
317 echo "empty-in-worktree filter=in-repo-header" >>.gitattributes &&
318 >empty-in-worktree &&
320 echo cleaned >expected &&
321 git add empty-in-worktree &&
322 git show :empty-in-worktree >actual &&
323 test_cmp expected actual
326 test_expect_success "filter: smudge empty file" '
327 test_config filter.empty-in-repo.clean "cat >/dev/null" &&
328 test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
330 echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
331 echo dead data walking >empty-in-repo &&
332 git add empty-in-repo &&
334 echo smudged >expected &&
335 git checkout-index --prefix=filtered- empty-in-repo &&
336 test_cmp expected filtered-empty-in-repo
339 test_expect_success 'disable filter with empty override' '
340 test_config_global filter.disable.smudge false &&
341 test_config_global filter.disable.clean false &&
342 test_config filter.disable.smudge false &&
343 test_config filter.disable.clean false &&
345 echo "*.disable filter=disable" >.gitattributes &&
347 echo test >test.disable &&
348 git -c filter.disable.clean= add test.disable 2>err &&
349 test_must_be_empty err &&
350 rm -f test.disable &&
351 git -c filter.disable.smudge= checkout -- test.disable 2>err &&
352 test_must_be_empty err
355 test_expect_success 'diff does not reuse worktree files that need cleaning' '
356 test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
357 echo "file filter=counter" >.gitattributes &&
358 test_commit one file &&
359 test_commit two file &&
361 >count &&
362 git diff-tree -p HEAD &&
363 test_line_count = 0 count
366 test_expect_success 'required process filter should filter data' '
367 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
368 test_config_global filter.protocol.required true &&
369 rm -rf repo &&
370 mkdir repo &&
372 cd repo &&
373 git init &&
375 echo "*.r filter=protocol" >.gitattributes &&
376 git add . &&
377 git commit -m "test commit 1" &&
378 git branch empty-branch &&
380 cp "$TEST_ROOT/test.o" test.r &&
381 cp "$TEST_ROOT/test2.o" test2.r &&
382 mkdir testsubdir &&
383 cp "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r" &&
384 >test4-empty.r &&
386 S=$(test_file_size test.r) &&
387 S2=$(test_file_size test2.r) &&
388 S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
389 M=$(git hash-object test.r) &&
390 M2=$(git hash-object test2.r) &&
391 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
392 EMPTY=$(git hash-object /dev/null) &&
394 filter_git add . &&
395 cat >expected.log <<-EOF &&
396 START
397 init handshake complete
398 IN: clean test.r $S [OK] -- OUT: $S . [OK]
399 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
400 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
401 IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
402 STOP
404 test_cmp_count expected.log debug.log &&
406 git commit -m "test commit 2" &&
407 MAIN=$(git rev-parse --verify main) &&
408 META="ref=refs/heads/main treeish=$MAIN" &&
409 rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
411 filter_git checkout --quiet --no-progress . &&
412 cat >expected.log <<-EOF &&
413 START
414 init handshake complete
415 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
416 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
417 STOP
419 test_cmp_exclude_clean expected.log debug.log &&
421 # Make sure that the file appears dirty, so checkout below has to
422 # run the configured filter.
423 touch test.r &&
424 filter_git checkout --quiet --no-progress empty-branch &&
425 cat >expected.log <<-EOF &&
426 START
427 init handshake complete
428 IN: clean test.r $S [OK] -- OUT: $S . [OK]
429 STOP
431 test_cmp_exclude_clean expected.log debug.log &&
433 filter_git checkout --quiet --no-progress main &&
434 cat >expected.log <<-EOF &&
435 START
436 init handshake complete
437 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
438 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
439 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
440 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
441 STOP
443 test_cmp_exclude_clean expected.log debug.log &&
445 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
446 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
447 test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r"
451 test_expect_success 'required process filter should filter data for various subcommands' '
452 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
453 test_config_global filter.protocol.required true &&
455 cd repo &&
457 S=$(test_file_size test.r) &&
458 S2=$(test_file_size test2.r) &&
459 S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
460 M=$(git hash-object test.r) &&
461 M2=$(git hash-object test2.r) &&
462 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
463 EMPTY=$(git hash-object /dev/null) &&
465 MAIN=$(git rev-parse --verify main) &&
467 cp "$TEST_ROOT/test.o" test5.r &&
468 git add test5.r &&
469 git commit -m "test commit 3" &&
470 git checkout empty-branch &&
471 filter_git rebase --onto empty-branch main^^ main &&
472 MAIN2=$(git rev-parse --verify main) &&
473 META="ref=refs/heads/main treeish=$MAIN2" &&
474 cat >expected.log <<-EOF &&
475 START
476 init handshake complete
477 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
478 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
479 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
480 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
481 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
482 STOP
484 test_cmp_exclude_clean expected.log debug.log &&
486 git reset --hard empty-branch &&
487 filter_git reset --hard $MAIN &&
488 META="treeish=$MAIN" &&
489 cat >expected.log <<-EOF &&
490 START
491 init handshake complete
492 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
493 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
494 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
495 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
496 STOP
498 test_cmp_exclude_clean expected.log debug.log &&
500 git branch old-main $MAIN &&
501 git reset --hard empty-branch &&
502 filter_git reset --hard old-main &&
503 META="ref=refs/heads/old-main treeish=$MAIN" &&
504 cat >expected.log <<-EOF &&
505 START
506 init handshake complete
507 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
508 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
509 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
510 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
511 STOP
513 test_cmp_exclude_clean expected.log debug.log &&
515 git checkout -b merge empty-branch &&
516 git branch -f main $MAIN2 &&
517 filter_git merge main &&
518 META="treeish=$MAIN2" &&
519 cat >expected.log <<-EOF &&
520 START
521 init handshake complete
522 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
523 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
524 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
525 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
526 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
527 STOP
529 test_cmp_exclude_clean expected.log debug.log &&
531 filter_git archive main >/dev/null &&
532 META="ref=refs/heads/main treeish=$MAIN2" &&
533 cat >expected.log <<-EOF &&
534 START
535 init handshake complete
536 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
537 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
538 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
539 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
540 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
541 STOP
543 test_cmp_exclude_clean expected.log debug.log &&
545 TREE="$(git rev-parse $MAIN2^{tree})" &&
546 filter_git archive $TREE >/dev/null &&
547 META="treeish=$TREE" &&
548 cat >expected.log <<-EOF &&
549 START
550 init handshake complete
551 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
552 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
553 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
554 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
555 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
556 STOP
558 test_cmp_exclude_clean expected.log debug.log
562 test_expect_success 'required process filter takes precedence' '
563 test_config_global filter.protocol.clean false &&
564 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
565 test_config_global filter.protocol.required true &&
566 rm -rf repo &&
567 mkdir repo &&
569 cd repo &&
570 git init &&
572 echo "*.r filter=protocol" >.gitattributes &&
573 cp "$TEST_ROOT/test.o" test.r &&
574 S=$(test_file_size test.r) &&
576 # Check that the process filter is invoked here
577 filter_git add . &&
578 cat >expected.log <<-EOF &&
579 START
580 init handshake complete
581 IN: clean test.r $S [OK] -- OUT: $S . [OK]
582 STOP
584 test_cmp_count expected.log debug.log
588 test_expect_success 'required process filter should be used only for "clean" operation only' '
589 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
590 rm -rf repo &&
591 mkdir repo &&
593 cd repo &&
594 git init &&
596 echo "*.r filter=protocol" >.gitattributes &&
597 cp "$TEST_ROOT/test.o" test.r &&
598 S=$(test_file_size test.r) &&
600 filter_git add . &&
601 cat >expected.log <<-EOF &&
602 START
603 init handshake complete
604 IN: clean test.r $S [OK] -- OUT: $S . [OK]
605 STOP
607 test_cmp_count expected.log debug.log &&
609 rm test.r &&
611 filter_git checkout --quiet --no-progress . &&
612 # If the filter would be used for "smudge", too, we would see
613 # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
614 cat >expected.log <<-EOF &&
615 START
616 init handshake complete
617 STOP
619 test_cmp_exclude_clean expected.log debug.log
623 test_expect_success 'required process filter should process multiple packets' '
624 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
625 test_config_global filter.protocol.required true &&
627 rm -rf repo &&
628 mkdir repo &&
630 cd repo &&
631 git init &&
633 # Generate data requiring 1, 2, 3 packets
634 S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
635 generate_random_characters $(($S )) 1pkt_1__.file &&
636 generate_random_characters $(($S +1)) 2pkt_1+1.file &&
637 generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
638 generate_random_characters $(($S*2 )) 2pkt_2__.file &&
639 generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
641 for FILE in "$TEST_ROOT"/*.file
643 cp "$FILE" . &&
644 rot13.sh <"$FILE" >"$FILE.rot13" || return 1
645 done &&
647 echo "*.file filter=protocol" >.gitattributes &&
648 filter_git add *.file .gitattributes &&
649 cat >expected.log <<-EOF &&
650 START
651 init handshake complete
652 IN: clean 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
653 IN: clean 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
654 IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
655 IN: clean 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
656 IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
657 STOP
659 test_cmp_count expected.log debug.log &&
661 M1="blob=$(git hash-object 1pkt_1__.file)" &&
662 M2="blob=$(git hash-object 2pkt_1+1.file)" &&
663 M3="blob=$(git hash-object 2pkt_2-1.file)" &&
664 M4="blob=$(git hash-object 2pkt_2__.file)" &&
665 M5="blob=$(git hash-object 3pkt_2+1.file)" &&
666 rm -f *.file debug.log &&
668 filter_git checkout --quiet --no-progress -- *.file &&
669 cat >expected.log <<-EOF &&
670 START
671 init handshake complete
672 IN: smudge 1pkt_1__.file $M1 $(($S )) [OK] -- OUT: $(($S )) . [OK]
673 IN: smudge 2pkt_1+1.file $M2 $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
674 IN: smudge 2pkt_2-1.file $M3 $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
675 IN: smudge 2pkt_2__.file $M4 $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
676 IN: smudge 3pkt_2+1.file $M5 $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
677 STOP
679 test_cmp_exclude_clean expected.log debug.log &&
681 for FILE in *.file
683 test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE || return 1
684 done
688 test_expect_success 'required process filter with clean error should fail' '
689 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
690 test_config_global filter.protocol.required true &&
691 rm -rf repo &&
692 mkdir repo &&
694 cd repo &&
695 git init &&
697 echo "*.r filter=protocol" >.gitattributes &&
699 cp "$TEST_ROOT/test.o" test.r &&
700 echo "this is going to fail" >clean-write-fail.r &&
701 echo "content-test3-subdir" >test3.r &&
703 test_must_fail git add .
707 test_expect_success 'process filter should restart after unexpected write failure' '
708 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
709 rm -rf repo &&
710 mkdir repo &&
712 cd repo &&
713 git init &&
715 echo "*.r filter=protocol" >.gitattributes &&
717 cp "$TEST_ROOT/test.o" test.r &&
718 cp "$TEST_ROOT/test2.o" test2.r &&
719 echo "this is going to fail" >smudge-write-fail.o &&
720 cp smudge-write-fail.o smudge-write-fail.r &&
722 S=$(test_file_size test.r) &&
723 S2=$(test_file_size test2.r) &&
724 SF=$(test_file_size smudge-write-fail.r) &&
725 M=$(git hash-object test.r) &&
726 M2=$(git hash-object test2.r) &&
727 MF=$(git hash-object smudge-write-fail.r) &&
728 rm -f debug.log &&
730 git add . &&
731 rm -f *.r &&
733 rm -f debug.log &&
734 git checkout --quiet --no-progress . 2>git-stderr.log &&
736 grep "smudge write error" git-stderr.log &&
737 test_grep "error: external filter" git-stderr.log &&
739 cat >expected.log <<-EOF &&
740 START
741 init handshake complete
742 IN: smudge smudge-write-fail.r blob=$MF $SF [OK] -- [WRITE FAIL]
743 START
744 init handshake complete
745 IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
746 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
747 STOP
749 test_cmp_exclude_clean expected.log debug.log &&
751 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
752 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
754 # Smudge failed
755 ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
756 rot13.sh <smudge-write-fail.o >expected &&
757 git cat-file blob :smudge-write-fail.r >actual &&
758 test_cmp expected actual
762 test_expect_success 'process filter should not be restarted if it signals an error' '
763 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
764 rm -rf repo &&
765 mkdir repo &&
767 cd repo &&
768 git init &&
770 echo "*.r filter=protocol" >.gitattributes &&
772 cp "$TEST_ROOT/test.o" test.r &&
773 cp "$TEST_ROOT/test2.o" test2.r &&
774 echo "this will cause an error" >error.o &&
775 cp error.o error.r &&
777 S=$(test_file_size test.r) &&
778 S2=$(test_file_size test2.r) &&
779 SE=$(test_file_size error.r) &&
780 M=$(git hash-object test.r) &&
781 M2=$(git hash-object test2.r) &&
782 ME=$(git hash-object error.r) &&
783 rm -f debug.log &&
785 git add . &&
786 rm -f *.r &&
788 filter_git checkout --quiet --no-progress . &&
789 cat >expected.log <<-EOF &&
790 START
791 init handshake complete
792 IN: smudge error.r blob=$ME $SE [OK] -- [ERROR]
793 IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
794 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
795 STOP
797 test_cmp_exclude_clean expected.log debug.log &&
799 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
800 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
801 test_cmp error.o error.r
805 test_expect_success 'process filter abort stops processing of all further files' '
806 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
807 rm -rf repo &&
808 mkdir repo &&
810 cd repo &&
811 git init &&
813 echo "*.r filter=protocol" >.gitattributes &&
815 cp "$TEST_ROOT/test.o" test.r &&
816 cp "$TEST_ROOT/test2.o" test2.r &&
817 echo "error this blob and all future blobs" >abort.o &&
818 cp abort.o abort.r &&
820 M="blob=$(git hash-object abort.r)" &&
821 rm -f debug.log &&
822 SA=$(test_file_size abort.r) &&
824 git add . &&
825 rm -f *.r &&
828 # Note: This test assumes that Git filters files in alphabetical
829 # order ("abort.r" before "test.r").
830 filter_git checkout --quiet --no-progress . &&
831 cat >expected.log <<-EOF &&
832 START
833 init handshake complete
834 IN: smudge abort.r $M $SA [OK] -- [ABORT]
835 STOP
837 test_cmp_exclude_clean expected.log debug.log &&
839 test_cmp "$TEST_ROOT/test.o" test.r &&
840 test_cmp "$TEST_ROOT/test2.o" test2.r &&
841 test_cmp abort.o abort.r
845 test_expect_success PERL 'invalid process filter must fail (and not hang!)' '
846 test_config_global filter.protocol.process cat &&
847 test_config_global filter.protocol.required true &&
848 rm -rf repo &&
849 mkdir repo &&
851 cd repo &&
852 git init &&
854 echo "*.r filter=protocol" >.gitattributes &&
856 cp "$TEST_ROOT/test.o" test.r &&
857 test_must_fail git add . 2>git-stderr.log &&
858 grep "expected git-filter-server" git-stderr.log
862 test_expect_success 'delayed checkout in process filter' '
863 test_config_global filter.a.process "test-tool rot13-filter --log=a.log clean smudge delay" &&
864 test_config_global filter.a.required true &&
865 test_config_global filter.b.process "test-tool rot13-filter --log=b.log clean smudge delay" &&
866 test_config_global filter.b.required true &&
868 rm -rf repo &&
869 mkdir repo &&
871 cd repo &&
872 git init &&
873 echo "*.a filter=a" >.gitattributes &&
874 echo "*.b filter=b" >>.gitattributes &&
875 cp "$TEST_ROOT/test.o" test.a &&
876 cp "$TEST_ROOT/test.o" test-delay10.a &&
877 cp "$TEST_ROOT/test.o" test-delay11.a &&
878 cp "$TEST_ROOT/test.o" test-delay20.a &&
879 cp "$TEST_ROOT/test.o" test-delay10.b &&
880 git add . &&
881 git commit -m "test commit"
882 ) &&
884 S=$(test_file_size "$TEST_ROOT/test.o") &&
885 PM="ref=refs/heads/main treeish=$(git -C repo rev-parse --verify main) " &&
886 M="${PM}blob=$(git -C repo rev-parse --verify main:test.a)" &&
887 cat >a.exp <<-EOF &&
888 START
889 init handshake complete
890 IN: smudge test.a $M $S [OK] -- OUT: $S . [OK]
891 IN: smudge test-delay10.a $M $S [OK] -- [DELAYED]
892 IN: smudge test-delay11.a $M $S [OK] -- [DELAYED]
893 IN: smudge test-delay20.a $M $S [OK] -- [DELAYED]
894 IN: list_available_blobs test-delay10.a test-delay11.a [OK]
895 IN: smudge test-delay10.a $M 0 [OK] -- OUT: $S . [OK]
896 IN: smudge test-delay11.a $M 0 [OK] -- OUT: $S . [OK]
897 IN: list_available_blobs test-delay20.a [OK]
898 IN: smudge test-delay20.a $M 0 [OK] -- OUT: $S . [OK]
899 IN: list_available_blobs [OK]
900 STOP
902 cat >b.exp <<-EOF &&
903 START
904 init handshake complete
905 IN: smudge test-delay10.b $M $S [OK] -- [DELAYED]
906 IN: list_available_blobs test-delay10.b [OK]
907 IN: smudge test-delay10.b $M 0 [OK] -- OUT: $S . [OK]
908 IN: list_available_blobs [OK]
909 STOP
912 rm -rf repo-cloned &&
913 filter_git clone repo repo-cloned &&
914 test_cmp_count a.exp repo-cloned/a.log &&
915 test_cmp_count b.exp repo-cloned/b.log &&
918 cd repo-cloned &&
919 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
920 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
921 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
922 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
923 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b &&
925 rm *.a *.b &&
926 filter_git checkout . &&
927 # We are not checking out a ref here, so filter out ref metadata.
928 sed -e "s!$PM!!" ../a.exp >a.exp.filtered &&
929 sed -e "s!$PM!!" ../b.exp >b.exp.filtered &&
930 test_cmp_count a.exp.filtered a.log &&
931 test_cmp_count b.exp.filtered b.log &&
933 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
934 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
935 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
936 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
937 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b
941 test_expect_success 'missing file in delayed checkout' '
942 test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
943 test_config_global filter.bug.required true &&
945 rm -rf repo &&
946 mkdir repo &&
948 cd repo &&
949 git init &&
950 echo "*.a filter=bug" >.gitattributes &&
951 cp "$TEST_ROOT/test.o" missing-delay.a &&
952 git add . &&
953 git commit -m "test commit"
954 ) &&
956 rm -rf repo-cloned &&
957 test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
958 grep "error: .missing-delay\.a. was not filtered properly" git-stderr.log
961 test_expect_success 'invalid file in delayed checkout' '
962 test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
963 test_config_global filter.bug.required true &&
965 rm -rf repo &&
966 mkdir repo &&
968 cd repo &&
969 git init &&
970 echo "*.a filter=bug" >.gitattributes &&
971 cp "$TEST_ROOT/test.o" invalid-delay.a &&
972 cp "$TEST_ROOT/test.o" unfiltered &&
973 git add . &&
974 git commit -m "test commit"
975 ) &&
977 rm -rf repo-cloned &&
978 test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
979 grep "error: external filter .* signaled that .unfiltered. is now available although it has not been delayed earlier" git-stderr.log
982 for mode in 'case' 'utf-8'
984 case "$mode" in
985 case) dir='A' symlink='a' mode_prereq='CASE_INSENSITIVE_FS' ;;
986 utf-8)
987 dir=$(printf "\141\314\210") symlink=$(printf "\303\244")
988 mode_prereq='UTF8_NFD_TO_NFC' ;;
989 esac
991 test_expect_success SYMLINKS,$mode_prereq \
992 "delayed checkout with $mode-collision don't write to the wrong place" '
993 test_config_global filter.delay.process \
994 "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
995 test_config_global filter.delay.required true &&
997 git init $mode-collision &&
999 cd $mode-collision &&
1000 mkdir target-dir &&
1002 empty_oid=$(printf "" | git hash-object -w --stdin) &&
1003 symlink_oid=$(printf "%s" "$PWD/target-dir" | git hash-object -w --stdin) &&
1004 attr_oid=$(echo "$dir/z filter=delay" | git hash-object -w --stdin) &&
1006 cat >objs <<-EOF &&
1007 100644 blob $empty_oid $dir/x
1008 100644 blob $empty_oid $dir/y
1009 100644 blob $empty_oid $dir/z
1010 120000 blob $symlink_oid $symlink
1011 100644 blob $attr_oid .gitattributes
1014 git update-index --index-info <objs &&
1015 git commit -m "test commit"
1016 ) &&
1018 git clone $mode-collision $mode-collision-cloned &&
1019 # Make sure z was really delayed
1020 grep "IN: smudge $dir/z .* \\[DELAYED\\]" $mode-collision-cloned/delayed.log &&
1022 # Should not create $dir/z at $symlink/z
1023 test_path_is_missing $mode-collision/target-dir/z
1025 done
1027 test_expect_success SYMLINKS,CASE_INSENSITIVE_FS \
1028 "delayed checkout with submodule collision don't write to the wrong place" '
1029 git init collision-with-submodule &&
1031 cd collision-with-submodule &&
1032 git config filter.delay.process "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
1033 git config filter.delay.required true &&
1035 # We need Git to treat the submodule "a" and the
1036 # leading dir "A" as different paths in the index.
1037 git config --local core.ignoreCase false &&
1039 empty_oid=$(printf "" | git hash-object -w --stdin) &&
1040 attr_oid=$(echo "A/B/y filter=delay" | git hash-object -w --stdin) &&
1041 cat >objs <<-EOF &&
1042 100644 blob $empty_oid A/B/x
1043 100644 blob $empty_oid A/B/y
1044 100644 blob $attr_oid .gitattributes
1046 git update-index --index-info <objs &&
1048 git init a &&
1049 mkdir target-dir &&
1050 symlink_oid=$(printf "%s" "$PWD/target-dir" | git -C a hash-object -w --stdin) &&
1051 echo "120000 blob $symlink_oid b" >objs &&
1052 git -C a update-index --index-info <objs &&
1053 git -C a commit -m sub &&
1054 git submodule add ./a &&
1055 git commit -m super &&
1057 git checkout --recurse-submodules . &&
1058 grep "IN: smudge A/B/y .* \\[DELAYED\\]" delayed.log &&
1059 test_path_is_missing target-dir/y
1063 test_expect_success 'setup for progress tests' '
1064 git init progress &&
1066 cd progress &&
1067 git config filter.delay.process "test-tool rot13-filter --log=delay-progress.log clean smudge delay" &&
1068 git config filter.delay.required true &&
1070 echo "*.a filter=delay" >.gitattributes &&
1071 touch test-delay10.a &&
1072 git add . &&
1073 git commit -m files
1077 test_delayed_checkout_progress () {
1078 if test "$1" = "!"
1079 then
1080 local expect_progress=N &&
1081 shift
1082 else
1083 local expect_progress=
1084 fi &&
1086 if test $# -lt 1
1087 then
1088 BUG "no command given to test_delayed_checkout_progress"
1089 fi &&
1092 cd progress &&
1093 GIT_PROGRESS_DELAY=0 &&
1094 export GIT_PROGRESS_DELAY &&
1095 rm -f *.a delay-progress.log &&
1097 "$@" 2>err &&
1098 grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delay-progress.log &&
1099 if test "$expect_progress" = N
1100 then
1101 ! grep "Filtering content" err
1102 else
1103 grep "Filtering content" err
1108 for mode in pathspec branch
1110 case "$mode" in
1111 pathspec) opt='.' ;;
1112 branch) opt='-f HEAD' ;;
1113 esac
1115 test_expect_success PERL,TTY "delayed checkout shows progress by default on tty ($mode checkout)" '
1116 test_delayed_checkout_progress test_terminal git checkout $opt
1119 test_expect_success PERL "delayed checkout omits progress on non-tty ($mode checkout)" '
1120 test_delayed_checkout_progress ! git checkout $opt
1123 test_expect_success PERL,TTY "delayed checkout omits progress with --quiet ($mode checkout)" '
1124 test_delayed_checkout_progress ! test_terminal git checkout --quiet $opt
1127 test_expect_success PERL,TTY "delayed checkout honors --[no]-progress ($mode checkout)" '
1128 test_delayed_checkout_progress ! test_terminal git checkout --no-progress $opt &&
1129 test_delayed_checkout_progress test_terminal git checkout --quiet --progress $opt
1131 done
1133 test_expect_success 'delayed checkout correctly reports the number of updated entries' '
1134 rm -rf repo &&
1135 git init repo &&
1137 cd repo &&
1138 git config filter.delay.process "test-tool rot13-filter --log=delayed.log clean smudge delay" &&
1139 git config filter.delay.required true &&
1141 echo "*.a filter=delay" >.gitattributes &&
1142 echo a >test-delay10.a &&
1143 echo a >test-delay11.a &&
1144 git add . &&
1145 git commit -m files &&
1147 rm *.a &&
1148 git checkout . 2>err &&
1149 grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delayed.log &&
1150 grep "IN: smudge test-delay11.a .* \\[DELAYED\\]" delayed.log &&
1151 grep "Updated 2 paths from the index" err
1155 test_done