我试图在python中执行pre commit git hook来检查文件的行长度是否小于80个字符.但是我得到一个没有这样的文件/目录错误.我在fedora上设置#!usr/bin/python.help将不胜感激
#!/usr/bin/env python
#-*- mode: python -*-
from subprocess import Popen, PIPE
import sys
def run(command):
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
p.wait()
return p.returncode, p.stdout.read().strip().split(), p.stderr.read()
def precommit():
_, files_modified, _= run("git diff-index --name-only HEAD")
i=1
for fname in files_modified:
file = open(fname)
while i==1:
line = file.readline()
if not line:
break
elif len(line)>80:
print("Commit failed: Line greater than 80 characters")
return 1
return 0
sys.exit(precommit())
Run Code Online (Sandbox Code Playgroud)
Rob*_*obᵩ 21
您的预提交文件中包含无关的回车符.如果在Windows中编辑文件并将文件复制到Linux计算机,则会发生这种情况.
试试这些命令:
cp .git/hooks/pre-commit /tmp/pre-commit
tr -d '\r' < /tmp/pre-commit > .git/hooks/pre-commit
Run Code Online (Sandbox Code Playgroud)
然后重新运行你的git
命令.
归档时间: |
|
查看次数: |
8649 次 |
最近记录: |