1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 require 'redmine/codeset_util'
21 module RepositoriesHelper
22 def format_revision(revision)
23 if revision.respond_to? :format_identifier
24 revision.format_identifier
30 def truncate_at_line_break(text, length = 255)
32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
36 def render_properties(properties)
37 unless properties.nil? || properties.empty?
39 properties.keys.sort.each do |property|
40 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
42 content_tag('ul', content, :class => 'properties')
46 def render_changeset_changes
47 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
50 # Detects moved/copied files
51 if !change.from_path.blank?
53 @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
57 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
64 changes.each do |change|
66 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
77 render_changes_tree(tree[:s])
80 def render_changes_tree(tree)
81 return '' if tree.nil?
84 tree.keys.sort.each do |file|
86 text = File.basename(h(file))
89 path_param = to_path_param(@repository.relative_path(file))
90 text = link_to(text, :controller => 'repositories',
94 :rev => @changeset.identifier)
95 output << "<li class='#{style}'>#{text}</li>"
96 output << render_changes_tree(s)
97 elsif c = tree[file][:c]
98 style << " change-#{c.action}"
99 path_param = to_path_param(@repository.relative_path(c.path))
100 text = link_to(text, :controller => 'repositories',
104 :rev => @changeset.identifier) unless c.action == 'D'
105 text << " - #{c.revision}" unless c.revision.blank?
106 text << ' (' + link_to('diff', :controller => 'repositories',
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
112 output << "<li class='#{style}'>#{text}</li>"
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')
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')
133 return str if str.empty?
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')
138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
139 @encodings.each do |encoding|
141 return Iconv.conv('UTF-8', encoding, str)
142 rescue Iconv::Failure
143 # do nothing here and try the next encoding
146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
148 private :to_utf8_internal
150 def repository_field_tags(form, repository)
151 method = repository.class.name.demodulize.underscore + "_field_tags"
152 if repository.is_a?(Repository) &&
153 respond_to?(method) && method != 'repository_field_tags'
154 send(method, form, repository)
158 def scm_select_tag(repository)
159 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
160 Redmine::Scm::Base.all.each do |scm|
161 if Setting.enabled_scm.include?(scm) ||
162 (repository && repository.class.name.demodulize == scm)
163 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
166 select_tag('repository_scm',
167 options_for_select(scm_options, repository.class.name.demodulize),
168 :disabled => (repository && !repository.new_record?),
169 :onchange => remote_function(
171 :controller => 'repositories',
176 :with => "Form.serialize(this.form)")
180 def with_leading_slash(path)
181 path.to_s.starts_with?('/') ? path : "/#{path}"
184 def without_leading_slash(path)
185 path.gsub(%r{^/+}, '')
188 def subversion_field_tags(form, repository)
189 content_tag('p', form.text_field(:url, :size => 60, :required => true,
190 :disabled => (repository && !repository.root_url.blank?)) +
191 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
192 content_tag('p', form.text_field(:login, :size => 30)) +
193 content_tag('p', form.password_field(
194 :password, :size => 30, :name => 'ignore',
195 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
196 :onfocus => "this.value=''; this.name='repository[password]';",
197 :onchange => "this.name='repository[password]';"))
200 def darcs_field_tags(form, repository)
201 content_tag('p', form.text_field(
202 :url, :label => l("field_path_to_repository"),
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))
210 def mercurial_field_tags(form, repository)
211 content_tag('p', form.text_field(
212 :url, :label => l("field_path_to_repository"),
213 :size => 60, :required => true,
214 :disabled => (repository && !repository.root_url.blank?)
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")
221 '<br />' + l("text_scm_path_encoding_note"))
224 def git_field_tags(form, repository)
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?)
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")
235 '<br />' + l("text_scm_path_encoding_note"))
238 def cvs_field_tags(form, repository)
239 content_tag('p', form.text_field(
241 :label => l("field_cvsroot"),
242 :size => 60, :required => true,
243 :disabled => !repository.new_record?)) +
244 content_tag('p', form.text_field(
246 :label => l("field_cvs_module"),
247 :size => 30, :required => true,
248 :disabled => !repository.new_record?)) +
249 content_tag('p', form.select(
250 :log_encoding, [nil] + Setting::ENCODINGS,
251 :label => l("field_commit_logs_encoding"), :required => true)) +
252 content_tag('p', form.select(
253 :path_encoding, [nil] + Setting::ENCODINGS,
254 :label => l("field_scm_path_encoding")
256 '<br />' + l("text_scm_path_encoding_note"))
259 def bazaar_field_tags(form, repository)
260 content_tag('p', form.text_field(
261 :url, :label => l("field_path_to_repository"),
262 :size => 60, :required => true,
263 :disabled => (repository && !repository.new_record?))) +
264 content_tag('p', form.select(
265 :log_encoding, [nil] + Setting::ENCODINGS,
266 :label => l("field_commit_logs_encoding"), :required => true))
269 def filesystem_field_tags(form, repository)
270 content_tag('p', form.text_field(
271 :url, :label => l("field_root_directory"),
272 :size => 60, :required => true,
273 :disabled => (repository && !repository.root_url.blank?))) +
274 content_tag('p', form.select(
275 :path_encoding, [nil] + Setting::ENCODINGS,
276 :label => l("field_scm_path_encoding")
278 '<br />' + l("text_scm_path_encoding_note"))