用于预提交挂钩的python脚本中的语法无效

时间:2014-11-19 10:38:45

标签: python git

我正在使用以下python代码来挂钩jshint的预提交挂钩但是当我执行git commit -m "commit comment"时它会显示此错误

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

这是代码

#!/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()

我对python一无所知,也无法找到正确的语法。

1 个答案:

答案 0 :(得分:0)

如果您的python安装确实是Python 3(这些天很可能),请将行更改为:

print(error, end=" ")

为了向后兼容,您可以将以下内容添加到导入中:

from __future__ import print_function