JSHint Git推送(更新钩子) [英] JSHint on Git Push (Update Hook)

查看:93
本文介绍了JSHint Git推送(更新钩子)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当客户端推送到远程git存储库(裸)我想要一个钩子,自动运行JSHint对传入更改的文件,并拒绝提交,如果JSHint返回错误。我只关心确保主分支符合我们的JSHint配置。所以我有这个脚本:

When a client pushes to a remote git repository (bare) I want a hook that automatically runs JSHint on the incoming changed files and rejects the commit if JSHint returns errors. I only care to make sure the master branch is conforming to our JSHint configuration. So I have this script:

#!/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

我想我有两个问题:

I guess I have two questions:


  1. 如何获取已在推送中更改的文件列表?

  2. 如何将这些文件传递给JSHint?


推荐答案

我不相信这是完成任务的最佳方式。基本上,代码会生成repo中每个JavaScript文件的文件,然后分别调用每个JavaScript文件的JSHint。奖金它实际上使用项目的.jshintrc文件(如果存在)。 同样在Gist

I'm not convinced this is the best way to accomplish the task. Basically, the code generates the files of each JavaScript file in the repo and then calls JSHint on each individually. Bonus it actually uses the project's .jshintrc file if one exists. Also on Gist

任何建议,指针,替代方案???

Any suggestions, pointers, alternatives???

#!/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

这篇关于JSHint Git推送(更新钩子)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆