baniol
4/11/2014 - 9:01 AM

jshint, git hook, python script

jshint, git hook, python script

#!/usr/bin/env python

import os, sys

"""
Checks your git commit with JSHint. Only checks staged files
"""
def jshint():
    
    errors = []
    
    # get all staged files
    f = os.popen('git diff-index --name-only --cached HEAD')
    
    for file in f.readlines():
        # you may have to change path to jshint!
        # global options stored in ~/.jshintrc
        g = os.popen('/usr/local/bin/jshint ' + file)
        
        # add all errors from all files together
        for error in g.readlines():
            errors.append(error)
    
    # got errors?
    if errors:
        for i, error in enumerate(errors):
            print error,

        # Abort the commit
        sys.exit(1)
    
    # All good
    sys.exit(0)
    
if __name__ == '__main__':
    jshint()
{
    "devel": false,
    "undef": true
}