0

I am using following python code to hook pre commit hook for jshint but when I execute git commit -m "commit comment" the it shows this error

File ".git/hooks/pre-commit", line 29
        print error
                  ^
SyntaxError: invalid syntax

Here is the code

#!/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 --cached --name-only --diff-filter=ACM')

    for file in f.read().splitlines():

        # makes sure we're dealing javascript files
        if file.endswith('.js') and not file.startswith('node_modules/'):       

            g = os.popen('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, #### <----- This is line 29

        # Abort the commit
        sys.exit(1) 

    # All good
    sys.exit(0) 

if __name__ == '__main__':
    jshint()

I don't know anything about python and cannot find what is the correct syntax.

filmor
  • 30,840
  • 6
  • 50
  • 48

1 Answers1

0

If your python installation is indeed Python 3 (which is probable these days) change the line to:

print(error, end=" ")

For backwards compatibility you can then add the following to the imports:

from __future__ import print_function
filmor
  • 30,840
  • 6
  • 50
  • 48