Created
June 21, 2017 10:23
-
-
Save derlarsen/1de6f1fcd6d496e4208ff56c7eefa58d to your computer and use it in GitHub Desktop.
Pre-commit hook for ESLinting that works with sourcetree
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/sh | |
# | |
# An example hook script to verify what is about to be committed. | |
# Called by "git commit" with no arguments. The hook should | |
# exit with non-zero status after issuing an appropriate message if | |
# it wants to stop the commit. | |
# | |
# To enable this hook, rename this file to "pre-commit". | |
HAS_NODE=`which node 2> /dev/null || which nodejs 2> /dev/null || which iojs 2> /dev/null` | |
# | |
# There are some issues with Source Tree because paths are not set correctly for | |
# the given environment. Sourcing the bash_profile seems to resolve this for bash users, | |
# sourcing the zshrc for zshell users. | |
# | |
# https://answers.atlassian.com/questions/140339/sourcetree-hook-failing-because-paths-don-t-seem-to-be-set-correctly | |
# | |
function source_home_file { | |
file="$HOME/$1" | |
[[ -f "${file}" ]] && source "${file}" | |
} | |
source_home_file ".bash_profile" || source_home_file ".zshrc" || true | |
# | |
# Determine node.js binary name and location | |
# | |
NODE=`which node 2> /dev/null` | |
NODEJS=`which nodejs 2> /dev/null` | |
IOJS=`which iojs 2> /dev/null` | |
LOCAL="/usr/local/bin/node" | |
BINARY= | |
# | |
# Figure out which binary we need to use for our script execution. | |
# | |
if [[ -n "$NODE" ]]; then | |
NODE_BINARY="$NODE" | |
elif [[ -n "$NODEJS" ]]; then | |
NODE_BINARY="$NODEJS" | |
elif [[ -n "$IOJS" ]]; then | |
NODE_BINARY="$IOJS" | |
elif [[ -x "$LOCAL" ]]; then | |
NODE_BINARY="$LOCAL" | |
fi | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
eslint=$(PATH="./node_modules/.bin:$PATH" which eslint) | |
if [[ -e "$eslint" ]] | |
then | |
jsfiles=$(git diff-index --cached --name-only --diff-filter=ACM $against | grep '.js$') | |
if [[ -n "$jsfiles" ]] | |
then | |
eslintOutput=$($NODE_BINARY node_modules/eslint/bin/eslint.js -f stylish -o .eslint.log $jsfiles) | |
if [[ $? != 0 ]] | |
then | |
cat .eslint.log | |
exit 1 | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment