public
Last active

Reads patterns from the .svnignore file in RAILS_ROOT and ignores them

  • Download Gist
svn.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
namespace :svn do
desc 'Reads patterns from the .svnignore file in RAILS_ROOT and ignores them'
task :ignore => :environment do
# Set the svnignore filename and define the absolute path to that file
#
svnignore_path = File.join(RAILS_ROOT, '.svnignore')
# Ensure the svnignore file exists
#
raise "File #{svnignore_path} does not exist!" unless File.exists?(svnignore_path)
# Stores the parsed ignore patters
#
ignores = Hash.new { |hash, key| hash[key] = [] }
# Read the patterns from the svn ignore file
#
patterns = File.read(svnignore_path).split("\n")
patterns.each do |pattern|
# Figure out paths
#
path = File.join RAILS_ROOT, pattern
parent = File.dirname(path)
relative_path = parent == RAILS_ROOT ? '.' : File.join(parent.gsub(File.join(RAILS_ROOT, ''), ''), '')
# Check for ** in path (recursive ignore)
#
if relative_path =~ /\*\*\//
relative_path.gsub!(/\*\*\/.*$/, '')
relative_path = '.' if relative_path.blank?
recursive = '-R '
else
recursive = ''
end
ignores[relative_path] << { :pattern => File.basename(pattern), :recursive => recursive }
end
# Ignore the patterns
#
ignores.sort.each do |(relative_path, patterns)|
if patterns.length == 1
options = patterns.first[:recursive]
pattern = "'#{patterns.first[:pattern].gsub(/'/, "\\\\'")}'"
else
options = '-F '
# Create a temporary file to store a newline separated list of files to ignore for this relative path
#
temp_file = Tempfile.new('svn-ignore-', RAILS_ROOT)
temp_file.print patterns.map { |configuration| configuration[:pattern] }.join("\n")
temp_file.flush
pattern = File.basename(temp_file.path)
end
command = "svn propset svn:ignore #{options}#{pattern} #{relative_path}"
# puts command
system command
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.