预提交钩子 git 错误

2024-05-02

我正在尝试在 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命令。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

预提交钩子 git 错误 的相关文章

随机推荐