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