经过大量研究,反复试验终于得到了可行的东西!我添加了所有这些评论,以便人们能够理解我的逻辑,因为我仍然相信它可以改进。
编辑 1:创建了一个 gist 来托管文件,以跟踪对其进行的任何改进pre-commit gist
例如,我每个文件访问一次远程仓库。当往返时间很长时,如果提交包含多个 .uasset 文件,那将是非常低效的。相反,在单个 svn 信息请求上批处理所有文件信息请求会很酷。我不知道这是否可能。
此外,我获取 Lock owner 用户名的方式太老套了,但至少它对我的情况有用。无法成功从第二个 grep 的两个单引号之间获取字符串。相反,我匹配的用户名模式当然只适用于我公司使用的所有 svn 用户名格式为 name.lastname 的场景
### Parameters
# Change SVN_USER to the SVN username you configured in subgit
SVN_USER="Firstname.Lastname"
# Remote url. Should end with trunk/ or branches/ . Assumes same folder name on svn and git
SVN_REPO="https://url/path/to/remote/trunk/"
# Currently only checks locks for files with .uasset extension
GREP_FILE_PATTERN_TO_INSPECT="\.uasset$"
# Pattern to match usernames other than your own on the lock error msg
GREP_USERNAME_PATTERN="[[:alpha:]]\+\.[[:alpha:]]\+"
### Start of the hook
# Only to be used for the lock msg. Not necessary...
CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
# leave as 0, used for aborting the commit if a non-owned lock is found
COMMIT_SHOULD_FAIL=0
# Array of relative file paths (to git project) from staged files that should be locked
FILES_TO_LOCK=$(git diff --cached --name-only | grep $GREP_FILE_PATTERN_TO_INSPECT)
for FILE in $FILES_TO_LOCK
do
PATH_TO_FILE_IN_SVN=$SVN_REPO$FILE
# Try to apply a lock. If the file is already locked, grab the lock owner's username
# 2>&1 redirects stderr to stdout, to pipe the error msg into grep for parsing it
# 1st grep would grab part of the error msg printed if the file is already locked
# 2nd grep would grab the lock owner's username with format GREP_USERNAME_PATTERN
LOCK_OWNER=$(svn lock $PATH_TO_FILE_IN_SVN -m "working on ${CURRENT_BRANCH_NAME}" 2>&1 \
| grep -o "already locked by user .\+'" \
| grep -o $GREP_USERNAME_PATTERN)
if [ $LOCK_OWNER ] && [ $LOCK_OWNER != $SVN_USER ] # If someone else locked it
then
echo "Error: File ${FILE} locked by ${LOCK_OWNER}"
COMMIT_SHOULD_FAIL=1
fi
done
if [ $COMMIT_SHOULD_FAIL -eq 1 ] # If at least 1 file was locked by another user
then
echo '--Commit ABORTED--'
exit 1 # Exiting with exit-code 1 makes the entire commit process abort
fi
超级开放的改进!请在评论中建议他们