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$/ && %x(file --brief --mime-type #{file}) == "text/x-ruby\n")
18 ok &&= system("bundle", "exec", "cookstyle", *ruby_files) unless ruby_files.empty?
20 system("git", "stash", "pop", "--quiet") if need_stash
22 puts "Unmerged files. Resolve before committing."