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