From 98cc5f14caf136d786fea344a4742735ca192da2 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 4 Feb 2015 19:22:14 +0000 Subject: [PATCH] Add a pre-commit hook to run rubocop To run rubocop on checkin, copy this script to .git/hooks and it will cause any attempt to commit code which fails rubcop to be blocked. --- hooks/pre-commit | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 hooks/pre-commit diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 000000000..6f7912f1f --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,24 @@ +#!/usr/bin/ruby + +ok = true + +if IO.popen(["git", "ls-files", "--unmerged"]).read.empty? + need_stash = IO.popen(%w(git diff)).read.length > 0 + + system("git", "stash", "save", "--keep-index", "--quiet") if need_stash + + files = IO.popen(["git", "diff", "--staged", "--name-only"]).readlines.map(&:chomp) + + ruby_files = files.select do |file| + file =~ /\.rb$/ || `file --brief --mime-type #{file}` == "text/x-ruby\n" + end + + ok &&= system("rubocop", *ruby_files) unless ruby_files.empty? + + system("git", "stash", "pop", "--quiet") if need_stash +else + puts "Unmerged files. Resolve before committing." + ok = false +end + +exit ok -- 2.39.5