]> git.openstreetmap.org Git - chef.git/blob - hooks/pre-commit
Use /usr/bin/env to find ruby. Improves cross platform support
[chef.git] / hooks / pre-commit
1 #!/usr/bin/env 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$/ && %x(file --brief --mime-type #{file}) == "text/x-ruby\n")
16   end
17
18   ok &&= system("bundle", "exec", "cookstyle", *ruby_files) unless ruby_files.empty?
19
20   system("git", "stash", "pop", "--quiet") if need_stash
21 else
22   puts "Unmerged files. Resolve before committing."
23   ok = false
24 end
25
26 exit ok