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