##// END OF EJS Templates
scm: code clean up RepositoriesHelper....
Toshi MARUYAMA -
r5386:8dcde8fc5916
parent child
Show More
@@ -1,263 +1,262
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 require 'redmine/codeset_util'
19 require 'redmine/codeset_util'
20
20
21 module RepositoriesHelper
21 module RepositoriesHelper
22 def format_revision(revision)
22 def format_revision(revision)
23 if revision.respond_to? :format_identifier
23 if revision.respond_to? :format_identifier
24 revision.format_identifier
24 revision.format_identifier
25 else
25 else
26 revision.to_s
26 revision.to_s
27 end
27 end
28 end
28 end
29
29
30 def truncate_at_line_break(text, length = 255)
30 def truncate_at_line_break(text, length = 255)
31 if text
31 if text
32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
32 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
33 end
33 end
34 end
34 end
35
35
36 def render_properties(properties)
36 def render_properties(properties)
37 unless properties.nil? || properties.empty?
37 unless properties.nil? || properties.empty?
38 content = ''
38 content = ''
39 properties.keys.sort.each do |property|
39 properties.keys.sort.each do |property|
40 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>")
41 end
41 end
42 content_tag('ul', content, :class => 'properties')
42 content_tag('ul', content, :class => 'properties')
43 end
43 end
44 end
44 end
45
45
46 def render_changeset_changes
46 def render_changeset_changes
47 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|
48 case change.action
48 case change.action
49 when 'A'
49 when 'A'
50 # Detects moved/copied files
50 # Detects moved/copied files
51 if !change.from_path.blank?
51 if !change.from_path.blank?
52 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'
53 end
54 end
54 change
55 change
55 when 'D'
56 when 'D'
56 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
57 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
57 else
58 else
58 change
59 change
59 end
60 end
60 end.compact
61 end.compact
61
62
62 tree = { }
63 tree = { }
63 changes.each do |change|
64 changes.each do |change|
64 p = tree
65 p = tree
65 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
66 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
66 path = ''
67 path = ''
67 dirs.each do |dir|
68 dirs.each do |dir|
68 path += '/' + dir
69 path += '/' + dir
69 p[:s] ||= {}
70 p[:s] ||= {}
70 p = p[:s]
71 p = p[:s]
71 p[path] ||= {}
72 p[path] ||= {}
72 p = p[path]
73 p = p[path]
73 end
74 end
74 p[:c] = change
75 p[:c] = change
75 end
76 end
76
77 render_changes_tree(tree[:s])
77 render_changes_tree(tree[:s])
78 end
78 end
79
79
80 def render_changes_tree(tree)
80 def render_changes_tree(tree)
81 return '' if tree.nil?
81 return '' if tree.nil?
82
83 output = ''
82 output = ''
84 output << '<ul>'
83 output << '<ul>'
85 tree.keys.sort.each do |file|
84 tree.keys.sort.each do |file|
86 style = 'change'
85 style = 'change'
87 text = File.basename(h(file))
86 text = File.basename(h(file))
88 if s = tree[file][:s]
87 if s = tree[file][:s]
89 style << ' folder'
88 style << ' folder'
90 path_param = to_path_param(@repository.relative_path(file))
89 path_param = to_path_param(@repository.relative_path(file))
91 text = link_to(text, :controller => 'repositories',
90 text = link_to(text, :controller => 'repositories',
92 :action => 'show',
91 :action => 'show',
93 :id => @project,
92 :id => @project,
94 :path => path_param,
93 :path => path_param,
95 :rev => @changeset.identifier)
94 :rev => @changeset.identifier)
96 output << "<li class='#{style}'>#{text}</li>"
95 output << "<li class='#{style}'>#{text}</li>"
97 output << render_changes_tree(s)
96 output << render_changes_tree(s)
98 elsif c = tree[file][:c]
97 elsif c = tree[file][:c]
99 style << " change-#{c.action}"
98 style << " change-#{c.action}"
100 path_param = to_path_param(@repository.relative_path(c.path))
99 path_param = to_path_param(@repository.relative_path(c.path))
101 text = link_to(text, :controller => 'repositories',
100 text = link_to(text, :controller => 'repositories',
102 :action => 'entry',
101 :action => 'entry',
103 :id => @project,
102 :id => @project,
104 :path => path_param,
103 :path => path_param,
105 :rev => @changeset.identifier) unless c.action == 'D'
104 :rev => @changeset.identifier) unless c.action == 'D'
106 text << " - #{c.revision}" unless c.revision.blank?
105 text << " - #{c.revision}" unless c.revision.blank?
107 text << ' (' + link_to('diff', :controller => 'repositories',
106 text << ' (' + link_to('diff', :controller => 'repositories',
108 :action => 'diff',
107 :action => 'diff',
109 :id => @project,
108 :id => @project,
110 :path => path_param,
109 :path => path_param,
111 :rev => @changeset.identifier) + ') ' if c.action == 'M'
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
112 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?
113 output << "<li class='#{style}'>#{text}</li>"
112 output << "<li class='#{style}'>#{text}</li>"
114 end
113 end
115 end
114 end
116 output << '</ul>'
115 output << '</ul>'
117 output
116 output
118 end
117 end
119
118
120 def to_utf8(str)
119 def to_utf8(str)
121 return str if str.nil?
120 return str if str.nil?
122 str = to_utf8_internal(str)
121 str = to_utf8_internal(str)
123 if str.respond_to?(:force_encoding)
122 if str.respond_to?(:force_encoding)
124 str.force_encoding('UTF-8')
123 str.force_encoding('UTF-8')
125 end
124 end
126 str
125 str
127 end
126 end
128
127
129 def to_utf8_internal(str)
128 def to_utf8_internal(str)
130 return str if str.nil?
129 return str if str.nil?
131 if str.respond_to?(:force_encoding)
130 if str.respond_to?(:force_encoding)
132 str.force_encoding('ASCII-8BIT')
131 str.force_encoding('ASCII-8BIT')
133 end
132 end
134 return str if str.empty?
133 return str if str.empty?
135 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
136 if str.respond_to?(:force_encoding)
135 if str.respond_to?(:force_encoding)
137 str.force_encoding('UTF-8')
136 str.force_encoding('UTF-8')
138 end
137 end
139 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
138 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
140 @encodings.each do |encoding|
139 @encodings.each do |encoding|
141 begin
140 begin
142 return Iconv.conv('UTF-8', encoding, str)
141 return Iconv.conv('UTF-8', encoding, str)
143 rescue Iconv::Failure
142 rescue Iconv::Failure
144 # do nothing here and try the next encoding
143 # do nothing here and try the next encoding
145 end
144 end
146 end
145 end
147 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
146 str = Redmine::CodesetUtil.replace_invalid_utf8(str)
148 end
147 end
149 private :to_utf8_internal
148 private :to_utf8_internal
150
149
151 def repository_field_tags(form, repository)
150 def repository_field_tags(form, repository)
152 method = repository.class.name.demodulize.underscore + "_field_tags"
151 method = repository.class.name.demodulize.underscore + "_field_tags"
153 if repository.is_a?(Repository) &&
152 if repository.is_a?(Repository) &&
154 respond_to?(method) && method != 'repository_field_tags'
153 respond_to?(method) && method != 'repository_field_tags'
155 send(method, form, repository)
154 send(method, form, repository)
156 end
155 end
157 end
156 end
158
157
159 def scm_select_tag(repository)
158 def scm_select_tag(repository)
160 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
159 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
161 Redmine::Scm::Base.all.each do |scm|
160 Redmine::Scm::Base.all.each do |scm|
162 if Setting.enabled_scm.include?(scm) ||
161 if Setting.enabled_scm.include?(scm) ||
163 (repository && repository.class.name.demodulize == scm)
162 (repository && repository.class.name.demodulize == scm)
164 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
163 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
165 end
164 end
166 end
165 end
167 select_tag('repository_scm',
166 select_tag('repository_scm',
168 options_for_select(scm_options, repository.class.name.demodulize),
167 options_for_select(scm_options, repository.class.name.demodulize),
169 :disabled => (repository && !repository.new_record?),
168 :disabled => (repository && !repository.new_record?),
170 :onchange => remote_function(
169 :onchange => remote_function(
171 :url => {
170 :url => {
172 :controller => 'repositories',
171 :controller => 'repositories',
173 :action => 'edit',
172 :action => 'edit',
174 :id => @project
173 :id => @project
175 },
174 },
176 :method => :get,
175 :method => :get,
177 :with => "Form.serialize(this.form)")
176 :with => "Form.serialize(this.form)")
178 )
177 )
179 end
178 end
180
179
181 def with_leading_slash(path)
180 def with_leading_slash(path)
182 path.to_s.starts_with?('/') ? path : "/#{path}"
181 path.to_s.starts_with?('/') ? path : "/#{path}"
183 end
182 end
184
183
185 def without_leading_slash(path)
184 def without_leading_slash(path)
186 path.gsub(%r{^/+}, '')
185 path.gsub(%r{^/+}, '')
187 end
186 end
188
187
189 def subversion_field_tags(form, repository)
188 def subversion_field_tags(form, repository)
190 content_tag('p', form.text_field(:url, :size => 60, :required => true,
189 content_tag('p', form.text_field(:url, :size => 60, :required => true,
191 :disabled => (repository && !repository.root_url.blank?)) +
190 :disabled => (repository && !repository.root_url.blank?)) +
192 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
191 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
193 content_tag('p', form.text_field(:login, :size => 30)) +
192 content_tag('p', form.text_field(:login, :size => 30)) +
194 content_tag('p', form.password_field(
193 content_tag('p', form.password_field(
195 :password, :size => 30, :name => 'ignore',
194 :password, :size => 30, :name => 'ignore',
196 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
195 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
197 :onfocus => "this.value=''; this.name='repository[password]';",
196 :onfocus => "this.value=''; this.name='repository[password]';",
198 :onchange => "this.name='repository[password]';"))
197 :onchange => "this.name='repository[password]';"))
199 end
198 end
200
199
201 def darcs_field_tags(form, repository)
200 def darcs_field_tags(form, repository)
202 content_tag('p', form.text_field(:url, :label => 'Root directory',
201 content_tag('p', form.text_field(:url, :label => 'Root directory',
203 :size => 60, :required => true,
202 :size => 60, :required => true,
204 :disabled => (repository && !repository.new_record?))) +
203 :disabled => (repository && !repository.new_record?))) +
205 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
204 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
206 :label => 'Commit messages encoding', :required => true))
205 :label => 'Commit messages encoding', :required => true))
207 end
206 end
208
207
209 def mercurial_field_tags(form, repository)
208 def mercurial_field_tags(form, repository)
210 content_tag('p', form.text_field(:url, :label => 'Root directory',
209 content_tag('p', form.text_field(:url, :label => 'Root directory',
211 :size => 60, :required => true,
210 :size => 60, :required => true,
212 :disabled => (repository && !repository.root_url.blank?)) +
211 :disabled => (repository && !repository.root_url.blank?)) +
213 '<br />Local repository (e.g. /hgrepo, c:\hgrepo)' ) +
212 '<br />Local repository (e.g. /hgrepo, c:\hgrepo)' ) +
214 content_tag('p', form.select(
213 content_tag('p', form.select(
215 :path_encoding, [nil] + Setting::ENCODINGS,
214 :path_encoding, [nil] + Setting::ENCODINGS,
216 :label => 'Path encoding') +
215 :label => 'Path encoding') +
217 '<br />Default: UTF-8')
216 '<br />Default: UTF-8')
218 end
217 end
219
218
220 def git_field_tags(form, repository)
219 def git_field_tags(form, repository)
221 content_tag('p', form.text_field(:url, :label => 'Path to repository',
220 content_tag('p', form.text_field(:url, :label => 'Path to repository',
222 :size => 60, :required => true,
221 :size => 60, :required => true,
223 :disabled => (repository && !repository.root_url.blank?)) +
222 :disabled => (repository && !repository.root_url.blank?)) +
224 '<br />Bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
223 '<br />Bare and local repository (e.g. /gitrepo, c:\gitrepo)') +
225 content_tag('p', form.select(
224 content_tag('p', form.select(
226 :path_encoding, [nil] + Setting::ENCODINGS,
225 :path_encoding, [nil] + Setting::ENCODINGS,
227 :label => 'Path encoding') +
226 :label => 'Path encoding') +
228 '<br />Default: UTF-8')
227 '<br />Default: UTF-8')
229 end
228 end
230
229
231 def cvs_field_tags(form, repository)
230 def cvs_field_tags(form, repository)
232 content_tag('p', form.text_field(:root_url,
231 content_tag('p', form.text_field(:root_url,
233 :label => 'CVSROOT', :size => 60, :required => true,
232 :label => 'CVSROOT', :size => 60, :required => true,
234 :disabled => !repository.new_record?)) +
233 :disabled => !repository.new_record?)) +
235 content_tag('p', form.text_field(:url, :label => 'Module',
234 content_tag('p', form.text_field(:url, :label => 'Module',
236 :size => 30, :required => true,
235 :size => 30, :required => true,
237 :disabled => !repository.new_record?)) +
236 :disabled => !repository.new_record?)) +
238 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
237 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
239 :label => 'Commit messages encoding', :required => true)) +
238 :label => 'Commit messages encoding', :required => true)) +
240 content_tag('p', form.select(
239 content_tag('p', form.select(
241 :path_encoding, [nil] + Setting::ENCODINGS,
240 :path_encoding, [nil] + Setting::ENCODINGS,
242 :label => 'Path encoding') +
241 :label => 'Path encoding') +
243 '<br />Default: UTF-8')
242 '<br />Default: UTF-8')
244 end
243 end
245
244
246 def bazaar_field_tags(form, repository)
245 def bazaar_field_tags(form, repository)
247 content_tag('p', form.text_field(:url, :label => 'Root directory',
246 content_tag('p', form.text_field(:url, :label => 'Root directory',
248 :size => 60, :required => true,
247 :size => 60, :required => true,
249 :disabled => (repository && !repository.new_record?))) +
248 :disabled => (repository && !repository.new_record?))) +
250 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
249 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
251 :label => 'Commit messages encoding', :required => true))
250 :label => 'Commit messages encoding', :required => true))
252 end
251 end
253
252
254 def filesystem_field_tags(form, repository)
253 def filesystem_field_tags(form, repository)
255 content_tag('p', form.text_field(:url, :label => 'Root directory',
254 content_tag('p', form.text_field(:url, :label => 'Root directory',
256 :size => 60, :required => true,
255 :size => 60, :required => true,
257 :disabled => (repository && !repository.root_url.blank?))) +
256 :disabled => (repository && !repository.root_url.blank?))) +
258 content_tag('p', form.select(
257 content_tag('p', form.select(
259 :path_encoding, [nil] + Setting::ENCODINGS,
258 :path_encoding, [nil] + Setting::ENCODINGS,
260 :label => 'Path encoding') +
259 :label => 'Path encoding') +
261 '<br />Default: UTF-8')
260 '<br />Default: UTF-8')
262 end
261 end
263 end
262 end
General Comments 0
You need to be logged in to leave comments. Login now