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