2011-12-08 35 views
0

%% 問題解決及以下行爲的代碼如預期 %%爲什麼從SVN事務中讀取iconv而不是從SVN版本讀取時,iconv會退出?

我想寫一個SVN預先承諾中的Bash鉤,用於測試輸入的文件爲UTF-8編碼。經過大量的字符串調整以獲取傳入文件的路徑並忽略dirs/pictures/deleted文件等,我使用'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 

的問題是,每次我嘗試提交文件與斯堪的納維亞的字符,如「æøå」的iconv返回一個錯誤(1號出口)的時間。

如果我禁用腳本,將文件提交爲「æøå」,將「svnlook -t」和「svnlook cat -t」中的-t(事務)更改爲-r(修訂版),然後運行腳本手動使用「文件」文件的版本號,然後iconv(並且因此腳本)返回出口0.並且everthing是花花公子。

爲什麼svnlook cat -r按預期工作(返回UTF-8編碼的字符串),但不是svnlook cat -t?

回答

0

問題是,如果沒有選擇輸出編碼,iconv顯然會表現出意外。

更改

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

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

解決了這個問題,並提出了腳本像預期的那樣:)

相關問題