t0021-conversion.sh 5.9 KB
Newer Older
J
Junio C Hamano 已提交
1 2 3 4 5 6
#!/bin/sh

test_description='blob conversion via gitattributes'

. ./test-lib.sh

7 8
cat <<EOF >rot13.sh
#!$SHELL_PATH
9 10 11
tr \
  'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
  'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
12 13 14
EOF
chmod +x rot13.sh

J
Junio C Hamano 已提交
15
test_expect_success setup '
16 17 18
	git config filter.rot13.smudge ./rot13.sh &&
	git config filter.rot13.clean ./rot13.sh &&

J
Junio C Hamano 已提交
19
	{
20
	    echo "*.t filter=rot13"
J
Junio C Hamano 已提交
21 22 23 24 25 26
	    echo "*.i ident"
	} >.gitattributes &&

	{
	    echo a b c d e f g h i j k l m
	    echo n o p q r s t u v w x y z
27
	    echo '\''$Id$'\''
J
Junio C Hamano 已提交
28 29 30 31 32 33 34 35 36
	} >test &&
	cat test >test.t &&
	cat test >test.o &&
	cat test >test.i &&
	git add test test.t test.i &&
	rm -f test test.t test.i &&
	git checkout -- test test.t test.i
'

37
script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
J
Junio C Hamano 已提交
38 39 40 41 42 43 44 45 46 47

test_expect_success check '

	cmp test.o test &&
	cmp test.o test.t &&

	# ident should be stripped in the repository
	git diff --raw --exit-code :test :test.i &&
	id=$(git rev-parse --verify :test) &&
	embedded=$(sed -ne "$script" test.i) &&
48 49 50 51 52 53
	test "z$id" = "z$embedded" &&

	git cat-file blob :test.t > test.r &&

	./rot13.sh < test.o > test.t &&
	cmp test.r test.t
J
Junio C Hamano 已提交
54 55
'

56 57 58 59 60 61 62 63 64 65 66 67
# If an expanded ident ever gets into the repository, we want to make sure that
# it is collapsed before being expanded again on checkout
test_expect_success expanded_in_repo '
	{
		echo "File with expanded keywords"
		echo "\$Id\$"
		echo "\$Id:\$"
		echo "\$Id: 0000000000000000000000000000000000000000 \$"
		echo "\$Id: NoSpaceAtEnd\$"
		echo "\$Id:NoSpaceAtFront \$"
		echo "\$Id:NoSpaceAtEitherEnd\$"
		echo "\$Id: NoTerminatingSymbol"
68
		echo "\$Id: Foreign Commit With Spaces \$"
69
	} >expanded-keywords.0 &&
70

71 72 73 74 75 76
	{
		cat expanded-keywords.0 &&
		printf "\$Id: NoTerminatingSymbolAtEOF"
	} >expanded-keywords &&
	cat expanded-keywords >expanded-keywords-crlf &&
	git add expanded-keywords expanded-keywords-crlf &&
77 78 79
	git commit -m "File with keywords expanded" &&
	id=$(git rev-parse --verify :expanded-keywords) &&

80 81
	{
		echo "File with expanded keywords"
82 83 84 85 86 87
		echo "\$Id: $id \$"
		echo "\$Id: $id \$"
		echo "\$Id: $id \$"
		echo "\$Id: $id \$"
		echo "\$Id: $id \$"
		echo "\$Id: $id \$"
88
		echo "\$Id: NoTerminatingSymbol"
89
		echo "\$Id: Foreign Commit With Spaces \$"
90 91 92
	} >expected-output.0 &&
	{
		cat expected-output.0 &&
93
		printf "\$Id: NoTerminatingSymbolAtEOF"
94 95 96 97 98 99 100 101 102
	} >expected-output &&
	{
		append_cr <expected-output.0 &&
		printf "\$Id: NoTerminatingSymbolAtEOF"
	} >expected-output-crlf &&
	{
		echo "expanded-keywords ident"
		echo "expanded-keywords-crlf ident text eol=crlf"
	} >>.gitattributes &&
103

104
	rm -f expanded-keywords expanded-keywords-crlf &&
105 106

	git checkout -- expanded-keywords &&
107 108 109 110
	test_cmp expanded-keywords expected-output &&

	git checkout -- expanded-keywords-crlf &&
	test_cmp expanded-keywords-crlf expected-output-crlf
111 112
'

113 114 115 116 117 118 119
# The use of %f in a filter definition is expanded to the path to
# the filename being smudged or cleaned.  It must be shell escaped.
# First, set up some interesting file names and pet them in
# .gitattributes.
test_expect_success 'filter shell-escaped filenames' '
	cat >argc.sh <<-EOF &&
	#!$SHELL_PATH
120
	cat >/dev/null
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	echo argc: \$# "\$@"
	EOF
	normal=name-no-magic &&
	special="name  with '\''sq'\'' and \$x" &&
	echo some test text >"$normal" &&
	echo some test text >"$special" &&
	git add "$normal" "$special" &&
	git commit -q -m "add files" &&
	echo "name* filter=argc" >.gitattributes &&

	# delete the files and check them out again, using a smudge filter
	# that will count the args and echo the command-line back to us
	git config filter.argc.smudge "sh ./argc.sh %f" &&
	rm "$normal" "$special" &&
	git checkout -- "$normal" "$special" &&

	# make sure argc.sh counted the right number of args
	echo "argc: 1 $normal" >expect &&
	test_cmp expect "$normal" &&
	echo "argc: 1 $special" >expect &&
	test_cmp expect "$special" &&

	# do the same thing, but with more args in the filter expression
	git config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" &&
	rm "$normal" "$special" &&
	git checkout -- "$normal" "$special" &&

	# make sure argc.sh counted the right number of args
	echo "argc: 2 $normal --my-extra-arg" >expect &&
	test_cmp expect "$normal" &&
	echo "argc: 2 $special --my-extra-arg" >expect &&
	test_cmp expect "$special" &&
	:
'

156 157 158
test_expect_success 'required filter should filter data' '
	git config filter.required.smudge ./rot13.sh &&
	git config filter.required.clean ./rot13.sh &&
159 160 161 162
	git config filter.required.required true &&

	echo "*.r filter=required" >.gitattributes &&

163
	cat test.o >test.r &&
164
	git add test.r &&
165

166
	rm -f test.r &&
167 168 169 170 171 172
	git checkout -- test.r &&
	cmp test.o test.r &&

	./rot13.sh <test.o >expected &&
	git cat-file blob :test.r >actual &&
	cmp expected actual
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
'

test_expect_success 'required filter smudge failure' '
	git config filter.failsmudge.smudge false &&
	git config filter.failsmudge.clean cat &&
	git config filter.failsmudge.required true &&

	echo "*.fs filter=failsmudge" >.gitattributes &&

	echo test >test.fs &&
	git add test.fs &&
	rm -f test.fs &&
	test_must_fail git checkout -- test.fs
'

test_expect_success 'required filter clean failure' '
	git config filter.failclean.smudge cat &&
	git config filter.failclean.clean false &&
	git config filter.failclean.required true &&

	echo "*.fc filter=failclean" >.gitattributes &&

	echo test >test.fc &&
	test_must_fail git add test.fc
'

199 200 201 202 203 204 205 206
test_expect_success 'filtering large input to small output should use little memory' '
	git config filter.devnull.clean "cat >/dev/null" &&
	git config filter.devnull.required true &&
	for i in $(test_seq 1 30); do printf "%1048576d" 1; done >30MB &&
	echo "30MB filter=devnull" >.gitattributes &&
	GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
'

207 208 209 210 211 212 213 214 215 216 217 218
test_expect_success EXPENSIVE 'filter large file' '
	git config filter.largefile.smudge cat &&
	git config filter.largefile.clean cat &&
	for i in $(test_seq 1 2048); do printf "%1048576d" 1; done >2GB &&
	echo "2GB filter=largefile" >.gitattributes &&
	git add 2GB 2>err &&
	! test -s err &&
	rm -f 2GB &&
	git checkout -- 2GB 2>err &&
	! test -s err
'

J
Junio C Hamano 已提交
219
test_done