0

我有一个脚本,我试图运行它来检查最新提交中文件的编码。当我手动运行它时,它的行为符合预期,但是当我执行提交时,它不会。如果变量在我的函数之外,我可以很好地打印它们,所以我怀疑它与我检索修改/添加的文件的方式有关。有没有办法让 Git 处理得更好?

#!/usr/bin/env python

import chardetect, subprocess, os
from sys import stdin, exit
from chardet.universaldetector import UniversalDetector

confidenceLevel = 0.8
allowedEncoding = ('ascii', 'utf-8')

# Get the current path and modify it to be the path to the repo
filePath = os.path.dirname(os.path.realpath(__file__))
filePath = filePath.replace('.git/hooks', '')

# Get all files that have been added or modified (filter is missing 'D' so that deleted files don't come through)
pr = subprocess.Popen(['/usr/bin/git', 'diff', '--diff-filter=ACMRTUXB', '--cached', '--name-only'],                  
       cwd=os.path.dirname('../../'), 
       stdout=subprocess.PIPE, 
       stderr=subprocess.PIPE, 
       shell=False) # Take note: Using shell=True has significant security implications.
(out, error) = pr.communicate()

# Create a list of files to check
out = out.split('\n')
out = [item for item in out if item != '']
out = [filePath + item for item in out]

messageList = [] # Keep this global

# If no paths are provided, it takes its input from stdin.
def description_of(file, name='stdin'):
    #Return a string describing the probable encoding of a file.
    u = UniversalDetector()
    for line in file:
        u.feed(line)
    u.close()
    result = u.result
    if result['encoding']:
        itPasses = ''
        if result['encoding'] in allowedEncoding and result['confidence'] >= confidenceLevel:
            pass
        else:
            messageList.append('%s: FAILS encode test %s with confidence %s\nYou must convert it before committing.' % (name, result['encoding'], result['confidence']))
    else:
        return '%s: no result' % name


def main():
    if len(out) <= 0:
        exit()
    else:
        for path in out:
            description_of(open(path, 'rb'), path)
        for item in messageList:
            print item
    if len(messageList) == 0:
        exit()
    else:
        exit(1)

if __name__ == '__main__':
    main()
4

1 回答 1

1

您脚本中的问题是这一行:

cwd=os.path.dirname('../../'), 

钩子在您的.git目录中运行,而不是在钩子目录中。因此,您将使用给定的行离开 repo。可以在此答案中找到更多详细信息。所以你不需要为git diff --cached.

您可能想要更改 cwd,以便您输入的路径UniversalDetector有意义。但是无论如何,您在那里所做的都是错误的。您不应该检查工作目录文件,而是检查索引中的文件,因为它们是实际要提交的文件。

您应该使用git ls-files --stagedandgit show来获取索引的内容。的简写是git show :filename,但这可能会导致奇怪的文件名出现问题。

还添加-z到参数中git diff,以便您可以处理更多文件名。

于 2013-05-31T13:37:39.207 回答