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