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