views:

453

answers:

4

I want to run JSLint before a commit into either a Mercurial or Git repo is done.

I want this as an automatic step that is set up instead of relying on the developer (mainly me) remembering to run JSLint before-hand. I normally run JSLint while developing, but want to specify a contract on JS files that they pass JSLint before being committed to the repo.

For Mercurial, this page spells out the precommit syntax, but the only variables that seem to be available are the parent1 and parent2 changeset IDs involved in the commit. What I really want are a list of file names that are involved with the commit, so that I can then choose the .js file and run jslint over them.

Similar issue for GIT, the default info available as part of the precommit script seems limited.

What might work is calling hg status/git status as part of the precommit script, parse that output to find JS files then do the work that way. I was hoping for something easier though, and I am not sure if calling status as part of a precommit hook reflect the correct information. For instance in Git if the changes files have not been added yet, but the git commit uses -a, would the files show up in the correct section of the git status output as being part of the commit set?

Update: I got something working, it is visible here: http://github.com/jrburke/dvcs_jslint/

+2  A: 

For git, there are examples in the .git/hooks directory. If you just need the file names for JSLint, you could use git diff --name-only, which in my example will list the names of the files that differ from the current HEAD.

Mike Ashley
Thanks for the hint on the git command. So it still seems that I'll need to use a dvcs command to get the list of files and parse out the files to feed to jslint. At least git diff --name-only will make parsing fairly easy.
jrburke
+1  A: 

JSLint with SpiderMonkey

for js in $(git diff-index --name-only --cached HEAD -- | grep '\.js$'); do
    if jslint.sh $js 2>&1 | grep 'Lint at line' ; then
        echo $js
        exit 1
    else
        echo "js files validated"
        exit 0
    fi  
done
Bitbieger
A: 

Just for completeness, here are some instructions for setting up JSLint as a Subversion Commit Hook.

Dan Zambonini
A: 

Had the same need so I wrote this http://bitbucket.org/robmadole/hgjslint/

Rob Madole