Git挂钩拒绝文件包含特定字符串的提交

Git挂钩拒绝文件包含特定字符串的提交

本文介绍了Git挂钩拒绝文件包含特定字符串的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将GuardRspec一起使用;我使用focus: true来强制它仅运行我正在进行的测试.但是有时候我忘记删除focus: true,这会导致我未来的自我和与之共事的人分心.

I am using Guard with Rspec; I use focus: true to force it run only tests I am working on. But sometimes I forget to remove focus: true and it is causing distraction for my future self and people I work with.

我想制作一个git钩子,以检查spec文件夹,以确保测试文件中除了spec/rails_helper.rb之外没有focus: true并将其保存在存储库中.

I want to make a git hook that would check the spec folder to make sure there is no focus: true in test files apart from spec/rails_helper.rb and keep it in Repository.

我已阅读此答案将git钩子放入存储库,猜测它必须有点尴尬.

I have read this answer Putting git hooks into repository, guess it has to be a bit awkward.

基于文件内容的钩子如何用于阻止提交?

How are hooks used to prevent a commit based on the contents of files?

更新

这是我现在拥有的东西,但是它不起作用,即使没有匹配项,git也拒绝提交.

Here is what I have now but it doesn't work, even if there is no match, git refuses to commit.

FILES_PATTERN='\.rb(\..+)?$'
FORBIDDEN="(\, focus: true|binding\.pry)"
git diff --cached --name-only | egrep "$FILES_PATTERN" | xargs egrep --with-filename -n "$FORBIDDEN" && echo "Commit reject, found $FORBIDDEN reference, please remove" && exit 1
exit 0

推荐答案

git-scm .

您需要预提交挂钩.要返回非零值(从而中止提交),您需要遵循以下内容:

You want the pre-commit hook. To return a non-zero value (and thus abort the commit), you'd want something along these lines:

FILES_PATTERN='\.rb(\..+)?$'
FORBIDDEN='focus: true'
git diff --cached --name-only | \
  grep -spec/ | \
  grep -E $FILES_PATTERN | \
  xargs grep --with-filename -n $FORBIDDEN && echo "COMMIT REJECTED Found '$FORBIDDEN' references. Please remove them before commiting" && exit 1

这是从相当好的提示网站中删除的.我尚未测试过所做的调整.

That's lifted from this rather good tips site. I haven't tested the tweaks I made.

这篇关于Git挂钩拒绝文件包含特定字符串的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 19:54