-
-
Save Marak/539124 to your computer and use it in GitHub Desktop.
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/env ruby | |
# script/whitespace | |
# | |
# Strips whitespace from any files modified in git | |
# Also: | |
# - converts tabs to spaces | |
# - ensures a single newline at the end | |
class WhitespaceProcessor | |
def self.process(code) | |
result = [] | |
code.each do |line| | |
line.gsub!(/(\s+)$/, "\n") | |
line.gsub!(/\t/, ' ') | |
result << line | |
end | |
while result.last =~ /^$/ | |
result.pop | |
end | |
unless result.last =~ /\n$/ | |
result << "\n" | |
end | |
code = result.join | |
code.gsub!(/\A\n*/, '') | |
code | |
end | |
end | |
if ARGV.include?('--all') | |
files = `find . -type file | grep -v .git | grep -v ./vendor | grep -v ./tmp | egrep ".(rb|js|haml|html|css|sass)"`.split(/\n/) | |
puts "* Stripping whitespace from all project files" | |
else | |
files = `git status`.split("\n").select { |file| file =~ /^#\t(modified|new file|renamed):/ && !(file =~ /\(new commits\)/) } | |
puts "* Stripping whitespace from modified files." | |
end | |
files.each do |line| | |
line = line.split(/[\t\s]+/).last | |
puts " processing #{line}..." | |
code = File.read(line) | |
File.open(line, 'w+') { |f| f << (WhitespaceProcessor.process(code)) } | |
end | |
puts "* DONE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yup,
It appears you guys are right, it just ignores whitespace added when diff'd. I guess my team at the time just started pruning the trailing whitespace we were seeing. :)