##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r15504:d8de3106ec5f
parent child
Show More
@@ -1,433 +1,439
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 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 'SVG/Graph/Bar'
19 19 require 'SVG/Graph/BarHorizontal'
20 20 require 'digest/sha1'
21 21 require 'redmine/scm/adapters'
22 22
23 23 class ChangesetNotFound < Exception; end
24 24 class InvalidRevisionParam < Exception; end
25 25
26 26 class RepositoriesController < ApplicationController
27 27 menu_item :repository
28 28 menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers]
29 29 default_search_scope :changesets
30 30
31 31 before_action :find_project_by_project_id, :only => [:new, :create]
32 before_action :build_new_repository_from_params, :only => [:new, :create]
32 33 before_action :find_repository, :only => [:edit, :update, :destroy, :committers]
33 34 before_action :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers]
34 35 before_action :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue]
35 36 before_action :authorize
36 37 accept_rss_auth :revisions
37 38
38 39 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
39 40
40 41 def new
41 scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
42 @repository = Repository.factory(scm)
43 42 @repository.is_default = @project.repository.nil?
44 @repository.project = @project
45 43 end
46 44
47 45 def create
48 @repository = Repository.factory(params[:repository_scm])
49 @repository.safe_attributes = params[:repository]
50 @repository.project = @project
51 if request.post? && @repository.save
46 if @repository.save
52 47 redirect_to settings_project_path(@project, :tab => 'repositories')
53 48 else
54 49 render :action => 'new'
55 50 end
56 51 end
57 52
58 53 def edit
59 54 end
60 55
61 56 def update
62 57 @repository.safe_attributes = params[:repository]
63 @repository.project = @project
64 58 if @repository.save
65 59 redirect_to settings_project_path(@project, :tab => 'repositories')
66 60 else
67 61 render :action => 'edit'
68 62 end
69 63 end
70 64
71 65 def committers
72 66 @committers = @repository.committers
73 67 @users = @project.users.to_a
74 68 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
75 69 @users += User.where(:id => additional_user_ids).to_a unless additional_user_ids.empty?
76 70 @users.compact!
77 71 @users.sort!
78 72 if request.post? && params[:committers].is_a?(Hash)
79 73 # Build a hash with repository usernames as keys and corresponding user ids as values
80 74 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
81 75 flash[:notice] = l(:notice_successful_update)
82 76 redirect_to settings_project_path(@project, :tab => 'repositories')
83 77 end
84 78 end
85 79
86 80 def destroy
87 81 @repository.destroy if request.delete?
88 82 redirect_to settings_project_path(@project, :tab => 'repositories')
89 83 end
90 84
91 85 def show
92 86 @repository.fetch_changesets if @project.active? && Setting.autofetch_changesets? && @path.empty?
93 87
94 88 @entries = @repository.entries(@path, @rev)
95 89 @changeset = @repository.find_changeset_by_name(@rev)
96 90 if request.xhr?
97 91 @entries ? render(:partial => 'dir_list_content') : head(200)
98 92 else
99 93 (show_error_not_found; return) unless @entries
100 94 @changesets = @repository.latest_changesets(@path, @rev)
101 95 @properties = @repository.properties(@path, @rev)
102 96 @repositories = @project.repositories
103 97 render :action => 'show'
104 98 end
105 99 end
106 100
107 101 alias_method :browse, :show
108 102
109 103 def changes
110 104 @entry = @repository.entry(@path, @rev)
111 105 (show_error_not_found; return) unless @entry
112 106 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
113 107 @properties = @repository.properties(@path, @rev)
114 108 @changeset = @repository.find_changeset_by_name(@rev)
115 109 end
116 110
117 111 def revisions
118 112 @changeset_count = @repository.changesets.count
119 113 @changeset_pages = Paginator.new @changeset_count,
120 114 per_page_option,
121 115 params['page']
122 116 @changesets = @repository.changesets.
123 117 limit(@changeset_pages.per_page).
124 118 offset(@changeset_pages.offset).
125 119 includes(:user, :repository, :parents).
126 120 to_a
127 121
128 122 respond_to do |format|
129 123 format.html { render :layout => false if request.xhr? }
130 124 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
131 125 end
132 126 end
133 127
134 128 def raw
135 129 entry_and_raw(true)
136 130 end
137 131
138 132 def entry
139 133 entry_and_raw(false)
140 134 end
141 135
142 136 def entry_and_raw(is_raw)
143 137 @entry = @repository.entry(@path, @rev)
144 138 (show_error_not_found; return) unless @entry
145 139
146 140 # If the entry is a dir, show the browser
147 141 (show; return) if @entry.is_dir?
148 142
149 143 if is_raw
150 144 # Force the download
151 145 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
152 146 send_type = Redmine::MimeType.of(@path)
153 147 send_opt[:type] = send_type.to_s if send_type
154 148 send_opt[:disposition] = disposition(@path)
155 149 send_data @repository.cat(@path, @rev), send_opt
156 150 else
157 151 if !@entry.size || @entry.size <= Setting.file_max_size_displayed.to_i.kilobyte
158 152 content = @repository.cat(@path, @rev)
159 153 (show_error_not_found; return) unless content
160 154
161 155 if content.size <= Setting.file_max_size_displayed.to_i.kilobyte &&
162 156 is_entry_text_data?(content, @path)
163 157 # TODO: UTF-16
164 158 # Prevent empty lines when displaying a file with Windows style eol
165 159 # Is this needed? AttachmentsController simply reads file.
166 160 @content = content.gsub("\r\n", "\n")
167 161 end
168 162 end
169 163 @changeset = @repository.find_changeset_by_name(@rev)
170 164 end
171 165 end
172 166 private :entry_and_raw
173 167
174 168 def is_entry_text_data?(ent, path)
175 169 # UTF-16 contains "\x00".
176 170 # It is very strict that file contains less than 30% of ascii symbols
177 171 # in non Western Europe.
178 172 return true if Redmine::MimeType.is_type?('text', path)
179 173 # Ruby 1.8.6 has a bug of integer divisions.
180 174 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
181 175 return false if ent.is_binary_data?
182 176 true
183 177 end
184 178 private :is_entry_text_data?
185 179
186 180 def annotate
187 181 @entry = @repository.entry(@path, @rev)
188 182 (show_error_not_found; return) unless @entry
189 183
190 184 @annotate = @repository.scm.annotate(@path, @rev)
191 185 if @annotate.nil? || @annotate.empty?
192 186 @annotate = nil
193 187 @error_message = l(:error_scm_annotate)
194 188 else
195 189 ann_buf_size = 0
196 190 @annotate.lines.each do |buf|
197 191 ann_buf_size += buf.size
198 192 end
199 193 if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
200 194 @annotate = nil
201 195 @error_message = l(:error_scm_annotate_big_text_file)
202 196 end
203 197 end
204 198 @changeset = @repository.find_changeset_by_name(@rev)
205 199 end
206 200
207 201 def revision
208 202 respond_to do |format|
209 203 format.html
210 204 format.js {render :layout => false}
211 205 end
212 206 end
213 207
214 208 # Adds a related issue to a changeset
215 209 # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues
216 210 def add_related_issue
217 211 issue_id = params[:issue_id].to_s.sub(/^#/,'')
218 212 @issue = @changeset.find_referenced_issue_by_id(issue_id)
219 213 if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
220 214 @issue = nil
221 215 end
222 216
223 217 if @issue
224 218 @changeset.issues << @issue
225 219 end
226 220 end
227 221
228 222 # Removes a related issue from a changeset
229 223 # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
230 224 def remove_related_issue
231 225 @issue = Issue.visible.find_by_id(params[:issue_id])
232 226 if @issue
233 227 @changeset.issues.delete(@issue)
234 228 end
235 229 end
236 230
237 231 def diff
238 232 if params[:format] == 'diff'
239 233 @diff = @repository.diff(@path, @rev, @rev_to)
240 234 (show_error_not_found; return) unless @diff
241 235 filename = "changeset_r#{@rev}"
242 236 filename << "_r#{@rev_to}" if @rev_to
243 237 send_data @diff.join, :filename => "#{filename}.diff",
244 238 :type => 'text/x-patch',
245 239 :disposition => 'attachment'
246 240 else
247 241 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
248 242 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
249 243
250 244 # Save diff type as user preference
251 245 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
252 246 User.current.pref[:diff_type] = @diff_type
253 247 User.current.preference.save
254 248 end
255 249 @cache_key = "repositories/diff/#{@repository.id}/" +
256 250 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
257 251 unless read_fragment(@cache_key)
258 252 @diff = @repository.diff(@path, @rev, @rev_to)
259 253 show_error_not_found unless @diff
260 254 end
261 255
262 256 @changeset = @repository.find_changeset_by_name(@rev)
263 257 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
264 258 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
265 259 end
266 260 end
267 261
268 262 def stats
269 263 end
270 264
271 265 def graph
272 266 data = nil
273 267 case params[:graph]
274 268 when "commits_per_month"
275 269 data = graph_commits_per_month(@repository)
276 270 when "commits_per_author"
277 271 data = graph_commits_per_author(@repository)
278 272 end
279 273 if data
280 274 headers["Content-Type"] = "image/svg+xml"
281 275 send_data(data, :type => "image/svg+xml", :disposition => "inline")
282 276 else
283 277 render_404
284 278 end
285 279 end
286 280
287 281 private
288 282
283 def build_new_repository_from_params
284 scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
285 unless @repository = Repository.factory(scm)
286 render_404
287 return
288 end
289
290 @repository.project = @project
291 @repository.safe_attributes = params[:repository]
292 @repository
293 end
294
289 295 def find_repository
290 296 @repository = Repository.find(params[:id])
291 297 @project = @repository.project
292 298 rescue ActiveRecord::RecordNotFound
293 299 render_404
294 300 end
295 301
296 302 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
297 303
298 304 def find_project_repository
299 305 @project = Project.find(params[:id])
300 306 if params[:repository_id].present?
301 307 @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
302 308 else
303 309 @repository = @project.repository
304 310 end
305 311 (render_404; return false) unless @repository
306 312 @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
307 313 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
308 314 @rev_to = params[:rev_to]
309 315
310 316 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
311 317 if @repository.branches.blank?
312 318 raise InvalidRevisionParam
313 319 end
314 320 end
315 321 rescue ActiveRecord::RecordNotFound
316 322 render_404
317 323 rescue InvalidRevisionParam
318 324 show_error_not_found
319 325 end
320 326
321 327 def find_changeset
322 328 if @rev.present?
323 329 @changeset = @repository.find_changeset_by_name(@rev)
324 330 end
325 331 show_error_not_found unless @changeset
326 332 end
327 333
328 334 def show_error_not_found
329 335 render_error :message => l(:error_scm_not_found), :status => 404
330 336 end
331 337
332 338 # Handler for Redmine::Scm::Adapters::CommandFailed exception
333 339 def show_error_command_failed(exception)
334 340 render_error l(:error_scm_command_failed, exception.message)
335 341 end
336 342
337 343 def graph_commits_per_month(repository)
338 344 @date_to = User.current.today
339 345 @date_from = @date_to << 11
340 346 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
341 347 commits_by_day = Changeset.
342 348 where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
343 349 group(:commit_date).
344 350 count
345 351 commits_by_month = [0] * 12
346 352 commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
347 353
348 354 changes_by_day = Change.
349 355 joins(:changeset).
350 356 where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
351 357 group(:commit_date).
352 358 count
353 359 changes_by_month = [0] * 12
354 360 changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
355 361
356 362 fields = []
357 363 today = User.current.today
358 364 12.times {|m| fields << month_name(((today.month - 1 - m) % 12) + 1)}
359 365
360 366 graph = SVG::Graph::Bar.new(
361 367 :height => 300,
362 368 :width => 800,
363 369 :fields => fields.reverse,
364 370 :stack => :side,
365 371 :scale_integers => true,
366 372 :step_x_labels => 2,
367 373 :show_data_values => false,
368 374 :graph_title => l(:label_commits_per_month),
369 375 :show_graph_title => true
370 376 )
371 377
372 378 graph.add_data(
373 379 :data => commits_by_month[0..11].reverse,
374 380 :title => l(:label_revision_plural)
375 381 )
376 382
377 383 graph.add_data(
378 384 :data => changes_by_month[0..11].reverse,
379 385 :title => l(:label_change_plural)
380 386 )
381 387
382 388 graph.burn
383 389 end
384 390
385 391 def graph_commits_per_author(repository)
386 392 #data
387 393 stats = repository.stats_by_author
388 394 fields, commits_data, changes_data = [], [], []
389 395 stats.each do |name, hsh|
390 396 fields << name
391 397 commits_data << hsh[:commits_count]
392 398 changes_data << hsh[:changes_count]
393 399 end
394 400
395 401 #expand to 10 values if needed
396 402 fields = fields + [""]*(10 - fields.length) if fields.length<10
397 403 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
398 404 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
399 405
400 406 # Remove email address in usernames
401 407 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
402 408
403 409 #prepare graph
404 410 graph = SVG::Graph::BarHorizontal.new(
405 411 :height => 30 * commits_data.length,
406 412 :width => 800,
407 413 :fields => fields,
408 414 :stack => :side,
409 415 :scale_integers => true,
410 416 :show_data_values => false,
411 417 :rotate_y_labels => false,
412 418 :graph_title => l(:label_commits_per_author),
413 419 :show_graph_title => true
414 420 )
415 421 graph.add_data(
416 422 :data => commits_data,
417 423 :title => l(:label_revision_plural)
418 424 )
419 425 graph.add_data(
420 426 :data => changes_data,
421 427 :title => l(:label_change_plural)
422 428 )
423 429 graph.burn
424 430 end
425 431
426 432 def disposition(path)
427 433 if Redmine::MimeType.is_type?('image', @path) || Redmine::MimeType.of(@path) == "application/pdf"
428 434 'inline'
429 435 else
430 436 'attachment'
431 437 end
432 438 end
433 439 end
General Comments 0
You need to be logged in to leave comments. Login now