comparison app/controllers/.svn/text-base/repositories_controller.rb.svn-base @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 051f544170fe
children 753f1380d6bc 0c939c159af4
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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 'SVG/Graph/Bar' 18 require 'SVG/Graph/Bar'
24 24
25 class RepositoriesController < ApplicationController 25 class RepositoriesController < ApplicationController
26 menu_item :repository 26 menu_item :repository
27 menu_item :settings, :only => :edit 27 menu_item :settings, :only => :edit
28 default_search_scope :changesets 28 default_search_scope :changesets
29 29
30 before_filter :find_repository, :except => :edit 30 before_filter :find_repository, :except => :edit
31 before_filter :find_project, :only => :edit 31 before_filter :find_project, :only => :edit
32 before_filter :authorize 32 before_filter :authorize
33 accept_key_auth :revisions 33 accept_key_auth :revisions
34 34
35 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed 35 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
36 36
37 def edit 37 def edit
38 @repository = @project.repository 38 @repository = @project.repository
39 if !@repository 39 if !@repository && !params[:repository_scm].blank?
40 @repository = Repository.factory(params[:repository_scm]) 40 @repository = Repository.factory(params[:repository_scm])
41 @repository.project = @project if @repository 41 @repository.project = @project if @repository
42 end 42 end
43 if request.post? && @repository 43 if request.post? && @repository
44 @repository.attributes = params[:repository] 44 p1 = params[:repository]
45 p = {}
46 p_extra = {}
47 p1.each do |k, v|
48 if k =~ /^extra_/
49 p_extra[k] = v
50 else
51 p[k] = v
52 end
53 end
54 @repository.attributes = p
55 @repository.merge_extra_info(p_extra)
45 @repository.save 56 @repository.save
46 end 57 end
47 render(:update) do |page| 58 render(:update) do |page|
48 page.replace_html "tab-content-repository", :partial => 'projects/settings/repository' 59 page.replace_html "tab-content-repository",
60 :partial => 'projects/settings/repository'
49 if @repository && !@project.repository 61 if @repository && !@project.repository
50 @project.reload #needed to reload association 62 @project.reload # needed to reload association
51 page.replace_html "main-menu", render_main_menu(@project) 63 page.replace_html "main-menu", render_main_menu(@project)
52 end 64 end
53 end 65 end
54 end 66 end
55 67
56 def committers 68 def committers
57 @committers = @repository.committers 69 @committers = @repository.committers
58 @users = @project.users 70 @users = @project.users
59 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id) 71 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
60 @users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty? 72 @users += User.find_all_by_id(additional_user_ids) unless additional_user_ids.empty?
68 end 80 end
69 end 81 end
70 82
71 def destroy 83 def destroy
72 @repository.destroy 84 @repository.destroy
73 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository' 85 redirect_to :controller => 'projects',
86 :action => 'settings',
87 :id => @project,
88 :tab => 'repository'
74 end 89 end
75 90
76 def show 91 def show
77 @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? 92 @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
78 93
79 @entries = @repository.entries(@path, @rev) 94 @entries = @repository.entries(@path, @rev)
95 @changeset = @repository.find_changeset_by_name(@rev)
80 if request.xhr? 96 if request.xhr?
81 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) 97 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
82 else 98 else
83 (show_error_not_found; return) unless @entries 99 (show_error_not_found; return) unless @entries
84 @changesets = @repository.latest_changesets(@path, @rev) 100 @changesets = @repository.latest_changesets(@path, @rev)
120 # If the entry is a dir, show the browser 136 # If the entry is a dir, show the browser
121 (show; return) if @entry.is_dir? 137 (show; return) if @entry.is_dir?
122 138
123 @content = @repository.cat(@path, @rev) 139 @content = @repository.cat(@path, @rev)
124 (show_error_not_found; return) unless @content 140 (show_error_not_found; return) unless @content
125 if 'raw' == params[:format] || @content.is_binary_data? || 141 if 'raw' == params[:format] ||
126 (@entry.size && @entry.size > Setting.file_max_size_displayed.to_i.kilobyte) 142 (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
143 ! is_entry_text_data?(@content, @path)
127 # Force the download 144 # Force the download
128 send_data @content, :filename => filename_for_content_disposition(@path.split('/').last) 145 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
146 send_type = Redmine::MimeType.of(@path)
147 send_opt[:type] = send_type.to_s if send_type
148 send_data @content, send_opt
129 else 149 else
130 # Prevent empty lines when displaying a file with Windows style eol 150 # Prevent empty lines when displaying a file with Windows style eol
151 # TODO: UTF-16
152 # Is this needs? AttachmentsController reads file simply.
131 @content.gsub!("\r\n", "\n") 153 @content.gsub!("\r\n", "\n")
132 @changeset = @repository.find_changeset_by_name(@rev) 154 @changeset = @repository.find_changeset_by_name(@rev)
133 end 155 end
134 end 156 end
157
158 def is_entry_text_data?(ent, path)
159 # UTF-16 contains "\x00".
160 # It is very strict that file contains less than 30% of ascii symbols
161 # in non Western Europe.
162 return true if Redmine::MimeType.is_type?('text', path)
163 # Ruby 1.8.6 has a bug of integer divisions.
164 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
165 return false if ent.is_binary_data?
166 true
167 end
168 private :is_entry_text_data?
135 169
136 def annotate 170 def annotate
137 @entry = @repository.entry(@path, @rev) 171 @entry = @repository.entry(@path, @rev)
138 (show_error_not_found; return) unless @entry 172 (show_error_not_found; return) unless @entry
139 173
165 :type => 'text/x-patch', 199 :type => 'text/x-patch',
166 :disposition => 'attachment' 200 :disposition => 'attachment'
167 else 201 else
168 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' 202 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
169 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) 203 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
170 204
171 # Save diff type as user preference 205 # Save diff type as user preference
172 if User.current.logged? && @diff_type != User.current.pref[:diff_type] 206 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
173 User.current.pref[:diff_type] = @diff_type 207 User.current.pref[:diff_type] = @diff_type
174 User.current.preference.save 208 User.current.preference.save
175 end 209 end
176 210 @cache_key = "repositories/diff/#{@repository.id}/" +
177 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}") 211 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
178 unless read_fragment(@cache_key) 212 unless read_fragment(@cache_key)
179 @diff = @repository.diff(@path, @rev, @rev_to) 213 @diff = @repository.diff(@path, @rev, @rev_to)
180 show_error_not_found unless @diff 214 show_error_not_found unless @diff
181 end 215 end
182 216
202 send_data(data, :type => "image/svg+xml", :disposition => "inline") 236 send_data(data, :type => "image/svg+xml", :disposition => "inline")
203 else 237 else
204 render_404 238 render_404
205 end 239 end
206 end 240 end
207 241
208 private 242 private
209 243
210 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i 244 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
211 245
212 def find_repository 246 def find_repository
215 (render_404; return false) unless @repository 249 (render_404; return false) unless @repository
216 @path = params[:path].join('/') unless params[:path].nil? 250 @path = params[:path].join('/') unless params[:path].nil?
217 @path ||= '' 251 @path ||= ''
218 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].strip 252 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].strip
219 @rev_to = params[:rev_to] 253 @rev_to = params[:rev_to]
220 254
221 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE) 255 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
222 if @repository.branches.blank? 256 if @repository.branches.blank?
223 raise InvalidRevisionParam 257 raise InvalidRevisionParam
224 end 258 end
225 end 259 end
230 end 264 end
231 265
232 def show_error_not_found 266 def show_error_not_found
233 render_error :message => l(:error_scm_not_found), :status => 404 267 render_error :message => l(:error_scm_not_found), :status => 404
234 end 268 end
235 269
236 # Handler for Redmine::Scm::Adapters::CommandFailed exception 270 # Handler for Redmine::Scm::Adapters::CommandFailed exception
237 def show_error_command_failed(exception) 271 def show_error_command_failed(exception)
238 render_error l(:error_scm_command_failed, exception.message) 272 render_error l(:error_scm_command_failed, exception.message)
239 end 273 end
240 274
241 def graph_commits_per_month(repository) 275 def graph_commits_per_month(repository)
242 @date_to = Date.today 276 @date_to = Date.today
243 @date_from = @date_to << 11 277 @date_from = @date_to << 11
244 @date_from = Date.civil(@date_from.year, @date_from.month, 1) 278 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
245 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to]) 279 commits_by_day = repository.changesets.count(
280 :all, :group => :commit_date,
281 :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
246 commits_by_month = [0] * 12 282 commits_by_month = [0] * 12
247 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last } 283 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
248 284
249 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to]) 285 changes_by_day = repository.changes.count(
286 :all, :group => :commit_date,
287 :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
250 changes_by_month = [0] * 12 288 changes_by_month = [0] * 12
251 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last } 289 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
252 290
253 fields = [] 291 fields = []
254 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)} 292 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
255 293
256 graph = SVG::Graph::Bar.new( 294 graph = SVG::Graph::Bar.new(
257 :height => 300, 295 :height => 300,
258 :width => 800, 296 :width => 800,
259 :fields => fields.reverse, 297 :fields => fields.reverse,
260 :stack => :side, 298 :stack => :side,
262 :step_x_labels => 2, 300 :step_x_labels => 2,
263 :show_data_values => false, 301 :show_data_values => false,
264 :graph_title => l(:label_commits_per_month), 302 :graph_title => l(:label_commits_per_month),
265 :show_graph_title => true 303 :show_graph_title => true
266 ) 304 )
267 305
268 graph.add_data( 306 graph.add_data(
269 :data => commits_by_month[0..11].reverse, 307 :data => commits_by_month[0..11].reverse,
270 :title => l(:label_revision_plural) 308 :title => l(:label_revision_plural)
271 ) 309 )
272 310
273 graph.add_data( 311 graph.add_data(
274 :data => changes_by_month[0..11].reverse, 312 :data => changes_by_month[0..11].reverse,
275 :title => l(:label_change_plural) 313 :title => l(:label_change_plural)
276 ) 314 )
277 315
278 graph.burn 316 graph.burn
279 end 317 end
280 318
281 def graph_commits_per_author(repository) 319 def graph_commits_per_author(repository)
282 commits_by_author = repository.changesets.count(:all, :group => :committer) 320 commits_by_author = repository.changesets.count(:all, :group => :committer)
283 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last} 321 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
284 322
285 changes_by_author = repository.changes.count(:all, :group => :committer) 323 changes_by_author = repository.changes.count(:all, :group => :committer)
286 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o} 324 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
287 325
288 fields = commits_by_author.collect {|r| r.first} 326 fields = commits_by_author.collect {|r| r.first}
289 commits_data = commits_by_author.collect {|r| r.last} 327 commits_data = commits_by_author.collect {|r| r.last}
290 changes_data = commits_by_author.collect {|r| h[r.first] || 0} 328 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
291 329
292 fields = fields + [""]*(10 - fields.length) if fields.length<10 330 fields = fields + [""]*(10 - fields.length) if fields.length<10
293 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10 331 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
294 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10 332 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
295 333
296 # Remove email adress in usernames 334 # Remove email adress in usernames
297 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') } 335 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
298 336
299 graph = SVG::Graph::BarHorizontal.new( 337 graph = SVG::Graph::BarHorizontal.new(
300 :height => 400, 338 :height => 400,
301 :width => 800, 339 :width => 800,
302 :fields => fields, 340 :fields => fields,
303 :stack => :side, 341 :stack => :side,
305 :show_data_values => false, 343 :show_data_values => false,
306 :rotate_y_labels => false, 344 :rotate_y_labels => false,
307 :graph_title => l(:label_commits_per_author), 345 :graph_title => l(:label_commits_per_author),
308 :show_graph_title => true 346 :show_graph_title => true
309 ) 347 )
310
311 graph.add_data( 348 graph.add_data(
312 :data => commits_data, 349 :data => commits_data,
313 :title => l(:label_revision_plural) 350 :title => l(:label_revision_plural)
314 ) 351 )
315
316 graph.add_data( 352 graph.add_data(
317 :data => changes_data, 353 :data => changes_data,
318 :title => l(:label_change_plural) 354 :title => l(:label_change_plural)
319 ) 355 )
320
321 graph.burn 356 graph.burn
322 end 357 end
323
324 end 358 end
325 359
326 class Date 360 class Date
327 def months_ago(date = Date.today) 361 def months_ago(date = Date.today)
328 (date.year - self.year)*12 + (date.month - self.month) 362 (date.year - self.year)*12 + (date.month - self.month)
329 end 363 end
330 364