Skip to content

Instantly share code, notes, and snippets.

@joshthewhite
Created October 12, 2012 14:06
Show Gist options
  • Save joshthewhite/3879337 to your computer and use it in GitHub Desktop.
Save joshthewhite/3879337 to your computer and use it in GitHub Desktop.
Git Syntax Check pre-commit Hook
# .git/hooks/pre-commit
git stash -q --keep-index
.git/hooks/syntax.rb
RESULT=$?
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0
#!/usr/bin/ruby
# .git/hooks/syntax.rb
exitCode = 0
puts "Starting pre-commit hook...", ""
puts "Doing php -l checks...", ""
# Check each added or modified file for syntax errors.
proc = IO.popen('git status --porcelain') do |f|
f.readlines.each do |fileline|
filebits = fileline.split ' ';
if not (filebits[0] == 'M' or filebits[0] == 'A')
next
end
next if not filebits[1].index('.php')
puts "Checking file #{filebits[1]}..."
IO.popen("php -l #{filebits[1]}") do |g|
resultLines = g.readlines
if resultLines.count > 1
exitCode = 2
end
puts resultLines
end
end
end
exit!(exitCode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment