Permalink
Browse files

Adding commit hooks that force tests and lint to pass before committi…

…ng. Make sure to run when cloning/pulling for the first time. Once they are run once, all git hooks will auto-update.
  • Loading branch information...
1 parent c3de9ae commit a9e2a270b41ef76fd1faeafde5569d4ed5ca0be0 @paularmstrong paularmstrong committed Aug 6, 2011
Showing with 61 additions and 1 deletion.
  1. +2 −1 Makefile
  2. +3 −0 scripts/githooks/post-merge
  3. +56 −0 scripts/githooks/pre-commit
View
3 Makefile
@@ -1,5 +1,6 @@
all:
- @echo ''
+ @cp scripts/githooks/* .git/hooks/
+ @chmod -R +x .git/hooks/
test:
@node tests/tests.js
View
3 scripts/githooks/post-merge
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+make
View
56 scripts/githooks/pre-commit
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+function failCommit() {
+ echo "\033[31m----------------------------------------\033[0m"
+ echo "FATAL ERROR: $1"
+ echo "\033[31m----------------------------------------\033[0m"
+ exit 1
+}
+
+function testFail() {
+ echo "\033[33m----------------------------------------\033[0m"
+ echo "$1"
+ echo "\033[33m----------------------------------------\033[0m"
+
+}
+
+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
+
+# Remove all of the trailing whitespace in this commit
+for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -E 's/:[0-9]+:.*//' | uniq` ; do
+ sed -i '' -E 's/[[:space:]]*$//' "$FILE"
+ git add $FILE
+done
+
+echo 'Running JSLint...'
+result=$(make lint)
+if ! grep -q "^0 errors" <<< $result; then
+ num=$(grep "[0-9] error" <<< "$result")
+ testFail "JSLint: $num"
+ echo "$result"
+ echo ''
+ lintFailed=1
+fi
+
+if [[ $lint_errors -gt 0 ]]; then
+ failCommit "Lint Errors"
+fi
+
+echo 'Running Tests...'
+result=$(make test)
+if grep -q FAILURES <<< $result; then
+ num=$(grep "FAILURES" <<< "$result")
+ testFail "Test $num"
+ echo "$result"
+ echo ''
+ testsFailed=1
+fi
+
+if [[ $testsFailed || $lintFailed ]]; then
+ failCommit "Unable To Commit"
+fi

0 comments on commit a9e2a27

Please sign in to comment.