Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created February 16, 2010 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balinterdi/305654 to your computer and use it in GitHub Desktop.
Save balinterdi/305654 to your computer and use it in GitHub Desktop.
### parse_modified_files_from_git.rb
#!/usr/bin/env ruby -wn
modified_file_pattern = /^#\s+(?:modified|new file):\s+(.*)$/
puts $1 if modified_file_pattern =~ $_
### rm_trailing_whitespace.rb
#!/usr/bin/env ruby -wn
$:.unshift(File.dirname(__FILE__))
require "trailing_whitespace_eraser"
TrailingWhiteSpaceEraser.rm_trailing_whitespace!($_)
### trailing_whitespace_eraser.rb
class TrailingWhiteSpaceEraser
FILE_TYPES = ["rb", "feature", "yml", "erb", "haml"]
def self.rm_trailing_whitespace_from_file!(file)
trimmed = File.readlines(file).map do |line|
line.gsub(/[\t ]+$/, "")
end
open(file, "w") { |f| f.write(trimmed) }
end
def self.rm_trailing_whitespace!(root)
root = File.expand_path(root)
files = File.directory?(root) ? Dir.glob("#{root}/**/*.{#{FILE_TYPES.join(',')}}") : [root]
files.each { |file| rm_trailing_whitespace_from_file!(file.chomp) }
end
end
### run it like so:
#!/bin/sh
parse_modified_files_from_git_status.rb | rm_trailing_whitespace.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment