git如何设置master分支的权限_GIT:我怎样才能阻止foxtrot在我的'master'分支中合并?...

本文介绍了如何使用Git的预推送(pre-push)和后合并(post-merge)钩子来检测并阻止foxtrot类型的合并到master分支。通过自定义的foxtrot-merge-detector脚本,可以在push前检查合并操作,如果检测到foxtrot合并则阻止推送,并在post-merge时给出警告。
摘要由CSDN通过智能技术生成

我写这篇文章是为了尽早提供反馈(pre-receive也需要钩子)。我用post-merge它来pre-push挂钩。在commit-msg钩子中阻止foxtrot合并是不可能的,因为检测到的信息只能在之后才可用。在post-merge钩,我只是警告。在pre-push钩子中,我抛出并阻止推动。请参阅d3f1821(“foxtrot:添加子钩以检测foxtrot merges”,2017-08-05)。

〜/ git的钩/助手/狐步合并检测器:

#!/bin/sh

#usage:

#   foxtrot-merge-detector []

#

# If foxtrot merge detected for branch (current branch if no branch),

# exit with 1.

# foxtrot merges:

# See http://bit-booster.blogspot.cz/2016/02/no-foxtrots-allowed.html

# https://stackoverflow.com/questions/35962754/git-how-can-i-prevent-foxtrot-merges-in-my-master-branch

remoteBranch=$(git rev-parse --abbrev-ref "$1"@{u} 2>/dev/null)

# no remote tracking branch, exit

if [[ -z "$remoteBranch" ]]; then

exit 0

fi

branch=$(git rev-parse --abbrev-ref "${1-@}" 2>/dev/null)

# branch commit does not cover remote branch commit, exit

if ! $(git merge-base --is-ancestor $remoteBranch $branch); then

exit 0

fi

remoteBranchCommit=$(git rev-parse $remoteBranch)

# branch commit is same as remote branch commit, exit

if [[ $(git rev-parse $branch) == $remoteBranchCommit ]]; then

exit 0

fi

# remote branch commit is first-parent of branch, exit

if [[ $(git log --first-parent --pretty='%P' $remoteBranchCommit..$branch | \

cut -d' ' -f1 | \

grep $remoteBranchCommit | wc -l) -eq 1 ]]; then

exit 0

fi

# foxtrot merge detected if here

exit 1

然后用它作为

预推钩:

#!/bin/sh

remote="$1"

url="$2"

z40=0000000000000000000000000000000000000000

while read local_ref local_sha remote_ref remote_sha

do

if [ "$local_sha" = $z40 ]; then

# handle delete, do nothing

:

else

# ex $local_ref as "refs/heads/dev"

branch=$(git rev-parse --abbrev-ref "$local_ref")

~/.git-hooks/helpers/foxtrot-merge-detector "$branch"

# check exit code and exit if needed

exitcode=$?

if [ $exitcode -ne 0 ]; then

echo 1>&2 "fatal: foxtrot merge detected, aborting push"

echo 1>&2 "fatal: branch $branch"

exit $exitcode

fi

fi

done

后合并:

#!/bin/sh

~/.git-hooks/helpers/foxtrot-merge-detector

# check exit code and exit if needed

exitcode=$?

if [ $exitcode -ne 0 ]; then

echo -e "  ${Yellow}WARNING:${None} foxtrot merge detected"

# swallow exit code

fi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值