051ea9d3c289d825601aed0a3055b1565370dc3a
   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_failure 'UTF-16 refused because of NULs' '
  38        echo UTF-16 >F &&
  39        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        grep "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        grep "did not conform" "$HOME"/stderr
  59'
  60
  61for H in ISO8859-1 eucJP ISO-2022-JP
  62do
  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
  68        '
  69done
  70
  71for H in ISO8859-1 eucJP ISO-2022-JP
  72do
  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'"
  76        '
  77done
  78
  79test_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
  89'
  90
  91test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
  92        compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
  93'
  94
  95for H in eucJP ISO-2022-JP
  96do
  97        test_expect_success "$H should be shown in UTF-8 now" '
  98                compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
  99        '
 100done
 101
 102test_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"
 110        fi
 111'
 112
 113for H in ISO8859-1 eucJP ISO-2022-JP
 114do
 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
 118        '
 119done
 120
 121test_expect_success 'config to tweak customization' '
 122        git config i18n.logoutputencoding UTF-8
 123'
 124
 125test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
 126        compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
 127'
 128
 129for H in eucJP ISO-2022-JP
 130do
 131        test_expect_success "$H should be shown in UTF-8 now" '
 132                compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
 133        '
 134done
 135
 136for J in eucJP ISO-2022-JP
 137do
 138        if test "$J" = ISO-2022-JP
 139        then
 140                ICONV=$J
 141        else
 142                ICONV=
 143        fi
 144        git config i18n.logoutputencoding $J
 145        for H in eucJP ISO-2022-JP
 146        do
 147                test_expect_success "$H should be shown in $J now" '
 148                        compare_with '$H' "$TEST_DIRECTORY"/t3900/'$J'.txt $ICONV
 149                '
 150        done
 151done
 152
 153for H in ISO8859-1 eucJP ISO-2022-JP
 154do
 155        test_expect_success "No conversion with $H" '
 156                compare_with "--encoding=none '$H'" "$TEST_DIRECTORY"/t3900/'$H'.txt
 157        '
 158done
 159
 160test_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
 182        '
 183}
 184
 185test_commit_autosquash_flags eucJP fixup
 186
 187test_commit_autosquash_flags ISO-2022-JP squash
 188
 189test_done