1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
5
6test_description='commit and log output encodings'
7
8. ./test-lib.sh
9
10compare_with () {
11	git show -s $1 | sed -e '1,/^$/d' -e 's/^    //' >current &&
12	case "$3" in
13	'')
14		test_cmp "$2" current ;;
15	?*)
16		iconv -f "$3" -t UTF-8 >current.utf8 <current &&
17		iconv -f "$3" -t UTF-8 >expect.utf8 <"$2" &&
18		test_cmp expect.utf8 current.utf8
19		;;
20	esac
21}
22
23test_expect_success setup '
24	: >F &&
25	git add F &&
26	T=$(git write-tree) &&
27	C=$(git commit-tree $T <"$TEST_DIRECTORY"/t3900/1-UTF-8.txt) &&
28	git update-ref HEAD $C &&
29	git tag C0
30'
31
32test_expect_success 'no encoding header for base case' '
33	E=$(git cat-file commit C0 | sed -ne "s/^encoding //p") &&
34	test z = "z$E"
35'
36
37test_expect_success 'UTF-16 refused because of NULs' '
38	echo UTF-16 >F &&
39	test_must_fail git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
40'
41
42test_expect_success 'UTF-8 invalid characters refused' '
43	test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
44	echo "UTF-8 characters" >F &&
45	printf "Commit message\n\nInvalid surrogate:\355\240\200\n" \
46		>"$HOME/invalid" &&
47	git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
48	test_i18ngrep "did not conform" "$HOME"/stderr
49'
50
51test_expect_success 'UTF-8 overlong sequences rejected' '
52	test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
53	rm -f "$HOME/stderr" "$HOME/invalid" &&
54	echo "UTF-8 overlong" >F &&
55	printf "\340\202\251ommit message\n\nThis is not a space:\300\240\n" \
56		>"$HOME/invalid" &&
57	git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
58	test_i18ngrep "did not conform" "$HOME"/stderr
59'
60
61test_expect_success 'UTF-8 non-characters refused' '
62	test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
63	echo "UTF-8 non-character 1" >F &&
64	printf "Commit message\n\nNon-character:\364\217\277\276\n" \
65		>"$HOME/invalid" &&
66	git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
67	test_i18ngrep "did not conform" "$HOME"/stderr
68'
69
70test_expect_success 'UTF-8 non-characters refused' '
71	test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
72	echo "UTF-8 non-character 2." >F &&
73	printf "Commit message\n\nNon-character:\357\267\220\n" \
74		>"$HOME/invalid" &&
75	git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
76	test_i18ngrep "did not conform" "$HOME"/stderr
77'
78
79for H in ISO8859-1 eucJP ISO-2022-JP
80do
81	test_expect_success "$H setup" '
82		git config i18n.commitencoding $H &&
83		git checkout -b $H C0 &&
84		echo $H >F &&
85		git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt
86	'
87done
88
89for H in ISO8859-1 eucJP ISO-2022-JP
90do
91	test_expect_success "check encoding header for $H" '
92		E=$(git cat-file commit '$H' | sed -ne "s/^encoding //p") &&
93		test "z$E" = "z'$H'"
94	'
95done
96
97test_expect_success 'config to remove customization' '
98	git config --unset-all i18n.commitencoding &&
99	if Z=$(git config --get-all i18n.commitencoding)
100	then
101		echo Oops, should have failed.
102		false
103	else
104		test z = "z$Z"
105	fi &&
106	git config i18n.commitencoding UTF-8
107'
108
109test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
110	compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
111'
112
113for H in eucJP ISO-2022-JP
114do
115	test_expect_success "$H should be shown in UTF-8 now" '
116		compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
117	'
118done
119
120test_expect_success 'config to add customization' '
121	git config --unset-all i18n.commitencoding &&
122	if Z=$(git config --get-all i18n.commitencoding)
123	then
124		echo Oops, should have failed.
125		false
126	else
127		test z = "z$Z"
128	fi
129'
130
131for H in ISO8859-1 eucJP ISO-2022-JP
132do
133	test_expect_success "$H should be shown in itself now" '
134		git config i18n.commitencoding '$H' &&
135		compare_with '$H' "$TEST_DIRECTORY"/t3900/'$H'.txt
136	'
137done
138
139test_expect_success 'config to tweak customization' '
140	git config i18n.logoutputencoding UTF-8
141'
142
143test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
144	compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
145'
146
147for H in eucJP ISO-2022-JP
148do
149	test_expect_success "$H should be shown in UTF-8 now" '
150		compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
151	'
152done
153
154for J in eucJP ISO-2022-JP
155do
156	if test "$J" = ISO-2022-JP
157	then
158		ICONV=$J
159	else
160		ICONV=
161	fi
162	git config i18n.logoutputencoding $J
163	for H in eucJP ISO-2022-JP
164	do
165		test_expect_success "$H should be shown in $J now" '
166			compare_with '$H' "$TEST_DIRECTORY"/t3900/'$J'.txt $ICONV
167		'
168	done
169done
170
171for H in ISO8859-1 eucJP ISO-2022-JP
172do
173	test_expect_success "No conversion with $H" '
174		compare_with "--encoding=none '$H'" "$TEST_DIRECTORY"/t3900/'$H'.txt
175	'
176done
177
178test_commit_autosquash_flags () {
179	H=$1
180	flag=$2
181	test_expect_success "commit --$flag with $H encoding" '
182		git config i18n.commitencoding $H &&
183		git checkout -b $H-$flag C0 &&
184		echo $H >>F &&
185		git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
186		test_tick &&
187		echo intermediate stuff >>G &&
188		git add G &&
189		git commit -a -m "intermediate commit" &&
190		test_tick &&
191		echo $H $flag >>F &&
192		git commit -a --$flag HEAD~1 &&
193		E=$(git cat-file commit '$H-$flag' |
194			sed -ne "s/^encoding //p") &&
195		test "z$E" = "z$H" &&
196		git config --unset-all i18n.commitencoding &&
197		git rebase --autosquash -i HEAD^^^ &&
198		git log --oneline >actual &&
199		test_line_count = 3 actual
200	'
201}
202
203test_commit_autosquash_flags eucJP fixup
204
205test_commit_autosquash_flags ISO-2022-JP squash
206
207test_commit_autosquash_multi_encoding () {
208	flag=$1
209	old=$2
210	new=$3
211	msg=$4
212	test_expect_success "commit --$flag into $old from $new" '
213		git checkout -b $flag-$old-$new C0 &&
214		git config i18n.commitencoding $old &&
215		echo $old >>F &&
216		git commit -a -F "$TEST_DIRECTORY"/t3900/$msg &&
217		test_tick &&
218		echo intermediate stuff >>G &&
219		git add G &&
220		git commit -a -m "intermediate commit" &&
221		test_tick &&
222		git config i18n.commitencoding $new &&
223		echo $new-$flag >>F &&
224		git commit -a --$flag HEAD^ &&
225		git rebase --autosquash -i HEAD^^^ &&
226		git rev-list HEAD >actual &&
227		test_line_count = 3 actual &&
228		iconv -f $old -t UTF-8 "$TEST_DIRECTORY"/t3900/$msg >expect &&
229		git cat-file commit HEAD^ >raw &&
230		(sed "1,/^$/d" raw | iconv -f $new -t utf-8) >actual &&
231		test_cmp expect actual
232	'
233}
234
235test_commit_autosquash_multi_encoding fixup UTF-8 ISO-8859-1 1-UTF-8.txt
236test_commit_autosquash_multi_encoding squash ISO-8859-1 UTF-8 ISO8859-1.txt
237test_commit_autosquash_multi_encoding squash eucJP ISO-2022-JP eucJP.txt
238test_commit_autosquash_multi_encoding fixup ISO-2022-JP UTF-8 ISO-2022-JP.txt
239
240test_done
241