Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 预提交钩子git错误_Python_Git - Fatal编程技术网

Python 预提交钩子git错误

Python 预提交钩子git错误,python,git,Python,Git,我试图在python中执行一个预提交git钩子,以检查文件的行长度是否小于80个字符。但是,我得到一个没有这样的文件/目录错误。我戴着软呢帽,已经做好了准备!欢迎使用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

我试图在python中执行一个预提交git钩子,以检查文件的行长度是否小于80个字符。但是,我得到一个没有这样的文件/目录错误。我戴着软呢帽,已经做好了准备!欢迎使用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())

预提交文件中包含无关的回车符。如果在Windows中编辑文件并将其复制到Linux计算机上,则可能会发生这种情况

请尝试以下命令:

cp .git/hooks/pre-commit /tmp/pre-commit
tr -d '\r' < /tmp/pre-commit > .git/hooks/pre-commit
cp.git/hooks/pre-commit/tmp/pre-commit
tr-d'\r'.git/hooks/pre-commit

然后重新运行
git
命令。

删除“\r”最简单的方法是:

dos2unix .git/hooks/pre-commit

向我们显示您正在运行的
git
命令以及收到的准确错误消息。我运行git commit命令,得到的错误消息没有这样的文件或目录。我运行git commit命令错误:无法运行。git/hooks/pre commit:没有这样的文件或目录直接运行
pre commit
文件:
$./git/hooks/pre-commit
。您是否收到错误消息?请复制粘贴准确、完整的错误消息。不要总结,不要重新键入,只需复制粘贴即可。1)您的路径上是否安装了python?尝试“哪个python”并确认它打印了“/usr/bin/python”。2) 您的预提交文件是否可能是以DOS行结尾而不是Unix行结尾保存的?尝试“file.git/hooks/pre-commit”,看看它是否显示“CRLF行结束”