运行上面的脚本会导致各种错误。我也不确定你为什么执行^[a-z0-9-]+$
并将结果存储在valid_chars
中。尽管如此:
- 你可能想用一个错误来退出,如果分支名称不匹配正则表达式
- 您缺少
valid_chars
一个$
前缀测试
if [[ "$local_branch" =~ valid_chars]]; then
应该有内部的空间]]
一如往常,确保脚本是.git/hooks/pre-push
下,正确命名,并被标记为可执行文件。
我下面的作品(我已经离开了,因为我很懒样品钩评论):
#!/bin/bash
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_chars="^[a-z0-9-]+$"
message='...'
if [[ ! $local_branch =~ $valid_chars ]]
then
echo "$message"
exit 1
fi
exit 0
,你究竟有什么问题? – Whymarrh 2015-04-01 22:12:31
@Whymarrh我已经添加了我的代码,但我似乎无法得到它当我混帐推动起源 –
2015-04-01 22:19:19
[ShellCheck](http://www.shellcheck.net/)是你的朋友在未来。 – Whymarrh 2015-04-01 23:13:11