3

我正在尝试在 Linux Bash 中编写一个 SVN 预提交挂钩脚本,如果无法将文件解析为 UTF-8,它将拒绝提交。

到目前为止,我已经编写了这个脚本:

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

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

# Make sure that all files to be committed are encoded in UTF-8

for FILE in $($SVNLOOK changed -t "$TXN" "$REPOS"); do
    if [$ICONV -f UTF-8 $FILE -o /dev/null]; then
        echo "Only UTF-8 files can be committed ("$FILE")" 1>&2
        exit 1
    fi

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

问题是 iconv 需要提交文件的路径(或其他形式的文本),我不知道如何获取它。

任何人都可以帮忙吗?

4

3 回答 3

3

顺便说一下这个答案有问题!您需要测试 $SVNLOOK 命令 ($?) 的结果,因为指令“exit 1”在子进程中,因此脚本永远不会阻止提交:

#!/bin/bash

REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv

# Make sure that all files to be committed are encoded in UTF-8.
$SVNLOOK changed -t "$TXN" "$REPOS" | while read changeline; do

    # Get just the file (not the add / update / etc. status).
    file=${changeline:4}

    # Only check source files.
    if [[ $file == *.cpp || $file == *.hpp || $file == *.c || $file == *.h ]] ; then
        $SVNLOOK cat -t "$TXN" "$REPOS" $file 2>&1 | iconv -f UTF-8 -t UTF-8 >& /dev/null
        if [ $? -ne 0 ] ; then
            exit 1
        fi
    fi
done
test $? -eq 1 && echo "Only UTF-8 files can be committed" 1>&2 && exit 1
exit 0
于 2014-10-16T09:41:39.480 回答
1

用于svnlook cat从事务中获取文件的内容:

$SVNLOOK cat -t "$TXN" "$REPOS" "$FILE"
于 2011-12-02T10:38:16.523 回答
0

根据原始问题中的脚本和这个答案,这是一个将所有这些放在一起的预提交挂钩:

#!/bin/bash

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

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

# Make sure that all files to be committed are encoded in UTF-8.
$SVNLOOK changed -t "$TXN" "$REPOS" | while read changeline; do

    # Get just the file (not the add / update / etc. status).
    file=${changeline:4}

    # Only check source files.
    if [[ $file == *.cpp || $file == *.hpp || $file == *.c || $file == *.h ]] ; then
        $SVNLOOK cat -t "$TXN" "$REPOS" "$file" 2>&1 | iconv -f UTF-8 -t UTF-8 >& /dev/null
        if [ $? -ne 0 ] ; then
            echo "Only UTF-8 files can be committed ("$file")" 1>&2
            exit 1
        fi
    fi
done

# All checks passed, so allow the commit.
exit 0
于 2013-03-21T18:26:19.777 回答