Skip to content

Instantly share code, notes, and snippets.

@dougireton
Created December 11, 2012 05:22
Show Gist options
  • Save dougireton/4256097 to your computer and use it in GitHub Desktop.
Save dougireton/4256097 to your computer and use it in GitHub Desktop.
git pre-commit hook for linting Chef cookbooks
#!/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
@MrudulaVarimadugu
Copy link

Where should I need to keep this pre-commit file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment