comparison app/controllers/.svn/text-base/repositories_controller.rb.svn-base @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children af80e5618e9b 8661b858af72
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require 'SVG/Graph/Bar'
19 require 'SVG/Graph/BarHorizontal'
20 require 'digest/sha1'
21
22 class ChangesetNotFound < Exception; end
23 class InvalidRevisionParam < Exception; end
24
25 class RepositoriesController < ApplicationController
26 menu_item :repository
27 menu_item :settings, :only => :edit
28 default_search_scope :changesets
29
30 before_filter :find_repository, :except => :edit
31 before_filter :find_project, :only => :edit
32 before_filter :authorize
33 accept_key_auth :revisions
34
35 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
36
37 def edit
38 @repository = @project.repository
39 if !@repository
40 @repository = Repository.factory(params[:repository_scm])
41 @repository.project = @project if @repository
42 end
43 if request.post? && @repository
44 @repository.attributes = params[:repository]
45 @repository.save
46 end
47 render(:update) do |page|
48 page.replace_html "tab-content-repository", :partial => 'projects/settings/repository'
49 if @repository && !@project.repository
50 @project.reload #needed to reload association
51 page.replace_html "main-menu", render_main_menu(@project)
52 end
53 end
54 end
55
56 def committers
57 @committers = @repository.committers
58 @users = @project.users
59 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?
61 @users.compact!
62 @users.sort!
63 if request.post? && params[:committers].is_a?(Hash)
64 # Build a hash with repository usernames as keys and corresponding user ids as values
65 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
66 flash[:notice] = l(:notice_successful_update)
67 redirect_to :action => 'committers', :id => @project
68 end
69 end
70
71 def destroy
72 @repository.destroy
73 redirect_to :controller => 'projects', :action => 'settings', :id => @project, :tab => 'repository'
74 end
75
76 def show
77 @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty?
78
79 @entries = @repository.entries(@path, @rev)
80 if request.xhr?
81 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
82 else
83 (show_error_not_found; return) unless @entries
84 @changesets = @repository.latest_changesets(@path, @rev)
85 @properties = @repository.properties(@path, @rev)
86 render :action => 'show'
87 end
88 end
89
90 alias_method :browse, :show
91
92 def changes
93 @entry = @repository.entry(@path, @rev)
94 (show_error_not_found; return) unless @entry
95 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
96 @properties = @repository.properties(@path, @rev)
97 end
98
99 def revisions
100 @changeset_count = @repository.changesets.count
101 @changeset_pages = Paginator.new self, @changeset_count,
102 per_page_option,
103 params['page']
104 @changesets = @repository.changesets.find(:all,
105 :limit => @changeset_pages.items_per_page,
106 :offset => @changeset_pages.current.offset,
107 :include => [:user, :repository])
108
109 respond_to do |format|
110 format.html { render :layout => false if request.xhr? }
111 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
112 end
113 end
114
115 def entry
116 @entry = @repository.entry(@path, @rev)
117 (show_error_not_found; return) unless @entry
118
119 # If the entry is a dir, show the browser
120 (show; return) if @entry.is_dir?
121
122 @content = @repository.cat(@path, @rev)
123 (show_error_not_found; return) unless @content
124 if 'raw' == params[:format] || @content.is_binary_data? || (@entry.size && @entry.size > Setting.file_max_size_displayed.to_i.kilobyte)
125 # Force the download
126 send_data @content, :filename => @path.split('/').last
127 else
128 # Prevent empty lines when displaying a file with Windows style eol
129 @content.gsub!("\r\n", "\n")
130 end
131 end
132
133 def annotate
134 @entry = @repository.entry(@path, @rev)
135 (show_error_not_found; return) unless @entry
136
137 @annotate = @repository.scm.annotate(@path, @rev)
138 (render_error l(:error_scm_annotate); return) if @annotate.nil? || @annotate.empty?
139 end
140
141 def revision
142 @changeset = @repository.find_changeset_by_name(@rev)
143 raise ChangesetNotFound unless @changeset
144
145 respond_to do |format|
146 format.html
147 format.js {render :layout => false}
148 end
149 rescue ChangesetNotFound
150 show_error_not_found
151 end
152
153 def diff
154 if params[:format] == 'diff'
155 @diff = @repository.diff(@path, @rev, @rev_to)
156 (show_error_not_found; return) unless @diff
157 filename = "changeset_r#{@rev}"
158 filename << "_r#{@rev_to}" if @rev_to
159 send_data @diff.join, :filename => "#{filename}.diff",
160 :type => 'text/x-patch',
161 :disposition => 'attachment'
162 else
163 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
164 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
165
166 # Save diff type as user preference
167 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
168 User.current.pref[:diff_type] = @diff_type
169 User.current.preference.save
170 end
171
172 @cache_key = "repositories/diff/#{@repository.id}/" + Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}")
173 unless read_fragment(@cache_key)
174 @diff = @repository.diff(@path, @rev, @rev_to)
175 show_error_not_found unless @diff
176 end
177 end
178 end
179
180 def stats
181 end
182
183 def graph
184 data = nil
185 case params[:graph]
186 when "commits_per_month"
187 data = graph_commits_per_month(@repository)
188 when "commits_per_author"
189 data = graph_commits_per_author(@repository)
190 end
191 if data
192 headers["Content-Type"] = "image/svg+xml"
193 send_data(data, :type => "image/svg+xml", :disposition => "inline")
194 else
195 render_404
196 end
197 end
198
199 private
200 def find_repository
201 @project = Project.find(params[:id])
202 @repository = @project.repository
203 (render_404; return false) unless @repository
204 @path = params[:path].join('/') unless params[:path].nil?
205 @path ||= ''
206 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].strip
207 @rev_to = params[:rev_to]
208 rescue ActiveRecord::RecordNotFound
209 render_404
210 rescue InvalidRevisionParam
211 show_error_not_found
212 end
213
214 def show_error_not_found
215 render_error l(:error_scm_not_found)
216 end
217
218 # Handler for Redmine::Scm::Adapters::CommandFailed exception
219 def show_error_command_failed(exception)
220 render_error l(:error_scm_command_failed, exception.message)
221 end
222
223 def graph_commits_per_month(repository)
224 @date_to = Date.today
225 @date_from = @date_to << 11
226 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
227 commits_by_day = repository.changesets.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
228 commits_by_month = [0] * 12
229 commits_by_day.each {|c| commits_by_month[c.first.to_date.months_ago] += c.last }
230
231 changes_by_day = repository.changes.count(:all, :group => :commit_date, :conditions => ["commit_date BETWEEN ? AND ?", @date_from, @date_to])
232 changes_by_month = [0] * 12
233 changes_by_day.each {|c| changes_by_month[c.first.to_date.months_ago] += c.last }
234
235 fields = []
236 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
237
238 graph = SVG::Graph::Bar.new(
239 :height => 300,
240 :width => 800,
241 :fields => fields.reverse,
242 :stack => :side,
243 :scale_integers => true,
244 :step_x_labels => 2,
245 :show_data_values => false,
246 :graph_title => l(:label_commits_per_month),
247 :show_graph_title => true
248 )
249
250 graph.add_data(
251 :data => commits_by_month[0..11].reverse,
252 :title => l(:label_revision_plural)
253 )
254
255 graph.add_data(
256 :data => changes_by_month[0..11].reverse,
257 :title => l(:label_change_plural)
258 )
259
260 graph.burn
261 end
262
263 def graph_commits_per_author(repository)
264 commits_by_author = repository.changesets.count(:all, :group => :committer)
265 commits_by_author.to_a.sort! {|x, y| x.last <=> y.last}
266
267 changes_by_author = repository.changes.count(:all, :group => :committer)
268 h = changes_by_author.inject({}) {|o, i| o[i.first] = i.last; o}
269
270 fields = commits_by_author.collect {|r| r.first}
271 commits_data = commits_by_author.collect {|r| r.last}
272 changes_data = commits_by_author.collect {|r| h[r.first] || 0}
273
274 fields = fields + [""]*(10 - fields.length) if fields.length<10
275 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
276 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
277
278 # Remove email adress in usernames
279 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
280
281 graph = SVG::Graph::BarHorizontal.new(
282 :height => 400,
283 :width => 800,
284 :fields => fields,
285 :stack => :side,
286 :scale_integers => true,
287 :show_data_values => false,
288 :rotate_y_labels => false,
289 :graph_title => l(:label_commits_per_author),
290 :show_graph_title => true
291 )
292
293 graph.add_data(
294 :data => commits_data,
295 :title => l(:label_revision_plural)
296 )
297
298 graph.add_data(
299 :data => changes_data,
300 :title => l(:label_change_plural)
301 )
302
303 graph.burn
304 end
305
306 end
307
308 class Date
309 def months_ago(date = Date.today)
310 (date.year - self.year)*12 + (date.month - self.month)
311 end
312
313 def weeks_ago(date = Date.today)
314 (date.year - self.year)*52 + (date.cweek - self.cweek)
315 end
316 end
317
318 class String
319 def with_leading_slash
320 starts_with?('/') ? self : "/#{self}"
321 end
322 end