摘要:原文請猛戳說起折騰這件事還是在去年年底。我廠向來木有等流程,全賴老司機們各種強力輸出。所以我便在里用檢查語法錯誤的代碼,把這種坑隊友的事扼殺在搖籃里。雖然不及那么強大,但也基本夠用了。
原文請猛戳:
http://galoisplusplus.coding.me/blog/2016/03/12/svn-precommit-hook/
說起折騰svn hook這件事還是在去年年底。我廠向來木有Code Review等Quality Assurance流程,全賴老司機們各種強力輸出。而當時我們項目的不少老司機要么去了其他部門要么離職了,新來的小鮮肉碼農們似乎對Version Control一無所知。別的不說,單是commit不寫comment,便讓大家頭疼不已,每次在merge時都得額外花不少時間來搞清楚commit的具體內容。雖然我自從投奔git陣營后基本都是用git svn,對svn生疏已久,但覺得加個svn提交限制應非難事,所以便額外花了點時間寫了個pre-commit hook,對commit的comment中的非空白字符做計數,少于一定字數的不讓提交。萬萬沒想到,當時讓SA大大部署上svn服務器后,就有人commit了一個“再試一下”orz...當時我的內心是崩潰的...在推上圍脖上吐槽后不久,眾位大神各種支招,例如巨硬的泉哥說再搞個語義分析器666...不過最受用的還是根爺所提到的用cpplint檢查代碼是否符合編碼規范、是否能夠編譯通過。恰好那段時間小鮮肉們提交了一些帶有語法錯誤的lua代碼,偶們又木有review制度,結果不寫程序的策劃大大們svn up下來后——「什么鬼?腫么不能運行了?!好拙計啊!AAA,快來看bug啊!BBB,SOS!」——導致別人不得不停下手頭的活、額外花時間排查。所以我便在pre-commit hook里用luac檢查語法錯誤的代碼,把這種坑隊友的事扼殺在搖籃里。雖然不及cpplint那么強大,但也基本夠用了。最近一次改這個腳本是因為前段時間有個小鮮肉做了一張4096x4096的圖集,而cocos2d-x文檔里寫得清清楚楚,大多數手機所支持的最大紋理尺寸其實只有2048x2048...臥槽,干得漂亮啊,一上線就造成了不少crash...木有review制度好可啪,在發現這個問題之前其他人一直毫不知情...所以我還是干脆在svn hook里再加個限制吧~
好了,不碎碎念了,這個gist便是我折騰的svn hook,主要做了這么幾項功能:
commit message非空白長度檢查
lua語法檢查
禁止添加文件名帶空格的文件
禁止被配在PROHIBITED_FILES的文件被修改
確保圖片尺寸小于2048x2048
#!/bin/sh # PRE-COMMIT HOOK # # The pre-commit hook is invoked before a Subversion txn is # committed. Subversion runs this hook by invoking a program # (script, executable, binary, etc.) named "pre-commit" (for which # this file is a template), with the following ordered arguments: # # [1] REPOS-PATH (the path to this repository) # [2] TXN-NAME (the name of the txn about to be committed) # # [STDIN] LOCK-TOKENS ** the lock tokens are passed via STDIN. # # If STDIN contains the line "LOCK-TOKENS: " (the " " denotes a # single newline), the lines following it are the lock tokens for # this commit. The end of the list is marked by a line containing # only a newline character. # # Each lock token line consists of a URI-escaped path, followed # by the separator character "|", followed by the lock token string, # followed by a newline. # # The default working directory for the invocation is undefined, so # the program should set one explicitly if it cares. # # If the hook program exits with success, the txn is committed; but # if it exits with failure (non-zero), the txn is aborted, no commit # takes place, and STDERR is returned to the client. The hook # program can use the "svnlook" utility to help it examine the txn. # # On a Unix system, the normal procedure is to have "pre-commit" # invoke other programs to do the real work, though it may do the # work itself too. # # *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT *** # *** FOR REVISION PROPERTIES (like svn:log or svn:author). *** # # This is why we recommend using the read-only "svnlook" utility. # In the future, Subversion may enforce the rule that pre-commit # hooks should not modify the versioned data in txns, or else come # up with a mechanism to make it safe to do so (by informing the # committing client of the changes). However, right now neither # mechanism is implemented, so hook writers just have to be careful. # # Note that "pre-commit" must be executable by the user(s) who will # invoke it (typically the user httpd runs as), and that user must # have filesystem-level permission to access the repository. # # On a Windows system, you should name the hook program # "pre-commit.bat" or "pre-commit.exe", # but the basic idea is the same. # # The hook program typically does not inherit the environment of # its parent process. For example, a common problem is for the # PATH environment variable to not be set to its usual value, so # that subprograms fail to launch unless invoked via absolute path. # If you"re having unexpected problems with a hook program, the # culprit may be unusual (or missing) environment variables. # # Here is an example hook script, for a Unix /bin/sh interpreter. # For more examples and pre-written hooks, see those in # the Subversion repository at # http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and # http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/ LOG="/tmp/svn.log" touch ${LOG} REPOS="$1" TXN="$2" echo "REPOS: $REPOS" > ${LOG} echo "TXN: $TXN" >> ${LOG} SVNLOOK="" # lua compiler LUAC="" # lua file extension LUA_EXT="lua" # png file extension PNG_EXT="png" MSG_MIN_CHAR_NUM=3 MAX_PNG_SIZE=2048 PROHIBITED_FILES=( ) TMP_DIR="/tmp/svn" if [[ -d ${TMP_DIR} ]]; then rm -r ${TMP_DIR} fi mkdir -p ${TMP_DIR} function check_lua_syntax { local lua_file=$1 echo `${LUAC} ${lua_file} 2>&1` } function create_file { local file_name=$1 # Create tmp file and copy content tmp_file="${TMP_DIR}/${file_name}" mkdir -p "$(dirname "${tmp_file}")" && touch "${tmp_file}" ${SVNLOOK} cat -t "${TXN}" "${REPOS}" "${file_name}" > ${tmp_file} } # Make sure that the log message contains some text. commit_msg=`$SVNLOOK log -t "$TXN" "$REPOS" | sed "s/[[:space:]]//g"` echo ${commit_msg} >> ${LOG} if [[ `echo ${commit_msg} | wc -c` -lt ${MSG_MIN_CHAR_NUM} ]]; then echo "Please write a meaningful comment when committing" 1>&2 exit 1 fi changed_info_str=`${SVNLOOK} changed -t "${TXN}" "${REPOS}"` IFS=$" " read -rd "" -a changed_infos <<<"${changed_info_str}" lua_error_msg="" png_error_msg="" for changed_info in "${changed_infos[@]}"; do # Prevent commiting file that contains space in its filename echo ${changed_info} >> ${LOG} operation=`echo ${changed_info} | awk "{print $1}"` if [[ ${operation} = "A" ]] && [[ `echo ${changed_info} | awk "{print NF}"` -gt 2 ]]; then echo "Please do not commit file that contains space in its filename!" 1>&2 exit 1 fi file_name=`echo ${changed_info} | awk "{print $2}"` echo "operation: ${operation}, file: ${file_name}, ext: ${ext}" >> ${LOG} # Check prohibit-commit files for prohibited_file in ${PROHIBITED_FILES[@]}; do if [[ ${file_name} = ${prohibited_file} ]]; then echo "${file_name} is not allowed to be changed!" 1>&2 exit 1 fi done ext=`echo ${file_name} | awk -F"." "{print $NF}"` if [[ ${operation} = "U" ]] || [[ ${operation} = "A" ]]; then tmp_file="${TMP_DIR}/${file_name}" # Check lua syntax if [[ ${ext} = ${LUA_EXT} ]]; then echo "Check syntax of ${tmp_file}" >> ${LOG} create_file ${file_name} error_msg=`check_lua_syntax ${tmp_file}` if [[ `echo ${error_msg} | sed "s/ //g"` != "" ]]; then lua_error_msg="${lua_error_msg} ${error_msg}" fi fi # Check file size if [[ ${ext} = ${PNG_EXT} ]]; then create_file ${file_name} png_info=`file ${tmp_file} | sed "s/,//g"` png_width=`echo ${png_info} | awk "{print $5}" | bc` png_height=`echo ${png_info} | awk "{print $7}" | bc` if [[ ${png_width} -gt ${MAX_PNG_SIZE} ]] || [[ ${png_height} -gt ${MAX_PNG_SIZE} ]]; then png_error_msg="${png_error_msg} ${file_name} is too large: ${png_width} x ${png_height}" fi fi fi done rm -r ${TMP_DIR} if [[ ${lua_error_msg} != "" ]] || [[ ${png_error_msg} != "" ]]; then if [[ ${lua_error_msg} != "" ]]; then echo "lua error: ${lua_error_msg}" >> ${LOG} echo "Please fix the error in your lua program:${lua_error_msg}" 1>&2 fi if [[ ${png_error_msg} != "" ]]; then echo "png error: ${png_error_msg}" >> ${LOG} echo "Please do not commit pictures which are larger than 2048 x 2048:${png_error_msg}" 1>&2 fi exit 1 fi # Check that the author of this commit has the rights to perform # the commit on the files and directories being modified. # commit-access-control.pl "$REPOS" "$TXN" commit-access-control.cfg || exit 1 # All checks passed, so allow the commit. exit 0
PS. 聰哥說作為一位開發去搞svn hook怕被公司的其他開發噴越權管了運維的事,我廠倒是不存在這種問題的——人少活多時間緊,啥活都得攬啊,例如我一位舍友兼同事的大神以前開發手游可是客戶端、服務器、策劃、切圖全都一人搞定,就差自己出美術圖和特效了。最近我也在像運維一樣寫腳本自動化處理某些項目文件,發現我們開發各種混亂啊有木有!例如路徑帶空格,我的一些awk腳本就血崩了——我在以上的hook腳本里也有同樣的問題,其實把列計數方式改一下,例如$7改成${NF-4},可以避免這種問題——而且還要把帶空格的路徑用sed替換成轉義后的字符串。不過這種情況還算好的,還有些人用的單詞有拼寫錯誤,要是全錯了倒也罷了,腳本hold得住,問題是有的對有的錯...偶只能棄療了...
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/7955.html
摘要:背景在公司開發的時候,每次不管還是提交代碼的時候都會對代碼進行簡單的自動檢測。并且明確告知文檔不可能的。猜想應該自動代碼檢測肯定是集成在工程代碼所在的服務端是最好的。寫在最后通過了解代碼的自動檢測,發現了很多問題。 背景 在公司開發的時候,每次不管svn還是git提交代碼的時候都會對代碼進行簡單的自動檢測。檢查不了代碼邏輯,但是最起碼能保證整體的編碼格式保持一致。這個功能還是挺有用的,...
摘要:當退出的錯誤碼不為的時候,表示失敗,操作終止,否則操作繼續。執行命令進行測試,如果測試全部通過的話,退出,錯誤碼為,否則錯誤碼為,同樣退出。這樣雖然沒有解決不會隨著倉庫移動的問題,但也提供了一種在項目組里通用一套的方案。 git hooks想必很多攻城獅都不陌生,官方對于hooks有詳細的文檔,也有站內網友的文章Git Hooks (1):介紹,GIt Hooks (2):腳本分類,說...
閱讀 2003·2021-11-23 10:08
閱讀 2336·2021-11-22 15:25
閱讀 3275·2021-11-11 16:55
閱讀 771·2021-11-04 16:05
閱讀 2600·2021-09-10 10:51
閱讀 711·2019-08-29 15:38
閱讀 1582·2019-08-29 14:11
閱讀 3486·2019-08-29 12:42