pope (owner)

Fork Of

gist: 150248 by cypher A git pre-commit hook that ...

Revisions

  • 513904 cypher Mon Jul 20 07:07:46 -0700 2009
  • 84dc3e cypher Mon Jul 20 06:45:19 -0700 2009
  • fbe463 cypher Mon Jul 20 05:47:03 -0700 2009
  • 7663b2 cypher Mon Jul 20 05:44:40 -0700 2009
  • 8677b0 cypher Mon Jul 20 03:52:25 -0700 2009
  • 2583ea cypher Mon Jul 20 03:46:31 -0700 2009
  • f972b3 cypher Mon Jul 20 03:45:13 -0700 2009
gist: 150508 Download_button fork
public
Public Clone URL: git://gist.github.com/150508.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby
#
# A hook script to verify that only syntactically valid ruby code is commited.
# Called by git-commit with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Put this code into a file called "pre-commit" inside your .git/hooks
# directory, and make sure it is executable ("chmod +x .git/hooks/pre-commit")
#
# Tested only with Git 1.6.4-rc1, but should work with any Git 1.6.*
# Requires Ruby 1.8.6 or better
 
require 'open3'
include Open3
 
changed_ruby_files = `git diff-index --name-only --cached HEAD`.inject([]) do |files, line|
  files << line.chomp if line =~ /(.+\.(e?rb|task)|Rakefile)/
  files
end
 
problematic_files = changed_ruby_files.inject([]) do |problematic_files, file|
  cmd = if file =~ /\.erb\z/
    # Set trim mode to "-", just as Rails does
    "erb -xT - #{file} | ruby -c"
  else
    "ruby -c #{file}"
  end
 
  errors = nil
  popen3(cmd) do |stdin, stdout, stderr|
    errors = stderr.read.split("\n")
  end
 
  unless errors.empty?
    errors.map!{ |line| line.sub(/#{file}:/, '') }
    problematic_files << "#{file}:\n#{errors.join("\n")}"
  end
 
  problematic_files
end
 
if problematic_files.size > 0
  $stderr.puts problematic_files.join("\n\n")
  exit 1
else
  # All is well
  exit 0
end