Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果检测到更改中的特定短语,如何防止git提交?_Git_Atlassian Sourcetree_Bitbucket Server - Fatal编程技术网

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

如果检测到更改中的特定短语,如何防止git提交?,git,atlassian-sourcetree,bitbucket-server,Git,Atlassian Sourcetree,Bitbucket Server,我注意到了一些模式,开发人员倾向于这样做——比如提交javascript测试时,只保留fdescribe或fit(这意味着只有一个测试/套件将运行),这通常是在审查阶段发现的,但最好早点抓住这些小东西。想知道如果在更改中检测到某种模式,是否有办法配置git以防止提交 这是git提交挂钩的经典作业(ManGitHooks);例如,从.git/hooks中获得的标准脚本示例中,可以将脚本预推添加到该文件夹中(使其可执行!) 这个来自git项目的示例脚本只检查提交名称,但是如果替换 git rev-l

我注意到了一些模式,开发人员倾向于这样做——比如提交javascript测试时,只保留
fdescribe
fit
(这意味着只有一个测试/套件将运行),这通常是在审查阶段发现的,但最好早点抓住这些小东西。想知道如果在更改中检测到某种模式,是否有办法配置git以防止提交

这是git提交挂钩的经典作业(
ManGitHooks
);例如,从
.git/hooks
中获得的标准脚本示例中,可以将脚本
预推
添加到该文件夹中(使其可执行!)

这个来自git项目的示例脚本只检查提交名称,但是如果替换

git rev-list
用类似

git diff $remote_sha $local_sha
和格雷普,你的可疑字符串,例如

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”
#在检查远程状态后,但在执行任何操作之前,按下“按钮”
#推。如果此脚本以非零状态退出,则不会推送任何内容。
#
#使用以下参数调用此挂钩:
#
#$1--正在向其进行推送的远程设备的名称
#$2—正在进行推送的URL
#
#如果不使用命名的远程设备进行推送,则这些参数将相等。
#
#有关正在推送的提交的信息作为行提供给
#表格中的标准输入:
#
#      
#
#此示例显示如何在日志消息开始时防止推送提交
#使用“在制品”(在制品)。
remote=“$1”
url=“$2”
z40=0000000000000000000000000000
读取本地\u ref本地\u sha远程\u ref远程\u sha时
做
如果[“$local_sha”=$z40]
然后
#句柄删除
:
其他的
如果[“$remote_sha”=$z40]
然后
#新建分支,检查所有提交
range=“$local_sha”
其他的
#更新现有分支,检查新提交
range=“$remote\u sha..$local\u sha”
fi
#检查WIP提交
提交=`git rev list-n1--grep'^WIP'$range“`
如果[-n“$commit”]
然后
echo>&2“在$local\u ref中找到WIP提交,未推送”
出口1
fi
fi
完成
出口0

pre-commit钩子可以做这些事情,为了让你走上正确的道路,我建议不要在pre-commit钩子中这样做,而是在pre-push钩子中这样做;git鼓励开发人员经常提交,但他们不应该将所有内容都推送到共享回购。推送新分支时,这不起作用:
git diff$local\u sha
不返回任何内容