]> git.openstreetmap.org Git - chef.git/blob - hooks/pre-commit
Extend years for mailman archive
[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.empty?
7
8   system("git", "stash", "save", "--keep-index", "--quiet") if need_stash
9
10   files = IO.popen(["git", "diff", "--staged", "--name-status"]).map do |line|
11     Regexp.last_match(1) if line =~ /^[AM]\s+(.*)$/
12   end.compact
13
14   ruby_files = files.select do |file|
15     file =~ /\.rb$/ || (file !~ /\.erb$/ && `file --brief --mime-type #{file}` == "text/x-ruby\n")
16   end
17
18   ok &&= system("bundle", "exec", "rubocop", *ruby_files) unless ruby_files.empty?
19
20   cookbooks = files.grep(%r{(cookbooks/[^/]+)/}) { Regexp.last_match(1) }.uniq
21
22   ok &&= system("bundle", "exec", "foodcritic", *cookbooks) unless cookbooks.empty?
23
24   system("git", "stash", "pop", "--quiet") if need_stash
25 else
26   puts "Unmerged files. Resolve before committing."
27   ok = false
28 end
29
30 exit ok