gpt4 book ai didi

linux - 为什么从 SVN 事务读取而不是从 SVN 修订版读取时 iconv 退出并出错?

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:31 25 4
gpt4 key购买 nike

%% 问题已解决,下面的代码按预期运行 %%

我正在尝试在 Bash 中编写一个 SVN 预提交 Hook ,用于测试传入文件的 UTF-8 编码。经过大量字符串处理来获取传入文件的路径并忽略目录/图片/删除的文件等,我使用“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。一切都很顺利。

为什么 svnlook cat -r 按预期工作(返回 UTF-8 编码的“æøå”字符串),但 svnlook cat -t 不工作?

最佳答案

问题在于,如果未选择输出编码,则 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

解决了问题,并使脚本按预期运行:)

关于linux - 为什么从 SVN 事务读取而不是从 SVN 修订版读取时 iconv 退出并出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8430925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com