annotate vendor/plugins/redmine_checkout/lib/checkout/protocol.rb @ 1481:93934eec7b56 issue_540

Close obsolete branch issue_540
author Chris Cannam
date Sat, 24 Nov 2012 17:53:51 +0000
parents 020926a36823
children
rev   line source
Chris@16 1 module Checkout
Chris@16 2 class <<self
Chris@16 3 def awesome?
Chris@16 4 # Yes, this plugin is awesome!
Chris@16 5 true
Chris@16 6 end
Chris@16 7 end
Chris@16 8
Chris@16 9 class Protocol
Chris@16 10 attr_accessor :protocol, :regex, :regex_replacement, :access, :repository
Chris@16 11 attr_writer :default, :command, :fixed_url, :append_path
Chris@16 12
Chris@16 13
Chris@16 14 def initialize(args={})
Chris@16 15 args = args.dup
Chris@16 16
Chris@16 17 @protocol = args.delete :protocol
Chris@16 18 @command = args.delete :command # optional, if not set the default from the repo is used
Chris@16 19
Chris@16 20 # either a fixed url
Chris@16 21 @fixed_url = args.delete :fixed_url
Chris@16 22
Chris@16 23 # or a regex
Chris@16 24 @regex = args.delete :regex
Chris@16 25 @regex_replacement = args.delete :regex_replacement
Chris@16 26
Chris@16 27 @access = args.delete :access
Chris@16 28 @append_path = args.delete :append_path
Chris@16 29 @default = args.delete :is_default
Chris@16 30
Chris@16 31 @repository = args.delete :repository
Chris@16 32 end
Chris@16 33
Chris@16 34 def full_command(path = "")
Chris@16 35 cmd = ""
Chris@16 36 if repository.checkout_display_command?
Chris@16 37 cmd = self.command.present? ? self.command.strip + " " : ""
Chris@16 38 end
Chris@16 39 cmd + URI.escape(self.url(path))
Chris@16 40 end
Chris@16 41
Chris@16 42 def default?
Chris@16 43 @default.to_i > 0
Chris@16 44 end
Chris@16 45
Chris@16 46 def command
Chris@16 47 @command || self.repository && self.repository.checkout_default_command || ""
Chris@16 48 end
Chris@16 49
Chris@16 50 def append_path?
Chris@16 51 @append_path.to_i > 0
Chris@16 52 end
Chris@16 53
Chris@16 54 def access_rw(user)
Chris@16 55 # reduces the three available access levels 'read+write', 'read-only' and 'permission'
Chris@16 56 # to 'read+write' and 'read-only' and nil (not allowed)
Chris@16 57
Chris@16 58 @access_rw ||= {}
Chris@16 59 return @access_rw[user] if @access_rw.key? user
Chris@16 60 @access_rw[user] = case access
Chris@16 61 when 'permission'
Chris@16 62 case
Chris@16 63 when user.allowed_to?(:commit_access, repository.project) && user.allowed_to?(:browse_repository, repository.project)
Chris@16 64 'read+write'
Chris@16 65 when user.allowed_to?(:browse_repository, repository.project)
Chris@16 66 'read-only'
Chris@16 67 else
Chris@16 68 nil
Chris@16 69 end
Chris@16 70 else
Chris@16 71 @access
Chris@16 72 end
Chris@16 73 end
Chris@16 74
Chris@16 75 def access_label(user)
Chris@16 76 case access_rw(user)
Chris@16 77 when 'read+write': :label_access_read_write
Chris@16 78 when 'read-only': :label_access_read_only
Chris@16 79 end
Chris@16 80 end
Chris@16 81
Chris@16 82 def fixed_url
Chris@16 83 @fixed_url.present? ? @fixed_url : begin
Chris@16 84 if (regex.blank? || regex_replacement.blank?)
Chris@16 85 repository.url
Chris@16 86 else
Chris@16 87 repository.url.gsub(Regexp.new(regex), regex_replacement)
Chris@16 88 end
Chris@16 89 end
Chris@16 90 rescue RegexpError
Chris@16 91 repository.url || ""
Chris@16 92 end
Chris@16 93
Chris@16 94 def url(path = "")
Chris@16 95 return "" unless repository
Chris@16 96
Chris@16 97 url = fixed_url.sub(/\/+$/, "")
Chris@16 98 if repository.allow_subtree_checkout? && self.append_path? && path.present?
Chris@16 99 url = "#{url}/#{path}"
Chris@16 100 end
Chris@16 101
Chris@16 102 if repository.checkout_display_login?
Chris@16 103 begin
Chris@16 104 uri = URI.parse url
Chris@16 105 (uri.user = repository.login) if repository.login
Chris@16 106 (uri.password = repository.password) if (repository.checkout_display_login == 'password' && repository.login && repository.password)
Chris@16 107 url = uri.to_s
Chris@16 108 rescue URI::InvalidURIError
Chris@16 109 end
Chris@16 110 end
Chris@16 111 url
Chris@16 112 end
Chris@16 113 end
Chris@16 114 end