Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created January 26, 2010 13:46
Show Gist options
  • Save trodrigues/286845 to your computer and use it in GitHub Desktop.
Save trodrigues/286845 to your computer and use it in GitHub Desktop.
# The following snippet checks your about to be committed js files with jslint and asks you what do to.
#
# You'll need either CocoaDialog (you can get it with macports) or plain old curses dialog installed.
# Using the shell's read wouldn't cut it because you have no stdin when running git hooks.
# Zenity would also be an option but I haven't considered it.
# You can get instructions for installing Rhino here http://www.nakedjavascript.com/installing-rhino
# You can get a jslint Rhino ready version here: http://www.jslint.com/rhino/index.html
# Paths are configured for my machine, so tweak them at your own taste.
# Tested on Linux and Mac OS X
exitmsg="Ok, exiting then."
commitquestion="Are you sure you want to commit ?"
jslint="java -jar /Users/trodrigues/Code/javascript/rhino1_7R2/js.jar /Users/trodrigues/Code/javascript/jslint.js"
cocoadialog="/opt/local/var/macports/software/CocoaDialog/2.1.1_0/Applications/MacPorts/CocoaDialog.app/Contents/MacOS/CocoaDialog"
dialog=`which dialog`
for i in `git diff --cached|grep \^diff|grep \.js$|cut -c 14-|cut -d " " -f 1` ; do
echo "checking $i with jslint"
lintoutput=`$jslint $i|wc -l`
echo $lintoutput
if [ $lintoutput -gt 0 ] ; then
$jslint $i
# try cocoa dialog
if [ -f "$cocoadialog" ] ; then
commit=`$cocoadialog ok-msgbox --text "$commitquestion"`
if [ "$commit" = "2" ] ; then
echo $exitmsg
exit 1
fi
fi
# try old school unix dialog
if [ -f "$dialog" ] ; then
$dialog --yesno "$commitquestion" 100 200
if [ $? -gt 0 ] ; then
echo $exitmsg
exit 1
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment