gpt4 book ai didi

git - 如果检测到更改中的特定短语,如何防止 git commit?

转载 作者:太空狗 更新时间:2023-10-29 14:37:30 25 4
gpt4 key购买 nike

我注意到一些模式,开发人员倾向于这样做——比如提交带有 fdescribefit 的 javascript 测试(这意味着只有一个测试/套件将运行),通常是在复习阶段发现的,但如果能早点捕获这些小东西就好了。想知道是否有一种方法可以配置 git 以防止在更改中检测到某些模式时提交?

最佳答案

这是 git commit hooks (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:
#
# $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).

remote="$1"
url="$2"

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

关于git - 如果检测到更改中的特定短语,如何防止 git commit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32866976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com