Git预提交钩 [英] Git pre-commit hook

查看:72
本文介绍了Git预提交钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是git hook的新手.我无法理解下面的预提交钩子.谁能告诉我这是如何工作的.我怀疑这是在提交的文件中发生grep的原因,因为我们没有将这些文件放在任何地方.抱歉,如果我问错了问题,但是请帮助我理解git hooks ..

I'm new to git hooks. I'm not able to understand below pre-commit hook. Can anyone tell me how this works please.Here my doubt is how grep will be happened in committed files as we are not taking those files anywhere. Sorry if am asking wrong question but please help me in understanding git hooks..

#!/usr/bin/env ruby
if `grep -rls "require 'ruby-debug'; raise" *` != ""
  puts "You twit, you've left a debugger in!"
  exit(1)
end

推荐答案

您应该对索引(缓存的)文件而不是工作树进行grep.
否则,您的grep可能会在不属于下一次提交的文件(或文件的一部分)中找到调试指令.

You should rather grep on indexed (cached) files, instead of your working tree.
Otherwise, your grep could find debug instructions in files (or part of files) which aren't part of the next commit.

请参见" Git预提交挂钩:已更改/添加的文件":

git diff --cached --name-only --diff-filter=ACM

如"为什么需要Git预先提交的钩子以及为什么大多数是错误的":

大多数测试是针对磁盘上当前存在的任何文件,而不是暂存区域(实际已提交的文件)中的文件.

Most test against whatever files are currently on disk, not what is in the staging area (the files actually being committed).

如果该钩子有点不同 :它将在搜索文件之前隐藏所有正在进行的工作.

The approach if that hook is a bit different: it stashes every work in progress before searching the files.

def main(all_files):
    # Stash any changes to the working tree that are not going to be committed
    subprocess.call(['git', 'stash', '-u', '--keep-index'], stdout=subprocess.PIPE)

这篇关于Git预提交钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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