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