]> git.openstreetmap.org Git - chef.git/blob - hooks/pre-commit
de3142ea000a0a567f6311797c982d4f6ec309fa
[chef.git] / hooks / pre-commit
1 #!/usr/bin/ruby
2
3 ok = true
4
5 if IO.popen(["git", "ls-files", "--unmerged"]).read.empty?
6   need_stash = IO.popen(%w(git diff)).read.length > 0
7
8   system("git", "stash", "save", "--keep-index", "--quiet") if need_stash
9
10   files = IO.popen(["git", "diff", "--staged", "--name-only"]).readlines.map(&:chomp)
11
12   ruby_files = files.select do |file|
13     file =~ /\.rb$/ || `file --brief --mime-type #{file}` == "text/x-ruby\n"
14   end
15
16   ok &&= system("rubocop", *ruby_files) unless ruby_files.empty?
17
18   cookbooks = files.grep(%r{(cookbooks/[^/]+)/}) { Regexp.last_match(1) }.uniq
19
20   ok &&= system("foodcritic", "-f", "any", *cookbooks) unless cookbooks.empty?
21
22   system("git", "stash", "pop", "--quiet") if need_stash
23 else
24   puts "Unmerged files. Resolve before committing."
25   ok = false
26 end
27
28 exit ok