| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2006 Junio C Hamano |
| 4 | # |
| 5 | |
| 6 | test_description='commit and log output encodings' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | if ! test_have_prereq ICONV |
| 11 | then |
| 12 | skip_all='skipping commit i18n tests; iconv not available' |
| 13 | test_done |
| 14 | fi |
| 15 | |
| 16 | compare_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 | |
| 29 | test_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 | |
| 38 | test_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 | |
| 43 | test_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 | |
| 48 | test_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 | |
| 57 | test_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 | |
| 67 | test_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 | |
| 76 | test_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 | |
| 85 | for H in ISO8859-1 eucJP ISO-2022-JP |
| 86 | do |
| 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 | ' |
| 93 | done |
| 94 | |
| 95 | for H in ISO8859-1 eucJP ISO-2022-JP |
| 96 | do |
| 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 | ' |
| 101 | done |
| 102 | |
| 103 | test_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 | |
| 115 | test_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 | |
| 119 | for H in eucJP ISO-2022-JP |
| 120 | do |
| 121 | test_expect_success "$H should be shown in UTF-8 now" ' |
| 122 | compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt |
| 123 | ' |
| 124 | done |
| 125 | |
| 126 | test_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 | |
| 137 | for H in ISO8859-1 eucJP ISO-2022-JP |
| 138 | do |
| 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 | ' |
| 143 | done |
| 144 | |
| 145 | test_expect_success 'config to tweak customization' ' |
| 146 | git config i18n.logoutputencoding UTF-8 |
| 147 | ' |
| 148 | |
| 149 | test_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 | |
| 153 | for H in eucJP ISO-2022-JP |
| 154 | do |
| 155 | test_expect_success "$H should be shown in UTF-8 now" ' |
| 156 | compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt |
| 157 | ' |
| 158 | done |
| 159 | |
| 160 | for J in eucJP ISO-2022-JP |
| 161 | do |
| 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 |
| 175 | done |
| 176 | |
| 177 | for H in ISO8859-1 eucJP ISO-2022-JP |
| 178 | do |
| 179 | test_expect_success "No conversion with $H" ' |
| 180 | compare_with "--encoding=none '$H'" "$TEST_DIRECTORY"/t3900/'$H'.txt |
| 181 | ' |
| 182 | done |
| 183 | |
| 184 | test_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 | |
| 209 | test_commit_autosquash_flags eucJP fixup |
| 210 | |
| 211 | test_commit_autosquash_flags ISO-2022-JP squash |
| 212 | |
| 213 | test_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 | commit_body HEAD^ >raw && |
| 236 | iconv -f $new -t utf-8 <raw >actual && |
| 237 | test_cmp expect actual |
| 238 | ' |
| 239 | } |
| 240 | |
| 241 | test_commit_autosquash_multi_encoding fixup UTF-8 ISO-8859-1 1-UTF-8.txt |
| 242 | test_commit_autosquash_multi_encoding squash ISO-8859-1 UTF-8 ISO8859-1.txt |
| 243 | test_commit_autosquash_multi_encoding squash eucJP ISO-2022-JP eucJP.txt |
| 244 | test_commit_autosquash_multi_encoding fixup ISO-2022-JP UTF-8 ISO-2022-JP.txt |
| 245 | |
| 246 | test_done |