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/
JSLint with SpiderMonkey
The following is a variation of @Bitbieger's Git solution that works with Node.js and a local copy of node-jslint (i.e. you need to
npm install jslint
in your root repository directory).Additionally the script:
--indent 4 --white true
jslint options to ensure source code consistencyTo get it to work copy the following to
.git/hooks/pre-commit
and don't forget tochmod +x .git/hooks/pre-commit
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 currentHEAD
.