我是git hooks的新手。我今天开始写一个预提交钩子。下面是我的钩子,它检查文件中的加注,警报,尾随空格等。
#!/usr/bin/env ruby
regex = Regexp.union(/raise/, /puts/, /alert/, /console.log/, /[\s\t]+$/)
folder_path =`git rev-parse --show-toplevel`
`git diff --cached --name-only --diff-filter=ACM`.each do |file|
file_text = File.read(File.join(folder_path.chomp, '/', file))
if file_text.match(regex)
puts "#{file} contains invalid word #{$&}. Please remove it"
exit 1
end
end
当我提交示例文件时,我没有收到此类文件或目录错误。 kranthi @ kranthi-Aspire-4755:〜/ learning / git_learning $ git commit -m“tet”
.git/hooks/pre-commit:9:in `read': No such file or directory - /home/kranthi/learning/git_learning/another_folder/test2.txt (Errno::ENOENT)
from .git/hooks/pre-commit:9
from .git/hooks/pre-commit:17:in `map'
from .git/hooks/pre-commit:8:in `each'
from .git/hooks/pre-commit:8:in `map'
from .git/hooks/pre-commit:8
有谁能告诉我这里有什么问题。
答案 0 :(得分:2)
通过其他git钩子(例如“pre-commit/utils/staged_files.rb
”或“i-wind/gpc/pre-commit.rb
”判断,您不需要在repo的根文件夹前面添加。
file_text = File.read(File.join(file))