Created
December 20, 2012 18:35
-
-
Save davidoram/4347556 to your computer and use it in GitHub Desktop.
svn pre-commit hook that prevents text files (based on extension) from being committed if they contain a Byte Order Mark (BOM). If using on windows must wrap in a batch script
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 | |
# pre-commit.rb | |
# Exit 1 if is a text file and has a BOM at the head of the file | |
# Create list of file extensions that are considered text files. | |
text_files = [ | |
".awk", | |
".bas", | |
".bat", | |
".cfc", | |
".cfm", | |
".cfml", | |
".config", | |
".cs", | |
".css", | |
".csv", | |
".dot", | |
".htm", | |
".html", | |
".java", | |
".js", | |
".log", | |
".mxml", | |
".php", | |
".pl", | |
".properties", | |
".py", | |
".rb", | |
".rdf", | |
".sh", | |
".sql", | |
".svg", | |
".txt", | |
".wsdl", | |
".xml", | |
".yml" ] | |
if text_files.include? File.extname(ARGV[0]).downcase | |
if File.open(ARGV[0]) {|f| f.read(3)} == "\xEF\xBB\xBF" | |
$stderr.puts " --------------------------------------------------------------------------- " | |
$stderr.puts " Your commit has been blocked because a Byte Order Mark (BOM)" | |
$stderr.puts " (http://en.wikipedia.org/wiki/Byte_order_mark) has been detected at the top" | |
$stderr.puts " of the file." | |
$stderr.puts " " | |
$stderr.puts " Please remove the BOM and recommit" | |
$stderr.puts " " | |
$stderr.puts " To remove: open in Notepad++, Encoding > Encode in UTF-8 without BOM, Save" | |
$stderr.puts " --------------------------------------------------------------------------- " | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment