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