Skip to content

Instantly share code, notes, and snippets.

@miyucy
Created August 4, 2011 02:49
Show Gist options
  • Save miyucy/1124401 to your computer and use it in GitHub Desktop.
Save miyucy/1124401 to your computer and use it in GitHub Desktop.
git pre-receive hooks
#!/usr/bin/env ruby
old_sha, new_sha, ref = STDIN.read.split(' ')
diff = %x{ git diff --raw --name-only #{old_sha} #{new_sha} 2> /dev/null }
diff.each_line do |line|
file = line.chomp
next unless file =~ /\.rb\Z/i
tree = %x{ git ls-tree --full-name #{new_sha} #{file} 2> /dev/null }.split(" ")
contents = %x{ git cat-file blob #{tree[2]} 2> /dev/null }
if found = contents.each_line.map(&:chomp).find{ |e| e =~ /\s+\Z/ }
puts <<EOF
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> !!!!!!!!! PUSH REJECTED !!!!!!!!!
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> REMOVE TRAILING WHITESPACE.
> * #{tree[3]}
>
> #{found.inspect}
>
> YOU SHOULD SEE ALSO:
> * http://keijinsonyaban.blogspot.com/2011/01/git.html
> * http://shugo.net/ruby-codeconv/codeconv.html
> * http://www.rubyist.net/~matz/20041006.html#p02
> * http://labs.nayutaya.jp/?ruby-coding-standards
>
> To fix:
> * Ammend previous commit (git add && git commit --amend)
> * git push
>
> Cheer!
> !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOF
exit 1
end
end
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment