comparison app/controllers/repositories_controller.rb @ 511:107d36338b70 live

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