5 if IO.popen(["git", "ls-files", "--unmerged"]).read.empty?
6 need_stash = !IO.popen(%w(git diff)).read.empty?
8 system("git", "stash", "save", "--keep-index", "--quiet") if need_stash
10 files = IO.popen(["git", "diff", "--staged", "--name-status"]).map do |line|
11 Regexp.last_match(1) if line =~ /^[AM]\s+(.*)$/
14 ruby_files = files.select do |file|
15 file =~ /\.rb$/ || (file !~ /\.erb$/ && `file --brief --mime-type #{file}` == "text/x-ruby\n")
18 ok &&= system("bundle", "exec", "rubocop", *ruby_files) unless ruby_files.empty?
20 cookbooks = files.grep(%r{(cookbooks/[^/]+)/}) { Regexp.last_match(1) }.uniq
22 ok &&= system("bundle", "exec", "foodcritic", *cookbooks) unless cookbooks.empty?
24 system("git", "stash", "pop", "--quiet") if need_stash
26 puts "Unmerged files. Resolve before committing."