clone_submodule: avoid using `access()` on directories
[git.git] / t / t0021-conversion.sh
blobabecd75e4e430b6a1182690ae54a0e5d4e76bc32
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-lib.sh
9 . "$TEST_DIRECTORY"/lib-terminal.sh
11 TEST_ROOT="$PWD"
12 PATH=$TEST_ROOT:$PATH
14 write_script <<\EOF "$TEST_ROOT/rot13.sh"
15 tr \
16 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
17 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
18 EOF
20 generate_random_characters () {
21 LEN=$1
22 NAME=$2
23 test-tool genrandom some-seed $LEN |
24 perl -pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
27 filter_git () {
28 rm -f *.log &&
29 git "$@"
32 # Compare two files and ensure that `clean` and `smudge` respectively are
33 # called at least once if specified in the `expect` file. The actual
34 # invocation count is not relevant because their number can vary.
35 # c.f. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
36 test_cmp_count () {
37 expect=$1
38 actual=$2
39 for FILE in "$expect" "$actual"
41 sort "$FILE" | uniq -c |
42 sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp"
43 done &&
44 test_cmp "$expect.tmp" "$actual.tmp" &&
45 rm "$expect.tmp" "$actual.tmp"
48 # Compare two files but exclude all `clean` invocations because Git can
49 # call `clean` zero or more times.
50 # c.f. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
51 test_cmp_exclude_clean () {
52 expect=$1
53 actual=$2
54 for FILE in "$expect" "$actual"
56 grep -v "IN: clean" "$FILE" >"$FILE.tmp"
57 done &&
58 test_cmp "$expect.tmp" "$actual.tmp" &&
59 rm "$expect.tmp" "$actual.tmp"
62 # Check that the contents of two files are equal and that their rot13 version
63 # is equal to the committed content.
64 test_cmp_committed_rot13 () {
65 test_cmp "$1" "$2" &&
66 rot13.sh <"$1" >expected &&
67 git cat-file blob :"$2" >actual &&
68 test_cmp expected actual
71 test_expect_success setup '
72 git config filter.rot13.smudge ./rot13.sh &&
73 git config filter.rot13.clean ./rot13.sh &&
76 echo "*.t filter=rot13" &&
77 echo "*.i ident"
78 } >.gitattributes &&
81 echo a b c d e f g h i j k l m &&
82 echo n o p q r s t u v w x y z &&
83 echo '\''$Id$'\''
84 } >test &&
85 cat test >test.t &&
86 cat test >test.o &&
87 cat test >test.i &&
88 git add test test.t test.i &&
89 rm -f test test.t test.i &&
90 git checkout -- test test.t test.i &&
92 echo "content-test2" >test2.o &&
93 echo "content-test3 - filename with special characters" >"test3 '\''sq'\'',\$x=.o"
96 script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
98 test_expect_success check '
100 test_cmp test.o test &&
101 test_cmp test.o test.t &&
103 # ident should be stripped in the repository
104 git diff --raw --exit-code :test :test.i &&
105 id=$(git rev-parse --verify :test) &&
106 embedded=$(sed -ne "$script" test.i) &&
107 test "z$id" = "z$embedded" &&
109 git cat-file blob :test.t >test.r &&
111 ./rot13.sh <test.o >test.t &&
112 test_cmp test.r test.t
115 # If an expanded ident ever gets into the repository, we want to make sure that
116 # it is collapsed before being expanded again on checkout
117 test_expect_success expanded_in_repo '
118 cat >expanded-keywords.0 <<-\EOF &&
119 File with expanded keywords
120 $Id$
121 $Id:$
122 $Id: 0000000000000000000000000000000000000000 $
123 $Id: NoSpaceAtEnd$
124 $Id:NoSpaceAtFront $
125 $Id:NoSpaceAtEitherEnd$
126 $Id: NoTerminatingSymbol
127 $Id: Foreign Commit With Spaces $
131 cat expanded-keywords.0 &&
132 printf "\$Id: NoTerminatingSymbolAtEOF"
133 } >expanded-keywords &&
134 cat expanded-keywords >expanded-keywords-crlf &&
135 git add expanded-keywords expanded-keywords-crlf &&
136 git commit -m "File with keywords expanded" &&
137 id=$(git rev-parse --verify :expanded-keywords) &&
139 cat >expected-output.0 <<-EOF &&
140 File with expanded keywords
141 \$Id: $id \$
142 \$Id: $id \$
143 \$Id: $id \$
144 \$Id: $id \$
145 \$Id: $id \$
146 \$Id: $id \$
147 \$Id: NoTerminatingSymbol
148 \$Id: Foreign Commit With Spaces \$
151 cat expected-output.0 &&
152 printf "\$Id: NoTerminatingSymbolAtEOF"
153 } >expected-output &&
155 append_cr <expected-output.0 &&
156 printf "\$Id: NoTerminatingSymbolAtEOF"
157 } >expected-output-crlf &&
159 echo "expanded-keywords ident" &&
160 echo "expanded-keywords-crlf ident text eol=crlf"
161 } >>.gitattributes &&
163 rm -f expanded-keywords expanded-keywords-crlf &&
165 git checkout -- expanded-keywords &&
166 test_cmp expected-output expanded-keywords &&
168 git checkout -- expanded-keywords-crlf &&
169 test_cmp expected-output-crlf expanded-keywords-crlf
172 # The use of %f in a filter definition is expanded to the path to
173 # the filename being smudged or cleaned. It must be shell escaped.
174 # First, set up some interesting file names and pet them in
175 # .gitattributes.
176 test_expect_success 'filter shell-escaped filenames' '
177 cat >argc.sh <<-EOF &&
178 #!$SHELL_PATH
179 cat >/dev/null
180 echo argc: \$# "\$@"
182 normal=name-no-magic &&
183 special="name with '\''sq'\'' and \$x" &&
184 echo some test text >"$normal" &&
185 echo some test text >"$special" &&
186 git add "$normal" "$special" &&
187 git commit -q -m "add files" &&
188 echo "name* filter=argc" >.gitattributes &&
190 # delete the files and check them out again, using a smudge filter
191 # that will count the args and echo the command-line back to us
192 test_config filter.argc.smudge "sh ./argc.sh %f" &&
193 rm "$normal" "$special" &&
194 git checkout -- "$normal" "$special" &&
196 # make sure argc.sh counted the right number of args
197 echo "argc: 1 $normal" >expect &&
198 test_cmp expect "$normal" &&
199 echo "argc: 1 $special" >expect &&
200 test_cmp expect "$special" &&
202 # do the same thing, but with more args in the filter expression
203 test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" &&
204 rm "$normal" "$special" &&
205 git checkout -- "$normal" "$special" &&
207 # make sure argc.sh counted the right number of args
208 echo "argc: 2 $normal --my-extra-arg" >expect &&
209 test_cmp expect "$normal" &&
210 echo "argc: 2 $special --my-extra-arg" >expect &&
211 test_cmp expect "$special" &&
215 test_expect_success 'required filter should filter data' '
216 test_config filter.required.smudge ./rot13.sh &&
217 test_config filter.required.clean ./rot13.sh &&
218 test_config filter.required.required true &&
220 echo "*.r filter=required" >.gitattributes &&
222 cat test.o >test.r &&
223 git add test.r &&
225 rm -f test.r &&
226 git checkout -- test.r &&
227 test_cmp test.o test.r &&
229 ./rot13.sh <test.o >expected &&
230 git cat-file blob :test.r >actual &&
231 test_cmp expected actual
234 test_expect_success 'required filter smudge failure' '
235 test_config filter.failsmudge.smudge false &&
236 test_config filter.failsmudge.clean cat &&
237 test_config filter.failsmudge.required true &&
239 echo "*.fs filter=failsmudge" >.gitattributes &&
241 echo test >test.fs &&
242 git add test.fs &&
243 rm -f test.fs &&
244 test_must_fail git checkout -- test.fs
247 test_expect_success 'required filter clean failure' '
248 test_config filter.failclean.smudge cat &&
249 test_config filter.failclean.clean false &&
250 test_config filter.failclean.required true &&
252 echo "*.fc filter=failclean" >.gitattributes &&
254 echo test >test.fc &&
255 test_must_fail git add test.fc
258 test_expect_success 'required filter with absent clean field' '
259 test_config filter.absentclean.smudge cat &&
260 test_config filter.absentclean.required true &&
262 echo "*.ac filter=absentclean" >.gitattributes &&
264 echo test >test.ac &&
265 test_must_fail git add test.ac 2>stderr &&
266 test_i18ngrep "fatal: test.ac: clean filter .absentclean. failed" stderr
269 test_expect_success 'required filter with absent smudge field' '
270 test_config filter.absentsmudge.clean cat &&
271 test_config filter.absentsmudge.required true &&
273 echo "*.as filter=absentsmudge" >.gitattributes &&
275 echo test >test.as &&
276 git add test.as &&
277 rm -f test.as &&
278 test_must_fail git checkout -- test.as 2>stderr &&
279 test_i18ngrep "fatal: test.as: smudge filter absentsmudge failed" stderr
282 test_expect_success 'filtering large input to small output should use little memory' '
283 test_config filter.devnull.clean "cat >/dev/null" &&
284 test_config filter.devnull.required true &&
285 for i in $(test_seq 1 30); do printf "%1048576d" 1 || return 1; done >30MB &&
286 echo "30MB filter=devnull" >.gitattributes &&
287 GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
290 test_expect_success 'filter that does not read is fine' '
291 test-tool genrandom foo $((128 * 1024 + 1)) >big &&
292 echo "big filter=epipe" >.gitattributes &&
293 test_config filter.epipe.clean "echo xyzzy" &&
294 git add big &&
295 git cat-file blob :big >actual &&
296 echo xyzzy >expect &&
297 test_cmp expect actual
300 test_expect_success EXPENSIVE 'filter large file' '
301 test_config filter.largefile.smudge cat &&
302 test_config filter.largefile.clean cat &&
303 for i in $(test_seq 1 2048); do printf "%1048576d" 1 || return 1; done >2GB &&
304 echo "2GB filter=largefile" >.gitattributes &&
305 git add 2GB 2>err &&
306 test_must_be_empty err &&
307 rm -f 2GB &&
308 git checkout -- 2GB 2>err &&
309 test_must_be_empty err
312 test_expect_success "filter: clean empty file" '
313 test_config filter.in-repo-header.clean "echo cleaned && cat" &&
314 test_config filter.in-repo-header.smudge "sed 1d" &&
316 echo "empty-in-worktree filter=in-repo-header" >>.gitattributes &&
317 >empty-in-worktree &&
319 echo cleaned >expected &&
320 git add empty-in-worktree &&
321 git show :empty-in-worktree >actual &&
322 test_cmp expected actual
325 test_expect_success "filter: smudge empty file" '
326 test_config filter.empty-in-repo.clean "cat >/dev/null" &&
327 test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
329 echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
330 echo dead data walking >empty-in-repo &&
331 git add empty-in-repo &&
333 echo smudged >expected &&
334 git checkout-index --prefix=filtered- empty-in-repo &&
335 test_cmp expected filtered-empty-in-repo
338 test_expect_success 'disable filter with empty override' '
339 test_config_global filter.disable.smudge false &&
340 test_config_global filter.disable.clean false &&
341 test_config filter.disable.smudge false &&
342 test_config filter.disable.clean false &&
344 echo "*.disable filter=disable" >.gitattributes &&
346 echo test >test.disable &&
347 git -c filter.disable.clean= add test.disable 2>err &&
348 test_must_be_empty err &&
349 rm -f test.disable &&
350 git -c filter.disable.smudge= checkout -- test.disable 2>err &&
351 test_must_be_empty err
354 test_expect_success 'diff does not reuse worktree files that need cleaning' '
355 test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
356 echo "file filter=counter" >.gitattributes &&
357 test_commit one file &&
358 test_commit two file &&
360 >count &&
361 git diff-tree -p HEAD &&
362 test_line_count = 0 count
365 test_expect_success 'required process filter should filter data' '
366 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
367 test_config_global filter.protocol.required true &&
368 rm -rf repo &&
369 mkdir repo &&
371 cd repo &&
372 git init &&
374 echo "*.r filter=protocol" >.gitattributes &&
375 git add . &&
376 git commit -m "test commit 1" &&
377 git branch empty-branch &&
379 cp "$TEST_ROOT/test.o" test.r &&
380 cp "$TEST_ROOT/test2.o" test2.r &&
381 mkdir testsubdir &&
382 cp "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r" &&
383 >test4-empty.r &&
385 S=$(test_file_size test.r) &&
386 S2=$(test_file_size test2.r) &&
387 S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
388 M=$(git hash-object test.r) &&
389 M2=$(git hash-object test2.r) &&
390 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
391 EMPTY=$(git hash-object /dev/null) &&
393 filter_git add . &&
394 cat >expected.log <<-EOF &&
395 START
396 init handshake complete
397 IN: clean test.r $S [OK] -- OUT: $S . [OK]
398 IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
399 IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
400 IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
401 STOP
403 test_cmp_count expected.log debug.log &&
405 git commit -m "test commit 2" &&
406 MAIN=$(git rev-parse --verify main) &&
407 META="ref=refs/heads/main treeish=$MAIN" &&
408 rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
410 filter_git checkout --quiet --no-progress . &&
411 cat >expected.log <<-EOF &&
412 START
413 init handshake complete
414 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
415 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
416 STOP
418 test_cmp_exclude_clean expected.log debug.log &&
420 # Make sure that the file appears dirty, so checkout below has to
421 # run the configured filter.
422 touch test.r &&
423 filter_git checkout --quiet --no-progress empty-branch &&
424 cat >expected.log <<-EOF &&
425 START
426 init handshake complete
427 IN: clean test.r $S [OK] -- OUT: $S . [OK]
428 STOP
430 test_cmp_exclude_clean expected.log debug.log &&
432 filter_git checkout --quiet --no-progress main &&
433 cat >expected.log <<-EOF &&
434 START
435 init handshake complete
436 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
437 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
438 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
439 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
440 STOP
442 test_cmp_exclude_clean expected.log debug.log &&
444 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
445 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
446 test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r"
450 test_expect_success 'required process filter should filter data for various subcommands' '
451 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
452 test_config_global filter.protocol.required true &&
454 cd repo &&
456 S=$(test_file_size test.r) &&
457 S2=$(test_file_size test2.r) &&
458 S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
459 M=$(git hash-object test.r) &&
460 M2=$(git hash-object test2.r) &&
461 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
462 EMPTY=$(git hash-object /dev/null) &&
464 MAIN=$(git rev-parse --verify main) &&
466 cp "$TEST_ROOT/test.o" test5.r &&
467 git add test5.r &&
468 git commit -m "test commit 3" &&
469 git checkout empty-branch &&
470 filter_git rebase --onto empty-branch main^^ main &&
471 MAIN2=$(git rev-parse --verify main) &&
472 META="ref=refs/heads/main treeish=$MAIN2" &&
473 cat >expected.log <<-EOF &&
474 START
475 init handshake complete
476 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
477 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
478 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
479 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
480 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
481 STOP
483 test_cmp_exclude_clean expected.log debug.log &&
485 git reset --hard empty-branch &&
486 filter_git reset --hard $MAIN &&
487 META="treeish=$MAIN" &&
488 cat >expected.log <<-EOF &&
489 START
490 init handshake complete
491 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
492 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
493 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
494 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
495 STOP
497 test_cmp_exclude_clean expected.log debug.log &&
499 git branch old-main $MAIN &&
500 git reset --hard empty-branch &&
501 filter_git reset --hard old-main &&
502 META="ref=refs/heads/old-main treeish=$MAIN" &&
503 cat >expected.log <<-EOF &&
504 START
505 init handshake complete
506 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
507 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
508 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
509 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
510 STOP
512 test_cmp_exclude_clean expected.log debug.log &&
514 git checkout -b merge empty-branch &&
515 git branch -f main $MAIN2 &&
516 filter_git merge main &&
517 META="treeish=$MAIN2" &&
518 cat >expected.log <<-EOF &&
519 START
520 init handshake complete
521 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
522 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
523 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
524 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
525 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
526 STOP
528 test_cmp_exclude_clean expected.log debug.log &&
530 filter_git archive main >/dev/null &&
531 META="ref=refs/heads/main treeish=$MAIN2" &&
532 cat >expected.log <<-EOF &&
533 START
534 init handshake complete
535 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
536 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
537 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
538 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
539 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
540 STOP
542 test_cmp_exclude_clean expected.log debug.log &&
544 TREE="$(git rev-parse $MAIN2^{tree})" &&
545 filter_git archive $TREE >/dev/null &&
546 META="treeish=$TREE" &&
547 cat >expected.log <<-EOF &&
548 START
549 init handshake complete
550 IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
551 IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
552 IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
553 IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
554 IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
555 STOP
557 test_cmp_exclude_clean expected.log debug.log
561 test_expect_success 'required process filter takes precedence' '
562 test_config_global filter.protocol.clean false &&
563 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
564 test_config_global filter.protocol.required true &&
565 rm -rf repo &&
566 mkdir repo &&
568 cd repo &&
569 git init &&
571 echo "*.r filter=protocol" >.gitattributes &&
572 cp "$TEST_ROOT/test.o" test.r &&
573 S=$(test_file_size test.r) &&
575 # Check that the process filter is invoked here
576 filter_git add . &&
577 cat >expected.log <<-EOF &&
578 START
579 init handshake complete
580 IN: clean test.r $S [OK] -- OUT: $S . [OK]
581 STOP
583 test_cmp_count expected.log debug.log
587 test_expect_success 'required process filter should be used only for "clean" operation only' '
588 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
589 rm -rf repo &&
590 mkdir repo &&
592 cd repo &&
593 git init &&
595 echo "*.r filter=protocol" >.gitattributes &&
596 cp "$TEST_ROOT/test.o" test.r &&
597 S=$(test_file_size test.r) &&
599 filter_git add . &&
600 cat >expected.log <<-EOF &&
601 START
602 init handshake complete
603 IN: clean test.r $S [OK] -- OUT: $S . [OK]
604 STOP
606 test_cmp_count expected.log debug.log &&
608 rm test.r &&
610 filter_git checkout --quiet --no-progress . &&
611 # If the filter would be used for "smudge", too, we would see
612 # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
613 cat >expected.log <<-EOF &&
614 START
615 init handshake complete
616 STOP
618 test_cmp_exclude_clean expected.log debug.log
622 test_expect_success 'required process filter should process multiple packets' '
623 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
624 test_config_global filter.protocol.required true &&
626 rm -rf repo &&
627 mkdir repo &&
629 cd repo &&
630 git init &&
632 # Generate data requiring 1, 2, 3 packets
633 S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
634 generate_random_characters $(($S )) 1pkt_1__.file &&
635 generate_random_characters $(($S +1)) 2pkt_1+1.file &&
636 generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
637 generate_random_characters $(($S*2 )) 2pkt_2__.file &&
638 generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
640 for FILE in "$TEST_ROOT"/*.file
642 cp "$FILE" . &&
643 rot13.sh <"$FILE" >"$FILE.rot13" || return 1
644 done &&
646 echo "*.file filter=protocol" >.gitattributes &&
647 filter_git add *.file .gitattributes &&
648 cat >expected.log <<-EOF &&
649 START
650 init handshake complete
651 IN: clean 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
652 IN: clean 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
653 IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
654 IN: clean 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
655 IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
656 STOP
658 test_cmp_count expected.log debug.log &&
660 M1="blob=$(git hash-object 1pkt_1__.file)" &&
661 M2="blob=$(git hash-object 2pkt_1+1.file)" &&
662 M3="blob=$(git hash-object 2pkt_2-1.file)" &&
663 M4="blob=$(git hash-object 2pkt_2__.file)" &&
664 M5="blob=$(git hash-object 3pkt_2+1.file)" &&
665 rm -f *.file debug.log &&
667 filter_git checkout --quiet --no-progress -- *.file &&
668 cat >expected.log <<-EOF &&
669 START
670 init handshake complete
671 IN: smudge 1pkt_1__.file $M1 $(($S )) [OK] -- OUT: $(($S )) . [OK]
672 IN: smudge 2pkt_1+1.file $M2 $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
673 IN: smudge 2pkt_2-1.file $M3 $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
674 IN: smudge 2pkt_2__.file $M4 $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
675 IN: smudge 3pkt_2+1.file $M5 $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
676 STOP
678 test_cmp_exclude_clean expected.log debug.log &&
680 for FILE in *.file
682 test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE || return 1
683 done
687 test_expect_success 'required process filter with clean error should fail' '
688 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
689 test_config_global filter.protocol.required true &&
690 rm -rf repo &&
691 mkdir repo &&
693 cd repo &&
694 git init &&
696 echo "*.r filter=protocol" >.gitattributes &&
698 cp "$TEST_ROOT/test.o" test.r &&
699 echo "this is going to fail" >clean-write-fail.r &&
700 echo "content-test3-subdir" >test3.r &&
702 test_must_fail git add .
706 test_expect_success 'process filter should restart after unexpected write failure' '
707 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
708 rm -rf repo &&
709 mkdir repo &&
711 cd repo &&
712 git init &&
714 echo "*.r filter=protocol" >.gitattributes &&
716 cp "$TEST_ROOT/test.o" test.r &&
717 cp "$TEST_ROOT/test2.o" test2.r &&
718 echo "this is going to fail" >smudge-write-fail.o &&
719 cp smudge-write-fail.o smudge-write-fail.r &&
721 S=$(test_file_size test.r) &&
722 S2=$(test_file_size test2.r) &&
723 SF=$(test_file_size smudge-write-fail.r) &&
724 M=$(git hash-object test.r) &&
725 M2=$(git hash-object test2.r) &&
726 MF=$(git hash-object smudge-write-fail.r) &&
727 rm -f debug.log &&
729 git add . &&
730 rm -f *.r &&
732 rm -f debug.log &&
733 git checkout --quiet --no-progress . 2>git-stderr.log &&
735 grep "smudge write error" git-stderr.log &&
736 test_i18ngrep "error: external filter" git-stderr.log &&
738 cat >expected.log <<-EOF &&
739 START
740 init handshake complete
741 IN: smudge smudge-write-fail.r blob=$MF $SF [OK] -- [WRITE FAIL]
742 START
743 init handshake complete
744 IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
745 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
746 STOP
748 test_cmp_exclude_clean expected.log debug.log &&
750 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
751 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
753 # Smudge failed
754 ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
755 rot13.sh <smudge-write-fail.o >expected &&
756 git cat-file blob :smudge-write-fail.r >actual &&
757 test_cmp expected actual
761 test_expect_success 'process filter should not be restarted if it signals an error' '
762 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
763 rm -rf repo &&
764 mkdir repo &&
766 cd repo &&
767 git init &&
769 echo "*.r filter=protocol" >.gitattributes &&
771 cp "$TEST_ROOT/test.o" test.r &&
772 cp "$TEST_ROOT/test2.o" test2.r &&
773 echo "this will cause an error" >error.o &&
774 cp error.o error.r &&
776 S=$(test_file_size test.r) &&
777 S2=$(test_file_size test2.r) &&
778 SE=$(test_file_size error.r) &&
779 M=$(git hash-object test.r) &&
780 M2=$(git hash-object test2.r) &&
781 ME=$(git hash-object error.r) &&
782 rm -f debug.log &&
784 git add . &&
785 rm -f *.r &&
787 filter_git checkout --quiet --no-progress . &&
788 cat >expected.log <<-EOF &&
789 START
790 init handshake complete
791 IN: smudge error.r blob=$ME $SE [OK] -- [ERROR]
792 IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
793 IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
794 STOP
796 test_cmp_exclude_clean expected.log debug.log &&
798 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
799 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
800 test_cmp error.o error.r
804 test_expect_success 'process filter abort stops processing of all further files' '
805 test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
806 rm -rf repo &&
807 mkdir repo &&
809 cd repo &&
810 git init &&
812 echo "*.r filter=protocol" >.gitattributes &&
814 cp "$TEST_ROOT/test.o" test.r &&
815 cp "$TEST_ROOT/test2.o" test2.r &&
816 echo "error this blob and all future blobs" >abort.o &&
817 cp abort.o abort.r &&
819 M="blob=$(git hash-object abort.r)" &&
820 rm -f debug.log &&
821 SA=$(test_file_size abort.r) &&
823 git add . &&
824 rm -f *.r &&
827 # Note: This test assumes that Git filters files in alphabetical
828 # order ("abort.r" before "test.r").
829 filter_git checkout --quiet --no-progress . &&
830 cat >expected.log <<-EOF &&
831 START
832 init handshake complete
833 IN: smudge abort.r $M $SA [OK] -- [ABORT]
834 STOP
836 test_cmp_exclude_clean expected.log debug.log &&
838 test_cmp "$TEST_ROOT/test.o" test.r &&
839 test_cmp "$TEST_ROOT/test2.o" test2.r &&
840 test_cmp abort.o abort.r
844 test_expect_success PERL 'invalid process filter must fail (and not hang!)' '
845 test_config_global filter.protocol.process cat &&
846 test_config_global filter.protocol.required true &&
847 rm -rf repo &&
848 mkdir repo &&
850 cd repo &&
851 git init &&
853 echo "*.r filter=protocol" >.gitattributes &&
855 cp "$TEST_ROOT/test.o" test.r &&
856 test_must_fail git add . 2>git-stderr.log &&
857 grep "expected git-filter-server" git-stderr.log
861 test_expect_success 'delayed checkout in process filter' '
862 test_config_global filter.a.process "test-tool rot13-filter --log=a.log clean smudge delay" &&
863 test_config_global filter.a.required true &&
864 test_config_global filter.b.process "test-tool rot13-filter --log=b.log clean smudge delay" &&
865 test_config_global filter.b.required true &&
867 rm -rf repo &&
868 mkdir repo &&
870 cd repo &&
871 git init &&
872 echo "*.a filter=a" >.gitattributes &&
873 echo "*.b filter=b" >>.gitattributes &&
874 cp "$TEST_ROOT/test.o" test.a &&
875 cp "$TEST_ROOT/test.o" test-delay10.a &&
876 cp "$TEST_ROOT/test.o" test-delay11.a &&
877 cp "$TEST_ROOT/test.o" test-delay20.a &&
878 cp "$TEST_ROOT/test.o" test-delay10.b &&
879 git add . &&
880 git commit -m "test commit"
881 ) &&
883 S=$(test_file_size "$TEST_ROOT/test.o") &&
884 PM="ref=refs/heads/main treeish=$(git -C repo rev-parse --verify main) " &&
885 M="${PM}blob=$(git -C repo rev-parse --verify main:test.a)" &&
886 cat >a.exp <<-EOF &&
887 START
888 init handshake complete
889 IN: smudge test.a $M $S [OK] -- OUT: $S . [OK]
890 IN: smudge test-delay10.a $M $S [OK] -- [DELAYED]
891 IN: smudge test-delay11.a $M $S [OK] -- [DELAYED]
892 IN: smudge test-delay20.a $M $S [OK] -- [DELAYED]
893 IN: list_available_blobs test-delay10.a test-delay11.a [OK]
894 IN: smudge test-delay10.a $M 0 [OK] -- OUT: $S . [OK]
895 IN: smudge test-delay11.a $M 0 [OK] -- OUT: $S . [OK]
896 IN: list_available_blobs test-delay20.a [OK]
897 IN: smudge test-delay20.a $M 0 [OK] -- OUT: $S . [OK]
898 IN: list_available_blobs [OK]
899 STOP
901 cat >b.exp <<-EOF &&
902 START
903 init handshake complete
904 IN: smudge test-delay10.b $M $S [OK] -- [DELAYED]
905 IN: list_available_blobs test-delay10.b [OK]
906 IN: smudge test-delay10.b $M 0 [OK] -- OUT: $S . [OK]
907 IN: list_available_blobs [OK]
908 STOP
911 rm -rf repo-cloned &&
912 filter_git clone repo repo-cloned &&
913 test_cmp_count a.exp repo-cloned/a.log &&
914 test_cmp_count b.exp repo-cloned/b.log &&
917 cd repo-cloned &&
918 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
919 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
920 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
921 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
922 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b &&
924 rm *.a *.b &&
925 filter_git checkout . &&
926 # We are not checking out a ref here, so filter out ref metadata.
927 sed -e "s!$PM!!" ../a.exp >a.exp.filtered &&
928 sed -e "s!$PM!!" ../b.exp >b.exp.filtered &&
929 test_cmp_count a.exp.filtered a.log &&
930 test_cmp_count b.exp.filtered b.log &&
932 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
933 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
934 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
935 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
936 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b
940 test_expect_success 'missing file in delayed checkout' '
941 test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
942 test_config_global filter.bug.required true &&
944 rm -rf repo &&
945 mkdir repo &&
947 cd repo &&
948 git init &&
949 echo "*.a filter=bug" >.gitattributes &&
950 cp "$TEST_ROOT/test.o" missing-delay.a &&
951 git add . &&
952 git commit -m "test commit"
953 ) &&
955 rm -rf repo-cloned &&
956 test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
957 grep "error: .missing-delay\.a. was not filtered properly" git-stderr.log
960 test_expect_success 'invalid file in delayed checkout' '
961 test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
962 test_config_global filter.bug.required true &&
964 rm -rf repo &&
965 mkdir repo &&
967 cd repo &&
968 git init &&
969 echo "*.a filter=bug" >.gitattributes &&
970 cp "$TEST_ROOT/test.o" invalid-delay.a &&
971 cp "$TEST_ROOT/test.o" unfiltered &&
972 git add . &&
973 git commit -m "test commit"
974 ) &&
976 rm -rf repo-cloned &&
977 test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
978 grep "error: external filter .* signaled that .unfiltered. is now available although it has not been delayed earlier" git-stderr.log
981 for mode in 'case' 'utf-8'
983 case "$mode" in
984 case) dir='A' symlink='a' mode_prereq='CASE_INSENSITIVE_FS' ;;
985 utf-8)
986 dir=$(printf "\141\314\210") symlink=$(printf "\303\244")
987 mode_prereq='UTF8_NFD_TO_NFC' ;;
988 esac
990 test_expect_success SYMLINKS,$mode_prereq \
991 "delayed checkout with $mode-collision don't write to the wrong place" '
992 test_config_global filter.delay.process \
993 "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
994 test_config_global filter.delay.required true &&
996 git init $mode-collision &&
998 cd $mode-collision &&
999 mkdir target-dir &&
1001 empty_oid=$(printf "" | git hash-object -w --stdin) &&
1002 symlink_oid=$(printf "%s" "$PWD/target-dir" | git hash-object -w --stdin) &&
1003 attr_oid=$(echo "$dir/z filter=delay" | git hash-object -w --stdin) &&
1005 cat >objs <<-EOF &&
1006 100644 blob $empty_oid $dir/x
1007 100644 blob $empty_oid $dir/y
1008 100644 blob $empty_oid $dir/z
1009 120000 blob $symlink_oid $symlink
1010 100644 blob $attr_oid .gitattributes
1013 git update-index --index-info <objs &&
1014 git commit -m "test commit"
1015 ) &&
1017 git clone $mode-collision $mode-collision-cloned &&
1018 # Make sure z was really delayed
1019 grep "IN: smudge $dir/z .* \\[DELAYED\\]" $mode-collision-cloned/delayed.log &&
1021 # Should not create $dir/z at $symlink/z
1022 test_path_is_missing $mode-collision/target-dir/z
1024 done
1026 test_expect_success SYMLINKS,CASE_INSENSITIVE_FS \
1027 "delayed checkout with submodule collision don't write to the wrong place" '
1028 git init collision-with-submodule &&
1030 cd collision-with-submodule &&
1031 git config filter.delay.process "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
1032 git config filter.delay.required true &&
1034 # We need Git to treat the submodule "a" and the
1035 # leading dir "A" as different paths in the index.
1036 git config --local core.ignoreCase false &&
1038 empty_oid=$(printf "" | git hash-object -w --stdin) &&
1039 attr_oid=$(echo "A/B/y filter=delay" | git hash-object -w --stdin) &&
1040 cat >objs <<-EOF &&
1041 100644 blob $empty_oid A/B/x
1042 100644 blob $empty_oid A/B/y
1043 100644 blob $attr_oid .gitattributes
1045 git update-index --index-info <objs &&
1047 git init a &&
1048 mkdir target-dir &&
1049 symlink_oid=$(printf "%s" "$PWD/target-dir" | git -C a hash-object -w --stdin) &&
1050 echo "120000 blob $symlink_oid b" >objs &&
1051 git -C a update-index --index-info <objs &&
1052 git -C a commit -m sub &&
1053 git submodule add ./a &&
1054 git commit -m super &&
1056 git checkout --recurse-submodules . &&
1057 grep "IN: smudge A/B/y .* \\[DELAYED\\]" delayed.log &&
1058 test_path_is_missing target-dir/y
1062 test_expect_success 'setup for progress tests' '
1063 git init progress &&
1065 cd progress &&
1066 git config filter.delay.process "test-tool rot13-filter --log=delay-progress.log clean smudge delay" &&
1067 git config filter.delay.required true &&
1069 echo "*.a filter=delay" >.gitattributes &&
1070 touch test-delay10.a &&
1071 git add . &&
1072 git commit -m files
1076 test_delayed_checkout_progress () {
1077 if test "$1" = "!"
1078 then
1079 local expect_progress=N &&
1080 shift
1081 else
1082 local expect_progress=
1083 fi &&
1085 if test $# -lt 1
1086 then
1087 BUG "no command given to test_delayed_checkout_progress"
1088 fi &&
1091 cd progress &&
1092 GIT_PROGRESS_DELAY=0 &&
1093 export GIT_PROGRESS_DELAY &&
1094 rm -f *.a delay-progress.log &&
1096 "$@" 2>err &&
1097 grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delay-progress.log &&
1098 if test "$expect_progress" = N
1099 then
1100 ! grep "Filtering content" err
1101 else
1102 grep "Filtering content" err
1107 for mode in pathspec branch
1109 case "$mode" in
1110 pathspec) opt='.' ;;
1111 branch) opt='-f HEAD' ;;
1112 esac
1114 test_expect_success PERL,TTY "delayed checkout shows progress by default on tty ($mode checkout)" '
1115 test_delayed_checkout_progress test_terminal git checkout $opt
1118 test_expect_success PERL "delayed checkout ommits progress on non-tty ($mode checkout)" '
1119 test_delayed_checkout_progress ! git checkout $opt
1122 test_expect_success PERL,TTY "delayed checkout ommits progress with --quiet ($mode checkout)" '
1123 test_delayed_checkout_progress ! test_terminal git checkout --quiet $opt
1126 test_expect_success PERL,TTY "delayed checkout honors --[no]-progress ($mode checkout)" '
1127 test_delayed_checkout_progress ! test_terminal git checkout --no-progress $opt &&
1128 test_delayed_checkout_progress test_terminal git checkout --quiet --progress $opt
1130 done
1132 test_expect_success 'delayed checkout correctly reports the number of updated entries' '
1133 rm -rf repo &&
1134 git init repo &&
1136 cd repo &&
1137 git config filter.delay.process "test-tool rot13-filter --log=delayed.log clean smudge delay" &&
1138 git config filter.delay.required true &&
1140 echo "*.a filter=delay" >.gitattributes &&
1141 echo a >test-delay10.a &&
1142 echo a >test-delay11.a &&
1143 git add . &&
1144 git commit -m files &&
1146 rm *.a &&
1147 git checkout . 2>err &&
1148 grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delayed.log &&
1149 grep "IN: smudge test-delay11.a .* \\[DELAYED\\]" delayed.log &&
1150 grep "Updated 2 paths from the index" err
1154 test_done