2011-12-08 4 views
0

%% 問題は解決し %%SVNリビジョンからではなくSVNトランザクションから読み込むと、iconvがエラーで終了するのはなぜですか?

期待通りに以下のコードは、私がUTF-8エンコーディングのための受信ファイルをテストバッシュでSVN事前にコミットフックを書くしようとして機能します。受信ファイルのパスを取得し、ディレクトリ/画像/削除されたファイルなどを無視するためにたくさんの文字列をジャグリングした後、私は 'svnlook cat'を使用して受信ファイルを読み込み、 'iconv -f UTF-8'にパイプします。この後、$ {PIPESTATUS [1]}でiconv操作の終了ステータスを読みます。

私のコードは次のようになります。

REPOS="$1" 
TXN="$2" 

SVNLOOK=/usr/bin/svnlook 
ICONV=/usr/bin/iconv 

# The file endings to ignore when checking for UTF-8: 
IGNORED_ENDINGS=(png jar) 

# Prepairing to set the IFS (Internal Field Separator) so "for CHANGE in ..." will iterate 
# over lines instead of words 
OIFS="${IFS}" 
NIFS=$'\n' 

# Make sure that all files to be committed are encoded in UTF-8 
IFS="${NIFS}" 

for CHANGE in $($SVNLOOK changed -t "$TXN" "$REPOS"); do 
    IFS="${OIFS}" 
    # Skip change if first character is "D" (we dont care about checking deleted files) 
    if [ "${CHANGE:0:1}" == "D" ]; then 
     continue 
    fi 

    # Skip change if it is a directory (directories don't have encoding) 
    if [ "${CHANGE:(-1)}" == "/" ]; then 
     continue 
    fi 

    # Extract file repository path (remove first 4 characters) 
    FILEPATH=${CHANGE:4:(${#CHANGE}-4)} 

    # Ignore files that starts with "." like ".classpath" 
    IFS="//" # Change seperator to "/" so we can find the file in the file path 
    for SPLIT in $FILEPATH 
    do 
     FILE=$SPLIT 
    done 
    if [ "${FILE:0:1}" == "." ]; then 
     continue 
    fi 
    IFS="${OIFS}" # Reset Internal Field Seperator 

    # Ignore files that are not supposed to be checked, like images. (list defined in IGNORED_ENDINGS field above) 
    IFS="." # Change seperator to "." so we can find the file ending 
    for SPLIT in $FILE 
    do 
     ENDING=$SPLIT 
    done 
    IFS="${OIFS}" # Reset Internal Field Seperator 
    IGNORE="0" 
    for IGNORED_ENDING in ${IGNORED_ENDINGS[@]} 
    do 
     if [ `echo $IGNORED_ENDING | tr [:upper:] [:lower:]` == `echo $ENDING | tr [:upper:] [:lower:]` ] # case insensitive compare of strings 
     then 
      IGNORE="1" 
     fi 
    done 
    if [ "$IGNORE" == "1" ]; then 
     continue 
    fi 

    # Read changed file and pipe it to iconv to parse it as UTF-8 
    $SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -t UTF-16 -o /dev/null 

    # If iconv exited with a non-zero value (error) then return error text and reject commit 
    if [ "${PIPESTATUS[1]}" != "0" ]; then 
     echo "Only UTF-8 files can be committed (violated in $FILEPATH)" 1>&2 
     exit 1 
    fi 
    IFS="${NIFS}" 
done 

IFS="${OIFS}" 

# All checks passed, so allow the commit. 
exit 0 

問題は、私はエラー(出口1)を返すのiconv「æøå」のような北欧の文字を含むファイルをコミットしようとするたびです。

"æøå"でファイルをコミットし、 "svnlook -t"と "svnlook cat -t"の-t(トランザクション)を-r(リビジョン)に変更し、スクリプトを実行します手動で "æøå"ファイルのリビジョン番号を指定すると、iconv(そしてそのためにスクリプト)はexit 0を返します。

なぜsvnlook cat -rは期待どおりに動作しますか(UTF-8でエンコードされた "æøå"文字列を返します)、svnlook cat -t?

答えて

0

出力エンコーディングが選択されていないとiconvが予期せず動作するという問題がありました。

$SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -t UTF-16 -o /dev/null 

$SVNLOOK cat -t "$TXN" "$REPOS" "$FILEPATH" | $ICONV -f UTF-8 -o /dev/null 

を変更

は、問題を解決し、そして期待どおりに動作するスクリプトを作った:)

関連する問題