annotate lib/redmine/scm/adapters/filesystem_adapter.rb @ 872:9d7526c3a78a feature_124

Close obsolete branch feature_124
author Chris Cannam
date Sat, 02 Apr 2011 11:56:49 +0100
parents 513646585e45
children 051f544170fe
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # FileSystem adapter
Chris@0 5 # File written by Paul Rivier, at Demotera.
Chris@0 6 #
Chris@0 7 # This program is free software; you can redistribute it and/or
Chris@0 8 # modify it under the terms of the GNU General Public License
Chris@0 9 # as published by the Free Software Foundation; either version 2
Chris@0 10 # of the License, or (at your option) any later version.
Chris@0 11 #
Chris@0 12 # This program is distributed in the hope that it will be useful,
Chris@0 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 15 # GNU General Public License for more details.
Chris@0 16 #
Chris@0 17 # You should have received a copy of the GNU General Public License
Chris@0 18 # along with this program; if not, write to the Free Software
Chris@0 19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 20
Chris@0 21 require 'redmine/scm/adapters/abstract_adapter'
Chris@0 22 require 'find'
Chris@0 23
Chris@0 24 module Redmine
Chris@0 25 module Scm
Chris@0 26 module Adapters
Chris@0 27 class FilesystemAdapter < AbstractAdapter
Chris@0 28
Chris@0 29
Chris@0 30 def initialize(url, root_url=nil, login=nil, password=nil)
Chris@0 31 @url = with_trailling_slash(url)
Chris@0 32 end
Chris@0 33
Chris@0 34 def format_path_ends(path, leading=true, trailling=true)
Chris@0 35 path = leading ? with_leading_slash(path) :
Chris@0 36 without_leading_slash(path)
Chris@0 37 trailling ? with_trailling_slash(path) :
Chris@0 38 without_trailling_slash(path)
Chris@0 39 end
Chris@0 40
Chris@0 41 def info
Chris@0 42 info = Info.new({:root_url => target(),
Chris@0 43 :lastrev => nil
Chris@0 44 })
Chris@0 45 info
Chris@0 46 rescue CommandFailed
Chris@0 47 return nil
Chris@0 48 end
Chris@0 49
Chris@0 50 def entries(path="", identifier=nil)
Chris@0 51 entries = Entries.new
Chris@0 52 Dir.new(target(path)).each do |e|
Chris@0 53 relative_path = format_path_ends((format_path_ends(path,
Chris@0 54 false,
Chris@0 55 true) + e),
Chris@0 56 false,false)
Chris@0 57 target = target(relative_path)
Chris@0 58 entries <<
Chris@0 59 Entry.new({ :name => File.basename(e),
Chris@0 60 # below : list unreadable files, but dont link them.
Chris@0 61 :path => File.readable?(target) ? relative_path : "",
Chris@0 62 :kind => (File.directory?(target) ? 'dir' : 'file'),
Chris@0 63 :size => (File.directory?(target) ? nil : [File.size(target)].pack('l').unpack('L').first),
Chris@0 64 :lastrev =>
Chris@0 65 Revision.new({:time => (File.mtime(target)).localtime,
Chris@0 66 })
Chris@0 67 }) if File.exist?(target) and # paranoid test
Chris@0 68 %w{file directory}.include?(File.ftype(target)) and # avoid special types
Chris@0 69 not File.basename(e).match(/^\.+$/) # avoid . and ..
Chris@0 70 end
Chris@0 71 entries.sort_by_name
Chris@0 72 end
Chris@0 73
Chris@0 74 def cat(path, identifier=nil)
Chris@0 75 File.new(target(path), "rb").read
Chris@0 76 end
Chris@0 77
Chris@0 78 private
Chris@0 79
Chris@0 80 # AbstractAdapter::target is implicitly made to quote paths.
Chris@0 81 # Here we do not shell-out, so we do not want quotes.
Chris@0 82 def target(path=nil)
Chris@0 83 #Prevent the use of ..
Chris@0 84 if path and !path.match(/(^|\/)\.\.(\/|$)/)
Chris@0 85 return "#{self.url}#{without_leading_slash(path)}"
Chris@0 86 end
Chris@0 87 return self.url
Chris@0 88 end
Chris@0 89
Chris@0 90 end
Chris@0 91 end
Chris@0 92 end
Chris@0 93 end