Writing a pre-push hook in Git to grep all files for regex want to reject push if regex not found

纵然是瞬间 提交于 2019-12-04 04:49:08

问题


I have the following pre-push hook. Ideally I would like it to go through all files that are being pushed to my repository and reject the push if the content of any of the files doesn't match the regular expression defined at the top. I'm getting the following error when attempting to loop through the files: "undefined method `each' for "":String (NoMethodError)". '.each' doesn't work as the git command is returning a string containing the changed files.

#!/usr/bin/env ruby

regex = "\\s*GO\\s*$"

localRef, remoteRef = ARGV
#puts localRef
#puts remoteRef

input = $stdin.readlines[0]
localSha = input.split(" ")[1]
remoteSha = input.split(" ")[3]
#puts localSha 
#puts remoteSha

range = "#{remoteSha}..#{localSha}"

#folderPath = `git rev-parse --show-toplevel`
#puts folderPath 

`git diff --name-only --diff-filter=ACMR #{range}`.each do |file|
  #puts file
  content = File.read(file)
  unless content.match(regex)
    puts "#{file} must end with 'GO' keyword"
    exit 1
  end
end

exit 0 

Does anyone have an idea of how I can loop through the files returned?

Thanks


回答1:


Just needed to use ".each_line" instead of ".each".



来源:https://stackoverflow.com/questions/44255141/writing-a-pre-push-hook-in-git-to-grep-all-files-for-regex-want-to-reject-push-i

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