Last active
December 16, 2015 13:38
-
-
Save jishnu7/5442705 to your computer and use it in GitHub Desktop.
Pre-commit script for JSHint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# author: [email protected] | |
# Pre-commit hook passing files through jshint | |
# Temperory staging area | |
TMP_STAGING=".tmp_staging" | |
# absolute path of this repo | |
ROOT_DIR=$(git rev-parse --show-toplevel) | |
# path of your jshint config | |
CONF="--config=${ROOT_DIR}/tools/jshintrc" | |
# jshint path | |
JSHINT=$(which jshint) | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# If initial commit, diff against an empty tree object | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
# retrieve all files in staging area that are added, modified or renamed | |
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- | egrep \.js$) | |
echo $FILES | |
if [ "$FILES" == "" ]; then | |
exit 0 | |
fi | |
# create temporary copy of staging area | |
if [ -e $TMP_STAGING ]; then | |
rm -rf $TMP_STAGING | |
fi | |
mkdir $TMP_STAGING | |
STAGED_FILES="" | |
for FILE in $FILES | |
do | |
ID=$(git diff-index --cached HEAD $FILE | cut -d " " -f4) | |
# create staged version of file in temporary staging area with the same | |
# path as the original file so that the phpcs ignore filters can be applied | |
mkdir -p "$TMP_STAGING/$(dirname $FILE)" | |
git cat-file blob $ID > "$TMP_STAGING/$FILE" | |
STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE" | |
done | |
for file in $STAGED_FILES; do | |
$JSHINT $file ${CONF} | |
RETVAL=$? | |
if [ $RETVAL -gt 0 ]; then | |
break | |
else | |
echo "jslint passed ${file}" | |
fi | |
done | |
# delete temporary copy of staging area | |
rm -rf $TMP_STAGING | |
if [ $RETVAL -gt 0 ]; then | |
exit 1 | |
else | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment