項目越來越大,人員越來越多陨晶,代碼變動很難通知到每一個項目組成員猬仁,尤其是一些核心配置的變動,故此需要在GIT代碼庫服務(wù)端做控制先誉,客戶端也可以湿刽,但是需要每一個人都在本地進(jìn)行配置,麻煩亦沒必要
核心訴求:
1褐耳、限制成員提交代碼注釋長度
2诈闺、核心配置文件修改需要郵件通知到每一個開發(fā)人員
要解決第一個訴求用到了GIT的pre-receive鉤子,這個鉤子會在客戶端發(fā)起push請求后調(diào)用執(zhí)行铃芦,只有執(zhí)行通過才能合并進(jìn)代碼庫雅镊,話不多說直接上代碼:
```
#!/bin/bash
#pre-receive script
#set -x #for debugging
validate_ref()
{
? ? # --- Arguments
? ? oldrev=$(git rev-parse $1)
? ? newrev=$(git rev-parse $2)
? ? refname="$3"
? ? commitList=`git rev-list $oldrev..$newrev`
? ? ? ? #echo $commitList
? ? split=($commitList)
? ? for s in ${split[@]}
? ? do
? ? ? ? echo "@@@@@@@"
? ? ? ? echo "$s"
? ? ? ? msg=`git cat-file commit $s | sed '1,/^$/d'`
? ? ? ? echo $msg
? ? ? ? if [ ${#msg} -lt 15 ];then
? ? ? ? ? ? echo "!!! Commit message length less than 15"
? ? ? ? ? ? exit 1
? ? ? ? else
? ? ? ? ? ? echo "bigger than 5"
? ? ? ? fi
? ? done
}
fail=""
# Allow dual mode: run from the command line just like the update hook, or
# if no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
? ? # Output to the terminal in command line mode - if someone wanted to
? ? # resend an email; they could redirect the output to sendmail
? ? # themselves
? ? PAGER= validate_ref $2 $3 $1
else
? ? while read oldrev newrev refname
? ? do
? ? ? ? validate_ref $oldrev $newrev $refname
? ? done
fi
if [ -n "$fail" ]; then
? ? exit $fail
fi
```