1

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.

2

1 Answer 1

2

Judging by other git hooks (like "pre-commit/utils/staged_files.rb" or "i-wind/gpc/pre-commit.rb", you don't need to prepend with the root folder of the repo.

file_text = File.read(File.join(file))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.