comparison app/helpers/repositories_helper.rb @ 514:7eba09d624db live

Merge
author Chris Cannam
date Thu, 14 Jul 2011 10:50:53 +0100
parents 350acce374a2
children 496b3cfa2f36
comparison
equal deleted inserted replaced
512:b9aebdd7dd40 514:7eba09d624db
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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 require 'redmine/codeset_util'
19 20
20 module RepositoriesHelper 21 module RepositoriesHelper
21 # truncate rev to 8 chars if it's quite long
22 def truncate_long_revision_name(rev)
23 rev.to_s.size <= 12 ? rev.to_s : rev.to_s[0, 8]
24 end
25 private :truncate_long_revision_name
26
27 def format_revision(revision) 22 def format_revision(revision)
28 if [:identifier, :revision, :scmid].all? { |e| revision.respond_to? e } 23 if revision.respond_to? :format_identifier
29 if revision.scmid and revision.revision != revision.scmid and /[^\d]/ !~ revision.revision 24 revision.format_identifier
30 "#{revision.revision}:#{revision.scmid}" # number:hashid
31 else
32 truncate_long_revision_name(revision.identifier)
33 end
34 else 25 else
35 truncate_long_revision_name(revision) 26 revision.to_s
36 end 27 end
37 end 28 end
38 module_function :format_revision # callable as RepositoriesHelper.format_revision 29
39
40 def truncate_at_line_break(text, length = 255) 30 def truncate_at_line_break(text, length = 255)
41 if text 31 if text
42 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...') 32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
43 end 33 end
44 end 34 end
45 35
46 def render_properties(properties) 36 def render_properties(properties)
47 unless properties.nil? || properties.empty? 37 unless properties.nil? || properties.empty?
48 content = '' 38 content = ''
49 properties.keys.sort.each do |property| 39 properties.keys.sort.each do |property|
50 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>") 40 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
51 end 41 end
52 content_tag('ul', content, :class => 'properties') 42 content_tag('ul', content, :class => 'properties')
53 end 43 end
54 end 44 end
55 45
56 def render_changeset_changes 46 def render_changeset_changes
57 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change| 47 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
58 case change.action 48 case change.action
59 when 'A' 49 when 'A'
60 # Detects moved/copied files 50 # Detects moved/copied files
61 if !change.from_path.blank? 51 if !change.from_path.blank?
62 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C' 52 change.action =
53 @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
63 end 54 end
64 change 55 change
65 when 'D' 56 when 'D'
66 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change 57 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
67 else 58 else
68 change 59 change
69 end 60 end
70 end.compact 61 end.compact
71 62
72 tree = { } 63 tree = { }
73 changes.each do |change| 64 changes.each do |change|
74 p = tree 65 p = tree
75 dirs = change.path.to_s.split('/').select {|d| !d.blank?} 66 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
76 path = '' 67 path = ''
81 p[path] ||= {} 72 p[path] ||= {}
82 p = p[path] 73 p = p[path]
83 end 74 end
84 p[:c] = change 75 p[:c] = change
85 end 76 end
86
87 render_changes_tree(tree[:s]) 77 render_changes_tree(tree[:s])
88 end 78 end
89 79
90 def render_changes_tree(tree) 80 def render_changes_tree(tree)
91 return '' if tree.nil? 81 return '' if tree.nil?
92
93 output = '' 82 output = ''
94 output << '<ul>' 83 output << '<ul>'
95 tree.keys.sort.each do |file| 84 tree.keys.sort.each do |file|
96 style = 'change' 85 style = 'change'
97 text = File.basename(h(file)) 86 text = File.basename(h(file))
124 end 113 end
125 end 114 end
126 output << '</ul>' 115 output << '</ul>'
127 output 116 output
128 end 117 end
129 118
130 def to_utf8(str) 119 def to_utf8(str)
120 return str if str.nil?
121 str = to_utf8_internal(str)
122 if str.respond_to?(:force_encoding)
123 str.force_encoding('UTF-8')
124 end
125 str
126 end
127
128 def to_utf8_internal(str)
129 return str if str.nil?
130 if str.respond_to?(:force_encoding)
131 str.force_encoding('ASCII-8BIT')
132 end
133 return str if str.empty?
131 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)
136 str.force_encoding('UTF-8')
137 end
132 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip) 138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
133 @encodings.each do |encoding| 139 @encodings.each do |encoding|
134 begin 140 begin
135 return Iconv.conv('UTF-8', encoding, str) 141 return Iconv.conv('UTF-8', encoding, str)
136 rescue Iconv::Failure 142 rescue Iconv::Failure
137 # do nothing here and try the next encoding 143 # do nothing here and try the next encoding
138 end 144 end
139 end 145 end
140 str 146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
141 end 147 end
142 148 private :to_utf8_internal
143 def repository_field_tags(form, repository) 149
150 def repository_field_tags(form, repository)
144 method = repository.class.name.demodulize.underscore + "_field_tags" 151 method = repository.class.name.demodulize.underscore + "_field_tags"
145 send(method, form, repository) if repository.is_a?(Repository) && respond_to?(method) && method != 'repository_field_tags' 152 if repository.is_a?(Repository) &&
146 end 153 respond_to?(method) && method != 'repository_field_tags'
147 154 send(method, form, repository)
155 end
156 end
157
148 def scm_select_tag(repository) 158 def scm_select_tag(repository)
149 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']] 159 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
150 Redmine::Scm::Base.all.each do |scm| 160 Redmine::Scm::Base.all.each do |scm|
151 scm_options << ["Repository::#{scm}".constantize.scm_name, scm] if Setting.enabled_scm.include?(scm) || (repository && repository.class.name.demodulize == scm) 161 if Setting.enabled_scm.include?(scm) ||
152 end 162 (repository && repository.class.name.demodulize == scm)
153 163 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
164 end
165 end
154 select_tag('repository_scm', 166 select_tag('repository_scm',
155 options_for_select(scm_options, repository.class.name.demodulize), 167 options_for_select(scm_options, repository.class.name.demodulize),
156 :disabled => (repository && !repository.new_record?), 168 :disabled => (repository && !repository.new_record?),
157 :onchange => remote_function(:url => { :controller => 'repositories', :action => 'edit', :id => @project }, :method => :get, :with => "Form.serialize(this.form)") 169 :onchange => remote_function(
158 ) 170 :url => {
159 end 171 :controller => 'repositories',
160 172 :action => 'edit',
173 :id => @project
174 },
175 :method => :get,
176 :with => "Form.serialize(this.form)")
177 )
178 end
179
161 def with_leading_slash(path) 180 def with_leading_slash(path)
162 path.to_s.starts_with?('/') ? path : "/#{path}" 181 path.to_s.starts_with?('/') ? path : "/#{path}"
163 end 182 end
164 183
165 def without_leading_slash(path) 184 def without_leading_slash(path)
166 path.gsub(%r{^/+}, '') 185 path.gsub(%r{^/+}, '')
167 end 186 end
168 187
169 def subversion_field_tags(form, repository) 188 def subversion_field_tags(form, repository)
170 content_tag('p', form.text_field(:url, :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)) + 189 content_tag('p', form.text_field(:url, :size => 60, :required => true,
190 :disabled => (repository && !repository.root_url.blank?)) +
171 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') + 191 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
172 content_tag('p', form.text_field(:login, :size => 30)) + 192 content_tag('p', form.text_field(:login, :size => 30)) +
173 content_tag('p', form.password_field(:password, :size => 30, :name => 'ignore', 193 content_tag('p', form.password_field(
174 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)), 194 :password, :size => 30, :name => 'ignore',
175 :onfocus => "this.value=''; this.name='repository[password]';", 195 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
176 :onchange => "this.name='repository[password]';")) 196 :onfocus => "this.value=''; this.name='repository[password]';",
197 :onchange => "this.name='repository[password]';"))
177 end 198 end
178 199
179 def darcs_field_tags(form, repository) 200 def darcs_field_tags(form, repository)
180 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?))) 201 content_tag('p', form.text_field(
181 end 202 :url, :label => l(:field_path_to_repository),
182 203 :size => 60, :required => true,
204 :disabled => (repository && !repository.new_record?))) +
205 content_tag('p', form.select(
206 :log_encoding, [nil] + Setting::ENCODINGS,
207 :label => l(:field_commit_logs_encoding), :required => true))
208 end
209
183 def mercurial_field_tags(form, repository) 210 def mercurial_field_tags(form, repository)
184 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => true)) 211 content_tag('p', form.text_field(
185 # (repository && !repository.root_url.blank?))) 212 :url, :label => l(:field_path_to_repository),
213 :size => 60, :required => true,
214 :disabled => (repository && !repository.root_url.blank?)
215 ) +
216 '<br />' + l(:text_mercurial_repository_note)) +
217 content_tag('p', form.select(
218 :path_encoding, [nil] + Setting::ENCODINGS,
219 :label => l(:field_scm_path_encoding)
220 ) +
221 '<br />' + l(:text_scm_path_encoding_note))
186 end 222 end
187 223
188 def git_field_tags(form, repository) 224 def git_field_tags(form, repository)
189 content_tag('p', form.text_field(:url, :label => 'Path to .git directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?))) 225 content_tag('p', form.text_field(
226 :url, :label => l(:field_path_to_repository),
227 :size => 60, :required => true,
228 :disabled => (repository && !repository.root_url.blank?)
229 ) +
230 '<br />' + l(:text_git_repository_note)) +
231 content_tag('p', form.select(
232 :path_encoding, [nil] + Setting::ENCODINGS,
233 :label => l(:field_scm_path_encoding)
234 ) +
235 '<br />' + l(:text_scm_path_encoding_note)) +
236 content_tag('p', form.check_box(
237 :extra_report_last_commit,
238 :label => l(:label_git_report_last_commit)
239 ))
190 end 240 end
191 241
192 def cvs_field_tags(form, repository) 242 def cvs_field_tags(form, repository)
193 content_tag('p', form.text_field(:root_url, :label => 'CVSROOT', :size => 60, :required => true, :disabled => !repository.new_record?)) + 243 content_tag('p', form.text_field(
194 content_tag('p', form.text_field(:url, :label => 'Module', :size => 30, :required => true, :disabled => !repository.new_record?)) 244 :root_url,
245 :label => l(:field_cvsroot),
246 :size => 60, :required => true,
247 :disabled => !repository.new_record?)) +
248 content_tag('p', form.text_field(
249 :url,
250 :label => l(:field_cvs_module),
251 :size => 30, :required => true,
252 :disabled => !repository.new_record?)) +
253 content_tag('p', form.select(
254 :log_encoding, [nil] + Setting::ENCODINGS,
255 :label => l(:field_commit_logs_encoding), :required => true)) +
256 content_tag('p', form.select(
257 :path_encoding, [nil] + Setting::ENCODINGS,
258 :label => l(:field_scm_path_encoding)
259 ) +
260 '<br />' + l(:text_scm_path_encoding_note))
195 end 261 end
196 262
197 def bazaar_field_tags(form, repository) 263 def bazaar_field_tags(form, repository)
198 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?))) 264 content_tag('p', form.text_field(
199 end 265 :url, :label => l(:field_path_to_repository),
200 266 :size => 60, :required => true,
267 :disabled => (repository && !repository.new_record?))) +
268 content_tag('p', form.select(
269 :log_encoding, [nil] + Setting::ENCODINGS,
270 :label => l(:field_commit_logs_encoding), :required => true))
271 end
272
201 def filesystem_field_tags(form, repository) 273 def filesystem_field_tags(form, repository)
202 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?))) 274 content_tag('p', form.text_field(
275 :url, :label => l(:field_root_directory),
276 :size => 60, :required => true,
277 :disabled => (repository && !repository.root_url.blank?))) +
278 content_tag('p', form.select(
279 :path_encoding, [nil] + Setting::ENCODINGS,
280 :label => l(:field_scm_path_encoding)
281 ) +
282 '<br />' + l(:text_scm_path_encoding_note))
203 end 283 end
204 end 284 end