python - Pre Commit hook git错误

标签 python git

我正在尝试在 python 中执行预提交 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())

最佳答案

您的预提交文件中有多余的回车符。如果您在 Windows 中编辑文件并将文件复制到 Linux 计算机,就会发生这种情况。

试试这些命令:

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

然后重新运行您的 git 命令。

关于python - Pre Commit hook git错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18885644/

相关文章:

Python - 将有符号整数转换为字节

linux - 两台远程服务器上的 git diff(在 git git --no-index 之外)

git - 为什么 "git checkout <filename>"仅适用于非暂存文件?

python - 卷积神经网络中的形状误差

python - 环境变量或 pserve 参数中的 Pyramid 服务器端口

python - 适用于 Python 2.7 的 Visual Microsoft Visual C++ 编译器与 MinGW

python - 带有 Keras Functional API 的多输入多输出模型

gitignore 无法与 Visual Studio 一起使用

git - 在 VSTS merge 请求中获取真实的源代码分支名称

linux - 如何将凭证烘焙到 git 的 docker 镜像中?