Created
October 23, 2012 18:01
-
-
Save jarib/3940376 to your computer and use it in GitHub Desktop.
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 | |
class File | |
def self.binary?(file) | |
s = (read(file, stat(file).blksize) || "").split(//) | |
s.size > 0 && ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 | |
end | |
def self.fix_trailing_space(file) | |
content = read(file) | |
if content =~ /\r\n/ | |
p :crlf => file | |
end | |
content.gsub!(/[\t ]+$/, '') | |
open(file, "w") { |f| f << content } | |
end | |
end | |
if ARGV.empty? | |
status = `git status`.split("\n") | |
files = status.map do |e| | |
e[/(modified|new file):\s*(.+?)$/, 2] || e[/renamed:.+->\s*(.+?)$/, 1] | |
end.compact | |
else | |
files = ARGV | |
end | |
abort "no modified files" if files.empty? | |
files.each do |file| | |
next if !File.file?(file) | |
if File.binary?(file) | |
p :binary => file | |
else | |
p :fixing => file | |
File.fix_trailing_space file.strip | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment