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