0
我有一個腳本,我試圖運行來檢查最新的提交中的文件的編碼。當我手動運行它時,它的行爲如預期,但是當我執行提交時,它不會。如果它們不在我的函數中,我可以打印變量,所以我懷疑它與檢索修改/添加文件的方式有關。有沒有辦法做到Git可以更好地處理?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()
具有完美的感覺。多麼不尋常的操作方式。現在,當我運行'git ls-files --stage'時,我得到了所有文件,包括那些未修改的文件。我真的只想添加/修改文件。我誤解了你的答案嗎? – Sneagan
是的,你是:)。你應該使用你的diff命令(加上一個'-z')來獲取已更改文件的列表。但是,然後使用'git ls-files --stage'來獲取將要提交的版本。然後使用'git show'來獲取這些內容。 – Chronial
哦,我明白了。我不需要那麼多的細節(至少我不認爲我會這樣做),因爲我想在這裏完成我想要完成的任務。不過感謝您的詳細解答! – Sneagan