2012-06-06 59 views
2

當客戶端推送到遠程git存儲庫(裸)我想要一個掛鉤,自動運行JSHint對傳入更改的文件,並拒絕提交如果JSHint返回錯誤。我只關心確保主分支符合我們的JSHint配置。所以我有這個腳本:JSHint在Git推(更新掛鉤)

#!/bin/bash 

# --- Command line 
refname="$1" 
oldrev="$2" 
newrev="$3" 
branch=${refname#refs/heads/} 

echo ${refname} 
echo ${oldrev} 
echo ${newrev} 
echo ${branch} 

if [ "$branch" == "master" ] 
then 
    echo "Need to JSHint" >&2 
    exit 1 
fi 

# Not updating master 
exit 0 

我想我有兩個問題:

  1. 我如何已經在推動改變的文件列表?
  2. 如何將這些文件傳遞給JSHint?
+0

其他解決方案是[pre-commit hook] [1]。 [1]:http://stackoverflow.com/questions/15703065/github-setup-pre-commit-hook-jshint/21238963#21238963 – igor

回答

4

我不相信這是完成任務的最佳方式。基本上,代碼生成repo中每個JavaScript文件的文件,然後分別調用每個JavaScript文件的JSHint。獎金它實際上使用項目的.jshintrc文件(如果存在)。 Also on Gist

任何建議,指針,備選方案???

#!/bin/bash 

# --- Command line 
refname="$1" 
oldrev="$2" 
newrev="$3" 
branch=${refname#refs/heads/} 

# Make a temp directory for writing the .jshintrc file 
TMP_DIR=`mktemp -d` 
EXIT_CODE=0 

# If commit was on the master branch 
if [ "$branch" == "master" ] 
then 
    # See if the git repo has a .jshintrc file 
    JSHINTRC=`git ls-tree --full-tree --name-only -r HEAD -- | egrep .jshintrc` 

    JSHINT="jshint" 
    if [ -n "$JSHINTRC" ] 
    then 
    # Create a path to a temp .jshintrc file 
    JSHINTRC_FILE="$TMP_DIR/`basename \"$JSHINTRC\"`" 

    # Write the repo file to the temp location 
    git cat-file blob HEAD:$JSHINTRC > $JSHINTRC_FILE 

    # Update the JSHint command to use the configuration file 
    JSHINT="$JSHINT --config=$JSHINTRC_TMP_DIR/$JSHINTRC" 
    fi 

    # Check all of the .js files 
    for FILE in `git ls-tree --full-tree --name-only -r ${newrev} -- | egrep *.js`; do 
    FILE_PATH=`dirname ${FILE}` 
    FULL_PATH=${TMP_DIR}/${FILE_PATH} 
    mkdir -p ${FULL_PATH} 
    git cat-file blob ${newrev}:${FILE} > "$TMP_DIR/$FILE" 
    ${JSHINT} ${TMP_DIR}/${FILE} >&2 
    # Exit status of last command 
    EXIT_CODE=$((${EXIT_CODE} + $?)) 
    if [[ $EXIT_CODE -ne 0 ]] 
    then 
     rm -rf ${TMP_DIR} 
     exit $EXIT_CODE 
    fi 
    done 
    rm -rf ${TMP_DIR} 
fi 

# Not updating master 
exit 0