我终于设置了一个“提交后”钩子,对存储库进行 rsync。
/var/svn/myproject/hooks/post-commit:
REPOS="$1"
REV="$2"
#"$REPOS"/hooks/mailer.py commit "$REPOS" $REV "$REPOS"/mailer.conf
if [ $# -ge 1 ]; then
/var/svn/.hooks/rsync-to-redmine "$REPOS" "$REV"
else
echo "ERROR: No repository given"
fi
/var/svn/myproject/hooks/post-commit:
#!/bin/bash
# Configuration
rsyncExecutable="/usr/bin/rsync"
localRepositoryBasePath="/var/svn/"
remoteUser="svnsync"
remoteHost="redmine.mycompany.com"
remoteRepositoryBasePath="/var/svn/"
privateKeyFilePath="/var/svn/.hooks/rsync-to-redmine-certFile"
# /Configuration
repositoryName=`basename "$1"`
if [ -z $repositoryName ]; then # No repository name given
echo "ERROR: No repository name given"
else
if [ -d "$localRepositoryBasePath$repositoryName/" ]; then # Given repository name seems to exists
repositoryDirectoryRealpath=`realpath "$localRepositoryBasePath$repositoryName"`/ # Compute realpath to validate directory (to protect against $1 setted to ".")
if [ "$repositoryDirectoryRealpath" != "$localRepositoryBasePath" ]; then # Directory is not the base path (otherwise we would rsync the entire $localRepositoryBasePath directory
#TODO: Check that $repositoryDirectoryRealpath is under $localRepositoryBasePath
$rsyncExecutable -rltgoDvz -e "ssh -i $privateKeyFilePath" "$repositoryDirectoryRealpath" "$remoteUser@$remoteHost:$remoteRepositoryBasePath$repositoryName"
else
echo "ERROR: Trying to rsync the whole '$localRepositoryBasePath' base directory"
fi
else
echo "ERROR: Directory '$localRepositoryBasePath$repositoryName' doesn't exists"
fi
fi
当有人在 SVN 存储库上提交时,rsync 程序会将 $remoteHost 上的修改推送到 $remoteRepositoryBasePath 目录中。