Created
December 11, 2012 05:22
-
-
Save dougireton/4256097 to your computer and use it in GitHub Desktop.
git pre-commit hook for linting Chef cookbooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# check for whitespace errors | |
git_ws_check = `git diff-index --check --cached HEAD --` | |
unless $?.success? | |
puts git_ws_check | |
exit 1 | |
end | |
COOKBOOK_PATH = File.split `git rev-parse --show-toplevel` | |
PARENT_PATH = COOKBOOK_PATH[0] | |
COOKBOOK_NAME = COOKBOOK_PATH[1].chomp # remove trailing newline | |
puts 'Running knife cookbook test...' | |
knife_output = `knife cookbook test #{ COOKBOOK_NAME } -o #{ PARENT_PATH } -c ~/chef/wit/chef-repo/.chef/knife.rb` | |
unless $?.success? | |
puts knife_output | |
exit 1 | |
end | |
puts 'Running tailor...' | |
# Get the file names of (A)dded, (C)opied, (M)odified Ruby files | |
STAGED_FILES = `git diff-index --name-only --diff-filter=ACM HEAD -- '*.rb'` | |
STAGED_FILES.lines do |file| | |
file.chomp! # remove carriage returns | |
puts file | |
tailor_output = `tailor --max-line-length 999 #{ file }` | |
unless $?.success? | |
puts tailor_output | |
exit 1 | |
end | |
end | |
puts "Running foodcritic..." | |
fc_output = `foodcritic --epic-fail any #{ File.join(PARENT_PATH, COOKBOOK_NAME) }` | |
unless $?.success? | |
puts fc_output | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where should I need to keep this pre-commit file?