2015-09-30 42 views
2

我注意到了一些模式,開發人員傾向於這樣做 - 就像在其中留下fdescribefit(這意味着只有一個測試/套件將運行)進行javascript測試,通常是在審查階段發現的,但它會很好早點趕上那些小事。想知道是否有一種方法來配置git以防止在更改中檢測到某種模式時提交?如何防止檢測到更改中的特定短語時git commit?

+0

pre-commit鉤子可以做這類事情,送你在正確的道路上 –

+0

我建議在提交前的,但推前鉤不這樣做; git鼓勵開發人員經常提交,但他們不應該將所有內容都推送到共享庫。 –

回答

4

這是一個經典的git commit hook(man githooks);例如,從.git/hooks中獲得的標準腳本示例中,可以將腳本pre-push添加到該文件夾​​(使其可執行!)。

從Git項目此示例腳本只檢查提交的名字,但如果你的東西更換

git rev-list 

線像

git diff $remote_sha $local_sha 

和grep爲您suspcious字符串,例如

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