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