##// END OF EJS Templates
scm: code clean up RepositoriesHelper....
Toshi MARUYAMA -
r5268:f690341d9ef1
parent child
Show More
@@ -1,277 +1,277
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'iconv'
19 19
20 20 module RepositoriesHelper
21 21 def format_revision(revision)
22 22 if revision.respond_to? :format_identifier
23 23 revision.format_identifier
24 24 else
25 25 revision.to_s
26 26 end
27 27 end
28 28
29 29 def truncate_at_line_break(text, length = 255)
30 30 if text
31 31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
32 32 end
33 33 end
34 34
35 35 def render_properties(properties)
36 36 unless properties.nil? || properties.empty?
37 37 content = ''
38 38 properties.keys.sort.each do |property|
39 39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
40 40 end
41 41 content_tag('ul', content, :class => 'properties')
42 42 end
43 43 end
44 44
45 45 def render_changeset_changes
46 46 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
47 47 case change.action
48 48 when 'A'
49 49 # Detects moved/copied files
50 50 if !change.from_path.blank?
51 51 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
52 52 end
53 53 change
54 54 when 'D'
55 55 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
56 56 else
57 57 change
58 58 end
59 59 end.compact
60 60
61 61 tree = { }
62 62 changes.each do |change|
63 63 p = tree
64 64 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
65 65 path = ''
66 66 dirs.each do |dir|
67 67 path += '/' + dir
68 68 p[:s] ||= {}
69 69 p = p[:s]
70 70 p[path] ||= {}
71 71 p = p[path]
72 72 end
73 73 p[:c] = change
74 74 end
75 75
76 76 render_changes_tree(tree[:s])
77 77 end
78 78
79 79 def render_changes_tree(tree)
80 80 return '' if tree.nil?
81 81
82 82 output = ''
83 83 output << '<ul>'
84 84 tree.keys.sort.each do |file|
85 85 style = 'change'
86 86 text = File.basename(h(file))
87 87 if s = tree[file][:s]
88 88 style << ' folder'
89 89 path_param = to_path_param(@repository.relative_path(file))
90 90 text = link_to(text, :controller => 'repositories',
91 91 :action => 'show',
92 92 :id => @project,
93 93 :path => path_param,
94 94 :rev => @changeset.identifier)
95 95 output << "<li class='#{style}'>#{text}</li>"
96 96 output << render_changes_tree(s)
97 97 elsif c = tree[file][:c]
98 98 style << " change-#{c.action}"
99 99 path_param = to_path_param(@repository.relative_path(c.path))
100 100 text = link_to(text, :controller => 'repositories',
101 101 :action => 'entry',
102 102 :id => @project,
103 103 :path => path_param,
104 104 :rev => @changeset.identifier) unless c.action == 'D'
105 105 text << " - #{c.revision}" unless c.revision.blank?
106 106 text << ' (' + link_to('diff', :controller => 'repositories',
107 107 :action => 'diff',
108 108 :id => @project,
109 109 :path => path_param,
110 110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
111 111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
112 112 output << "<li class='#{style}'>#{text}</li>"
113 113 end
114 114 end
115 115 output << '</ul>'
116 116 output
117 117 end
118 118
119 119 def to_utf8(str)
120 120 return str if str.nil?
121 121 str = to_utf8_internal(str)
122 122 if str.respond_to?(:force_encoding)
123 123 str.force_encoding('UTF-8')
124 124 end
125 125 str
126 126 end
127 127
128 128 def to_utf8_internal(str)
129 129 return str if str.nil?
130 130 if str.respond_to?(:force_encoding)
131 131 str.force_encoding('ASCII-8BIT')
132 132 end
133 133 return str if str.empty?
134 134 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
135 135 if str.respond_to?(:force_encoding)
136 136 str.force_encoding('UTF-8')
137 137 end
138 138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
139 139 @encodings.each do |encoding|
140 140 begin
141 141 return Iconv.conv('UTF-8', encoding, str)
142 142 rescue Iconv::Failure
143 143 # do nothing here and try the next encoding
144 144 end
145 145 end
146 146 str = replace_invalid_utf8(str)
147 147 end
148 148 private :to_utf8_internal
149 149
150 150 def replace_invalid_utf8(str)
151 151 return str if str.nil?
152 152 if str.respond_to?(:force_encoding)
153 153 str.force_encoding('UTF-8')
154 154 if ! str.valid_encoding?
155 155 str = str.encode("US-ASCII", :invalid => :replace,
156 156 :undef => :replace, :replace => '?').encode("UTF-8")
157 157 end
158 158 else
159 159 # removes invalid UTF8 sequences
160 160 begin
161 161 str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
162 162 rescue Iconv::InvalidEncoding
163 163 # "UTF-8//IGNORE" is not supported on some OS
164 164 end
165 165 end
166 166 str
167 167 end
168 168
169 169 def repository_field_tags(form, repository)
170 170 method = repository.class.name.demodulize.underscore + "_field_tags"
171 171 if repository.is_a?(Repository) &&
172 172 respond_to?(method) && method != 'repository_field_tags'
173 173 send(method, form, repository)
174 174 end
175 175 end
176 176
177 177 def scm_select_tag(repository)
178 178 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
179 179 Redmine::Scm::Base.all.each do |scm|
180 180 if Setting.enabled_scm.include?(scm) ||
181 181 (repository && repository.class.name.demodulize == scm)
182 182 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
183 183 end
184 184 end
185 185 select_tag('repository_scm',
186 186 options_for_select(scm_options, repository.class.name.demodulize),
187 187 :disabled => (repository && !repository.new_record?),
188 188 :onchange => remote_function(
189 189 :url => {
190 190 :controller => 'repositories',
191 191 :action => 'edit',
192 192 :id => @project
193 193 },
194 194 :method => :get,
195 195 :with => "Form.serialize(this.form)")
196 196 )
197 197 end
198 198
199 199 def with_leading_slash(path)
200 200 path.to_s.starts_with?('/') ? path : "/#{path}"
201 201 end
202 202
203 203 def without_leading_slash(path)
204 204 path.gsub(%r{^/+}, '')
205 205 end
206 206
207 207 def subversion_field_tags(form, repository)
208 208 content_tag('p', form.text_field(:url, :size => 60, :required => true,
209 209 :disabled => (repository && !repository.root_url.blank?)) +
210 210 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
211 211 content_tag('p', form.text_field(:login, :size => 30)) +
212 212 content_tag('p', form.password_field(
213 213 :password, :size => 30, :name => 'ignore',
214 214 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
215 215 :onfocus => "this.value=''; this.name='repository[password]';",
216 216 :onchange => "this.name='repository[password]';"))
217 217 end
218 218
219 219 def darcs_field_tags(form, repository)
220 220 content_tag('p', form.text_field(:url, :label => 'Root directory',
221 221 :size => 60, :required => true,
222 222 :disabled => (repository && !repository.new_record?))) +
223 223 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
224 224 :label => 'Commit messages encoding', :required => true))
225 225 end
226 226
227 227 def mercurial_field_tags(form, repository)
228 228 content_tag('p', form.text_field(:url, :label => 'Root directory',
229 229 :size => 60, :required => true,
230 230 :disabled => (repository && !repository.root_url.blank?)) +
231 231 '<br />Local repository (e.g. /hgrepo, c:\hgrepo)' ) +
232 232 content_tag('p', form.select(
233 233 :path_encoding, [nil] + Setting::ENCODINGS,
234 234 :label => 'Path encoding') +
235 235 '<br />Default: UTF-8')
236 236 end
237 237
238 238 def git_field_tags(form, repository)
239 239 content_tag('p', form.text_field(:url, :label => 'Path to repository',
240 240 :size => 60, :required => true,
241 241 :disabled => (repository && !repository.root_url.blank?)) +
242 242 '<br />Bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
243 243 content_tag('p', form.select(
244 244 :path_encoding, [nil] + Setting::ENCODINGS,
245 245 :label => 'Path encoding') +
246 246 '<br />Default: UTF-8')
247 247 end
248 248
249 249 def cvs_field_tags(form, repository)
250 content_tag('p', form.text_field(:root_url,
251 :label => 'CVSROOT', :size => 60, :required => true,
252 :disabled => !repository.new_record?)) +
253 content_tag('p', form.text_field(:url, :label => 'Module',
254 :size => 30, :required => true,
255 :disabled => !repository.new_record?)) +
256 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
257 :label => 'Commit messages encoding', :required => true))
250 content_tag('p', form.text_field(:root_url,
251 :label => 'CVSROOT', :size => 60, :required => true,
252 :disabled => !repository.new_record?)) +
253 content_tag('p', form.text_field(:url, :label => 'Module',
254 :size => 30, :required => true,
255 :disabled => !repository.new_record?)) +
256 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
257 :label => 'Commit messages encoding', :required => true))
258 258 end
259 259
260 260 def bazaar_field_tags(form, repository)
261 content_tag('p', form.text_field(:url, :label => 'Root directory',
262 :size => 60, :required => true,
263 :disabled => (repository && !repository.new_record?))) +
264 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
265 :label => 'Commit messages encoding', :required => true))
261 content_tag('p', form.text_field(:url, :label => 'Root directory',
262 :size => 60, :required => true,
263 :disabled => (repository && !repository.new_record?))) +
264 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
265 :label => 'Commit messages encoding', :required => true))
266 266 end
267 267
268 268 def filesystem_field_tags(form, repository)
269 269 content_tag('p', form.text_field(:url, :label => 'Root directory',
270 270 :size => 60, :required => true,
271 271 :disabled => (repository && !repository.root_url.blank?))) +
272 272 content_tag('p', form.select(
273 273 :path_encoding, [nil] + Setting::ENCODINGS,
274 274 :label => 'Path encoding') +
275 275 '<br />Default: UTF-8')
276 276 end
277 277 end
General Comments 0
You need to be logged in to leave comments. Login now