├── README.md ├── LICENSE └── pre-commit /README.md: -------------------------------------------------------------------------------- 1 | # DESCRIPTION 2 | 3 | A hook script to verify that only syntactically valid ruby code is commited. 4 | 5 | # INSTALLATION 6 | 7 | Put this code into a file called "pre-commit" inside your .git/hooks 8 | directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit") 9 | 10 | # REQUIREMENTS 11 | 12 | Requires Ruby 1.8.6 or better, Git 1.7 or better 13 | 14 | If Rubinius is installed and available via `rbx`, the hook will use it to test the Ruby files for warnings and errors. 15 | 16 | # SIMILAR PROJECTS 17 | 18 | * [Code Beautifier.tmbundle][beautify] by [Joel Chippindale (mocoso)](mocoso) does something similar for TextMate. 19 | 20 | [beautify]: http://github.com/mocoso/code-beautifier.tmbundle "Code Beautifier.tmbundle" 21 | [mocoso]: http://github.com/mocoso "Joel Chippindale" 22 | 23 | # LICENSE 24 | 25 | Licensed under the MIT License. See LICENSE file for details. 26 | 27 | # AUTHORS 28 | 29 | Written by Markus Prinz, with contributions by Jérémy Lecour 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2009 Markus Prinz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /pre-commit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # encoding: utf-8 3 | # 4 | # A hook script to verify that only syntactically valid ruby code is commited. 5 | # Called by git-commit with no arguments. The hook should 6 | # exit with non-zero status after issuing an appropriate message if 7 | # it wants to stop the commit. 8 | # 9 | # Put this code into a file called "pre-commit" inside your .git/hooks 10 | # directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit") 11 | 12 | # Ruby 2 or higher required 13 | if RUBY_VERSION.to_f < 2.0 14 | $stderr.puts "Ruby version 2.0 or higher required!" 15 | exit 1 16 | end 17 | 18 | require 'shellwords' 19 | require 'open3' 20 | include Open3 21 | 22 | # Set this to true if you want warnings to stop your commit 23 | stop_on_warnings = (%x(git config --bool hooks.stop-on-warnings).chomp != "false") 24 | 25 | # Set this to true if you'd like to skip checking erb files 26 | skip_erb_files = (%x(git config --bool hooks.skip-erb-files).chomp == "true") 27 | 28 | # Set this if you'd like to use a specific ruby executable to for syntax checking 29 | ruby_compiler = %x(git config hooks.ruby-compiler).chomp 30 | if ruby_compiler.empty? 31 | compiler_ruby = %x(which ruby).chomp 32 | end 33 | 34 | if compiler_ruby.nil? or compiler_ruby == "" 35 | $stderr.puts "Couldn't find `rbx` or `ruby` executable!" 36 | exit 1 37 | end 38 | 39 | changed_ruby_files = `git diff-index --name-only --cached HEAD`.split("\n").inject([]) do |files, line| 40 | files << line.chomp if line =~ /(.+\.(e?rb|task|rake|thor)|[Rr]akefile|[Tt]horfile)/ 41 | files 42 | end 43 | 44 | warnings_and_errors = [] 45 | 46 | changed_ruby_files.each do |file| 47 | next if not File.readable?(file) 48 | 49 | if file =~ /\.erb\z/ 50 | next if skip_erb_files 51 | 52 | # use actual rails ERB renderer 53 | cmd = "#{compiler_ruby} -e 'require \"action_view\"; puts ActionView::Template::Handlers::Erubis.new(File.read(\"#{file}\")).src' | #{compiler_ruby} -wc" 54 | else 55 | cmd = [compiler_ruby, "-wc", file].shelljoin 56 | end 57 | 58 | errors = [] 59 | popen3(cmd) do |stdin, stdout, stderr| 60 | errors = stderr.read.split("\n") 61 | end 62 | 63 | errors.reject!{ |line| line =~ /[0-9]+:\s+warning:/ } unless stop_on_warnings 64 | 65 | if errors.any? 66 | errors.map!{ |line| line.sub(/#{file}:/, '') } 67 | warnings_and_errors << "#{file}:\n#{errors.join("\n")}" 68 | end 69 | end 70 | 71 | if warnings_and_errors.any? 72 | $stderr.puts warnings_and_errors.join("\n\n") 73 | exit 1 74 | else 75 | # All is well 76 | exit 0 77 | end 78 | --------------------------------------------------------------------------------