如果检测到更改中的特定短语,如何防止 git 提交?
How to prevent git commit if specific phrase in changes is detected?
我注意到一些模式,开发人员倾向于这样做 - 比如提交 javascript 测试时留下 fdescribe
或 fit
(这意味着只有一个 test/suite 会是 运行),通常是在复习阶段发现的,但如果能早点抓住这些小东西就好了。想知道是否有一种方法可以配置 git 以防止在更改中检测到某些模式时提交?
这是 git 提交挂钩 (man githooks
) 的经典工作;例如,根据您在 .git/hooks
中获得的标准脚本示例,您可以将脚本 pre-push
添加到该文件夹(使其可执行!)。
来自 git 项目的示例脚本仅检查提交名称,但如果您替换
git rev-list
符合
git diff $remote_sha $local_sha
并用 grep 查找您的可疑字符串,例如
git diff $remote_sha $local_sha|grep -E '^\+.*(fdescribe|fit)'
你可以为你的情况做到这一点。
#!/bin/sh
# 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:
#
# -- Name of the remote to which the push is being done
# -- 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).
remote=""
url=""
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
if [ -n "$commit" ]
then
echo >&2 "Found WIP commit in $local_ref, not pushing"
exit 1
fi
fi
done
exit 0
我注意到一些模式,开发人员倾向于这样做 - 比如提交 javascript 测试时留下 fdescribe
或 fit
(这意味着只有一个 test/suite 会是 运行),通常是在复习阶段发现的,但如果能早点抓住这些小东西就好了。想知道是否有一种方法可以配置 git 以防止在更改中检测到某些模式时提交?
这是 git 提交挂钩 (man githooks
) 的经典工作;例如,根据您在 .git/hooks
中获得的标准脚本示例,您可以将脚本 pre-push
添加到该文件夹(使其可执行!)。
来自 git 项目的示例脚本仅检查提交名称,但如果您替换
git rev-list
符合
git diff $remote_sha $local_sha
并用 grep 查找您的可疑字符串,例如
git diff $remote_sha $local_sha|grep -E '^\+.*(fdescribe|fit)'
你可以为你的情况做到这一点。
#!/bin/sh
# 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:
#
# -- Name of the remote to which the push is being done
# -- 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).
remote=""
url=""
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
range="$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
if [ -n "$commit" ]
then
echo >&2 "Found WIP commit in $local_ref, not pushing"
exit 1
fi
fi
done
exit 0