運行上面的腳本會導致各種錯誤。我也不確定你爲什麼執行^[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