Created
October 12, 2012 14:06
-
-
Save joshthewhite/3879337 to your computer and use it in GitHub Desktop.
Git Syntax Check pre-commit Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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