Git pre-commit hook

独自空忆成欢 提交于 2019-12-03 22:46:06

问题


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

回答1:


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.

See "Git pre-commit hook : changed/added files":

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

As explained in "Why You Need a Git Pre-Commit Hook and Why Most Are Wrong":

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)


来源:https://stackoverflow.com/questions/20278573/git-pre-commit-hook

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!