【问题标题】:Pre Commit hook git error预提交钩子 git 错误
【发布时间】:2013-09-19 03:15:51
【问题描述】:

我正在尝试在 python 中执行预提交 git 挂钩以检查文件的行长度是否小于 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())

【问题讨论】:

  • 向我们展示您正在运行的git 命令以及您收到的精确错误消息。
  • 我运行 git commit 命令并收到错误消息 no such file or directory。我运行 git commit 命令错误:无法运行 .git/hooks/pre-commit: No such file or directory
  • 尝试直接运行pre-commit 文件:$ ./git/hooks/pre-commit。您收到错误消息吗?
  • 请复制粘贴准确、完整的错误信息。不要总结,不要重新输入,只需复制粘贴即可。
  • 1) python 安装在你的 PATH 上吗?尝试“which python”并确认它打印“/usr/bin/python”。 2)您的预提交文件是否有可能以 DOS 行结尾而不是 Unix 行结尾保存?尝试“file .git/hooks/pre-commit”,看看它是否显示“CRLF line endings”。

标签: python git


【解决方案1】:

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

试试这些命令:

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

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

【讨论】:

    【解决方案2】:

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

    dos2unix .git/hooks/pre-commit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 2015-08-23
      • 2019-07-12
      • 2015-01-13
      • 2018-05-21
      • 2020-09-11
      • 1970-01-01
      相关资源
      最近更新 更多