Skip to content

Instantly share code, notes, and snippets.

@mikemackintosh
Last active May 31, 2016 15:33
Show Gist options
  • Save mikemackintosh/940730c1a5207ce59a262c7f4850563d to your computer and use it in GitHub Desktop.
Save mikemackintosh/940730c1a5207ce59a262c7f4850563d to your computer and use it in GitHub Desktop.
R.I.G.B.Y. - Tabs v Spaces
#!/usr/bin/env ruby
files_modified = `git diff-index --cached --name-only HEAD`.split(/\n/)
# expand_tabs from Stack Overflow
# -> http://stackoverflow.com/a/8900610/1431239
# This algorithm by Brian Candler (B.Candler@pobox.com) found on the
# org.ruby-lang.ruby-talk mailing list
# http://markmail.org/message/avdjw34ahxi447qk
# Date: 2003-5-31 13:35:09
# Subject: Re: expandtabs
def expand_tabs(s, tab_stops = 8)
s.gsub(/([^\t\n]*)\t/) do
$1 + " " * (tab_stops - ($1.size % tab_stops))
end
end
files_modified.each do |f|
# If we find opening tabs, then replace them
lines = IO.readlines(f).map do |line|
line = expand_tabs(line, 4)
end
# Write them back
File.open(f, 'w') do |f|
f.puts lines
end
# Spit out a message
puts "[R.I.G.B.Y.] Changed tabs to spaces in #{f}."
# Add this back to the index
`git add #{f}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment