Pre Commit挂钩git错误 [英] Pre Commit hook git error

查看:451
本文介绍了Pre Commit挂钩git错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中执行一个预提交git钩子来检查文件是否有长度小于80个字符的行。但是我得到一个没有这样的文件/目录错误。我在Fedora上设置了#!usr / bin / python.help,谢谢

I am trying to execute a pre commit git hook in python to check if files have line lengths less than 80 chars. However i get a no such file/directory error. i am on fedora and have set the #!usr/bin/python.help would be appreciated

#!/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计算机上,则会发生这种情况。

Your pre-commit file has extraneous carriage returns in it. This can happen if you edit the file in Windows and copy the file to a Linux computer.

请尝试以下命令:

Try these commands:

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

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

And then rerun your git command.

这篇关于Pre Commit挂钩git错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆