I'm a newbie for git hooks. I started writing a pre-commit hook today. Below is my hook which checks for raise, alert, trailing white space in file etc.
#!/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
while I commit a sample file am getting no such file or directory error. 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
Can anyone tell me what is the issue here.