builtin/reset: compute checkout metadata for reset
[git] / t / t0021-conversion.sh
1 #!/bin/sh
2
3 test_description='blob conversion via gitattributes'
4
5 . ./test-lib.sh
6
7 TEST_ROOT="$PWD"
8 PATH=$TEST_ROOT:$PATH
9
10 write_script <<\EOF "$TEST_ROOT/rot13.sh"
11 tr \
12   'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
13   'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
14 EOF
15
16 write_script rot13-filter.pl "$PERL_PATH" \
17         <"$TEST_DIRECTORY"/t0021/rot13-filter.pl
18
19 generate_random_characters () {
20         LEN=$1
21         NAME=$2
22         test-tool genrandom some-seed $LEN |
23                 perl -pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
24 }
25
26 file_size () {
27         test-tool path-utils file-size "$1"
28 }
29
30 filter_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. http://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
39 test_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. http://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
54 test_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.
67 test_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
74 test_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
99 script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
100
101 test_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
120 test_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.
179 test_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
218 test_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
237 test_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
250 test_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
261 test_expect_success 'filtering large input to small output should use little memory' '
262         test_config filter.devnull.clean "cat >/dev/null" &&
263         test_config filter.devnull.required true &&
264         for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB &&
265         echo "30MB filter=devnull" >.gitattributes &&
266         GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
267 '
268
269 test_expect_success 'filter that does not read is fine' '
270         test-tool genrandom foo $((128 * 1024 + 1)) >big &&
271         echo "big filter=epipe" >.gitattributes &&
272         test_config filter.epipe.clean "echo xyzzy" &&
273         git add big &&
274         git cat-file blob :big >actual &&
275         echo xyzzy >expect &&
276         test_cmp expect actual
277 '
278
279 test_expect_success EXPENSIVE 'filter large file' '
280         test_config filter.largefile.smudge cat &&
281         test_config filter.largefile.clean cat &&
282         for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB &&
283         echo "2GB filter=largefile" >.gitattributes &&
284         git add 2GB 2>err &&
285         test_must_be_empty err &&
286         rm -f 2GB &&
287         git checkout -- 2GB 2>err &&
288         test_must_be_empty err
289 '
290
291 test_expect_success "filter: clean empty file" '
292         test_config filter.in-repo-header.clean  "echo cleaned && cat" &&
293         test_config filter.in-repo-header.smudge "sed 1d" &&
294
295         echo "empty-in-worktree    filter=in-repo-header" >>.gitattributes &&
296         >empty-in-worktree &&
297
298         echo cleaned >expected &&
299         git add empty-in-worktree &&
300         git show :empty-in-worktree >actual &&
301         test_cmp expected actual
302 '
303
304 test_expect_success "filter: smudge empty file" '
305         test_config filter.empty-in-repo.clean "cat >/dev/null" &&
306         test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
307
308         echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
309         echo dead data walking >empty-in-repo &&
310         git add empty-in-repo &&
311
312         echo smudged >expected &&
313         git checkout-index --prefix=filtered- empty-in-repo &&
314         test_cmp expected filtered-empty-in-repo
315 '
316
317 test_expect_success 'disable filter with empty override' '
318         test_config_global filter.disable.smudge false &&
319         test_config_global filter.disable.clean false &&
320         test_config filter.disable.smudge false &&
321         test_config filter.disable.clean false &&
322
323         echo "*.disable filter=disable" >.gitattributes &&
324
325         echo test >test.disable &&
326         git -c filter.disable.clean= add test.disable 2>err &&
327         test_must_be_empty err &&
328         rm -f test.disable &&
329         git -c filter.disable.smudge= checkout -- test.disable 2>err &&
330         test_must_be_empty err
331 '
332
333 test_expect_success 'diff does not reuse worktree files that need cleaning' '
334         test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
335         echo "file filter=counter" >.gitattributes &&
336         test_commit one file &&
337         test_commit two file &&
338
339         >count &&
340         git diff-tree -p HEAD &&
341         test_line_count = 0 count
342 '
343
344 test_expect_success PERL 'required process filter should filter data' '
345         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
346         test_config_global filter.protocol.required true &&
347         rm -rf repo &&
348         mkdir repo &&
349         (
350                 cd repo &&
351                 git init &&
352
353                 echo "*.r filter=protocol" >.gitattributes &&
354                 git add . &&
355                 git commit -m "test commit 1" &&
356                 git branch empty-branch &&
357
358                 cp "$TEST_ROOT/test.o" test.r &&
359                 cp "$TEST_ROOT/test2.o" test2.r &&
360                 mkdir testsubdir &&
361                 cp "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r" &&
362                 >test4-empty.r &&
363
364                 S=$(file_size test.r) &&
365                 S2=$(file_size test2.r) &&
366                 S3=$(file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
367                 M=$(git hash-object test.r) &&
368                 M2=$(git hash-object test2.r) &&
369                 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
370                 EMPTY=$(git hash-object /dev/null) &&
371
372                 filter_git add . &&
373                 cat >expected.log <<-EOF &&
374                         START
375                         init handshake complete
376                         IN: clean test.r $S [OK] -- OUT: $S . [OK]
377                         IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
378                         IN: clean test4-empty.r 0 [OK] -- OUT: 0  [OK]
379                         IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
380                         STOP
381                 EOF
382                 test_cmp_count expected.log debug.log &&
383
384                 git commit -m "test commit 2" &&
385                 MASTER=$(git rev-parse --verify master) &&
386                 META="ref=refs/heads/master treeish=$MASTER" &&
387                 rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
388
389                 filter_git checkout --quiet --no-progress . &&
390                 cat >expected.log <<-EOF &&
391                         START
392                         init handshake complete
393                         IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
394                         IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
395                         STOP
396                 EOF
397                 test_cmp_exclude_clean expected.log debug.log &&
398
399                 # Make sure that the file appears dirty, so checkout below has to
400                 # run the configured filter.
401                 touch test.r &&
402                 filter_git checkout --quiet --no-progress empty-branch &&
403                 cat >expected.log <<-EOF &&
404                         START
405                         init handshake complete
406                         IN: clean test.r $S [OK] -- OUT: $S . [OK]
407                         STOP
408                 EOF
409                 test_cmp_exclude_clean expected.log debug.log &&
410
411                 filter_git checkout --quiet --no-progress master &&
412                 cat >expected.log <<-EOF &&
413                         START
414                         init handshake complete
415                         IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
416                         IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
417                         IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0  [OK]
418                         IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
419                         STOP
420                 EOF
421                 test_cmp_exclude_clean expected.log debug.log &&
422
423                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
424                 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
425                 test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r"
426         )
427 '
428
429 test_expect_success PERL 'required process filter should filter data for various subcommands' '
430         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
431         test_config_global filter.protocol.required true &&
432         (
433                 cd repo &&
434
435                 S=$(file_size test.r) &&
436                 S2=$(file_size test2.r) &&
437                 S3=$(file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
438                 M=$(git hash-object test.r) &&
439                 M2=$(git hash-object test2.r) &&
440                 M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
441                 EMPTY=$(git hash-object /dev/null) &&
442
443                 MASTER=$(git rev-parse --verify master) &&
444
445                 cp "$TEST_ROOT/test.o" test5.r &&
446                 git add test5.r &&
447                 git commit -m "test commit 3" &&
448                 git checkout empty-branch &&
449                 filter_git rebase --onto empty-branch master^^ master &&
450                 MASTER2=$(git rev-parse --verify master) &&
451                 META="ref=refs/heads/master treeish=$MASTER2" &&
452                 cat >expected.log <<-EOF &&
453                         START
454                         init handshake complete
455                         IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
456                         IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
457                         IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0  [OK]
458                         IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
459                         IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
460                         STOP
461                 EOF
462                 test_cmp_exclude_clean expected.log debug.log &&
463
464                 git reset --hard empty-branch &&
465                 filter_git reset --hard $MASTER &&
466                 META="treeish=$MASTER" &&
467                 cat >expected.log <<-EOF &&
468                         START
469                         init handshake complete
470                         IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
471                         IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
472                         IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0  [OK]
473                         IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
474                         STOP
475                 EOF
476                 test_cmp_exclude_clean expected.log debug.log &&
477
478                 git branch old-master $MASTER &&
479                 git reset --hard empty-branch &&
480                 filter_git reset --hard old-master &&
481                 META="ref=refs/heads/old-master treeish=$MASTER" &&
482                 cat >expected.log <<-EOF &&
483                         START
484                         init handshake complete
485                         IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
486                         IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
487                         IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0  [OK]
488                         IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
489                         STOP
490                 EOF
491                 test_cmp_exclude_clean expected.log debug.log
492         )
493 '
494
495 test_expect_success PERL 'required process filter takes precedence' '
496         test_config_global filter.protocol.clean false &&
497         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean" &&
498         test_config_global filter.protocol.required true &&
499         rm -rf repo &&
500         mkdir repo &&
501         (
502                 cd repo &&
503                 git init &&
504
505                 echo "*.r filter=protocol" >.gitattributes &&
506                 cp "$TEST_ROOT/test.o" test.r &&
507                 S=$(file_size test.r) &&
508
509                 # Check that the process filter is invoked here
510                 filter_git add . &&
511                 cat >expected.log <<-EOF &&
512                         START
513                         init handshake complete
514                         IN: clean test.r $S [OK] -- OUT: $S . [OK]
515                         STOP
516                 EOF
517                 test_cmp_count expected.log debug.log
518         )
519 '
520
521 test_expect_success PERL 'required process filter should be used only for "clean" operation only' '
522         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean" &&
523         rm -rf repo &&
524         mkdir repo &&
525         (
526                 cd repo &&
527                 git init &&
528
529                 echo "*.r filter=protocol" >.gitattributes &&
530                 cp "$TEST_ROOT/test.o" test.r &&
531                 S=$(file_size test.r) &&
532
533                 filter_git add . &&
534                 cat >expected.log <<-EOF &&
535                         START
536                         init handshake complete
537                         IN: clean test.r $S [OK] -- OUT: $S . [OK]
538                         STOP
539                 EOF
540                 test_cmp_count expected.log debug.log &&
541
542                 rm test.r &&
543
544                 filter_git checkout --quiet --no-progress . &&
545                 # If the filter would be used for "smudge", too, we would see
546                 # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
547                 cat >expected.log <<-EOF &&
548                         START
549                         init handshake complete
550                         STOP
551                 EOF
552                 test_cmp_exclude_clean expected.log debug.log
553         )
554 '
555
556 test_expect_success PERL 'required process filter should process multiple packets' '
557         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
558         test_config_global filter.protocol.required true &&
559
560         rm -rf repo &&
561         mkdir repo &&
562         (
563                 cd repo &&
564                 git init &&
565
566                 # Generate data requiring 1, 2, 3 packets
567                 S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
568                 generate_random_characters $(($S    )) 1pkt_1__.file &&
569                 generate_random_characters $(($S  +1)) 2pkt_1+1.file &&
570                 generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
571                 generate_random_characters $(($S*2  )) 2pkt_2__.file &&
572                 generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
573
574                 for FILE in "$TEST_ROOT"/*.file
575                 do
576                         cp "$FILE" . &&
577                         rot13.sh <"$FILE" >"$FILE.rot13"
578                 done &&
579
580                 echo "*.file filter=protocol" >.gitattributes &&
581                 filter_git add *.file .gitattributes &&
582                 cat >expected.log <<-EOF &&
583                         START
584                         init handshake complete
585                         IN: clean 1pkt_1__.file $(($S    )) [OK] -- OUT: $(($S    )) . [OK]
586                         IN: clean 2pkt_1+1.file $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]
587                         IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
588                         IN: clean 2pkt_2__.file $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]
589                         IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
590                         STOP
591                 EOF
592                 test_cmp_count expected.log debug.log &&
593
594                 M1="blob=$(git hash-object 1pkt_1__.file)" &&
595                 M2="blob=$(git hash-object 2pkt_1+1.file)" &&
596                 M3="blob=$(git hash-object 2pkt_2-1.file)" &&
597                 M4="blob=$(git hash-object 2pkt_2__.file)" &&
598                 M5="blob=$(git hash-object 3pkt_2+1.file)" &&
599                 rm -f *.file debug.log &&
600
601                 filter_git checkout --quiet --no-progress -- *.file &&
602                 cat >expected.log <<-EOF &&
603                         START
604                         init handshake complete
605                         IN: smudge 1pkt_1__.file $M1 $(($S    )) [OK] -- OUT: $(($S    )) . [OK]
606                         IN: smudge 2pkt_1+1.file $M2 $(($S  +1)) [OK] -- OUT: $(($S  +1)) .. [OK]
607                         IN: smudge 2pkt_2-1.file $M3 $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
608                         IN: smudge 2pkt_2__.file $M4 $(($S*2  )) [OK] -- OUT: $(($S*2  )) .. [OK]
609                         IN: smudge 3pkt_2+1.file $M5 $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
610                         STOP
611                 EOF
612                 test_cmp_exclude_clean expected.log debug.log &&
613
614                 for FILE in *.file
615                 do
616                         test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE
617                 done
618         )
619 '
620
621 test_expect_success PERL 'required process filter with clean error should fail' '
622         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
623         test_config_global filter.protocol.required true &&
624         rm -rf repo &&
625         mkdir repo &&
626         (
627                 cd repo &&
628                 git init &&
629
630                 echo "*.r filter=protocol" >.gitattributes &&
631
632                 cp "$TEST_ROOT/test.o" test.r &&
633                 echo "this is going to fail" >clean-write-fail.r &&
634                 echo "content-test3-subdir" >test3.r &&
635
636                 test_must_fail git add .
637         )
638 '
639
640 test_expect_success PERL 'process filter should restart after unexpected write failure' '
641         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
642         rm -rf repo &&
643         mkdir repo &&
644         (
645                 cd repo &&
646                 git init &&
647
648                 echo "*.r filter=protocol" >.gitattributes &&
649
650                 cp "$TEST_ROOT/test.o" test.r &&
651                 cp "$TEST_ROOT/test2.o" test2.r &&
652                 echo "this is going to fail" >smudge-write-fail.o &&
653                 cp smudge-write-fail.o smudge-write-fail.r &&
654
655                 S=$(file_size test.r) &&
656                 S2=$(file_size test2.r) &&
657                 SF=$(file_size smudge-write-fail.r) &&
658                 M=$(git hash-object test.r) &&
659                 M2=$(git hash-object test2.r) &&
660                 MF=$(git hash-object smudge-write-fail.r) &&
661                 rm -f debug.log &&
662
663                 git add . &&
664                 rm -f *.r &&
665
666                 rm -f debug.log &&
667                 git checkout --quiet --no-progress . 2>git-stderr.log &&
668
669                 grep "smudge write error at" git-stderr.log &&
670                 test_i18ngrep "error: external filter" git-stderr.log &&
671
672                 cat >expected.log <<-EOF &&
673                         START
674                         init handshake complete
675                         IN: smudge smudge-write-fail.r blob=$MF $SF [OK] -- [WRITE FAIL]
676                         START
677                         init handshake complete
678                         IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
679                         IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
680                         STOP
681                 EOF
682                 test_cmp_exclude_clean expected.log debug.log &&
683
684                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
685                 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
686
687                 # Smudge failed
688                 ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
689                 rot13.sh <smudge-write-fail.o >expected &&
690                 git cat-file blob :smudge-write-fail.r >actual &&
691                 test_cmp expected actual
692         )
693 '
694
695 test_expect_success PERL 'process filter should not be restarted if it signals an error' '
696         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
697         rm -rf repo &&
698         mkdir repo &&
699         (
700                 cd repo &&
701                 git init &&
702
703                 echo "*.r filter=protocol" >.gitattributes &&
704
705                 cp "$TEST_ROOT/test.o" test.r &&
706                 cp "$TEST_ROOT/test2.o" test2.r &&
707                 echo "this will cause an error" >error.o &&
708                 cp error.o error.r &&
709
710                 S=$(file_size test.r) &&
711                 S2=$(file_size test2.r) &&
712                 SE=$(file_size error.r) &&
713                 M=$(git hash-object test.r) &&
714                 M2=$(git hash-object test2.r) &&
715                 ME=$(git hash-object error.r) &&
716                 rm -f debug.log &&
717
718                 git add . &&
719                 rm -f *.r &&
720
721                 filter_git checkout --quiet --no-progress . &&
722                 cat >expected.log <<-EOF &&
723                         START
724                         init handshake complete
725                         IN: smudge error.r blob=$ME $SE [OK] -- [ERROR]
726                         IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
727                         IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
728                         STOP
729                 EOF
730                 test_cmp_exclude_clean expected.log debug.log &&
731
732                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
733                 test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
734                 test_cmp error.o error.r
735         )
736 '
737
738 test_expect_success PERL 'process filter abort stops processing of all further files' '
739         test_config_global filter.protocol.process "rot13-filter.pl debug.log clean smudge" &&
740         rm -rf repo &&
741         mkdir repo &&
742         (
743                 cd repo &&
744                 git init &&
745
746                 echo "*.r filter=protocol" >.gitattributes &&
747
748                 cp "$TEST_ROOT/test.o" test.r &&
749                 cp "$TEST_ROOT/test2.o" test2.r &&
750                 echo "error this blob and all future blobs" >abort.o &&
751                 cp abort.o abort.r &&
752
753                 M="blob=$(git hash-object abort.r)" &&
754                 rm -f debug.log &&
755                 SA=$(file_size abort.r) &&
756
757                 git add . &&
758                 rm -f *.r &&
759
760
761                 # Note: This test assumes that Git filters files in alphabetical
762                 # order ("abort.r" before "test.r").
763                 filter_git checkout --quiet --no-progress . &&
764                 cat >expected.log <<-EOF &&
765                         START
766                         init handshake complete
767                         IN: smudge abort.r $M $SA [OK] -- [ABORT]
768                         STOP
769                 EOF
770                 test_cmp_exclude_clean expected.log debug.log &&
771
772                 test_cmp "$TEST_ROOT/test.o" test.r &&
773                 test_cmp "$TEST_ROOT/test2.o" test2.r &&
774                 test_cmp abort.o abort.r
775         )
776 '
777
778 test_expect_success PERL 'invalid process filter must fail (and not hang!)' '
779         test_config_global filter.protocol.process cat &&
780         test_config_global filter.protocol.required true &&
781         rm -rf repo &&
782         mkdir repo &&
783         (
784                 cd repo &&
785                 git init &&
786
787                 echo "*.r filter=protocol" >.gitattributes &&
788
789                 cp "$TEST_ROOT/test.o" test.r &&
790                 test_must_fail git add . 2>git-stderr.log &&
791                 grep "expected git-filter-server" git-stderr.log
792         )
793 '
794
795 test_expect_success PERL 'delayed checkout in process filter' '
796         test_config_global filter.a.process "rot13-filter.pl a.log clean smudge delay" &&
797         test_config_global filter.a.required true &&
798         test_config_global filter.b.process "rot13-filter.pl b.log clean smudge delay" &&
799         test_config_global filter.b.required true &&
800
801         rm -rf repo &&
802         mkdir repo &&
803         (
804                 cd repo &&
805                 git init &&
806                 echo "*.a filter=a" >.gitattributes &&
807                 echo "*.b filter=b" >>.gitattributes &&
808                 cp "$TEST_ROOT/test.o" test.a &&
809                 cp "$TEST_ROOT/test.o" test-delay10.a &&
810                 cp "$TEST_ROOT/test.o" test-delay11.a &&
811                 cp "$TEST_ROOT/test.o" test-delay20.a &&
812                 cp "$TEST_ROOT/test.o" test-delay10.b &&
813                 git add . &&
814                 git commit -m "test commit"
815         ) &&
816
817         S=$(file_size "$TEST_ROOT/test.o") &&
818         PM="ref=refs/heads/master treeish=$(git -C repo rev-parse --verify master) " &&
819         M="${PM}blob=$(git -C repo rev-parse --verify master:test.a)" &&
820         cat >a.exp <<-EOF &&
821                 START
822                 init handshake complete
823                 IN: smudge test.a $M $S [OK] -- OUT: $S . [OK]
824                 IN: smudge test-delay10.a $M $S [OK] -- [DELAYED]
825                 IN: smudge test-delay11.a $M $S [OK] -- [DELAYED]
826                 IN: smudge test-delay20.a $M $S [OK] -- [DELAYED]
827                 IN: list_available_blobs test-delay10.a test-delay11.a [OK]
828                 IN: smudge test-delay10.a $M 0 [OK] -- OUT: $S . [OK]
829                 IN: smudge test-delay11.a $M 0 [OK] -- OUT: $S . [OK]
830                 IN: list_available_blobs test-delay20.a [OK]
831                 IN: smudge test-delay20.a $M 0 [OK] -- OUT: $S . [OK]
832                 IN: list_available_blobs [OK]
833                 STOP
834         EOF
835         cat >b.exp <<-EOF &&
836                 START
837                 init handshake complete
838                 IN: smudge test-delay10.b $M $S [OK] -- [DELAYED]
839                 IN: list_available_blobs test-delay10.b [OK]
840                 IN: smudge test-delay10.b $M 0 [OK] -- OUT: $S . [OK]
841                 IN: list_available_blobs [OK]
842                 STOP
843         EOF
844
845         rm -rf repo-cloned &&
846         filter_git clone repo repo-cloned &&
847         test_cmp_count a.exp repo-cloned/a.log &&
848         test_cmp_count b.exp repo-cloned/b.log &&
849
850         (
851                 cd repo-cloned &&
852                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
853                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
854                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
855                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
856                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b &&
857
858                 rm *.a *.b &&
859                 filter_git checkout . &&
860                 # We are not checking out a ref here, so filter out ref metadata.
861                 sed -e "s!$PM!!" ../a.exp >a.exp.filtered &&
862                 sed -e "s!$PM!!" ../b.exp >b.exp.filtered &&
863                 test_cmp_count a.exp.filtered a.log &&
864                 test_cmp_count b.exp.filtered b.log &&
865
866                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
867                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
868                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
869                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
870                 test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b
871         )
872 '
873
874 test_expect_success PERL 'missing file in delayed checkout' '
875         test_config_global filter.bug.process "rot13-filter.pl bug.log clean smudge delay" &&
876         test_config_global filter.bug.required true &&
877
878         rm -rf repo &&
879         mkdir repo &&
880         (
881                 cd repo &&
882                 git init &&
883                 echo "*.a filter=bug" >.gitattributes &&
884                 cp "$TEST_ROOT/test.o" missing-delay.a &&
885                 git add . &&
886                 git commit -m "test commit"
887         ) &&
888
889         rm -rf repo-cloned &&
890         test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
891         grep "error: .missing-delay\.a. was not filtered properly" git-stderr.log
892 '
893
894 test_expect_success PERL 'invalid file in delayed checkout' '
895         test_config_global filter.bug.process "rot13-filter.pl bug.log clean smudge delay" &&
896         test_config_global filter.bug.required true &&
897
898         rm -rf repo &&
899         mkdir repo &&
900         (
901                 cd repo &&
902                 git init &&
903                 echo "*.a filter=bug" >.gitattributes &&
904                 cp "$TEST_ROOT/test.o" invalid-delay.a &&
905                 cp "$TEST_ROOT/test.o" unfiltered &&
906                 git add . &&
907                 git commit -m "test commit"
908         ) &&
909
910         rm -rf repo-cloned &&
911         test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
912         grep "error: external filter .* signaled that .unfiltered. is now available although it has not been delayed earlier" git-stderr.log
913 '
914
915 test_done