Last active
June 28, 2017 18:26
-
-
Save stephenreid/f8d55b129af5fcb4398a2fbac4445070 to your computer and use it in GitHub Desktop.
prevent_asset_commit.rb
This file contains 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/env ruby | |
# Place in your .git/hooks directory | |
# Prevents rails assets commotted to master and other merge conflicts, etc | |
def on_master? | |
`git rev-parse --abbrev-ref HEAD`.include?("master") | |
end | |
# Don't allow master to have a manifest | |
if on_master? && File.directory?("public/assets") | |
puts "Master Cannot Have Compiled Assets - this interrupts live compile and build scripts" | |
exit 1 | |
end | |
FORBIDDEN = [ | |
/>>>>>>/, # Git conflict markers | |
/<<<<<</, # '' | |
/======/, # '' | |
/binding\.pry/, # pry debugging code | |
/binding\.remote_pry/, # '' | |
/debugger/ # Ruby debugging code | |
] | |
error_found = false | |
full_diff = `git diff --cached --` | |
full_diff.scan(%r{^\+\+\+ b/(.+)\n@@.*\n([\s\S]*?)(?:^diff|\z)}).each do |file, diff| | |
added = diff.split("\n").select { |x| x.start_with?("+") }.join("\n") | |
# Scan for "forbidden" calls | |
FORBIDDEN.each do |re| | |
if added.match(re) | |
puts %{Error: git pre-commit hook forbids committing lines with "#{$1 || $&}" to #{file}\n--------------} | |
error_found = true | |
end | |
end | |
end | |
# Finally, report errors | |
if error_found | |
puts "To commit anyway, use --no-verify" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment