我有以下预推 Hook 。理想情况下,我希望它遍历所有被推送到我的存储库的文件,如果任何文件的内容与顶部定义的正则表达式不匹配,则拒绝推送。尝试遍历文件时出现以下错误:“undefined method ‘each’ for “”:String (NoMethodError)”。 '.each' 不起作用,因为 git 命令正在返回包含已更改文件的字符串。
#!/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
有没有人知道我如何循环返回的文件?
谢谢
最佳答案
只需要使用“.each_line”而不是“.each”。
关于ruby - 在 Git 中编写一个预推送 Hook 以 grep 正则表达式的所有文件想要在找不到正则表达式时拒绝推送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44255141/