JSHint on Git Push (Update Hook)

你离开我真会死。 提交于 2019-12-06 11:58:48

问题


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. How do I get the list of files that have been changed in the push?
  2. How can I pass those files to JSHint?

回答1:


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


来源:https://stackoverflow.com/questions/10921050/jshint-on-git-push-update-hook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!