小赖子的英国生活和资讯

git 小技巧: 如何通过pre-push hooks避免向主分支提交代码?

阅读 桌面完整版

有时候无意的提交代码 push 到本地主/开发分支时, 你有没有想过把头撞到墙上? 一般这种情况下 我们能通过 `git reset HEAD files` 来取消提交 (unstage), 但是, 这还是挺让人不爽的.

还好, git 提供了很多 hooks 挂钩, 这些挂钩可以在事件完成前后做些检查, 其中 pre-push 就用于在推送代码前进行检查, 当脚本返回1时, 那么动作将停止, 而返回0时则表示一切正常可以继续提交.

pre-push 是 BASH脚本, 在每个代码仓库下的 一个隐藏文件夹 .git/hooks 下. 其中有一个 pre-push 的样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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><local sha1> <remote ref> </remote><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
#!/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><local sha1> <remote ref> </remote><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

我们可以通过下面的脚本检查 当前是否在主分支上, 主分支可以自定义: 比如 master, origin/develop, develop 等. 以下脚本需要通过命令 chmod +x pre-push 让其可执行. 当当前分支是主分支时, 则返回1, 否则返回0表示正常继续.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
# run `chmod +x .git/hooks/pre-push` (We make the hook executable by using the chmod utility.)
# To bypass this hook and force the push, use 'git push --no-verify'
 
master_branches=('origin/develop' 'master' 'develop')
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
 
for branch in "${master_branches[@]}"
do
    if [ $branch = $current_branch ]; then
        echo "You're about to push $branch and you don't want that"
        exit 1
    fi
done
exit 0
#!/bin/bash
# run `chmod +x .git/hooks/pre-push` (We make the hook executable by using the chmod utility.)
# To bypass this hook and force the push, use 'git push --no-verify'

master_branches=('origin/develop' 'master' 'develop')
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

for branch in "${master_branches[@]}"
do
    if [ $branch = $current_branch ]; then
        echo "You're about to push $branch and you don't want that"
        exit 1
    fi
done
exit 0

使用例子:

1
2
3
4
5
$ git branch
* master
$ git push origin master 
You're about to push master and you don't want that
error: failed to push some refs to 'https://github.com/DoctorLai/ACM.git'
$ git branch
* master
$ git push origin master 
You're about to push master and you don't want that
error: failed to push some refs to 'https://github.com/DoctorLai/ACM.git'

由于每个代码仓库中含有一份 pre-push, 您可以通过以下命令把根目录下的 pre-push 给复制到每一个子代码仓库中.

1
find . -type d -name "hooks" | grep ".git/hooks" | xargs echo cp pre-push
find . -type d -name "hooks" | grep ".git/hooks" | xargs echo cp pre-push

Windows 下的代码仓库应该用的也是 bash 语言编写的 pre-push 脚本, 否则, 以下 windows 批处理可能可以用.

@echo off
REM pre-push.cmd
REM To bypass this hook and force the push, use 'git push --no-verify'

setlocal enabledelayedexpansion
set branches=origin^/develop master develop

for /f "tokens=2" %%i in ('git branch') do (set current=%%i)
for %%j in (%branches%) do (
    if /i "%%j"=="%current%" (
        echo You're about to push to %%j, you don't want that
        exit /b 1
    )
)
endlocal
exit /b 0

Git 还有其它的 Hooks 检查, 比如: git 小技巧: 通过pre-commit hook 来防止程序员把其它测试给禁用了

英文: How to Prevent Commiting to master/develop branch by Accidents using Pre-push Hooks in Git?

–EOF (The Ultimate Computing & Technology Blog) —

强烈推荐

微信公众号: 小赖子的英国生活和资讯 JustYYUK

阅读 桌面完整版
Exit mobile version