commit: reject overlong UTF-8 sequences
[alt-git.git] / t / t3900-i18n-commit.sh
blob051ea9d3c289d825601aed0a3055b1565370dc3a
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='commit and log output encodings'
8 . ./test-lib.sh
10 compare_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
20 esac
23 test_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
32 test_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"
37 test_expect_failure 'UTF-16 refused because of NULs' '
38 echo UTF-16 >F &&
39 git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
42 test_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 grep "did not conform" "$HOME"/stderr
51 test_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 grep "did not conform" "$HOME"/stderr
61 for H in ISO8859-1 eucJP ISO-2022-JP
63 test_expect_success "$H setup" '
64 git config i18n.commitencoding $H &&
65 git checkout -b $H C0 &&
66 echo $H >F &&
67 git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt
69 done
71 for H in ISO8859-1 eucJP ISO-2022-JP
73 test_expect_success "check encoding header for $H" '
74 E=$(git cat-file commit '$H' | sed -ne "s/^encoding //p") &&
75 test "z$E" = "z'$H'"
77 done
79 test_expect_success 'config to remove customization' '
80 git config --unset-all i18n.commitencoding &&
81 if Z=$(git config --get-all i18n.commitencoding)
82 then
83 echo Oops, should have failed.
84 false
85 else
86 test z = "z$Z"
87 fi &&
88 git config i18n.commitencoding UTF-8
91 test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
92 compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
95 for H in eucJP ISO-2022-JP
97 test_expect_success "$H should be shown in UTF-8 now" '
98 compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
100 done
102 test_expect_success 'config to add customization' '
103 git config --unset-all i18n.commitencoding &&
104 if Z=$(git config --get-all i18n.commitencoding)
105 then
106 echo Oops, should have failed.
107 false
108 else
109 test z = "z$Z"
113 for H in ISO8859-1 eucJP ISO-2022-JP
115 test_expect_success "$H should be shown in itself now" '
116 git config i18n.commitencoding '$H' &&
117 compare_with '$H' "$TEST_DIRECTORY"/t3900/'$H'.txt
119 done
121 test_expect_success 'config to tweak customization' '
122 git config i18n.logoutputencoding UTF-8
125 test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
126 compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
129 for H in eucJP ISO-2022-JP
131 test_expect_success "$H should be shown in UTF-8 now" '
132 compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
134 done
136 for J in eucJP ISO-2022-JP
138 if test "$J" = ISO-2022-JP
139 then
140 ICONV=$J
141 else
142 ICONV=
144 git config i18n.logoutputencoding $J
145 for H in eucJP ISO-2022-JP
147 test_expect_success "$H should be shown in $J now" '
148 compare_with '$H' "$TEST_DIRECTORY"/t3900/'$J'.txt $ICONV
150 done
151 done
153 for H in ISO8859-1 eucJP ISO-2022-JP
155 test_expect_success "No conversion with $H" '
156 compare_with "--encoding=none '$H'" "$TEST_DIRECTORY"/t3900/'$H'.txt
158 done
160 test_commit_autosquash_flags () {
161 H=$1
162 flag=$2
163 test_expect_success "commit --$flag with $H encoding" '
164 git config i18n.commitencoding $H &&
165 git checkout -b $H-$flag C0 &&
166 echo $H >>F &&
167 git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
168 test_tick &&
169 echo intermediate stuff >>G &&
170 git add G &&
171 git commit -a -m "intermediate commit" &&
172 test_tick &&
173 echo $H $flag >>F &&
174 git commit -a --$flag HEAD~1 &&
175 E=$(git cat-file commit '$H-$flag' |
176 sed -ne "s/^encoding //p") &&
177 test "z$E" = "z$H" &&
178 git config --unset-all i18n.commitencoding &&
179 git rebase --autosquash -i HEAD^^^ &&
180 git log --oneline >actual &&
181 test_line_count = 3 actual
185 test_commit_autosquash_flags eucJP fixup
187 test_commit_autosquash_flags ISO-2022-JP squash
189 test_done